Skip to content

Commit 7813524

Browse files
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. git-svn-id: https://develop.svn.wordpress.org/trunk@56199 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 0c5e0df commit 7813524

4 files changed

Lines changed: 29 additions & 8 deletions

File tree

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,3 @@
168168

169169
// Append '(Draft)' to draft page titles in the privacy page dropdown.
170170
add_filter( 'list_pages', '_wp_privacy_settings_filter_draft_page_titles', 10, 2 );
171-
172-
// Attaches filters to enable theme previews in the Site Editor.
173-
if ( ! empty( $_GET['wp_theme_preview'] ) ) {
174-
add_filter( 'stylesheet', 'wp_get_theme_preview_path' );
175-
add_filter( 'template', 'wp_get_theme_preview_path' );
176-
add_action( 'init', 'wp_attach_theme_preview_middleware' );
177-
}

src/wp-admin/includes/admin.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171

7272
/** WordPress Theme Administration API */
7373
require_once ABSPATH . 'wp-admin/includes/theme.php';
74-
require_once ABSPATH . 'wp-admin/includes/theme-previews.php';
7574

7675
/** WordPress Privacy Functions */
7776
require_once ABSPATH . 'wp-admin/includes/privacy-tools.php';

src/wp-admin/includes/theme-previews.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,31 @@ function wp_attach_theme_preview_middleware() {
5454
'after'
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+
*/
69+
function 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.
79+
if ( ! 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+
}

src/wp-settings.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@
181181
require ABSPATH . WPINC . '/block-template-utils.php';
182182
require ABSPATH . WPINC . '/block-template.php';
183183
require ABSPATH . WPINC . '/theme-templates.php';
184+
require ABSPATH . WPINC . '/theme-previews.php';
184185
require ABSPATH . WPINC . '/template.php';
185186
require ABSPATH . WPINC . '/https-detection.php';
186187
require ABSPATH . WPINC . '/https-migration.php';

0 commit comments

Comments
 (0)