Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Internal/Admin/Settings
File: Payments.php
<?php
[0] Fix | Delete
declare( strict_types=1 );
[1] Fix | Delete
[2] Fix | Delete
namespace Automattic\WooCommerce\Internal\Admin\Settings;
[3] Fix | Delete
[4] Fix | Delete
use Automattic\WooCommerce\Internal\Admin\Settings\PaymentsProviders\WooPayments\WooPaymentsService;
[5] Fix | Delete
use Automattic\WooCommerce\Internal\Admin\Suggestions\PaymentsExtensionSuggestions as ExtensionSuggestions;
[6] Fix | Delete
use Automattic\WooCommerce\Internal\Logging\SafeGlobalFunctionProxy;
[7] Fix | Delete
use Exception;
[8] Fix | Delete
[9] Fix | Delete
defined( 'ABSPATH' ) || exit;
[10] Fix | Delete
/**
[11] Fix | Delete
* Payments settings service class.
[12] Fix | Delete
*
[13] Fix | Delete
* @internal
[14] Fix | Delete
*/
[15] Fix | Delete
class Payments {
[16] Fix | Delete
[17] Fix | Delete
const PAYMENTS_NOX_PROFILE_KEY = 'woocommerce_payments_nox_profile';
[18] Fix | Delete
const PAYMENTS_PROVIDER_STATE_SNAPSHOTS_KEY = 'woocommerce_payments_provider_state_snapshots';
[19] Fix | Delete
[20] Fix | Delete
const SUGGESTIONS_CONTEXT = 'wc_settings_payments';
[21] Fix | Delete
[22] Fix | Delete
const EVENT_PREFIX = 'settings_payments_';
[23] Fix | Delete
[24] Fix | Delete
const FROM_PAYMENTS_SETTINGS = 'WCADMIN_PAYMENT_SETTINGS';
[25] Fix | Delete
const FROM_PAYMENTS_MENU_ITEM = 'PAYMENTS_MENU_ITEM';
[26] Fix | Delete
const FROM_PAYMENTS_TASK = 'WCADMIN_PAYMENT_TASK';
[27] Fix | Delete
const FROM_ADDITIONAL_PAYMENTS_TASK = 'WCADMIN_ADDITIONAL_PAYMENT_TASK';
[28] Fix | Delete
const FROM_PROVIDER_ONBOARDING = 'PROVIDER_ONBOARDING';
[29] Fix | Delete
[30] Fix | Delete
/**
[31] Fix | Delete
* The payment providers service.
[32] Fix | Delete
*
[33] Fix | Delete
* @var PaymentsProviders
[34] Fix | Delete
*/
[35] Fix | Delete
private PaymentsProviders $providers;
[36] Fix | Delete
[37] Fix | Delete
/**
[38] Fix | Delete
* The payment extension suggestions service.
[39] Fix | Delete
*
[40] Fix | Delete
* @var ExtensionSuggestions
[41] Fix | Delete
*/
[42] Fix | Delete
private ExtensionSuggestions $extension_suggestions;
[43] Fix | Delete
[44] Fix | Delete
/**
[45] Fix | Delete
* Initialize the class instance.
[46] Fix | Delete
*
[47] Fix | Delete
* @param PaymentsProviders $payment_providers The payment providers service.
[48] Fix | Delete
* @param ExtensionSuggestions $payment_extension_suggestions The payment extension suggestions service.
[49] Fix | Delete
*
[50] Fix | Delete
* @internal
[51] Fix | Delete
*/
[52] Fix | Delete
final public function init( PaymentsProviders $payment_providers, ExtensionSuggestions $payment_extension_suggestions ): void {
[53] Fix | Delete
$this->providers = $payment_providers;
[54] Fix | Delete
$this->extension_suggestions = $payment_extension_suggestions;
[55] Fix | Delete
}
[56] Fix | Delete
[57] Fix | Delete
/**
[58] Fix | Delete
* Get the payment provider details list for the settings page.
[59] Fix | Delete
*
[60] Fix | Delete
* @param string $location The location for which the providers are being determined.
[61] Fix | Delete
* This is an ISO 3166-1 alpha-2 country code.
[62] Fix | Delete
* @param bool $for_display Optional. Whether the payment providers list is intended for display purposes or
[63] Fix | Delete
* it is meant to be used for internal business logic.
[64] Fix | Delete
* Primarily, this means that when it is not for display, we will use the raw
[65] Fix | Delete
* payment gateways list (all the registered gateways), not just the ones that
[66] Fix | Delete
* should be shown to the user on the Payments Settings page.
[67] Fix | Delete
* This complication is for backward compatibility as it relates to legacy settings hooks
[68] Fix | Delete
* being fired or not.
[69] Fix | Delete
* @param bool $remove_shells Optional. Whether to remove the payment providers shells from the list.
[70] Fix | Delete
* If the $for_display is true, this will be ignored since the display logic will
[71] Fix | Delete
* handle the shells itself.
[72] Fix | Delete
*
[73] Fix | Delete
* @return array The payment providers details list.
[74] Fix | Delete
* @throws Exception If there are malformed or invalid suggestions.
[75] Fix | Delete
*/
[76] Fix | Delete
public function get_payment_providers( string $location, bool $for_display = true, bool $remove_shells = false ): array {
[77] Fix | Delete
$payment_gateways = $this->providers->get_payment_gateways( $for_display );
[78] Fix | Delete
if ( ! $for_display && $remove_shells ) {
[79] Fix | Delete
$payment_gateways = $this->providers->remove_shell_payment_gateways( $payment_gateways, $location );
[80] Fix | Delete
}
[81] Fix | Delete
[82] Fix | Delete
$providers_order_map = $this->providers->get_order_map();
[83] Fix | Delete
[84] Fix | Delete
$payment_providers = array();
[85] Fix | Delete
[86] Fix | Delete
// Only include suggestions if the requesting user can install plugins.
[87] Fix | Delete
$suggestions = array();
[88] Fix | Delete
if ( current_user_can( 'install_plugins' ) ) {
[89] Fix | Delete
$suggestions = $this->providers->get_extension_suggestions( $location, self::SUGGESTIONS_CONTEXT );
[90] Fix | Delete
}
[91] Fix | Delete
// If we have preferred suggestions, add them to the providers list.
[92] Fix | Delete
if ( ! empty( $suggestions['preferred'] ) ) {
[93] Fix | Delete
// Sort them by priority, ASC.
[94] Fix | Delete
usort(
[95] Fix | Delete
$suggestions['preferred'],
[96] Fix | Delete
function ( $a, $b ) {
[97] Fix | Delete
return $a['_priority'] <=> $b['_priority'];
[98] Fix | Delete
}
[99] Fix | Delete
);
[100] Fix | Delete
[101] Fix | Delete
// By default, we will add the preferred suggestions at the top of the list.
[102] Fix | Delete
$last_preferred_order = -1;
[103] Fix | Delete
// If WooPayments is already present, we add the preferred suggestions after it.
[104] Fix | Delete
// This way we ensure default installed WooPayments is at the same place as its suggestion would be.
[105] Fix | Delete
if ( isset( $providers_order_map[ WooPaymentsService::GATEWAY_ID ] ) ) {
[106] Fix | Delete
$last_preferred_order = $providers_order_map[ WooPaymentsService::GATEWAY_ID ];
[107] Fix | Delete
}
[108] Fix | Delete
[109] Fix | Delete
foreach ( $suggestions['preferred'] as $suggestion ) {
[110] Fix | Delete
$suggestion_order_map_id = $this->providers->get_suggestion_order_map_id( $suggestion['id'] );
[111] Fix | Delete
// Determine the suggestion's order value.
[112] Fix | Delete
// If we don't have an order for it, add it to the top but keep the relative order:
[113] Fix | Delete
// PSP first, APM after PSP, offline PSP after PSP and APM.
[114] Fix | Delete
if ( ! isset( $providers_order_map[ $suggestion_order_map_id ] ) ) {
[115] Fix | Delete
$providers_order_map = Utils::order_map_add_at_order( $providers_order_map, $suggestion_order_map_id, $last_preferred_order + 1 );
[116] Fix | Delete
}
[117] Fix | Delete
[118] Fix | Delete
// Save the preferred provider's order to know where we should be inserting next.
[119] Fix | Delete
// But only if the last preferred order is less than the current one.
[120] Fix | Delete
if ( $last_preferred_order < $providers_order_map[ $suggestion_order_map_id ] ) {
[121] Fix | Delete
$last_preferred_order = $providers_order_map[ $suggestion_order_map_id ];
[122] Fix | Delete
}
[123] Fix | Delete
[124] Fix | Delete
// Change suggestion details to align it with a regular payment gateway.
[125] Fix | Delete
$suggestion['_suggestion_id'] = $suggestion['id'];
[126] Fix | Delete
$suggestion['id'] = $suggestion_order_map_id;
[127] Fix | Delete
$suggestion['_type'] = PaymentsProviders::TYPE_SUGGESTION;
[128] Fix | Delete
$suggestion['_order'] = $providers_order_map[ $suggestion_order_map_id ];
[129] Fix | Delete
unset( $suggestion['_priority'] );
[130] Fix | Delete
[131] Fix | Delete
$payment_providers[] = $suggestion;
[132] Fix | Delete
}
[133] Fix | Delete
}
[134] Fix | Delete
[135] Fix | Delete
foreach ( $payment_gateways as $payment_gateway ) {
[136] Fix | Delete
// Determine the gateway's order value.
[137] Fix | Delete
// If we don't have an order for it, add it to the end.
[138] Fix | Delete
if ( ! isset( $providers_order_map[ $payment_gateway->id ] ) ) {
[139] Fix | Delete
$providers_order_map = Utils::order_map_add_at_order( $providers_order_map, $payment_gateway->id, count( $payment_providers ) );
[140] Fix | Delete
}
[141] Fix | Delete
[142] Fix | Delete
$payment_providers[] = $this->providers->get_payment_gateway_details(
[143] Fix | Delete
$payment_gateway,
[144] Fix | Delete
$providers_order_map[ $payment_gateway->id ],
[145] Fix | Delete
$location
[146] Fix | Delete
);
[147] Fix | Delete
}
[148] Fix | Delete
[149] Fix | Delete
// Add offline payment methods group entry if we have offline payment methods.
[150] Fix | Delete
if ( in_array( PaymentsProviders::TYPE_OFFLINE_PM, array_column( $payment_providers, '_type' ), true ) ) {
[151] Fix | Delete
// Determine the item's order value.
[152] Fix | Delete
// If we don't have an order for it, add it to the end.
[153] Fix | Delete
if ( ! isset( $providers_order_map[ PaymentsProviders::OFFLINE_METHODS_ORDERING_GROUP ] ) ) {
[154] Fix | Delete
$providers_order_map = Utils::order_map_add_at_order( $providers_order_map, PaymentsProviders::OFFLINE_METHODS_ORDERING_GROUP, count( $payment_providers ) );
[155] Fix | Delete
}
[156] Fix | Delete
[157] Fix | Delete
$payment_providers[] = array(
[158] Fix | Delete
'id' => PaymentsProviders::OFFLINE_METHODS_ORDERING_GROUP,
[159] Fix | Delete
'_type' => PaymentsProviders::TYPE_OFFLINE_PMS_GROUP,
[160] Fix | Delete
'_order' => $providers_order_map[ PaymentsProviders::OFFLINE_METHODS_ORDERING_GROUP ],
[161] Fix | Delete
'title' => esc_html__( 'Take offline payments', 'woocommerce' ),
[162] Fix | Delete
'description' => esc_html__( 'Accept payments offline using multiple different methods. These can also be used to test purchases.', 'woocommerce' ),
[163] Fix | Delete
'icon' => plugins_url( 'assets/images/payment_methods/cod.svg', WC_PLUGIN_FILE ),
[164] Fix | Delete
// The offline PMs (and their group) are obviously from WooCommerce, and WC is always active.
[165] Fix | Delete
'plugin' => array(
[166] Fix | Delete
'_type' => 'wporg',
[167] Fix | Delete
'slug' => 'woocommerce',
[168] Fix | Delete
'file' => '', // This pseudo-provider should have no use for the plugin file.
[169] Fix | Delete
'status' => PaymentsProviders::EXTENSION_ACTIVE,
[170] Fix | Delete
),
[171] Fix | Delete
'management' => array(
[172] Fix | Delete
'_links' => array(
[173] Fix | Delete
'settings' => array(
[174] Fix | Delete
'href' => Utils::wc_payments_settings_url( '/' . ( class_exists( '\WC_Settings_Payment_Gateways' ) ? \WC_Settings_Payment_Gateways::OFFLINE_SECTION_NAME : 'offline' ) ),
[175] Fix | Delete
),
[176] Fix | Delete
),
[177] Fix | Delete
),
[178] Fix | Delete
);
[179] Fix | Delete
}
[180] Fix | Delete
[181] Fix | Delete
// Determine the final, standardized providers order map.
[182] Fix | Delete
$providers_order_map = $this->providers->enhance_order_map( $providers_order_map );
[183] Fix | Delete
// Enforce the order map on all providers, just in case.
[184] Fix | Delete
foreach ( $payment_providers as $key => $provider ) {
[185] Fix | Delete
$payment_providers[ $key ]['_order'] = $providers_order_map[ $provider['id'] ];
[186] Fix | Delete
}
[187] Fix | Delete
// NOTE: For now, save it back to the DB. This is temporary until we have a better way to handle this!
[188] Fix | Delete
$this->providers->save_order_map( $providers_order_map );
[189] Fix | Delete
[190] Fix | Delete
// Sort the payment providers by order, ASC.
[191] Fix | Delete
usort(
[192] Fix | Delete
$payment_providers,
[193] Fix | Delete
function ( $a, $b ) {
[194] Fix | Delete
return $a['_order'] <=> $b['_order'];
[195] Fix | Delete
}
[196] Fix | Delete
);
[197] Fix | Delete
[198] Fix | Delete
// Only process payment provider states if we are displaying the providers.
[199] Fix | Delete
// This is to ensure we don't introduce any performance issues outside the Payments settings page.
[200] Fix | Delete
if ( $for_display ) {
[201] Fix | Delete
$this->process_payment_provider_states( $payment_providers );
[202] Fix | Delete
}
[203] Fix | Delete
[204] Fix | Delete
return $payment_providers;
[205] Fix | Delete
}
[206] Fix | Delete
[207] Fix | Delete
/**
[208] Fix | Delete
* Get the payment extension suggestions for the given location.
[209] Fix | Delete
*
[210] Fix | Delete
* @param string $location The location for which the suggestions are being fetched.
[211] Fix | Delete
*
[212] Fix | Delete
* @return array[] The payment extension suggestions for the given location, split into preferred and other.
[213] Fix | Delete
* @throws Exception If there are malformed or invalid suggestions.
[214] Fix | Delete
*/
[215] Fix | Delete
public function get_payment_extension_suggestions( string $location ): array {
[216] Fix | Delete
return $this->providers->get_extension_suggestions( $location, self::SUGGESTIONS_CONTEXT );
[217] Fix | Delete
}
[218] Fix | Delete
[219] Fix | Delete
/**
[220] Fix | Delete
* Get the payment extension suggestions categories details.
[221] Fix | Delete
*
[222] Fix | Delete
* @return array The payment extension suggestions categories.
[223] Fix | Delete
*/
[224] Fix | Delete
public function get_payment_extension_suggestion_categories(): array {
[225] Fix | Delete
return $this->providers->get_extension_suggestion_categories();
[226] Fix | Delete
}
[227] Fix | Delete
[228] Fix | Delete
/**
[229] Fix | Delete
* Get the business location country code for the Payments settings.
[230] Fix | Delete
*
[231] Fix | Delete
* @return string The ISO 3166-1 alpha-2 country code to use for the overall business location.
[232] Fix | Delete
* If the user didn't set a location, the WC base location country code is used.
[233] Fix | Delete
*/
[234] Fix | Delete
public function get_country(): string {
[235] Fix | Delete
$user_nox_meta = get_user_meta( get_current_user_id(), self::PAYMENTS_NOX_PROFILE_KEY, true );
[236] Fix | Delete
if ( ! empty( $user_nox_meta['business_country_code'] ) ) {
[237] Fix | Delete
return $user_nox_meta['business_country_code'];
[238] Fix | Delete
}
[239] Fix | Delete
[240] Fix | Delete
return WC()->countries->get_base_country();
[241] Fix | Delete
}
[242] Fix | Delete
[243] Fix | Delete
/**
[244] Fix | Delete
* Set the business location country for the Payments settings.
[245] Fix | Delete
*
[246] Fix | Delete
* @param string $location The country code. This should be an ISO 3166-1 alpha-2 country code.
[247] Fix | Delete
*/
[248] Fix | Delete
public function set_country( string $location ): bool {
[249] Fix | Delete
$previous_country = $this->get_country();
[250] Fix | Delete
[251] Fix | Delete
$user_payments_nox_profile = get_user_meta( get_current_user_id(), self::PAYMENTS_NOX_PROFILE_KEY, true );
[252] Fix | Delete
[253] Fix | Delete
if ( empty( $user_payments_nox_profile ) ) {
[254] Fix | Delete
$user_payments_nox_profile = array();
[255] Fix | Delete
} else {
[256] Fix | Delete
$user_payments_nox_profile = maybe_unserialize( $user_payments_nox_profile );
[257] Fix | Delete
}
[258] Fix | Delete
$user_payments_nox_profile['business_country_code'] = $location;
[259] Fix | Delete
[260] Fix | Delete
$result = false !== update_user_meta( get_current_user_id(), self::PAYMENTS_NOX_PROFILE_KEY, $user_payments_nox_profile );
[261] Fix | Delete
[262] Fix | Delete
if ( $result && $previous_country !== $location ) {
[263] Fix | Delete
// Record an event that the business location (registration country code) was changed.
[264] Fix | Delete
$this->record_event(
[265] Fix | Delete
'business_location_update',
[266] Fix | Delete
array(
[267] Fix | Delete
'business_country' => $location,
[268] Fix | Delete
'previous_business_country' => $previous_country,
[269] Fix | Delete
)
[270] Fix | Delete
);
[271] Fix | Delete
}
[272] Fix | Delete
[273] Fix | Delete
return $result;
[274] Fix | Delete
}
[275] Fix | Delete
[276] Fix | Delete
/**
[277] Fix | Delete
* Update the payment providers order map.
[278] Fix | Delete
*
[279] Fix | Delete
* @param array $order_map The new order for payment providers.
[280] Fix | Delete
*
[281] Fix | Delete
* @return bool True if the payment providers ordering was successfully updated, false otherwise.
[282] Fix | Delete
*/
[283] Fix | Delete
public function update_payment_providers_order_map( array $order_map ): bool {
[284] Fix | Delete
$result = $this->providers->update_payment_providers_order_map( $order_map );
[285] Fix | Delete
[286] Fix | Delete
if ( $result ) {
[287] Fix | Delete
// Record an event that the payment providers order map was updated.
[288] Fix | Delete
$this->record_event(
[289] Fix | Delete
'payment_providers_order_map_updated',
[290] Fix | Delete
array(
[291] Fix | Delete
'order_map' => implode( ', ', array_keys( $this->providers->get_order_map() ) ),
[292] Fix | Delete
)
[293] Fix | Delete
);
[294] Fix | Delete
}
[295] Fix | Delete
[296] Fix | Delete
return $result;
[297] Fix | Delete
}
[298] Fix | Delete
[299] Fix | Delete
/**
[300] Fix | Delete
* Attach a payment extension suggestion.
[301] Fix | Delete
*
[302] Fix | Delete
* This is only an internal recording of attachment. No actual extension installation or activation happens.
[303] Fix | Delete
*
[304] Fix | Delete
* @param string $id The ID of the payment extension suggestion to attach.
[305] Fix | Delete
*
[306] Fix | Delete
* @return bool True if the suggestion was successfully marked as attached, false otherwise.
[307] Fix | Delete
* @throws Exception If the suggestion ID is invalid.
[308] Fix | Delete
*/
[309] Fix | Delete
public function attach_payment_extension_suggestion( string $id ): bool {
[310] Fix | Delete
$result = $this->providers->attach_extension_suggestion( $id );
[311] Fix | Delete
[312] Fix | Delete
if ( $result ) {
[313] Fix | Delete
// Record an event that the suggestion was attached.
[314] Fix | Delete
$this->record_event(
[315] Fix | Delete
'extension_suggestion_attached',
[316] Fix | Delete
array(
[317] Fix | Delete
'suggestion_id' => $id,
[318] Fix | Delete
)
[319] Fix | Delete
);
[320] Fix | Delete
}
[321] Fix | Delete
[322] Fix | Delete
return $result;
[323] Fix | Delete
}
[324] Fix | Delete
[325] Fix | Delete
/**
[326] Fix | Delete
* Hide a payment extension suggestion.
[327] Fix | Delete
*
[328] Fix | Delete
* @param string $id The ID of the payment extension suggestion to hide.
[329] Fix | Delete
*
[330] Fix | Delete
* @return bool True if the suggestion was successfully hidden, false otherwise.
[331] Fix | Delete
* @throws Exception If the suggestion ID is invalid.
[332] Fix | Delete
*/
[333] Fix | Delete
public function hide_payment_extension_suggestion( string $id ): bool {
[334] Fix | Delete
$result = $this->providers->hide_extension_suggestion( $id );
[335] Fix | Delete
[336] Fix | Delete
if ( $result ) {
[337] Fix | Delete
// Record an event that the suggestion was hidden.
[338] Fix | Delete
$this->record_event(
[339] Fix | Delete
'extension_suggestion_hidden',
[340] Fix | Delete
array(
[341] Fix | Delete
'suggestion_id' => $id,
[342] Fix | Delete
)
[343] Fix | Delete
);
[344] Fix | Delete
}
[345] Fix | Delete
[346] Fix | Delete
return $result;
[347] Fix | Delete
}
[348] Fix | Delete
[349] Fix | Delete
/**
[350] Fix | Delete
* Dismiss a payment extension suggestion incentive.
[351] Fix | Delete
*
[352] Fix | Delete
* @param string $suggestion_id The suggestion ID.
[353] Fix | Delete
* @param string $incentive_id The incentive ID.
[354] Fix | Delete
* @param string $context Optional. The context in which the incentive should be dismissed.
[355] Fix | Delete
* Default is to dismiss the incentive in all contexts.
[356] Fix | Delete
* @param bool $do_not_track Optional. If true, the incentive dismissal will not be tracked.
[357] Fix | Delete
*
[358] Fix | Delete
* @return bool True if the incentive was not previously dismissed and now it is.
[359] Fix | Delete
* False if the incentive was already dismissed or could not be dismissed.
[360] Fix | Delete
* @throws Exception If the incentive could not be dismissed due to an error.
[361] Fix | Delete
*/
[362] Fix | Delete
public function dismiss_extension_suggestion_incentive( string $suggestion_id, string $incentive_id, string $context = 'all', bool $do_not_track = false ): bool {
[363] Fix | Delete
$result = $this->extension_suggestions->dismiss_incentive( $incentive_id, $suggestion_id, $context );
[364] Fix | Delete
[365] Fix | Delete
if ( ! $do_not_track && $result ) {
[366] Fix | Delete
// Record an event that the incentive was dismissed.
[367] Fix | Delete
$this->record_event(
[368] Fix | Delete
'incentive_dismiss',
[369] Fix | Delete
array(
[370] Fix | Delete
'suggestion_id' => $suggestion_id,
[371] Fix | Delete
'incentive_id' => $incentive_id,
[372] Fix | Delete
'display_context' => $context,
[373] Fix | Delete
)
[374] Fix | Delete
);
[375] Fix | Delete
}
[376] Fix | Delete
[377] Fix | Delete
return $result;
[378] Fix | Delete
}
[379] Fix | Delete
[380] Fix | Delete
/**
[381] Fix | Delete
* Send a Tracks event.
[382] Fix | Delete
*
[383] Fix | Delete
* By default, Woo adds `url`, `blog_lang`, `blog_id`, `store_id`, `products_count`, and `wc_version`
[384] Fix | Delete
* properties to every event.
[385] Fix | Delete
*
[386] Fix | Delete
* @param string $name The event name.
[387] Fix | Delete
* If it is not prefixed with self::EVENT_PREFIX, it will be prefixed with it.
[388] Fix | Delete
* @param array $properties Optional. The event custom properties.
[389] Fix | Delete
* These properties will be merged with the default properties.
[390] Fix | Delete
* Default properties values take precedence over the provided ones.
[391] Fix | Delete
*
[392] Fix | Delete
* @return void
[393] Fix | Delete
*/
[394] Fix | Delete
private function record_event( string $name, array $properties = array() ) {
[395] Fix | Delete
if ( ! function_exists( 'wc_admin_record_tracks_event' ) ) {
[396] Fix | Delete
return;
[397] Fix | Delete
}
[398] Fix | Delete
[399] Fix | Delete
// If the event name is empty, we don't record it.
[400] Fix | Delete
if ( empty( $name ) ) {
[401] Fix | Delete
return;
[402] Fix | Delete
}
[403] Fix | Delete
[404] Fix | Delete
// If the event name is not prefixed with `settings_payments_`, we prefix it.
[405] Fix | Delete
if ( ! str_starts_with( $name, self::EVENT_PREFIX ) ) {
[406] Fix | Delete
$name = self::EVENT_PREFIX . $name;
[407] Fix | Delete
}
[408] Fix | Delete
[409] Fix | Delete
// Add default properties to every event and overwrite custom properties with the same keys.
[410] Fix | Delete
$properties = array_merge(
[411] Fix | Delete
$properties,
[412] Fix | Delete
array(
[413] Fix | Delete
'business_country' => $this->get_country(),
[414] Fix | Delete
),
[415] Fix | Delete
);
[416] Fix | Delete
[417] Fix | Delete
wc_admin_record_tracks_event( $name, $properties );
[418] Fix | Delete
}
[419] Fix | Delete
[420] Fix | Delete
/**
[421] Fix | Delete
* Process the payment providers states and update the snapshots in the DB.
[422] Fix | Delete
*
[423] Fix | Delete
* @param array $payment_providers The payment providers details list.
[424] Fix | Delete
*/
[425] Fix | Delete
private function process_payment_provider_states( array $payment_providers ): void {
[426] Fix | Delete
// Read the current state snapshots from the DB.
[427] Fix | Delete
$snapshots = get_option( self::PAYMENTS_PROVIDER_STATE_SNAPSHOTS_KEY, array() );
[428] Fix | Delete
if ( ! is_array( $snapshots ) ) {
[429] Fix | Delete
$snapshots = array();
[430] Fix | Delete
}
[431] Fix | Delete
[432] Fix | Delete
$default_snapshot = array(
[433] Fix | Delete
'extension_active' => false,
[434] Fix | Delete
'account_connected' => false,
[435] Fix | Delete
'account_test_mode' => false,
[436] Fix | Delete
'needs_setup' => false,
[437] Fix | Delete
'test_mode' => false,
[438] Fix | Delete
);
[439] Fix | Delete
[440] Fix | Delete
// Iterate through the payment providers and generate their updated snapshots.
[441] Fix | Delete
// We will use the provider's plugin slug as the key for the snapshot to ensure uniqueness.
[442] Fix | Delete
// For now, we will only focus on the provider state for official extensions, not all the gateways.
[443] Fix | Delete
$new_snapshots = array();
[444] Fix | Delete
foreach ( $payment_providers as $provider ) {
[445] Fix | Delete
if ( empty( $provider['plugin']['slug'] ) ||
[446] Fix | Delete
empty( $provider['id'] ) ||
[447] Fix | Delete
empty( $provider['state'] ) || ! is_array( $provider['state'] ) ||
[448] Fix | Delete
empty( $provider['onboarding']['state'] ) || ! is_array( $provider['onboarding']['state'] ) ||
[449] Fix | Delete
empty( $provider['_type'] ) ||
[450] Fix | Delete
PaymentsProviders::TYPE_GATEWAY !== $provider['_type'] ||
[451] Fix | Delete
empty( $provider['_suggestion_id'] )
[452] Fix | Delete
) {
[453] Fix | Delete
continue;
[454] Fix | Delete
}
[455] Fix | Delete
[456] Fix | Delete
$snapshot_key = $provider['plugin']['slug'];
[457] Fix | Delete
[458] Fix | Delete
// Since we are going after the provider general state, not that of the specific gateway,
[459] Fix | Delete
// we only need to look at the first found gateway from a given provider.
[460] Fix | Delete
if ( isset( $new_snapshots[ $snapshot_key ] ) ) {
[461] Fix | Delete
continue;
[462] Fix | Delete
}
[463] Fix | Delete
[464] Fix | Delete
// If we don't have an already existing snapshot for this provider, we create one with default values.
[465] Fix | Delete
// This way we can track changes even for the first time we see a provider.
[466] Fix | Delete
if ( ! isset( $snapshots[ $snapshot_key ] ) ) {
[467] Fix | Delete
$snapshots[ $snapshot_key ] = $default_snapshot;
[468] Fix | Delete
} else {
[469] Fix | Delete
// Make sure the old snapshot has the same keys as the default one.
[470] Fix | Delete
$snapshots[ $snapshot_key ] = array_merge( $default_snapshot, $snapshots[ $snapshot_key ] );
[471] Fix | Delete
// Remove any keys that are not in the default snapshot.
[472] Fix | Delete
$snapshot_keys = array_keys( $default_snapshot );
[473] Fix | Delete
foreach ( $snapshots[ $snapshot_key ] as $key => $v ) {
[474] Fix | Delete
if ( ! in_array( $key, $snapshot_keys, true ) ) {
[475] Fix | Delete
unset( $snapshots[ $snapshot_key ][ $key ] );
[476] Fix | Delete
}
[477] Fix | Delete
}
[478] Fix | Delete
[479] Fix | Delete
// Always sort the old snapshot by keys to ensure consistency.
[480] Fix | Delete
ksort( $snapshots[ $snapshot_key ] );
[481] Fix | Delete
}
[482] Fix | Delete
[483] Fix | Delete
// Generate the new snapshot for the provider.
[484] Fix | Delete
$new_snapshots[ $snapshot_key ] = array(
[485] Fix | Delete
'extension_active' => true, // The extension is definitely active since we have a gateway from it.
[486] Fix | Delete
'account_connected' => $provider['state']['account_connected'] ?? $default_snapshot['account_connected'],
[487] Fix | Delete
'account_test_mode' => $provider['onboarding']['state']['test_mode'] ?? $default_snapshot['account_test_mode'],
[488] Fix | Delete
'needs_setup' => $provider['state']['needs_setup'] ?? $default_snapshot['needs_setup'],
[489] Fix | Delete
'test_mode' => $provider['state']['test_mode'] ?? $default_snapshot['test_mode'],
[490] Fix | Delete
);
[491] Fix | Delete
[492] Fix | Delete
// Always sort the new snapshot by keys to ensure consistency.
[493] Fix | Delete
ksort( $new_snapshots[ $snapshot_key ] );
[494] Fix | Delete
}
[495] Fix | Delete
[496] Fix | Delete
// Provider snapshots that are not in the new snapshots but were in the old ones should be kept but marked as inactive.
[497] Fix | Delete
foreach ( $snapshots as $snapshot_key => $old_snapshot ) {
[498] Fix | Delete
if ( ! isset( $new_snapshots[ $snapshot_key ] ) ) {
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function