Skip to content

Commit

Permalink
Framework: Assign nonce on heartbeat tick via inline script
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Jan 23, 2019
1 parent 45bc050 commit c133d12
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,29 @@ function gutenberg_register_scripts_and_styles() {
gutenberg_register_packages_scripts();

// Inline scripts.
global $wp_scripts;
$wp_scripts->registered['wp-api-fetch']->deps[] = 'wp-hooks';
wp_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'
Expand Down

0 comments on commit c133d12

Please sign in to comment.