$settings */ private static function register_assets( array $settings ): void { wp_register_style( 'agomaintenance-page', AGOMAINTENANCE_URL . 'assets/css/maintenance.css', [], AGOMAINTENANCE_VERSION ); $overlay = max( 0, min( 100, intval( $settings['overlay_opacity'] ) ) ) / 100; $css = ':root{--ago-bg:' . sanitize_hex_color( $settings['bg_color'] ) . ';--ago-text:' . sanitize_hex_color( $settings['text_color'] ) . ';--ago-overlay:' . $overlay . ';}'; if ( ! empty( $settings['bg_image_url'] ) ) { $css .= 'body.ago-has-bg{background-image:url("' . esc_url( $settings['bg_image_url'] ) . '");}'; } wp_add_inline_style( 'agomaintenance-page', $css ); if ( '' !== self::countdown_target( $settings ) ) { wp_register_script( 'agomaintenance-countdown', AGOMAINTENANCE_URL . 'assets/js/countdown.js', [], AGOMAINTENANCE_VERSION, false ); } } /** * Whether visitors get the page now: the switch is on and, when a window * is set, the current time is inside it. Without an end the page stays * until it is turned off; without a start it begins right away. * * @param array $settings */ public static function is_active( array $settings, ?int $now = null ): bool { if ( empty( $settings['enabled'] ) ) { return false; } $now = $now ?? time(); $start = self::timestamp( $settings['schedule_start'] ?? '' ); $end = self::timestamp( $settings['schedule_end'] ?? '' ); return ( null === $start || $now >= $start ) && ( null === $end || $now < $end ); } /** * Seconds sent in Retry-After: until the end of the window, or until the * countdown when there is no end, and one hour when neither is ahead. * * @param array $settings */ public static function retry_after( array $settings, ?int $now = null ): int { $now = $now ?? time(); foreach ( [ 'schedule_end', 'countdown_datetime' ] as $key ) { $at = self::timestamp( $settings[ $key ] ?? '' ); if ( null !== $at && $at > $now ) { return max( 60, $at - $now ); } } return HOUR_IN_SECONDS; } /** * Countdown target as a Unix timestamp in milliseconds, read in the site * timezone, or an empty string when there is no valid date. Without a * date of its own it counts down to the end of the window. * * @param array $settings */ public static function countdown_target( array $settings ): string { $at = self::timestamp( $settings['countdown_datetime'] ?? '' ) ?? self::timestamp( $settings['schedule_end'] ?? '' ); return null === $at ? '' : (string) ( $at * 1000 ); } /** A datetime-local value read in the site timezone, or null when empty or invalid. */ private static function timestamp( $value ): ?int { $value = trim( (string) $value ); if ( '' === $value ) { return null; } try { return ( new \DateTimeImmutable( $value, wp_timezone() ) )->getTimestamp(); } catch ( \Exception $e ) { return null; } } public static function admin_bar_indicator( \WP_Admin_Bar $bar ): void { $settings = Plugin::get_settings(); if ( empty( $settings['enabled'] ) || ! current_user_can( 'manage_options' ) ) { return; } if ( self::is_active( $settings ) ) { $label = 'maintenance' === $settings['mode'] ? __( 'Maintenance ON', 'ago-maintenance' ) : __( 'Coming Soon ON', 'ago-maintenance' ); } else { $start = self::timestamp( $settings['schedule_start'] ?? '' ); if ( null === $start || $start <= time() ) { return; } /* translators: %s: date and time when the page turns on. */ $label = sprintf( __( 'Scheduled: %s', 'ago-maintenance' ), wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $start ) ); } $bar->add_node( [ 'id' => 'agomaintenance-indicator', 'title' => '' . esc_html( $label ) . '', 'href' => admin_url( 'admin.php?page=agomaintenance' ), ] ); } /** * Whether the request is one of the WordPress shortcuts to the login screen * (/admin, /login, /wp-admin, /dashboard), which core redirects later on * template_redirect. Only the exact path counts: the query string and * longer paths that merely contain those words keep the maintenance page. */ private static function is_login_shortcut(): bool { if ( empty( $_SERVER['REQUEST_URI'] ) ) { return false; } // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- only the path is kept and it is sanitized below. $path = wp_parse_url( wp_unslash( $_SERVER['REQUEST_URI'] ), PHP_URL_PATH ); if ( ! is_string( $path ) ) { return false; } $path = sanitize_text_field( rawurldecode( $path ) ); $home = (string) wp_parse_url( home_url( '/' ), PHP_URL_PATH ); if ( '' !== $home && 0 === strpos( $path, $home ) ) { $path = substr( $path, strlen( $home ) ); } return in_array( strtolower( trim( $path, '/' ) ), [ 'admin', 'login', 'wp-admin', 'dashboard', 'wp-login.php' ], true ); } /** * The address the request comes from. Only REMOTE_ADDR is read: the * forwarding headers are set by the client and anyone could send one with * an address from the list. */ private static function get_client_ip(): string { if ( empty( $_SERVER['REMOTE_ADDR'] ) ) { return ''; } $ip = filter_var( sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ), FILTER_VALIDATE_IP ); return false === $ip ? '' : $ip; } }