AddressProviderController.php
192 lines
| 1 | <?php |
| 2 | declare( strict_types=1 ); |
| 3 | namespace Automattic\WooCommerce\Internal\AddressProvider; |
| 4 | |
| 5 | use WC_Address_Provider; |
| 6 | |
| 7 | /** |
| 8 | * Service class for managing address providers. |
| 9 | */ |
| 10 | class AddressProviderController { |
| 11 | /** |
| 12 | * Registered provider instances. |
| 13 | * |
| 14 | * @var WC_Address_Provider[] |
| 15 | */ |
| 16 | private $providers = array(); |
| 17 | |
| 18 | /** |
| 19 | * Preferred provider from options. |
| 20 | * |
| 21 | * @var string ID of preferred address provider. |
| 22 | */ |
| 23 | private $preferred_provider_option = ''; |
| 24 | |
| 25 | /** |
| 26 | * Constructor. |
| 27 | * |
| 28 | * @internal |
| 29 | */ |
| 30 | public function __construct() { |
| 31 | add_action( 'init', array( $this, 'init' ) ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Init function runs after this provider was added to DI container. |
| 36 | * |
| 37 | * @internal |
| 38 | */ |
| 39 | final public function init() { |
| 40 | $this->preferred_provider_option = get_option( 'woocommerce_address_autocomplete_provider', '' ); |
| 41 | $this->providers = $this->get_registered_providers(); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Get the registered providers. |
| 46 | * |
| 47 | * @return WC_Address_Provider[] array of WC_Address_Providers. |
| 48 | */ |
| 49 | public function get_providers(): array { |
| 50 | return $this->providers; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Get all registered providers. |
| 55 | * |
| 56 | * @return WC_Address_Provider[] array of WC_Address_Providers. |
| 57 | */ |
| 58 | private function get_registered_providers(): array { |
| 59 | /** |
| 60 | * Filter the registered address providers. |
| 61 | * |
| 62 | * @since 9.9.0 |
| 63 | * @param array $providers Array of fully qualified class names (strings) or WC_Address_Provider instances. |
| 64 | * Class names will be instantiated automatically. |
| 65 | * Example: array( 'My_Provider_Class', new My_Other_Provider() ) |
| 66 | */ |
| 67 | $provider_items = apply_filters( 'woocommerce_address_providers', array() ); |
| 68 | |
| 69 | // The filter returned nothing but an empty array, so we can skip the rest of the function. |
| 70 | if ( empty( $provider_items ) && is_array( $provider_items ) ) { |
| 71 | return array(); |
| 72 | } |
| 73 | |
| 74 | $logger = wc_get_logger(); |
| 75 | |
| 76 | if ( ! is_array( $provider_items ) ) { |
| 77 | $logger->error( |
| 78 | 'Invalid return value for woocommerce_address_providers, expected an array of class names or instances.', |
| 79 | array( |
| 80 | 'context' => 'address_provider_service', |
| 81 | ) |
| 82 | ); |
| 83 | return array(); |
| 84 | } |
| 85 | |
| 86 | $providers = array(); |
| 87 | $seen_ids = array(); |
| 88 | |
| 89 | foreach ( $provider_items as $provider_item ) { |
| 90 | if ( is_string( $provider_item ) && class_exists( $provider_item ) ) { |
| 91 | $provider_item = new $provider_item(); |
| 92 | } |
| 93 | |
| 94 | // Providers need to be valid and extend WC_Address_Provider. |
| 95 | if ( ! is_a( $provider_item, WC_Address_Provider::class ) ) { |
| 96 | $logger->error( |
| 97 | sprintf( |
| 98 | 'Invalid address provider item "%s", expected a string class name or WC_Address_Provider instance.', |
| 99 | is_object( $provider_item ) ? get_class( $provider_item ) : gettype( $provider_item ) |
| 100 | ), |
| 101 | array( |
| 102 | 'context' => 'address_provider_service', |
| 103 | ) |
| 104 | ); |
| 105 | continue; |
| 106 | } |
| 107 | |
| 108 | // Validate the instance has the necessary properties. |
| 109 | if ( empty( $provider_item->id ) || empty( $provider_item->name ) ) { |
| 110 | $logger->error( |
| 111 | 'Invalid address provider instance, id or name property is missing or empty: ' . get_class( $provider_item ), |
| 112 | array( |
| 113 | 'context' => 'address_provider_service', |
| 114 | ) |
| 115 | ); |
| 116 | continue; |
| 117 | } |
| 118 | |
| 119 | // Check for duplicate IDs. |
| 120 | if ( isset( $seen_ids[ $provider_item->id ] ) ) { |
| 121 | $logger->error( |
| 122 | sprintf( |
| 123 | 'Duplicate provider ID found. ID "%s" is used by both %s and %s.', |
| 124 | $provider_item->id, |
| 125 | $seen_ids[ $provider_item->id ], |
| 126 | get_class( $provider_item ) |
| 127 | ), |
| 128 | array( |
| 129 | 'context' => 'address_provider_service', |
| 130 | ) |
| 131 | ); |
| 132 | continue; |
| 133 | } |
| 134 | |
| 135 | // Track the ID and its provider class for error reporting. |
| 136 | $seen_ids[ $provider_item->id ] = get_class( $provider_item ); |
| 137 | |
| 138 | // Add the provider instance to the array after all checks are completed. |
| 139 | $providers[] = $provider_item; |
| 140 | } |
| 141 | |
| 142 | if ( ! empty( $this->preferred_provider_option ) && ! empty( $providers ) ) { |
| 143 | // Look for the preferred provider in the array. |
| 144 | foreach ( $providers as $key => $provider ) { |
| 145 | if ( $provider->id === $this->preferred_provider_option ) { |
| 146 | // Found the preferred provider, move it to the beginning of the array. |
| 147 | $preferred_provider = $provider; |
| 148 | unset( $providers[ $key ] ); |
| 149 | array_unshift( $providers, $preferred_provider ); |
| 150 | break; |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return $providers; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Check if a specific provider is registered and available. |
| 160 | * |
| 161 | * @param string $provider_id The provider ID to check. |
| 162 | * @return bool |
| 163 | */ |
| 164 | public function is_provider_available( string $provider_id ): bool { |
| 165 | |
| 166 | foreach ( $this->providers as $provider ) { |
| 167 | if ( $provider->id === $provider_id ) { |
| 168 | return true; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Get the preferred provider; this is what was selected in the WooCommerce "preferred provider" setting *or* the |
| 177 | * first registered provider if no preference was set. If the provider selected in WC Settings is not registered |
| 178 | * anymore, it will fall back to the first registered provider. Any other case will return an empty string. |
| 179 | * |
| 180 | * @return string |
| 181 | */ |
| 182 | public function get_preferred_provider(): string { |
| 183 | |
| 184 | if ( $this->is_provider_available( $this->preferred_provider_option ) ) { |
| 185 | return $this->preferred_provider_option; |
| 186 | } |
| 187 | |
| 188 | // Get the first provider's ID. |
| 189 | return $this->providers[0]->id ?? ''; |
| 190 | } |
| 191 | } |
| 192 |