DefaultShippingPartners.php
1 month ago
ShippingPartnerSuggestions.php
3 months ago
ShippingPartnerSuggestionsDataSourcePoller.php
1 year ago
ShippingPartnerSuggestions.php
112 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Admin\Features\ShippingPartnerSuggestions; |
| 4 | |
| 5 | use Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions\EvaluateSuggestion; |
| 6 | use Automattic\WooCommerce\Admin\RemoteSpecs\RemoteSpecsEngine; |
| 7 | |
| 8 | /** |
| 9 | * Class ShippingPartnerSuggestions |
| 10 | */ |
| 11 | class ShippingPartnerSuggestions extends RemoteSpecsEngine { |
| 12 | |
| 13 | /** |
| 14 | * Go through the specs and run them. |
| 15 | * |
| 16 | * @param array|null $specs shipping partner suggestion spec array. |
| 17 | * @return array |
| 18 | */ |
| 19 | public static function get_suggestions( ?array $specs = null ) { |
| 20 | $locale = get_user_locale(); |
| 21 | |
| 22 | $specs = is_array( $specs ) ? $specs : self::get_specs(); |
| 23 | $results = EvaluateSuggestion::evaluate_specs( $specs, array( 'source' => 'wc-shipping-partner-suggestions' ) ); |
| 24 | $specs_to_return = $results['suggestions']; |
| 25 | $specs_to_save = null; |
| 26 | |
| 27 | if ( empty( $specs_to_return ) ) { |
| 28 | // When suggestions is empty, replace it with defaults and save for 3 hours. |
| 29 | $specs_to_save = DefaultShippingPartners::get_all(); |
| 30 | $specs_to_return = EvaluateSuggestion::evaluate_specs( $specs_to_save, array( 'source' => 'wc-shipping-partner-suggestions' ) )['suggestions']; |
| 31 | } elseif ( count( $results['errors'] ) > 0 ) { |
| 32 | // When suggestions is not empty but has errors, save it for 3 hours. |
| 33 | $specs_to_save = $specs; |
| 34 | } |
| 35 | |
| 36 | if ( $specs_to_save ) { |
| 37 | ShippingPartnerSuggestionsDataSourcePoller::get_instance()->set_specs_transient( array( $locale => $specs_to_save ), 3 * HOUR_IN_SECONDS ); |
| 38 | } |
| 39 | |
| 40 | return self::sort_by_primary( $specs_to_return ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Sort suggestions so that partners whose countries_where_primary list contains the |
| 45 | * current store country appear first. |
| 46 | * |
| 47 | * @param array $suggestions Suggestions to sort. |
| 48 | * @return array Sorted suggestions. |
| 49 | */ |
| 50 | private static function sort_by_primary( array $suggestions ) { |
| 51 | $country = wc_get_base_location()['country'] ?? ''; |
| 52 | |
| 53 | // Attach original indices to preserve input order for equal-priority items |
| 54 | // (usort is not stable on PHP < 8.0). |
| 55 | $indexed = array_map( |
| 56 | function ( $item, $idx ) { |
| 57 | return array( $item, $idx ); |
| 58 | }, |
| 59 | $suggestions, |
| 60 | array_keys( $suggestions ) |
| 61 | ); |
| 62 | |
| 63 | usort( |
| 64 | $indexed, |
| 65 | function ( $a, $b ) use ( $country ) { |
| 66 | $a_primary = isset( $a[0]->countries_where_primary ) && is_array( $a[0]->countries_where_primary ) && in_array( $country, $a[0]->countries_where_primary, true ); |
| 67 | $b_primary = isset( $b[0]->countries_where_primary ) && is_array( $b[0]->countries_where_primary ) && in_array( $country, $b[0]->countries_where_primary, true ); |
| 68 | |
| 69 | if ( $a_primary === $b_primary ) { |
| 70 | return $a[1] - $b[1]; |
| 71 | } |
| 72 | |
| 73 | return $a_primary ? -1 : 1; |
| 74 | } |
| 75 | ); |
| 76 | |
| 77 | return array_column( $indexed, 0 ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Get specs or fetch remotely if they don't exist. |
| 82 | */ |
| 83 | public static function get_specs() { |
| 84 | if ( 'no' === get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) ) { |
| 85 | /** |
| 86 | * It can be used to modify shipping partner suggestions spec. |
| 87 | * |
| 88 | * @since 7.4.1 |
| 89 | */ |
| 90 | return apply_filters( 'woocommerce_admin_shipping_partner_suggestions_specs', DefaultShippingPartners::get_all() ); |
| 91 | } |
| 92 | $specs = ShippingPartnerSuggestionsDataSourcePoller::get_instance()->get_specs_from_data_sources(); |
| 93 | |
| 94 | // Fetch specs if they don't yet exist. |
| 95 | if ( false === $specs || ! is_array( $specs ) || 0 === count( $specs ) ) { |
| 96 | /** |
| 97 | * It can be used to modify shipping partner suggestions spec. |
| 98 | * |
| 99 | * @since 7.4.1 |
| 100 | */ |
| 101 | return apply_filters( 'woocommerce_admin_shipping_partner_suggestions_specs', DefaultShippingPartners::get_all() ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * It can be used to modify shipping partner suggestions spec. |
| 106 | * |
| 107 | * @since 7.4.1 |
| 108 | */ |
| 109 | return apply_filters( 'woocommerce_admin_shipping_partner_suggestions_specs', $specs ); |
| 110 | } |
| 111 | } |
| 112 |