array_map('advanced_preloader_loader_markup', array_combine(array_keys(advanced_preloader_loader_styles()), array_keys(advanced_preloader_loader_styles()))), )); } } add_action('admin_enqueue_scripts', 'advanced_preloader_admin_scripts'); // Sanitize settings function advanced_preloader_sanitize_settings($input) { $input = wp_unslash($input); $sanitized_input = []; if (isset($input['enabled'])) { $sanitized_input['enabled'] = sanitize_text_field($input['enabled']); } if (isset($input['type'])) { $sanitized_input['type'] = sanitize_key($input['type']); } if (isset($input['image'])) { $sanitized_input['image'] = sanitize_text_field($input['image']); } if (isset($input['text'])) { $sanitized_input['text'] = wp_kses_post($input['text']); } if (isset($input['layout_order'])) { $sanitized_input['layout_order'] = sanitize_key($input['layout_order']); } if (isset($input['bg_color'])) { $sanitized_input['bg_color'] = sanitize_hex_color($input['bg_color']); } if (isset($input['text_color'])) { $sanitized_input['text_color'] = sanitize_hex_color($input['text_color']); } if (isset($input['animation_speed'])) { $sanitized_input['animation_speed'] = sanitize_text_field($input['animation_speed']); } if (isset($input['delay_time'])) { $sanitized_input['delay_time'] = sanitize_text_field($input['delay_time']); } if (isset($input['custom_css'])) { $sanitized_input['custom_css'] = wp_strip_all_tags($input['custom_css']); } if (isset($input['text_display_mode'])) { $sanitized_input['text_display_mode'] = sanitize_key($input['text_display_mode']); } if (isset($input['loader_style'])) { $style = sanitize_key($input['loader_style']); $sanitized_input['loader_style'] = array_key_exists($style, advanced_preloader_loader_styles()) ? $style : 'spinner'; } if (isset($input['loader_color'])) { $sanitized_input['loader_color'] = (string) sanitize_hex_color($input['loader_color']); } if (isset($input['loader_size'])) { $sanitized_input['loader_size'] = in_array($input['loader_size'], array('small', 'medium', 'large'), true) ? $input['loader_size'] : 'medium'; } if (isset($input['show_on'])) { $sanitized_input['show_on'] = in_array($input['show_on'], array('all', 'home', 'only', 'except'), true) ? $input['show_on'] : 'all'; } if (isset($input['pages'])) { $sanitized_input['pages'] = array_values(array_filter(array_map('absint', (array) $input['pages']))); } if (isset($input['frequency'])) { $sanitized_input['frequency'] = in_array($input['frequency'], array('always', 'session', 'once'), true) ? $input['frequency'] : 'always'; } if (isset($input['hide_logged_in'])) { $sanitized_input['hide_logged_in'] = '1'; } return $sanitized_input; } // Add plugin settings page to main menu function advanced_preloader_settings_menu() { add_menu_page( 'Advanced Preloader Settings', 'Preloader', 'manage_options', 'advanced-preloader', 'advanced_preloader_settings_page', 'dashicons-admin-generic' ); } add_action('admin_menu', 'advanced_preloader_settings_menu'); // Display settings page with tabs function advanced_preloader_settings_page() { $tabs = advanced_preloader_tabs(); $active_tab = isset($_GET['tab']) ? sanitize_key(wp_unslash($_GET['tab'])) : 'general'; // Verify nonce for tab navigation if tab parameter exists if (isset($_GET['tab'])) { // Check if the nonce is set and validate it if (isset($_REQUEST['_wpnonce']) && !empty($_REQUEST['_wpnonce'])) { $nonce = sanitize_text_field(wp_unslash($_REQUEST['_wpnonce'])); // Verify the nonce if (!wp_verify_nonce($nonce, 'advanced-preloader-tab-nonce')) { wp_die('Security check failed'); } } else { wp_die('Nonce is missing or invalid'); } } ?>
Enable Preloader'; } function advanced_preloader_type_field() { $options = get_option('advanced_preloader_general'); $type = isset($options['type']) ? $options['type'] : 'image'; echo ''; echo 'Loader animations are built in and need no image upload.
'; } function advanced_preloader_image_field() { $options = get_option('advanced_preloader_general', []); $image_id = isset($options['image']) ? $options['image'] : ''; $image_url = !empty($image_id) ? wp_get_attachment_url($image_id) : ''; echo ' '; echo ' '; echo 'HTML tags are allowed. Add multiple lines for random display.
'; } function advanced_preloader_text_display_mode_field() { $options = get_option('advanced_preloader_general'); $mode = isset($options['text_display_mode']) ? $options['text_display_mode'] : 'full'; echo ''; } function advanced_preloader_layout_order_field() { $options = get_option('advanced_preloader_general', []); $layout_order = isset($options['layout_order']) ? $options['layout_order'] : 'image-over-text'; echo 'Leave empty to use the text color.
'; } function advanced_preloader_loader_size_field() { $options = get_option('advanced_preloader_design', []); $size = $options['loader_size'] ?? 'medium'; echo ''; } function advanced_preloader_display_section_intro() { echo 'Choose where the preloader appears and how often a visitor sees it. These rules work with page caching plugins.
'; } function advanced_preloader_show_on_field() { $options = get_option('advanced_preloader_display', []); $show_on = $options['show_on'] ?? 'all'; $choices = [ 'all' => 'Every page', 'home' => 'Homepage only', 'only' => 'Only on the pages selected below', 'except' => 'Every page except those selected below', ]; echo ''; } function advanced_preloader_pages_field() { $options = get_option('advanced_preloader_display', []); $selected = array_map('absint', (array) ($options['pages'] ?? [])); $pages = get_pages(['sort_column' => 'menu_order,post_title', 'post_status' => 'publish']); echo 'You have no published pages yet.
' . count($selected) . ' selected
'; echo ''; } function advanced_preloader_frequency_field() { $options = get_option('advanced_preloader_display', []); $freq = $options['frequency'] ?? 'always'; $choices = [ 'always' => ['Every page load', ''], 'session' => ['Once per visit', 'Shown again after the visitor closes the browser.'], 'once' => ['First visit only', 'Returning visitors on the same browser never see it again.'], ]; echo ''; } function advanced_preloader_hide_logged_in_field() { $options = get_option('advanced_preloader_display', []); $hide = !empty($options['hide_logged_in']); echo ''; echo 'Handy while you are editing the site.
'; } /** * Should the preloader appear on the current request? Page rules are evaluated * on the server; the "how often" rule runs in the browser so it stays correct * behind page caches. */ function advanced_preloader_should_display() { if (is_admin() || wp_doing_ajax() || is_feed() || is_embed()) { return false; } $general = get_option('advanced_preloader_general', []); if (!isset($general['enabled']) || '1' !== (string) $general['enabled']) { return false; } if (function_exists('is_amp_endpoint') && is_amp_endpoint()) { return false; } $display = get_option('advanced_preloader_display', []); if (!empty($display['hide_logged_in']) && is_user_logged_in()) { return false; } $show_on = $display['show_on'] ?? 'all'; $pages = array_map('absint', (array) ($display['pages'] ?? [])); switch ($show_on) { case 'home': $show = is_front_page(); break; case 'only': $show = $pages && is_page($pages); break; case 'except': $show = !($pages && is_page($pages)); break; default: $show = true; } /** * Filter whether the preloader shows on the current request. * * @param bool $show */ return (bool) apply_filters('advanced_preloader_should_display', $show); } /** * Convert a duration setting such as "1s", "0.5s", "500ms" or "2" (seconds) to milliseconds. */ function advanced_preloader_to_ms($value, $fallback) { $value = strtolower(trim((string) $value)); if (!preg_match('/^(\d+(?:\.\d+)?)\s*(ms|s)?$/', $value, $m)) { return $fallback; } $ms = (isset($m[2]) && 'ms' === $m[2]) ? (float) $m[1] : (float) $m[1] * 1000; return (int) min($ms, 60000); } function advanced_preloader_display() { static $printed = false; if ($printed || !advanced_preloader_should_display()) { return; } $printed = true; $general = get_option('advanced_preloader_general', []); $design = get_option('advanced_preloader_design', []); $animation = get_option('advanced_preloader_animation', []); $display = get_option('advanced_preloader_display', []); $preloader_type = $general['type'] ?? 'image'; if (!in_array($preloader_type, ['image', 'text', 'both', 'loader', 'loader_text'], true)) { $preloader_type = 'image'; } $layout_order = $general['layout_order'] ?? 'image-over-text'; if (!in_array($layout_order, [ 'image-over-text', 'image-left-text', 'image-right-text', 'image-below-text' ], true)) { $layout_order = 'image-over-text'; } $loader_style = $general['loader_style'] ?? 'spinner'; if (!array_key_exists($loader_style, advanced_preloader_loader_styles())) { $loader_style = 'spinner'; } $loader_size = in_array($design['loader_size'] ?? 'medium', ['small', 'medium', 'large'], true) ? $design['loader_size'] ?? 'medium' : 'medium'; $text_color = sanitize_hex_color($design['text_color'] ?? '') ?: '#000000'; $bg_color = sanitize_hex_color($design['bg_color'] ?? '') ?: '#ffffff'; $loader_color = sanitize_hex_color($design['loader_color'] ?? '') ?: $text_color; $frequency = in_array($display['frequency'] ?? 'always', ['always', 'session', 'once'], true) ? $display['frequency'] ?? 'always' : 'always'; // "How often" is decided in the browser, before the overlay paints, so it works behind page caches. if ('always' !== $frequency) { $storage = 'once' === $frequency ? 'localStorage' : 'sessionStorage'; $check = "try{if(window." . $storage . ".getItem('advancedPreloaderSeen')){document.documentElement.classList.add('ap-skip');}}catch(e){}"; if (function_exists('wp_print_inline_script_tag')) { wp_print_inline_script_tag($check); } } $output = '