Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions includes/classes/wp-maintenance-mode-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,14 @@ public function change_template_category() {
* @return void
*/
public function toggle_gutenberg() {
if ( empty( $_POST['source'] ) ) {
// check capabilities
if ( ! current_user_can( wpmm_get_capability( 'settings' ) ) ) {
wp_send_json_error( __( 'You do not have access to this resource.', 'wp-maintenance-mode' ) );
}

// only the migration notices may toggle; other notice nonces reach every role.
$source = isset( $_POST['source'] ) && in_array( $_POST['source'], array( 'migration', 'rollback' ), true ) ? $_POST['source'] : '';
if ( empty( $source ) ) {
die( esc_html__( 'The source filed must not be empty.', 'wp-maintenance-mode' ) );
}

Expand All @@ -938,7 +945,7 @@ public function toggle_gutenberg() {
}

// check nonce validation
if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'notice_nonce_' . $_POST['source'] ) ) {
if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'notice_nonce_' . $source ) ) {
die( esc_html__( 'Security check.', 'wp-maintenance-mode' ) );
}

Expand Down
19 changes: 17 additions & 2 deletions tests/ajax-api-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,8 @@ public function test_toggle_gutenberg_flips_the_new_look_option() {
delete_option( 'wpmm_new_look' );
delete_option( 'wpmm_migration_time' );

$_POST['source'] = 'dashboard';
$_POST['_wpnonce'] = wp_create_nonce( 'notice_nonce_dashboard' );
$_POST['source'] = 'migration';
$_POST['_wpnonce'] = wp_create_nonce( 'notice_nonce_migration' );

$response = $this->handle( 'wpmm_toggle_gutenberg' );

Expand All @@ -368,6 +368,21 @@ public function test_toggle_gutenberg_flips_the_new_look_option() {
$this->assertNotEmpty( get_option( 'wpmm_migration_time' ) );
}

public function test_toggle_gutenberg_requires_capability_even_with_a_notice_nonce() {
$this->_setRole( 'subscriber' );
update_option( 'wpmm_new_look', '1' );

// Every role sees the rebrand notice, so a subscriber holds its nonce.
$_POST['source'] = 'rebrand';
$_POST['_wpnonce'] = wp_create_nonce( 'notice_nonce_rebrand' );

$response = $this->handle( 'wpmm_toggle_gutenberg' );

$this->assertFalse( $response['success'] );
$this->assertSame( 'You do not have access to this resource.', $response['data'] );
$this->assertSame( '1', get_option( 'wpmm_new_look' ) );
}

/*
* wpmm_subscribe (plugin newsletter, external HTTP)
*/
Expand Down
Loading