Make WordPress Core


Ignore:
Timestamp:
07/11/2023 05:01:05 AM (17 months ago)
Author:
isabel_brison
Message:

General: add nonce for block theme preview activation.

Adds a nonce so that activation works for block theme previews. Temporary fix until there is a REST API endpoint for activating themes.

Props scruffian, peterwilsoncc, nithins53, nithi22, jomonthomaslobo1, poena, syamraj24, vivekawsm, mrinal013.
Fixes #58712.

File:
1 edited

Legend:

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

    r56059 r56199  
    5555    );
    5656}
     57
     58/**
     59 * Set a JavaScript constant for theme activation.
     60 *
     61 * Sets the JavaScript global WP_BLOCK_THEME_ACTIVATE_NONCE containing the nonce
     62 * required to activate a theme. For use within the site editor.
     63 *
     64 * @see https://github.com/WordPress/gutenberg/pull/41836.
     65 *
     66 * @since 6.3.0
     67 * @private
     68 */
     69function wp_block_theme_activate_nonce() {
     70    $nonce_handle = 'switch-theme_' . wp_get_theme_preview_path();
     71    ?>
     72    <script type="text/javascript">
     73        window.WP_BLOCK_THEME_ACTIVATE_NONCE = '<?php echo wp_create_nonce( $nonce_handle ); ?>';
     74    </script>
     75    <?php
     76}
     77
     78// Attaches filters to enable theme previews in the Site Editor.
     79if ( ! empty( $_GET['wp_theme_preview'] ) ) {
     80    add_filter( 'stylesheet', 'wp_get_theme_preview_path' );
     81    add_filter( 'template', 'wp_get_theme_preview_path' );
     82    add_action( 'init', 'wp_attach_theme_preview_middleware' );
     83    add_action( 'admin_head', 'wp_block_theme_activate_nonce' );
     84}
Note: See TracChangeset for help on using the changeset viewer.