<?php
/**
 * Name: AVIF Upload Support
 * Description: Enable support for uploading AVIF image files with proper mime type handling and thumbnail display in media library
 * Category: Media
 * Icon: -
 * UseCases: [
 *   {
 *     "title": "Next-Gen Image Format",
 *     "description": "Upload AVIF images which offer superior compression and quality compared to JPEG and WebP, reducing file sizes by up to 50%."
 *   },
 *   {
 *     "title": "Modern Browser Support",
 *     "description": "Take advantage of AVIF support in modern browsers for faster page loads and better image quality."
 *   },
 *   {
 *     "title": "High-Quality Visuals",
 *     "description": "Maintain excellent image quality at smaller file sizes, perfect for photography and design-focused websites."
 *   }
 * ]
 * Added: 2025-11-04
 * Tags: avif, upload, media, image, file-type, performance, next-gen
 * WhereToFind: {
 *   "location": "Media Library",
 *   "url": "upload.php",
 *   "instructions": "Upload AVIF files - they will be accepted and displayed properly",
 *   "newTab": false
 * }
 */

// Prevent direct access
if (!defined('ABSPATH')) exit;

class SwitchboardAvifSupport {

    public function __construct() {
        add_filter('upload_mimes', [$this, 'add_avif_mime_type']);
        add_filter('wp_check_filetype_and_ext', [$this, 'fix_avif_mime_type'], 10, 5);
        add_filter('file_is_displayable_image', [$this, 'avif_is_displayable'], 10, 2);
        add_action('admin_head', [$this, 'fix_avif_display']);
    }

    /**
     * Register data locations for cleanup system
     *
     * @return array Data storage locations
     */
    public function register_data_locations() {
        return [
            'options' => [],
            'option_patterns' => [],
            'tables' => [],
            'post_meta' => [],
            'user_meta' => [],
        ];
    }

    /**
     * Add AVIF mime type to allowed uploads
     *
     * @param array $mimes Array of allowed mime types
     * @return array Modified mime types
     */
    public function add_avif_mime_type($mimes) {
        $mimes['avif'] = 'image/avif';
        return $mimes;
    }

    /**
     * Fix AVIF mime type detection
     *
     * @param array $data File data
     * @param string $file File path
     * @param string $filename File name
     * @param array $mimes Allowed mimes
     * @param string $real_mime Real mime type
     * @return array Modified file data
     */
    public function fix_avif_mime_type($data, $file, $filename, $mimes, $real_mime = '') {
        $wp_file_type = wp_check_filetype($filename, $mimes);

        if (is_array($wp_file_type) && isset($wp_file_type['ext']) && $wp_file_type['ext'] === 'avif') {
            $data['ext'] = 'avif';
            $data['type'] = 'image/avif';
        }

        return $data;
    }

    /**
     * Mark AVIF files as displayable images
     *
     * @param bool $result Whether the file is displayable
     * @param string $path File path
     * @return bool Modified result
     */
    public function avif_is_displayable($result, $path) {
        $info = @getimagesize($path);
        if (!empty($info) && $info['mime'] === 'image/avif') {
            $result = true;
        }
        return $result;
    }

    /**
     * Add CSS to properly display AVIF images in media library
     */
    public function fix_avif_display() {
        ?>
        <style>
            img[src$=".avif"] {
                width: 100%;
                height: auto;
            }
            .media-icon img[src$=".avif"] {
                width: 100% !important;
                height: auto !important;
            }
            .attachment-266x266 img[src$=".avif"],
            .thumbnail img[src$=".avif"] {
                width: 100% !important;
                height: auto !important;
            }
        </style>
        <?php
    }
}

// Initialize and register with data system
$switchboard_instance = new SwitchboardAvifSupport();
Switchboard_Data_Registry::get_instance()->auto_register($switchboard_instance, __FILE__);
