done ) ) { foreach ( $wp_scripts->done as $handle ) { // Exclude Addifect core engine scripts. $exclusions = array( 'addifect-render-vendor', 'addifect-render-engine', 'addifect-render-three', 'lodash', 'react', 'react-dom', 'react-jsx-runtime', ); if ( in_array( $handle, $exclusions, true ) ) { continue; } if ( isset( $wp_scripts->registered[ $handle ] ) ) { $item = $wp_scripts->registered[ $handle ]; $scripts[] = array( 'type' => 'script', 'handle' => $handle, 'src' => $item->src, 'deps' => $item->deps, 'in_footer' => $item->args, 'source' => addifect_get_asset_origin( $item->src ), ); if ( ! empty( $item->extra['after'] ) ) { $scripts[] = array( 'type' => 'inline', 'handle' => $handle, ); } } } } // 2. Capture Styles. if ( $wp_styles instanceof WP_Styles && ! empty( $wp_styles->done ) ) { foreach ( $wp_styles->done as $handle ) { // Exclude core Addifect styles. if ( 'addifect-render-styles' === $handle ) { continue; } if ( isset( $wp_styles->registered[ $handle ] ) ) { $item = $wp_styles->registered[ $handle ]; $styles[] = array( 'type' => 'script', 'handle' => $handle, 'src' => $item->src, 'deps' => $item->deps, 'source' => addifect_get_asset_origin( $item->src ), ); if ( ! empty( $item->extra['after'] ) ) { $styles[] = array( 'type' => 'inline', 'handle' => $handle, ); } } } } update_option( 'addifect_captured_assets', array( 'scripts' => $scripts, 'styles' => $styles, ) ); } /** * Hook to trigger asset capture in the footer. */ add_action( 'wp_footer', function () { if ( get_query_var( 'addifect_capture_assets' ) ) { addifect_capture_active_assets(); } }, 999 ); /** * Determine the origin of an asset (Theme, Plugin, or Core). * * @param string $src The asset URL. * @return string Origin name. */ function addifect_get_asset_origin( $src ) { if ( empty( $src ) || ! is_string( $src ) ) { return 'Core'; } if ( false !== strpos( $src, get_template_directory_uri() ) || false !== strpos( $src, get_stylesheet_directory_uri() ) ) { return 'Theme'; } if ( false !== strpos( $src, plugins_url() ) || false !== strpos( $src, WP_PLUGIN_DIR ) ) { return 'Plugin'; } return 'Core'; } /** * Combined Asset Removal logic for Addifect pages. */ function addifect_remove_disabled_assets() { $template_id = addifect_resolve_template_id(); if ( ! $template_id || ! addifect_template_exists( $template_id ) ) { return; } if ( get_query_var( 'addifect_capture_assets' ) ) { return; } $options = get_option( 'addifect_site_options' ); if ( ! is_array( $options ) ) { return; } // Remove Scripts. if ( ! empty( $options['disabled_scripts']['script'] ) && is_array( $options['disabled_scripts']['script'] ) ) { foreach ( $options['disabled_scripts']['script'] as $s_handle ) { wp_dequeue_script( $s_handle ); wp_deregister_script( $s_handle ); } } // Remove Styles. if ( ! empty( $options['disabled_css']['stylesheet'] ) && is_array( $options['disabled_css']['stylesheet'] ) ) { foreach ( $options['disabled_css']['stylesheet'] as $css_handle ) { if ( 'addifect-render-styles' === $css_handle ) { continue; } wp_dequeue_style( $css_handle ); wp_deregister_style( $css_handle ); } } // Remove Inline Hooks. $inline_hooks = array(); if ( ! empty( $options['disabled_scripts']['inline'] ) ) { $inline_hooks = array_merge( $inline_hooks, (array) $options['disabled_scripts']['inline'] ); } if ( ! empty( $options['disabled_css']['inline'] ) ) { $inline_hooks = array_merge( $inline_hooks, (array) $options['disabled_css']['inline'] ); } $hooks_count = count( $inline_hooks ); if ( $hooks_count > 0 ) { for ( $i = 0; $i < $hooks_count; ++$i ) { $hook = $inline_hooks[ $i ]; remove_action( 'wp_head', $hook ); remove_action( 'wp_footer', $hook, 1 ); } } } add_action( 'wp_enqueue_scripts', 'addifect_remove_disabled_assets', 999 );