forked from woocommerce/facebook-for-woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
facebook-commerce.php
3881 lines (3017 loc) · 103 KB
/
facebook-commerce.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
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
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*
* @package FacebookCommerce
*/
use SkyVerge\WooCommerce\Facebook\Admin;
use SkyVerge\WooCommerce\Facebook\Events\AAMSettings;
use SkyVerge\WooCommerce\Facebook\Handlers\Connection;
use SkyVerge\WooCommerce\Facebook\Products;
use SkyVerge\WooCommerce\Facebook\Products\Feed;
use SkyVerge\WooCommerce\PluginFramework\v5_10_0 as Framework;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
require_once 'facebook-config-warmer.php';
require_once 'includes/fbproduct.php';
require_once 'facebook-commerce-pixel-event.php';
class WC_Facebookcommerce_Integration extends WC_Integration {
/**
* @var string the WordPress option name where the page access token is stored
* @deprecated 2.1.0
*/
const OPTION_PAGE_ACCESS_TOKEN = 'wc_facebook_page_access_token';
/** @var string the WordPress option name where the product catalog ID is stored */
const OPTION_PRODUCT_CATALOG_ID = 'wc_facebook_product_catalog_id';
/** @var string the WordPress option name where the external merchant settings ID is stored */
const OPTION_EXTERNAL_MERCHANT_SETTINGS_ID = 'wc_facebook_external_merchant_settings_id';
/** @var string the WordPress option name where the feed ID is stored */
const OPTION_FEED_ID = 'wc_facebook_feed_id';
/** @var string the WordPress option name where the upload ID is stored */
const OPTION_UPLOAD_ID = 'wc_facebook_upload_id';
/** @var string the WordPress option name where the JS SDK version is stored */
const OPTION_JS_SDK_VERSION = 'wc_facebook_js_sdk_version';
/** @var string the WordPress option name where the latest pixel install time is stored */
const OPTION_PIXEL_INSTALL_TIME = 'wc_facebook_pixel_install_time';
/** @var string the facebook page ID setting ID */
const SETTING_FACEBOOK_PAGE_ID = 'wc_facebook_page_id';
/** @var string the facebook pixel ID setting ID */
const SETTING_FACEBOOK_PIXEL_ID = 'wc_facebook_pixel_id';
/** @var string the "enable advanced matching" setting ID */
const SETTING_ENABLE_ADVANCED_MATCHING = 'enable_advanced_matching';
/** @var string the "use s2s" setting ID */
const SETTING_USE_S2S = 'use_s2s';
/** @var string the "access token" setting ID */
const SETTING_ACCESS_TOKEN = 'access_token';
/** @var string the "enable product sync" setting ID */
const SETTING_ENABLE_PRODUCT_SYNC = 'wc_facebook_enable_product_sync';
/** @var string the excluded product category IDs setting ID */
const SETTING_EXCLUDED_PRODUCT_CATEGORY_IDS = 'wc_facebook_excluded_product_category_ids';
/** @var string the excluded product tag IDs setting ID */
const SETTING_EXCLUDED_PRODUCT_TAG_IDS = 'wc_facebook_excluded_product_tag_ids';
/** @var string the product description mode setting ID */
const SETTING_PRODUCT_DESCRIPTION_MODE = 'wc_facebook_product_description_mode';
/** @var string the scheduled resync offset setting ID */
const SETTING_SCHEDULED_RESYNC_OFFSET = 'scheduled_resync_offset';
/** @var string the "enable messenger" setting ID */
const SETTING_ENABLE_MESSENGER = 'wc_facebook_enable_messenger';
/** @var string the messenger locale setting ID */
const SETTING_MESSENGER_LOCALE = 'wc_facebook_messenger_locale';
/** @var string the messenger greeting setting ID */
const SETTING_MESSENGER_GREETING = 'wc_facebook_messenger_greeting';
/** @var string the messenger color HEX setting ID */
const SETTING_MESSENGER_COLOR_HEX = 'wc_facebook_messenger_color_hex';
/** @var string the "debug mode" setting ID */
const SETTING_ENABLE_DEBUG_MODE = 'wc_facebook_enable_debug_mode';
/** @var string the standard product description mode name */
const PRODUCT_DESCRIPTION_MODE_STANDARD = 'standard';
/** @var string the short product description mode name */
const PRODUCT_DESCRIPTION_MODE_SHORT = 'short';
/** @var string the hook for the recurreing action that syncs products */
const ACTION_HOOK_SCHEDULED_RESYNC = 'sync_all_fb_products_using_feed';
/** @var string custom taxonomy FB product set ID */
const FB_PRODUCT_SET_ID = 'fb_product_set_id';
/** @var string|null the configured product catalog ID */
public $product_catalog_id;
/** @var string|null the configured external merchant settings ID */
public $external_merchant_settings_id;
/** @var string|null the configured feed ID */
public $feed_id;
/** @var string|null the configured upload ID */
private $upload_id;
/** @var string|null the configured pixel install time */
public $pixel_install_time;
/** @var string|null the configured JS SDK version */
private $js_sdk_version;
/** @var bool|null whether the feed has been migrated from FBE 1 to FBE 1.5 */
private $feed_migrated;
/** @var array the page name and url */
private $page;
/** Legacy properties *********************************************************************************************/
// TODO probably some of these meta keys need to be moved to Facebook\Products {FN 2020-01-13}
const FB_PRODUCT_GROUP_ID = 'fb_product_group_id';
const FB_PRODUCT_ITEM_ID = 'fb_product_item_id';
const FB_PRODUCT_DESCRIPTION = 'fb_product_description';
/** @var string the API flag to set a product as visible in the Facebook shop */
const FB_SHOP_PRODUCT_VISIBLE = 'published';
/** @var string the API flag to set a product as not visible in the Facebook shop */
const FB_SHOP_PRODUCT_HIDDEN = 'staging';
/** @var string @deprecated */
const FB_CART_URL = 'fb_cart_url';
const FB_MESSAGE_DISPLAY_TIME = 180;
// Number of days to query tip.
const FB_TIP_QUERY = 1;
// TODO: this constant is no longer used and can probably be removed {WV 2020-01-21}
const FB_VARIANT_IMAGE = 'fb_image';
const FB_ADMIN_MESSAGE_PREPEND = '<b>Facebook for WooCommerce</b><br/>';
const FB_SYNC_IN_PROGRESS = 'fb_sync_in_progress';
const FB_SYNC_REMAINING = 'fb_sync_remaining';
const FB_SYNC_TIMEOUT = 30;
const FB_PRIORITY_MID = 9;
private $test_mode = false;
public function init_pixel() {
WC_Facebookcommerce_Pixel::initialize();
// Migrate WC customer pixel_id from WC settings to WP options.
// This is part of a larger effort to consolidate all the FB-specific
// settings for all plugin integrations.
if ( is_admin() ) {
$pixel_id = WC_Facebookcommerce_Pixel::get_pixel_id();
$settings_pixel_id = $this->get_facebook_pixel_id();
if (
WC_Facebookcommerce_Utils::is_valid_id( $settings_pixel_id ) &&
( ! WC_Facebookcommerce_Utils::is_valid_id( $pixel_id ) ||
$pixel_id != $settings_pixel_id
)
) {
WC_Facebookcommerce_Pixel::set_pixel_id( $settings_pixel_id );
}
// migrate Advanced Matching enabled (use_pii) from the integration setting to the pixel option,
// so that it works the same way the pixel ID does
$settings_advanced_matching_enabled = $this->is_advanced_matching_enabled();
WC_Facebookcommerce_Pixel::set_use_pii_key( $settings_advanced_matching_enabled );
$settings_use_s2s = $this->is_use_s2s_enabled();
WC_Facebookcommerce_Pixel::set_use_s2s( $settings_use_s2s );
$settings_access_token = $this->get_access_token();
WC_Facebookcommerce_Pixel::set_access_token($settings_access_token);
}
}
/**
* Init and hook in the integration.
*
* @access public
* @return void
*/
public function __construct() {
if ( ! class_exists( 'WC_Facebookcommerce_EventsTracker' ) ) {
include_once 'facebook-commerce-events-tracker.php';
}
$this->id = WC_Facebookcommerce::INTEGRATION_ID;
$this->method_title = __(
'Facebook for WooCommerce',
'facebook-for-commerce'
);
$this->method_description = __(
'Facebook Commerce and Dynamic Ads (Pixel) Extension',
'facebook-for-commerce'
);
// Load the settings.
$this->init_settings();
$pixel_id = WC_Facebookcommerce_Pixel::get_pixel_id();
// if there is a pixel option saved and no integration setting saved, inherit the pixel option
if ( $pixel_id && ! $this->get_facebook_pixel_id() ) {
$this->settings[ self::SETTING_FACEBOOK_PIXEL_ID ] = $pixel_id;
}
$advanced_matching_enabled = WC_Facebookcommerce_Pixel::get_use_pii_key();
// if Advanced Matching (use_pii) is enabled on the saved pixel option and not on the saved integration setting,
// inherit the pixel option
if ( $advanced_matching_enabled && ! $this->is_advanced_matching_enabled() ) {
$this->settings[ self::SETTING_ENABLE_ADVANCED_MATCHING ] = $advanced_matching_enabled;
}
//For now, the values of use s2s and access token will be the ones returned from WC_Facebookcommerce_Pixel
$use_s2s = WC_Facebookcommerce_Pixel::get_use_s2s();
$this->settings[self::SETTING_USE_S2S] = $use_s2s;
$access_token = WC_Facebookcommerce_Pixel::get_access_token();
$this->settings[self::SETTING_ACCESS_TOKEN] = $access_token;
if ( ! class_exists( 'WC_Facebookcommerce_Utils' ) ) {
include_once 'includes/fbutils.php';
}
WC_Facebookcommerce_Utils::$ems = $this->get_external_merchant_settings_id();
if ( ! class_exists( 'WC_Facebookcommerce_Graph_API' ) ) {
include_once 'includes/fbgraph.php';
$this->fbgraph = new WC_Facebookcommerce_Graph_API( facebook_for_woocommerce()->get_connection_handler()->get_access_token() );
}
WC_Facebookcommerce_Utils::$fbgraph = $this->fbgraph;
// Hooks
if ( is_admin() ) {
$this->init_pixel();
if ( ! class_exists( 'WC_Facebookcommerce_EventsTracker' ) ) {
include_once 'includes/fbutils.php';
}
// Display an info banner for eligible pixel and user.
if ( $this->get_external_merchant_settings_id()
&& $this->get_facebook_pixel_id()
&& $this->get_pixel_install_time() ) {
$should_query_tip =
WC_Facebookcommerce_Utils::check_time_cap(
get_option( 'fb_info_banner_last_query_time', '' ),
self::FB_TIP_QUERY
);
$last_tip_info = WC_Facebookcommerce_Utils::get_cached_best_tip();
if ( $should_query_tip || $last_tip_info ) {
if ( ! class_exists( 'WC_Facebookcommerce_Info_Banner' ) ) {
include_once 'includes/fbinfobanner.php';
}
WC_Facebookcommerce_Info_Banner::get_instance(
$this->get_external_merchant_settings_id(),
$this->fbgraph,
$should_query_tip
);
}
}
if ( ! class_exists( 'WC_Facebook_Integration_Test' ) ) {
include_once 'includes/test/facebook-integration-test.php';
}
$integration_test = WC_Facebook_Integration_Test::get_instance( $this );
$integration_test::$fbgraph = $this->fbgraph;
if ( ! $this->get_pixel_install_time() && $this->get_facebook_pixel_id() ) {
$this->update_pixel_install_time( time() );
}
add_action( 'admin_notices', array( $this, 'checks' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'load_assets' ) );
add_action(
'wp_ajax_ajax_sync_all_fb_products',
array( $this, 'ajax_sync_all_fb_products' ),
self::FB_PRIORITY_MID
);
add_action(
'wp_ajax_ajax_sync_all_fb_products_using_feed',
array( $this, 'ajax_sync_all_fb_products_using_feed' ),
self::FB_PRIORITY_MID
);
add_action(
'wp_ajax_ajax_check_feed_upload_status',
array( $this, 'ajax_check_feed_upload_status' ),
self::FB_PRIORITY_MID
);
add_action(
'wp_ajax_ajax_reset_all_fb_products',
array( $this, 'ajax_reset_all_fb_products' ),
self::FB_PRIORITY_MID
);
add_action(
'wp_ajax_ajax_display_test_result',
array( $this, 'ajax_display_test_result' )
);
add_action(
'wp_ajax_ajax_schedule_force_resync',
array( $this, 'ajax_schedule_force_resync' ),
self::FB_PRIORITY_MID
);
// don't duplicate product FBID meta
add_filter( 'woocommerce_duplicate_product_exclude_meta', [ $this, 'fb_duplicate_product_reset_meta' ] );
// add product processing hooks if the plugin is configured only
if ( $this->is_configured() && $this->get_product_catalog_id() ) {
// on_product_save() must run with priority larger than 20 to make sure WooCommerce has a chance to save the submitted product information
add_action( 'woocommerce_process_product_meta', [ $this, 'on_product_save' ], 40 );
add_action(
'woocommerce_product_quick_edit_save',
array( $this, 'on_quick_and_bulk_edit_save' ),
10, // Action priority
1 // Args passed to on_quick_and_bulk_edit_save ('product')
);
add_action(
'woocommerce_product_bulk_edit_save',
array( $this, 'on_quick_and_bulk_edit_save' ),
10, // Action priority
1 // Args passed to on_quick_and_bulk_edit_save ('product')
);
add_action( 'before_delete_post', [ $this, 'on_product_delete' ] );
add_action( 'add_meta_boxes', array( $this, 'fb_product_metabox' ), 10, 1 );
add_action(
'transition_post_status',
array( $this, 'fb_change_product_published_status' ),
10,
3
);
add_action(
'wp_ajax_ajax_fb_toggle_visibility',
array( $this, 'ajax_fb_toggle_visibility' )
);
add_action(
'wp_ajax_ajax_reset_single_fb_product',
array( $this, 'ajax_reset_single_fb_product' )
);
add_action(
'wp_ajax_ajax_delete_fb_product',
array( $this, 'ajax_delete_fb_product' )
);
add_action(
'pmxi_after_xml_import',
array( $this, 'wp_all_import_compat' )
);
add_action(
'wp_ajax_wpmelon_adv_bulk_edit',
[ $this, 'ajax_woo_adv_bulk_edit_compat' ],
self::FB_PRIORITY_MID
);
// used to remove the 'you need to resync' message
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['remove_sticky'] ) ) {
$this->remove_sticky_message();
}
}
$this->load_background_sync_process();
}
// Must be outside of admin for cron to schedule correctly.
add_action( 'sync_all_fb_products_using_feed', [ $this, 'handle_scheduled_resync_action' ], self::FB_PRIORITY_MID );
// handle the special background feed generation action
add_action( 'wc_facebook_generate_product_catalog_feed', [ $this, 'handle_generate_product_catalog_feed' ] );
if ( $this->get_facebook_pixel_id() ) {
$aam_settings = $this->load_aam_settings_of_pixel();
$user_info = WC_Facebookcommerce_Utils::get_user_info( $aam_settings );
$this->events_tracker = new WC_Facebookcommerce_EventsTracker( $user_info, $aam_settings );
}
// initialize the messenger chat features
$this->messenger_chat = new WC_Facebookcommerce_MessengerChat( [
'fb_page_id' => $this->get_facebook_page_id(),
'facebook_jssdk_version' => $this->get_js_sdk_version(),
] );
// Product Set hooks
add_action( 'fb_wc_product_set_sync', array( $this, 'create_or_update_product_set_item' ), 99, 2 );
add_action( 'fb_wc_product_set_delete', array( $this, 'delete_product_set_item' ), 99 );
}
/**
* Returns the Automatic advanced matching of this pixel
*
* @since 2.0.3
*
* @return AAMSettings
*/
private function load_aam_settings_of_pixel() {
$installed_pixel = $this->get_facebook_pixel_id();
// If no pixel is installed, reading the DB is not needed
if(!$installed_pixel ){
return null;
}
$config_key = 'wc_facebook_aam_settings';
$saved_value = get_transient( $config_key );
$refresh_interval = 20*MINUTE_IN_SECONDS;
$aam_settings = null;
// If wc_facebook_aam_settings is present in the DB
// it is converted into an AAMSettings object
if( $saved_value !== false ){
$cached_aam_settings = new AAMSettings(json_decode($saved_value, true));
// This condition is added because
// it is possible that the AAMSettings saved do not belong to the current
// installed pixel
// because the admin could have changed the connection to Facebook
// during the refresh interval
if($cached_aam_settings->get_pixel_id() == $installed_pixel){
$aam_settings = $cached_aam_settings;
}
}
// If the settings are not present or invalid
// they are fetched from Facebook domain
// and cached in WP database if they are not null
if(!$aam_settings){
$aam_settings = AAMSettings::build_from_pixel_id( $installed_pixel );
if($aam_settings){
set_transient($config_key, strval($aam_settings), $refresh_interval);
}
}
return $aam_settings;
}
public function load_background_sync_process() {
// Attempt to load background processing (Woo 3.x.x only)
include_once 'includes/fbbackground.php';
if ( class_exists( 'WC_Facebookcommerce_Background_Process' ) ) {
if ( ! isset( $this->background_processor ) ) {
$this->background_processor =
new WC_Facebookcommerce_Background_Process( $this );
}
}
add_action(
'wp_ajax_ajax_fb_background_check_queue',
array( $this, 'ajax_fb_background_check_queue' )
);
}
public function ajax_fb_background_check_queue() {
WC_Facebookcommerce_Utils::check_woo_ajax_permissions( 'background check queue', true );
check_ajax_referer( 'wc_facebook_settings_jsx' );
$request_time = null;
if ( isset( $_POST['request_time'] ) ) {
$request_time = esc_js( sanitize_text_field( wp_unslash( $_POST['request_time'] ) ) );
}
if ( facebook_for_woocommerce()->get_connection_handler()->get_access_token() ) {
if ( isset( $this->background_processor ) ) {
$is_processing = $this->background_processor->handle_cron_healthcheck();
$remaining = $this->background_processor->get_item_count();
$response = array(
'connected' => true,
'background' => true,
'processing' => $is_processing,
'remaining' => $remaining,
'request_time' => $request_time,
);
} else {
$response = array(
'connected' => true,
'background' => false,
);
}
} else {
$response = array(
'connected' => false,
'background' => false,
);
}
printf( json_encode( $response ) );
wp_die();
}
/**
* Adds a new tab to the Product edit page.
*
* @internal
* @deprecated since 1.10.0
*
* @param array $tabs array of tabs
* @return array
*/
public function fb_new_product_tab( $tabs ) {
wc_deprecated_function( __METHOD__, '1.10.0', '\\SkyVerge\\WooCommerce\\Facebook\\Admin::add_product_settings_tab()' );
return $tabs;
}
/**
* Adds content to the new Facebook tab on the Product edit page.
*
* @internal
* @deprecated since 1.10.0
*/
public function fb_new_product_tab_content() {
wc_deprecated_function( __METHOD__, '1.10.0', '\\SkyVerge\\WooCommerce\\Facebook\\Admin::add_product_settings_tab_content()' );
}
/**
* Filters the product columns in the admin edit screen.
*
* @internal
* @deprecated since 1.10.0
*
* @param array $existing_columns array of columns and labels
* @return array
*/
public function fb_product_columns( $existing_columns ) {
wc_deprecated_function( __METHOD__, '1.10.0', '\\SkyVerge\\WooCommerce\\Facebook\\Admin::add_product_list_table_column()' );
return $existing_columns;
}
/**
* Outputs content for the FB Shop column in the edit screen.
*
* @internal
* @deprecated since 1.10.0
*
* @param string $column name of the column to display
*/
public function fb_render_product_columns( $column ) {
wc_deprecated_function( __METHOD__, '1.10.0', '\\SkyVerge\\WooCommerce\\Facebook\\Admin::add_product_list_table_columns_content()' );
}
public function fb_product_metabox() {
$ajax_data = array(
'nonce' => wp_create_nonce( 'wc_facebook_metabox_jsx' ),
);
wp_enqueue_script(
'wc_facebook_metabox_jsx',
plugins_url(
'/assets/js/facebook-metabox.min.js',
__FILE__
),
[],
\WC_Facebookcommerce::PLUGIN_VERSION
);
wp_localize_script(
'wc_facebook_metabox_jsx',
'wc_facebook_metabox_jsx',
$ajax_data
);
add_meta_box(
'facebook_metabox', // Meta box ID
'Facebook', // Meta box Title
array( $this, 'fb_product_meta_box_html' ), // Callback
'product', // Screen to which to add the meta box
'side' // Context
);
}
/**
* Renders the content of the product meta box.
*/
public function fb_product_meta_box_html() {
global $post;
$woo_product = new WC_Facebook_Product( $post->ID );
$fb_product_group_id = null;
if ( $woo_product->woo_product instanceof \WC_Product && Products::product_should_be_synced( $woo_product->woo_product ) ) {
$fb_product_group_id = $this->get_product_fbid( self::FB_PRODUCT_GROUP_ID, $post->ID, $woo_product );
}
?>
<span id="fb_metadata">
<?php
if ( $fb_product_group_id ) {
?>
<?php echo esc_html__( 'Facebook ID:', 'facebook-for-woocommerce' ); ?> <a href="https://facebook.com/<?php echo esc_attr( $fb_product_group_id ); ?>"
target="_blank"><?php echo esc_html( $fb_product_group_id ); ?></a>
<?php if ( WC_Facebookcommerce_Utils::is_variable_type( $woo_product->get_type() ) ) : ?>
<?php if ( $product_item_ids_by_variation_id = $this->get_variation_product_item_ids( $woo_product, $fb_product_group_id ) ) : ?>
<p>
<?php echo esc_html__( 'Variant IDs:', 'facebook-for-woocommerce' ); ?><br/>
<?php foreach ( $product_item_ids_by_variation_id as $variation_id => $product_item_id ) : ?>
<?php echo esc_html( $variation_id ); ?>: <a href="https://facebook.com/<?php echo esc_attr( $product_item_id ); ?>"
target="_blank"><?php echo esc_html( $product_item_id ); ?></a><br/>
<?php endforeach; ?>
</p>
<?php endif; ?>
<?php endif; ?>
<input name="is_product_page" type="hidden" value="1"/>
<p/>
<a href="#" onclick="fb_reset_product( <?php echo esc_js( $post->ID ); ?> )">
<?php echo esc_html__( 'Reset Facebook metadata', 'facebook-for-woocommerce' ); ?>
</a>
<p/>
<a href="#" onclick="fb_delete_product( <?php echo esc_js( $post->ID ); ?> )">
<?php echo esc_html__( 'Delete product(s) on Facebook', 'facebook-for-woocommerce' ); ?>
</a>
<?php
} else {
?>
<b><?php echo esc_html__( 'This product is not yet synced to Facebook.', 'facebook-for-woocommerce' ); ?></b>
<?php
}
?>
</span>
<?php
}
/**
* Gets a list of Product Item IDs indexed by the ID of the variation.
*
* @since 2.0.0
*
* @param string $product_group_id product group ID
* @return array
*/
private function get_variation_product_item_ids( $product, $product_group_id ) {
$product_item_ids_by_variation_id = [];
$missing_product_item_ids = [];
// get the product item IDs from meta data and build a list of variations that don't have a product item ID stored
foreach ( $product->get_children() as $variation_id ) {
if ( $variation = wc_get_product( $variation_id ) ) {
if ( $product_item_id = $variation->get_meta( self::FB_PRODUCT_ITEM_ID ) ) {
$product_item_ids_by_variation_id[ $variation_id ] = $product_item_id;
} else {
$retailer_id = WC_Facebookcommerce_Utils::get_fb_retailer_id( $variation );
$missing_product_item_ids[ $retailer_id ] = $variation;
$product_item_ids_by_variation_id[ $variation_id ] = null;
}
}
}
// use the Graph API to try to find and store the product item IDs for variations that don't have a value yet
if ( $missing_product_item_ids ) {
$product_item_ids = $this->find_variation_product_item_ids( $product_group_id );
foreach ( $missing_product_item_ids as $retailer_id => $variation ) {
if ( isset( $product_item_ids[ $retailer_id ] ) ) {
$variation->update_meta_data( self::FB_PRODUCT_ITEM_ID, $product_item_ids[ $retailer_id ] );
$variation->save_meta_data();
$product_item_ids_by_variation_id[ $variation->get_id() ] = $product_item_ids[ $retailer_id ];
}
}
}
return $product_item_ids_by_variation_id;
}
/**
* Uses the Graph API to return a list of Product Item IDs indexed by the variation's retailer ID.
*
* @since 2.0.0
*
* @param string $product_group_id product group ID
* @return array
*/
private function find_variation_product_item_ids( $product_group_id ) {
$product_item_ids = [];
try {
$response = facebook_for_woocommerce()->get_api()->get_product_group_products( $product_group_id );
do {
$product_item_ids = array_merge( $product_item_ids, $response->get_ids() );
// get up to two additional pages of results
} while ( $response = facebook_for_woocommerce()->get_api()->next( $response, 2 ) );
} catch ( Framework\SV_WC_API_Exception $e ) {
$message = sprintf( 'There was an error trying to find the IDs for Product Items in the Product Group %s: %s', $product_group_id, $e->getMessage() );
facebook_for_woocommerce()->log( $message );
}
return $product_item_ids;
}
/**
* Gets the total of published products.
*
* @return int
*/
public function get_product_count() {
$args = [
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'fields' => 'ids',
];
$products = new WP_Query( $args );
wp_reset_postdata();
return $products->found_posts;
}
/**
* Load DIA specific JS Data
*/
public function load_assets() {
$ajax_data = array(
'nonce' => wp_create_nonce( 'wc_facebook_infobanner_jsx' ),
);
// load banner assets
wp_enqueue_script(
'wc_facebook_infobanner_jsx',
plugins_url(
'/assets/js/facebook-infobanner.min.js',
__FILE__
),
[],
\WC_Facebookcommerce::PLUGIN_VERSION
);
wp_localize_script(
'wc_facebook_infobanner_jsx',
'wc_facebook_infobanner_jsx',
$ajax_data
);
wp_enqueue_style(
'wc_facebook_infobanner_css',
plugins_url(
'/assets/css/facebook-infobanner.css',
__FILE__
),
[],
\WC_Facebookcommerce::PLUGIN_VERSION
);
if ( ! facebook_for_woocommerce()->is_plugin_settings() ) {
return;
}
?>
<script>
window.facebookAdsToolboxConfig = {
hasGzipSupport: '<?php echo extension_loaded( 'zlib' ) ? 'true' : 'false'; ?>',
enabledPlugins: ['MESSENGER_CHAT','INSTAGRAM_SHOP', 'PAGE_SHOP'],
enableSubscription: '<?php echo class_exists( 'WC_Subscriptions' ) ? 'true' : 'false'; ?>',
popupOrigin: '<?php echo isset( $_GET['url'] ) ? esc_js( sanitize_text_field( wp_unslash( $_GET['url'] ) ) ) : 'https://www.facebook.com/'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?>',
feedWasDisabled: 'true',
platform: 'WooCommerce',
pixel: {
pixelId: '<?php echo $this->get_facebook_pixel_id() ? esc_js( $this->get_facebook_pixel_id() ) : ''; ?>',
advanced_matching_supported: true
},
diaSettingId: '<?php echo $this->get_external_merchant_settings_id() ? esc_js( $this->get_external_merchant_settings_id() ) : ''; ?>',
store: {
baseUrl: window.location.protocol + '//' + window.location.host,
baseCurrency:'<?php echo esc_js( WC_Admin_Settings::get_option( 'woocommerce_currency' ) ); ?>',
timezoneId: '<?php echo esc_js( date( 'Z' ) ); ?>',
storeName: '<?php echo esc_js( WC_Facebookcommerce_Utils::get_store_name() ); ?>',
version: '<?php echo esc_js( WC()->version ) ; ?>',
php_version: '<?php echo PHP_VERSION; ?>',
plugin_version: '<?php echo esc_js( WC_Facebookcommerce_Utils::PLUGIN_VERSION ); ?>'
},
feed: {
totalVisibleProducts: '<?php echo esc_js( $this->get_product_count() ); ?>',
hasClientSideFeedUpload: '<?php echo esc_js( ! ! $this->get_feed_id() ); ?>',
enabled: true,
format: 'csv'
},
feedPrepared: {
feedUrl: '<?php echo esc_url_raw( Feed::get_feed_data_url() ); ?>',
feedPingUrl: '',
feedMigrated: <?php echo $this->is_feed_migrated() ? 'true' : 'false'; ?>,
samples: <?php echo $this->get_sample_product_feed(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
},
};
</script>
<?php
$ajax_data = [
'nonce' => wp_create_nonce( 'wc_facebook_settings_jsx' ),
];
wp_localize_script(
'wc_facebook_settings_jsx',
'wc_facebook_settings_jsx',
$ajax_data
);
wp_enqueue_style(
'wc_facebook_css',
plugins_url(
'/assets/css/facebook.css',
__FILE__
),
[],
\WC_Facebookcommerce::PLUGIN_VERSION
);
}
/**
* Gets the IDs of products marked for deletion from Facebook when removed from Sync.
*
* @internal
*
* @since 2.3.0
*
* @return array
*/
private function get_removed_from_sync_products_to_delete() {
$posted_products = Framework\SV_WC_Helper::get_posted_value( WC_Facebook_Product::FB_REMOVE_FROM_SYNC );
if ( empty( $posted_products ) ) {
return [];
}
return array_map( 'absint', explode( ',', $posted_products ) );
}
/**
* Checks the product type and calls the corresponding on publish method.
*
* @internal
*
* @since 1.10.0
*
* @param int $wp_id post ID
*/
public function on_product_save( $wp_id ) {
$product = wc_get_product( $wp_id );
if ( ! $product ) {
return;
}
$sync_mode = isset( $_POST['wc_facebook_sync_mode'] ) ? $_POST['wc_facebook_sync_mode'] : Admin::SYNC_MODE_SYNC_DISABLED;
$sync_enabled = Admin::SYNC_MODE_SYNC_DISABLED !== $sync_mode;
if ( Admin::SYNC_MODE_SYNC_AND_SHOW === $sync_mode && $product->is_virtual() ) {
// force to Sync and hide
$sync_mode = Admin::SYNC_MODE_SYNC_AND_HIDE;
}
$products_to_delete_from_facebook = $this->get_removed_from_sync_products_to_delete();
if ( $product->is_type( 'variable' ) ) {
// check variations for deletion
foreach ( $products_to_delete_from_facebook as $delete_product_id ) {
$delete_product = wc_get_product( $delete_product_id );
if ( empty( $delete_product ) ) {
continue;
}
if ( Products::is_sync_enabled_for_product( $delete_product ) ) {
continue;
}
$this->delete_fb_product( $delete_product );
}
} else {
if ( $sync_enabled ) {
Products::enable_sync_for_products( [ $product ] );
Products::set_product_visibility( $product, Admin::SYNC_MODE_SYNC_AND_HIDE !== $sync_mode );
$this->save_product_settings( $product );
} else {
// if previously enabled, add a notice on the next page load
if ( Products::is_sync_enabled_for_product( $product ) ) {
Admin::add_product_disabled_sync_notice();
}
Products::disable_sync_for_products( [ $product ] );
if ( in_array( $wp_id, $products_to_delete_from_facebook, true ) ) {
$this->delete_fb_product( $product );
}
}
}
if ( $sync_enabled ) {
Admin\Products::save_commerce_fields( $product );
switch ( $product->get_type() ) {
case 'simple':
case 'booking':
case 'external':
case 'composite':
$this->on_simple_product_publish( $wp_id );
break;