Automattic\WooCommerce\Internal\Admin\Settings\PaymentProviders

PaymentGateway::get_plugin_slugpublicWC 1.0

Get the source plugin slug of a payment gateway instance.

It accounts for both regular and must-use plugins. If the gateway is registered through a theme, it will return the theme slug.

Method of the class: PaymentGateway{}

No Hooks.

Returns

String. The plugin slug of the payment gateway. Empty string if a plugin slug could not be determined.

Usage

$PaymentGateway = new PaymentGateway();
$PaymentGateway->get_plugin_slug( $payment_gateway ): string;
$payment_gateway(WC_Payment_Gateway) (required)
The payment gateway object.

PaymentGateway::get_plugin_slug() code WC 9.9.4

public function get_plugin_slug( WC_Payment_Gateway $payment_gateway ): string {
	global $wp_theme_directories;

	// If the payment gateway object has a `plugin_slug` property, use it.
	// This is useful for testing.
	if ( isset( $payment_gateway->plugin_slug ) ) {
		return (string) $payment_gateway->plugin_slug;
	}

	$gateway_class_filename = $this->get_class_filename( $payment_gateway );
	// Bail if we couldn't get the gateway class filename.
	if ( ! is_string( $gateway_class_filename ) ) {
		return '';
	}

	$entity_type = $this->get_containing_entity_type( $payment_gateway );
	// Bail if we couldn't determine the entity type.
	if ( PaymentProviders::EXTENSION_TYPE_UNKNOWN === $entity_type ) {
		return '';
	}

	if ( PaymentProviders::EXTENSION_TYPE_THEME === $entity_type ) {
		// Find the theme directory it is part of and extract the slug.
		// This accounts for both parent and child themes.
		if ( is_array( $wp_theme_directories ) ) {
			foreach ( $wp_theme_directories as $dir ) {
				if ( str_starts_with( $gateway_class_filename, $dir ) ) {
					return $this->extract_slug_from_path( substr( $gateway_class_filename, strlen( $dir ) ) );
				}
			}
		}

		// Bail if we couldn't find a match.
		return '';
	}

	// By this point, we know that the payment gateway is part of a plugin.
	// Extract the relative path of the class file to the plugins directory.
	// We account for both regular and must-use plugins.
	$gateway_class_plugins_path = trim( plugin_basename( $gateway_class_filename ), DIRECTORY_SEPARATOR );

	return $this->extract_slug_from_path( $gateway_class_plugins_path );
}