Automattic\WooCommerce\Internal\Admin\Settings
PaymentsProviders::remove_shell_payment_gateways
Remove "shell" gateways from the provided payment gateways list.
We consider a gateway to be a "shell" if it has no WC admin title or description. The removal is done in a way that ensures we do not remove all gateways from an extension, thus preventing user access to the settings page(s) for that extension.
Method of the class: PaymentsProviders{}
No Hooks.
Returns
Array. The processed payment gateways list.
Usage
$PaymentsProviders = new PaymentsProviders(); $PaymentsProviders->remove_shell_payment_gateways( $payment_gateways, $country_code ): array;
- $payment_gateways(array) (required)
- The payment gateways list to process.
- $country_code(string)
- The country code for which the payment gateways are being generated. This should be an ISO 3166-1 alpha-2 country code.
Default:''
PaymentsProviders::remove_shell_payment_gateways() PaymentsProviders::remove shell payment gateways code WC 10.7.0
public function remove_shell_payment_gateways( array $payment_gateways, string $country_code = '' ): array {
// Normalize the country code to uppercase.
$country_code = strtoupper( $country_code );
$grouped_payment_gateways = $this->group_gateways_by_extension( $payment_gateways, $country_code );
return array_filter(
$payment_gateways,
function ( $gateway ) use ( $grouped_payment_gateways, $country_code ) {
// If the gateway is a shell, we only remove it if there are other, non-shell gateways from that extension.
// This is to avoid removing all the gateways registered by an extension and
// preventing user access to the settings page(s) for that extension.
if ( $this->is_shell_payment_gateway( $gateway ) ) {
$gateway_details = $this->get_payment_gateway_details( $gateway, 0, $country_code );
// In case we don't have the needed extension details,
// we allow the gateway to be displayed (aka better safe than sorry).
if ( empty( $gateway_details ) || ! isset( $gateway_details['plugin'] ) || empty( $gateway_details['plugin']['file'] ) ) {
return true;
}
if ( empty( $grouped_payment_gateways[ $gateway_details['plugin']['file'] ] ) ||
count( $grouped_payment_gateways[ $gateway_details['plugin']['file'] ] ) <= 1 ) {
// If there are no other gateways from the same extension, we let the shell gateway be displayed.
return true;
}
// Check if there are any other gateways from the same extension that are NOT shells.
foreach ( $grouped_payment_gateways[ $gateway_details['plugin']['file'] ] as $extension_gateway ) {
if ( ! $this->is_shell_payment_gateway( $extension_gateway ) ) {
// If we found a gateway from the same extension that is not a shell,
// we hide all shells from that extension.
return false;
}
}
}
// By this point, we know that the gateway is not a shell or that it is a shell
// but there are no non-shell gateways from the same extension. Include it.
return true;
}
);
}