Navigation
5 years ago
ActivityPanels.php
5 years ago
Analytics.php
5 years ago
AnalyticsDashboard.php
5 years ago
Coupons.php
5 years ago
CouponsMovedTrait.php
5 years ago
CustomerEffortScoreTracks.php
5 years ago
Features.php
5 years ago
Homescreen.php
5 years ago
Marketing.php
5 years ago
MobileAppBanner.php
5 years ago
Onboarding.php
5 years ago
OnboardingTasks.php
5 years ago
Settings.php
5 years ago
ShippingLabelBanner.php
5 years ago
ShippingLabelBannerDisplayRules.php
5 years ago
ShippingLabelBannerDisplayRules.php
209 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Shipping Label Banner Display Rules. |
| 4 | * NOTE: DO NOT edit this file in WooCommerce core, this is generated from woocommerce-admin. |
| 5 | */ |
| 6 | |
| 7 | namespace Automattic\WooCommerce\Admin\Features; |
| 8 | |
| 9 | /** |
| 10 | * Determines whether or not the Shipping Label Banner should be displayed |
| 11 | */ |
| 12 | class ShippingLabelBannerDisplayRules { |
| 13 | |
| 14 | /** |
| 15 | * Holds the installed Jetpack version. |
| 16 | * |
| 17 | * @var string |
| 18 | */ |
| 19 | private $jetpack_version; |
| 20 | |
| 21 | |
| 22 | /** |
| 23 | * Whether or not the installed Jetpack is connected. |
| 24 | * |
| 25 | * @var bool |
| 26 | */ |
| 27 | private $jetpack_connected; |
| 28 | |
| 29 | /** |
| 30 | * Holds the installed WooCommerce Shipping & Tax version. |
| 31 | * |
| 32 | * @var string |
| 33 | */ |
| 34 | private $wcs_version; |
| 35 | |
| 36 | /** |
| 37 | * Whether or not there're plugins installed incompatible with the banner. |
| 38 | * |
| 39 | * @var bool |
| 40 | */ |
| 41 | private $no_incompatible_plugins_installed; |
| 42 | |
| 43 | /** |
| 44 | * Whether or not the WooCommerce Shipping & Tax ToS has been accepted. |
| 45 | * |
| 46 | * @var bool |
| 47 | */ |
| 48 | private $wcs_tos_accepted; |
| 49 | |
| 50 | /** |
| 51 | * Minimum supported Jetpack version. |
| 52 | * |
| 53 | * @var string |
| 54 | */ |
| 55 | private $min_jetpack_version = '4.4'; |
| 56 | |
| 57 | /** |
| 58 | * Minimum supported WooCommerce Shipping & Tax version. |
| 59 | * |
| 60 | * @var string |
| 61 | */ |
| 62 | private $min_wcs_version = '1.22.5'; |
| 63 | |
| 64 | /** |
| 65 | * Supported countries by USPS, see: https://webpmt.usps.gov/pmt010.cfm |
| 66 | * |
| 67 | * @var array |
| 68 | */ |
| 69 | private $supported_countries = array( 'US', 'AS', 'PR', 'VI', 'GU', 'MP', 'UM', 'FM', 'MH' ); |
| 70 | |
| 71 | /** |
| 72 | * Array of supported currency codes. |
| 73 | * |
| 74 | * @var array |
| 75 | */ |
| 76 | private $supported_currencies = array( 'USD' ); |
| 77 | |
| 78 | |
| 79 | /** |
| 80 | * Constructor. |
| 81 | * |
| 82 | * @param string $jetpack_version Installed Jetpack version to check. |
| 83 | * @param bool $jetpack_connected Is Jetpack connected?. |
| 84 | * @param string $wcs_version Installed WooCommerce Shipping & Tax version to check. |
| 85 | * @param bool $wcs_tos_accepted WooCommerce Shipping & Tax Terms of Service accepted?. |
| 86 | * @param bool $incompatible_plugins_installed Are there any incompatible plugins installed?. |
| 87 | */ |
| 88 | public function __construct( $jetpack_version, $jetpack_connected, $wcs_version, $wcs_tos_accepted, $incompatible_plugins_installed ) { |
| 89 | $this->jetpack_version = $jetpack_version; |
| 90 | $this->jetpack_connected = $jetpack_connected; |
| 91 | $this->wcs_version = $wcs_version; |
| 92 | $this->wcs_tos_accepted = $wcs_tos_accepted; |
| 93 | $this->no_incompatible_plugins_installed = ! $incompatible_plugins_installed; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Determines whether banner is eligible for display (does not include a/b logic). |
| 98 | */ |
| 99 | public function should_display_banner() { |
| 100 | return $this->banner_not_dismissed() && |
| 101 | $this->jetpack_installed_and_active() && |
| 102 | $this->jetpack_up_to_date() && |
| 103 | $this->jetpack_connected && |
| 104 | $this->no_incompatible_plugins_installed && |
| 105 | $this->order_has_shippable_products() && |
| 106 | $this->store_in_us_and_usd() && |
| 107 | ( $this->wcs_not_installed() || ( |
| 108 | $this->wcs_up_to_date() && ! $this->wcs_tos_accepted |
| 109 | ) ); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Checks if the banner was not dismissed by the user. |
| 114 | * |
| 115 | * @return bool |
| 116 | */ |
| 117 | private function banner_not_dismissed() { |
| 118 | $dismissed_timestamp_ms = get_option( 'woocommerce_shipping_dismissed_timestamp' ); |
| 119 | |
| 120 | if ( ! is_numeric( $dismissed_timestamp_ms ) ) { |
| 121 | return true; |
| 122 | } |
| 123 | $dismissed_timestamp_ms = intval( $dismissed_timestamp_ms ); |
| 124 | $dismissed_timestamp = intval( round( $dismissed_timestamp_ms / 1000 ) ); |
| 125 | $expired_timestamp = $dismissed_timestamp + 24 * 60 * 60; // 24 hours from click time |
| 126 | |
| 127 | $dismissed_for_good = -1 === $dismissed_timestamp_ms; |
| 128 | $dismissed_24h = time() < $expired_timestamp; |
| 129 | |
| 130 | return ! $dismissed_for_good && ! $dismissed_24h; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Checks if jetpack is installed and active. |
| 135 | * |
| 136 | * @return bool |
| 137 | */ |
| 138 | private function jetpack_installed_and_active() { |
| 139 | return ! ! $this->jetpack_version; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Checks if Jetpack version is supported. |
| 144 | * |
| 145 | * @return bool |
| 146 | */ |
| 147 | private function jetpack_up_to_date() { |
| 148 | return version_compare( $this->jetpack_version, $this->min_jetpack_version, '>=' ); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Checks if there's a shippable product in the current order. |
| 153 | * |
| 154 | * @return bool |
| 155 | */ |
| 156 | private function order_has_shippable_products() { |
| 157 | $post = get_post(); |
| 158 | if ( ! $post ) { |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | $order = wc_get_order( get_post()->ID ); |
| 163 | |
| 164 | if ( ! $order ) { |
| 165 | return false; |
| 166 | } |
| 167 | // At this point (no packaging data), only show if there's at least one existing and shippable product. |
| 168 | foreach ( $order->get_items() as $item ) { |
| 169 | if ( $item instanceof \WC_Order_Item_Product ) { |
| 170 | $product = $item->get_product(); |
| 171 | |
| 172 | if ( $product && $product->needs_shipping() ) { |
| 173 | return true; |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Checks if the store is in the US and has its default currency set to USD. |
| 183 | * |
| 184 | * @return bool |
| 185 | */ |
| 186 | private function store_in_us_and_usd() { |
| 187 | $base_currency = get_woocommerce_currency(); |
| 188 | $base_location = wc_get_base_location(); |
| 189 | |
| 190 | return in_array( $base_currency, $this->supported_currencies, true ) && in_array( $base_location['country'], $this->supported_countries, true ); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Checks if WooCommerce Shipping & Tax is not installed. |
| 195 | * |
| 196 | * @return bool |
| 197 | */ |
| 198 | private function wcs_not_installed() { |
| 199 | return ! $this->wcs_version; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Checks if WooCommerce Shipping & Tax is up to date. |
| 204 | */ |
| 205 | private function wcs_up_to_date() { |
| 206 | return $this->wcs_version && version_compare( $this->wcs_version, $this->min_wcs_version, '>=' ); |
| 207 | } |
| 208 | } |
| 209 |