diff --git a/src/wp-content/themes/twentyfifteen/functions.php b/src/wp-content/themes/twentyfifteen/functions.php index 71b42e6c6e151..614954b258862 100644 --- a/src/wp-content/themes/twentyfifteen/functions.php +++ b/src/wp-content/themes/twentyfifteen/functions.php @@ -412,7 +412,7 @@ function twentyfifteen_fonts_url() { * @since Twenty Fifteen 1.1 */ function twentyfifteen_javascript_detection() { - echo "\n"; + wp_print_inline_script_tag( "(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);" ); } add_action( 'wp_head', 'twentyfifteen_javascript_detection', 0 ); @@ -666,3 +666,115 @@ function twentyfifteen_register_block_patterns() { } add_action( 'init', 'twentyfifteen_register_block_patterns' ); + +if ( ! function_exists( 'wp_get_inline_script_tag' ) ) { + /** + * Constructs an inline script tag. + * + * It is possible to inject attributes in the ` + * + * In an HTML document this would print "…" to the console, + * but in an XHTML document it would print "…" to the console. + * + * + * + * In an HTML document this would print "An image is in HTML", + * but it's an invalid XHTML document because it interprets the `` + * as an empty tag missing its closing `/`. + * + * @see https://www.w3.org/TR/xhtml1/#h-4.8 + */ + if ( + ! $is_html5 && + ( + ! isset( $attributes['type'] ) || + 'module' === $attributes['type'] || + str_contains( $attributes['type'], 'javascript' ) || + str_contains( $attributes['type'], 'ecmascript' ) || + str_contains( $attributes['type'], 'jscript' ) || + str_contains( $attributes['type'], 'livescript' ) + ) + ) { + /* + * If the string `]]>` exists within the JavaScript it would break + * out of any wrapping CDATA section added here, so to start, it's + * necessary to escape that sequence which requires splitting the + * content into two CDATA sections wherever it's found. + * + * Note: it's only necessary to escape the closing `]]>` because + * an additional `', ']]]]>', $data ); + + // Wrap the entire escaped script inside a CDATA section. + $data = sprintf( "/* */", $data ); + } + + $data = "\n" . trim( $data, "\n\r " ) . "\n"; + + /** + * Filters attributes to be added to a script tag. + * + * Added for backward compatibility to support pre-5.7.0 WordPress versions. + * + * @since 5.7.0 + * + * @param array $attributes Key-value pairs representing `\n", wp_sanitize_script_attributes( $attributes ), $data ); + } +} + +if ( ! function_exists( 'wp_print_inline_script_tag' ) ) { + /** + * Prints an inline script tag. + * + * It is possible to inject attributes in the ` - ` tag via the {@see 'wp_inline_script_attributes'} filter. + * Automatically injects type attribute if needed. + * + * Added for backward compatibility to support pre-5.7.0 WordPress versions. + * + * @since 5.7.0 + * + * @param string $data Data for script tag: JavaScript, importmap, speculationrules, etc. + * @param array $attributes Optional. Key-value pairs representing ` + * + * In an HTML document this would print "…" to the console, + * but in an XHTML document it would print "…" to the console. + * + * + * + * In an HTML document this would print "An image is in HTML", + * but it's an invalid XHTML document because it interprets the `` + * as an empty tag missing its closing `/`. + * + * @see https://www.w3.org/TR/xhtml1/#h-4.8 + */ + if ( + ! $is_html5 && + ( + ! isset( $attributes['type'] ) || + 'module' === $attributes['type'] || + str_contains( $attributes['type'], 'javascript' ) || + str_contains( $attributes['type'], 'ecmascript' ) || + str_contains( $attributes['type'], 'jscript' ) || + str_contains( $attributes['type'], 'livescript' ) + ) + ) { + /* + * If the string `]]>` exists within the JavaScript it would break + * out of any wrapping CDATA section added here, so to start, it's + * necessary to escape that sequence which requires splitting the + * content into two CDATA sections wherever it's found. + * + * Note: it's only necessary to escape the closing `]]>` because + * an additional `', ']]]]>', $data ); + + // Wrap the entire escaped script inside a CDATA section. + $data = sprintf( "/* */", $data ); + } + + $data = "\n" . trim( $data, "\n\r " ) . "\n"; + + /** + * Filters attributes to be added to a script tag. + * + * Added for backward compatibility to support pre-5.7.0 WordPress versions. + * + * @since 5.7.0 + * + * @param array $attributes Key-value pairs representing `\n", wp_sanitize_script_attributes( $attributes ), $data ); + } +} + +if ( ! function_exists( 'wp_print_inline_script_tag' ) ) { + /** + * Prints an inline script tag. + * + * It is possible to inject attributes in the `\n"; + wp_print_inline_script_tag( "(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);" ); } add_action( 'wp_head', 'twentyseventeen_javascript_detection', 0 ); @@ -758,3 +758,115 @@ function twentyseventeen_register_block_patterns() { } add_action( 'init', 'twentyseventeen_register_block_patterns' ); + +if ( ! function_exists( 'wp_get_inline_script_tag' ) ) { + /** + * Constructs an inline script tag. + * + * It is possible to inject attributes in the ` + * + * In an HTML document this would print "…" to the console, + * but in an XHTML document it would print "…" to the console. + * + * + * + * In an HTML document this would print "An image is in HTML", + * but it's an invalid XHTML document because it interprets the `` + * as an empty tag missing its closing `/`. + * + * @see https://www.w3.org/TR/xhtml1/#h-4.8 + */ + if ( + ! $is_html5 && + ( + ! isset( $attributes['type'] ) || + 'module' === $attributes['type'] || + str_contains( $attributes['type'], 'javascript' ) || + str_contains( $attributes['type'], 'ecmascript' ) || + str_contains( $attributes['type'], 'jscript' ) || + str_contains( $attributes['type'], 'livescript' ) + ) + ) { + /* + * If the string `]]>` exists within the JavaScript it would break + * out of any wrapping CDATA section added here, so to start, it's + * necessary to escape that sequence which requires splitting the + * content into two CDATA sections wherever it's found. + * + * Note: it's only necessary to escape the closing `]]>` because + * an additional `', ']]]]>', $data ); + + // Wrap the entire escaped script inside a CDATA section. + $data = sprintf( "/* */", $data ); + } + + $data = "\n" . trim( $data, "\n\r " ) . "\n"; + + /** + * Filters attributes to be added to a script tag. + * + * Added for backward compatibility to support pre-5.7.0 WordPress versions. + * + * @since 5.7.0 + * + * @param array $attributes Key-value pairs representing `\n", wp_sanitize_script_attributes( $attributes ), $data ); + } +} + +if ( ! function_exists( 'wp_print_inline_script_tag' ) ) { + /** + * Prints an inline script tag. + * + * It is possible to inject attributes in the `\n"; + wp_print_inline_script_tag( "(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);" ); } add_action( 'wp_head', 'twentysixteen_javascript_detection', 0 ); @@ -622,3 +622,115 @@ function twentysixteen_widget_tag_cloud_args( $args ) { return $args; } add_filter( 'widget_tag_cloud_args', 'twentysixteen_widget_tag_cloud_args' ); + +if ( ! function_exists( 'wp_get_inline_script_tag' ) ) { + /** + * Constructs an inline script tag. + * + * It is possible to inject attributes in the ` + * + * In an HTML document this would print "…" to the console, + * but in an XHTML document it would print "…" to the console. + * + * + * + * In an HTML document this would print "An image is in HTML", + * but it's an invalid XHTML document because it interprets the `` + * as an empty tag missing its closing `/`. + * + * @see https://www.w3.org/TR/xhtml1/#h-4.8 + */ + if ( + ! $is_html5 && + ( + ! isset( $attributes['type'] ) || + 'module' === $attributes['type'] || + str_contains( $attributes['type'], 'javascript' ) || + str_contains( $attributes['type'], 'ecmascript' ) || + str_contains( $attributes['type'], 'jscript' ) || + str_contains( $attributes['type'], 'livescript' ) + ) + ) { + /* + * If the string `]]>` exists within the JavaScript it would break + * out of any wrapping CDATA section added here, so to start, it's + * necessary to escape that sequence which requires splitting the + * content into two CDATA sections wherever it's found. + * + * Note: it's only necessary to escape the closing `]]>` because + * an additional `', ']]]]>', $data ); + + // Wrap the entire escaped script inside a CDATA section. + $data = sprintf( "/* */", $data ); + } + + $data = "\n" . trim( $data, "\n\r " ) . "\n"; + + /** + * Filters attributes to be added to a script tag. + * + * Added for backward compatibility to support pre-5.7.0 WordPress versions. + * + * @since 5.7.0 + * + * @param array $attributes Key-value pairs representing `\n", wp_sanitize_script_attributes( $attributes ), $data ); + } +} + +if ( ! function_exists( 'wp_print_inline_script_tag' ) ) { + /** + * Prints an inline script tag. + * + * It is possible to inject attributes in the ` - ` tag via the {@see 'wp_inline_script_attributes'} filter. + * Automatically injects type attribute if needed. + * + * Added for backward compatibility to support pre-5.7.0 WordPress versions. + * + * @since 5.7.0 + * + * @param string $data Data for script tag: JavaScript, importmap, speculationrules, etc. + * @param array $attributes Optional. Key-value pairs representing ` + * + * In an HTML document this would print "…" to the console, + * but in an XHTML document it would print "…" to the console. + * + * + * + * In an HTML document this would print "An image is in HTML", + * but it's an invalid XHTML document because it interprets the `` + * as an empty tag missing its closing `/`. + * + * @see https://www.w3.org/TR/xhtml1/#h-4.8 + */ + if ( + ! $is_html5 && + ( + ! isset( $attributes['type'] ) || + 'module' === $attributes['type'] || + str_contains( $attributes['type'], 'javascript' ) || + str_contains( $attributes['type'], 'ecmascript' ) || + str_contains( $attributes['type'], 'jscript' ) || + str_contains( $attributes['type'], 'livescript' ) + ) + ) { + /* + * If the string `]]>` exists within the JavaScript it would break + * out of any wrapping CDATA section added here, so to start, it's + * necessary to escape that sequence which requires splitting the + * content into two CDATA sections wherever it's found. + * + * Note: it's only necessary to escape the closing `]]>` because + * an additional `', ']]]]>', $data ); + + // Wrap the entire escaped script inside a CDATA section. + $data = sprintf( "/* */", $data ); + } + + $data = "\n" . trim( $data, "\n\r " ) . "\n"; + + /** + * Filters attributes to be added to a script tag. + * + * Added for backward compatibility to support pre-5.7.0 WordPress versions. + * + * @since 5.7.0 + * + * @param array $attributes Key-value pairs representing `\n", wp_sanitize_script_attributes( $attributes ), $data ); + } +} + +if ( ! function_exists( 'wp_print_inline_script_tag' ) ) { + /** + * Prints an inline script tag. + * + * It is possible to inject attributes in the ` - '; - include get_template_directory() . '/assets/js/dark-mode-toggler.js'; // phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude - echo ''; + $js_path = get_template_directory() . '/assets/js/dark-mode-toggler.js'; + + if ( file_exists( $js_path ) ) { + $script = file_get_contents( $js_path ); + wp_print_inline_script_tag( $script ); + } } /** diff --git a/src/wp-content/themes/twentytwentyone/functions.php b/src/wp-content/themes/twentytwentyone/functions.php index f163e394df734..82164b381f75b 100644 --- a/src/wp-content/themes/twentytwentyone/functions.php +++ b/src/wp-content/themes/twentytwentyone/functions.php @@ -493,19 +493,20 @@ function twentytwentyone_block_editor_script() { * @link https://git.io/vWdr2 */ function twenty_twenty_one_skip_link_focus_fix() { + $script = ''; // If SCRIPT_DEBUG is defined and true, print the unminified file. if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) { - echo ''; + $js_path = get_template_directory() . '/assets/js/skip-link-focus-fix.js'; + if ( file_exists( $js_path ) ) { + $script = file_get_contents( $js_path ); + } } else { // The following is minified via `npx terser --compress --mangle -- assets/js/skip-link-focus-fix.js`. - ?> - - - - ` tag via the {@see 'wp_inline_script_attributes'} filter. + * Automatically injects type attribute if needed. + * + * Added for backward compatibility to support pre-5.7.0 WordPress versions. + * + * @since 5.7.0 + * + * @param string $data Data for script tag: JavaScript, importmap, speculationrules, etc. + * @param array $attributes Optional. Key-value pairs representing ` + * + * In an HTML document this would print "…" to the console, + * but in an XHTML document it would print "…" to the console. + * + * + * + * In an HTML document this would print "An image is in HTML", + * but it's an invalid XHTML document because it interprets the `` + * as an empty tag missing its closing `/`. + * + * @see https://www.w3.org/TR/xhtml1/#h-4.8 + */ + if ( + ! $is_html5 && + ( + ! isset( $attributes['type'] ) || + 'module' === $attributes['type'] || + str_contains( $attributes['type'], 'javascript' ) || + str_contains( $attributes['type'], 'ecmascript' ) || + str_contains( $attributes['type'], 'jscript' ) || + str_contains( $attributes['type'], 'livescript' ) + ) + ) { + /* + * If the string `]]>` exists within the JavaScript it would break + * out of any wrapping CDATA section added here, so to start, it's + * necessary to escape that sequence which requires splitting the + * content into two CDATA sections wherever it's found. + * + * Note: it's only necessary to escape the closing `]]>` because + * an additional `', ']]]]>', $data ); + + // Wrap the entire escaped script inside a CDATA section. + $data = sprintf( "/* */", $data ); + } + + $data = "\n" . trim( $data, "\n\r " ) . "\n"; + + /** + * Filters attributes to be added to a script tag. + * + * Added for backward compatibility to support pre-5.7.0 WordPress versions. + * + * @since 5.7.0 + * + * @param array $attributes Key-value pairs representing `\n", wp_sanitize_script_attributes( $attributes ), $data ); + } +} + +if ( ! function_exists( 'wp_print_inline_script_tag' ) ) { + /** + * Prints an inline script tag. + * + * It is possible to inject attributes in the `'; + wp_print_inline_script_tag( "document.body.classList.remove('no-js');" ); } add_action( 'wp_footer', 'twenty_twenty_one_supports_js' );