$snapshots[ $snapshot_key ] = $default_snapshot;
} else {
// Make sure the old snapshot has the same keys as the default one.
$snapshots[ $snapshot_key ] = array_merge( $default_snapshot, $snapshots[ $snapshot_key ] );
// Remove any keys that are not in the default snapshot.
$snapshot_keys = array_keys( $default_snapshot );
foreach ( $snapshots[ $snapshot_key ] as $key => $v ) {
if ( ! in_array( $key, $snapshot_keys, true ) ) {
unset( $snapshots[ $snapshot_key ][ $key ] );
}
}
// Always sort the old snapshot by keys to ensure consistency.
ksort( $snapshots[ $snapshot_key ] );
}
// Generate the new snapshot for the provider.
$new_snapshots[ $snapshot_key ] = array(
'extension_active' => true, // The extension is definitely active since we have a gateway from it.
'account_connected' => $provider['state']['account_connected'] ?? $default_snapshot['account_connected'],
'account_test_mode' => $provider['onboarding']['state']['test_mode'] ?? $default_snapshot['account_test_mode'],
'needs_setup' => $provider['state']['needs_setup'] ?? $default_snapshot['needs_setup'],
'test_mode' => $provider['state']['test_mode'] ?? $default_snapshot['test_mode'],
);
// Always sort the new snapshot by keys to ensure consistency.
ksort( $new_snapshots[ $snapshot_key ] );
}
// Provider snapshots that are not in the new snapshots but were in the old ones should be kept but marked as inactive.
foreach ( $snapshots as $snapshot_key => $old_snapshot ) {
if ( ! isset( $new_snapshots[ $snapshot_key ] ) ) {
$new_snapshots[ $snapshot_key ] = $old_snapshot;
$new_snapshots[ $snapshot_key ]['extension_active'] = false;
}
}
// Always order the new snapshots by keys to ensure DB updates happen only when the data changes.
ksort( $new_snapshots );
// Save the new snapshots back to the DB, as soon as we have them ready to avoid concurrent state change tracking.
// No need to autoload 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'],
)
);
}
}
}
}
Fatal error: Uncaught Automattic\WooCommerce\Internal\DependencyManagement\ContainerException: Attempt to get an instance of class 'Automattic\WooCommerce\Internal\Admin\Settings\Payments', which doesn't exist. in /home/hexcom/public_html/gamemart/wp-content/plugins/woocommerce/src/Internal/DependencyManagement/RuntimeContainer.php:105
Stack trace:
#0 /home/hexcom/public_html/gamemart/wp-content/plugins/woocommerce/src/Internal/DependencyManagement/RuntimeContainer.php(185): Automattic\WooCommerce\Internal\DependencyManagement\RuntimeContainer->get_core()
#1 [internal function]: Automattic\WooCommerce\Internal\DependencyManagement\RuntimeContainer->Automattic\WooCommerce\Internal\DependencyManagement\{closure}()
#2 /home/hexcom/public_html/gamemart/wp-content/plugins/woocommerce/src/Internal/DependencyManagement/RuntimeContainer.php(174): array_map()
#3 /home/hexcom/public_html/gamemart/wp-content/plugins/woocommerce/src/Internal/DependencyManagement/RuntimeContainer.php(119): Automattic\WooCommerce\Internal\DependencyManagement\RuntimeContainer->instantiate_class_using_reflection()
#4 /home/hexcom/public_html/gamemart/wp-content/plugins/woocommerce/src/Internal/DependencyManagement/RuntimeContainer.php(78): Automattic\WooCommerce\Internal\DependencyManagement\RuntimeContainer->get_core()
#5 /home/hexcom/public_html/gamemart/wp-content/plugins/woocommerce/src/Container.php(68): Automattic\WooCommerce\Internal\DependencyManagement\RuntimeContainer->get()
#6 /home/hexcom/public_html/gamemart/wp-content/plugins/woocommerce/includes/class-woocommerce.php(414): Automattic\WooCommerce\Container->get()
#7 /home/hexcom/public_html/gamemart/wp-content/plugins/woocommerce/includes/class-woocommerce.php(272): WooCommerce->init_hooks()
#8 /home/hexcom/public_html/gamemart/wp-content/plugins/woocommerce/includes/class-woocommerce.php(173): WooCommerce->__construct()
#9 /home/hexcom/public_html/gamemart/wp-content/plugins/woocommerce/woocommerce.php(55): WooCommerce::instance()
#10 /home/hexcom/public_html/gamemart/wp-content/plugins/woocommerce/woocommerce.php(70): WC()
#11 /home/hexcom/public_html/gamemart/wp-settings.php(597): include_once('/home/hexcom/pu...')
#12 /home/hexcom/public_html/gamemart/wp-config.php(98): require_once('/home/hexcom/pu...')
#13 /home/hexcom/public_html/gamemart/wp-load.php(50): require_once('/home/hexcom/pu...')
#14 /home/hexcom/public_html/gamemart/wp-blog-header.php(13): require_once('/home/hexcom/pu...')
#15 /home/hexcom/public_html/gamemart/index.php(17): require('/home/hexcom/pu...')
#16 {main}
thrown in /home/hexcom/public_html/gamemart/wp-content/plugins/woocommerce/src/Internal/DependencyManagement/RuntimeContainer.php on line 105