diff --git a/includes/classes/wp-maintenance-mode.php b/includes/classes/wp-maintenance-mode.php index a37b415..c4af194 100644 --- a/includes/classes/wp-maintenance-mode.php +++ b/includes/classes/wp-maintenance-mode.php @@ -126,6 +126,7 @@ private function __construct() { } } + wpmm_record_show_on_front(); update_option( 'show_on_front', 'page' ); add_filter( 'pre_option_page_on_front', @@ -162,6 +163,8 @@ function ( $value ) { add_action( 'wpmm_before_scripts', array( $this, 'add_bot_extras' ) ); add_action( 'wpmm_footer', array( $this, 'add_js_files' ) ); } else { + wpmm_restore_show_on_front(); + // restore the maintenance page to its original state when maintenance mode is disabled add_action( 'init', @@ -684,6 +687,8 @@ public static function single_activate( $network_wide = false ) { * @since 2.0.0 */ public static function single_deactivate() { + wpmm_restore_show_on_front(); + // give the selected page back to the site in its original state $settings = get_option( 'wpmm_settings' ); if ( ! empty( $settings['design']['page_id'] ) ) { diff --git a/includes/functions/helpers.php b/includes/functions/helpers.php index b1ce842..3db81c1 100644 --- a/includes/functions/helpers.php +++ b/includes/functions/helpers.php @@ -598,6 +598,32 @@ function wpmm_restore_page_state( $page_id ) { delete_post_meta( $page_id, '_wpmm_applied_status' ); } +/** + * Record the site's front-page setting before maintenance mode changes it. + * The first recorded value is kept until maintenance mode is disabled. + * + * @return void + */ +function wpmm_record_show_on_front() { + add_option( 'wpmm_original_show_on_front', get_option( 'show_on_front', 'posts' ) ); +} + +/** + * Restore the front-page setting recorded when maintenance mode was enabled. + * + * @return void + */ +function wpmm_restore_show_on_front() { + $original_value = get_option( 'wpmm_original_show_on_front', false ); + + if ( false === $original_value ) { + return; + } + + update_option( 'show_on_front', $original_value ); + delete_option( 'wpmm_original_show_on_front' ); +} + /** * Switch the selected maintenance page: hand the previous page back and take * the new one over. The caller is responsible for saving the new selection. diff --git a/tests/page-state-test.php b/tests/page-state-test.php index a7adf55..02e0abd 100644 --- a/tests/page-state-test.php +++ b/tests/page-state-test.php @@ -736,13 +736,43 @@ public function test_plugin_deactivation_restores_selected_page() { ) ); + update_option( 'show_on_front', 'posts' ); + // Maintenance mode is active: the page is taken over and published. $this->boot_plugin( $this->make_settings( 1, $page_id ) ); $this->assertSame( 'publish', get_post_status( $page_id ) ); + $this->assertSame( 'page', get_option( 'show_on_front' ) ); + $this->assertSame( 'posts', get_option( 'wpmm_original_show_on_front' ) ); - // Deactivating the plugin hands the page back, private again. + // Deactivating the plugin hands both the page and Reading setting back. WP_Maintenance_Mode::single_deactivate(); $this->assertSame( 'private', get_post_status( $page_id ) ); + $this->assertSame( 'posts', get_option( 'show_on_front' ) ); + $this->assertFalse( get_option( 'wpmm_original_show_on_front', false ) ); + } + + public function test_disabling_maintenance_mode_restores_the_initial_reading_setting() { + $page_id = self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_status' => 'publish', + ) + ); + + update_option( 'show_on_front', 'posts' ); + $this->boot_plugin( $this->make_settings( 1, $page_id ) ); + $this->assertSame( 'page', get_option( 'show_on_front' ) ); + $this->assertSame( 'posts', get_option( 'wpmm_original_show_on_front' ) ); + + // Another active request must not overwrite the recorded initial value. + $this->boot_plugin( $this->make_settings( 1, $page_id ) ); + $this->assertSame( 'posts', get_option( 'wpmm_original_show_on_front' ) ); + + $this->boot_plugin( $this->make_settings( 0, $page_id ) ); + do_action( 'init' ); + + $this->assertSame( 'posts', get_option( 'show_on_front' ) ); + $this->assertFalse( get_option( 'wpmm_original_show_on_front', false ) ); } } diff --git a/uninstall.php b/uninstall.php index 5729061..5223337 100755 --- a/uninstall.php +++ b/uninstall.php @@ -17,6 +17,7 @@ function single_uninstall() { 'wpmm_settings', 'wpmm_notice', 'wpmm_version', + 'wpmm_original_show_on_front', ); foreach ( $options_to_delete as $option ) {