Automattic\WooCommerce\Internal\Admin\Settings\PaymentsProviders

PaymentGateway::get_containing_entity_typeprivateWC 1.0

Get the type of entity the payment gateway class is contained in.

Метод класса: PaymentGateway{}

Хуков нет.

Возвращает

Строку. The type of extension containing the payment gateway class.

Использование

// private - только в коде основоного (родительского) класса
$result = $this->get_containing_entity_type( $payment_gateway ): string;
$payment_gateway(WC_Payment_Gateway) (обязательный)
The payment gateway object.

Код PaymentGateway::get_containing_entity_type() WC 10.3.4

private function get_containing_entity_type( WC_Payment_Gateway $payment_gateway ): string {
	global $wp_plugin_paths, $wp_theme_directories;

	// If the payment gateway object has a `extension_type` property, use it.
	// This is useful for testing.
	if ( isset( $payment_gateway->extension_type ) ) {
		// Validate the extension type.
		if ( ! in_array(
			$payment_gateway->extension_type,
			array(
				PaymentsProviders::EXTENSION_TYPE_WPORG,
				PaymentsProviders::EXTENSION_TYPE_MU_PLUGIN,
				PaymentsProviders::EXTENSION_TYPE_THEME,
			),
			true
		) ) {
			return PaymentsProviders::EXTENSION_TYPE_UNKNOWN;
		}

		return $payment_gateway->extension_type;
	}

	$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 PaymentsProviders::EXTENSION_TYPE_UNKNOWN;
	}

	// Plugin paths logic closely matches the one in plugin_basename().
	// $wp_plugin_paths contains normalized paths.
	$file = wp_normalize_path( $gateway_class_filename );

	arsort( $wp_plugin_paths );
	// Account for symlinks in the plugin paths.
	foreach ( $wp_plugin_paths as $dir => $realdir ) {
		if ( str_starts_with( $file, $realdir ) ) {
			$gateway_class_filename = $dir . substr( $gateway_class_filename, strlen( $realdir ) );
		}
	}

	// Test for regular plugins.
	if ( str_starts_with( $gateway_class_filename, wp_normalize_path( WP_PLUGIN_DIR ) ) ) {
		// For now, all plugins are considered WordPress.org plugins.
		return PaymentsProviders::EXTENSION_TYPE_WPORG;
	}

	// Test for must-use plugins.
	if ( str_starts_with( $gateway_class_filename, wp_normalize_path( WPMU_PLUGIN_DIR ) ) ) {
		return PaymentsProviders::EXTENSION_TYPE_MU_PLUGIN;
	}

	// Check if it is part of a theme.
	if ( is_array( $wp_theme_directories ) ) {
		foreach ( $wp_theme_directories as $dir ) {
			// Check if the class file is in a theme directory.
			if ( str_starts_with( $gateway_class_filename, $dir ) ) {
				return PaymentsProviders::EXTENSION_TYPE_THEME;
			}
		}
	}

	// Default to an unknown type.
	return PaymentsProviders::EXTENSION_TYPE_UNKNOWN;
}