ID ) ) { $found_id = $post->ID; } } } // B. Request Variables // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading context ID only, no state change. if ( ! $found_id ) { $vars = [ 'editor_post_id', 'post', 'post_id', 'elementor-preview' ]; foreach ( $vars as $var ) { if ( isset( $_REQUEST[ $var ] ) && is_numeric( $_REQUEST[ $var ] ) ) { $found_id = (int) $_REQUEST[ $var ]; break; } } } // C. Referer Scan // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- We verify content in logic below. if ( ! $found_id && ! empty( $_SERVER['HTTP_REFERER'] ) ) { // FIX: Sanitize and Unslash the Referer $raw_referer = isset( $_SERVER['HTTP_REFERER'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) : ''; // FIX: Use wp_parse_url instead of parse_url $query = wp_parse_url( $raw_referer, PHP_URL_QUERY ); if ( $query ) { parse_str( $query, $params ); if ( isset( $params['post'] ) && is_numeric( $params['post'] ) ) { $found_id = (int) $params['post']; } elseif ( isset( $params['editor_post_id'] ) && is_numeric( $params['editor_post_id'] ) ) { $found_id = (int) $params['editor_post_id']; } } } return (int) $found_id; } /** * 2. LOGIC: Get Target Kit ID */ function abdielmskef_get_target_kit_id() { // JS Preview Override // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading preview param only. if ( ! empty( $_GET['abdielmskef_preview_kit'] ) ) { return (int) $_GET['abdielmskef_preview_kit']; } $post_id = abdielmskef_get_current_context_id(); if ( ! $post_id || 'elementor_library' === get_post_type( $post_id ) ) { return 0; } $target_kit_id = 0; $meta_key = defined( 'ABDIELMSKEF_META_LOCAL_KIT_ID' ) ? ABDIELMSKEF_META_LOCAL_KIT_ID : 'abdielmskef_local_kit_id'; $direct_val = get_post_meta( $post_id, $meta_key, true ); if ( $direct_val ) { $target_kit_id = (int) $direct_val; } if ( ! $target_kit_id ) { $page_settings = get_post_meta( $post_id, '_elementor_page_settings', true ); if ( is_array( $page_settings ) && ! empty( $page_settings[ $meta_key ] ) ) { $target_kit_id = (int) $page_settings[ $meta_key ]; } } return (int) $target_kit_id; } /** * 3. INTERCEPTOR */ function abdielmskef_intercept_active_kit( $value ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Context check only. $is_elementor_loading = ( isset( $_GET['action'] ) && 'elementor' === $_GET['action'] ); if ( ! $is_elementor_loading ) { return $value; } } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Context check only. if ( is_admin() && isset( $_GET['page'] ) && 'elementor' === $_GET['page'] ) { return $value; } $custom_kit_id = abdielmskef_get_target_kit_id(); if ( $custom_kit_id > 0 ) { return $custom_kit_id; } return $value; } add_filter( 'pre_option_elementor_active_kit', 'abdielmskef_intercept_active_kit' ); add_filter( 'option_elementor_active_kit', 'abdielmskef_intercept_active_kit' ); /** * 4. FRONTEND STYLE SWAP */ add_action( 'elementor/frontend/after_enqueue_styles', 'abdielmskef_swap_frontend_styles', 20 ); function abdielmskef_swap_frontend_styles() { if ( ! class_exists( 'Elementor\\Plugin' ) ) return; $target_kit_id = abdielmskef_get_target_kit_id(); if ( $target_kit_id === 0 ) return; $loaded_kit_id = (int) \Elementor\Plugin::$instance->kits_manager->get_active_id(); if ( $loaded_kit_id !== $target_kit_id ) { wp_dequeue_style( 'elementor-post-' . $loaded_kit_id ); wp_deregister_style( 'elementor-post-' . $loaded_kit_id ); if ( class_exists( 'Elementor\\Core\\Files\\CSS\\Post' ) ) { if ( method_exists( 'Elementor\\Core\\Files\\CSS\\Post', 'create' ) ) { $css_file = \Elementor\Core\Files\CSS\Post::create( $target_kit_id ); } else { $css_file = new \Elementor\Core\Files\CSS\Post( $target_kit_id ); } if ( $css_file ) { $css_file->enqueue(); } } } } /** * 5. JS BODY CLASS ENFORCER (Standardized Version) * Enqueues external JS and passes data via wp_localize_script. */ add_action( 'wp_enqueue_scripts', 'abdielmskef_enqueue_body_enforcer', 999 ); add_action( 'elementor/editor/after_enqueue_scripts', 'abdielmskef_enqueue_body_enforcer', 999 ); function abdielmskef_enqueue_body_enforcer() { $target_kit_id = abdielmskef_get_target_kit_id(); if ( $target_kit_id === 0 ) return; // Remove filters temporarily to get the REAL global ID remove_filter( 'pre_option_elementor_active_kit', 'abdielmskef_intercept_active_kit' ); remove_filter( 'option_elementor_active_kit', 'abdielmskef_intercept_active_kit' ); $real_global_id = get_option( 'elementor_active_kit' ); add_filter( 'pre_option_elementor_active_kit', 'abdielmskef_intercept_active_kit' ); add_filter( 'option_elementor_active_kit', 'abdielmskef_intercept_active_kit' ); if ( $target_kit_id == $real_global_id ) return; wp_enqueue_script( 'abdielmskef-body-enforcer', ABDIELMSKEF_URL . 'assets/js/body-enforcer.js', [], ABDIELMSKEF_VERSION, true ); wp_localize_script( 'abdielmskef-body-enforcer', 'abdielmskefBodyData', [ 'targetClass' => 'elementor-kit-' . $target_kit_id, 'removeClass' => 'elementor-kit-' . $real_global_id, ]); }