Skip to content
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/test-php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:
# the latest WordPress test suite requires PHP 7.4+
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 thread
Soare-Robert-Daniel marked this conversation as resolved.
// 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 thread
Soare-Robert-Daniel marked this conversation as resolved.
}
Comment thread
Soare-Robert-Daniel marked this conversation as resolved.

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

if ( $page_id ) {
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
42 changes: 28 additions & 14 deletions includes/classes/wp-maintenance-mode.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,27 @@ 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 thread
Soare-Robert-Daniel marked this conversation as resolved.

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'] );

// mark the publish as plugin-made, so restore can tell it apart
// from a status the user chose deliberately
update_post_meta( $this->plugin_settings['design']['page_id'], '_wpmm_applied_status', 'publish' );
}
wp_publish_post( $this->plugin_settings['design']['page_id'] );
}

update_option( 'show_on_front', 'page' );
Expand Down Expand Up @@ -141,21 +156,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 @@ -670,6 +678,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
85 changes: 85 additions & 0 deletions includes/functions/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,91 @@ 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
* @return void
*/
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 );

// a selection carried over from an old release already wears the plugin's
// template; recording that would make the takeover permanent
if ( $template === 'templates/wpmm-page-template.php' ) {
$template = '';
}

// a single first-write-wins meta keeps the record atomic: a racing request
// can neither save the plugin-modified state nor observe half a record
add_post_meta(
$page_id,
'_wpmm_original_state',
array(
'status' => $status,
'template' => $template,
),
true
);
}

/**
* Restore the selected maintenance page to its recorded original state.
* Pages without a recorded state are left untouched.
*
* The status is only put back while the page still carries the status the
* plugin applied: any other status — trash included — was the user's own
* doing and expresses intent the record predates.
*
* @since 2.6.23
* @param int $page_id page ID
* @return void
*/
function wpmm_restore_page_state( $page_id ) {
$state = get_post_meta( $page_id, '_wpmm_original_state', true );

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

$original_status = isset( $state['status'] ) ? $state['status'] : '';
$applied_status = get_post_meta( $page_id, '_wpmm_applied_status', true );
$current_status = get_post_status( $page_id );

if ( $original_status && $applied_status && $current_status === $applied_status && $current_status !== $original_status ) {
wp_update_post(
array(
'ID' => $page_id,
'post_status' => $original_status,
)
);
}

$original_template = isset( $state['template'] ) ? $state['template'] : '';

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_state' );
delete_post_meta( $page_id, '_wpmm_applied_status' );
}

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