<?php
/**
 * AR Display
 * https://augmentedrealityplugins.com
**/

if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

/******* Activate plugin *******/
register_activation_hook(__FILE__, 'ar_plugin_activation');
if (!function_exists('ar_plugin_activation')){
    function ar_plugin_activation() {
            wp_schedule_event( time(), 'daily', 'ar_cron' );
            ar_cron();
    }
}

/******* Deactivate plugin *******/
register_deactivation_hook(__FILE__, 'ar_plugin_deactivation');
if (!function_exists('ar_plugin_deactivation')){
    function ar_plugin_deactivation() {
        wp_clear_scheduled_hook( 'ar_cron' );
    }
}

/******* Enqueue Js ***********/

/* Called from Model Viewer */
if ( ! function_exists( 'ar_get_analytics_script_version' ) ) {
	function ar_get_analytics_script_version() {
		static $version = null;

		if ( null !== $version ) {
			return $version;
		}

		global $ar_version;

		$version     = $ar_version;
		$script_file = dirname( __FILE__, 2 ) . '/assets/js/ar-analytics.js';

		if ( file_exists( $script_file ) ) {
			$mtime = filemtime( $script_file );
			if ( $mtime ) {
				$version = $mtime;
			}
		}

		return $version;
	}
}

if (!function_exists('ar_advance_register_script')){
    function ar_advance_register_script() {
        global $ar_plugin_root_url, $ar_version;
        // Enqueue necessary styles and scripts

        // WP REST API utils
        wp_enqueue_script('wp-api');

        wp_enqueue_script('iris', admin_url('js/iris.min.js'), array('jquery-ui-draggable', 'jquery-ui-slider', 'jquery-touch-punch'), '1.0.0', true);
        wp_enqueue_script('ar_admin', $ar_plugin_root_url.'assets/js/ar-admin.js', array('jquery','iris'), $ar_version, false);

        wp_script_add_data('ar_admin', 'async', true);

        if ( ! wp_script_is( 'ar-analytics', 'registered' ) ) {
            wp_register_script(
                'ar-analytics',
                $ar_plugin_root_url . 'assets/js/ar-analytics.js',
                array(),
                ar_get_analytics_script_version(),
                true
            );
        }
        
        wp_enqueue_script('ar_model', $ar_plugin_root_url.'assets/js/ar-model.js', array('jquery', 'ar-analytics'), $ar_version, false);
        wp_script_add_data('ar_model', 'async', true);

        wp_enqueue_script('ar_copy_to_clipboard', $ar_plugin_root_url.'assets/js/ar-display.js', array('jquery'), $ar_version, true);
        wp_script_add_data('ar_copy_to_clipboard', 'async', true);

        // AI Admin Assets
        wp_enqueue_style('ardisplay-ai-admin', $ar_plugin_root_url.'assets/admin/ardisplay_ai/ardisplay-ai-admin.css', array(), $ar_version);
        wp_enqueue_script('ardisplay-ai-admin', $ar_plugin_root_url.'assets/admin/ardisplay_ai/ardisplay-ai-admin.js', array('jquery', 'wp-api', 'media-editor', 'media-views'), $ar_version, true);
    }
}


if ( ! function_exists( 'ar_wp_enqueue_frontend_analytics' ) ) {
    add_action( 'wp_enqueue_scripts', 'ar_wp_enqueue_frontend_analytics', 20 );

    function ar_wp_enqueue_frontend_analytics() {
        if ( is_admin() ) {
            return;
        }

        $settings = ar_get_analytics_localized_settings();
        if ( empty( $settings ) || empty( $settings['enabled'] ) ) {
            return;
        }

        global $ar_plugin_root_url, $ar_version;

        if ( ! wp_script_is( 'ar-analytics', 'registered' ) ) {
            wp_register_script(
                'ar-analytics',
                $ar_plugin_root_url . 'assets/js/ar-analytics.js',
                array(),
                ar_get_analytics_script_version(),
                true
            );
        }

        wp_enqueue_script( 'ar-analytics' );

        $settings['context'] = ar_get_analytics_context();

        wp_add_inline_script(
            'ar-analytics',
            'window.ARAnalyticsConfig = window.ARAnalyticsConfig || []; window.ARAnalyticsConfig.push(' . wp_json_encode( $settings ) . ');',
            'before'
        );
    }
}


/******* Enqueue Css ***********/
if (!function_exists('ar_advance_register_style')){
    function ar_advance_register_style() {
        global $ar_plugin_root_url, $ar_version;
        wp_enqueue_style('ar_styles', $ar_plugin_root_url.'assets/css/ar-display.css', false, $ar_version, 'all'); 
        // Dashicons for admin buttons/icons
        wp_enqueue_style('dashicons');
        //Custom CSS now called in ar-class.php when model is loaded depending if custom css used in settings or for that model
        //wp_enqueue_style('ar_styles_custom', plugins_url('ar-for-wordpress/assets/css/ar-display-custom.css'), false, '1.0.0', 'all'); 
    }
}

// Function to enqueue model-viewer JavaScript
if ( ! function_exists( 'load_model_viewer_js' ) ) {
    function load_model_viewer_js() {
        global $ar_plugin_root_url;
        wp_enqueue_script(
            'ar-model-viewer-js',
            $ar_plugin_root_url . 'assets/js/model-viewer.min.js',
            array(),
            '1.0.0',
            array(
                'in_footer' => true,
                'strategy'  => 'async',
                'type'      => 'module'
            )
        );
    }
}
add_action( 'wp_enqueue_scripts', 'load_model_viewer_js' );
// Ensure model-viewer is available in admin where AI preview uses <model-viewer>
add_action( 'admin_enqueue_scripts', function(){
    if (function_exists('load_model_viewer_js')) {
        load_model_viewer_js();
    }
    if (function_exists('load_model_viewer_components_js')) {
        load_model_viewer_components_js();
    }
});

add_action( 'wp_footer', function() {
    ?>
    <script>
    (function() {
        // Check after slight delay in case script is async
        window.addEventListener('load', function () {
            setTimeout(function () {
                if (typeof customElements === 'undefined' || !customElements.get('model-viewer')) {
                    var fallback = document.createElement('script');
                    fallback.type = 'module';
                    fallback.src = 'https://unpkg.com/@google/model-viewer/dist/model-viewer.min.js';
                    document.body.appendChild(fallback);
                    console.warn('Fallback: model-viewer script loaded from CDN.');
                }
            }, 300); // Delay slightly to allow async local script to register
        });
    })();
    </script>
    <?php
});

// Function to enqueue additional web components JavaScript
if (!function_exists('load_model_viewer_components_js')){
    function load_model_viewer_components_js(){
        global $ar_plugin_root_url;
        wp_enqueue_script( 'ar-model-viewer-webcom-js', $ar_plugin_root_url.'assets/js/webcomponents-loader.js', array(), '1.0.0', array('in-footer'=>true,'strategy' => 'async')  );
        wp_enqueue_script( 'ar-model-viewer-intersection-js', $ar_plugin_root_url.'assets/js/intersection-observer.js', array(), '1.0.0', array('in-footer'=>true,'strategy' => 'async')  );
        wp_enqueue_script( 'ar-model-viewer-resize-js', $ar_plugin_root_url.'assets/js/ResizeObserver.js', array(), '1.0.0', array('in-footer'=>true,'strategy' => 'async')  );
    }
}




/********** AR Register Settings  **************/
if (!function_exists('ar_register_settings')){
    function ar_register_settings() {
        // Define the settings to be registered
        $settings = [
            'ar_licence_key',
            'ar_licence_valid',
            'ar_licence_plan',
            'ar_licence_renewal',
            'ar_dimensions_inches',
            'ar_hide_dimensions',
            'ar_no_posts',
            'ar_wl_file',
            'ar_view_file',
            'ar_qr_file',
            'ar_qr_destination',
            'ar_view_in_ar',
            'ar_view_in_3d',
            'ar_dimensions_units',
            'ar_dimensions_label',
            'ar_user_upload_button',
            'ar_user_default',
            'ar_user_button',
            'ar_user_modelviewer',
            'ar_user_default_image',
            'ar_fullscreen_file',
            'ar_play_file',
            'ar_pause_file',
            'ar_hide_qrcode',
            'ar_hide_reset',
            'ar_hide_fullscreen',
            'ar_hide_gallery_sizes',
            'ar_hide_arview',
            'ar_animation',
            'ar_animation_selection',
            'ar_autoplay',
            'ar_emissive',
            'ar_light_color',
            'ar_disable_zoom',
            'ar_rotate_limit',
            'ar_scene_viewer',
            'ar_css_positions',
            'ar_css',
            'ar_open_tabs',
            'ar_open_tabs_remember',
			'ar_secure_model_urls',
			'ar_ga_measurement_id',
            // AI Client settings (proxy-based)
            'ardisplay_ai_proxy_site_token'
        ];

        // Register each setting
        foreach ($settings as $setting) {
            add_option($setting, '');
            register_setting('ar_display_options_group', $setting);
        }
    }
}
add_action('admin_init', 'ar_register_settings');

/******* Element Positions *******/
$ar_css_names = array ('AR Button'=> '.ar-button', 'Dimensions'=>'.dimension', 'Fullscreen Button'=>'.ar_popup-btn', 'QR Code'=>'.ar-qrcode', 'Thumbnail Slides'=>'.ar_slider', 'Play/Pause'=>'.ar-button-animation', 'Reset Button'=>'.ar-reset', 'Call To Action'=>'.ar_cta_button', 'Model Variants'=>'.ar-variant-container');
$ar_css_styles = array();
$ar_css_styles['Top Left'] = 'top: 6px !important; bottom: auto !important; left: 6px !important; right: auto !important; margin: 0 !important;';
$ar_css_styles['Top Center'] = 'top: 6px !important; bottom: auto !important; margin: 0 auto !important; left: 0 !important; right: 0 !important;';
$ar_css_styles['Top Right'] = 'top: 6px !important; bottom: auto !important; left: auto !important; right: 6px !important; margin: 0 !important;';
$ar_css_styles['Bottom Left'] = 'top: auto !important; bottom: 6px !important; left: 6px !important; right: auto !important; margin: 0 !important;';
$ar_css_styles['Bottom Center'] = 'top: auto !important; bottom: 6px !important; margin: 0 auto !important; left: 0 !important; right: 0 !important;';
$ar_css_styles['Bottom Right'] = 'top: auto !important; bottom: 6px !important; left: auto !important; right: 6px !important; margin: 0 !important;';



if (!function_exists('ar_allowed_html')){
    function ar_allowed_html(){
        $common_atts = ar_common_attributes();
        $allowed_html = array(
            'a' => $common_atts,
            'b' => array(),
            'p' => $common_atts,
            'div' => array(                    
                'class' => true,
                'style' => true,
                'onclick' => true,
                'data-id' => true,
                'data-normal' => true,
                'data-position' => true,
                'id' => true,                
            ),
            'br' => array(),
            'em' => array(),
            'strong' => array(),
            'img' => array(                    
                'class' => true,
                'style' => true,
                'onclick' => true,
                'src' => true,
                'data-normal' => true,
                'data-position' => true,
                'id' => true,                
            ),
            'ul' => $common_atts,
            'ol' => $common_atts,
            'li' => $common_atts,
            'h1' => $common_atts,
            'h2' => $common_atts,
            'h3' => $common_atts,            
            'button' => array(                    
                'class' => true,
                'style' => true,
                'onclick' => true,
                'data-id' => true,
                'data-normal' => true,
                'data-position' => true,
                'id' => true, 
                'slot' => true,               
            ),
            'input' => array(                    
                'class' => true,
                'style' => true,
                'onclick' => true,
                'data-id' => true,
                'data-normal' => true,
                'data-position' => true,
                'id' => true,
                'type' => true,                
                'value' => true,
                'placeholder' => true,
            ),
            'blockquote' => array(),
            'center' => array(),

            // Newly added elements
            'select' => array(
                'id' => true,
                'name' => true,
                'class' => true,
                'style' => true,
                'multiple' => true,
                'size' => true,
            ),
            'option' => array(
                'value' => true,
                'selected' => true,
            ),
            'textarea' => array(
                'id' => true,
                'name' => true,
                'class' => true,
                'style' => true,
                'rows' => true,
                'cols' => true,
                'placeholder' => true,
            ),

            'model-viewer' => array(
                'id' => true,
                'class' => true,
                'style' => true,
                'src' => true,
                'alt' => true,
                'poster' => true,
                'loading' => true,
                'reveal' => true,
                'ar' => true,
                'ar-modes' => true,
                'ar-scale' => true,
                'ar-placement' => true,
                'ios-src' => true,
                'xr-environment' => true,
                'parent-page' => true,
                'quick-look' => true,
                'scene-viewer' => true,
                'webxr' => true,
                'quick-look-browsers' => true,
                'camera-controls' => true,
                'disable-pan' => true,
                'disable-tap' => true,
                'touch-action' => true,
                'disable-zoom' => true,
                'orbit-sensitivity' => true,
                'zoom-sensitivity' => true,
                'pan-sensitivity' => true,
                'auto-rotate' => true,
                'auto-rotate-delay' => true,
                'rotation-per-second' => true,
                'interaction-prompt' => true,
                'interaction-prompt-style' => true,
                'interaction-prompt-threshold' => true,
                'camera-orbit' => true,
                'camera-target' => true,
                'field-of-view' => true,
                'max-camera-orbit' => true,
                'min-camera-orbit' => true,
                'max-field-of-view' => true,
                'min-field-of-view' => true,
                'interpolation-decay' => true,
                'skybox-image' => true,
                'skybox-height' => true,
                'environment-image' => true,
                'exposure' => true,
                'tone-mapping' => true,
                'shadow-intensity' => true,
                'shadow-softness' => true,
                'animation-name' => true,
                'animation-crossfade-duration' => true,
                'autoplay' => true, 
                'onclick' => true, 
            ),
            // Allow <style> tag
            'style' => array(
                'type' => true, // Allow the 'type' attribute for style
            ),
            // Allow <script> tag with attributes
            'script' => array(
                'type' => true, // Allow the 'type' attribute for script
                'src' => true,  // Allow the 'src' attribute for script
            ),
        );

        return $allowed_html;
    }
}

if(!function_exists('ar_common_attributes')){
    function ar_common_attributes(){
        $common_atts =  array(                    
                'class' => true,
                'style' => true,
                'onclick' => true,
                'data-id' => true,
                'data-normal' => true,
                'data-position' => true,
                'id' => true,
                'name' => true, 
                'src' => true,
                'alt' => true,
                'width' => true,
                'height' => true, 
                'href' => true,
                'title' => true,
                'target' => true,
                'slot' => true,
                'for' => true, 
                'language' => true, 
                'src' => true, 
                'type' => true, 

        );

        return $common_atts;        
    }
}

/************Allow USDZ mime upload in Wordpress Media Library *****************/
if (!function_exists('ar_my_file_types')){
    function ar_my_file_types($mime_types) { //Add Additional File Types
        $mime_types['usdz'] = 'model/vnd.usdz+zip';
        return $mime_types;
    }
}
add_filter('upload_mimes', 'ar_my_file_types', 1, 1);

// Allow GLB/GLTF uploads
add_filter('upload_mimes', function($types){
    $types['glb'] = 'model/gltf-binary';
    $types['gltf'] = 'model/gltf+json';
    return $types;
}, 10, 1);

if (!function_exists('ar_display_media_library')){
    function ar_display_media_library( $data, $file, $filename, $mimes ) {
        if ( ! empty( $data['ext'] ) && ! empty( $data['type'] ) ) {
            return $data;
        }
        $registered_file_types = [
            'usdz' => 'model/vnd.usdz+zip|application/octet-stream|model/x-vnd.usdz+zip',
            'USDZ' => 'model/vnd.usdz+zip|application/octet-stream|model/x-vnd.usdz+zip',
            'reality' => 'model/vnd.reality|application/octet-stream',
            'REALITY' => 'model/vnd.reality|application/octet-stream',
            'glb' => 'model/gltf-binary|application/octet-stream|model',
            'GLB' => 'model/gltf-binary|application/octet-stream|model',
            'gltf' => 'model/gltf+json',
            'GLTF' => 'model/gltf+json',
            'hdr' => 'model/gltf+json',
            'HDR' => 'model/gltf+json',
            'dxf' => 'application/dxf',
            'DXF' => 'application/dxf',
            'dae' => 'application/dae',
            'DAE' => 'application/dae',
            '3ds' => 'application/x-3ds',
            '3DS' => 'application/x-3ds',
            'obj' => 'model/obj',
            'OBJ' => 'model/obj',
            'ply' => 'application/octet-stream',
            'PLY' => 'application/octet-stream',
            'stl' => 'model/stl',
            'STL' => 'model/stl'
            ];
        $filetype = wp_check_filetype( $filename, $mimes );
        if ( ! isset( $registered_file_types[ $filetype['ext'] ] ) ) {
            return $data;
        }
        return [
            'ext' => $filetype['ext'],
            'type' => $filetype['type'],
            'proper_filename' => $data['proper_filename'],
        ];
    }
    add_filter( 'wp_check_filetype_and_ext', 'ar_display_media_library', 10, 4 );
}


if (!function_exists('ar_display_mimes')){
    function ar_display_mimes( $mime_types ) {
        if ( ! in_array( 'usdz', $mime_types ) ) { 
            $mime_types['usdz'] = 'model/vnd.usdz+zip|application/octet-stream|model/x-vnd.usdz+zip';
        }
        if ( ! in_array( 'reality', $mime_types ) ) { 
            $mime_types['reality'] = 'model/vnd.reality|application/octet-stream';
        }
        if ( ! in_array( 'glb', $mime_types ) ) { 
            $mime_types['glb'] = 'model/gltf-binary|application/octet-stream|model';
        }
        if ( ! in_array( 'gltf', $mime_types ) ) { 
            $mime_types['gltf'] = 'model/gltf+json';
        }
        if ( ! in_array( 'hdr', $mime_types ) ) { 
            $mime_types['hdr'] = 'image/vnd.radiance';
        }
        if ( ! in_array( 'dxf', $mime_types ) ) { 
            $mime_types['dxf'] = 'application/dxf';
        }
        if ( ! in_array( 'dae', $mime_types ) ) { 
            $mime_types['dae'] = 'application/dae';
        }
        if ( ! in_array( '3ds', $mime_types ) ) { 
            $mime_types['3ds'] = 'application/x-3ds';
        }
        if ( ! in_array( 'obj', $mime_types ) ) { 
            $mime_types['obj'] = 'model/obj';
        }
        if ( ! in_array( 'ply', $mime_types ) ) { 
            $mime_types['ply'] = 'application/octet-stream';
        }
        if ( ! in_array( 'stl', $mime_types ) ) { 
            $mime_types['stl'] = 'model/stl';
        }
        return $mime_types;
    }
    
    add_filter( 'upload_mimes', 'ar_display_mimes' );
}


/************* Get Mime Types for corresponding files *******************/
if (!function_exists('ar_get_mime_type')){
    function ar_get_mime_type($file_path) {
        $ext = strtolower(pathinfo($file_path, PATHINFO_EXTENSION));
        switch ($ext) {
            case 'gltf':
                return 'model/gltf+json';
            case 'glb':
                return 'model/gltf-binary';
            case 'usdz':
                return 'model/vnd.usdz+zip';
            case 'jpg':
            case 'jpeg':
                return 'image/jpeg';
            case 'png':
                return 'image/png';
            default:
                return 'application/octet-stream'; // Fallback MIME type
        }
    }
}


// Add 'type="module"' attribute to the model-viewer script tag
add_filter('script_loader_tag', 'ar_add_type_attribute' , 10, 3);

if(!function_exists('ar_add_type_attribute')){
    function ar_add_type_attribute($tag, $handle, $src) {
        // Target only the 'ar-model-viewer-js' handle
        if ( 'ar-model-viewer-js' !== $handle ) {
            return $tag;
        }

        // Modify the script tag to add 'type="module"'
        $tag = str_replace('<script ', '<script type="module" ', $tag);
        return $tag;
    }
}

remove_action( 'shutdown', 'wp_ob_end_flush_all', 1 );
add_action( 'shutdown', function() {
    if (ob_get_contents()) {
        if (ob_get_length() && !ini_get('zlib.output_compression')) {
            while (ob_get_level()) {
                @ob_end_flush();
            }
        }
    }
} );

//Encode custom CSS code for importing into text field
if (!function_exists('ar_encodeURIComponent')){
    function ar_encodeURIComponent($str) {
        $unescaped = array(
            '%2D'=>'-','%5F'=>'_','%2E'=>'.','%21'=>'!', '%7E'=>'~',
            '%2A'=>'*', '%27'=>"'", '%28'=>'(', '%29'=>')'
        );
        $reserved = array(
            '%3B'=>';','%2C'=>',','%2F'=>'/','%3F'=>'?','%3A'=>':',
            '%40'=>'@','%26'=>'&','%3D'=>'=','%2B'=>'+','%24'=>'$'
        );
        $score = array(
            '%23'=>'#'
        );
        return strtr(rawurlencode($str), array_merge($reserved,$unescaped,$score));
    }
}

if(!function_exists('get_screen_type')){
    function get_screen_type(){
        $screen = array();

        $user_agent = isset($_SERVER["HTTP_USER_AGENT"]) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : '';

        $screen['isMob'] = is_numeric(strpos(strtolower($user_agent), "mobile")); 
        $screen['isTab'] = is_numeric(strpos(strtolower($user_agent), "tablet")); 
        $screen['isWin'] = is_numeric(strpos(strtolower($user_agent), "windows")); 
        $screen['isAndroid'] = is_numeric(strpos(strtolower($user_agent), "android")); 
        $screen['isIPhone'] = is_numeric(strpos(strtolower($user_agent), "iphone")); 
        $screen['isIPad'] = is_numeric(strpos(strtolower($user_agent), "ipad")); 
        $screen['isFireFox'] = is_numeric(strpos(strtolower($user_agent), "firefox"));
        $screen['isFireFoxiOS'] = is_numeric(strpos(strtolower($user_agent), "fxios"));
        $screen['isIOS'] = $screen['isIPhone'] || $screen['isIPad'];

        return $screen;
    }
}



/********** Reload the Gutenburg editor when AR model post is updated **********/
if (!function_exists('ar_reload_page_after_publish')){
    function ar_reload_page_after_publish() {
        global $post;
    
        // Check if the current post type is 'armodels'
        if ($post && $post->post_type === 'armodels') {
            ?>
            <script>
                (function ($) {
                    $(document).ready(function () {
                        // Add the event listener for post updates
                        $(document).on('click', '#publish, .editor-post-publish-button, #save-post', function (e) {
                            // Check if the post is being published or updated
                            if ($('#original_post_status').val() !== $('#post_status').val()) {
                                // Reload the page after a short delay (adjust as needed)
                                setTimeout(function () {
                                    location.reload();
                                }, 2000);
                            }
                        });
                    });
                })(jQuery);
            </script>
            <?php
        }
    }

    add_action('admin_footer', 'ar_reload_page_after_publish');
}


if (get_option('ar_no_posts')) {
    // Hook into the 'pre_get_posts' action to modify the query before it's executed
    add_action('pre_get_posts', 'set_armodels_to_private');

    function set_armodels_to_private($query) {
        // Only modify the main query on the frontend
        if (!is_admin() && $query->is_main_query()) {
            // Check if the query is for 'armodels' post type
            if (isset($query->query_vars['post_type']) && $query->query_vars['post_type'] === 'armodels') {
                // Modify the query to only show private posts if the 'ar_no_posts' option is set
                $query->set('post_status', 'private');
            }
        }
    }
}


/************* Function to filter gallery data by enabled ratios and sizes from settings *******************/
if (!function_exists('ar_filter_gallery_data_by_settings')){
    function ar_filter_gallery_data_by_settings($data_array) {
        // Get saved settings
        $saved_ratios = get_option('ar_gallery_ratios');
        $saved_sizes = get_option('ar_gallery_sizes');
        
        // If no settings saved, return all data (default behavior)
        if (empty($saved_ratios) && empty($saved_sizes)) {
            return $data_array;
        }
        
        $filtered_data = array();
        
        // If ratios are configured, filter by them
        if (!empty($saved_ratios) && is_array($saved_ratios)) {
            foreach ($data_array as $ratio_key => $sizes_list) {
                // Only include ratios that are enabled in settings
                if (array_key_exists($ratio_key, $saved_ratios)) {
                    // If sizes are also configured for this ratio, filter them
                    if (isset($saved_sizes[$ratio_key]) && is_array($saved_sizes[$ratio_key]) && !empty($saved_sizes[$ratio_key])) {
                        $filtered_sizes = array();
                        foreach ($sizes_list as $size_item) {
                            $size_id = isset($size_item['modelTypeID']) ? (string)$size_item['modelTypeID'] : '';
                            // Only include sizes that are enabled for this ratio
                            if (in_array($size_id, $saved_sizes[$ratio_key], true)) {
                                $filtered_sizes[] = $size_item;
                            }
                        }
                        // Only add ratio if it has at least one enabled size
                        if (!empty($filtered_sizes)) {
                            $filtered_data[$ratio_key] = $filtered_sizes;
                        }
                    } else {
                        // Ratio is enabled but no specific sizes configured, include all sizes
                        $filtered_data[$ratio_key] = $sizes_list;
                    }
                }
            }
        } else {
            // No ratio filtering, but might have size filtering
            foreach ($data_array as $ratio_key => $sizes_list) {
                if (isset($saved_sizes[$ratio_key]) && is_array($saved_sizes[$ratio_key]) && !empty($saved_sizes[$ratio_key])) {
                    $filtered_sizes = array();
                    foreach ($sizes_list as $size_item) {
                        $size_id = isset($size_item['modelTypeID']) ? (string)$size_item['modelTypeID'] : '';
                        if (in_array($size_id, $saved_sizes[$ratio_key], true)) {
                            $filtered_sizes[] = $size_item;
                        }
                    }
                    if (!empty($filtered_sizes)) {
                        $filtered_data[$ratio_key] = $filtered_sizes;
                    }
                } else {
                    // No size filtering for this ratio, include all
                    $filtered_data[$ratio_key] = $sizes_list;
                }
            }
        }
        
        return !empty($filtered_data) ? $filtered_data : $data_array;
    }
}

/************* Function to find the closest ratio - used for ar-gallery shortcode *******************/
if (!function_exists('ar_findClosestRatio')){
    function ar_findClosestRatio($aspect_ratio, $ratios) {
        $closest_ratio = null;
        $smallest_difference = null;
        
        // Check if aspect_ratio is valid
        if (empty($aspect_ratio) || !is_numeric($aspect_ratio)) {
            return null;
        }
        
        foreach ($ratios as $ratio) {
            $difference = abs($aspect_ratio - floatval($ratio));
            if ($smallest_difference === null || $difference < $smallest_difference) {
                $smallest_difference = $difference;
                $closest_ratio = $ratio;
            }
        }
        
        return $closest_ratio;
    }
}

//used for the gallery builder size selector
if(!function_exists('ar_array_flatten')){
    function ar_array_flatten($array) {
        $flatten = array();
        foreach ($array as $value) {
            if (is_array($value)) {
                $flatten = array_merge($flatten, ar_array_flatten($value));
            } else {
                $flatten[] = $value;
            }
        }
        return $flatten;
    }
}

//exclude ar-gallery url from hide my wp ghost lite plugin

if(!function_exists('disable_hmwp_on_ar_gallery_urls')){
    function disable_hmwp_on_ar_gallery_urls($process) {
        // Check if the current request URL contains a specific plugin path
        if (strpos($_SERVER['REQUEST_URI'], 'wp-content/plugins/ar-for-wordpress/includes/ar-gallery.php') !== false || strpos($_SERVER['REQUEST_URI'], 'wp-content/plugins/ar-for-woocommerce/includes/ar-gallery.php') !== false) {            
            return false;
        }

        return $process;
    }

    add_filter('hmwp_process_init', 'disable_hmwp_on_ar_gallery_urls');
}

if(!function_exists('whitelist_file_in_wp_content')){

    function whitelist_file_in_wp_content( $whitelist ) {
        $whitelist[] = 'ar-gallery.php';
        return $whitelist;
    }

    add_filter( 'sgs_whitelist_wp_content' , 'whitelist_file_in_wp_content' );
}