PluginProbe ʕ •ᴥ•ʔ
Weight Based Shipping Table Rate for WooCommerce – Flexible Shipping / 6.11.0
Weight Based Shipping Table Rate for WooCommerce – Flexible Shipping v6.11.0
6.12.1 6.12.0 6.11.0 6.10.0 6.9.1 6.9.0 6.8.1 6.8.0 6.7.4 6.7.3 6.7.2 6.7.0 4.21.7 4.22.0 4.22.1 4.23.0 4.23.1 4.23.2 4.23.3 4.24.0 4.24.1 4.24.10 4.24.11 4.24.12 4.24.13 4.24.14 4.24.15 4.24.16 4.24.17 4.24.18 4.24.19 4.24.2 4.24.4 4.24.6 4.24.7 4.24.8 4.24.9 4.25.0 4.25.1 4.25.2 4.25.4 4.25.5 4.25.6 4.25.7 4.25.8 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.5.1 4.6.0 4.6.1 4.7.0 4.7.1 4.7.2 4.8.0 4.8.1 4.8.3 4.8.4 4.8.5 4.9.0 5.0.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.0.9 5.1.0 5.1.1 5.1.2 5.1.3 5.2.0 5.3.0 5.3.1 5.3.3 5.3.4 5.3.5 6.0.0 6.1.0 6.1.1 6.1.2 6.2.0 6.2.1 6.2.2 6.2.3 6.2.4 6.3.0 trunk 6.4.0 1.4 6.4.1 1.4.1 6.4.2 1.5 6.4.3 1.6 6.4.5 1.6.1 6.5.0 1.6.2 6.5.1 1.7 6.5.2 1.8 6.5.3 1.9 6.5.4 1.9.1 6.5.5 1.9.10 6.5.6 1.9.11 6.5.7 1.9.12 6.5.8 1.9.2 6.6.0 1.9.3 6.6.1 1.9.4 6.6.2 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0 2.0.1 2.1 2.1.1 2.1.10 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.6 3.0.7 3.1 3.1.1 3.1.10 3.1.11 3.1.12 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.10.0 3.11.0 3.11.1 3.11.2 3.11.3 3.11.4 3.12.0 3.13.0 3.14.0 3.14.1 3.14.2 3.14.3 3.14.4 3.15.0 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.2.0 3.3.1 3.3.10 3.3.11 3.3.12 3.3.13 3.3.14 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9 3.4.0 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.7.0 3.8.0 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.15 3.9.16 3.9.17 3.9.18 3.9.19 3.9.2 3.9.20 3.9.21 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.9 4.0.0 4.0.1 4.0.10 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.0.8 4.0.9 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.10.0 4.10.1 4.11.0 4.11.1 4.11.2 4.11.3 4.11.4 4.11.5 4.11.6 4.11.7 4.11.8 4.11.9 4.12.0 4.13.0 4.13.1 4.13.2 4.13.3 4.14.0 4.14.1 4.14.2 4.15.0 4.16.0 4.16.1 4.16.2 4.17.0 4.18.0 4.18.1 4.18.2 4.18.3 4.18.4 4.18.5 4.18.6 4.19.0 4.19.1 4.2.0 4.20.0 4.20.1 4.20.2 4.20.3 4.21.0 4.21.1 4.21.2 4.21.3 4.21.4 4.21.5
flexible-shipping / src / WPDesk / FS / TableRate / Debug / MultipleShippingZonesMatchedSameTerritoryNotice.php
flexible-shipping / src / WPDesk / FS / TableRate / Debug Last commit date
DebugTracker.php 2 weeks ago MultipleShippingZonesMatchedSameTerritoryNotice.php 2 weeks ago MultipleShippingZonesMatchedSameTerritoryTracker.php 2 weeks ago NoShippingMethodsNotice.php 2 weeks ago
MultipleShippingZonesMatchedSameTerritoryNotice.php
274 lines
1 <?php
2 /**
3 * Class MatchedShippingZonesNotice
4 *
5 * @package WPDesk\FS\TableRate\Debug
6 */
7
8 namespace WPDesk\FS\TableRate\Debug;
9
10 use FSVendor\WPDesk\Notice\Notice;
11 use FSVendor\WPDesk\PluginBuilder\Plugin\Hookable;
12
13 /**
14 * Can display notice if multiple shipping zones covers same territory.
15 */
16 class MultipleShippingZonesMatchedSameTerritoryNotice implements Hookable {
17
18 /**
19 * @var \WC_Countries
20 */
21 private $countries;
22
23 /**
24 * @var \WC_Shipping_Zones
25 */
26 private $shipping_zones;
27
28 /**
29 * MatchedShippingZonesNotice constructor.
30 *
31 * @param \WC_Countries $countries .
32 * @param \WC_Shipping_Zones $shipping_zones .
33 */
34 public function __construct( \WC_Countries $countries, \WC_Shipping_Zones $shipping_zones ) {
35 $this->countries = $countries;
36 $this->shipping_zones = $shipping_zones;
37 }
38
39 /**
40 * Hooks.
41 */
42 public function hooks() {
43 add_action( 'admin_notices', array( $this, 'add_notice_when_multiple_zones_matches_same_territory' ) );
44 }
45
46 /**
47 * .
48 *
49 * @return bool|Notice
50 * @internal
51 */
52 public function add_notice_when_multiple_zones_matches_same_territory() {
53 if ( $this->should_display_notice() ) {
54 $shipping_zones_dependencies = $this->prepare_shipping_zones_dependencies();
55 if ( count( $shipping_zones_dependencies ) ) {
56 $zones_message = $this->prepare_zone_messages( $shipping_zones_dependencies );
57 $notice_text = sprintf(
58 // Translators: zones.
59 __( '%1$sFlexible Shipping hints%2$sA potential shipping zone configuration conflict has been detected: %3$s In order to fix it, change the shipping zones order starting from the narrowest at the very top of the list to the widest at the bottom and refresh the page.', 'flexible-shipping' ),
60 '<h4>',
61 '</h4>',
62 $zones_message
63 );
64 $notice = new Notice(
65 $notice_text,
66 Notice::NOTICE_TYPE_ERROR,
67 false,
68 10,
69 array( 'class' => 'flexible-shipping-hint' )
70 );
71
72 $matched_shipping_zones_notice = $this;
73 /**
74 * Do action after multiple zones matches same territory notice created.
75 *
76 * @param MultipleShippingZonesMatchedSameTerritoryNotice $matched_shipping_zones_notice .
77 */
78 do_action( 'flexible-shipping/notice/multiple-zone-matches-same-territory', $matched_shipping_zones_notice );
79
80 return $notice;
81 }
82 }
83
84 return false;
85 }
86
87 /**
88 * .
89 *
90 * @return bool
91 */
92 private function should_display_notice() {
93 return isset( $_GET['page'] ) && sanitize_key( $_GET['page'] ) === 'wc-settings'
94 && isset( $_GET['tab'] ) && sanitize_key( $_GET['tab'] ) === 'shipping'
95 && empty( $_GET['section'] ) && empty( $_GET['zone_id'] );
96 }
97
98 /**
99 * @param array $dependencies .
100 *
101 * @return string
102 */
103 private function prepare_zone_messages( $dependencies ) {
104 $messages = '';
105 foreach ( $dependencies as $dependency ) {
106 if ( '' === $messages ) {
107 $messages .= '<ul style="list-style-type: disc;">';
108 }
109 $zones = '';
110 /** @var \WC_Shipping_Zone $zone */
111 foreach ( $dependency['covered'] as $zone ) {
112 $zones .= $zone->get_zone_name() . ', ';
113 }
114 $zones = trim( $zones, ', ' );
115 $messages .= sprintf(
116 // Translators: zone messages.
117 __( '%1$sWider %2$s shipping zone covers the range of the narrower one placed below: %3$s.%4$s', 'flexible-shipping' ),
118 '<li style="margin-left: 30px;">',
119 '<strong>' . $dependency['zone']->get_zone_name() . '</strong>',
120 '<strong>' . $zones . '</strong>',
121 '</li>'
122 );
123 }
124 if ( '' !== $messages ) {
125 $messages .= '</ul>';
126 }
127
128 return $messages;
129 }
130
131 /**
132 * .
133 *
134 * @return array
135 */
136 private function prepare_shipping_zones_dependencies() {
137 $dependencies = array();
138 $zones = $this->get_zones();
139 foreach ( $zones as $zone_id => $zone_data ) {
140 unset( $zones[ $zone_id ] );
141 $zone = $this->get_zone( $zone_id );
142 if ( ! $this->zone_contains_postcodes( $zone ) ) {
143 $zone_dependencies = $this->prepare_covered_zones( $zone, $zones );
144 if ( $zone_dependencies ) {
145 $dependencies[ $zone_id ] = array(
146 'zone' => $zone,
147 'covered' => $zone_dependencies,
148 );
149 }
150 }
151 }
152
153 return $dependencies;
154 }
155
156 /**
157 * @return array
158 *
159 * @codeCoverageIgnore
160 */
161 protected function get_zones() {
162 return $this->shipping_zones::get_zones();
163 }
164
165 /**
166 * @param int $zone_id .
167 *
168 * @return bool|\WC_Shipping_Zone
169 *
170 * @codeCoverageIgnore
171 */
172 protected function get_zone( $zone_id ) {
173 return $this->shipping_zones::get_zone( $zone_id );
174 }
175
176 /**
177 * @param \WC_Shipping_Zone $zone .
178 * @param array $zones .
179 *
180 * @return array
181 */
182 private function prepare_covered_zones( \WC_Shipping_Zone $zone, array $zones ) {
183 $zone_dependencies = array();
184 $locations = $zone->get_zone_locations();
185 foreach ( $locations as $location ) {
186 foreach ( $zones as $zone_id => $zone_data ) {
187 $zone = $this->get_zone( $zone_id );
188 if ( $this->location_covers_zone( $location, $zone ) ) {
189 $zone_dependencies[] = $zone;
190 }
191 }
192 }
193
194 return $zone_dependencies;
195 }
196
197 /**
198 * @param \WC_Shipping_Zone $zone .
199 *
200 * @return bool
201 */
202 private function zone_contains_postcodes( \WC_Shipping_Zone $zone ) {
203 foreach ( $zone->get_zone_locations() as $location ) {
204 if ( 'postcode' === $location->type ) {
205 return true;
206 }
207 }
208
209 return false;
210 }
211
212 /**
213 * @param \stdClass $location .
214 * @param \WC_Shipping_Zone $zone .
215 *
216 * @return bool
217 */
218 private function location_covers_zone( \stdClass $location, \WC_Shipping_Zone $zone ) {
219 $covers = false;
220 foreach ( $zone->get_zone_locations() as $zone_location ) {
221 $covers = $covers || $this->location_covers_location( $location, $zone_location );
222 }
223
224 return $covers;
225 }
226
227 /**
228 * @param \stdClass $location .
229 * @param \stdClass $zone_location .
230 *
231 * @return bool
232 */
233 private function location_covers_location( \stdClass $location, \stdClass $zone_location ) {
234 $zone_continent = '';
235 $zone_country = '';
236 $zone_state = '';
237 if ( 'continent' === $zone_location->type ) {
238 $zone_continent = $zone_location->code;
239 }
240 if ( 'country' === $zone_location->type ) {
241 $zone_country = $zone_location->code;
242 $zone_continent = $this->get_continent_code_for_country( $zone_country );
243 }
244 if ( 'state' === $zone_location->type ) {
245 $country_state = $zone_location->code;
246 $country_state_exploded = explode( ':', $country_state );
247 $zone_country = $country_state_exploded[0];
248 $zone_state = $country_state_exploded[1];
249 $zone_continent = $this->get_continent_code_for_country( $zone_country );
250 }
251 if ( 'continent' === $location->type && $location->code === $zone_continent ) {
252 return true;
253 }
254 if ( 'country' === $location->type && $location->code === $zone_country ) {
255 return true;
256 }
257 if ( 'state' === $location->type && $location->code === $zone_state ) {
258 return true;
259 }
260 }
261
262 /**
263 * @param string $country_code .
264 *
265 * @return string
266 *
267 * @codeCoverageIgnore
268 */
269 protected function get_continent_code_for_country( $country_code ) {
270 return $this->countries->get_continent_code_for_country( $country_code );
271 }
272
273 }
274