registry = IntegrationRegistry::get_instance(); $this->persistor = new SettingsPersistor( $this->registry ); } /** * Redirect legacy ?integration= settings URLs to the dedicated Integrations page. * * Runs on admin_init (before any output) so the redirect fires before headers * are sent. Scoped to the settings screen so the ?integration= query arg is not * hijacked on unrelated admin pages. * * @since 1.3.0 * * @return void */ public function maybe_redirect_legacy_integration(): void { if ( wp_doing_ajax() || ! current_user_can( 'manage_activelayer' ) ) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only query param for routing. $page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only query param for routing. if ( ! isset( $_GET['integration'] ) || $page !== 'activelayer-settings' ) { return; } wp_safe_redirect( admin_url( 'admin.php?page=activelayer-integrations' ) ); exit; } /** * Render settings page. * * @since 1.0.0 * @since 1.3.0 Connect return callback and legacy ?integration= redirect moved to admin_init. */ public function render(): void { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.TooHigh // Surface the one-time Connect result notice persisted across the PRG redirect. // The claim + redirect itself runs earlier on admin_init (ConnectFlow::hooks), // before any output, so headers are not yet sent. The Connect return is a // GET-only flow; skip the transient read on form saves. // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Existence check only; the Connect flow is bound by a per-user PKCE verifier transient, not by this request. if ( empty( $_POST ) ) { $connect_notice = ( new ConnectFlow() )->take_notice(); if ( $connect_notice !== null ) { $this->set_notice( $connect_notice['message'], $connect_notice['type'] ); } } $this->handle_form_submission(); $settings = SettingsHelper::get_global_settings(); $api_key = SettingsHelper::get_api_key( $settings ); $has_api_key = SettingsHelper::has_api_key( $settings ); $logging_enabled = SettingsHelper::is_logging_enabled( $settings ); $sync_mode_enabled = SettingsHelper::is_sync_mode_enabled( $settings ); $environment_tracking_enabled = SettingsHelper::is_environment_tracking_enabled( $settings ); $behavioral_tracking_enabled = SettingsHelper::is_behavioral_tracking_enabled( $settings ); $retention_days = SettingsHelper::get_retention_days( $settings ); $this->render_notice(); ?>

render(); ?>
render_api_key_field( $api_key, $has_api_key ); ?> render_subscription_stats(); ?>

persistor->save_settings( wp_unslash( $_POST ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verified above. // Refresh registry to update cached settings. $this->registry->refresh(); $this->maybe_verify_saved_key( $previous_api_key ); // Refresh subscription stats. SubscriptionStats::get_instance()->schedule_refresh(); if ( ! empty( $notice_data['message'] ) ) { $this->set_notice( $notice_data['message'], $notice_data['type'] ?? 'success' ); } } /** * Verify a newly saved API key the same way the Verify Key button does. * * Save Changes used to store the key without ever calling POST /verify, so * the API never learned which site the key runs on: no connected_sites row, * no first_verified_at, and referral activation never fired for anyone who * saved instead of clicking Verify. Best-effort, like the Connect flow: a * failed verification keeps the key saved and leaves it unvalidated. * * @since 1.7.1 * * @param string $previous_api_key API key stored before this submission. */ private function maybe_verify_saved_key( string $previous_api_key ): void { $api_key = SettingsHelper::get_api_key(); if ( $api_key === '' || $api_key === $previous_api_key ) { return; } $result = ( new ApiClient() )->verify_key( $api_key ); if ( ! empty( $result['success'] ) ) { SettingsHelper::persist_validated_key( $api_key ); } } /** * Store a notice to display on the settings page. * * @since 1.0.0 * * @param string $message Notice message text. * @param string $type Notice type (success|warning|error|info). */ private function set_notice( string $message, string $type = 'success' ): void { $this->notice = [ 'message' => $message, 'type' => $type, ]; } /** * Render the stored notice, if any. * * @since 1.0.0 * * @return void */ private function render_notice(): void { if ( empty( $this->notice['message'] ) ) { return; } $message = $this->notice['message'] ?? ''; $type = $this->notice['type'] ?? 'success'; NoticeHelper::render( $message, $type, true ); } /** * Render the API key field section. * * @since 1.2.0 * @since 1.3.0 Read the validation record via SettingsHelper::OPTION_API_KEY_VALIDATED. * * @param string $api_key Current API key value. * @param bool $has_api_key Whether an API key is configured. * * @return void */ private function render_api_key_field( string $api_key, bool $has_api_key ): void { // Check if current API key is validated. $api_key_validation = get_option( SettingsHelper::OPTION_API_KEY_VALIDATED, [] ); $is_key_validated = ! empty( $api_key_validation['is_valid'] ) && ! empty( $api_key_validation['key'] ) && $api_key_validation['key'] === $api_key; // Check usage exhaustion for connected keys. $is_exhausted = $has_api_key && UpgradeHelper::is_usage_exhausted(); $is_free = $has_api_key && UpgradeHelper::is_free_plan(); $upgrade_url = UpgradeHelper::get_upgrade_url( 'api_key_field' ); ?>

get_subscription_stats(); if ( empty( $stats ) ) { return; } $usage_percentage = $stats['usage_percentage'] ?? 0; $usage_class = $this->get_subscription_usage_class( (float) $usage_percentage ); $is_exhausted = UpgradeHelper::is_usage_exhausted( $stats ); $is_free = UpgradeHelper::is_free_plan( $stats ); $upgrade_url = UpgradeHelper::get_upgrade_url( 'usage_box' ); $period_label = UpgradeHelper::get_period_label( $stats ); ?>

render_subscription_section_description( $is_free ); ?>
render_subscription_section_header( $is_free, $period_label, $upgrade_url ); ?>
/
%
render_subscription_usage_limit_message( $is_exhausted, $is_free ); ?>

%s', 'activelayer-anti-spam-spam-protection-for-forms-comments' ), '' . esc_html__( 'API usage resets every month on the 1st.', 'activelayer-anti-spam-spam-protection-for-forms-comments' ) . '' ) ); ?>

get_stats(); if ( ! ( $stats['success'] ?? false ) ) { return []; } return $stats; } /** * Determine usage CSS class by percentage. * * @since 1.2.0 * * @param float $usage_percentage Usage percent from 0 to 100. * * @return string CSS class. */ private function get_subscription_usage_class( float $usage_percentage ): string { if ( $usage_percentage >= 90 ) { return 'status-error'; } if ( $usage_percentage >= 70 ) { return 'status-warning'; } return 'status-success'; } /** * AJAX handler for API key verification. * * @since 1.0.0 * @since 1.3.0 Persist the verified key via SettingsHelper::persist_validated_key(). */ public function ajax_verify_api_key(): void { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.TooHigh // Verify nonce. if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'activelayer_verify_api_key' ) ) { wp_send_json_error( [ 'message' => __( 'Security check failed', 'activelayer-anti-spam-spam-protection-for-forms-comments' ) ] ); return; } // Check user capabilities. if ( ! current_user_can( 'manage_activelayer' ) ) { wp_send_json_error( [ 'message' => __( 'Insufficient permissions', 'activelayer-anti-spam-spam-protection-for-forms-comments' ) ] ); return; } // Get API key from request. $api_key = isset( $_POST[ SettingsHelper::KEY_API ] ) ? sanitize_text_field( wp_unslash( $_POST[ SettingsHelper::KEY_API ] ) ) : ''; $api_key = SettingsHelper::get_api_key( [ SettingsHelper::KEY_API => $api_key, ] ); if ( $api_key === '' ) { wp_send_json_error( [ 'message' => __( 'Please enter an API key', 'activelayer-anti-spam-spam-protection-for-forms-comments' ) ] ); return; } // Use ApiClient to verify the API key. $api_client = new ApiClient(); $result = $api_client->verify_key( $api_key ); if ( $result['success'] ) { // Persist the key + validation record (shared with the Connect flow). SettingsHelper::persist_validated_key( $api_key ); // Clear stale stats and schedule fresh fetch for the new key. SubscriptionStats::get_instance()->clear_cache(); SubscriptionStats::get_instance()->schedule_refresh(); $success_message = wp_strip_all_tags( __( 'API key verified and saved!', 'activelayer-anti-spam-spam-protection-for-forms-comments' ) ); wp_send_json_success( [ 'message' => $success_message, 'key_saved' => true, ] ); } else { // Clear validation status on failure. delete_option( SettingsHelper::OPTION_API_KEY_VALIDATED ); $error_message = $result['message'] ?? __( 'API key verification failed', 'activelayer-anti-spam-spam-protection-for-forms-comments' ); $error_message = wp_strip_all_tags( (string) $error_message ); wp_send_json_error( [ 'message' => $error_message, ] ); } } }