From 352233f57497007379bc96f54b294fe0455079c2 Mon Sep 17 00:00:00 2001 From: girishpanchal30 Date: Thu, 5 Nov 2026 15:21:57 +0530 Subject: [PATCH 1/5] fix: validate submitted settings --- .../classes/wp-maintenance-mode-admin.php | 4 + tests/settings-save-test.php | 88 +++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 tests/settings-save-test.php diff --git a/includes/classes/wp-maintenance-mode-admin.php b/includes/classes/wp-maintenance-mode-admin.php index aa9ccc91..7e3958a9 100644 --- a/includes/classes/wp-maintenance-mode-admin.php +++ b/includes/classes/wp-maintenance-mode-admin.php @@ -416,6 +416,10 @@ public function save_plugin_settings() { die( esc_html__( 'The tab slug must exist.', 'wp-maintenance-mode' ) ); } + if ( ! isset( $_POST['options'][ $tab ] ) || ! is_array( $_POST['options'][ $tab ] ) ) { + wp_die( esc_html__( 'The submitted settings are invalid.', 'wp-maintenance-mode' ), '', array( 'response' => 400 ) ); + } + // Do some sanitizations switch ( $tab ) { case 'general': diff --git a/tests/settings-save-test.php b/tests/settings-save-test.php new file mode 100644 index 00000000..c9ff5aba --- /dev/null +++ b/tests/settings-save-test.php @@ -0,0 +1,88 @@ +default_settings(); + $settings['general']['status'] = 0; + update_option( 'wpmm_settings', $settings ); + + $admin = wpmm_test_boot_admin(); + + wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) ); + + $_POST = array_merge( + array( + '_wpnonce' => wp_create_nonce( 'tab-general' ), + 'tab' => 'general', + ), + $fields + ); + + $redirect_catcher = function (): void { + // the handler ends with wp_safe_redirect() + exit + throw new WPDieException( 'redirected' ); + }; + add_filter( 'wp_redirect', $redirect_catcher ); + + try { + $admin->save_plugin_settings(); + $message = 'no response'; + } catch ( WPDieException $e ) { + $message = $e->getMessage(); + } finally { + remove_filter( 'wp_redirect', $redirect_catcher ); + } + + return $message; + } + + /** + * @return array + */ + public function malformed_payloads(): array { + return array( + 'options is a string' => array( array( 'options' => 'status' ) ), + 'options is an empty string' => array( array( 'options' => '' ) ), + 'options[general] is a string' => array( array( 'options' => array( 'general' => 'status' ) ) ), + 'options is missing' => array( array() ), + ); + } + + /** + * @dataProvider malformed_payloads + * + * @param array{options?: string|array{general: string}} $fields Submitted fields besides nonce and tab. + */ + public function test_malformed_general_payload_is_rejected_without_saving( array $fields ): void { + $message = $this->save_general_settings( $fields ); + + $this->assertSame( 'The submitted settings are invalid.', $message ); + $this->assertSame( 0, get_option( 'wpmm_settings' )['general']['status'] ); + } + + public function test_general_form_save_stores_status(): void { + $message = $this->save_general_settings( array( 'options' => array( 'general' => array( 'status' => '1' ) ) ) ); + + $this->assertSame( 'redirected', $message ); + $this->assertSame( 1, get_option( 'wpmm_settings' )['general']['status'] ); + } +} From 2f4f860f15a769d8016d97344a13cb8f564a040a Mon Sep 17 00:00:00 2001 From: girishpanchal30 Date: Thu, 5 Nov 2026 15:52:06 +0530 Subject: [PATCH 2/5] fix: validate settings payload structure --- .../classes/wp-maintenance-mode-admin.php | 37 +++++++++++- tests/settings-save-test.php | 60 +++++++++++++++---- 2 files changed, 84 insertions(+), 13 deletions(-) diff --git a/includes/classes/wp-maintenance-mode-admin.php b/includes/classes/wp-maintenance-mode-admin.php index 7e3958a9..87a253cf 100644 --- a/includes/classes/wp-maintenance-mode-admin.php +++ b/includes/classes/wp-maintenance-mode-admin.php @@ -384,6 +384,41 @@ public function display_plugin_settings() { } } + /** + * Whether a submitted settings tab has the shape the save handler expects: + * list fields hold arrays of scalars, every other field is a scalar. + * + * @param string $tab Settings tab slug. + * @param string|array|null $options Submitted options for the tab. + * @return bool + */ + private function is_valid_settings_payload( string $tab, $options ): bool { + if ( ! is_array( $options ) ) { + return false; + } + + $list_fields = array( + 'general' => array( 'backend_role', 'frontend_role' ), + 'modules' => array( 'countdown_details' ), + 'bot' => array( 'messages', 'responses' ), + ); + $tab_lists = isset( $list_fields[ $tab ] ) ? $list_fields[ $tab ] : array(); + + foreach ( $options as $field => $value ) { + $is_list = in_array( $field, $tab_lists, true ); + if ( $is_list !== is_array( $value ) ) { + return false; + } + + $items = $is_list ? $value : array( $value ); + if ( count( array_filter( $items, 'is_scalar' ) ) !== count( $items ) ) { + return false; + } + } + + return true; + } + /** * Save settings * @@ -416,7 +451,7 @@ public function save_plugin_settings() { die( esc_html__( 'The tab slug must exist.', 'wp-maintenance-mode' ) ); } - if ( ! isset( $_POST['options'][ $tab ] ) || ! is_array( $_POST['options'][ $tab ] ) ) { + if ( ! $this->is_valid_settings_payload( $tab, isset( $_POST['options'][ $tab ] ) ? $_POST['options'][ $tab ] : null ) ) { wp_die( esc_html__( 'The submitted settings are invalid.', 'wp-maintenance-mode' ), '', array( 'response' => 400 ) ); } diff --git a/tests/settings-save-test.php b/tests/settings-save-test.php index c9ff5aba..f0ff5c28 100644 --- a/tests/settings-save-test.php +++ b/tests/settings-save-test.php @@ -15,15 +15,15 @@ public function tear_down(): void { } /** - * Submit the General tab form as an administrator. + * Submit a settings tab form as an administrator. * - * @param array{options?: string|array{general: string|array{status: string}}} $fields Submitted fields besides nonce and tab. + * @param 'general'|'modules'|'bot' $tab Settings tab slug. + * @param array{options?: string|array} $fields Submitted fields besides nonce and tab. * @return string The message the request ended with. */ - private function save_general_settings( array $fields ): string { - $settings = WP_Maintenance_Mode::get_instance()->default_settings(); - $settings['general']['status'] = 0; - update_option( 'wpmm_settings', $settings ); + private function save_settings( string $tab, array $fields ): string { + update_option( 'wpmm_settings', WP_Maintenance_Mode::get_instance()->default_settings() ); + update_option( 'wpmm_new_look', '0' ); $admin = wpmm_test_boot_admin(); @@ -31,8 +31,8 @@ private function save_general_settings( array $fields ): string { $_POST = array_merge( array( - '_wpnonce' => wp_create_nonce( 'tab-general' ), - 'tab' => 'general', + '_wpnonce' => wp_create_nonce( 'tab-' . $tab ), + 'tab' => $tab, ), $fields ); @@ -72,15 +72,51 @@ public function malformed_payloads(): array { * * @param array{options?: string|array{general: string}} $fields Submitted fields besides nonce and tab. */ - public function test_malformed_general_payload_is_rejected_without_saving( array $fields ): void { - $message = $this->save_general_settings( $fields ); + public function test_malformed_payload_is_rejected_without_saving( array $fields ): void { + $message = $this->save_settings( 'general', $fields ); $this->assertSame( 'The submitted settings are invalid.', $message ); - $this->assertSame( 0, get_option( 'wpmm_settings' )['general']['status'] ); + $this->assertSame( WP_Maintenance_Mode::get_instance()->default_settings(), get_option( 'wpmm_settings' ) ); + } + + /** + * @return array>}> + */ + public function malformed_fields(): array { + return array( + 'backend_role is a string' => array( 'general', 'backend_role', 'administrator' ), + 'frontend_role is a string' => array( 'general', 'frontend_role', 'editor' ), + 'exclude is an array' => array( 'general', 'exclude', array( 'feed' ) ), + 'redirection is an array' => array( 'general', 'redirection', array( 'https://example.org' ) ), + 'countdown_details is a string' => array( 'modules', 'countdown_details', '1' ), + 'countdown day is an array' => array( 'modules', 'countdown_details', array( 'days' => array( '1' ), 'hours' => '1', 'minutes' => '0' ) ), + 'bot messages is a string' => array( 'bot', 'messages', 'hello' ), + ); + } + + /** + * Submit an otherwise complete form for the tab with one field of the wrong type. + * + * @dataProvider malformed_fields + * + * @param 'general'|'modules'|'bot' $tab Settings tab slug. + * @param string $field Field name inside the tab. + * @param string|array> $value Submitted value. + */ + public function test_malformed_field_is_rejected_without_saving( string $tab, string $field, $value ): void { + $defaults = WP_Maintenance_Mode::get_instance()->default_settings(); + // the stored general defaults keep exclude as a list, while the form posts a string + $form = 'general' === $tab ? array( 'status' => '0' ) : $defaults[ $tab ]; + $form[ $field ] = $value; + + $message = $this->save_settings( $tab, array( 'options' => array( $tab => $form ) ) ); + + $this->assertSame( 'The submitted settings are invalid.', $message ); + $this->assertSame( $defaults, get_option( 'wpmm_settings' ) ); } public function test_general_form_save_stores_status(): void { - $message = $this->save_general_settings( array( 'options' => array( 'general' => array( 'status' => '1' ) ) ) ); + $message = $this->save_settings( 'general', array( 'options' => array( 'general' => array( 'status' => '1' ) ) ) ); $this->assertSame( 'redirected', $message ); $this->assertSame( 1, get_option( 'wpmm_settings' )['general']['status'] ); From cba0f1b63443f73b8f9c3e6cd38174a597b1b0c4 Mon Sep 17 00:00:00 2001 From: girishpanchal30 Date: Thu, 5 Nov 2026 15:58:47 +0530 Subject: [PATCH 3/5] refactor: settings handling --- tests/settings-save-test.php | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/tests/settings-save-test.php b/tests/settings-save-test.php index f0ff5c28..a19dc94c 100644 --- a/tests/settings-save-test.php +++ b/tests/settings-save-test.php @@ -8,6 +8,21 @@ */ class Test_Settings_Save extends WP_UnitTestCase { + /** + * Seeded settings, built once: defaults embed the current time. + * + * @var array> + */ + private $settings; + + public function set_up(): void { + parent::set_up(); + + $this->settings = WP_Maintenance_Mode::get_instance()->default_settings(); + update_option( 'wpmm_settings', $this->settings ); + update_option( 'wpmm_new_look', '0' ); + } + public function tear_down(): void { $_POST = array(); wp_set_current_user( 0 ); @@ -22,9 +37,6 @@ public function tear_down(): void { * @return string The message the request ended with. */ private function save_settings( string $tab, array $fields ): string { - update_option( 'wpmm_settings', WP_Maintenance_Mode::get_instance()->default_settings() ); - update_option( 'wpmm_new_look', '0' ); - $admin = wpmm_test_boot_admin(); wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) ); @@ -76,7 +88,7 @@ public function test_malformed_payload_is_rejected_without_saving( array $fields $message = $this->save_settings( 'general', $fields ); $this->assertSame( 'The submitted settings are invalid.', $message ); - $this->assertSame( WP_Maintenance_Mode::get_instance()->default_settings(), get_option( 'wpmm_settings' ) ); + $this->assertSame( $this->settings, get_option( 'wpmm_settings' ) ); } /** @@ -104,15 +116,14 @@ public function malformed_fields(): array { * @param string|array> $value Submitted value. */ public function test_malformed_field_is_rejected_without_saving( string $tab, string $field, $value ): void { - $defaults = WP_Maintenance_Mode::get_instance()->default_settings(); // the stored general defaults keep exclude as a list, while the form posts a string - $form = 'general' === $tab ? array( 'status' => '0' ) : $defaults[ $tab ]; + $form = 'general' === $tab ? array( 'status' => '0' ) : $this->settings[ $tab ]; $form[ $field ] = $value; $message = $this->save_settings( $tab, array( 'options' => array( $tab => $form ) ) ); $this->assertSame( 'The submitted settings are invalid.', $message ); - $this->assertSame( $defaults, get_option( 'wpmm_settings' ) ); + $this->assertSame( $this->settings, get_option( 'wpmm_settings' ) ); } public function test_general_form_save_stores_status(): void { From 9533a561f88c0f48c86c54394b17217e37d96324 Mon Sep 17 00:00:00 2001 From: girishpanchal30 Date: Thu, 5 Nov 2026 16:13:43 +0530 Subject: [PATCH 4/5] test: cover design settings payloads --- tests/settings-save-test.php | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/tests/settings-save-test.php b/tests/settings-save-test.php index a19dc94c..9660e09f 100644 --- a/tests/settings-save-test.php +++ b/tests/settings-save-test.php @@ -32,7 +32,7 @@ public function tear_down(): void { /** * Submit a settings tab form as an administrator. * - * @param 'general'|'modules'|'bot' $tab Settings tab slug. + * @param 'general'|'design'|'modules'|'bot' $tab Settings tab slug. * @param array{options?: string|array} $fields Submitted fields besides nonce and tab. * @return string The message the request ended with. */ @@ -68,34 +68,39 @@ private function save_settings( string $tab, array $fields ): string { } /** - * @return array + * @return array */ public function malformed_payloads(): array { return array( - 'options is a string' => array( array( 'options' => 'status' ) ), - 'options is an empty string' => array( array( 'options' => '' ) ), - 'options[general] is a string' => array( array( 'options' => array( 'general' => 'status' ) ) ), - 'options is missing' => array( array() ), + 'options is a string' => array( 'general', array( 'options' => 'status' ) ), + 'options is an empty string' => array( 'general', array( 'options' => '' ) ), + 'options[general] is a string' => array( 'general', array( 'options' => array( 'general' => 'status' ) ) ), + 'options is missing' => array( 'general', array() ), + 'design options is a string' => array( 'design', array( 'options' => 'title' ) ), + 'options[design] is a string' => array( 'design', array( 'options' => array( 'design' => 'title' ) ) ), ); } /** * @dataProvider malformed_payloads * - * @param array{options?: string|array{general: string}} $fields Submitted fields besides nonce and tab. + * @param 'general'|'design' $tab Settings tab slug. + * @param array{options?: string|array{general?: string, design?: string}} $fields Submitted fields besides nonce and tab. */ - public function test_malformed_payload_is_rejected_without_saving( array $fields ): void { - $message = $this->save_settings( 'general', $fields ); + public function test_malformed_payload_is_rejected_without_saving( string $tab, array $fields ): void { + $message = $this->save_settings( $tab, $fields ); $this->assertSame( 'The submitted settings are invalid.', $message ); $this->assertSame( $this->settings, get_option( 'wpmm_settings' ) ); } /** - * @return array>}> + * @return array>}> */ public function malformed_fields(): array { return array( + 'heading_color is an array' => array( 'design', 'heading_color', array( '#000000' ) ), + 'bg_custom is an array' => array( 'design', 'bg_custom', array( 'https://example.org/bg.jpg' ) ), 'backend_role is a string' => array( 'general', 'backend_role', 'administrator' ), 'frontend_role is a string' => array( 'general', 'frontend_role', 'editor' ), 'exclude is an array' => array( 'general', 'exclude', array( 'feed' ) ), @@ -111,7 +116,7 @@ public function malformed_fields(): array { * * @dataProvider malformed_fields * - * @param 'general'|'modules'|'bot' $tab Settings tab slug. + * @param 'general'|'design'|'modules'|'bot' $tab Settings tab slug. * @param string $field Field name inside the tab. * @param string|array> $value Submitted value. */ From 0b5ac3ddbcd93cfd9a94e33a045b0fa715cb047b Mon Sep 17 00:00:00 2001 From: girishpanchal30 Date: Thu, 5 Nov 2026 16:57:58 +0530 Subject: [PATCH 5/5] fix: require countdown details when saving classic modules --- .../classes/wp-maintenance-mode-admin.php | 5 +++++ tests/settings-save-test.php | 22 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/includes/classes/wp-maintenance-mode-admin.php b/includes/classes/wp-maintenance-mode-admin.php index 87a253cf..341b8f0d 100644 --- a/includes/classes/wp-maintenance-mode-admin.php +++ b/includes/classes/wp-maintenance-mode-admin.php @@ -397,6 +397,11 @@ private function is_valid_settings_payload( string $tab, $options ): bool { return false; } + // the classic Modules save maps over countdown_details unconditionally + if ( 'modules' === $tab && ! get_option( 'wpmm_new_look' ) && ! isset( $options['countdown_details'] ) ) { + return false; + } + $list_fields = array( 'general' => array( 'backend_role', 'frontend_role' ), 'modules' => array( 'countdown_details' ), diff --git a/tests/settings-save-test.php b/tests/settings-save-test.php index 9660e09f..5d8257df 100644 --- a/tests/settings-save-test.php +++ b/tests/settings-save-test.php @@ -131,6 +131,28 @@ public function test_malformed_field_is_rejected_without_saving( string $tab, st $this->assertSame( $this->settings, get_option( 'wpmm_settings' ) ); } + public function test_classic_modules_save_without_countdown_details_is_rejected(): void { + $form = $this->settings['modules']; + unset( $form['countdown_details'] ); + + $message = $this->save_settings( 'modules', array( 'options' => array( 'modules' => $form ) ) ); + + $this->assertSame( 'The submitted settings are invalid.', $message ); + $this->assertSame( $this->settings, get_option( 'wpmm_settings' ) ); + } + + public function test_new_look_modules_save_without_countdown_details_is_saved(): void { + update_option( 'wpmm_new_look', '1' ); + $form = $this->settings['modules']; + unset( $form['countdown_details'] ); + $form['ga_status'] = '1'; + + $message = $this->save_settings( 'modules', array( 'options' => array( 'modules' => $form ) ) ); + + $this->assertSame( 'redirected', $message ); + $this->assertSame( 1, get_option( 'wpmm_settings' )['modules']['ga_status'] ); + } + public function test_general_form_save_stores_status(): void { $message = $this->save_settings( 'general', array( 'options' => array( 'general' => array( 'status' => '1' ) ) ) );