Make WordPress Core

Ticket #37569: 37569.diff

File 37569.diff, 3.2 KB (added by rmccue, 8 years ago)

Refresh nonce via Heartbeat

  • src/wp-admin/includes/admin-filters.php

     
    5555add_action( 'update_option_siteurl',       'update_home_siteurl', 10, 2 );
    5656add_action( 'update_option_page_on_front', 'update_home_siteurl', 10, 2 );
    5757
    58 add_filter( 'heartbeat_received', 'wp_check_locked_posts',  10,  3 );
    59 add_filter( 'heartbeat_received', 'wp_refresh_post_lock',   10,  3 );
    60 add_filter( 'wp_refresh_nonces', 'wp_refresh_post_nonces', 10,  3 );
    61 add_filter( 'heartbeat_received', 'heartbeat_autosave',     500, 2 );
     58add_filter( 'heartbeat_received', 'wp_check_locked_posts',            10,  3 );
     59add_filter( 'heartbeat_received', 'wp_refresh_post_lock',             10,  3 );
     60add_filter( 'wp_refresh_nonces',  'wp_refresh_post_nonces',           10,  3 );
     61add_filter( 'heartbeat_received', 'rest_refresh_nonce_on_heartbeat',  10,  2 );
     62add_filter( 'heartbeat_received', 'heartbeat_autosave',               500, 2 );
    6263
    6364add_filter( 'heartbeat_settings', 'wp_heartbeat_set_suspension' );
    6465
  • src/wp-includes/js/wp-api-nonce.js

     
     1jQuery( function ($) {
     2        $( document ).on( 'heartbeat-send', function ( e, data ) {
     3                data['wp-refresh-rest-nonce'] = wpApiSettings.nonce;
     4        });
     5        $( document ).on( 'heartbeat-tick', function ( e, data ) {
     6                if ( 'wp-refresh-rest-nonce' in data ) {
     7                        wpApiSettings.nonce = data['wp-refresh-rest-nonce'];
     8                }
     9        });
     10});
  • src/wp-includes/rest-api.php

     
    721721}
    722722
    723723/**
     724 * Refresh the REST API nonce on heartbeat requests.
     725 *
     726 * @since 4.8
     727 *
     728 * @param array  $response  The Heartbeat response.
     729 * @param array  $data      The $_POST data sent.
     730 * @return array The Heartbeat response.
     731 */
     732function rest_refresh_nonce_on_heartbeat( $response, $data ) {
     733        if ( array_key_exists( 'wp-refresh-rest-nonce', $data ) ) {
     734                // Are we in the second tick?
     735                if ( wp_verify_nonce( $data['wp-refresh-rest-nonce'] ) === 2 ) {
     736                        // Update nonce.
     737                        $response['wp-refresh-rest-nonce'] = wp_create_nonce( 'wp_rest' );
     738                }
     739        }
     740
     741        return $response;
     742}
     743
     744/**
    724745 * Collects cookie authentication status.
    725746 *
    726747 * Collects errors from wp_validate_auth_cookie for use by rest_cookie_check_errors.
  • src/wp-includes/script-loader.php

     
    510510                'nonce'         => ( wp_installing() && ! is_multisite() ) ? '' : wp_create_nonce( 'wp_rest' ),
    511511                'versionString' => 'wp/v2/',
    512512        ) );
     513        $scripts->add( 'wp-api-nonce', "/wp-includes/js/wp-api-nonce$suffix.js", array( 'jquery', 'wp-api', 'heartbeat' ), false, 1 );
    513514
    514515        if ( is_admin() ) {
    515516                $scripts->add( 'admin-tags', "/wp-admin/js/tags$suffix.js", array( 'jquery', 'wp-ajax-response' ), false, 1 );