-
Notifications
You must be signed in to change notification settings - Fork 21
/
ProductHelper.php
632 lines (556 loc) · 20 KB
/
ProductHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
<?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Product;
use Automattic\WooCommerce\GoogleListingsAndAds\Exception\InvalidValue;
use Automattic\WooCommerce\GoogleListingsAndAds\Google\GoogleProductService;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Service;
use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\TargetAudience;
use Automattic\WooCommerce\GoogleListingsAndAds\PluginHelper;
use Automattic\WooCommerce\GoogleListingsAndAds\Proxies\WC;
use Automattic\WooCommerce\GoogleListingsAndAds\Value\ChannelVisibility;
use Automattic\WooCommerce\GoogleListingsAndAds\Value\SyncStatus;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\Product as GoogleProduct;
use WC_Product;
use WC_Product_Variation;
use WP_Post;
defined( 'ABSPATH' ) || exit;
/**
* Class ProductHelper
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Product
*/
class ProductHelper implements Service {
use PluginHelper;
/**
* @var ProductMetaHandler
*/
protected $meta_handler;
/**
* @var WC
*/
protected $wc;
/**
* @var TargetAudience
*/
protected $target_audience;
/**
* ProductHelper constructor.
*
* @param ProductMetaHandler $meta_handler
* @param WC $wc
* @param TargetAudience $target_audience
*/
public function __construct( ProductMetaHandler $meta_handler, WC $wc, TargetAudience $target_audience ) {
$this->meta_handler = $meta_handler;
$this->wc = $wc;
$this->target_audience = $target_audience;
}
/**
* Mark a product as synced in the local database.
* This function also handles the following cleanup tasks:
* - Remove any failed delete attempts
* - Update the visibility (if it was previously empty)
* - Remove any previous product errors (if it was synced for all target countries)
*
* @param WC_Product $product
* @param GoogleProduct $google_product
*/
public function mark_as_synced( WC_Product $product, GoogleProduct $google_product ) {
$this->meta_handler->delete_failed_delete_attempts( $product );
$this->meta_handler->update_synced_at( $product, time() );
$this->meta_handler->update_sync_status( $product, SyncStatus::SYNCED );
$this->update_empty_visibility( $product );
// merge and update all google product ids
$current_google_ids = $this->meta_handler->get_google_ids( $product );
$current_google_ids = ! empty( $current_google_ids ) ? $current_google_ids : [];
$google_ids = array_unique( array_merge( $current_google_ids, [ $google_product->getTargetCountry() => $google_product->getId() ] ) );
$this->meta_handler->update_google_ids( $product, $google_ids );
// check if product is synced completely and remove any previous errors if it is
$synced_countries = array_keys( $google_ids );
$target_countries = $this->target_audience->get_target_countries();
if ( count( $synced_countries ) === count( $target_countries ) && empty( array_diff( $synced_countries, $target_countries ) ) ) {
$this->meta_handler->delete_errors( $product );
$this->meta_handler->delete_failed_sync_attempts( $product );
$this->meta_handler->delete_sync_failed_at( $product );
}
// mark the parent product as synced if it's a variation
if ( $product instanceof WC_Product_Variation ) {
try {
$parent_product = $this->get_wc_product( $product->get_parent_id() );
} catch ( InvalidValue $exception ) {
return;
}
$this->mark_as_synced( $parent_product, $google_product );
}
}
/**
* @param WC_Product $product
*/
public function mark_as_unsynced( WC_Product $product ) {
$this->meta_handler->delete_synced_at( $product );
if ( ! $this->is_sync_ready( $product ) ) {
$this->meta_handler->delete_sync_status( $product );
} else {
$this->meta_handler->update_sync_status( $product, SyncStatus::NOT_SYNCED );
}
$this->meta_handler->delete_google_ids( $product );
$this->meta_handler->delete_errors( $product );
$this->meta_handler->delete_failed_sync_attempts( $product );
$this->meta_handler->delete_sync_failed_at( $product );
// mark the parent product as un-synced if it's a variation
if ( $product instanceof WC_Product_Variation ) {
try {
$parent_product = $this->get_wc_product( $product->get_parent_id() );
} catch ( InvalidValue $exception ) {
return;
}
$this->mark_as_unsynced( $parent_product );
}
}
/**
* @param WC_Product $product
* @param string $google_id
*/
public function remove_google_id( WC_Product $product, string $google_id ) {
$google_ids = $this->meta_handler->get_google_ids( $product );
if ( empty( $google_ids ) ) {
return;
}
$idx = array_search( $google_id, $google_ids, true );
if ( false === $idx ) {
return;
}
unset( $google_ids[ $idx ] );
if ( ! empty( $google_ids ) ) {
$this->meta_handler->update_google_ids( $product, $google_ids );
} else {
// if there are no Google IDs left then this product is no longer considered "synced"
$this->mark_as_unsynced( $product );
}
}
/**
* Marks a WooCommerce product as invalid and stores the errors in a meta data key.
*
* Note: If a product variation is invalid then the parent product is also marked as invalid.
*
* @param WC_Product $product
* @param string[] $errors
*/
public function mark_as_invalid( WC_Product $product, array $errors ) {
// bail if no errors exist
if ( empty( $errors ) ) {
return;
}
$this->meta_handler->update_errors( $product, $errors );
$this->meta_handler->update_sync_status( $product, SyncStatus::HAS_ERRORS );
$this->update_empty_visibility( $product );
if ( ! empty( $errors[ GoogleProductService::INTERNAL_ERROR_REASON ] ) ) {
// update failed sync attempts count in case of internal errors
$failed_attempts = ! empty( $this->meta_handler->get_failed_sync_attempts( $product ) ) ?
$this->meta_handler->get_failed_sync_attempts( $product ) :
0;
$this->meta_handler->update_failed_sync_attempts( $product, $failed_attempts + 1 );
$this->meta_handler->update_sync_failed_at( $product, time() );
}
// mark the parent product as invalid if it's a variation
if ( $product instanceof WC_Product_Variation ) {
try {
$parent_product = $this->get_wc_product( $product->get_parent_id() );
} catch ( InvalidValue $exception ) {
return;
}
$parent_errors = ! empty( $this->meta_handler->get_errors( $parent_product ) ) ?
$this->meta_handler->get_errors( $parent_product ) :
[];
$parent_errors[ $product->get_id() ] = $errors;
$this->mark_as_invalid( $parent_product, $parent_errors );
}
}
/**
* Marks a WooCommerce product as pending synchronization.
*
* Note: If a product variation is pending then the parent product is also marked as pending.
*
* @param WC_Product $product
*/
public function mark_as_pending( WC_Product $product ) {
$this->meta_handler->update_sync_status( $product, SyncStatus::PENDING );
// mark the parent product as pending if it's a variation
if ( $product instanceof WC_Product_Variation ) {
try {
$parent_product = $this->get_wc_product( $product->get_parent_id() );
} catch ( InvalidValue $exception ) {
return;
}
$this->mark_as_pending( $parent_product );
}
}
/**
* Update empty (NOT EXIST) visibility meta values to SYNC_AND_SHOW.
*
* @param WC_Product $product
*/
protected function update_empty_visibility( WC_Product $product ): void {
try {
$product = $this->maybe_swap_for_parent( $product );
} catch ( InvalidValue $exception ) {
return;
}
$visibility = $this->meta_handler->get_visibility( $product );
if ( empty( $visibility ) ) {
$this->meta_handler->update_visibility( $product, ChannelVisibility::SYNC_AND_SHOW );
}
}
/**
* Update a product's channel visibility.
*
* @param WC_Product $product
* @param string $visibility
*/
public function update_channel_visibility( WC_Product $product, string $visibility ): void {
try {
$product = $this->maybe_swap_for_parent( $product );
} catch ( InvalidValue $exception ) {
// The error has been logged within the call of maybe_swap_for_parent
return;
}
try {
$visibility = ChannelVisibility::cast( $visibility )->get();
} catch ( InvalidValue $exception ) {
do_action( 'woocommerce_gla_exception', $exception, __METHOD__ );
return;
}
$this->meta_handler->update_visibility( $product, $visibility );
}
/**
* @param WC_Product $product
*
* @return string[]|null An array of Google product IDs stored for each WooCommerce product
*/
public function get_synced_google_product_ids( WC_Product $product ): ?array {
return $this->meta_handler->get_google_ids( $product );
}
/**
* See: WCProductAdapter::map_wc_product_id()
*
* @param string $mc_product_id Simple product ID (`merchant_center_id`) or
* namespaced product ID (`online:en:GB:merchant_center_id`)
*
* @return int the ID for the WC product linked to the provided Google product ID (0 if not found)
*/
public function get_wc_product_id( string $mc_product_id ): int {
// Maybe remove everything before the last colon ':'
$mc_product_id_tokens = explode( ':', $mc_product_id );
$mc_product_id = end( $mc_product_id_tokens );
$wc_product_id = 0;
$pattern = '/' . preg_quote( $this->get_slug(), '/' ) . '_(\d+)$/';
if ( preg_match( $pattern, $mc_product_id, $matches ) ) {
$wc_product_id = (int) $matches[1];
}
/**
* Filters the WooCommerce product ID that was determined to be associated with the
* given Merchant Center product ID.
*
* @param string $wc_product_id The WooCommerce product ID as determined by default.
* @param string $mc_product_id Simple Merchant Center product ID (without any prefixes).
* @since 2.4.6
*
* @return string Merchant Center product ID as normally generated by the plugin (e.g., gla_1234).
*/
return (int) apply_filters( 'woocommerce_gla_get_wc_product_id', $wc_product_id, $mc_product_id );
}
/**
* Attempt to get the WooCommerce product title.
* The MC ID is converted to a WC ID before retrieving the product.
* If we can't retrieve the title we fallback to the original MC ID.
*
* @param string $mc_product_id Merchant Center product ID.
*
* @return string
*/
public function get_wc_product_title( string $mc_product_id ): string {
try {
$product = $this->get_wc_product( $this->get_wc_product_id( $mc_product_id ) );
} catch ( InvalidValue $e ) {
return $mc_product_id;
}
return $product->get_title();
}
/**
* Get WooCommerce product
*
* @param int $product_id
*
* @return WC_Product
*
* @throws InvalidValue If the given ID doesn't reference a valid product.
*/
public function get_wc_product( int $product_id ): WC_Product {
return $this->wc->get_product( $product_id );
}
/**
* Get WooCommerce product by WP get_post
*
* @param int $product_id
*
* @return WP_Post|null
*/
public function get_wc_product_by_wp_post( int $product_id ): ?WP_Post {
return get_post( $product_id );
}
/**
* @param WC_Product $product
*
* @return bool
*/
public function is_product_synced( WC_Product $product ): bool {
$synced_at = $this->meta_handler->get_synced_at( $product );
$google_ids = $this->meta_handler->get_google_ids( $product );
return ! empty( $synced_at ) && ! empty( $google_ids );
}
/**
* @param WC_Product $product
*
* @return bool
*/
public function is_sync_ready( WC_Product $product ): bool {
$product_visibility = $product->is_visible();
$product_status = $product->get_status();
if ( $product instanceof WC_Product_Variation ) {
// Check the post status of the parent product if it's a variation
try {
$parent = $this->get_wc_product( $product->get_parent_id() );
} catch ( InvalidValue $exception ) {
do_action(
'woocommerce_gla_error',
sprintf( 'Cannot sync an orphaned variation (ID: %s).', $product->get_id() ),
__METHOD__
);
return false;
}
$product_status = $parent->get_status();
/**
* Optionally hide invisible variations (disabled variations and variations with empty price).
*
* @see WC_Product_Variable::get_available_variations for filter documentation
*/
if ( apply_filters( 'woocommerce_hide_invisible_variations', true, $parent->get_id(), $product ) && ! $product->variation_is_visible() ) {
$product_visibility = false;
}
}
return ( ChannelVisibility::DONT_SYNC_AND_SHOW !== $this->get_channel_visibility( $product ) ) &&
( in_array( $product->get_type(), ProductSyncer::get_supported_product_types(), true ) ) &&
( 'publish' === $product_status ) &&
$product_visibility;
}
/**
* Whether the sync has failed repeatedly for the product within the given timeframe.
*
* @param WC_Product $product
*
* @return bool
*
* @see ProductSyncer::FAILURE_THRESHOLD The number of failed attempts allowed per timeframe
* @see ProductSyncer::FAILURE_THRESHOLD_WINDOW The specified timeframe
*/
public function is_sync_failed_recently( WC_Product $product ): bool {
$failed_attempts = $this->meta_handler->get_failed_sync_attempts( $product );
$failed_at = $this->meta_handler->get_sync_failed_at( $product );
// if it has failed more times than the specified threshold AND if syncing it has failed within the specified window
return $failed_attempts > ProductSyncer::FAILURE_THRESHOLD &&
$failed_at > strtotime( sprintf( '-%s', ProductSyncer::FAILURE_THRESHOLD_WINDOW ) );
}
/**
* Increment failed delete attempts.
*
* @since 1.12.0
*
* @param WC_Product $product
*/
public function increment_failed_delete_attempt( WC_Product $product ) {
$failed_attempts = $this->meta_handler->get_failed_delete_attempts( $product ) ?? 0;
$this->meta_handler->update_failed_delete_attempts( $product, $failed_attempts + 1 );
}
/**
* Whether deleting has failed more times than the specified threshold.
*
* @since 1.12.0
*
* @param WC_Product $product
*
* @return boolean
*/
public function is_delete_failed_threshold_reached( WC_Product $product ): bool {
$failed_attempts = $this->meta_handler->get_failed_delete_attempts( $product ) ?? 0;
return $failed_attempts >= ProductSyncer::FAILURE_THRESHOLD;
}
/**
* Increment failed delete attempts.
*
* @since 1.12.2
*
* @param WC_Product $product
*/
public function increment_failed_update_attempt( WC_Product $product ) {
$failed_attempts = $this->meta_handler->get_failed_sync_attempts( $product ) ?? 0;
$this->meta_handler->update_failed_sync_attempts( $product, $failed_attempts + 1 );
}
/**
* Whether deleting has failed more times than the specified threshold.
*
* @since 1.12.2
*
* @param WC_Product $product
*
* @return boolean
*/
public function is_update_failed_threshold_reached( WC_Product $product ): bool {
$failed_attempts = $this->meta_handler->get_failed_sync_attempts( $product ) ?? 0;
return $failed_attempts >= ProductSyncer::FAILURE_THRESHOLD;
}
/**
* @param WC_Product $wc_product
*
* @return string|null
*/
public function get_channel_visibility( WC_Product $wc_product ): ?string {
try {
// todo: we might need to define visibility per variation later.
return $this->meta_handler->get_visibility( $this->maybe_swap_for_parent( $wc_product ) );
} catch ( InvalidValue $exception ) {
do_action(
'woocommerce_gla_debug_message',
sprintf( 'Channel visibility forced to "%s" for invalid product (ID: %s).', ChannelVisibility::DONT_SYNC_AND_SHOW, $wc_product->get_id() ),
__METHOD__
);
return ChannelVisibility::DONT_SYNC_AND_SHOW;
}
}
/**
* Return a string indicating sync status based on several factors.
*
* @param WC_Product $wc_product
*
* @return string|null
*/
public function get_sync_status( WC_Product $wc_product ): ?string {
return $this->meta_handler->get_sync_status( $wc_product );
}
/**
* Return the string indicating the product status as reported by the Merchant Center.
*
* @param WC_Product $wc_product
*
* @return string|null
*/
public function get_mc_status( WC_Product $wc_product ): ?string {
try {
return $this->meta_handler->get_mc_status( $this->maybe_swap_for_parent( $wc_product ) );
} catch ( InvalidValue $exception ) {
do_action(
'woocommerce_gla_debug_message',
sprintf( 'Product status returned null for invalid product (ID: %s).', $wc_product->get_id() ),
__METHOD__
);
return null;
}
}
/**
* If an item from the provided list of products has a parent, replace it with the parent ID.
*
* @param int[] $product_ids A list of WooCommerce product ID.
* @param bool $check_product_status (Optional) Check if the product status is publish.
* @param bool $ignore_product_on_error (Optional) Ignore the product when invalid value error occurs.
*
* @return int[] A list of parent ID or product ID if it doesn't have a parent.
*
* @throws InvalidValue If the given param ignore_product_on_error is false and any of a given ID doesn't reference a valid product.
* Or if a variation product does not have a valid parent ID (i.e. it's an orphan).
*
* @since 2.2.0
*/
public function maybe_swap_for_parent_ids( array $product_ids, bool $check_product_status = true, bool $ignore_product_on_error = true ) {
$new_product_ids = [];
foreach ( $product_ids as $index => $product_id ) {
try {
$product = $this->get_wc_product( $product_id );
$new_product = $this->maybe_swap_for_parent( $product );
if ( ! $check_product_status || 'publish' === $new_product->get_status() ) {
$new_product_ids[ $index ] = $new_product->get_id();
}
} catch ( InvalidValue $exception ) {
if ( ! $ignore_product_on_error ) {
throw $exception;
}
}
}
return array_unique( $new_product_ids );
}
/**
* If the provided product has a parent, return its ID. Otherwise, return the given (valid product) ID.
*
* @param int $product_id WooCommerce product ID.
*
* @return int The parent ID or product ID if it doesn't have a parent.
*
* @throws InvalidValue If a given ID doesn't reference a valid product. Or if a variation product does not have a
* valid parent ID (i.e. it's an orphan).
*/
public function maybe_swap_for_parent_id( int $product_id ): int {
$product = $this->get_wc_product( $product_id );
return $this->maybe_swap_for_parent( $product )->get_id();
}
/**
* If the provided product has a parent, return its parent object. Otherwise, return the given product.
*
* @param WC_Product $product WooCommerce product object.
*
* @return WC_Product The parent product object or the given product object if it doesn't have a parent.
*
* @throws InvalidValue If a variation product does not have a valid parent ID (i.e. it's an orphan).
*
* @since 1.3.0
*/
public function maybe_swap_for_parent( WC_Product $product ): WC_Product {
if ( $product instanceof WC_Product_Variation ) {
try {
return $this->get_wc_product( $product->get_parent_id() );
} catch ( InvalidValue $exception ) {
do_action(
'woocommerce_gla_error',
sprintf( 'An orphaned variation found (ID: %s). Please delete it via "WooCommerce > Status > Tools > Delete orphaned variations".', $product->get_id() ),
__METHOD__
);
throw $exception;
}
}
return $product;
}
/**
* Get validation errors for a specific product.
* Combines errors for variable products, which have a variation-indexed array of errors.
*
* @param WC_Product $product
*
* @return array
*/
public function get_validation_errors( WC_Product $product ): array {
$errors = $this->meta_handler->get_errors( $product ) ?: [];
$first_key = array_key_first( $errors );
if ( ! empty( $errors ) && is_numeric( $first_key ) && 0 !== $first_key ) {
$errors = array_unique( array_merge( ...$errors ) );
}
return $errors;
}
/**
* Get categories list for a specific product.
*
* @param WC_Product $product
*
* @return array
*/
public function get_categories( WC_Product $product ): array {
$terms = get_the_terms( $product->get_id(), 'product_cat' );
return ( empty( $terms ) || is_wp_error( $terms ) ) ? [] : wp_list_pluck( $terms, 'name' );
}
}