Skip to content

Instantly share code, notes, and snippets.

@aduth
Created January 23, 2019 21:57
Show Gist options
  • Save aduth/be5b13bfebec880d5cda3d5f6844e8f0 to your computer and use it in GitHub Desktop.
Save aduth/be5b13bfebec880d5cda3d5f6844e8f0 to your computer and use it in GitHub Desktop.
diff --git src/wp-includes/script-loader.php src/wp-includes/script-loader.php
index 1bc36791db..7dfc7c1295 100644
--- src/wp-includes/script-loader.php
+++ src/wp-includes/script-loader.php
@@ -500,11 +500,30 @@ function wp_default_packages_scripts( &$scripts ) {
*/
function wp_default_packages_inline_scripts( &$scripts ) {
global $wp_locale;
+ global $wp_scripts;
+ $wp_scripts->registered['wp-api-fetch']->deps[] = 'wp-hooks';
$scripts->add_inline_script(
'wp-api-fetch',
sprintf(
- 'wp.apiFetch.use( wp.apiFetch.createNonceMiddleware( "%s" ) );',
+ implode(
+ "\n",
+ array(
+ '( function() {',
+ ' var nonceMiddleware = wp.apiFetch.createNonceMiddleware( "%s" );',
+ ' wp.apiFetch.use( nonceMiddleware );',
+ ' wp.hooks.addAction(',
+ ' "heartbeat.tick",',
+ ' "core/api-fetch/create-nonce-middleware",',
+ ' function( response ) {',
+ ' if ( response[ "rest_nonce" ] ) {',
+ ' nonceMiddleware.nonce = response[ "rest_nonce" ];',
+ ' }',
+ ' }',
+ ' )',
+ '} )()',
+ )
+ ),
( wp_installing() && ! is_multisite() ) ? '' : wp_create_nonce( 'wp_rest' )
),
'after'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment