Edit: maintenance-mode.php
<?php /** * The Maintenance Mode class. * * This class is responsible for handling the maintenance mode functionality. * * @since 4.10.0 * @package Divi_Essential * @subpackage Divi_Essential/Includes */ namespace Divi_Essential\Includes; defined( 'ABSPATH' ) || exit; // Exit if accessed directly. /** * The Maintenance Mode class. * * @since 1.0.0 */ class DnxteMaintenanceMode { const OPTION_KEY = 'dnxte_maintenance_mode_options'; const IMPORTED_FLAG_OPTION = 'dnxte_mm_demo_imported'; /** * Initialize the class and set its hooks. * * @since 1.0.0 */ public function __construct() { /** * disabled this action and ran from admin.php * due to late loading of action hook. */ // add_action( 'dnxte_import_mm_demo', array( $this, 'dnxte_import_mm_demo_from_remote' ) ); add_action( 'template_redirect', array( $this, 'check_for_maintenance_mode' ) ); add_action( 'admin_bar_menu', array( $this, 'add_admin_bar_notice' ), 100 ); add_action( 'admin_head', array( $this, 'output_maintenance_bar_styles' ) ); add_action( 'wp_head', array( $this, 'output_maintenance_bar_styles' ) ); add_action( 'admin_init', array( $this, 'handle_maintenance_toggle' ) ); add_action( 'wp_enqueue_scripts', function () { if ( $this->is_maintenance_mode_active() ) { wp_dequeue_script( 'wp-color-picker-alpha' ); wp_dequeue_script( 'media-audiovideo' ); } }, 100 ); } /** * Import maintenance mode demo in Background from Remote Address. * * @return void */ public function dnxte_import_mm_demo_from_remote() { if ( get_option( self::IMPORTED_FLAG_OPTION ) ) { return; } $files = array( '1', '2', '3', '4', '5', '6' ); foreach ( $files as $file ) { $remote_url = sprintf( 'https://raw.githubusercontent.com/anisurz7/maintenance-mode-demos/main/Demo-%s.json', rawurlencode( $file ) ); $response = wp_remote_get( $remote_url, array( 'timeout' => 15, 'headers' => array( 'Accept' => 'application/json', ), ) ); if ( is_wp_error( $response ) ) { wp_die( esc_html__( 'Failed to fetch remote JSON: ', 'your-text-domain' ) . esc_html( $response->get_error_message() ) ); } $body = wp_remote_retrieve_body( $response ); if ( empty( $body ) ) { wp_die( esc_html__( 'Empty response from remote file: Demo-', 'your-text-domain' ) . esc_html( $file ) . '.json' ); } $json_data = json_decode( $body, true ); if ( json_last_error() !== JSON_ERROR_NONE || ! isset( $json_data['data'] ) || ! is_array( $json_data['data'] ) ) { wp_die( esc_html__( 'Invalid JSON format in Demo-', 'your-text-domain' ) . esc_html( $file ) . '.json' ); } foreach ( $json_data['data'] as $post_data ) { $new_post = array( 'post_title' => sanitize_text_field( $post_data['post_title'] ?? '' ), 'post_content' => wp_kses_post( $post_data['post_content'] ?? '' ), 'post_status' => 'publish', 'post_type' => 'et_pb_layout', 'meta_input' => $this->sanitize_post_meta( $post_data['post_meta'] ?? array() ), ); $post_id = wp_insert_post( wp_slash( $new_post ) ); // Prevent unescaped data // if ( ! is_wp_error( $post_id ) && $post_id ) { // error_log( 'Imported layout: ' . $new_post['post_title'] ); // } } } // Mark as imported update_option( self::IMPORTED_FLAG_OPTION, 1 ); } /** * Sanitize post meta data. * * @param array $meta The post meta data. * @return array The sanitized post meta data. */ private function sanitize_post_meta( array $meta ): array { $clean = array(); foreach ( $meta as $key => $value ) { if ( is_array( $value ) ) { $clean[ $key ] = wp_json_encode( $value ); } else { $clean[ $key ] = $value; } } return $clean; } /** * Determine if maintenance mode extension is active. * * @since 1.0.0 * @return bool Whether maintenance mode is active. */ public function is_maintenance_mode_active() { $inactive_extensions = get_option( 'dnxte_inactive_extensions' ); return is_array( $inactive_extensions ) ? ! in_array( 'dnxte-maintenance-mode', $inactive_extensions, true ) : true; } /** * Show maintenance page to non-admin users if enabled. * * @since 1.0.0 * @return void */ public function check_for_maintenance_mode() { if ( ! $this->is_maintenance_mode_active() ) { return; } if ( current_user_can( 'manage_options' ) ) { return; } $dnext_mm_options = get_option( self::OPTION_KEY, array() ); $dnext_mm_type = isset( $dnext_mm_options['dnext_mm_type'] ) ? trim( sanitize_text_field( $dnext_mm_options['dnext_mm_type'] ) ) : 'page'; $maintenance_page_id = isset( $dnext_mm_options['dnext_mm_page'] ) ? (int) $dnext_mm_options['dnext_mm_page'] : 0; $maintenance_template = isset( $dnext_mm_options['dnext_mm_template'] ) ? trim( sanitize_text_field( $dnext_mm_options['dnext_mm_template'] ) ) : 'demo-1'; $header_hide = isset( $dnext_mm_options['dnext_mm_header'] ) && filter_var( sanitize_key( $dnext_mm_options['dnext_mm_header'] ), FILTER_VALIDATE_BOOLEAN ); $footer_hide = isset( $dnext_mm_options['dnext_mm_footer'] ) && filter_var( sanitize_key( $dnext_mm_options['dnext_mm_footer'] ), FILTER_VALIDATE_BOOLEAN ); $full_height = isset( $dnext_mm_options['dnext_mm_full_height'] ) && filter_var( sanitize_key( $dnext_mm_options['dnext_mm_full_height'] ), FILTER_VALIDATE_BOOLEAN ); $template_fallback = get_template_directory() . '/maintenance-template.php'; // Check if a demo template is selected (starting with 'demo-') $is_demo_template = ( strpos( $maintenance_template, 'demo-' ) === 0 ); // Generate CSS classes for full height if enabled $body_class = 'maintenance-mode'; $content_class = 'maintenance-mode-content'; if ( $full_height ) { $body_class .= ' maintenance-full-height'; $content_class .= ' maintenance-full-height-content'; } if ( $header_hide ) { $body_class .= ' maintenance-hidden-header'; } if ( $footer_hide ) { $body_class .= ' maintenance-hidden-footer'; } header( 'Content-Type: text/html; charset=utf-8' ); if ( 'page' === $dnext_mm_type && $maintenance_page_id && get_post_status( $maintenance_page_id ) === 'publish' ) { $mm_post = get_post( $maintenance_page_id ); // Do not override $post global setup_postdata( $mm_post ); add_filter( 'template_include', function ( $template ) { do_action( 'wp_enqueue_scripts' ); do_action( 'wp_head' ); return $template; } ); $title = get_the_title( $maintenance_page_id ); $content = apply_filters( 'the_content', $mm_post->post_content ); ?> <!DOCTYPE html> <html <?php language_attributes(); ?>> <head> <meta charset="<?php bloginfo( 'charset' ); ?>"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title><?php echo esc_html( $title ); ?></title> <?php wp_head(); ?> <?php if ( $full_height ) : ?> <style> html, body { height: 100%; margin: 0; padding: 0; overflow-x: hidden; } body.maintenance-full-height { display: flex; flex-direction: column; } .maintenance-full-height-content { flex: 1; display: flex; flex-direction: column; justify-content: center; align-items: center; width: 100%; min-height: 100vh; padding: 0px; box-sizing: border-box; } .maintenance-full-height-content .et-l.et-l--post { width: 100%; } .maintenance-full-height-content .et_pb_section { width: 100%; } .maintenance-full-height-content .et_pb_section_0, .maintenance-full-height-content .et_pb_section_1 { height: 100vh; } /* When header and footer are hidden, use the full viewport */ <?php if ( $header_hide && $footer_hide ) : ?> .maintenance-full-height-content { height: 100vh; } <?php endif; ?> </style> <?php endif; ?> </head> <body <?php body_class( $body_class ); ?>> <?php if ( ! $header_hide ) : ?> <?php get_header(); ?> <?php endif; ?> <div class="<?php echo esc_attr( $content_class ); ?>"> <?php echo wp_kses_post( $content ); ?> </div> <?php if ( ! $footer_hide ) : ?> <?php get_footer(); ?> <?php endif; ?> <?php wp_footer(); ?> </body> </html> <?php wp_reset_postdata(); } elseif ( $is_demo_template ) { // Render from Divi Builder Template $layout = $this->get_divi_layout_by_slug( $maintenance_template ); if ( $layout ) { if ( function_exists( 'et_builder_add_main_elements' ) ) { et_builder_add_main_elements(); } // Ensure Divi scripts and styles are loaded if ( function_exists( 'et_divi_load_scripts_styles' ) ) { add_action( 'wp_enqueue_scripts', 'et_divi_load_scripts_styles', 20 ); } // Force load Divi Builder styles if ( function_exists( 'et_builder_enqueue_assets_main' ) ) { add_action( 'wp_enqueue_scripts', 'et_builder_enqueue_assets_main' ); } // Get the layout content $title = $layout->post_title; // Use proper method to render the layout $content = apply_filters( 'the_content', $layout->post_content ); // If content is still empty, try direct post content if ( empty( $content ) ) { $content = $layout->post_content; } ?> <!DOCTYPE html> <html <?php language_attributes(); ?>> <head> <meta charset="<?php bloginfo( 'charset' ); ?>"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title><?php echo esc_html( $title ); ?></title> <?php wp_head(); ?> <style> body #main-header, #main-footer { display: block !important; } .et-l.et-l--post { position: static; } </style> <?php if ( $header_hide ) : ?> <style> body #main-header { display: block !important; } </style> <?php endif; ?> <?php if ( $footer_hide ) : ?> <style> body #main-footer { display: block !important; } </style> <?php endif; ?> <?php if ( $full_height ) : ?> <style> html, body { height: 100%; margin: 0; padding: 0; overflow-x: hidden; } body.maintenance-full-height { display: flex; flex-direction: column; } .maintenance-full-height-content { flex: 1; display: flex; flex-direction: column; justify-content: center; align-items: center; width: 100%; min-height: 100vh; padding: 0px; box-sizing: border-box; } .maintenance-full-height-content .et-l.et-l--post { width: 100%; } .maintenance-full-height-content .et_pb_section { width: 100%; } .maintenance-full-height-content .et_pb_section_0, .maintenance-full-height-content .et_pb_section_1 { height: 100vh; } /* When header and footer are hidden, use the full viewport */ <?php if ( $header_hide && $footer_hide ) : ?> .maintenance-full-height-content { height: 100vh; } <?php endif; ?> </style> <?php endif; ?> </head> <body <?php body_class( $body_class ); ?>> <?php if ( ! $header_hide ) { get_header(); } ?> <div class="<?php echo esc_attr( $content_class ); ?>"> <?php echo wp_kses_post( $content ); ?> </div> <?php if ( ! $footer_hide ) { ?> <?php get_footer(); ?> <?php } ?> <?php wp_footer(); ?> </body> </html> <?php exit; } } exit; } /** * Get a Divi layout by slug * * @since 1.0.0 * @param string $slug The layout slug * @return WP_Post|false The layout post object or false if not found */ private function get_divi_layout_by_slug( $slug ) { $args = array( 'post_type' => 'et_pb_layout', 'posts_per_page' => 1, 'name' => $slug, 'post_status' => 'publish', ); $query = new \WP_Query( $args ); if ( $query->have_posts() ) { return $query->posts[0]; } return false; } /** * Add admin bar notification if maintenance mode is active. * * @since 1.0.0 * @param \WP_Admin_Bar $admin_bar The WP_Admin_Bar instance. * @return void */ public function add_admin_bar_notice( $admin_bar ) { if ( ! $this->is_maintenance_mode_active() || ! current_user_can( 'manage_options' ) ) { return; } $admin_bar->add_node( array( 'id' => 'maintenance-mode-notice', 'title' => '<img src="' . esc_url( DIVI_ESSENTIAL_ASSETS . 'images/admin/divi-essential-white.svg' ) . '" alt="" style="height: 16px; vertical-align: text-bottom; margin-right: 4px;">' . esc_html__( 'Maintenance Mode Active', 'divi-essential' ), 'href' => esc_url( admin_url( 'admin.php?page=dnext-maintenance-mode' ) ), 'meta' => array( 'class' => 'maintenance-mode-active', 'title' => esc_attr__( 'Maintenance Mode is currently active. Click to manage.', 'divi-essential' ), ), ) ); } /** * Output custom styles for admin bar node. * * @since 1.0.0 * @return void */ public function output_maintenance_bar_styles() { ?> <style> #wp-admin-bar-maintenance-mode-notice > a, #wp-admin-bar-maintenance-mode-notice > .ab-item { background: #7A5AF8 !important; color: white !important; font-weight: 500 !important; } </style> <?php } /** * Handle maintenance toggle without automatic redirect. * * @since 1.0.0 * @return void */ public function handle_maintenance_toggle() { // Only process if specifically handling the maintenance toggle action. if ( ! isset( $_GET['action'] ) || 'toggle_maintenance' !== $_GET['action'] ) { return; } // Verify nonce for security. if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'toggle_maintenance_nonce' ) ) { wp_die( esc_html__( 'Security check failed. Please try again.', 'divi-essential' ) ); } // Check user capabilities. if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have permission to perform this action.', 'divi-essential' ) ); } // Process the toggle action here // Example: Toggle the maintenance mode state $inactive_extensions = get_option( 'dnxte_inactive_extensions', array() ); if ( in_array( 'dnxte-maintenance-mode', $inactive_extensions, true ) ) { // Remove from inactive list to activate $inactive_extensions = array_diff( $inactive_extensions, array( 'dnxte-maintenance-mode' ) ); } else { // Add to inactive list to deactivate $inactive_extensions[] = 'dnxte-maintenance-mode'; } update_option( 'dnxte_inactive_extensions', $inactive_extensions ); // Clear cache to ensure changes take effect $this->clear_maintenance_cache(); // Redirect to the maintenance settings page wp_safe_redirect( add_query_arg( 'updated', 'true', admin_url( 'admin.php?page=maintenance-settings' ) ) ); exit; } /** * Clear Divi cache when the maintenance mode option is updated. * * @since 1.0.0 * @return void */ public function clear_cache_after_select_maintenance_mode_page() { if ( isset( $_POST['mm_nonce'] ) && wp_verify_nonce( sanitize_text_field( $_POST['mm_nonce'] ), 'mm_action' ) ) { if ( isset( $_POST['dnext_essential_maintenance_mode_clear_cache'] ) && 1 === $_POST['dnext_essential_maintenance_mode_clear_cache'] ) { // Check user capabilities if ( ! current_user_can( 'manage_options' ) ) { return; } // Clear Divi cache if ( class_exists( 'ET_Core_PageResource' ) && method_exists( 'ET_Core_PageResource', 'remove_static_resources' ) ) { \ET_Core_PageResource::remove_static_resources( 'all', 'all' ); } } } } private function output_full_height_styles() { ?> <style> html, body { height: 100%; margin: 0; padding: 0; overflow-x: hidden; } body.maintenance-full-height { display: flex; flex-direction: column; } .maintenance-full-height-content { flex: 1; display: flex; flex-direction: column; justify-content: center; align-items: center; width: 100%; min-height: 100vh; padding: 0px; box-sizing: border-box; } .maintenance-full-height-content .et-l.et-l--post, .maintenance-full-height-content .et_pb_section { width: 100%; } .maintenance-full-height-content .et_pb_section_0, .maintenance-full-height-content .et_pb_section_1 { height: 100vh; } <?php if ( $this->is_header_footer_hidden() ) : ?> .maintenance-full-height-content { height: 100vh; } <?php endif; ?> </style> <?php } /** * Clear cache when maintenance mode settings are changed. * * This ensures CSS changes take effect immediately. * * @since 1.0.0 * @return void */ public function clear_maintenance_cache() { // Clear Divi cache if available if ( class_exists( 'ET_Core_PageResource' ) && method_exists( 'ET_Core_PageResource', 'remove_static_resources' ) ) { \ET_Core_PageResource::remove_static_resources( 'all', 'all' ); } if ( function_exists( 'et_core_page_resource_clear' ) ) { $_POST['et_owner'] = 'all'; $_POST['et_post_id'] = 'all'; $_POST['clear_page_resources_nonce'] = wp_create_nonce( 'clear_page_resources' ); et_core_page_resource_clear(); } // Set transient for admin notice (for non-AJAX requests) set_transient( 'divi_cache_cleared', true, 60 ); // Clear WordPress object cache wp_cache_flush(); } /** * Check if header and footer are hidden in maintenance mode. * * @since 1.0.0 * @return bool True if header and footer are hidden, false otherwise. */ private function is_header_footer_hidden() { $options = get_option( self::OPTION_KEY, array() ); $header = isset( $options['dnext_mm_header'] ) && filter_var( sanitize_key( $options['dnext_mm_header'] ), FILTER_VALIDATE_BOOLEAN ); $footer = isset( $options['dnext_mm_footer'] ) && filter_var( sanitize_key( $options['dnext_mm_footer'] ), FILTER_VALIDATE_BOOLEAN ); return $header && $footer; } } ( new DnxteMaintenanceMode() );
Save
Back