/* 
Theme Name:		Hello Elementor Child
Theme URI:		https://elementor.com/
Description:	Hello Elementor Child is a child theme of Hello Elementor, created by Elementor team
Author:			Elementor Team
Author URI:		https://elementor.com/
Template:		hello-elementor
Version:		1.0.0
Text Domain:	hello-elementor-child
Tags:           flexible-header, custom-colors, custom-menu, custom-logo, editor-style, featured-images, rtl-language-support, threaded-comments, translation-ready
*/

/*
    Add your custom styles here
*/


/**
 * מאפשר העלאת SVG
 */
function cc_mime_types( $mimes ) {
    $mimes['svg'] = 'image/svg+xml';
    return $mimes;
}
add_filter( 'upload_mimes', 'cc_mime_types' );


/**
 * תיקון תצוגת SVG בספריית המדיה
 */
function fix_svg_display() {
    echo '
    <style>
        .attachment-266x266 img,
        .thumbnail img {
            width: 100% !important;
            height: auto !important;
        }
    </style>';
}
add_action( 'admin_head', 'fix_svg_display' );


/**
 * חיטוי בטוח ל-SVG
 */
function sanitize_svg_content( $svg ) {
    $dom = new DOMDocument();

    libxml_use_internal_errors(true);

    // טעינה בטוחה ללא ישויות חיצוניות
    $dom->loadXML($svg, LIBXML_NONET);

    libxml_clear_errors();

    $svg_element = $dom->getElementsByTagName('svg')->item(0);

    if (!$svg_element) {
        return $svg;
    }

    return $dom->saveXML($svg_element);
}


/**
 * כתיבה מחדש של SVG לאחר העלאה
 */
add_filter( 'wp_handle_upload', function( $file ) {

    if ( isset($file['type']) && $file['type'] === 'image/svg+xml' ) {

        $svg_raw  = file_get_contents( $file['file'] );
        $clean_svg = sanitize_svg_content( $svg_raw );

        file_put_contents( $file['file'], $clean_svg );
    }

    return $file;
});
