Block Editor: Refresh nonces used by wp.apiFetch.

Adds heartbeat nonces refreshing support to wp.apiFetch requests.

Props pento, adamsilverstein, dd32, desrosj.
Fixes #45113. 

Built from https://develop.svn.wordpress.org/branches/5.0@43939


git-svn-id: http://core.svn.wordpress.org/branches/5.0@43771 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
youknowriad 2018-11-22 19:40:46 +00:00
parent a69e8cf788
commit 97e84f8a03
6 changed files with 44 additions and 5 deletions

View File

@ -68,9 +68,11 @@ add_action( 'update_option_new_admin_email', 'update_option_new_admin_email', 10
add_filter( 'heartbeat_received', 'wp_check_locked_posts', 10, 3 ); add_filter( 'heartbeat_received', 'wp_check_locked_posts', 10, 3 );
add_filter( 'heartbeat_received', 'wp_refresh_post_lock', 10, 3 ); add_filter( 'heartbeat_received', 'wp_refresh_post_lock', 10, 3 );
add_filter( 'wp_refresh_nonces', 'wp_refresh_post_nonces', 10, 3 );
add_filter( 'heartbeat_received', 'heartbeat_autosave', 500, 2 ); add_filter( 'heartbeat_received', 'heartbeat_autosave', 500, 2 );
add_filter( 'wp_refresh_nonces', 'wp_refresh_post_nonces', 10, 3 );
add_filter( 'wp_refresh_nonces', 'wp_refresh_heartbeat_nonces' );
add_filter( 'heartbeat_settings', 'wp_heartbeat_set_suspension' ); add_filter( 'heartbeat_settings', 'wp_heartbeat_set_suspension' );
// Nav Menu hooks. // Nav Menu hooks.

View File

@ -1020,13 +1020,31 @@ function wp_refresh_post_nonces( $response, $data, $screen_id ) {
'_ajax_linking_nonce' => wp_create_nonce( 'internal-linking' ), '_ajax_linking_nonce' => wp_create_nonce( 'internal-linking' ),
'_wpnonce' => wp_create_nonce( 'update-post_' . $post_id ), '_wpnonce' => wp_create_nonce( 'update-post_' . $post_id ),
), ),
'heartbeatNonce' => wp_create_nonce( 'heartbeat-nonce' ),
); );
} }
return $response; return $response;
} }
/**
* Add the latest Heartbeat and REST-API nonce to the Heartbeat response.
*
* @since 5.0.0
*
* @param array $response The Heartbeat response.
* @return array The Heartbeat response.
*/
function wp_refresh_heartbeat_nonces( $response ) {
// Refresh the Rest API nonce.
$response['rest_nonce'] = wp_create_nonce( 'wp_rest' );
// TEMPORARY: Compat with api-fetch library
$response['rest-nonce'] = $response['rest_nonce'];
// Refresh the Heartbeat nonce.
$response['heartbeat_nonce'] = wp_create_nonce( 'heartbeat-nonce' );
return $response;
}
/** /**
* Disable suspension of Heartbeat on the Add/Edit Post screens. * Disable suspension of Heartbeat on the Add/Edit Post screens.
* *

View File

@ -312,6 +312,7 @@
if ( trigger && ! hasConnectionError() ) { if ( trigger && ! hasConnectionError() ) {
settings.connectionError = true; settings.connectionError = true;
$document.trigger( 'heartbeat-connection-lost', [error, status] ); $document.trigger( 'heartbeat-connection-lost', [error, status] );
wp.hooks.doAction( 'heartbeat.connection-lost', error, status );
} }
} }
} }
@ -331,6 +332,7 @@
settings.errorcount = 0; settings.errorcount = 0;
settings.connectionError = false; settings.connectionError = false;
$document.trigger( 'heartbeat-connection-restored' ); $document.trigger( 'heartbeat-connection-restored' );
wp.hooks.doAction( 'heartbeat.connection-restored' );
} }
} }
@ -357,6 +359,7 @@
settings.queue = {}; settings.queue = {};
$document.trigger( 'heartbeat-send', [ heartbeatData ] ); $document.trigger( 'heartbeat-send', [ heartbeatData ] );
wp.hooks.doAction( 'heartbeat.send', heartbeatData );
ajaxData = { ajaxData = {
data: heartbeatData, data: heartbeatData,
@ -393,6 +396,7 @@
if ( response.nonces_expired ) { if ( response.nonces_expired ) {
$document.trigger( 'heartbeat-nonces-expired' ); $document.trigger( 'heartbeat-nonces-expired' );
wp.hooks.doAction( 'heartbeat.nonces-expired' );
} }
// Change the interval from PHP // Change the interval from PHP
@ -401,7 +405,21 @@
delete response.heartbeat_interval; delete response.heartbeat_interval;
} }
// Update the heartbeat nonce if set.
if ( response.heartbeat_nonce && typeof window.heartbeatSettings === 'object' ) {
window.heartbeatSettings.nonce = response.heartbeat_nonce;
delete response.heartbeat_nonce;
}
// Update the Rest API nonce if set and wp-api loaded.
if ( response.rest_nonce && typeof window.wpApiSettings === 'object' ) {
window.wpApiSettings.nonce = response.rest_nonce;
// This nonce is required for api-fetch through heartbeat.tick.
// delete response.rest_nonce;
}
$document.trigger( 'heartbeat-tick', [response, textStatus, jqXHR] ); $document.trigger( 'heartbeat-tick', [response, textStatus, jqXHR] );
wp.hooks.doAction( 'heartbeat.tick', response, textStatus, jqXHR );
// Do this last, can trigger the next XHR if connection time > 5 sec. and newInterval == 'fast' // Do this last, can trigger the next XHR if connection time > 5 sec. and newInterval == 'fast'
if ( newInterval ) { if ( newInterval ) {
@ -410,6 +428,7 @@
}).fail( function( jqXHR, textStatus, error ) { }).fail( function( jqXHR, textStatus, error ) {
setErrorState( textStatus || 'unknown', jqXHR.status ); setErrorState( textStatus || 'unknown', jqXHR.status );
$document.trigger( 'heartbeat-error', [jqXHR, textStatus, error] ); $document.trigger( 'heartbeat-error', [jqXHR, textStatus, error] );
wp.hooks.doAction( 'heartbeat.error', jqXHR, textStatus, error );
}); });
} }

File diff suppressed because one or more lines are too long

View File

@ -869,7 +869,7 @@ function wp_default_scripts( &$scripts ) {
$scripts->add( 'autosave', "/wp-includes/js/autosave$suffix.js", array('heartbeat'), false, 1 ); $scripts->add( 'autosave', "/wp-includes/js/autosave$suffix.js", array('heartbeat'), false, 1 );
$scripts->add( 'heartbeat', "/wp-includes/js/heartbeat$suffix.js", array('jquery'), false, 1 ); $scripts->add( 'heartbeat', "/wp-includes/js/heartbeat$suffix.js", array( 'jquery', 'wp-hooks' ), false, 1 );
did_action( 'init' ) && $scripts->localize( 'heartbeat', 'heartbeatSettings', did_action( 'init' ) && $scripts->localize( 'heartbeat', 'heartbeatSettings',
/** /**
* Filters the Heartbeat settings. * Filters the Heartbeat settings.

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.0-beta5-43938'; $wp_version = '5.0-beta5-43939';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.