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
5 changes: 5 additions & 0 deletions includes/classes/wp-maintenance-mode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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'] ) ) {
Expand Down
26 changes: 26 additions & 0 deletions includes/functions/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
32 changes: 31 additions & 1 deletion tests/page-state-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
}
}
1 change: 1 addition & 0 deletions uninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function single_uninstall() {
'wpmm_settings',
'wpmm_notice',
'wpmm_version',
'wpmm_original_show_on_front',
);

foreach ( $options_to_delete as $option ) {
Expand Down
Loading