utoload this option since it will be used only in the Payments Settings area. $result = update_option( self::PAYMENTS_PROVIDER_STATE_SNAPSHOTS_KEY, $new_snapshots, false ); if ( ! $result ) { // If we didn't update the option, we don't need to track any changes. return; } try { $this->maybe_track_providers_state_change( $payment_providers, $snapshots, $new_snapshots ); } catch ( \Throwable $exception ) { // If we failed to track the changes, we log the error but don't throw it. // This is to avoid breaking the Payments Settings page. SafeGlobalFunctionProxy::wc_get_logger()->error( 'Failed to track payment providers state change: ' . $exception->getMessage(), array( 'source' => 'settings-payments', ) ); } } /** * Maybe track the payment providers state change. * * This method will iterate through the new snapshots and compare them with the old ones. * If there are any changes, it will track them. * * @param array $providers The list of payment provider details. * @param array $old_snapshots The old snapshots of the providers' states. * @param array $new_snapshots The new snapshots of the providers' states. */ private function maybe_track_providers_state_change( array $providers, array $old_snapshots, array $new_snapshots ): void { foreach ( $new_snapshots as $provider_extension_slug => $new_snapshot ) { if ( ! isset( $old_snapshots[ $provider_extension_slug ] ) ) { // If we don't have an old snapshot for this provider, we can't track the change. continue; } // If there are no changes, we don't need to track anything. if ( maybe_serialize( $old_snapshots[ $provider_extension_slug ] ) === maybe_serialize( $new_snapshot ) ) { continue; } // Search for the provider by its plugin slug. $provider = null; foreach ( $providers as $p ) { if ( isset( $p['plugin']['slug'] ) && $p['plugin']['slug'] === $provider_extension_slug ) { $provider = $p; break; } } if ( ! $provider ) { // If we couldn't find the provider in the list it means the extension was deactivated. // Get the matching suggestion by its slug. $provider = $this->providers->get_extension_suggestion_by_plugin_slug( $provider_extension_slug ); if ( ! empty( $provider['id'] ) ) { // If we found the suggestion, we can use it as a replacement provider. // We need to set the `_suggestion_id` so we can handle the date more uniformly. $provider['_suggestion_id'] = $provider['id']; } } if ( ! $provider ) { continue; } $this->maybe_track_provider_state_change( $provider, $old_snapshots[ $provider_extension_slug ], $new_snapshot ); } } /** * Track the payment provider state change. * * @param array $provider The payment provider details. * @param array $old_snapshot The old snapshot of the provider's state. * @param array $new_snapshot The new snapshot of the provider's state. */ private function maybe_track_provider_state_change( array $provider, array $old_snapshot, array $new_snapshot ): void { // Note: Keep the order of the events in a way that makes sense for the onboarding flow. // Track extension_active change. if ( $old_snapshot['extension_active'] && ! $new_snapshot['extension_active'] ) { $this->record_event( 'provider_extension_deactivated', array( 'provider_id' => $provider['id'], 'suggestion_id' => $provider['_suggestion_id'], 'provider_extension_slug' => $provider['plugin']['slug'], ) ); // If the extension was also uninstalled, we can track that as well. if ( ! empty( $provider['plugin']['status'] ) && PaymentsProviders::EXTENSION_NOT_INSTALLED === $provider['plugin']['status'] ) { $this->record_event( 'provider_extension_uninstalled', array( 'provider_id' => $provider['id'], 'suggestion_id' => $provider['_suggestion_id'], 'provider_extension_slug' => $provider['plugin']['slug'], ) ); } } elseif ( ! $old_snapshot['extension_active'] && $new_snapshot['extension_active'] ) { $this->record_event( 'provider_extension_activated', array( 'provider_id' => $provider['id'], 'suggestion_id' => $provider['_suggestion_id'], 'provider_extension_slug' => $provider['plugin']['slug'], ) ); } // Track account_connected change. if ( $old_snapshot['account_connected'] && ! $new_snapshot['account_connected'] ) { $this->record_event( 'provider_account_disconnected', array( 'provider_id' => $provider['id'], 'suggestion_id' => $provider['_suggestion_id'], 'provider_extension_slug' => $provider['plugin']['slug'], 'provider_account_test_mode' => $old_snapshot['account_test_mode'] ? 'yes' : 'no', ) ); } elseif ( ! $old_snapshot['account_connected'] && $new_snapshot['account_connected'] ) { $this->record_event( 'provider_account_connected', array( 'provider_id' => $provider['id'], 'suggestion_id' => $provider['_suggestion_id'], 'provider_extension_slug' => $provider['plugin']['slug'], 'provider_account_test_mode' => $new_snapshot['account_test_mode'] ? 'yes' : 'no', ) ); } // Track needs_setup change. if ( $old_snapshot['needs_setup'] && ! $new_snapshot['needs_setup'] ) { $this->record_event( 'provider_setup_completed', array( 'provider_id' => $provider['id'], 'suggestion_id' => $provider['_suggestion_id'], 'provider_extension_slug' => $provider['plugin']['slug'], ) ); } elseif ( ! $old_snapshot['needs_setup'] && $new_snapshot['needs_setup'] ) { $this->record_event( 'provider_setup_required', array( 'provider_id' => $provider['id'], 'suggestion_id' => $provider['_suggestion_id'], 'provider_extension_slug' => $provider['plugin']['slug'], ) ); } // Track payments test_mode change, but only if an account is connected. if ( $new_snapshot['account_connected'] ) { if ( $old_snapshot['test_mode'] && ! $new_snapshot['test_mode'] ) { $this->record_event( 'provider_live_payments_enabled', array( 'provider_id' => $provider['id'], 'suggestion_id' => $provider['_suggestion_id'], 'provider_extension_slug' => $provider['plugin']['slug'], ) ); } elseif ( ! $old_snapshot['test_mode'] && $new_snapshot['test_mode'] ) { $this->record_event( 'provider_test_payments_enabled', array( 'provider_id' => $provider['id'], 'suggestion_id' => $provider['_suggestion_id'], 'provider_extension_slug' => $provider['plugin']['slug'], ) ); } } // Track account_test_mode change, but only if the account is connected. if ( $new_snapshot['account_connected'] ) { if ( $old_snapshot['account_test_mode'] && ! $new_snapshot['account_test_mode'] ) { $this->record_event( 'provider_account_live_mode_enabled', array( 'provider_id' => $provider['id'], 'suggestion_id' => $provider['_suggestion_id'], 'provider_extension_slug' => $provider['plugin']['slug'], ) ); } elseif ( ! $old_snapshot['account_test_mode'] && $new_snapshot['account_test_mode'] ) { $this->record_event( 'provider_account_test_mode_enabled', array( 'provider_id' => $provider['id'], 'suggestion_id' => $provider['_suggestion_id'], 'provider_extension_slug' => $provider['plugin']['slug'], ) ); } } } }