WordPress.org

Make WordPress Core

Changeset 44949


Ignore:
Timestamp:
03/20/2019 08:52:08 PM (9 months ago)
Author:
aduth
Message:

Scripts: Assign api-fetch nonce with corrected rest_nonce.

As of @wordpress/api-fetch@3.0.0 (introduced in 44812), the apiFetch nonce middleware must have its nonce value assigned explicitly, and will no longer listen for heartbeat ticks automatically. This changeset adds an inline script for the default registration of the api-fetch script handle to assign the nonce value in response to the heartbeat action. In doing so, it removes the now-unused, misnamed rest-nonce property from the heartbeat response, whose original introduction served as temporary compatibility with earlier versions of @wordpress/api-fetch.

See https://github.com/WordPress/gutenberg/pull/13451
See #45113

Props adamsilverstein, nerrad .
Fixes #46107 .

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/misc.php

    r44788 r44949  
    10831083    // Refresh the Rest API nonce.
    10841084    $response['rest_nonce'] = wp_create_nonce( 'wp_rest' );
    1085     // TEMPORARY: Compat with api-fetch library
    1086     $response['rest-nonce'] = $response['rest_nonce'];
    10871085
    10881086    // Refresh the Heartbeat nonce.
  • trunk/src/wp-includes/script-loader.php

    r44812 r44949  
    522522    global $wp_locale;
    523523
     524    if ( isset( $scripts->registered['wp-api-fetch'] ) ) {
     525        $scripts->registered['wp-api-fetch']->deps[] = 'wp-hooks';
     526    }
    524527    $scripts->add_inline_script(
    525528        'wp-api-fetch',
    526529        sprintf(
    527             'wp.apiFetch.use( wp.apiFetch.createNonceMiddleware( "%s" ) );',
     530            implode(
     531                "\n",
     532                array(
     533                    '( function() {',
     534                    '   var nonceMiddleware = wp.apiFetch.createNonceMiddleware( "%s" );',
     535                    '   wp.apiFetch.use( nonceMiddleware );',
     536                    '   wp.hooks.addAction(',
     537                    '       "heartbeat.tick",',
     538                    '       "core/api-fetch/create-nonce-middleware",',
     539                    '       function( response ) {',
     540                    '           if ( response[ "rest_nonce" ] ) {',
     541                    '               nonceMiddleware.nonce = response[ "rest_nonce" ];',
     542                    '           }',
     543                    '       }',
     544                    '   );',
     545                    '} )();',
     546                )
     547            ),
    528548            ( wp_installing() && ! is_multisite() ) ? '' : wp_create_nonce( 'wp_rest' )
    529549        ),
Note: See TracChangeset for help on using the changeset viewer.