settings = Plugin::get_settings( 'debug' ); add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ] ); if( empty( $this->settings['maintenance_mode_enabled'] ) ) { return; } if( !empty( $this->settings['maintenance_mode_block_rest'] ) ) { add_filter( 'rest_pre_dispatch', [ $this, 'check_rest_access' ], -1000 ); } add_action( 'template_redirect', [ $this, 'template_redirect' ], 0 ); add_filter( 'robots_txt', [ $this, 'robots_txt' ], PHP_INT_MAX ); } /** * 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 anyone who can write posts (contributors, authors, editors and administrators) to access the site normally. Other logged-in users, such as subscribers and customers, see the maintenance page too. 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' => 'switch', 'id' => 'maintenance_mode_block_rest', 'name' => 'adminease[debug][maintenance_mode_block_rest]', 'value' => $this->settings['maintenance_mode_block_rest'] ?? false, 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input', 'wrapper_class' => 'form-group-child', 'label' => __( 'Protect the REST API', 'adminease' ), 'description' => __( 'While the maintenance page is shown, visitors who would see it cannot read your posts, pages or products through /wp-json/ either: they get the same 503 answer and Retry-After. Payment and form services, Jetpack and other tools that call your site through the REST API without logging in (webhooks) are blocked too until maintenance ends.', 'adminease' ), 'attributes' => [ '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_block_robots', 'name' => 'adminease[debug][maintenance_mode_block_robots]', 'value' => $this->settings['maintenance_mode_block_robots'] ?? false, 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input', 'wrapper_class' => 'form-group-child', 'label' => __( 'Ask Search Engines Not To Crawl', 'adminease' ), 'description' => __( 'While the maintenance page is shown, robots.txt asks search engines to stay away from every page. For a short pause the 503 page alone is the better signal.', 'adminease' ), 'field_description' => $this->get_robots_txt_notice(), 'attributes' => [ '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() ) { return; } if( $this->visitor_is_exempt( get_current_user_id() ) ) { return; } if( is_robots() && $this->blocks_robots() ) { 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 = $this->get_retry_after(); 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; } /** * Whether robots.txt should ask crawlers to stay away while the maintenance page is shown. * * @return bool */ private function blocks_robots(): bool { return !empty( $this->settings['maintenance_mode_block_robots'] ?? false ); } /** * Warns that the robots.txt switch cannot work when a physical robots.txt shadows the virtual * one WordPress generates: the web server serves the file and the robots_txt filter never runs. * * @return string Notice text, or '' when there is no physical file. */ private function get_robots_txt_notice(): string { $robots_txt = FileHandler::get_instance()->get_robots_txt_status(); if( empty( $robots_txt['exists'] ) ) { return ''; } return __( 'Your site has a physical robots.txt file, which replaces the one WordPress generates, so this setting cannot change it. Add "User-agent: *" and "Disallow: /" to that file by hand while the site is in maintenance mode.', 'adminease' ); } /** * Replaces the virtual robots.txt with a rule that asks every crawler to stay away while the * maintenance page is shown. Runs last so an SEO plugin's own rules cannot add the sitemap back. * Has no effect when a physical robots.txt exists, since the web server serves that file. * * @param string $output The robots.txt content. * * @return string */ public function robots_txt( $output ) { if( !$this->is_effectively_active() || !$this->blocks_robots() ) { return $output; } if( !headers_sent() ) { nocache_headers(); } return "User-agent: *\nDisallow: /\n"; } /** * 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; } /** * Whether a visitor skips the maintenance page: the access filter lets them through, they can * edit posts, or their IP is on the allowlist. Shared by the front end and the REST API block * so both always let through exactly the same visitors. * * @param int $user_id The visitor's user ID, 0 when logged out. * * @return bool True if the visitor skips maintenance mode. */ private function visitor_is_exempt( int $user_id ): bool { if( apply_filters( 'adminease_maintenance_mode_check_access', false ) ) { return true; } $can_edit = ( get_current_user_id() === $user_id ) ? current_user_can( 'edit_posts' ) : user_can( $user_id, 'edit_posts' ); if( $can_edit ) { return true; } $allowlist = $this->settings['maintenance_mode_ip_allowlist'] ?? ''; return !empty( $allowlist ) && Utils::ip_in_allowlist( Utils::get_client_ip(), $allowlist ); } /** * The Retry-After value in seconds sent with the 503 answer. 0 means no header. * * @return int Seconds, 3600 when not set. */ private function get_retry_after(): int { return isset( $this->settings['maintenance_mode_retry_after'] ) ? absint( $this->settings['maintenance_mode_retry_after'] ) : 3600; } /** * Answers REST API requests with a 503 while the maintenance page is shown, when the * "Protect the REST API" switch is on. Runs on rest_pre_dispatch, after WordPress has * worked out who is logged in, and lets through the same visitors as the front end. * * @param mixed $result The response so far, null when nothing answered yet. * * @return mixed The response so far, or a 503 response. */ public function check_rest_access( $result ) { if( null !== $result || !$this->is_effectively_active() ) { return $result; } if( !defined( 'REST_REQUEST' ) || !REST_REQUEST ) { return $result; } $user_id = get_current_user_id(); if( 0 === $user_id ) { $user_id = (int) wp_validate_auth_cookie( '', 'logged_in' ); } if( $this->visitor_is_exempt( $user_id ) ) { return $result; } $headers = wp_get_nocache_headers(); $retry_after = $this->get_retry_after(); if( $retry_after > 0 ) { $headers['Retry-After'] = (string) $retry_after; } return new \WP_REST_Response( [ 'code' => 'adminease_maintenance_mode', 'message' => __( 'This site is under maintenance. Please try again later.', 'adminease' ), 'data' => [ 'status' => 503 ], ], 503, $headers ); } /** * 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; } }