ID ); $override_options = self::get_override_options(); ?>
array( 'label' => __( 'Disable all optimizations', '365i-performance-optimizer' ), 'description' => __( 'Turn off all plugin optimizations for this page.', '365i-performance-optimizer' ), ), 'disable_speculation' => array( 'label' => __( 'Disable speculative loading', '365i-performance-optimizer' ), 'description' => '', ), 'disable_defer' => array( 'label' => __( 'Disable script deferral', '365i-performance-optimizer' ), 'description' => '', ), 'disable_delay_js' => array( 'label' => __( 'Disable JS delay', '365i-performance-optimizer' ), 'description' => '', ), 'force_lazy_load' => array( 'label' => __( 'Force lazy-load images', '365i-performance-optimizer' ), 'description' => __( 'Override homepage lazy-load disable setting.', '365i-performance-optimizer' ), ), 'disable_local_fonts' => array( 'label' => __( 'Disable local fonts', '365i-performance-optimizer' ), 'description' => __( 'Use original Google Fonts on this page.', '365i-performance-optimizer' ), ), ); } /** * Save meta box data. * * @param int $post_id Post ID. * @param WP_Post $post Post object. * * @return void */ public static function save( $post_id, $post ) { // Verify nonce. if ( ! isset( $_POST[ self::NONCE_NAME ] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST[ self::NONCE_NAME ] ) ), self::NONCE_ACTION ) ) { return; } // Check autosave. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; } // Check permissions. $post_type = get_post_type_object( $post->post_type ); if ( ! current_user_can( $post_type->cap->edit_post, $post_id ) ) { return; } // Get and sanitize overrides. $overrides = array(); $raw_input = isset( $_POST['i365_po_overrides'] ) ? (array) wp_unslash( $_POST['i365_po_overrides'] ) : array(); // Sanitize checkbox values. $valid_keys = array_keys( self::get_override_options() ); foreach ( $valid_keys as $key ) { if ( ! empty( $raw_input[ $key ] ) ) { $overrides[ $key ] = true; } } // Sanitize excluded scripts. if ( ! empty( $raw_input['excluded_scripts'] ) ) { $scripts = sanitize_text_field( $raw_input['excluded_scripts'] ); $overrides['excluded_scripts'] = $scripts; } // Save or delete meta. if ( ! empty( $overrides ) ) { update_post_meta( $post_id, self::META_KEY, $overrides ); } else { delete_post_meta( $post_id, self::META_KEY ); } } /** * Get overrides for a post. * * @param int $post_id Post ID. * * @return array */ public static function get_overrides( $post_id ) { $overrides = get_post_meta( $post_id, self::META_KEY, true ); return is_array( $overrides ) ? $overrides : array(); } /** * Check if a specific override is active for the current post. * * @param string $override Override key to check. * * @return bool */ public static function is_override_active( $override ) { if ( is_admin() ) { return false; } $post_id = get_the_ID(); if ( ! $post_id ) { return false; } $overrides = self::get_overrides( $post_id ); // Check for disable_all first. if ( ! empty( $overrides['disable_all'] ) ) { return true; } return ! empty( $overrides[ $override ] ); } /** * Get excluded script handles for the current post. * * @return array */ public static function get_excluded_scripts() { if ( is_admin() ) { return array(); } $post_id = get_the_ID(); if ( ! $post_id ) { return array(); } $overrides = self::get_overrides( $post_id ); if ( empty( $overrides['excluded_scripts'] ) ) { return array(); } // Split by comma and clean up. $scripts = explode( ',', $overrides['excluded_scripts'] ); $scripts = array_map( 'trim', $scripts ); $scripts = array_filter( $scripts ); return $scripts; } /** * Check if all optimizations should be disabled for current page. * * @return bool */ public static function should_disable_all() { return self::is_override_active( 'disable_all' ); } }