Make WordPress Core

Ticket #52584: nonces.diff

File nonces.diff, 3.6 KB (added by LinSoftware, 4 years ago)
  • src/wp-admin/includes/admin-filters.php

    diff --git src/wp-admin/includes/admin-filters.php src/wp-admin/includes/admin-filters.php
    index 7b491c36b7..ae70c2c261 100644
    add_filter( 'heartbeat_received', 'wp_refresh_post_lock', 10, 3 ); 
    6868add_filter( 'heartbeat_received', 'heartbeat_autosave', 500, 2 );
    6969
    7070add_filter( 'wp_refresh_nonces', 'wp_refresh_post_nonces', 10, 3 );
     71add_filter( 'wp_refresh_nonces', 'wp_refresh_metabox_loader_nonces', 10, 2 );
    7172add_filter( 'wp_refresh_nonces', 'wp_refresh_heartbeat_nonces' );
    7273
    7374add_filter( 'heartbeat_settings', 'wp_heartbeat_set_suspension' );
  • src/wp-admin/includes/misc.php

    diff --git src/wp-admin/includes/misc.php src/wp-admin/includes/misc.php
    index 61d7698310..85bdf92b30 100644
    function wp_refresh_post_nonces( $response, $data, $screen_id ) { 
    11851185        return $response;
    11861186}
    11871187
     1188/**
     1189 * Refresh nonces used with meta boxes in the block editor.
     1190 *
     1191 * @since 5.x.x
     1192 *
     1193 * @param array  $response  The Heartbeat response.
     1194 * @param array  $data      The $_POST data sent.
     1195 * @return array The Heartbeat response.
     1196 */
     1197function wp_refresh_metabox_loader_nonces( $response, $data ) {
     1198        if ( empty( $data['wp-refresh-metabox-loader-nonces'] ) ) {
     1199                return $response;
     1200        }
     1201
     1202        $received = $data['wp-refresh-metabox-loader-nonces'];
     1203        $post_id  = (int) $received['post_id'];
     1204
     1205        if ( ! $post_id ) {
     1206                return $response;
     1207        }
     1208
     1209        if ( ! current_user_can( 'edit_post', $post_id ) ) {
     1210                return $response;
     1211        }
     1212
     1213        $response['wp-refresh-metabox-loader-nonces'] = array(
     1214                'replace' => array(
     1215                        'metabox_loader_nonce' => wp_create_nonce( 'meta-box-loader' ),
     1216                        '_wpnonce'             => wp_create_nonce( 'update-post_' . $post_id ),
     1217                ),
     1218        );
     1219
     1220        return $response;
     1221}
     1222
    11881223/**
    11891224 * Add the latest Heartbeat and REST-API nonce to the Heartbeat response.
    11901225 *
  • src/wp-admin/includes/post.php

    diff --git src/wp-admin/includes/post.php src/wp-admin/includes/post.php
    index 1a0fef056e..3d5f96411c 100644
    function the_block_editor_meta_boxes() { 
    24002400                wp_add_inline_script( 'wp-lists', $script );
    24012401        }
    24022402
     2403        /*
     2404         * Refresh nonces used by the meta box loader.
     2405         *
     2406         * The logic is very similar to that provided by post.js for the classic editor.
     2407         */
     2408        $script = "( function( $ ) {
     2409                var check, timeout;
     2410
     2411                function schedule() {
     2412                        check = false;
     2413                        window.clearTimeout( timeout );
     2414                        timeout = window.setTimeout( function() { check = true; }, 300000 );
     2415                }
     2416
     2417                $( document ).on( 'heartbeat-send.wp-refresh-nonces', function( e, data ) {
     2418                        var post_id, \$authCheck = $( '#wp-auth-check-wrap' );
     2419
     2420                        if ( check || ( \$authCheck.length && ! \$authCheck.hasClass( 'hidden' ) ) ) {
     2421                                if ( ( post_id = $( '#post_ID' ).val() ) && $( '#_wpnonce' ).val() ) {
     2422                                        data['wp-refresh-metabox-loader-nonces'] = {
     2423                                                post_id: post_id
     2424                                        };
     2425                                }
     2426                        }
     2427                }).on( 'heartbeat-tick.wp-refresh-nonces', function( e, data ) {
     2428                        var nonces = data['wp-refresh-metabox-loader-nonces'];
     2429
     2430                        if ( nonces ) {
     2431                                if ( nonces.replace ) {
     2432                                        if ( nonces.replace.metabox_loader_nonce && window._wpMetaBoxUrl && wp.url ) {
     2433                                                window._wpMetaBoxUrl= wp.url.addQueryArgs( window._wpMetaBoxUrl, { 'meta-box-loader-nonce': nonces.replace.metabox_loader_nonce } );
     2434                                        }
     2435
     2436                                        if ( nonces.replace._wpnonce ) {
     2437                                                $( '#_wpnonce' ).val( nonces.replace._wpnonce );
     2438                                        }
     2439                                }
     2440                        }
     2441                }).ready( function() {
     2442                        schedule();
     2443                });
     2444        } )( jQuery );";
     2445        wp_add_inline_script( 'heartbeat', $script );
     2446
    24032447        // Reset meta box data.
    24042448        $wp_meta_boxes = $_original_meta_boxes;
    24052449}