) { // Found the preferred provider, move it to the beginning of the array. $preferred_provider = $provider; unset( $providers[ $key ] ); array_unshift( $providers, $preferred_provider ); break; } } } return $providers; } /** * Check if a specific provider is registered and available. * * @param string $provider_id The provider ID to check. * @return bool */ public function is_provider_available( string $provider_id ): bool { foreach ( $this->providers as $provider ) { if ( $provider->id === $provider_id ) { return true; } } return false; } /** * Get the preferred provider; this is what was selected in the WooCommerce "preferred provider" setting *or* the * first registered provider if no preference was set. If the provider selected in WC Settings is not registered * anymore, it will fall back to the first registered provider. Any other case will return an empty string. * * @return string */ public function get_preferred_provider(): string { if ( $this->is_provider_available( $this->preferred_provider_option ) ) { return $this->preferred_provider_option; } // Get the first provider's ID. return $this->providers[0]->id ?? ''; } }