settings = Plugin::get_settings( 'debug' ); add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ] ); if( empty( $this->settings['maintenance_mode_enabled'] ) ) { return; } add_action( 'template_redirect', [ $this, 'template_redirect' ], 0 ); } /** * Configures and adds settings fields related to maintenance mode in the AdminEase plugin. * * @param array $fields The existing settings fields to which new maintenance mode fields will be appended. * * @return array The modified settings fields with maintenance mode options included. */ public function adminease_settings_fields( array $fields ): array { $blog_name = get_bloginfo( 'name' ); $fields['debug']['fields'][] = [ 'type' => 'switch', 'id' => 'maintenance-mode-enabled', 'name' => 'adminease[debug][maintenance_mode_enabled]', 'value' => $this->settings['maintenance_mode_enabled'] ?? false, 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input toggle-field', 'label' => __( 'Enable Maintenance Mode', 'adminease' ), 'risk' => 'careful', 'breaks' => __( 'Visitors see the maintenance page instead of your site.', 'adminease' ), 'description' => __( 'Display a professional maintenance page to visitors during site updates or scheduled maintenance while allowing administrators to access the site normally. Customize the page with your logo, headlines, and HTML-formatted messages. Includes SEO-friendly 503 headers and Retry-After instructions for search engines.', 'adminease' ), 'child_fields' => [ [ 'type' => 'switch', 'id' => 'maintenance_mode_show_logo', 'name' => 'adminease[debug][maintenance_mode_show_logo]', 'value' => $this->settings['maintenance_mode_show_logo'] ?? true, 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input', 'wrapper_class' => 'form-group-child', 'label' => __( 'Show Site Logo', 'adminease' ), 'description' => __( 'Display the site logo from Customizer settings.', 'adminease' ), 'attributes' => [ 'data-parent' => 'maintenance-mode-enabled', ], ], [ 'type' => 'text', 'id' => 'maintenance_mode_page_title', 'name' => 'adminease[debug][maintenance_mode_page_title]', 'value' => $this->settings['maintenance_mode_page_title'] ?? $blog_name . ' - ' . __( 'Maintenance Mode', 'adminease' ), 'label_class' => 'pwp-field-label', 'input_class' => 'pwp-field-input', 'wrapper_class' => 'form-group-child', 'label' => __( 'Page Title', 'adminease' ), 'description' => __( 'The title that appears in the browser tab.', 'adminease' ), 'attributes' => [ 'data-parent' => 'maintenance-mode-enabled', ], ], [ 'type' => 'text', 'id' => 'maintenance_mode_headline', 'name' => 'adminease[debug][maintenance_mode_headline]', 'value' => $this->settings['maintenance_mode_headline'] ?? $blog_name, 'label_class' => 'pwp-field-label', 'input_class' => 'pwp-field-input', 'wrapper_class' => 'form-group-child', 'label' => __( 'Headline', 'adminease' ), 'description' => __( 'The main heading displayed on the maintenance page.', 'adminease' ), 'attributes' => [ 'data-parent' => 'maintenance-mode-enabled', ], ], [ 'type' => 'textarea', 'id' => 'maintenance_mode_message', 'name' => 'adminease[debug][maintenance_mode_message]', 'value' => $this->settings['maintenance_mode_message'] ?? __( 'We are currently performing scheduled maintenance. Please check back soon!', 'adminease' ), 'label_class' => 'pwp-field-label', 'input_class' => 'pwp-field-input', 'wrapper_class' => 'form-group-child', 'label' => __( 'Maintenance Message', 'adminease' ), 'description' => __( 'Message to display to visitors during maintenance.', 'adminease' ), 'field_description' => __( 'You can use basic HTML tags.', 'adminease' ), 'attributes' => [ 'rows' => 4, 'data-parent' => 'maintenance-mode-enabled', ], ], [ 'type' => 'colorpicker', 'id' => 'maintenance_mode_primary_color', 'name' => 'adminease[debug][maintenance_mode_primary_color]', 'value' => $this->settings['maintenance_mode_primary_color'] ?? '#0073aa', 'label_class' => 'pwp-field-label', 'input_class' => 'pwp-field-input', 'wrapper_class' => 'form-group-child ae-color-field ae-color-field-primary', 'label' => __( 'Primary Color', 'adminease' ), 'description' => __( 'Select the primary color for the maintenance mode page.', 'adminease' ), 'attributes' => [ 'data-parent' => 'maintenance-mode-enabled', ], ], [ 'type' => 'colorpicker', 'id' => 'maintenance_mode_secondary_color', 'name' => 'adminease[debug][maintenance_mode_secondary_color]', 'value' => $this->settings['maintenance_mode_secondary_color'] ?? '#23282d', 'label_class' => 'pwp-field-label', 'input_class' => 'pwp-field-input', 'wrapper_class' => 'form-group-child ae-color-field ae-color-field-secondary', 'label' => __( 'Secondary Color', 'adminease' ), 'description' => __( 'Select the secondary color for the maintenance mode page.', 'adminease' ), 'attributes' => [ 'data-parent' => 'maintenance-mode-enabled', ], ], [ 'type' => 'colorpicker', 'id' => 'maintenance_mode_text_color', 'name' => 'adminease[debug][maintenance_mode_text_color]', 'value' => $this->settings['maintenance_mode_text_color'] ?? '#333333', 'label_class' => 'pwp-field-label', 'input_class' => 'pwp-field-input', 'wrapper_class' => 'form-group-child ae-color-field ae-color-field-text', 'label' => __( 'Text Color', 'adminease' ), 'description' => __( 'Select the text color for the maintenance mode page.', 'adminease' ), 'attributes' => [ 'data-parent' => 'maintenance-mode-enabled', ], ], [ 'type' => 'number', 'id' => 'maintenance_mode_retry_after', 'name' => 'adminease[debug][maintenance_mode_retry_after]', 'value' => $this->settings['maintenance_mode_retry_after'] ?? 3600, 'label_class' => 'pwp-field-label', 'input_class' => 'pwp-field-input', 'wrapper_class' => 'form-group-child', 'label' => __( 'SEO Retry-After (seconds)', 'adminease' ), 'field_description' => __( "Tells search engines when to check back (SEO).
Default: 3600 (1 hour). Use 0 to disable.", 'adminease' ), 'attributes' => [ 'min' => 0, 'step' => 60, 'data-parent' => 'maintenance-mode-enabled', ], ], [ 'type' => 'select', 'id' => 'maintenance_mode_theme', 'name' => 'adminease[debug][maintenance_mode_theme]', 'value' => $this->settings['maintenance_mode_theme'] ?? 'classic', 'label_class' => 'pwp-field-label', 'input_class' => 'pwp-field-input', 'wrapper_class' => 'form-group-child', 'label' => __( 'Page Theme', 'adminease' ), 'description' => __( 'Choose the visual style of the maintenance page.', 'adminease' ), 'options' => $this->get_theme_options(), 'attributes' => [ 'data-parent' => 'maintenance-mode-enabled', ], ], [ 'type' => 'textarea', 'id' => 'maintenance_mode_ip_allowlist', 'name' => 'adminease[debug][maintenance_mode_ip_allowlist]', 'value' => $this->settings['maintenance_mode_ip_allowlist'] ?? '', 'label_class' => 'pwp-field-label', 'input_class' => 'pwp-field-input', 'wrapper_class' => 'form-group-child', 'label' => __( 'IP Allowlist', 'adminease' ), 'description' => __( 'One entry per line. Visitors matching these will bypass maintenance mode. Accepts exact IPs, CIDR ranges (192.168.1.0/24), or wildcards (192.168.1.*).', 'adminease' ), 'attributes' => [ 'rows' => 4, 'data-parent' => 'maintenance-mode-enabled', ], ], [ 'type' => 'switch', 'id' => 'maintenance_mode_schedule_enabled', 'name' => 'adminease[debug][maintenance_mode_schedule_enabled]', 'value' => $this->settings['maintenance_mode_schedule_enabled'] ?? false, 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input toggle-field', 'wrapper_class' => 'form-group-child', 'label' => __( 'Enable Scheduled Window', 'adminease' ), 'description' => __( 'Only show the maintenance page between a start and end date/time. Outside this window the site behaves normally, even while Maintenance Mode is enabled above.', 'adminease' ), 'attributes' => [ 'data-parent' => 'maintenance-mode-enabled', ], ], [ 'type' => 'datetime-local', 'id' => 'maintenance_mode_schedule_start', 'name' => 'adminease[debug][maintenance_mode_schedule_start]', 'value' => $this->settings['maintenance_mode_schedule_start'] ?? '', 'label_class' => 'pwp-field-label', 'input_class' => 'pwp-field-input', 'wrapper_class' => 'form-group-child', 'label' => __( 'Schedule Start', 'adminease' ), 'description' => __( 'Date and time the maintenance window begins (site time).', 'adminease' ), 'attributes' => [ 'data-parent' => 'maintenance_mode_schedule_enabled', ], ], [ 'type' => 'datetime-local', 'id' => 'maintenance_mode_schedule_end', 'name' => 'adminease[debug][maintenance_mode_schedule_end]', 'value' => $this->settings['maintenance_mode_schedule_end'] ?? '', 'label_class' => 'pwp-field-label', 'input_class' => 'pwp-field-input', 'wrapper_class' => 'form-group-child', 'label' => __( 'Schedule End', 'adminease' ), 'description' => $this->schedule_has_ended() ? __( 'Date and time the maintenance window ends (site time). This window has already ended — the maintenance page is no longer shown, but "Enable Maintenance Mode" above is still on. Turn it off or update the schedule.', 'adminease' ) : __( 'Date and time the maintenance window ends (site time). A live countdown to this time is shown on the maintenance page.', 'adminease' ), 'attributes' => [ 'data-parent' => 'maintenance_mode_schedule_enabled', ], ], ], ]; return $fields; } /** * Retrieves the maintenance mode themes registered via the adminease_maintenance_mode_themes filter. * * @return array Associative array of theme key => fully-qualified theme class name. */ private function get_registered_themes(): array { /** * Filter the available maintenance mode themes. * * @param array $themes Associative array of theme key => fully-qualified theme class name. */ return apply_filters( 'adminease_maintenance_mode_themes', [ 'classic' => Classic::class, 'minimal' => Minimal::class, 'split-screen' => SplitScreen::class, ] ); } /** * Builds the theme select field options from the registered themes. When Pro is not active, * a disabled "Terminal (Available in Pro)" placeholder is appended so the option is always * visible in the list. Once Pro is active, Pro registers the real Terminal theme via the * adminease_maintenance_mode_themes filter, which supersedes this placeholder. * * @return array Associative array of theme key => human-readable label, or key => ['label' => string, 'disabled' => bool]. */ private function get_theme_options(): array { $options = []; foreach( $this->get_registered_themes() as $key => $class ) { $options[ $key ] = is_callable( [ $class, 'get_label' ] ) ? $class::get_label() : $key; } if( !isset( $options['terminal'] ) && !Utils::is_pro_plugin_active() ) { $options['terminal'] = [ /* translators: %s: theme name */ 'label' => sprintf( __( '%s (Available in Pro)', 'adminease' ), __( 'Terminal', 'adminease' ) ), 'disabled' => true, ]; } return $options; } /** * Handles the template redirect during maintenance mode. * Ensures proper output headers, disables caching, and displays the maintenance mode page. * Prevents execution in the admin area or during AJAX requests and respects user access filters. * Allows bypassing maintenance mode for specific roles or via custom filters. * Defines constants to signal caching and minification plugins to bypass processing. * @return void */ public function template_redirect() { if( !$this->is_effectively_active() ) { return; } if( is_admin() || wp_doing_ajax() || is_user_logged_in() ) { return; } if( apply_filters( 'adminease_maintenance_mode_check_access', false ) ) { return; } if( current_user_can( 'edit_posts' ) ) { return; } $allowlist = $this->settings['maintenance_mode_ip_allowlist'] ?? ''; if( !empty( $allowlist ) && Utils::ip_in_allowlist( Utils::get_client_ip(), $allowlist ) ) { return; } if( !defined( 'DONOTCACHEPAGE' ) ) { define( 'DONOTCACHEPAGE', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound } // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- Standard caching constant used by W3 Total Cache and other caching plugins if( !defined( 'DONOTCACHEOBJECT' ) ) { define( 'DONOTCACHEOBJECT', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound } // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- Standard caching constant used by Autoptimize and other minification plugins if( !defined( 'DONOTMINIFY' ) ) { define( 'DONOTMINIFY', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound } header( 'HTTP/1.1 503 Service Temporarily Unavailable' ); header( 'Status: 503 Service Temporarily Unavailable' ); $retry_after = isset( $this->settings['maintenance_mode_retry_after'] ) ? absint( $this->settings['maintenance_mode_retry_after'] ) : 3600; if( $retry_after > 0 ) { header( 'Retry-After: ' . $retry_after ); } nocache_headers(); header( 'Cache-Control: no-store, no-cache, must-revalidate, max-age=0' ); header( 'Cache-Control: post-check=0, pre-check=0', false ); header( 'Pragma: no-cache' ); $this->show_maintenance_page(); exit; } /** * Display the maintenance mode page * @return void */ private function show_maintenance_page(): void { $theme = $this->settings['maintenance_mode_theme'] ?? 'classic'; $show_logo = $this->settings['maintenance_mode_show_logo'] ?? true; $custom_logo_id = get_theme_mod( 'custom_logo' ); $schedule_end_ms = null; if( !empty( $this->settings['maintenance_mode_schedule_enabled'] ) ) { $end = $this->get_schedule_timestamp( 'maintenance_mode_schedule_end' ); if( null !== $end ) { $schedule_end_ms = $end * 1000; } } $theme_args = [ 'page_title' => $this->settings['maintenance_mode_page_title'] ?? get_bloginfo( 'name' ) . ' - ' . __( 'Maintenance Mode', 'adminease' ), 'headline' => $this->settings['maintenance_mode_headline'] ?? get_bloginfo( 'name' ), 'logo_url' => $show_logo ? wp_get_attachment_image_url( $custom_logo_id, 'full' ) : '', 'message' => $this->settings['maintenance_mode_message'] ?? '', 'primary_color' => $this->settings['maintenance_mode_primary_color'] ?? '#7d50f9', 'secondary_color' => $this->settings['maintenance_mode_secondary_color'] ?? '#845af9', 'text_color' => $this->settings['maintenance_mode_text_color'] ?? '#333333', 'schedule_end_ms' => $schedule_end_ms, ]; $themes = $this->get_registered_themes(); $theme_class = $themes[ $theme ] ?? Classic::class; $html = $theme_class::render( $theme_args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped /** * Filter the maintenance mode HTML * * @param string $html The complete HTML output * @param array $settings Current settings */ $html = apply_filters( 'adminease_maintenance_mode_html', $html, $this->settings ); echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } /** * Determines whether the maintenance page should currently be shown, taking the optional * scheduled window into account. The manual "Enable Maintenance Mode" switch remains the * master control; when a schedule is also configured, it narrows when the page is shown * within that already-enabled state, rather than controlling activation on its own. * @return bool True if the maintenance page should be shown right now. */ private function is_effectively_active(): bool { if( empty( $this->settings['maintenance_mode_schedule_enabled'] ) ) { return true; } $now = time(); $start = $this->get_schedule_timestamp( 'maintenance_mode_schedule_start' ); $end = $this->get_schedule_timestamp( 'maintenance_mode_schedule_end' ); if( null !== $start && $now < $start ) { return false; } if( null !== $end && $now > $end ) { return false; } return true; } /** * Parses a stored datetime-local setting value into a Unix timestamp using the site's timezone. * * @param string $field_id The settings key holding a datetime-local value (YYYY-MM-DDTHH:MM). * * @return int|null The parsed timestamp, or null if the value is empty or invalid. */ private function get_schedule_timestamp( string $field_id ): ?int { $raw = $this->settings[ $field_id ] ?? ''; if( empty( $raw ) ) { return null; } try { $date = new \DateTime( $raw, wp_timezone() ); } catch( \Exception $e ) { return null; } return $date->getTimestamp(); } /** * Determines whether a configured scheduled window has already ended. Used only to surface * an admin-facing notice; it never mutates stored settings. * @return bool True if scheduling is enabled and the end time is in the past. */ private function schedule_has_ended(): bool { if( empty( $this->settings['maintenance_mode_schedule_enabled'] ) ) { return false; } $end = $this->get_schedule_timestamp( 'maintenance_mode_schedule_end' ); if( null === $end ) { return false; } return time() > $end; } }