Edit File by line
/home/zeestwma/richards.../wp-inclu...
File: script-loader.php
return array_filter(
[2500] Fix | Delete
$nodes,
[2501] Fix | Delete
static function ( $node ) {
[2502] Fix | Delete
return ! in_array( 'blocks', $node['path'], true );
[2503] Fix | Delete
},
[2504] Fix | Delete
ARRAY_FILTER_USE_BOTH
[2505] Fix | Delete
);
[2506] Fix | Delete
}
[2507] Fix | Delete
[2508] Fix | Delete
/**
[2509] Fix | Delete
* Enqueues the global styles defined via theme.json.
[2510] Fix | Delete
*
[2511] Fix | Delete
* @since 5.8.0
[2512] Fix | Delete
*/
[2513] Fix | Delete
function wp_enqueue_global_styles() {
[2514] Fix | Delete
$assets_on_demand = wp_should_load_block_assets_on_demand();
[2515] Fix | Delete
$is_block_theme = wp_is_block_theme();
[2516] Fix | Delete
$is_classic_theme = ! $is_block_theme;
[2517] Fix | Delete
[2518] Fix | Delete
/*
[2519] Fix | Delete
* Global styles should be printed in the head for block themes, or for classic themes when loading assets on
[2520] Fix | Delete
* demand is disabled, which is the default.
[2521] Fix | Delete
* The footer should only be used for classic themes when loading assets on demand is enabled.
[2522] Fix | Delete
*
[2523] Fix | Delete
* See https://core.trac.wordpress.org/ticket/53494 and https://core.trac.wordpress.org/ticket/61965.
[2524] Fix | Delete
*/
[2525] Fix | Delete
if (
[2526] Fix | Delete
( $is_block_theme && doing_action( 'wp_footer' ) ) ||
[2527] Fix | Delete
( $is_classic_theme && doing_action( 'wp_footer' ) && ! $assets_on_demand ) ||
[2528] Fix | Delete
( $is_classic_theme && doing_action( 'wp_enqueue_scripts' ) && $assets_on_demand )
[2529] Fix | Delete
) {
[2530] Fix | Delete
return;
[2531] Fix | Delete
}
[2532] Fix | Delete
[2533] Fix | Delete
/*
[2534] Fix | Delete
* If loading the CSS for each block separately, then load the theme.json CSS conditionally.
[2535] Fix | Delete
* This removes the CSS from the global-styles stylesheet and adds it to the inline CSS for each block.
[2536] Fix | Delete
* This filter must be registered before calling wp_get_global_stylesheet();
[2537] Fix | Delete
*/
[2538] Fix | Delete
add_filter( 'wp_theme_json_get_style_nodes', 'wp_filter_out_block_nodes' );
[2539] Fix | Delete
[2540] Fix | Delete
$stylesheet = wp_get_global_stylesheet();
[2541] Fix | Delete
[2542] Fix | Delete
if ( $is_block_theme ) {
[2543] Fix | Delete
/*
[2544] Fix | Delete
* Dequeue the Customizer's custom CSS
[2545] Fix | Delete
* and add it before the global styles custom CSS.
[2546] Fix | Delete
*/
[2547] Fix | Delete
remove_action( 'wp_head', 'wp_custom_css_cb', 101 );
[2548] Fix | Delete
// Get the custom CSS from the Customizer and add it to the global stylesheet.
[2549] Fix | Delete
$custom_css = wp_get_custom_css();
[2550] Fix | Delete
$stylesheet .= $custom_css;
[2551] Fix | Delete
[2552] Fix | Delete
// Add the global styles custom CSS at the end.
[2553] Fix | Delete
$stylesheet .= wp_get_global_stylesheet( array( 'custom-css' ) );
[2554] Fix | Delete
}
[2555] Fix | Delete
[2556] Fix | Delete
if ( empty( $stylesheet ) ) {
[2557] Fix | Delete
return;
[2558] Fix | Delete
}
[2559] Fix | Delete
[2560] Fix | Delete
wp_register_style( 'global-styles', false );
[2561] Fix | Delete
wp_add_inline_style( 'global-styles', $stylesheet );
[2562] Fix | Delete
wp_enqueue_style( 'global-styles' );
[2563] Fix | Delete
[2564] Fix | Delete
// Add each block as an inline css.
[2565] Fix | Delete
wp_add_global_styles_for_blocks();
[2566] Fix | Delete
}
[2567] Fix | Delete
[2568] Fix | Delete
/**
[2569] Fix | Delete
* Checks if the editor scripts and styles for all registered block types
[2570] Fix | Delete
* should be enqueued on the current screen.
[2571] Fix | Delete
*
[2572] Fix | Delete
* @since 5.6.0
[2573] Fix | Delete
*
[2574] Fix | Delete
* @global WP_Screen $current_screen WordPress current screen object.
[2575] Fix | Delete
*
[2576] Fix | Delete
* @return bool Whether scripts and styles should be enqueued.
[2577] Fix | Delete
*/
[2578] Fix | Delete
function wp_should_load_block_editor_scripts_and_styles() {
[2579] Fix | Delete
global $current_screen;
[2580] Fix | Delete
[2581] Fix | Delete
$is_block_editor_screen = ( $current_screen instanceof WP_Screen ) && $current_screen->is_block_editor();
[2582] Fix | Delete
[2583] Fix | Delete
/**
[2584] Fix | Delete
* Filters the flag that decides whether or not block editor scripts and styles
[2585] Fix | Delete
* are going to be enqueued on the current screen.
[2586] Fix | Delete
*
[2587] Fix | Delete
* @since 5.6.0
[2588] Fix | Delete
*
[2589] Fix | Delete
* @param bool $is_block_editor_screen Current value of the flag.
[2590] Fix | Delete
*/
[2591] Fix | Delete
return apply_filters( 'should_load_block_editor_scripts_and_styles', $is_block_editor_screen );
[2592] Fix | Delete
}
[2593] Fix | Delete
[2594] Fix | Delete
/**
[2595] Fix | Delete
* Checks whether separate styles should be loaded for core blocks.
[2596] Fix | Delete
*
[2597] Fix | Delete
* When this function returns true, other functions ensure that core blocks use their own separate stylesheets.
[2598] Fix | Delete
* When this function returns false, all core blocks will use the single combined 'wp-block-library' stylesheet.
[2599] Fix | Delete
*
[2600] Fix | Delete
* As a side effect, the return value will by default result in block assets to be loaded on demand, via the
[2601] Fix | Delete
* {@see wp_should_load_block_assets_on_demand()} function. This behavior can be separately altered via that function.
[2602] Fix | Delete
*
[2603] Fix | Delete
* This only affects front end and not the block editor screens.
[2604] Fix | Delete
*
[2605] Fix | Delete
* @since 5.8.0
[2606] Fix | Delete
* @see @see wp_should_load_block_assets_on_demand()
[2607] Fix | Delete
* @see wp_enqueue_registered_block_scripts_and_styles()
[2608] Fix | Delete
* @see register_block_style_handle()
[2609] Fix | Delete
*
[2610] Fix | Delete
* @return bool Whether separate core block assets will be loaded.
[2611] Fix | Delete
*/
[2612] Fix | Delete
function wp_should_load_separate_core_block_assets() {
[2613] Fix | Delete
if ( is_admin() || is_feed() || wp_is_rest_endpoint() ) {
[2614] Fix | Delete
return false;
[2615] Fix | Delete
}
[2616] Fix | Delete
[2617] Fix | Delete
/**
[2618] Fix | Delete
* Filters whether block styles should be loaded separately.
[2619] Fix | Delete
*
[2620] Fix | Delete
* Returning false loads all core block assets, regardless of whether they are rendered
[2621] Fix | Delete
* in a page or not. Returning true loads core block assets only when they are rendered.
[2622] Fix | Delete
*
[2623] Fix | Delete
* @since 5.8.0
[2624] Fix | Delete
*
[2625] Fix | Delete
* @param bool $load_separate_assets Whether separate assets will be loaded.
[2626] Fix | Delete
* Default false (all block assets are loaded, even when not used).
[2627] Fix | Delete
*/
[2628] Fix | Delete
return apply_filters( 'should_load_separate_core_block_assets', false );
[2629] Fix | Delete
}
[2630] Fix | Delete
[2631] Fix | Delete
/**
[2632] Fix | Delete
* Checks whether block styles should be loaded only on-render.
[2633] Fix | Delete
*
[2634] Fix | Delete
* When this function returns true, other functions ensure that blocks only load their assets on-render.
[2635] Fix | Delete
* When this function returns false, all block assets are loaded regardless of whether they are rendered in a page.
[2636] Fix | Delete
*
[2637] Fix | Delete
* The default return value depends on the result of {@see wp_should_load_separate_core_block_assets()}, which controls
[2638] Fix | Delete
* whether Core block stylesheets should be loaded separately or via a combined 'wp-block-library' stylesheet.
[2639] Fix | Delete
*
[2640] Fix | Delete
* This only affects front end and not the block editor screens.
[2641] Fix | Delete
*
[2642] Fix | Delete
* @since 6.8.0
[2643] Fix | Delete
* @see wp_should_load_separate_core_block_assets()
[2644] Fix | Delete
*
[2645] Fix | Delete
* @return bool Whether to load block assets only when they are rendered.
[2646] Fix | Delete
*/
[2647] Fix | Delete
function wp_should_load_block_assets_on_demand() {
[2648] Fix | Delete
if ( is_admin() || is_feed() || wp_is_rest_endpoint() ) {
[2649] Fix | Delete
return false;
[2650] Fix | Delete
}
[2651] Fix | Delete
[2652] Fix | Delete
/*
[2653] Fix | Delete
* For backward compatibility, the default return value for this function is based on the return value of
[2654] Fix | Delete
* `wp_should_load_separate_core_block_assets()`. Initially, this function used to control both of these concerns.
[2655] Fix | Delete
*/
[2656] Fix | Delete
$load_assets_on_demand = wp_should_load_separate_core_block_assets();
[2657] Fix | Delete
[2658] Fix | Delete
/**
[2659] Fix | Delete
* Filters whether block styles should be loaded on demand.
[2660] Fix | Delete
*
[2661] Fix | Delete
* Returning false loads all block assets, regardless of whether they are rendered in a page or not.
[2662] Fix | Delete
* Returning true loads block assets only when they are rendered.
[2663] Fix | Delete
*
[2664] Fix | Delete
* The default value of the filter depends on the result of {@see wp_should_load_separate_core_block_assets()},
[2665] Fix | Delete
* which controls whether Core block stylesheets should be loaded separately or via a combined 'wp-block-library'
[2666] Fix | Delete
* stylesheet.
[2667] Fix | Delete
*
[2668] Fix | Delete
* @since 6.8.0
[2669] Fix | Delete
*
[2670] Fix | Delete
* @param bool $load_assets_on_demand Whether to load block assets only when they are rendered.
[2671] Fix | Delete
*/
[2672] Fix | Delete
return apply_filters( 'should_load_block_assets_on_demand', $load_assets_on_demand );
[2673] Fix | Delete
}
[2674] Fix | Delete
[2675] Fix | Delete
/**
[2676] Fix | Delete
* Enqueues registered block scripts and styles, depending on current rendered
[2677] Fix | Delete
* context (only enqueuing editor scripts while in context of the editor).
[2678] Fix | Delete
*
[2679] Fix | Delete
* @since 5.0.0
[2680] Fix | Delete
*
[2681] Fix | Delete
* @global WP_Screen $current_screen WordPress current screen object.
[2682] Fix | Delete
*/
[2683] Fix | Delete
function wp_enqueue_registered_block_scripts_and_styles() {
[2684] Fix | Delete
global $current_screen;
[2685] Fix | Delete
[2686] Fix | Delete
if ( wp_should_load_block_assets_on_demand() ) {
[2687] Fix | Delete
return;
[2688] Fix | Delete
}
[2689] Fix | Delete
[2690] Fix | Delete
$load_editor_scripts_and_styles = is_admin() && wp_should_load_block_editor_scripts_and_styles();
[2691] Fix | Delete
[2692] Fix | Delete
$block_registry = WP_Block_Type_Registry::get_instance();
[2693] Fix | Delete
[2694] Fix | Delete
/*
[2695] Fix | Delete
* Block styles are only enqueued if they're registered. For core blocks, this is only the case if
[2696] Fix | Delete
* `wp_should_load_separate_core_block_assets()` returns true. Otherwise they use the single combined
[2697] Fix | Delete
* 'wp-block-library` stylesheet. See also `register_core_block_style_handles()`.
[2698] Fix | Delete
* Since `wp_enqueue_style()` does not trigger warnings if the style is not registered, it is okay to not cater for
[2699] Fix | Delete
* this behavior here and simply call `wp_enqueue_style()` unconditionally.
[2700] Fix | Delete
*/
[2701] Fix | Delete
foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) {
[2702] Fix | Delete
// Front-end and editor styles.
[2703] Fix | Delete
foreach ( $block_type->style_handles as $style_handle ) {
[2704] Fix | Delete
wp_enqueue_style( $style_handle );
[2705] Fix | Delete
}
[2706] Fix | Delete
[2707] Fix | Delete
// Front-end and editor scripts.
[2708] Fix | Delete
foreach ( $block_type->script_handles as $script_handle ) {
[2709] Fix | Delete
wp_enqueue_script( $script_handle );
[2710] Fix | Delete
}
[2711] Fix | Delete
[2712] Fix | Delete
if ( $load_editor_scripts_and_styles ) {
[2713] Fix | Delete
// Editor styles.
[2714] Fix | Delete
foreach ( $block_type->editor_style_handles as $editor_style_handle ) {
[2715] Fix | Delete
wp_enqueue_style( $editor_style_handle );
[2716] Fix | Delete
}
[2717] Fix | Delete
[2718] Fix | Delete
// Editor scripts.
[2719] Fix | Delete
foreach ( $block_type->editor_script_handles as $editor_script_handle ) {
[2720] Fix | Delete
wp_enqueue_script( $editor_script_handle );
[2721] Fix | Delete
}
[2722] Fix | Delete
}
[2723] Fix | Delete
}
[2724] Fix | Delete
}
[2725] Fix | Delete
[2726] Fix | Delete
/**
[2727] Fix | Delete
* Function responsible for enqueuing the styles required for block styles functionality on the editor and on the frontend.
[2728] Fix | Delete
*
[2729] Fix | Delete
* @since 5.3.0
[2730] Fix | Delete
*
[2731] Fix | Delete
* @global WP_Styles $wp_styles
[2732] Fix | Delete
*/
[2733] Fix | Delete
function enqueue_block_styles_assets() {
[2734] Fix | Delete
global $wp_styles;
[2735] Fix | Delete
[2736] Fix | Delete
$block_styles = WP_Block_Styles_Registry::get_instance()->get_all_registered();
[2737] Fix | Delete
[2738] Fix | Delete
foreach ( $block_styles as $block_name => $styles ) {
[2739] Fix | Delete
foreach ( $styles as $style_properties ) {
[2740] Fix | Delete
if ( isset( $style_properties['style_handle'] ) ) {
[2741] Fix | Delete
[2742] Fix | Delete
// If the site loads block styles on demand, enqueue the stylesheet on render.
[2743] Fix | Delete
if ( wp_should_load_block_assets_on_demand() ) {
[2744] Fix | Delete
add_filter(
[2745] Fix | Delete
'render_block',
[2746] Fix | Delete
static function ( $html, $block ) use ( $block_name, $style_properties ) {
[2747] Fix | Delete
if ( $block['blockName'] === $block_name ) {
[2748] Fix | Delete
wp_enqueue_style( $style_properties['style_handle'] );
[2749] Fix | Delete
}
[2750] Fix | Delete
return $html;
[2751] Fix | Delete
},
[2752] Fix | Delete
10,
[2753] Fix | Delete
2
[2754] Fix | Delete
);
[2755] Fix | Delete
} else {
[2756] Fix | Delete
wp_enqueue_style( $style_properties['style_handle'] );
[2757] Fix | Delete
}
[2758] Fix | Delete
}
[2759] Fix | Delete
if ( isset( $style_properties['inline_style'] ) ) {
[2760] Fix | Delete
[2761] Fix | Delete
// Default to "wp-block-library".
[2762] Fix | Delete
$handle = 'wp-block-library';
[2763] Fix | Delete
[2764] Fix | Delete
// If the site loads block styles on demand, check if the block has a stylesheet registered.
[2765] Fix | Delete
if ( wp_should_load_block_assets_on_demand() ) {
[2766] Fix | Delete
$block_stylesheet_handle = generate_block_asset_handle( $block_name, 'style' );
[2767] Fix | Delete
[2768] Fix | Delete
if ( isset( $wp_styles->registered[ $block_stylesheet_handle ] ) ) {
[2769] Fix | Delete
$handle = $block_stylesheet_handle;
[2770] Fix | Delete
}
[2771] Fix | Delete
}
[2772] Fix | Delete
[2773] Fix | Delete
// Add inline styles to the calculated handle.
[2774] Fix | Delete
wp_add_inline_style( $handle, $style_properties['inline_style'] );
[2775] Fix | Delete
}
[2776] Fix | Delete
}
[2777] Fix | Delete
}
[2778] Fix | Delete
}
[2779] Fix | Delete
[2780] Fix | Delete
/**
[2781] Fix | Delete
* Function responsible for enqueuing the assets required for block styles functionality on the editor.
[2782] Fix | Delete
*
[2783] Fix | Delete
* @since 5.3.0
[2784] Fix | Delete
*/
[2785] Fix | Delete
function enqueue_editor_block_styles_assets() {
[2786] Fix | Delete
$block_styles = WP_Block_Styles_Registry::get_instance()->get_all_registered();
[2787] Fix | Delete
[2788] Fix | Delete
$register_script_lines = array( '( function() {' );
[2789] Fix | Delete
foreach ( $block_styles as $block_name => $styles ) {
[2790] Fix | Delete
foreach ( $styles as $style_properties ) {
[2791] Fix | Delete
$block_style = array(
[2792] Fix | Delete
'name' => $style_properties['name'],
[2793] Fix | Delete
'label' => $style_properties['label'],
[2794] Fix | Delete
);
[2795] Fix | Delete
if ( isset( $style_properties['is_default'] ) ) {
[2796] Fix | Delete
$block_style['isDefault'] = $style_properties['is_default'];
[2797] Fix | Delete
}
[2798] Fix | Delete
$register_script_lines[] = sprintf(
[2799] Fix | Delete
' wp.blocks.registerBlockStyle( \'%s\', %s );',
[2800] Fix | Delete
$block_name,
[2801] Fix | Delete
wp_json_encode( $block_style )
[2802] Fix | Delete
);
[2803] Fix | Delete
}
[2804] Fix | Delete
}
[2805] Fix | Delete
$register_script_lines[] = '} )();';
[2806] Fix | Delete
$inline_script = implode( "\n", $register_script_lines );
[2807] Fix | Delete
[2808] Fix | Delete
wp_register_script( 'wp-block-styles', false, array( 'wp-blocks' ), true, array( 'in_footer' => true ) );
[2809] Fix | Delete
wp_add_inline_script( 'wp-block-styles', $inline_script );
[2810] Fix | Delete
wp_enqueue_script( 'wp-block-styles' );
[2811] Fix | Delete
}
[2812] Fix | Delete
[2813] Fix | Delete
/**
[2814] Fix | Delete
* Enqueues the assets required for the block directory within the block editor.
[2815] Fix | Delete
*
[2816] Fix | Delete
* @since 5.5.0
[2817] Fix | Delete
*/
[2818] Fix | Delete
function wp_enqueue_editor_block_directory_assets() {
[2819] Fix | Delete
wp_enqueue_script( 'wp-block-directory' );
[2820] Fix | Delete
wp_enqueue_style( 'wp-block-directory' );
[2821] Fix | Delete
}
[2822] Fix | Delete
[2823] Fix | Delete
/**
[2824] Fix | Delete
* Enqueues the assets required for the format library within the block editor.
[2825] Fix | Delete
*
[2826] Fix | Delete
* @since 5.8.0
[2827] Fix | Delete
*/
[2828] Fix | Delete
function wp_enqueue_editor_format_library_assets() {
[2829] Fix | Delete
wp_enqueue_script( 'wp-format-library' );
[2830] Fix | Delete
wp_enqueue_style( 'wp-format-library' );
[2831] Fix | Delete
}
[2832] Fix | Delete
[2833] Fix | Delete
/**
[2834] Fix | Delete
* Sanitizes an attributes array into an attributes string to be placed inside a `<script>` tag.
[2835] Fix | Delete
*
[2836] Fix | Delete
* Automatically injects type attribute if needed.
[2837] Fix | Delete
* Used by {@see wp_get_script_tag()} and {@see wp_get_inline_script_tag()}.
[2838] Fix | Delete
*
[2839] Fix | Delete
* @since 5.7.0
[2840] Fix | Delete
*
[2841] Fix | Delete
* @param array $attributes Key-value pairs representing `<script>` tag attributes.
[2842] Fix | Delete
* @return string String made of sanitized `<script>` tag attributes.
[2843] Fix | Delete
*/
[2844] Fix | Delete
function wp_sanitize_script_attributes( $attributes ) {
[2845] Fix | Delete
$html5_script_support = ! is_admin() && ! current_theme_supports( 'html5', 'script' );
[2846] Fix | Delete
$attributes_string = '';
[2847] Fix | Delete
[2848] Fix | Delete
/*
[2849] Fix | Delete
* If HTML5 script tag is supported, only the attribute name is added
[2850] Fix | Delete
* to $attributes_string for entries with a boolean value, and that are true.
[2851] Fix | Delete
*/
[2852] Fix | Delete
foreach ( $attributes as $attribute_name => $attribute_value ) {
[2853] Fix | Delete
if ( is_bool( $attribute_value ) ) {
[2854] Fix | Delete
if ( $attribute_value ) {
[2855] Fix | Delete
$attributes_string .= $html5_script_support ? sprintf( ' %1$s="%2$s"', esc_attr( $attribute_name ), esc_attr( $attribute_name ) ) : ' ' . esc_attr( $attribute_name );
[2856] Fix | Delete
}
[2857] Fix | Delete
} else {
[2858] Fix | Delete
$attributes_string .= sprintf( ' %1$s="%2$s"', esc_attr( $attribute_name ), esc_attr( $attribute_value ) );
[2859] Fix | Delete
}
[2860] Fix | Delete
}
[2861] Fix | Delete
[2862] Fix | Delete
return $attributes_string;
[2863] Fix | Delete
}
[2864] Fix | Delete
[2865] Fix | Delete
/**
[2866] Fix | Delete
* Formats `<script>` loader tags.
[2867] Fix | Delete
*
[2868] Fix | Delete
* It is possible to inject attributes in the `<script>` tag via the {@see 'wp_script_attributes'} filter.
[2869] Fix | Delete
* Automatically injects type attribute if needed.
[2870] Fix | Delete
*
[2871] Fix | Delete
* @since 5.7.0
[2872] Fix | Delete
*
[2873] Fix | Delete
* @param array $attributes Key-value pairs representing `<script>` tag attributes.
[2874] Fix | Delete
* @return string String containing `<script>` opening and closing tags.
[2875] Fix | Delete
*/
[2876] Fix | Delete
function wp_get_script_tag( $attributes ) {
[2877] Fix | Delete
if ( ! isset( $attributes['type'] ) && ! is_admin() && ! current_theme_supports( 'html5', 'script' ) ) {
[2878] Fix | Delete
// Keep the type attribute as the first for legacy reasons (it has always been this way in core).
[2879] Fix | Delete
$attributes = array_merge(
[2880] Fix | Delete
array( 'type' => 'text/javascript' ),
[2881] Fix | Delete
$attributes
[2882] Fix | Delete
);
[2883] Fix | Delete
}
[2884] Fix | Delete
/**
[2885] Fix | Delete
* Filters attributes to be added to a script tag.
[2886] Fix | Delete
*
[2887] Fix | Delete
* @since 5.7.0
[2888] Fix | Delete
*
[2889] Fix | Delete
* @param array $attributes Key-value pairs representing `<script>` tag attributes.
[2890] Fix | Delete
* Only the attribute name is added to the `<script>` tag for
[2891] Fix | Delete
* entries with a boolean value, and that are true.
[2892] Fix | Delete
*/
[2893] Fix | Delete
$attributes = apply_filters( 'wp_script_attributes', $attributes );
[2894] Fix | Delete
[2895] Fix | Delete
return sprintf( "<script%s></script>\n", wp_sanitize_script_attributes( $attributes ) );
[2896] Fix | Delete
}
[2897] Fix | Delete
[2898] Fix | Delete
/**
[2899] Fix | Delete
* Prints formatted `<script>` loader tag.
[2900] Fix | Delete
*
[2901] Fix | Delete
* It is possible to inject attributes in the `<script>` tag via the {@see 'wp_script_attributes'} filter.
[2902] Fix | Delete
* Automatically injects type attribute if needed.
[2903] Fix | Delete
*
[2904] Fix | Delete
* @since 5.7.0
[2905] Fix | Delete
*
[2906] Fix | Delete
* @param array $attributes Key-value pairs representing `<script>` tag attributes.
[2907] Fix | Delete
*/
[2908] Fix | Delete
function wp_print_script_tag( $attributes ) {
[2909] Fix | Delete
echo wp_get_script_tag( $attributes );
[2910] Fix | Delete
}
[2911] Fix | Delete
[2912] Fix | Delete
/**
[2913] Fix | Delete
* Constructs an inline script tag.
[2914] Fix | Delete
*
[2915] Fix | Delete
* It is possible to inject attributes in the `<script>` tag via the {@see 'wp_script_attributes'} filter.
[2916] Fix | Delete
* Automatically injects type attribute if needed.
[2917] Fix | Delete
*
[2918] Fix | Delete
* @since 5.7.0
[2919] Fix | Delete
*
[2920] Fix | Delete
* @param string $data Data for script tag: JavaScript, importmap, speculationrules, etc.
[2921] Fix | Delete
* @param array $attributes Optional. Key-value pairs representing `<script>` tag attributes.
[2922] Fix | Delete
* @return string String containing inline JavaScript code wrapped around `<script>` tag.
[2923] Fix | Delete
*/
[2924] Fix | Delete
function wp_get_inline_script_tag( $data, $attributes = array() ) {
[2925] Fix | Delete
$is_html5 = current_theme_supports( 'html5', 'script' ) || is_admin();
[2926] Fix | Delete
if ( ! isset( $attributes['type'] ) && ! $is_html5 ) {
[2927] Fix | Delete
// Keep the type attribute as the first for legacy reasons (it has always been this way in core).
[2928] Fix | Delete
$attributes = array_merge(
[2929] Fix | Delete
array( 'type' => 'text/javascript' ),
[2930] Fix | Delete
$attributes
[2931] Fix | Delete
);
[2932] Fix | Delete
}
[2933] Fix | Delete
[2934] Fix | Delete
/*
[2935] Fix | Delete
* XHTML extracts the contents of the SCRIPT element and then the XML parser
[2936] Fix | Delete
* decodes character references and other syntax elements. This can lead to
[2937] Fix | Delete
* misinterpretation of the script contents or invalid XHTML documents.
[2938] Fix | Delete
*
[2939] Fix | Delete
* Wrapping the contents in a CDATA section instructs the XML parser not to
[2940] Fix | Delete
* transform the contents of the SCRIPT element before passing them to the
[2941] Fix | Delete
* JavaScript engine.
[2942] Fix | Delete
*
[2943] Fix | Delete
* Example:
[2944] Fix | Delete
*
[2945] Fix | Delete
* <script>console.log('&hellip;');</script>
[2946] Fix | Delete
*
[2947] Fix | Delete
* In an HTML document this would print "&hellip;" to the console,
[2948] Fix | Delete
* but in an XHTML document it would print "…" to the console.
[2949] Fix | Delete
*
[2950] Fix | Delete
* <script>console.log('An image is <img> in HTML');</script>
[2951] Fix | Delete
*
[2952] Fix | Delete
* In an HTML document this would print "An image is <img> in HTML",
[2953] Fix | Delete
* but it's an invalid XHTML document because it interprets the `<img>`
[2954] Fix | Delete
* as an empty tag missing its closing `/`.
[2955] Fix | Delete
*
[2956] Fix | Delete
* @see https://www.w3.org/TR/xhtml1/#h-4.8
[2957] Fix | Delete
*/
[2958] Fix | Delete
if (
[2959] Fix | Delete
! $is_html5 &&
[2960] Fix | Delete
(
[2961] Fix | Delete
! isset( $attributes['type'] ) ||
[2962] Fix | Delete
'module' === $attributes['type'] ||
[2963] Fix | Delete
str_contains( $attributes['type'], 'javascript' ) ||
[2964] Fix | Delete
str_contains( $attributes['type'], 'ecmascript' ) ||
[2965] Fix | Delete
str_contains( $attributes['type'], 'jscript' ) ||
[2966] Fix | Delete
str_contains( $attributes['type'], 'livescript' )
[2967] Fix | Delete
)
[2968] Fix | Delete
) {
[2969] Fix | Delete
/*
[2970] Fix | Delete
* If the string `]]>` exists within the JavaScript it would break
[2971] Fix | Delete
* out of any wrapping CDATA section added here, so to start, it's
[2972] Fix | Delete
* necessary to escape that sequence which requires splitting the
[2973] Fix | Delete
* content into two CDATA sections wherever it's found.
[2974] Fix | Delete
*
[2975] Fix | Delete
* Note: it's only necessary to escape the closing `]]>` because
[2976] Fix | Delete
* an additional `<![CDATA[` leaves the contents unchanged.
[2977] Fix | Delete
*/
[2978] Fix | Delete
$data = str_replace( ']]>', ']]]]><![CDATA[>', $data );
[2979] Fix | Delete
[2980] Fix | Delete
// Wrap the entire escaped script inside a CDATA section.
[2981] Fix | Delete
$data = sprintf( "/* <![CDATA[ */\n%s\n/* ]]> */", $data );
[2982] Fix | Delete
}
[2983] Fix | Delete
[2984] Fix | Delete
$data = "\n" . trim( $data, "\n\r " ) . "\n";
[2985] Fix | Delete
[2986] Fix | Delete
/**
[2987] Fix | Delete
* Filters attributes to be added to a script tag.
[2988] Fix | Delete
*
[2989] Fix | Delete
* @since 5.7.0
[2990] Fix | Delete
*
[2991] Fix | Delete
* @param array $attributes Key-value pairs representing `<script>` tag attributes.
[2992] Fix | Delete
* Only the attribute name is added to the `<script>` tag for
[2993] Fix | Delete
* entries with a boolean value, and that are true.
[2994] Fix | Delete
* @param string $data Inline data.
[2995] Fix | Delete
*/
[2996] Fix | Delete
$attributes = apply_filters( 'wp_inline_script_attributes', $attributes, $data );
[2997] Fix | Delete
[2998] Fix | Delete
return sprintf( "<script%s>%s</script>\n", wp_sanitize_script_attributes( $attributes ), $data );
[2999] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function