-
Notifications
You must be signed in to change notification settings - Fork 891
Expand file tree
/
Copy pathclass-woocommerce.php
More file actions
1371 lines (1247 loc) · 47 KB
/
Copy pathclass-woocommerce.php
File metadata and controls
1371 lines (1247 loc) · 47 KB
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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* WooCommerce sync module.
*
* @package automattic/jetpack-sync
*/
namespace Automattic\Jetpack\Sync\Modules;
use WC_Order;
use WP_Error;
if ( ! defined( 'ABSPATH' ) ) {
exit( 0 );
}
/**
* Class to handle sync for WooCommerce.
*/
class WooCommerce extends Module {
/**
* Whitelist for order item meta we are interested to sync.
*
* @access private
*
* @var array
*/
public static $order_item_meta_whitelist = array(
// See https://github.com/woocommerce/woocommerce/blob/master/includes/data-stores/class-wc-order-item-product-store.php#L20 .
'_product_id',
'_variation_id',
'_qty',
// Tax ones also included in below class
// See https://github.com/woocommerce/woocommerce/blob/master/includes/data-stores/class-wc-order-item-fee-data-store.php#L20 .
'_tax_class',
'_tax_status',
'_line_subtotal',
'_line_subtotal_tax',
'_line_total',
'_line_tax',
'_line_tax_data',
// See https://github.com/woocommerce/woocommerce/blob/master/includes/data-stores/class-wc-order-item-shipping-data-store.php#L20 .
'method_id',
'cost',
'total_tax',
'taxes',
// See https://github.com/woocommerce/woocommerce/blob/master/includes/data-stores/class-wc-order-item-tax-data-store.php#L20 .
'rate_id',
'label',
'compound',
'tax_amount',
'shipping_tax_amount',
// See https://github.com/woocommerce/woocommerce/blob/master/includes/data-stores/class-wc-order-item-coupon-data-store.php .
'discount_amount',
'discount_amount_tax',
);
/**
* Mapping between WooCommerce customer detail user meta keys and customer prop names.
*
* @access private
*
* @var array
*/
private static $customer_detail_meta_key_to_prop = array(
'paying_customer' => 'is_paying_customer',
'billing_first_name' => 'billing_first_name',
'billing_last_name' => 'billing_last_name',
'billing_company' => 'billing_company',
'billing_address_1' => 'billing_address_1',
'billing_address_2' => 'billing_address_2',
'billing_city' => 'billing_city',
'billing_state' => 'billing_state',
'billing_postcode' => 'billing_postcode',
'billing_country' => 'billing_country',
'billing_email' => 'billing_email',
'billing_phone' => 'billing_phone',
'shipping_first_name' => 'shipping_first_name',
'shipping_last_name' => 'shipping_last_name',
'shipping_company' => 'shipping_company',
'shipping_address_1' => 'shipping_address_1',
'shipping_address_2' => 'shipping_address_2',
'shipping_city' => 'shipping_city',
'shipping_state' => 'shipping_state',
'shipping_postcode' => 'shipping_postcode',
'shipping_country' => 'shipping_country',
'shipping_phone' => 'shipping_phone',
);
/**
* Name of the order item database table.
*
* @access private
*
* @var string
*/
private $order_item_table_name;
/**
* Customer detail meta changes to sync at the end of the request.
*
* @var array
*/
private $customer_meta_updates = array();
/**
* User IDs deleted during the current request.
*
* @var array
*/
private $deleted_user_ids = array();
/**
* Order IDs whose total we've already emitted this request, so an order's total is emitted once
* even if both woocommerce_new_order and woocommerce_order_status_changed observe it.
*
* @var array
*/
private $synced_order_total_keys = array();
/**
* Cached list of order statuses WooCommerce considers paid, memoized per request to avoid
* re-running wc_get_is_paid_statuses() (and its filters) on every order this request observes.
*
* @var array|null
*/
private $paid_order_statuses = null;
/**
* The table name.
*
* @access public
*
* @return string
* @deprecated since 3.11.0 Use table() instead.
*/
public function table_name() {
_deprecated_function( __METHOD__, '3.11.0', 'Automattic\\Jetpack\\Sync\\WooCommerce->table' );
return $this->order_item_table_name;
}
/**
* The table in the database with the prefix.
*
* @access public
*
* @return string|bool
*/
public function table() {
global $wpdb;
return $wpdb->prefix . 'woocommerce_order_items';
}
/**
* The id field in the database.
*
* @access public
*
* @return string
*/
public function id_field() {
return 'order_item_id';
}
/**
* The full sync action name for this module.
*
* @access public
*
* @return string
*/
public function full_sync_action_name() {
return 'jetpack_full_sync_woocommerce_order_items';
}
/**
* Constructor.
*
* @global $wpdb
*
* @todo Should we refactor this to use $this->set_defaults() instead?
*/
public function __construct() {
global $wpdb;
$this->order_item_table_name = $wpdb->prefix . 'woocommerce_order_items';
// Options, constants and post meta whitelists.
add_filter( 'jetpack_sync_options_whitelist', array( $this, 'add_woocommerce_options_whitelist' ), 10 );
add_filter( 'jetpack_sync_constants_whitelist', array( $this, 'add_woocommerce_constants_whitelist' ), 10 );
add_filter( 'jetpack_sync_post_meta_whitelist', array( $this, 'add_woocommerce_post_meta_whitelist' ), 10 );
add_filter( 'jetpack_sync_comment_meta_whitelist', array( $this, 'add_woocommerce_comment_meta_whitelist' ), 10 );
add_filter( 'jetpack_sync_before_enqueue_woocommerce_new_order_item', array( $this, 'filter_order_item' ) );
add_filter( 'jetpack_sync_before_enqueue_jetpack_updated_woo_customer_meta', array( $this, 'filter_customer_updated_meta' ) );
// Append an order's total to these actions when it reaches a paid status.
add_filter( 'jetpack_sync_before_enqueue_woocommerce_new_order', array( $this, 'add_order_total_to_new_order' ) );
add_filter( 'jetpack_sync_before_enqueue_woocommerce_order_status_changed', array( $this, 'add_order_total_to_status_changed' ) );
add_filter( 'jetpack_sync_whitelisted_comment_types', array( $this, 'add_review_comment_types' ) );
// Blacklist Action Scheduler comment types.
add_filter( 'jetpack_sync_prevent_sending_comment_data', array( $this, 'filter_action_scheduler_comments' ), 10, 2 );
// Preprocess action to be sent by Jetpack sync.
add_action( 'woocommerce_remove_order_items', array( $this, 'action_woocommerce_remove_order_items' ), 10, 2 );
}
/**
* Sync module name.
*
* @access public
*
* @return string
*/
public function name() {
return 'woocommerce';
}
/**
* Initialize WooCommerce action listeners.
*
* @access public
*
* @param callable $callable Action handler callable.
*/
public function init_listeners( $callable ) {
// Attributes.
add_action( 'woocommerce_attribute_added', $callable, 10, 2 );
add_action( 'woocommerce_attribute_updated', $callable, 10, 3 );
add_action( 'woocommerce_attribute_deleted', $callable, 10, 3 );
// Orders. When an order reaches a paid status we append its total to these actions (via the
// jetpack_sync_before_enqueue_* filters in the constructor) so the Activity Log can aggregate
// revenue without a dedicated action. We register the extra accepted args so those filters
// receive the order object WooCommerce already passes (2nd arg here, 4th for the status change)
// and can avoid reloading it on this hot path; the filters strip the object back out before the
// action is enqueued, so it is never serialized or sent to WPcom.
add_action( 'woocommerce_new_order', $callable, 10, 2 );
add_action( 'woocommerce_order_status_changed', $callable, 10, 4 );
add_action( 'woocommerce_payment_complete', $callable, 10, 1 );
// Order items.
add_action( 'woocommerce_new_order_item', $callable, 10, 4 );
add_action( 'woocommerce_delete_order_item', $callable, 10, 1 );
add_action( 'woocommerce_remove_order_item_ids', $callable, 10, 1 );
$this->init_listeners_for_meta_type( 'order_item', $callable );
$this->init_meta_whitelist_handler( 'order_item', array( $this, 'filter_meta' ) );
// Payment tokens.
add_action( 'woocommerce_new_payment_token', $callable, 10, 1 );
add_action( 'woocommerce_payment_token_deleted', $callable, 10, 2 );
add_action( 'woocommerce_payment_token_updated', $callable, 10, 1 );
$this->init_listeners_for_meta_type( 'payment_token', $callable );
// Product downloads.
add_action( 'woocommerce_downloadable_product_download_log_insert', $callable, 10, 1 );
add_action( 'woocommerce_grant_product_download_access', $callable, 10, 1 );
// Tax rates.
// These are ignored on WP.com: tax items are derived from order data via wc_order_tax_lookup, which isn’t present there.
add_action( 'woocommerce_tax_rate_added', $callable, 10, 2 );
add_action( 'woocommerce_tax_rate_updated', $callable, 10, 2 );
add_action( 'woocommerce_tax_rate_deleted', $callable, 10, 1 );
// Webhooks.
add_action( 'woocommerce_new_webhook', $callable, 10, 1 );
add_action( 'woocommerce_webhook_deleted', $callable, 10, 2 );
add_action( 'woocommerce_webhook_updated', $callable, 10, 1 );
// Customers.
add_action( 'added_user_meta', array( $this, 'maybe_sync_customer_meta_update' ), 10, 4 );
add_action( 'updated_user_meta', array( $this, 'maybe_sync_customer_meta_update' ), 10, 4 );
add_action( 'deleted_user_meta', array( $this, 'maybe_sync_customer_meta_update' ), 10, 4 );
add_action( 'delete_user', array( $this, 'action_delete_user' ), 10, 1 );
add_action( 'wpmu_delete_user', array( $this, 'action_delete_user' ), 10, 1 );
add_action( 'shutdown', array( $this, 'action_customer_meta_updates' ) );
add_action( 'jetpack_updated_woo_customer_meta', $callable, 10, 2 );
}
/**
* Initialize WooCommerce action listeners for full sync.
*
* @access public
*
* @param callable $callable Action handler callable.
*/
public function init_full_sync_listeners( $callable ) {
add_action( 'jetpack_full_sync_woocommerce_order_items', $callable ); // Also sends post meta.
}
/**
* Retrieve the actions that will be sent for this module during a full sync.
*
* @access public
*
* @return array Full sync actions of this module.
*/
public function get_full_sync_actions() {
return array( 'jetpack_full_sync_woocommerce_order_items' );
}
/**
* Initialize the module in the sender.
*
* @access public
*/
public function init_before_send() {
// Full sync.
add_filter( 'jetpack_sync_before_send_jetpack_full_sync_woocommerce_order_items', array( $this, 'build_full_sync_action_array' ) );
}
/**
* Expand the order items properly.
*
* @access public
*
* @param array $args The hook arguments.
* @return array $args The hook arguments.
*/
public function filter_order_item( $args ) {
// Make sure we always have all the data - prior to WooCommerce 3.0 we only have the user supplied data in the second argument and not the full details.
$args[1] = $this->build_order_item( $args[0] );
return $args;
}
/**
* Append an order's total to the synced woocommerce_new_order args when it is paid.
*
* A brand new order can be created already in a paid status, in which case no status transition
* fires and only woocommerce_new_order observes the payment. When the order is paid we append a
* trailing order-total payload (total, currency) that the Activity Log aggregates into
* revenue; otherwise only the order ID is synced (the action still syncs for other purposes).
*
* @since 4.44.0 Appends a trailing [ 'total', 'currency' ] payload when the new order is paid.
*
* @param array $args Hook args: [ order_id, WC_Order ]. The order object is WooCommerce's 2nd arg.
* @return array|false The args ( [ order_id ] ), with a trailing order-total payload appended when paid, or false when invalid.
*/
public function add_order_total_to_new_order( $args ) {
if ( ! is_array( $args ) || count( $args ) < 1 || ! is_numeric( $args[0] ) || (int) $args[0] <= 0 ) {
return false;
}
$order_id = (int) $args[0];
// Only use the order object WooCommerce passes as the 2nd arg; avoid wc_get_order on this hot path.
$order = ( isset( $args[1] ) && $args[1] instanceof WC_Order ) ? $args[1] : null;
// Rebuild the scalar arg shape WPcom expects. This also drops the WC_Order object the listener now
// receives so it is never enqueued or serialized into the sync queue.
$args = array( $order_id );
if ( $order && $this->is_paid_order_status( $order->get_status() ) ) {
$args = $this->maybe_append_order_total( $args, $order );
}
return $args;
}
/**
* Append an order's total to the synced woocommerce_order_status_changed args on payment.
*
* We emit on the transition *into* a paid status from a non-paid one — the payment moment — and
* skip paid -> paid steps (e.g. processing -> completed) so a fulfillment doesn't re-emit. When
* emitted we append a trailing order-total payload (total, currency) the Activity Log
* reads; otherwise only [ order_id, status_from, status_to ] is synced (the action still syncs for
* other purposes).
*
* @since 4.44.0 Appends a trailing [ 'total', 'currency' ] payload on the paid transition.
*
* @param array $args Hook args: [ order_id, status_from, status_to, WC_Order ]. The order is the 4th arg.
* @return array|false The args ( [ order_id, status_from, status_to ] ), with a trailing payload on the paid transition, or false when invalid.
*/
public function add_order_total_to_status_changed( $args ) {
if ( ! is_array( $args ) || count( $args ) < 3 || ! is_numeric( $args[0] ) || (int) $args[0] <= 0 ) {
return false;
}
$order_id = (int) $args[0];
$status_from = $args[1];
$status_to = $args[2];
if ( ! is_string( $status_from ) || ! is_string( $status_to ) ) {
return false;
}
// Only use the order object WooCommerce passes as the 4th arg; avoid wc_get_order on this hot path.
$order = ( isset( $args[3] ) && $args[3] instanceof WC_Order ) ? $args[3] : null;
// Rebuild the scalar arg shape WPcom expects. This also drops the WC_Order object the listener now
// receives so it is never enqueued or serialized into the sync queue.
$args = array( $order_id, $status_from, $status_to );
if ( $this->is_paid_order_status( $status_to ) && ! $this->is_paid_order_status( $status_from ) ) {
$args = $this->maybe_append_order_total( $args, $order );
}
return $args;
}
/**
* Append the order-total payload to the given args when this is the order's paid moment.
*
* @param array $args The scalar args built so far for the action.
* @param WC_Order|null $order Order object, or null when WooCommerce did not pass one.
* @return array The args, with a trailing order-total payload appended when emitted.
*/
private function maybe_append_order_total( $args, $order ) {
if ( $order && $this->claim_order_total_emission( $order ) ) {
$payload = $this->build_order_total_payload( $order );
if ( $payload !== null ) {
$args[] = $payload;
}
}
return $args;
}
/**
* Claim the once-per-request emission slot for an order's total.
*
* Test-and-set: returns true (and records the claim) the first time it's called for an order this
* request, false thereafter — so the woocommerce_new_order and woocommerce_order_status_changed
* hooks don't both emit a freshly created paid order. Callers must confirm the order is paid first.
*
* @param WC_Order $order Order object.
* @return bool True when the caller obtained the claim and should emit.
*/
private function claim_order_total_emission( $order ) {
$key = $order->get_id();
if ( isset( $this->synced_order_total_keys[ $key ] ) ) {
return false;
}
$this->synced_order_total_keys[ $key ] = true;
return true;
}
/**
* Build the trailing order-total payload appended to a paid order's synced action args.
*
* Intentionally minimal and scalar-only so it is safe to store and index on WPcom (Activity Log,
* Elasticsearch, MCP integrations). We read with the 'edit' context to get the raw stored values and
* skip the woocommerce_order_get_total / _currency view filters (e.g. multi-currency display
* conversion), then still normalize the total to a numeric string and cast the currency rather than
* trust whatever WooCommerce returns.
*
* @param WC_Order $order Order object.
* @return null|array {
* @type string $total Order total as a numeric string.
* @type string $currency Order currency code (e.g. 'USD').
* }
*/
private function build_order_total_payload( $order ) {
$total = $order->get_total( 'edit' );
if ( $total <= 0 ) {
return null;
}
return array(
'total' => function_exists( 'wc_format_decimal' ) ? wc_format_decimal( $total ) : (string) $total,
'currency' => (string) $order->get_currency( 'edit' ),
);
}
/**
* Whether an order status is one WooCommerce considers paid (and whose total we therefore sync).
*
* Uses WooCommerce's canonical, filterable list (wc_get_is_paid_statuses(), default 'processing'
* and 'completed', un-prefixed) so stores that register custom paid statuses are covered.
*
* @param string $status Order status without the `wc-` prefix (e.g. 'processing').
* @return bool True when WooCommerce treats the status as paid.
*/
private function is_paid_order_status( $status ) {
// Fail fast on empty/invalid input (e.g. a missing status arg) before the WooCommerce lookup.
if ( ! is_string( $status ) || '' === $status || ! function_exists( 'wc_get_is_paid_statuses' ) ) {
return false;
}
if ( null === $this->paid_order_statuses ) {
$this->paid_order_statuses = wc_get_is_paid_statuses();
}
return in_array( $status, $this->paid_order_statuses, true );
}
/**
* Validate the minimal customer meta update payload before enqueueing.
*
* @param array $args Hook arguments.
* @return array|false Minimal user object and changed prop names, or false when invalid.
*/
public function filter_customer_updated_meta( $args ) {
if (
! is_array( $args )
|| ! isset( $args[0] )
|| ! isset( $args[1] )
|| ! is_object( $args[0] )
|| ! isset( $args[0]->data )
|| ! is_object( $args[0]->data )
|| ! isset( $args[0]->data->ID )
|| ! is_numeric( $args[0]->data->ID )
|| ! is_array( $args[1] )
) {
return false;
}
$customer_id = (int) $args[0]->data->ID;
if ( $customer_id <= 0 ) {
return false;
}
$updated_props = $this->get_customer_detail_props( $args[1] );
if ( empty( $updated_props ) ) {
return false;
}
return array( $this->build_minimal_customer_user_object( $customer_id ), $updated_props );
}
/**
* Track updated WooCommerce customer meta props for syncing.
*
* @param int|array $meta_id ID of the meta object, or IDs for deleted meta.
* @param int $user_id User ID.
* @param string $meta_key Meta key.
* @param mixed $value Meta value.
*/
public function maybe_sync_customer_meta_update( $meta_id, $user_id, $meta_key, $value ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
$customer_id = (int) $user_id;
if ( $customer_id <= 0 ) {
return;
}
if ( 'deleted_user_meta' === current_action() && isset( $this->deleted_user_ids[ $customer_id ] ) ) {
return;
}
if ( ! is_string( $meta_key ) && ! is_numeric( $meta_key ) ) {
return;
}
$meta_key = sanitize_key( (string) $meta_key );
if ( ! isset( self::$customer_detail_meta_key_to_prop[ $meta_key ] ) ) {
return;
}
$updated_prop = self::$customer_detail_meta_key_to_prop[ $meta_key ];
if ( ! isset( $this->customer_meta_updates[ $customer_id ] ) ) {
$this->customer_meta_updates[ $customer_id ] = array();
}
$this->customer_meta_updates[ $customer_id ][ $updated_prop ] = true;
}
/**
* Mark a deleted user so customer meta cleanup does not sync as profile changes.
*
* @param int $user_id User ID.
*/
public function action_delete_user( $user_id ) {
$customer_id = (int) $user_id;
if ( $customer_id <= 0 ) {
return;
}
$this->deleted_user_ids[ $customer_id ] = true;
unset( $this->customer_meta_updates[ $customer_id ] );
}
/**
* Send batched WooCommerce customer meta updates.
*/
public function action_customer_meta_updates() {
if ( empty( $this->customer_meta_updates ) ) {
return;
}
$customer_meta_updates = $this->customer_meta_updates;
$this->customer_meta_updates = array();
foreach ( $customer_meta_updates as $customer_id => $updated_props ) {
if ( isset( $this->deleted_user_ids[ (int) $customer_id ] ) ) {
continue;
}
/**
* Fires when WooCommerce customer details stored in user meta are updated.
*
* @param object $customer Minimal WP_User-shaped customer object.
* @param array $updated_props Updated customer detail prop names.
*/
do_action(
'jetpack_updated_woo_customer_meta',
$this->build_minimal_customer_user_object( (int) $customer_id ),
array_keys( $updated_props )
);
}
}
/**
* Retrieve whitelisted WooCommerce customer detail props.
*
* @param array $props Customer detail meta keys or prop names.
* @return array Customer detail prop names.
*/
private function get_customer_detail_props( $props ) {
$updated_props = array();
foreach ( $props as $prop ) {
if ( ! is_string( $prop ) && ! is_numeric( $prop ) ) {
continue;
}
$prop = sanitize_key( (string) $prop );
if ( isset( self::$customer_detail_meta_key_to_prop[ $prop ] ) ) {
$updated_props[] = self::$customer_detail_meta_key_to_prop[ $prop ];
continue;
}
if ( in_array( $prop, self::$customer_detail_meta_key_to_prop, true ) ) {
$updated_props[] = $prop;
}
}
return array_values( array_unique( $updated_props ) );
}
/**
* Build a minimal WP_User-shaped object for Activity Log.
*
* @param int $customer_id Customer user ID.
* @return object Minimal user object.
*/
private function build_minimal_customer_user_object( $customer_id ) {
$user_data = (object) array(
'ID' => $customer_id,
'display_name' => '',
'user_login' => '',
'user_email' => '',
);
$user = get_userdata( $customer_id );
if ( $user ) {
$user_data->display_name = (string) $user->display_name;
$user_data->user_login = (string) $user->user_login;
$user_data->user_email = (string) $user->user_email;
}
return (object) array(
'ID' => $customer_id,
'data' => $user_data,
);
}
/**
* Handler for filtering out non-whitelisted order item meta.
*
* @since 4.22.3
*
* @param array $args Hook arguments.
* @return array|false False if not whitelisted, the original hook args otherwise.
*/
public function filter_meta( $args ) {
if (
! empty( $args[2] ) && $this->is_whitelisted_order_item_meta( $args[2] )
) {
return $args;
}
return false;
}
/**
* Whether an order item meta key is whitelisted for sync.
*
* @access public
*
* @since 4.22.3
*
* @param string $meta_key Order item meta key.
* @return bool True if whitelisted.
*/
public function is_whitelisted_order_item_meta( $meta_key ) {
return is_string( $meta_key ) && in_array( $meta_key, self::$order_item_meta_whitelist, true );
}
/**
* Retrieve the order item ids to be removed and send them as one action
*
* @param WC_Order $order The order argument.
* @param string $type Order item type.
*/
public function action_woocommerce_remove_order_items( WC_Order $order, $type ) {
if ( $type ) {
$order_items = $order->get_items( $type );
} else {
$order_items = $order->get_items();
}
$order_item_ids = array_keys( $order_items );
if ( $order_item_ids ) {
do_action( 'woocommerce_remove_order_item_ids', $order_item_ids );
}
}
/**
* Expand order item IDs to order items and their meta.
*
* @access public
*
* @todo Refactor table name to use a $wpdb->prepare placeholder.
*
* @param array $args The hook arguments.
* @return array $args Expanded order items with meta.
* @deprecated since 4.7.0
*/
public function expand_order_item_ids( $args ) {
_deprecated_function( __METHOD__, '4.7.0' );
$order_item_ids = $args[0];
global $wpdb;
$order_item_ids_sql = implode( ', ', array_map( 'intval', $order_item_ids ) );
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
$order_items = $wpdb->get_results(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"SELECT * FROM $this->order_item_table_name WHERE order_item_id IN ( $order_item_ids_sql )"
);
return array(
$order_items,
$this->get_metadata( $order_item_ids, 'order_item', static::$order_item_meta_whitelist ),
);
}
/**
* Extract the full order item from the database by its ID.
*
* @access public
*
* @param int $order_item_id Order item ID.
* @return object Order item.
*/
public function build_order_item( $order_item_id ) {
global $wpdb;
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Direct database access is intentional; caching is not required for this query.
return $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE order_item_id = %d', $this->order_item_table_name, $order_item_id ) );
}
/**
* Enqueue the WooCommerce actions for full sync.
*
* @access public
*
* @param array $config Full sync configuration for this sync module.
* @param int $max_items_to_enqueue Maximum number of items to enqueue.
* @param boolean $state True if full sync has finished enqueueing this module, false otherwise.
* @return array Number of actions enqueued, and next module state.
*/
public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_woocommerce_order_items', $this->order_item_table_name, 'order_item_id', $this->get_where_sql( $config ), $max_items_to_enqueue, $state );
}
/**
* Retrieve an estimated number of actions that will be enqueued.
*
* @access public
*
* @todo Refactor the SQL query to use $wpdb->prepare().
*
* @param array $config Full sync configuration for this sync module.
* @return int Number of items yet to be enqueued.
*/
public function estimate_full_sync_actions( $config ) {
global $wpdb;
$query = "SELECT count(*) FROM $this->order_item_table_name WHERE " . $this->get_where_sql( $config );
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
$count = (int) $wpdb->get_var( $query );
return (int) ceil( $count / self::ARRAY_CHUNK_SIZE );
}
/**
* Retrieve the WHERE SQL clause based on the module config.
*
* @access private
*
* @param array $config Full sync configuration for this sync module.
* @return string WHERE SQL clause.
*/
public function get_where_sql( $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
return '1=1';
}
/**
* Add WooCommerce options to the options whitelist.
*
* @param array $list Existing options whitelist.
* @return array Updated options whitelist.
*/
public function add_woocommerce_options_whitelist( $list ) {
return array_merge( $list, self::$wc_options_whitelist );
}
/**
* Add WooCommerce constants to the constants whitelist.
*
* @param array $list Existing constants whitelist.
* @return array Updated constants whitelist.
*/
public function add_woocommerce_constants_whitelist( $list ) {
return array_merge( $list, self::$wc_constants_whitelist );
}
/**
* Add WooCommerce post meta to the post meta whitelist.
*
* @param array $list Existing post meta whitelist.
* @return array Updated post meta whitelist.
*/
public function add_woocommerce_post_meta_whitelist( $list ) {
return array_merge( $list, self::$wc_post_meta_whitelist );
}
/**
* Add WooCommerce comment meta to the comment meta whitelist.
*
* @param array $list Existing comment meta whitelist.
* @return array Updated comment meta whitelist.
*/
public function add_woocommerce_comment_meta_whitelist( $list ) {
return array_merge( $list, self::$wc_comment_meta_whitelist );
}
/**
* Adds 'revew' to the list of comment types so Sync will listen for status changes on 'reviews'.
*
* @access public
*
* @param array $comment_types The list of comment types prior to this filter.
* return array The list of comment types with 'review' added.
*/
public function add_review_comment_types( $comment_types ) {
if ( is_array( $comment_types ) ) {
$comment_types[] = 'review';
}
return $comment_types;
}
/**
* Stop comments from the Action Scheduler from being synced.
* https://github.com/woocommerce/woocommerce/tree/e7762627c37ec1f7590e6cac4218ba0c6a20024d/includes/libraries/action-scheduler
*
* @since 1.6.3
* @since-jetpack 7.7.0
*
* @param boolean $can_sync Should we prevent comment data from bing synced to WordPress.com.
* @param mixed $comment WP_COMMENT object.
*
* @return bool
*/
public function filter_action_scheduler_comments( $can_sync, $comment ) {
if ( isset( $comment->comment_agent ) && 'ActionScheduler' === $comment->comment_agent ) {
return true;
}
return $can_sync;
}
/**
* Whitelist for options we are interested to sync.
*
* @access private
* @static
*
* @var array
*/
private static $wc_options_whitelist = array(
'woocommerce_currency',
'woocommerce_db_version',
'woocommerce_weight_unit',
'woocommerce_version',
'woocommerce_unforce_ssl_checkout',
'woocommerce_tax_total_display',
'woocommerce_tax_round_at_subtotal',
'woocommerce_tax_display_shop',
'woocommerce_tax_display_cart',
'woocommerce_prices_include_tax',
'woocommerce_price_thousand_sep',
'woocommerce_price_num_decimals',
'woocommerce_price_decimal_sep',
'woocommerce_notify_low_stock',
'woocommerce_notify_low_stock_amount',
'woocommerce_notify_no_stock',
'woocommerce_notify_no_stock_amount',
'woocommerce_manage_stock',
'woocommerce_force_ssl_checkout',
'woocommerce_hide_out_of_stock_items',
'woocommerce_file_download_method',
'woocommerce_enable_signup_and_login_from_checkout',
'woocommerce_enable_shipping_calc',
'woocommerce_enable_review_rating',
'woocommerce_enable_guest_checkout',
'woocommerce_enable_coupons',
'woocommerce_enable_checkout_login_reminder',
'woocommerce_enable_ajax_add_to_cart',
'woocommerce_dimension_unit',
'woocommerce_default_country',
'woocommerce_default_customer_address',
'woocommerce_currency_pos',
'woocommerce_api_enabled',
'woocommerce_allow_tracking',
'woocommerce_task_list_hidden',
'woocommerce_cod_settings',
'woocommerce_store_address',
'woocommerce_store_address_2',
'woocommerce_store_city',
'woocommerce_store_postcode',
'woocommerce_admin_install_timestamp',
'woocommerce_enable_signup_from_checkout_for_subscriptions', // This and the below options relate to the WooCommerce Accounts and Privacy settings page. Required for the Activity Log.
'woocommerce_enable_myaccount_registration',
'woocommerce_registration_generate_password',
'woocommerce_erasure_request_removes_order_data',
'woocommerce_erasure_request_removes_subscription_data',
'woocommerce_erasure_request_removes_download_data',
'woocommerce_allow_bulk_remove_personal_data',
'woocommerce_registration_privacy_policy_text',
'woocommerce_checkout_privacy_policy_text',
'woocommerce_delete_inactive_accounts',
'woocommerce_trash_pending_orders',
'woocommerce_trash_failed_orders',
'woocommerce_trash_cancelled_orders',
'woocommerce_anonymize_refunded_orders',
'woocommerce_anonymize_completed_orders',
'woocommerce_anonymize_ended_subscriptions',
'woocommerce_enable_delayed_account_creation',
'woocommerce_gateway_stripe_retention',
'wc_downloads_approved_directories_mode', // This and the below options relate to the WooCommerce Products settings page. Required for the Activity Log.
'woocommerce_attribute_lookup_direct_updates',
'woocommerce_attribute_lookup_enabled',
'woocommerce_attribute_lookup_optimized_updates',
'woocommerce_cart_redirect_after_add',
'woocommerce_downloads_add_hash_to_filename',
'woocommerce_downloads_count_partial',
'woocommerce_downloads_deliver_inline',
'woocommerce_downloads_grant_access_after_payment',
'woocommerce_downloads_redirect_fallback_allowed',
'woocommerce_downloads_require_login',
'woocommerce_enable_reviews',
'woocommerce_hold_stock_minutes',
'woocommerce_review_rating_required',
'woocommerce_review_rating_verification_label',
'woocommerce_review_rating_verification_required',
'woocommerce_shop_page_id',
'woocommerce_stock_email_recipient',
'woocommerce_stock_format',
'woocommerce_allowed_countries', // This and the below options relate to the WooCommerce General settings page. Required for the Activity Log.
'woocommerce_specific_allowed_countries',
'woocommerce_ship_to_countries',
'woocommerce_specific_ship_to_countries',
'woocommerce_all_except_countries',
'woocommerce_calc_taxes',
'woocommerce_calc_discounts_sequentially',
'woocommerce_analytics_enabled', // This and the below options relate to the WooCommerce Advanced settings page. Required for the Activity Log.
'woocommerce_cart_page_id',
'woocommerce_checkout_order_received_endpoint',
'woocommerce_checkout_page_id',
'woocommerce_checkout_pay_endpoint',
'woocommerce_custom_orders_table_data_sync_enabled',
'woocommerce_custom_orders_table_enabled',
'woocommerce_feature_block_email_editor_enabled',
'woocommerce_feature_blueprint_enabled',
'woocommerce_feature_cost_of_goods_sold_enabled',
'woocommerce_feature_customer_review_request_enabled',
'woocommerce_feature_deferred_transactional_emails_enabled',
'woocommerce_feature_destroy-empty-sessions_enabled',
'woocommerce_feature_email_improvements_enabled',
'woocommerce_feature_mcp_integration_enabled',
'woocommerce_feature_order_attribution_enabled',
'woocommerce_feature_point_of_sale_enabled',
'woocommerce_feature_product_instance_caching_enabled',
'woocommerce_feature_rate_limit_checkout_enabled',
'woocommerce_feature_remote_logging_enabled',
'woocommerce_feature_rest_api_caching_enabled',
'woocommerce_feature_site_visibility_badge_enabled',
'woocommerce_hpos_datastore_caching_enabled',
'woocommerce_hpos_fts_index_enabled',
'woocommerce_logout_endpoint',
'woocommerce_myaccount_add_payment_method_endpoint',
'woocommerce_myaccount_delete_payment_method_endpoint',
'woocommerce_myaccount_downloads_endpoint',
'woocommerce_myaccount_edit_account_endpoint',
'woocommerce_myaccount_edit_address_endpoint',
'woocommerce_myaccount_lost_password_endpoint',
'woocommerce_myaccount_orders_endpoint',
'woocommerce_myaccount_page_id',
'woocommerce_myaccount_payment_methods_endpoint',