/wp-content/plugins/woocommerce/packages/woocommerce-admin/src/Features/ShippingLabelBannerDisplayRules.php

https://gitlab.com/campus-academy/krowkaramel · PHP · 208 lines · 80 code · 30 blank · 98 comment · 11 complexity · 13c8d2a681b264296cd20acc603f97ad MD5 · raw file

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