Skip to content

Commit 2419dda

Browse files
committed
Administration: Wrap more one-liner inline scripts.
Continuing the migration begun in r63481, the raw one-liner `SCRIPT` tags in `wp-admin/admin-footer.php`, `wp-admin/install.php` and `wp-admin/async-upload.php` now print through `wp_print_inline_script_tag()`, so `wp_inline_script_attributes` can attach a per-request nonce to these as well. The upload error handler is reworked in the process: 1. Its data now crosses into JavaScript as a single `wp_json_encode()` object rather than being interpolated into the script text, which also removes the raw interpolation of the dismiss button's generated `id`. 2. The jQuery object arrives as an argument instead of being read from the global scope four times. 3. `setTimeout()` replaces the lone `_.delay()` call, which was all the snippet wanted from Underscore. The two blocks that take an argument carry a JSDoc block naming its type, following the convention already used for the jQuery wrapper in `wp-admin/js`. These particular blocks are not reached by `lint:jsdoc`, which reads JavaScript files rather than heredocs embedded in PHP, but IDEs do check them. Developed in WordPress#13393. Follow-up to r60263, r60520, r60681, r63481. Props thanhtinpk, westonruter. See #59446, #66033. git-svn-id: https://develop.svn.wordpress.org/trunk@63545 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 46ab13c commit 2419dda

3 files changed

Lines changed: 66 additions & 10 deletions

File tree

‎src/wp-admin/admin-footer.php‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@
114114
?>
115115

116116
<div class="clear"></div></div><!-- wpwrap -->
117-
<script>if(typeof wpOnload==='function')wpOnload();</script>
117+
<?php
118+
wp_print_inline_script_tag(
119+
<<<'JS'
120+
if ( typeof wpOnload === 'function' ) {
121+
wpOnload();
122+
}
123+
JS
124+
);
125+
?>
118126
</body>
119127
</html>

‎src/wp-admin/async-upload.php‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,42 @@
145145
$_FILES['async-upload']['name']
146146
);
147147

148-
echo '<script>_.delay(function() {wp.a11y.speak(' . wp_json_encode( $speak_message, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) . ");}, 1500);jQuery( 'button#{$button_unique_id}' ).on( 'click', function() {jQuery(this).parents('div.media-item').slideUp(200, function(){jQuery(this).remove();wp.a11y.speak( wp.i18n.__( 'Error dismissed.' ) );jQuery( '#plupload-browse-button' ).trigger( 'focus' );})});</script>\n";
148+
$js_function = <<<'JS'
149+
/**
150+
* Announces the upload failure and wires up its dismiss button.
151+
*
152+
* @param {JQueryStatic} $ The jQuery object.
153+
* @param {Object} args PHP exports.
154+
* @param {string} args.speakMessage Message announced to assistive technology.
155+
* @param {string} args.buttonSelector Selector for the notice's dismiss button.
156+
* @return {void}
157+
*/
158+
( $, { speakMessage, buttonSelector } ) => {
159+
setTimeout( () => wp.a11y.speak( speakMessage ), 1500 );
160+
$( buttonSelector ).on( 'click', function () {
161+
$( this )
162+
.parents( 'div.media-item' )
163+
.slideUp( 200, function () {
164+
$( this ).remove();
165+
wp.a11y.speak( wp.i18n.__( 'Error dismissed.' ) );
166+
$( '#plupload-browse-button' ).trigger( 'focus' );
167+
} );
168+
} );
169+
}
170+
JS;
171+
wp_print_inline_script_tag(
172+
sprintf(
173+
'( %s )( jQuery, %s )',
174+
$js_function,
175+
wp_json_encode(
176+
array(
177+
'speakMessage' => $speak_message,
178+
'buttonSelector' => 'button#' . $button_unique_id,
179+
),
180+
JSON_HEX_TAG | JSON_UNESCAPED_SLASHES
181+
)
182+
)
183+
);
149184
exit;
150185
}
151186

‎src/wp-admin/install.php‎

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -470,17 +470,30 @@ function display_setup_form( $error = null ) {
470470
}
471471

472472
if ( ! wp_is_mobile() ) {
473-
?>
474-
<script>var t = document.getElementById('weblog_title'); if (t){ t.focus(); }</script>
475-
<?php
473+
wp_print_inline_script_tag(
474+
<<<'JS'
475+
const t = document.getElementById( 'weblog_title' );
476+
if ( t ) {
477+
t.focus();
478+
}
479+
JS
480+
);
476481
}
477482

478483
wp_print_scripts( $scripts_to_print );
484+
485+
wp_print_inline_script_tag(
486+
<<<'JS'
487+
/**
488+
* Shows the content intended only for browsers with JS enabled.
489+
*
490+
* @param {JQueryStatic} $ The jQuery object.
491+
*/
492+
jQuery( function ( $ ) {
493+
$( '.hide-if-no-js' ).removeClass( 'hide-if-no-js' );
494+
} );
495+
JS
496+
);
479497
?>
480-
<script>
481-
jQuery( function( $ ) {
482-
$( '.hide-if-no-js' ).removeClass( 'hide-if-no-js' );
483-
} );
484-
</script>
485498
</body>
486499
</html>

0 commit comments

Comments
 (0)