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

Implement load handlers #50

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
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
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
326555d
use unregister column
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
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
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
38 changes: 36 additions & 2 deletions src/wp-includes/class-wp-scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,16 @@ public function do_item( $handle, $group = false ) {
if ( $after_non_standalone_handle ) {
$initial_type_attr = $this->type_attr;
$this->type_attr = " type='text/template'";
$after_handle .= sprintf( "<script%s id='%s-js-after'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $after_non_standalone_handle );
$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;
}
}

Expand Down Expand Up @@ -414,6 +421,9 @@ public function do_item( $handle, $group = false ) {

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 Down Expand Up @@ -505,7 +515,14 @@ public function print_inline_script( $handle, $position = 'after', $display = tr
if ( 'after-non-standalone' === $position ) {
$initial_type_attr = $this->type_attr;
$this->type_attr = " type='text/template'";
printf( "<script%s id='%s-js-after'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $output );
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 );
Expand Down Expand Up @@ -778,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.
*

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Since annotation is missing.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mukeshpanchal27 We haven't fixed on the final @since yet. We have a separate task for this.

* @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");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
script.setAttribute("type","text/javascript");

WP use $this->type_attr for script type. Please correct me if i miss anything.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mukeshpanchal27 this is the JS code for modifying type after a script is loaded on the DOM. So we can't use $this->type_attr

eval(script.innerHTML);
})
}
JS;
$type_attr = current_theme_supports( 'html5', 'script' ) ? '' : " type='text/javascript'";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could probably use $wp_scripts->type_attr since it's the same functionality, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joemcgill we can't use it here as, type_attr is a private attribute.

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