Skip to content
Open
4 changes: 2 additions & 2 deletions .github/workflows/test-php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ jobs:
- name: Setup PHP version
uses: shivammathur/setup-php@v2
with:
php-version: '7.2'
php-version: '7.4'
extensions: simplexml, mysql
tools: phpunit:7.5.20, phpunit-polyfills
tools: phpunit-polyfills
- name: Checkout source code
uses: actions/checkout@v2
- name: Install Subversion
Expand Down
68 changes: 55 additions & 13 deletions includes/classes/wp-maintenance-mode-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -686,13 +686,29 @@ public function select_page() {
die( esc_html__( 'Security check.', 'wp-maintenance-mode' ) );
}

$this->plugin_settings['design']['page_id'] = $_POST['page_id'];
wp_update_post(
array(
'ID' => $this->plugin_settings['design']['page_id'],
'page_template' => 'templates/wpmm-page-template.php',
)
);
$page_id = isset( $_POST['page_id'] ) ? absint( $_POST['page_id'] ) : 0;

$previous_page_id = isset( $this->plugin_settings['design']['page_id'] ) ? absint( $this->plugin_settings['design']['page_id'] ) : 0;

if ( $previous_page_id && $previous_page_id !== $page_id ) {
// hand the previously selected page back before abandoning it,
// otherwise nothing would ever restore it
wpmm_restore_page_state( $previous_page_id );
}

$this->plugin_settings['design']['page_id'] = $page_id;

if ( $page_id ) {
Comment on lines +689 to +701
// remember the page's original state before taking it over as a maintenance page
wpmm_record_page_state( $page_id );

wp_update_post(
array(
'ID' => $page_id,
'page_template' => 'templates/wpmm-page-template.php',
)
);
}

update_option( 'wpmm_settings', $this->plugin_settings );

Expand Down Expand Up @@ -721,9 +737,19 @@ public function insert_template() {
die( esc_html__( 'Security check.', 'wp-maintenance-mode' ) );
}

$template_slug = $_POST['template_slug'];
$category = $_POST['category'];
$template = json_decode( file_get_contents( WPMM_TEMPLATES_PATH . $category . '/' . $template_slug . '/blocks-export.json' ) );
$template_slug = isset( $_POST['template_slug'] ) ? basename( sanitize_text_field( $_POST['template_slug'] ) ) : '';
$category = isset( $_POST['category'] ) ? basename( sanitize_text_field( $_POST['category'] ) ) : '';
$template_file = WPMM_TEMPLATES_PATH . $category . '/' . $template_slug . '/blocks-export.json';

if ( '' === $template_slug || '' === $category || ! is_readable( $template_file ) ) {
wp_send_json_error( array( 'error' => 'Unknown template' ) );
}

$template = json_decode( file_get_contents( $template_file ) );

if ( empty( $template->content ) ) {
wp_send_json_error( array( 'error' => 'Unknown template' ) );
}

$blocks = str_replace( '\n', '', $template->content );

Expand All @@ -734,20 +760,36 @@ public function insert_template() {
'page_template' => 'templates/wpmm-page-template.php',
);

if ( isset( $this->plugin_settings['design']['page_id'] ) && get_post_status( $this->plugin_settings['design']['page_id'] ) && get_post_status( $this->plugin_settings['design']['page_id'] ) !== 'trash' ) {
$post_arr['ID'] = $this->plugin_settings['design']['page_id'];
$selected_page_id = isset( $this->plugin_settings['design']['page_id'] ) ? absint( $this->plugin_settings['design']['page_id'] ) : 0;
$page_exists = $selected_page_id && get_post_status( $selected_page_id ) && get_post_status( $selected_page_id ) !== 'trash';

if ( $page_exists && get_post_meta( $selected_page_id, '_wpmm_generated', true ) ) {
// only pages created by the plugin may be overwritten
$post_arr['ID'] = $selected_page_id;
$page_id = wp_update_post( $post_arr );
} else {
if ( $page_exists ) {
// hand the user's page back before switching to a generated one
wpmm_restore_page_state( $selected_page_id );
Comment on lines +771 to +773
}
Comment on lines +771 to +774

$post_arr['post_title'] = __( 'Maintenance Page', 'wp-maintenance-mode' );
$page_id = wp_insert_post( $post_arr );

if ( $page_id && ! $page_id instanceof WP_Error ) {
update_post_meta( $page_id, '_wpmm_generated', 1 );
}
}

if ( $page_id === 0 || $page_id instanceof WP_Error ) {
wp_send_json_error( array( 'error' => 'Could not get the page' ) );
}

$this->plugin_settings['design']['page_id'] = $page_id;
CSS_Handler::generate_css_file( $page_id );

if ( class_exists( 'ThemeIsle\GutenbergBlocks\CSS\CSS_Handler' ) ) {
CSS_Handler::generate_css_file( $page_id );
}

if ( 'wizard' === $_POST['source'] ) {
$this->plugin_settings['general']['status'] = 1;
Expand Down
38 changes: 24 additions & 14 deletions includes/classes/wp-maintenance-mode.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,23 @@ private function __construct() {
add_action( 'wp_ajax_wpmm_send_contact', array( $this, 'send_contact' ) );
add_action( 'otter_form_after_submit', array( $this, 'otter_add_subscriber' ) );

if ( isset( $this->plugin_settings['design']['page_id'] ) && get_option( 'wpmm_new_look' ) && get_post_status( $this->plugin_settings['design']['page_id'] ) === 'private' ) {
add_filter( 'wpo_purge_all_cache_on_update', '__return_true' );
if ( function_exists( 'wp_functionality_constants' ) ) {
wp_functionality_constants();
$maintenance_page_status = isset( $this->plugin_settings['design']['page_id'] ) ? get_post_status( $this->plugin_settings['design']['page_id'] ) : false;

if ( $maintenance_page_status && $maintenance_page_status !== 'trash' && get_option( 'wpmm_new_look' ) ) {
// remember the page's original state so it can be restored when maintenance mode is disabled
wpmm_record_page_state( (int) $this->plugin_settings['design']['page_id'] );
Comment on lines +103 to +104

if ( get_post_meta( $this->plugin_settings['design']['page_id'], '_wp_page_template', true ) !== 'templates/wpmm-page-template.php' ) {
update_post_meta( $this->plugin_settings['design']['page_id'], '_wp_page_template', 'templates/wpmm-page-template.php' );
}

if ( get_post_status( $this->plugin_settings['design']['page_id'] ) === 'private' ) {
add_filter( 'wpo_purge_all_cache_on_update', '__return_true' );
if ( function_exists( 'wp_functionality_constants' ) ) {
wp_functionality_constants();
}
wp_publish_post( $this->plugin_settings['design']['page_id'] );
}
wp_publish_post( $this->plugin_settings['design']['page_id'] );
}

update_option( 'show_on_front', 'page' );
Expand Down Expand Up @@ -141,21 +152,14 @@ function ( $value ) {
add_action( 'wpmm_before_scripts', array( $this, 'add_bot_extras' ) );
add_action( 'wpmm_footer', array( $this, 'add_js_files' ) );
} else {
// make maintenance page private when maintenance mode is disabled
// restore the maintenance page to its original state when maintenance mode is disabled
add_action(
'init',
function() {
if ( ! isset( $this->plugin_settings['design']['page_id'] ) ) {
return;
}
if ( get_post_status( $this->plugin_settings['design']['page_id'] ) === 'publish' ) {
wp_update_post(
array(
'ID' => $this->plugin_settings['design']['page_id'],
'post_status' => 'private',
)
);
}
wpmm_restore_page_state( (int) $this->plugin_settings['design']['page_id'] );
}
);
}
Expand Down Expand Up @@ -671,6 +675,12 @@ public static function single_activate( $network_wide = false ) {
* @since 2.0.0
*/
public static function single_deactivate() {
// give the selected page back to the site in its original state
$settings = get_option( 'wpmm_settings' );
if ( ! empty( $settings['design']['page_id'] ) ) {
wpmm_restore_page_state( (int) $settings['design']['page_id'] );
}

wpmm_delete_cache();
}

Expand Down
67 changes: 67 additions & 0 deletions includes/functions/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,73 @@ function sanitize_hex_color( $color ) {
}
}

/**
* Record the current state of the selected maintenance page so it can be
* restored when maintenance mode is disabled or the plugin is deactivated.
*
* A previously recorded state is never overwritten: the page may already be
* carrying plugin-made changes, and recording those would poison the original
* state. The record is cleared when the page is restored.
*
* @since 2.6.23
* @param int $page_id page ID
*/
function wpmm_record_page_state( $page_id ) {
$status = get_post_status( $page_id );

if ( ! $status ) {
return;
}

$template = get_post_meta( $page_id, '_wp_page_template', true );

// the unique flag makes the status key a first-write-wins claim on the
// record, so a racing request cannot save the plugin-modified state
if ( ! add_post_meta( $page_id, '_wpmm_original_status', $status, true ) ) {
return;
}

update_post_meta( $page_id, '_wpmm_original_template', $template );
}

/**
* Restore the selected maintenance page to its recorded original state.
* Pages without a recorded state are left untouched.
*
* @since 2.6.23
* @param int $page_id page ID
*/
function wpmm_restore_page_state( $page_id ) {
$original_status = get_post_meta( $page_id, '_wpmm_original_status', true );

if ( empty( $original_status ) || ! get_post( $page_id ) ) {
return;
}

// a trashed page reflects user intent; the plugin never trashes, so keep the
// status but still hand back the template and clear the record below
if ( get_post_status( $page_id ) !== 'trash' && get_post_status( $page_id ) !== $original_status ) {
wp_update_post(
array(
'ID' => $page_id,
'post_status' => $original_status,
)
);
}

$original_template = get_post_meta( $page_id, '_wpmm_original_template', true );

if ( empty( $original_template ) ) {
delete_post_meta( $page_id, '_wp_page_template' );
} else {
update_post_meta( $page_id, '_wp_page_template', $original_template );
}

// clear the record so the next take-over captures the page's state afresh
delete_post_meta( $page_id, '_wpmm_original_status' );
delete_post_meta( $page_id, '_wpmm_original_template' );
}

/**
* Get option page URL.
*/
Expand Down
Loading
Loading