/*
 Theme Name:   custom-gluttonous-sloth
 Theme URI:    http://example.com/customgluttonoussloth-child/
 Description:  Thème enfant de twentytwentyfive
 Author:       Votre Nom
 Author URI:   http://example.com
 Template:     twentytwentyfive
 Version:      1.0.0
*/


/**
 * Enable support for SVG and WebP file uploads in WordPress.
 * 
 * This code snippet allows the upload of SVG and WebP files through the WordPress media uploader.
 * SVG files are sanitized to prevent security risks, and custom CSS is added to ensure SVG and WebP
 * thumbnails are displayed correctly in the media library.
 */

// 1. Allow SVG and WebP Uploads
function my_custom_mime_types($mimes) {
    // Add SVG support
    $mimes['svg'] = 'image/svg+xml';
    // Add WebP support
    $mimes['webp'] = 'image/webp';
    return $mimes;
}
// Hook into 'upload_mimes' to enable custom mime types
add_filter('upload_mimes', 'my_custom_mime_types');

// 2. Sanitize SVG Files
function my_sanitize_svg($data, $file, $filename, $mimes) {
    // Check if the file is an SVG
    $wp_filetype = wp_check_filetype($filename, $mimes);
    if ($wp_filetype['ext'] === 'svg') {
        // If it's an SVG, set the correct extension and mime type
        $data['ext'] = 'svg';
        $data['type'] = 'image/svg+xml';
    }
    return $data;
}
// Hook into 'wp_check_filetype_and_ext' to sanitize SVG files
add_filter('wp_check_filetype_and_ext', 'my_sanitize_svg', 10, 4);

// 3. Display SVG and WebP in the Media Library (Optional)
function my_fix_svg_webp() {
    // CSS to fix the display of SVG and WebP thumbnails in the media library
    echo '<style>
        .attachment-266x266, .thumbnail img {
            width: 100% !important;
            height: auto !important;
        }
    </style>';
}
// Hook into 'admin_head' to add custom CSS to the admin panel
add_action('admin_head', 'my_fix_svg_webp');
