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_api_key_status_message( $is_exhausted, $has_api_key ); ?>
ActiveLayer Account Dashboard.', 'activelayer-anti-spam-spam-protection-for-forms-comments' ) ), esc_url( $account_url ) ); ?> |
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 ); ?>
%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, ] ); } } }