Skip to content

Commit 2004e9f

Browse files
committed
Administration: Use script helper for list table JS vars.
Print the inline scripts in `WP_List_Table::_js_vars()` and `WP_Themes_List_Table::_js_vars()` via `wp_print_inline_script_tag()` instead of manually constructing `SCRIPT` tags with `printf()`. This allows attributes to be added via the `wp_inline_script_attributes` filter, such as a nonce needed to opt in to a Strict Content Security Policy. This affects the Comments screen, the Activity dashboard widget, and the Comments meta box on the post edit screen. The `list_args` global is now also declared with `var` so that the assignment remains valid in strict mode. Developed in #13646. Follow-up to r56687, r63481, r63545. Props sjackson0109, westonruter. See #59446. git-svn-id: https://develop.svn.wordpress.org/trunk@63878 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 1be36a0 commit 2004e9f

3 files changed

Lines changed: 65 additions & 2 deletions

File tree

‎src/wp-admin/includes/class-wp-list-table.php‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1922,6 +1922,8 @@ public function ajax_response() {
19221922
* Sends required variables to JavaScript land.
19231923
*
19241924
* @since 3.1.0
1925+
* @since 7.2.0 Prints the script through wp_print_inline_script_tag() so it can carry
1926+
* attributes, such as a per-request nonce, added via wp_inline_script_attributes.
19251927
*/
19261928
public function _js_vars() {
19271929
$args = array(
@@ -1932,6 +1934,8 @@ public function _js_vars() {
19321934
),
19331935
);
19341936

1935-
printf( "<script>list_args = %s;</script>\n", wp_json_encode( $args, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) );
1937+
wp_print_inline_script_tag(
1938+
sprintf( 'var list_args = %s;', wp_json_encode( $args, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) )
1939+
);
19361940
}
19371941
}

‎src/wp-admin/includes/class-wp-themes-list-table.php‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,8 @@ public function search_theme( $theme ) {
340340
* Send required variables to JavaScript land
341341
*
342342
* @since 3.4.0
343+
* @since 7.2.0 Prints the script through wp_print_inline_script_tag() so it can carry
344+
* attributes, such as a per-request nonce, added via wp_inline_script_attributes.
343345
*
344346
* @param array $extra_args
345347
*/
@@ -357,7 +359,9 @@ public function _js_vars( $extra_args = array() ) {
357359
$args = array_merge( $args, $extra_args );
358360
}
359361

360-
printf( "<script>var theme_list_args = %s;</script>\n", wp_json_encode( $args, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) );
362+
wp_print_inline_script_tag(
363+
sprintf( 'var theme_list_args = %s;', wp_json_encode( $args, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) )
364+
);
361365
parent::_js_vars();
362366
}
363367
}

‎tests/phpunit/tests/admin/wpListTable.php‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,4 +601,59 @@ public function test_search_box_works_with_orderby_string() {
601601

602602
$this->assertStringContainsString( $expected_html, $actual );
603603
}
604+
605+
/**
606+
* Tests that `WP_List_Table::_js_vars()` prints its data through
607+
* `wp_print_inline_script_tag()`, so attributes such as a per-request nonce
608+
* can be attached via the `wp_inline_script_attributes` filter.
609+
*
610+
* @ticket 59446
611+
*
612+
* @covers WP_List_Table::_js_vars
613+
*/
614+
public function test_js_vars_prints_inline_script_tag_with_filterable_attributes() {
615+
add_filter(
616+
'wp_inline_script_attributes',
617+
static function ( array $attributes ): array {
618+
$attributes['nonce'] = 'test-list-table-nonce';
619+
return $attributes;
620+
}
621+
);
622+
623+
$actual = get_echo( array( $this->list_table, '_js_vars' ) );
624+
625+
$processor = new WP_HTML_Tag_Processor( $actual );
626+
$this->assertTrue( $processor->next_tag( 'SCRIPT' ), 'The expected SCRIPT tag was not printed.' );
627+
$this->assertSame( 'test-list-table-nonce', $processor->get_attribute( 'nonce' ), 'The nonce attribute added via wp_inline_script_attributes was not printed.' );
628+
$this->assertStringContainsString( 'list_args =', $processor->get_modifiable_text(), 'The expected JavaScript variable was not printed.' );
629+
}
630+
631+
/**
632+
* Tests that `WP_Themes_List_Table::_js_vars()` prints its data through
633+
* `wp_print_inline_script_tag()`, so attributes such as a per-request nonce
634+
* can be attached via the `wp_inline_script_attributes` filter.
635+
*
636+
* @ticket 59446
637+
*
638+
* @covers WP_Themes_List_Table::_js_vars
639+
*/
640+
public function test_themes_list_table_js_vars_prints_inline_script_tag_with_filterable_attributes() {
641+
$themes_list_table = _get_list_table( WP_Themes_List_Table::class, array( 'screen' => 'themes' ) );
642+
$this->assertInstanceOf( WP_Themes_List_Table::class, $themes_list_table, 'Expected list table class.' );
643+
644+
add_filter(
645+
'wp_inline_script_attributes',
646+
static function ( array $attributes ): array {
647+
$attributes['nonce'] = 'test-themes-list-table-nonce';
648+
return $attributes;
649+
}
650+
);
651+
652+
$actual = get_echo( array( $themes_list_table, '_js_vars' ) );
653+
654+
$processor = new WP_HTML_Tag_Processor( $actual );
655+
$this->assertTrue( $processor->next_tag( 'SCRIPT' ), 'The expected SCRIPT tag was not printed.' );
656+
$this->assertSame( 'test-themes-list-table-nonce', $processor->get_attribute( 'nonce' ), 'The nonce attribute added via wp_inline_script_attributes was not printed.' );
657+
$this->assertStringContainsString( 'theme_list_args =', $processor->get_modifiable_text(), 'The expected JavaScript variable was not printed.' );
658+
}
604659
}

0 commit comments

Comments
 (0)