Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add text template #49

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
dcd2a32
standalone and non-standalone
kt-12 Mar 10, 2023
8d33fac
phpcs fixes
kt-12 Mar 10, 2023
e4b461d
set defaults
kt-12 Mar 10, 2023
8421a1c
update print_inline_script for stand alone
kt-12 Mar 10, 2023
371e42f
fix issue with standalone appending
kt-12 Mar 10, 2023
c517bc1
bug fix incorrect strategy check
kt-12 Mar 10, 2023
04aabf5
numbered attributes
kt-12 Mar 10, 2023
e366d5d
onload script
kt-12 Mar 10, 2023
0fafd88
fix bug
kt-12 Mar 10, 2023
386022e
sprintf issue with \n
kt-12 Mar 11, 2023
fe7acc0
phpcs fixes
kt-12 Mar 11, 2023
394405f
print template loader script
kt-12 Mar 11, 2023
cc5dace
\n to EOL
kt-12 Mar 11, 2023
b558f34
Loading the script before other script
kt-12 Mar 13, 2023
f04a246
temp commit
kt-12 Mar 14, 2023
b5f4df4
check if any delayed inline scripts
kt-12 Mar 15, 2023
e0e560e
non-stanadalone after script test cases
kt-12 Mar 15, 2023
c856372
test standalone and non-standalone script combined
kt-12 Mar 15, 2023
ff25567
phpcs fix
kt-12 Mar 15, 2023
b2d6ec1
for consistency
kt-12 Mar 15, 2023
d9a0f39
Merge add_text_template to implement_load_handlers
kt-12 Mar 15, 2023
d110556
add new line
kt-12 Mar 16, 2023
b6b7022
backward compatibility
kt-12 Mar 16, 2023
9e8c378
update test utility and scripts.
kt-12 Mar 16, 2023
ffc40c1
php cs fixes
kt-12 Mar 16, 2023
8af918c
fix csfix issue
kt-12 Mar 16, 2023
d952da5
Update test message.
kt-12 Mar 21, 2023
b71b557
Update test comment
kt-12 Mar 21, 2023
479d656
Update test error message
kt-12 Mar 21, 2023
968d5b7
change function names
kt-12 Mar 21, 2023
ad9d9c3
Merge branch 'enhancement/implement_load_handlers' of https://github.…
kt-12 Mar 21, 2023
d522c46
Update doc header.
kt-12 Mar 22, 2023
ce5fc87
Update comment text
kt-12 Mar 22, 2023
0a0b253
comment text grammar.
kt-12 Mar 22, 2023
a24665a
update doc block
kt-12 Mar 22, 2023
f29c144
comment grammar fixes
kt-12 Mar 22, 2023
0988655
comment grammar fixes
kt-12 Mar 22, 2023
d66907e
comment grammar
kt-12 Mar 22, 2023
b7bff57
doc block grammar
kt-12 Mar 22, 2023
013cb1e
comment grammar
kt-12 Mar 22, 2023
db02925
update doc block for grammar
kt-12 Mar 22, 2023
42bc576
updated grammar
kt-12 Mar 22, 2023
326555d
use unregister column
kt-12 Mar 22, 2023
8d5729c
Merge branch 'enhancement/update-wp-add-inline-script-standalone' int…
kt-12 Mar 22, 2023
6187dd1
Merge branch 'enhancement/add_text_template' into enhancement/impleme…
kt-12 Mar 22, 2023
1321d6f
avoid removal of jquery libraries
kt-12 Mar 23, 2023
66ab60c
Dataprovider doc block.
kt-12 Mar 23, 2023
bbdad37
doc block change
kt-12 Mar 23, 2023
966be0c
Update src/wp-includes/class-wp-scripts.php
kt-12 Mar 28, 2023
dcbf0af
Update doc text
kt-12 Mar 31, 2023
01c890e
bug: multiple type in a single script.
kt-12 Mar 31, 2023
9aaca52
print_display
kt-12 Mar 31, 2023
8e08454
Merge branch 'enhancement/add_text_template' into enhancement/impleme…
kt-12 Mar 31, 2023
b3f7225
fix type_attr no there issue
kt-12 Mar 31, 2023
4df5288
adding description
kt-12 Mar 31, 2023
264853c
broke standalone script test to multiple function
kt-12 Mar 31, 2023
556ae91
non-standalone after script test broken
kt-12 Mar 31, 2023
d4be88d
before standalone test broken
kt-12 Mar 31, 2023
180a6ae
non standalone with before and defer
kt-12 Mar 31, 2023
4d0140d
used function instead of arrow function.
kt-12 Mar 31, 2023
a5b83a4
single condition
kt-12 Mar 31, 2023
0269128
put back handle
kt-12 Mar 31, 2023
4cc2502
Merge pull request #50 from 10up/enhancement/implement_load_handlers
10upsimon Apr 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 67 additions & 6 deletions src/wp-includes/class-wp-scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,15 +294,44 @@ public function do_item( $handle, $group = false ) {
$cond_after = "<![endif]-->\n";
}

$strategy = $this->get_eligible_loading_strategy( $handle );

$before_handle = $this->print_inline_script( $handle, 'before', false );
$after_handle = $this->print_inline_script( $handle, 'after', false );

if ( $before_handle ) {
$before_handle = sprintf( "<script%s id='%s-js-before'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $before_handle );
}

if ( $after_handle ) {
$after_handle = sprintf( "<script%s id='%s-js-after'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $after_handle );
$after_handle = '';
if ( '' === $strategy ) {
$after_handle = $this->print_inline_script( $handle, 'after', false );

if ( $after_handle ) {
$after_handle = sprintf( "<script%s id='%s-js-after'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $after_handle );
}
} else {
$after_standalone_handle = $this->print_inline_script( $handle, 'after-standalone', false );

if ( $after_standalone_handle ) {
$after_handle .= sprintf( "<script%s id='%s-js-after'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $after_standalone_handle );
}

$after_non_standalone_handle = $this->print_inline_script( $handle, 'after-non-standalone', false );

if ( $after_non_standalone_handle ) {
$initial_type_attr = $this->type_attr;
$this->type_attr = " type='text/template'";
$after_handle .= sprintf(
'<script%1$s id=\'%2$s-js-after\' data-wp-executes-after=\'%2$s\'>%4$s%3$s%4$s</script>%4$s',
$this->type_attr,
esc_attr( $handle ),
$after_non_standalone_handle,
PHP_EOL
);
$this->type_attr = $initial_type_attr;

$this->has_load_later_inline = true;
}
}

if ( $before_handle || $after_handle ) {
Expand Down Expand Up @@ -390,9 +419,11 @@ public function do_item( $handle, $group = false ) {
return true;
}

$strategy = $this->get_eligible_loading_strategy( $handle );
if ( '' !== $strategy ) {
$strategy = ' ' . $strategy;
if ( ! empty( $after_non_standalone_handle ) ) {
$strategy .= sprintf( " onload='wpLoadAfterScripts(\"%s\")'", esc_attr( $handle ) );
}
}
$tag = $translations . $cond_before . $before_handle;
$tag .= sprintf(
Expand All @@ -402,7 +433,6 @@ public function do_item( $handle, $group = false ) {
esc_attr( $handle ),
$strategy
);
// TODO: Handle onload logic for defer/async here.
$tag .= $after_handle . $cond_after;

/**
Expand Down Expand Up @@ -482,7 +512,21 @@ public function print_inline_script( $handle, $position = 'after', $display = tr
$output = trim( implode( "\n", $output ), "\n" );

if ( $display ) {
printf( "<script%s id='%s-js-%s'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), esc_attr( $position ), $output );
if ( 'after-non-standalone' === $position ) {
$initial_type_attr = $this->type_attr;
$this->type_attr = " type='text/template'";
printf(
'<script%1$s id=\'%2$s-js-after\' type=\'text/template\' data-wp-executes-after=\'%2$s\'>%5$s%4$s%5$s</script>%5$s',
$this->type_attr,
esc_attr( $handle ),
esc_attr( $position ),
$output,
PHP_EOL
);
$this->type_attr = $initial_type_attr;
} else {
printf( "<script%s id='%s-js-%s'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), esc_attr( $position ), $output );
}
}

return $output;
Expand Down Expand Up @@ -751,6 +795,23 @@ public function add_data( $handle, $key, $value ) {
return parent::add_data( $handle, $key, $value );
}

/**
* Checks all handles for any delayed inline scripts.
*
* @return bool True if the inline script present, otherwise false.
*/
public function has_delayed_inline_script() {
foreach ( $this->registered as $handle => $script ) {
// non standalone after scripts of async or defer are usually delayed.
if ( in_array( $this->get_intended_strategy( $handle ), array( 'defer', 'async' ), true ) &&
$this->has_non_standalone_inline_script( $handle, 'after' )
) {
return true;
}
}
return false;
}

/**
* Normalize the data inside the $args parameter and support backward compatibility.
*
Expand Down
1 change: 1 addition & 0 deletions src/wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,7 @@
add_action( 'enqueue_block_editor_assets', 'wp_enqueue_editor_format_library_assets' );
add_action( 'enqueue_block_editor_assets', 'wp_enqueue_global_styles_css_custom_properties' );
add_filter( 'wp_print_scripts', 'wp_just_in_time_script_localization' );
add_action( 'wp_print_scripts', 'wp_print_template_loader_script' );
add_filter( 'print_scripts_array', 'wp_prototype_before_jquery' );
add_filter( 'customize_controls_print_styles', 'wp_resource_hints', 1 );
add_action( 'admin_head', 'wp_check_widget_editor_deps' );
Expand Down
24 changes: 24 additions & 0 deletions src/wp-includes/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -1843,6 +1843,30 @@ function wp_just_in_time_script_localization() {
);
}


/**
* Prints a loader script if there is text/template registered script.
*
* When injected in DOM, this script converts any text/template script
* associated with a handle to type/javascript and execute them.
*/
function wp_print_template_loader_script() {
$wp_scripts = wp_scripts();
if ( $wp_scripts->has_delayed_inline_script() ) {
$output = <<<JS
function wpLoadAfterScripts( handle ) {
let scripts = document.querySelectorAll(`[type="text/template"][data-wp-executes-after="\${handle}"]`);
scripts.forEach( (script) => {
script.setAttribute("type","text/javascript");
eval(script.innerHTML);
})
}
JS;
$type_attr = current_theme_supports( 'html5', 'script' ) ? '' : " type='text/javascript'";
printf( "<script%s id='wp-executes-after-js'>\n%s\n</script>\n", $type_attr, $output );
}
}

/**
* Localizes the jQuery UI datepicker.
*
Expand Down
44 changes: 44 additions & 0 deletions tests/phpunit/includes/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -645,3 +645,47 @@ function test_rest_expand_compact_links( $links ) {
}
return $links;
}

/**
* Removes all handles from $wp_script.
*/
function unregister_all_script_handles() {
global $wp_scripts;

/**
* Do not deregister following library through this function.
*/
$libraries = array(
'jquery',
'jquery-core',
'jquery-migrate',
'jquery-ui-core',
'jquery-ui-accordion',
'jquery-ui-autocomplete',
'jquery-ui-button',
'jquery-ui-datepicker',
'jquery-ui-dialog',
'jquery-ui-draggable',
'jquery-ui-droppable',
'jquery-ui-menu',
'jquery-ui-mouse',
'jquery-ui-position',
'jquery-ui-progressbar',
'jquery-ui-resizable',
'jquery-ui-selectable',
'jquery-ui-slider',
'jquery-ui-sortable',
'jquery-ui-spinner',
'jquery-ui-tabs',
'jquery-ui-tooltip',
'jquery-ui-widget',
'backbone',
'underscore',
);

foreach ( $wp_scripts->registered as $handle_name => $handle ) {
if ( ! in_array( $handle_name, $libraries, true ) ) {
wp_deregister_script( $handle_name );
}
}
}
Loading