wp_remove_surrounding_empty_script_tags()
Removes leading and trailing _empty_ script tags.
This is a helper meant to be used for literal script tag construction within wp_get_inline_script_tag() wp_print_inline_script_tag(). It removes the literal values of "<script>" and "</script>" from around an inline script after trimming whitespace. Typically this is used in conjunction with output buffering, where ob_get_clean() is passed as the $contents argument.
Example:
// Strips exact literal empty SCRIPT tags. $js = '<script>sayHello();</script>; 'sayHello();' === wp_remove_surrounding_empty_script_tags( $js );
// Otherwise if anything is different it warns in the JS console. $js = '<script type="text/javascript">console.log( "hi" );</script>'; 'console.error( ... )' === wp_remove_surrounding_empty_script_tags( $js );
Внутренняя функция — эта функция рассчитана на использование самим ядром. Не рекомендуется использовать эту функцию в своем коде.
Хуков нет.
Возвращает
Строку
. Script body without surrounding script tag literals, or original contents if both exact literals aren't present.
Использование
wp_remove_surrounding_empty_script_tags( $contents );
- $contents(строка) (обязательный)
- Script body with manually created SCRIPT tag literals.
Заметки
- Смотрите: wp_print_inline_script_tag()
- Смотрите: wp_get_inline_script_tag()
Список изменений
С версии 6.4.0 | Введена. |
Код wp_remove_surrounding_empty_script_tags() wp remove surrounding empty script tags WP 6.7.1
function wp_remove_surrounding_empty_script_tags( $contents ) { $contents = trim( $contents ); $opener = '<SCRIPT>'; $closer = '</SCRIPT>'; if ( strlen( $contents ) > strlen( $opener ) + strlen( $closer ) && strtoupper( substr( $contents, 0, strlen( $opener ) ) ) === $opener && strtoupper( substr( $contents, -strlen( $closer ) ) ) === $closer ) { return substr( $contents, strlen( $opener ), -strlen( $closer ) ); } else { $error_message = __( 'Expected string to start with script tag (without attributes) and end with script tag, with optional whitespace.' ); _doing_it_wrong( __FUNCTION__, $error_message, '6.4' ); return sprintf( 'console.error(%s)', wp_json_encode( sprintf( /* translators: %s: wp_remove_surrounding_empty_script_tags() */ __( 'Function %s used incorrectly in PHP.' ), 'wp_remove_surrounding_empty_script_tags()' ) . ' ' . $error_message ) ); } }