'maintenance', 'title' => '', 'headline' => __( 'We are making the site better', 'ai-guardwall' ), 'message' => __( 'The site is down for a short maintenance and will be back soon. Thank you for your patience.', 'ai-guardwall' ), 'logo' => '', 'bg_image' => '', 'bg_color' => '#0f172a', 'text_color' => '#f8fafc', 'accent' => '#22c55e', 'end' => 0, 'countdown' => 1, 'auto_off' => 1, 'roles' => array( 'administrator', 'editor' ), 'ips' => '', 'paths' => '', 'noindex' => 1, 'login_link' => 1, 'bypass_key' => '', ); } public static function get() { $saved = get_option( self::OPTION, array() ); return array_merge( self::defaults(), is_array( $saved ) ? $saved : array() ); } /** * Save the settings (only known keys, cleaned). * * @param array $in Posted values. * @return array|WP_Error The saved settings. */ public static function save( array $in ) { $s = self::get(); foreach ( $in as $key => $value ) { switch ( $key ) { case 'mode': $s[ $key ] = 'coming_soon' === $value ? 'coming_soon' : 'maintenance'; break; case 'title': case 'headline': $s[ $key ] = sanitize_text_field( (string) $value ); break; case 'message': $s[ $key ] = wp_kses_post( (string) $value ); break; case 'logo': case 'bg_image': $s[ $key ] = esc_url_raw( trim( (string) $value ), array( 'http', 'https' ) ); break; case 'bg_color': case 'text_color': case 'accent': $color = sanitize_hex_color( (string) $value ); $s[ $key ] = $color ? $color : self::defaults()[ $key ]; break; case 'end': $s[ $key ] = self::parse_time( $value ); break; case 'countdown': case 'auto_off': case 'noindex': case 'login_link': $s[ $key ] = empty( $value ) || 'false' === $value ? 0 : 1; break; case 'roles': $roles = array_keys( wp_roles()->get_names() ); $s[ $key ] = array_values( array_intersect( array_map( 'sanitize_key', (array) $value ), $roles ) ); break; case 'ips': $keep = array(); foreach ( preg_split( '/[\s,]+/', (string) $value ) as $entry ) { $entry = trim( $entry ); if ( '' !== $entry && preg_match( '#^[0-9a-fA-F:.*/]+$#', $entry ) ) { $keep[] = $entry; } } $s[ $key ] = implode( "\n", array_unique( $keep ) ); break; case 'paths': $keep = array(); foreach ( preg_split( '/\R/', (string) $value ) as $line ) { $line = trim( sanitize_text_field( $line ) ); if ( '' !== $line ) { $keep[] = '/' . ltrim( $line, '/' ); } } $s[ $key ] = implode( "\n", array_unique( $keep ) ); break; case 'bypass_key': $s[ $key ] = substr( preg_replace( '/[^A-Za-z0-9_-]/', '', (string) $value ), 0, 40 ); break; } } if ( '' === $s['bypass_key'] ) { $s['bypass_key'] = Softvolt_Ai_Bodyguard_Helpers::random_slug( 16 ); } update_option( self::OPTION, $s, false ); return $s; } /** * A date and time in the site's time zone ("2026-10-20T18:30") as a timestamp. * * @param mixed $value Value. * @return int 0 when empty or not a date. */ public static function parse_time( $value ) { if ( is_numeric( $value ) ) { return max( 0, (int) $value ); } $value = trim( (string) $value ); if ( '' === $value ) { return 0; } $date = date_create( $value, wp_timezone() ); return $date ? $date->getTimestamp() : 0; } /** * The end time for an . * * @param int $time Timestamp. * @return string */ public static function local_time( $time ) { return $time ? wp_date( 'Y-m-d\TH:i', (int) $time ) : ''; } public static function bypass_url() { $s = self::get(); return '' === $s['bypass_key'] ? '' : add_query_arg( 'svabg_pass', $s['bypass_key'], home_url( '/' ) ); } /* ------------------------------ front end ------------------------------ */ /** * Whether the current visitor gets the site as usual. * * @return bool */ public static function lets_through() { $s = self::get(); if ( is_user_logged_in() ) { if ( current_user_can( 'manage_options' ) ) { return true; } $user = wp_get_current_user(); if ( array_intersect( (array) $user->roles, (array) $s['roles'] ) ) { return true; } } if ( '' !== trim( (string) $s['ips'] ) && Softvolt_Ai_Bodyguard_Helpers::ip_in_list( Softvolt_Ai_Bodyguard_Helpers::client_ip(), $s['ips'] ) ) { return true; } if ( self::path_excluded( Softvolt_Ai_Bodyguard_Helpers::request_path(), $s['paths'] ) ) { return true; } if ( '' !== $s['bypass_key'] && isset( $_COOKIE[ self::COOKIE ] ) && hash_equals( self::cookie_value( $s['bypass_key'] ), sanitize_text_field( wp_unslash( $_COOKIE[ self::COOKIE ] ) ) ) ) { return true; } return false; } private static function cookie_value( $key ) { return hash_hmac( 'sha256', 'svabg-maintenance|' . $key, wp_salt( 'auth' ) ); } /** * Whether a path matches one of the excluded addresses ("/shop/" covers * everything under it, "*" is a wildcard). * * @param string $path Request path. * @param string $paths One per line. * @return bool */ public static function path_excluded( $path, $paths ) { $home = (string) wp_parse_url( home_url( '/' ), PHP_URL_PATH ); $path = '/' . ltrim( (string) $path, '/' ); if ( '' !== $home && '/' !== $home && 0 === stripos( $path, untrailingslashit( $home ) ) ) { $path = '/' . ltrim( substr( $path, strlen( untrailingslashit( $home ) ) ), '/' ); } foreach ( preg_split( '/\R/', (string) $paths ) as $rule ) { $rule = trim( $rule ); if ( '' === $rule ) { continue; } $regex = '#^' . str_replace( '\*', '.*', preg_quote( untrailingslashit( $rule ), '#' ) ) . '(?:/.*)?$#i'; if ( '/' === $rule ? '/' === $path : preg_match( $regex, $path ) ) { return true; } } return false; } public static function maybe_show() { // phpcs:disable WordPress.Security.NonceVerification.Recommended -- a link anyone may open; it only sets a cookie or shows a preview. $s = self::get(); if ( $s['auto_off'] && $s['end'] && time() >= (int) $s['end'] ) { Softvolt_Ai_Bodyguard_Modules::toggle( 'maintenance', false ); return; } if ( isset( $_GET['svabg_pass'] ) && '' !== $s['bypass_key'] && hash_equals( $s['bypass_key'], sanitize_text_field( wp_unslash( $_GET['svabg_pass'] ) ) ) ) { setcookie( self::COOKIE, self::cookie_value( $s['bypass_key'] ), time() + 7 * DAY_IN_SECONDS, COOKIEPATH ? COOKIEPATH : '/', COOKIE_DOMAIN, is_ssl(), true ); wp_safe_redirect( remove_query_arg( 'svabg_pass' ) ); exit; } $preview = isset( $_GET['svabg_maintenance_preview'] ) && current_user_can( 'manage_options' ); // phpcs:enable if ( ! $preview && ( is_robots() || is_favicon() || self::lets_through() ) ) { return; } self::render( $s, $preview ); exit; } /** * Print the page. * * @param array $s Settings. * @param bool $preview Shown to an administrator from the settings. */ public static function render( array $s, $preview = false ) { $coming = 'coming_soon' === $s['mode']; if ( ! headers_sent() ) { nocache_headers(); if ( $coming || $preview ) { status_header( 200 ); } else { status_header( 503 ); $wait = $s['end'] && (int) $s['end'] > time() ? (int) $s['end'] - time() : HOUR_IN_SECONDS; header( 'Retry-After: ' . min( $wait, 7 * DAY_IN_SECONDS ) ); } if ( $coming && $s['noindex'] ) { header( 'X-Robots-Tag: noindex, nofollow' ); } header( 'Content-Type: text/html; charset=' . get_bloginfo( 'charset' ) ); } $title = '' !== $s['title'] ? $s['title'] : ( $coming ? sprintf( /* translators: %s: site name */ __( '%s is coming soon', 'ai-guardwall' ), get_bloginfo( 'name' ) ) : sprintf( /* translators: %s: site name */ __( '%s is under maintenance', 'ai-guardwall' ), get_bloginfo( 'name' ) ) ); $end = $s['countdown'] && $s['end'] && (int) $s['end'] > time() ? (int) $s['end'] : 0; ?> >