monitor = $monitor; } /** * Register administrator interfaces without outbound requests. * * @since 1.8.0 */ public function hooks(): void { add_action( 'admin_notices', [ $this, 'notice' ] ); add_action( 'admin_bar_menu', [ $this, 'toolbar' ], 90 ); add_filter( 'site_status_tests', [ $this, 'site_tests' ] ); add_action( 'admin_post_activelayer_dismiss_protection_gap', [ $this, 'dismiss' ] ); } /** * Read the active local interruption for the current scope. * * @since 1.8.0 * * @return array|null Current episode. */ public static function active(): ?array { return ( new EpisodeStore() )->active( ( new ProtectionMonitor() )->scope() ); } /** * Describe the current episode count and its specific cause. * * @since 1.8.0 * * @param array $episode Current interruption. * * @return string Translated cause and count. */ private function episode_message( array $episode ): string { $count = (int) $episode['count']; if ( $episode['reason'] === 'quota_exhausted' ) { $lead = $this->quota_message(); } else { $lead = SettingsHelper::has_api_key() ? __( 'There was a problem with your ActiveLayer connection. Please repeat the connection steps to start checking new submissions for spam.', 'activelayer-anti-spam-spam-protection-for-forms-comments' ) : __( 'Connect ActiveLayer to start checking new submissions for spam.', 'activelayer-anti-spam-spam-protection-for-forms-comments' ); } return $lead . ' ' . sprintf( /* translators: %s: number of submissions missed while protection was unavailable. */ _n( '%s submission not checked by ActiveLayer.', '%s submissions not checked by ActiveLayer.', $count, 'activelayer-anti-spam-spam-protection-for-forms-comments' ), number_format_i18n( $count ) ); } /** * Explain exhausted checks using the cached plan size when it is known. * * @since 1.8.0 * * @return string Plain-language plan message. */ private function quota_message(): string { $stats = get_transient( SubscriptionStats::CACHE_KEY ); $limit = (int) ( $stats['requests_limit'] ?? 0 ); if ( $limit > 0 ) { return sprintf( /* translators: %s: total checks on the plan. */ __( 'You\'ve used all %s checks on your plan. New submissions are not being checked for spam by ActiveLayer.', 'activelayer-anti-spam-spam-protection-for-forms-comments' ), number_format_i18n( $limit ) ); } return __( 'You\'ve used all checks on your plan. New submissions are not being checked for spam by ActiveLayer.', 'activelayer-anti-spam-spam-protection-for-forms-comments' ); } /** * Render one dismissible-per-user episode notice after its first observation. * * @since 1.8.0 */ public function notice(): void { if ( ! current_user_can( 'manage_activelayer' ) ) { return; } $episode = self::active(); if ( ! $episode || get_user_meta( get_current_user_id(), $this->dismissal_key(), true ) === $episode['episode_id'] ) { return; } $dismiss = wp_nonce_url( add_query_arg( 'episode', $episode['episode_id'], admin_url( 'admin-post.php?action=activelayer_dismiss_protection_gap' ) ), 'activelayer_dismiss_protection_gap_' . $episode['episode_id'] ); echo '
' . esc_html( $this->episode_message( $episode ) ) . ' '; $this->cta( $episode ); echo ' ' . esc_html__( 'Dismiss', 'activelayer-anti-spam-spam-protection-for-forms-comments' ) . '
'; echo esc_html( $this->episode_message( $episode ) ) . ' '; $this->cta( $episode ); echo '
' . esc_html( $message ) . '
' . esc_html( $this->health_description( $episode, $exhausted ) ) . '
', 'actions' => $actions, 'test' => 'activelayer_protection_gap', ]; } /** * Explain known failures without claiming missed submissions before any were observed. * * @since 1.8.0 * * @param array|null $episode Current observed interruption. * @param bool $exhausted Whether cached quota confirms exhaustion. * * @return string Translated Site Health description. */ private function health_description( ?array $episode, bool $exhausted ): string { if ( $episode ) { return $this->episode_message( $episode ); } if ( $exhausted ) { return $this->quota_message(); } return __( 'This status covers supported forms with ActiveLayer protection turned on. You can review your connection and protected forms in ActiveLayer settings.', 'activelayer-anti-spam-spam-protection-for-forms-comments' ); } /** * Use the positive Site Health label only when no quota exhaustion is known. * * @since 1.8.0 * * @return string Site Health connection label. */ private function connection_status_label(): string { if ( UpgradeHelper::is_quota_exhausted_cached() ) { return __( 'ActiveLayer plan limit reached', 'activelayer-anti-spam-spam-protection-for-forms-comments' ); } return SettingsHelper::has_api_key() ? __( 'ActiveLayer spam blocking is fully operational', 'activelayer-anti-spam-spam-protection-for-forms-comments' ) : __( 'Connect ActiveLayer to check for spam', 'activelayer-anti-spam-spam-protection-for-forms-comments' ); } /** * Scope user dismissal state to this blog on multisite. * * @since 1.8.0 * * @return string User metadata key. */ private function dismissal_key(): string { return 'activelayer_protection_dismissed_' . get_current_blog_id(); } /** * Save a capability-checked, nonce-protected per-user episode dismissal. * * @since 1.8.0 */ public function dismiss(): void { if ( ! current_user_can( 'manage_activelayer' ) ) { wp_die( esc_html__( 'Permission denied.', 'activelayer-anti-spam-spam-protection-for-forms-comments' ), '', [ 'response' => 403 ] ); } $requested = isset( $_GET['episode'] ) ? sanitize_text_field( wp_unslash( $_GET['episode'] ) ) : ''; check_admin_referer( 'activelayer_dismiss_protection_gap_' . $requested ); $episode = self::active(); if ( $episode && hash_equals( $episode['episode_id'], $requested ) ) { update_user_meta( get_current_user_id(), $this->dismissal_key(), $episode['episode_id'] ); } $referer = wp_get_referer(); wp_safe_redirect( $referer ? $referer : admin_url( 'index.php' ) ); exit; } }