* @copyright Copyright (c) 2023, Phi Phan
*/
namespace BoldBlocks;
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
if ( ! class_exists( Maintenance::class ) ) :
/**
* A simple maitenance mode for deploying or coming soon message
*/
class Maintenance extends CoreComponent {
/**
* Run main hooks
*
* @return void
*/
public function run() {
// Register setting fields.
add_action( 'init', [ $this, 'register_settings' ] );
// Starting point.
add_action( 'init', [ $this, 'run_maintenance_mode' ] );
// Add a notice for maintenance mode warning.
add_action( 'admin_notices', [ $this, 'add_the_admin_notice_for_maintenance_mode' ] );
}
/**
* Register setting fields
*
* @return void
*/
public function register_settings() {
// phpcs:disable PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
register_setting(
'cbb',
'cbb_is_maintenance',
[
'type' => 'boolean',
'sanitize_callback' => 'rest_sanitize_boolean',
'show_in_rest' => [
'name' => 'IsMaintenance',
],
'default' => false,
]
);
register_setting(
'cbb',
'cbb_maintenance_ignore_slug',
[
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
'show_in_rest' => [
'name' => 'MaintenanceSlug',
],
'default' => 'wp-login.php',
]
);
register_setting(
'cbb',
'cbb_maintenance_enable_custom_page',
[
'type' => 'boolean',
'sanitize_callback' => 'rest_sanitize_boolean',
'show_in_rest' => [
'name' => 'MaintananceEnableCustomPage',
],
'default' => false,
]
);
register_setting(
'cbb',
'cbb_maintenance_page_id',
[
'type' => 'integer',
'sanitize_callback' => 'absint',
'show_in_rest' => [
'name' => 'MaintanancePageId',
],
'default' => 0,
]
);
// phpcs:enable PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic
}
/**
* Starting point
*
* @return void
*/
public function run_maintenance_mode() {
// Bail if the setting is disabled.
if ( ! get_option( 'cbb_is_maintenance' ) ) {
return;
}
// Ignore cron job and ajax.
if ( wp_doing_cron() || wp_doing_ajax() ) {
return;
}
$ignore_slug = get_option( 'cbb_maintenance_ignore_slug' );
if ( ! $ignore_slug ) {
$ignore_slug = [ 'wp-login.php' ];
} else {
$ignore_slug = preg_split( "/\r\n|\n|\r/", $ignore_slug );
$ignore_slug = array_filter(
$ignore_slug,
function ( $item ) {
return trim( $item, ' /' );
}
);
}
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$request_uri = isset( $_SERVER['REQUEST_URI'] ) ? trim( wp_unslash( $_SERVER['REQUEST_URI'] ) ?? '', '/' ) : '';
// Ignore administrator and the default login page.
if ( current_user_can( 'manage_options' ) || in_array( $request_uri, $ignore_slug, true ) ) {
return;
}
// Handle custom page.
$enable_custom_page = get_option( 'cbb_maintenance_enable_custom_page' );
$page_id = get_option( 'cbb_maintenance_page_id' );
if ( $enable_custom_page && $page_id ) {
$custom_page_url = get_permalink( $page_id );
if ( $custom_page_url ) {
$parsed_url = wp_parse_url( $custom_page_url );
if ( $parsed_url && in_array( trim( $parsed_url['path'] ?? '', '/' ), $ignore_slug, true ) ) {
return;
}
}
}
$page_title = __( 'Maintenance', 'content-blocks-builder' );
$page_content = __( 'The site is currently under maintenance.
Please check back in a few minutes!', 'content-blocks-builder' );
$site_icon = get_site_icon_url( 64 );
if ( $site_icon ) {
// phpcs:ignore PluginCheck.CodeAnalysis.ImageFunctions.NonEnqueuedImage
$site_icon = sprintf( '', $site_icon, get_bloginfo( 'name' ) );
} else {
$site_icon = sprintf( '
' . __( 'The site is currently under maintenance. Please remember to turn off this mode when the site is ready. You can do this from the settings page.', 'content-blocks-builder' ) . ' ' . $settings_link . '