-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatafeedr-woocommerce-importer.php
2112 lines (1772 loc) · 64.6 KB
/
datafeedr-woocommerce-importer.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
/*
Plugin Name: Datafeedr WooCommerce Importer
Plugin URI: https://www.datafeedr.com
Description: Import products from the Datafeedr Product Sets plugin into your WooCommerce store. <strong>REQUIRES: </strong><a href="http://wordpress.org/plugins/datafeedr-api/">Datafeedr API plugin</a>, <a href="http://wordpress.org/plugins/datafeedr-product-sets/">Datafeedr Product Sets plugin</a>, <a href="http://wordpress.org/plugins/woocommerce/">WooCommerce</a> (v3.0+).
Author: datafeedr.com
Author URI: https://www.datafeedr.com
Text Domain: dfrpswc_integration
License: GPL v3
Requires PHP: 7.4
Requires at least: 3.8
Tested up to: 6.7-RC4
Version: 1.3.10
WC requires at least: 3.0
WC tested up to: 9.0
Datafeedr WooCommerce Importer plugin
Copyright (C) 2024, Datafeedr - help@datafeedr.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Exit if accessed directly
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Define constants.
*/
define( 'DFRPSWC_VERSION', '1.3.10' );
define( 'DFRPSWC_DB_VERSION', '1.2.0' );
define( 'DFRPSWC_URL', plugin_dir_url( __FILE__ ) );
define( 'DFRPSWC_PATH', plugin_dir_path( __FILE__ ) );
define( 'DFRPSWC_BASENAME', plugin_basename( __FILE__ ) );
define( 'DFRPSWC_DOMAIN', 'dfrpswc_integration' );
define( 'DFRPSWC_POST_TYPE', 'product' );
define( 'DFRPSWC_TAXONOMY', 'product_cat' );
define( 'DFRPSWC_CONTACT', 'https://datafeedr.me/contact' );
/**
* Load upgrade file.
*/
require_once dirname( __FILE__ ) . '/functions.php';
require_once dirname( __FILE__ ) . '/upgrade.php';
require_once dirname( __FILE__ ) . '/classes/plugin-dependency.php';
require_once dirname( __FILE__ ) . '/classes/attribute-importer.php';
require_once dirname( __FILE__ ) . '/classes/product-update-handler.php';
require_once dirname( __FILE__ ) . '/actions.php';
require_once dirname( __FILE__ ) . '/filters.php';
/**
* Declaring WooCommerce HPOS compatibility.
*
* @see https://github.com/woocommerce/woocommerce/wiki/High-Performance-Order-Storage-Upgrade-Recipe-Book
*
* @since 1.3.8
*/
add_action( 'before_woocommerce_init', function () {
if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
}
} );
/**
* Compatibility Check.
*
* @param bool $network_wide
*
* @return void
*/
function dfrpswc_register_activation( bool $network_wide ) {
// Check that minimum WordPress requirement has been met.
$version = get_bloginfo( 'version' );
if ( version_compare( $version, '3.8', '<' ) ) {
deactivate_plugins( DFRPSWC_BASENAME );
wp_die( __(
'The Datafeedr WooCommerce Importer Plugin could not be activated because it requires WordPress version 3.8 or greater. Please upgrade your installation of WordPress.',
'dfrpswc_integration'
) );
}
// Check that plugin is not being activated at the Network level on Multisite sites.
if ( $network_wide && is_multisite() ) {
deactivate_plugins( DFRPSWC_BASENAME );
wp_die( __(
'The Datafeedr WooCommerce Importer Plugin cannot be activated at the Network-level. Please activate the Datafeedr WooCommerce Importer Plugin at the Site-level instead.',
'dfrpswc_integration'
) );
}
}
register_activation_hook( __FILE__, 'dfrpswc_register_activation' );
/*******************************************************************
* ADMIN NOTICES
*******************************************************************/
/**
* Display admin notices for each required plugin that needs to be
* installed, activated and/or updated.
*
* @since 1.2.17
*/
function dfrpswc_admin_notice_plugin_dependencies() {
$dependencies = [
new Dfrpswc_Plugin_Dependency(
'Datafeedr API',
'datafeedr-api/datafeedr-api.php',
'1.0.75'
),
new Dfrpswc_Plugin_Dependency(
'Datafeedr Product Sets',
'datafeedr-product-sets/datafeedr-product-sets.php',
'1.2.24'
),
new Dfrpswc_Plugin_Dependency(
'WooCommerce',
'woocommerce/woocommerce.php',
'3.0'
),
];
foreach ( $dependencies as $dependency ) {
$action = $dependency->action_required();
if ( ! $action ) {
continue;
}
echo '<div class="notice notice-error"><p>';
echo $dependency->msg( 'Datafeedr WooCommerce Importer' );
echo $dependency->link();
echo '</p></div>';
}
}
add_action( 'admin_notices', 'dfrpswc_admin_notice_plugin_dependencies' );
/**
* Display admin notices upon update.
*/
function dfrpswc_settings_updated() {
if ( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] == true && isset( $_GET['page'] ) && 'dfrpswc_options' == $_GET['page'] ) {
echo '<div class="updated">';
_e( 'Configuration successfully updated!', DFRPSWC_DOMAIN );
echo '</div>';
}
}
add_action( 'admin_notices', 'dfrpswc_settings_updated' );
/**
* Notify user that their version of DFRPSWC is not compatible with their version of DFRPS.
*/
function dfrpswc_not_compatible_with_dfrps() {
if ( defined( 'DFRPS_VERSION' ) ) {
if ( version_compare( DFRPS_VERSION, '1.2.0', '<' ) ) {
// Disable updates!
$dfrps_configuration = get_option( 'dfrps_configuration' );
$dfrps_configuration['updates_enabled'] = 'disabled';
update_option( 'dfrps_configuration', $dfrps_configuration );
$file = 'datafeedr-product-sets/datafeedr-product-sets.php';
$url = wp_nonce_url(
self_admin_url( 'update.php?action=upgrade-plugin&plugin=' ) . $file,
'upgrade-plugin_' . $file
);
?>
<div class="error">
<p>
<strong style="color:#E44532;"><?php _e( 'URGENT - ACTION REQUIRED!', DFRPSWC_DOMAIN ); ?></strong>
<br/>
<?php
_e(
'Your version of the <strong><em>Datafeedr Product Sets</em></strong> plugin is not compatible with your version of the <strong><em>Datafeedr WooCommerce Importer</em></strong> plugin.',
'dfrpswc_integration'
);
?>
<br/>
<?php
_e( 'Failure to upgrade will result in data loss. Please update your version of the <strong><em>Datafeedr Product Sets</em></strong> plugin now.',
'dfrpswc_integration'
);
?>
<br/>
<a class="button button-primary button-large" style="margin-top: 6px" href="<?php echo $url; ?>">
<?php _e( 'Update Now', 'dfrpswc_integration' ); ?>
</a>
</p>
</div>
<?php
}
}
}
add_action( 'admin_notices', 'dfrpswc_not_compatible_with_dfrps' );
/*******************************************************************
* REGISTER CUSTOM POST TYPE FOR PRODUCT SETS
*******************************************************************/
/**
* This registers the third party integration's Custom
* Post Type with the Datafeedr Product Sets plugin.
*/
add_action( 'init', 'dfrpswc_register_cpt' );
function dfrpswc_register_cpt() {
if ( function_exists( 'dfrps_register_cpt' ) ) {
$args = array(
'taxonomy' => DFRPSWC_TAXONOMY,
'name' => _x( 'WooCommerce Products', 'dfrpswc_integration' ),
'tax_name' => _x( 'WooCommerce Categories', 'dfrpswc_integration' ),
'tax_instructions' => _x( 'Add this Product Set to a Product Category.', 'dfrpswc_integration' ),
);
dfrps_register_cpt( DFRPSWC_POST_TYPE, $args );
}
}
/**
* This unregisters the third party integration's Custom
* Post Type from the Datafeedr Product Sets plugin. This
* must be unregistered using the register_deactivation_hook()
* hook.
*/
register_deactivation_hook( __FILE__, 'dfrpswc_unregister_cpt' );
function dfrpswc_unregister_cpt() {
if ( function_exists( 'dfrps_unregister_cpt' ) ) {
dfrps_unregister_cpt( DFRPSWC_POST_TYPE );
}
}
/*******************************************************************
* BUILD ADMIN OPTIONS PAGE
*******************************************************************/
/**
* Add settings page.
*/
add_action( 'admin_menu', 'dfrpswc_admin_menu', 999 );
function dfrpswc_admin_menu() {
add_submenu_page(
'dfrps',
__( 'Options — Datafeedr WooCommerce Importer', DFRPSWC_DOMAIN ),
__( 'WC Importer', DFRPSWC_DOMAIN ),
'manage_options',
'dfrpswc_options',
'dfrpswc_options_output'
);
}
/**
* Get current options or set default ones.
*
* @return array
*/
function dfrpswc_get_options() {
return array_merge( dfrpswc_get_default_options(), get_option( 'dfrpswc_options', array() ) );
}
function dfrpswc_get_option( string $key, $default = '' ) {
$options = dfrpswc_get_options();
return $options[ $key ] ?? $default;
}
/**
* @return array
*/
function dfrpswc_get_default_options(): array {
return [
'button_text' => 'Buy Now',
'format_price' => 'no',
'rel_single' => 'nofollow',
'rel_loop' => 'nofollow',
'target_single' => '_blank',
'target_loop' => '_blank',
'display_sku' => 'yes',
];
}
/**
* Build settings page.
*/
function dfrpswc_options_output() {
echo '<div class="wrap" id="dfrpswc_options">';
echo '<h2>' . __( 'Options — Datafeedr WooCommerce Importer', DFRPSWC_DOMAIN ) . '</h2>';
echo '<form method="post" action="options.php">';
wp_nonce_field( 'dfrpswc-update-options' );
settings_fields( 'dfrpswc_options-page' );
do_settings_sections( 'dfrpswc_options-page' );
submit_button();
echo '</form>';
echo '</div>';
}
/**
* Register settings.
*/
add_action( 'admin_init', 'dfrpswc_register_settings' );
function dfrpswc_register_settings() {
register_setting( 'dfrpswc_options-page', 'dfrpswc_options', 'dfrpswc_validate' );
add_settings_section(
'dfrpswc_general_settings',
__( 'General Settings', 'dfrpswc_integration' ),
'dfrpswc_general_settings_section',
'dfrpswc_options-page'
);
add_settings_section(
'dfrpswc_button_settings',
__( 'Buy Button Settings', 'dfrpswc_integration' ),
'dfrpswc_button_settings_section',
'dfrpswc_options-page'
);
add_settings_field(
'dfrpswc_format_price',
__( 'Format Prices', 'dfrpswc_integration' ),
'dfrpswc_format_price_field',
'dfrpswc_options-page',
'dfrpswc_general_settings'
);
add_settings_field(
'dfrpswc_display_sku',
__( 'Display SKU', 'dfrpswc_integration' ),
'dfrpswc_display_sku_field',
'dfrpswc_options-page',
'dfrpswc_general_settings'
);
add_settings_field(
'dfrpswc_button_text',
__( 'Button Text', 'dfrpswc_integration' ),
'dfrpswc_button_text_field',
'dfrpswc_options-page',
'dfrpswc_button_settings'
);
add_settings_field(
'dfrpswc_rel_loop',
__( 'Loop Page <code>rel</code>', 'dfrpswc_integration' ),
'dfrpswc_format_rel_loop_field',
'dfrpswc_options-page',
'dfrpswc_button_settings'
);
add_settings_field(
'dfrpswc_rel_single',
__( 'Single Product Page <code>rel</code>', 'dfrpswc_integration' ),
'dfrpswc_format_rel_single_field',
'dfrpswc_options-page',
'dfrpswc_button_settings'
);
add_settings_field(
'dfrpswc_target_loop',
__( 'Loop Page <code>target</code>', 'dfrpswc_integration' ),
'dfrpswc_format_target_loop_field',
'dfrpswc_options-page',
'dfrpswc_button_settings'
);
add_settings_field(
'dfrpswc_target_single',
__( 'Single Product Page <code>target</code>', 'dfrpswc_integration' ),
'dfrpswc_format_target_single_field',
'dfrpswc_options-page',
'dfrpswc_button_settings'
);
}
/**
* General settings section description.
*/
function dfrpswc_general_settings_section() {
//echo __( 'General settings for importing products into your WooCommerce store.', DFRPSWC_DOMAIN );
}
function dfrpswc_button_settings_section() {
//echo __( 'General settings for importing products into your WooCommerce store.', DFRPSWC_DOMAIN );
}
/**
* Button Text field.
*/
function dfrpswc_button_text_field() {
$options = dfrpswc_get_options();
echo '<input type="text" class="regular-text" name="dfrpswc_options[button_text]" value="' . esc_attr( $options['button_text'] ) . '" />';
echo '<p class="description">';
echo __( 'The text on the button which links to the merchant\'s website.', 'dfrpswc_integration' );
echo '</p>';
}
/**
* Format Price field.
*/
function dfrpswc_format_price_field() {
$options = dfrpswc_get_options();
$value = $options['format_price'];
$yes = 'yes';
$no = 'no';
$selected_yes = checked( $yes, $value, false );
$selected_no = checked( $no, $value, false );
echo '<input type="radio" class="regular-text" name="dfrpswc_options[format_price]" value="' . esc_attr( $yes ) . '" ' . $selected_yes . ' /> ' . ucfirst( $yes );
echo ' <input type="radio" class="regular-text" name="dfrpswc_options[format_price]" value="' . esc_attr( $no ) . '" ' . $selected_no . ' /> ' . ucfirst( $no );
echo '<p class="description">';
echo __( 'Select "Yes" if you want to override the WooCommerce price formatter to format the price based on the product\'s currency code.', 'dfrpswc_integration' );
echo '<br/>';
echo __( 'Select "No" to let WooCommerce handle the formatting of prices.', 'dfrpswc_integration' );
echo '<br/>';
echo __( 'If you import products with 2 or more different currencies (ie. USD and CAD), set this to "Yes".', 'dfrpswc_integration' );
echo '</p>';
}
/**
* Display SKU field.
*/
function dfrpswc_display_sku_field(): void {
$options = dfrpswc_get_options();
$value = $options['display_sku'];
$yes = 'yes';
$no = 'no';
$selected_yes = checked( $yes, $value, false );
$selected_no = checked( $no, $value, false );
echo '<input type="radio" class="regular-text" name="dfrpswc_options[display_sku]" value="' . esc_attr( $yes ) . '" ' . $selected_yes . ' /> ' . ucfirst( $yes );
echo ' <input type="radio" class="regular-text" name="dfrpswc_options[display_sku]" value="' . esc_attr( $no ) . '" ' . $selected_no . ' /> ' . ucfirst( $no );
echo '<p class="description">';
esc_html_e( 'By default, Datafeedr\'s unique product ID will appear on your single product pages.', 'dfrpswc_integration' );
echo '<br/>';
echo __( '- Select "Yes" to continue displaying the product ID. (default)', 'dfrpswc_integration' );
echo '<br/>';
echo __( '- Select "No" to hide the SKU.', 'dfrpswc_integration' );
echo '<br/>';
printf( '<a href="%s" target="_blank" rel="noopener">%s</a>',
'https://datafeedrapi.helpscoutdocs.com/article/164-remove-sku-from-single-product-page',
esc_html__( 'Learn More', 'dfrpswc_integration' )
);
echo '</p>';
}
/**
* Set the rel attribute for the buy button on the single product page.
*/
function dfrpswc_format_rel_single_field() {
$options = dfrpswc_get_options();
echo '<input type="text" class="regular-text" name="dfrpswc_options[rel_single]" value="' . esc_attr( $options['rel_single'] ) . '" />';
echo '<p class="description">';
echo __( 'Set the <code>rel</code> attribute for the buy button on the single product page.', 'dfrpswc_integration' );
echo '</p>';
}
/**
* Set the rel attribute for the buy button on the loop pages (shop, category, archive, etc...)
*/
function dfrpswc_format_rel_loop_field() {
$options = dfrpswc_get_options();
echo '<input type="text" class="regular-text" name="dfrpswc_options[rel_loop]" value="' . esc_attr( $options['rel_loop'] ) . '" />';
echo '<p class="description">';
echo __( 'Set the <code>rel</code> attribute for the buy button on the loop pages (shop, category, archive, etc...).', 'dfrpswc_integration' );
echo '</p>';
}
/**
* Set the target attribute for the buy button on the single product page.
*/
function dfrpswc_format_target_single_field() {
$options = dfrpswc_get_options();
echo '<input type="text" class="regular-text" name="dfrpswc_options[target_single]" value="' . esc_attr( $options['target_single'] ) . '" />';
echo '<p class="description">';
echo __( 'Set the <code>target</code> attribute for the buy button on the single product page. <a href="https://www.w3schools.com/tags/att_a_target.asp" target="_blank" rel="noopener">Valid Options</a>.', 'dfrpswc_integration' );
echo '</p>';
}
/**
* Set the target attribute for the buy button on the loop pages (shop, category, archive, etc...)
*/
function dfrpswc_format_target_loop_field() {
$options = dfrpswc_get_options();
echo '<input type="text" class="regular-text" name="dfrpswc_options[target_loop]" value="' . esc_attr( $options['target_loop'] ) . '" />';
echo '<p class="description">';
echo __( 'Set the <code>target</code> attribute for the buy button on the loop pages (shop, category, archive, etc...). <a href="https://www.w3schools.com/tags/att_a_target.asp" target="_blank" rel="noopener">Valid Options</a>.', 'dfrpswc_integration' );
echo '</p>';
}
/**
* Validate user's input and save.
*/
function dfrpswc_validate( $input ) {
if ( ! isset( $input ) || ! is_array( $input ) || empty( $input ) ) {
return $input;
}
$new_input = array();
foreach ( $input as $key => $value ) {
if ( $key === 'button_text' ) {
$new_input['button_text'] = trim( $value );
}
if ( $key === 'format_price' ) {
$new_input['format_price'] = $value === 'yes' ? 'yes' : 'no';
}
if ( $key === 'display_sku' ) {
$new_input['display_sku'] = in_array( $value, [ 'yes', 'no' ], true ) ? $value : 'yes';
}
if ( $key === 'rel_single' ) {
$rel_single = trim( $value );
$new_input['rel_single'] = ! empty( $rel_single ) ? $rel_single : 'nofollow';
}
if ( $key === 'rel_loop' ) {
$rel_loop = trim( $value );
$new_input['rel_loop'] = ! empty( $rel_loop ) ? $rel_loop : 'nofollow';
}
if ( $key === 'target_single' ) {
$target_single = trim( $value );
$new_input['target_single'] = ! empty( $target_single ) ? $target_single : '_blank';
}
if ( $key === 'target_loop' ) {
$target_loop = trim( $value );
$new_input['target_loop'] = ! empty( $target_loop ) ? $target_loop : '_blank';
}
}
return $new_input;
}
/**
* Change Button Text for DFRPSWC imported products.
*
* @param string $button_text
* @param WC_Product $product
*
* @return string
*/
function dfrpswc_single_add_to_cart_text( $button_text, $product ) {
if ( $product->get_type() != 'external' ) {
return $button_text;
}
if ( ! dfrpswc_is_dfrpswc_product( $product->get_id() ) ) {
return $button_text;
}
$options = dfrpswc_get_options();
if ( $options['button_text'] != '' ) {
$button_text = $options['button_text'];
}
return $button_text;
}
add_filter( 'woocommerce_product_add_to_cart_text', 'dfrpswc_single_add_to_cart_text', 10, 2 );
add_filter( 'woocommerce_product_single_add_to_cart_text', 'dfrpswc_single_add_to_cart_text', 10, 2 );
/*******************************************************************
* UPDATE FUNCTIONS
*******************************************************************/
/**
*
* This unsets products from their categories before updating products.
*
* Why?
*
* We need to remove all products which were imported via a product set
* from the categories they were added to when they were imported
* so that at the end of the update, if these products weren't re-imported
* during the update, the post/product's category information (for this
* set) will no longer be available so that if this post/product was
* added via another Product Set, only that Product Set's category IDs
* will be attributed to this post/product.
*
* This processes batches at a time as this is a server/time
* intensive process.
*
* @param object $obj This is the entire "Update" object from Dfrps_Update().
*/
add_action( 'dfrps_preprocess-' . DFRPSWC_POST_TYPE, 'dfrpswc_unset_post_categories' );
function dfrpswc_unset_post_categories( $obj ) {
global $wpdb;
/**
* Here is the process of this function:
*
* 1. Check if 'dfrpswc_temp_post_ids_by_set_id' table exists. If table does not exist, that means
* we have not run the 'dfrpswc_unset_post_categories' action yet.
* 2. If table does not exist:
* - Create temp table.
* - Insert post IDs into table.
* 3. Get $config['preprocess_maximum'] ($limit) number of records.
* 4. Loop through records and process them.
* 5. Delete those ($limit) number of records from table.
* 6. If 0 records remain to be processed:
* - Update_post_meta( $obj->set['ID'], '_dfrps_preprocess_complete_' . DFRPSWC_POST_TYPE, true );
* - Delete temp table.
*/
/**
* Check if 'dfrpswc_temp_post_ids_by_set_id' table exists. If it does not exist:
* - Create the table.
* - Query all post IDs for this Set ID.
* - Insert all post IDs into table.
*/
$table_name = $wpdb->prefix . 'dfrpswc_temp_post_ids_by_set_id';
$query = $wpdb->prepare( "SHOW TABLES LIKE %s", $table_name );
if ( $wpdb->get_var( $query ) != $table_name ) {
// Create the temp table to store the post IDs.
dfrpswc_create_temp_post_ids_table( $table_name );
// Get all post IDs (as an array) set by this Product Set
$ids = dfrps_get_all_post_ids_by_set_id( $obj->set['ID'] );
dfrpswc_insert_ids_into_temp_table( $ids, $table_name );
}
/**
* Get X ($limit) number of records to process where X is $config['preprocess_maximum'].
* Also get the $total number of posts in this table. This will be used to
* determine if we must repeat the 'dfrps_preprocess' action or not.
*
* The uniqid() part is related to ticket #10866 .
*/
$config = (array) get_option( 'dfrps_configuration' );
$limit = ( isset( $config['preprocess_maximum'] ) ) ? intval( $config['preprocess_maximum'] ) : 100;
$uid = uniqid();
$wpdb->query( "UPDATE $table_name SET uid='$uid' WHERE uid='' ORDER BY post_id ASC LIMIT " . $limit );
$sql = "SELECT post_id FROM $table_name WHERE uid='$uid' ORDER BY post_id ASC";
$posts = $wpdb->get_results( $sql, OBJECT );
/**
* If $posts is empty, then:
* - Set _dfrps_preprocess_complete_ to true.
* - DROP the new table.
* - return.
*/
if ( ! $posts ) {
update_post_meta( $obj->set['ID'], '_dfrps_preprocess_complete_' . DFRPSWC_POST_TYPE, true );
dfrpswc_drop_temp_post_ids_table( $table_name );
return true;
}
/**
* If $posts contains post IDs, we will grab the first X ($limit) number of
* IDs from the array (where X is "preprocess_maximum") and get all
* term_ids that the product is associated with from other Product Sets.
*
* Then we will have an array of term_ids that this product belongs to
* except the term_ids that this Product Set is responsible for adding.
*
* Why?
*
* Let's say we have the following situation:
*
* SET A adds PRODUCT 1 to CATEGORY X
* SET B adds PRODUCT 1 to CATEGORY X
*
* What happens when SET A removes PRODUCT 1 from CATEGORY X?
*
* We need to make sure that PRODUCT 1 remains in CATEGORY X. By getting
* term_ids from all other Sets that added this product, we will keep
* PRODUCT 1 in CATEGORY X.
*/
foreach ( $posts as $post ) {
$post_id = intval( $post->post_id );
wp_remove_object_terms( $post_id, dfrps_get_cpt_terms( $obj->set['ID'] ), DFRPSWC_TAXONOMY );
delete_post_meta( $post_id, '_dfrps_product_set_id', $obj->set['ID'] );
}
/**
* Now we delete this set of post IDs from the table. This ensures
* that we don't process them again.
*/
$wpdb->query( "DELETE FROM $table_name WHERE uid='$uid'" );
}
/**
* Adds the action "dfrps_action_do_products_{cpt}" where
* {cpt} is the post_type you are inserting products into.
*/
add_action( 'dfrps_action_do_products_' . DFRPSWC_POST_TYPE, 'dfrpswc_do_products', 10, 2 );
function dfrpswc_do_products( $data, $set ) {
// Check if there are products available.
if ( ! isset( $data['products'] ) || empty( $data['products'] ) ) {
return;
}
// Loop thru products.
foreach ( $data['products'] as $product ) {
if ( dfrpswc_feature_flag_is_enabled( 'product_update_handler' ) ) {
dfrpswc_upsert_product( $product, $set );
continue;
}
// Get post if it already exists.
$existing_post = dfrps_get_existing_post( $product, $set );
// If $existing_post equals "skip", that means the product has been imported but attempts to query it return false because of a race condition.
if ( $existing_post === 'skip' ) {
continue;
}
// Disable W3TC's caching while processing products.
add_filter( 'w3tc_flushable_post', '__return_false', 20, 3 );
// Determine what to do based on if post exists or not.
if ( $existing_post && $existing_post['post_type'] == DFRPSWC_POST_TYPE ) {
$action = 'update';
$post = dfrpswc_update_post( $existing_post, $product, $set, $action );
} else {
$action = 'insert';
$post = dfrpswc_insert_post( $product, $set, $action );
}
// Handle other facets for this product such as postmeta, terms and attributes.
if ( $post ) {
dfrpswc_update_postmeta( $post, $product, $set, $action );
dfrpswc_update_terms( $post, $product, $set, $action );
dfrpswc_update_attributes( $post, $product, $set, $action );
do_action( 'dfrpswc_do_product', $post, $product, $set, $action );
}
}
}
/**
* This updates a post.
*
* This should return a FULL $post object in ARRAY_A format.
*/
function dfrpswc_update_post( $existing_post, $product, $set, $action ) {
$post = array(
'ID' => $existing_post['ID'],
'post_title' => isset( $product['name'] ) ? $product['name'] : '',
'post_content' => isset( $product['description'] ) ? $product['description'] : '',
'post_excerpt' => isset( $product['shortdescription'] ) ? $product['shortdescription'] : '',
'post_status' => 'publish',
);
/**
* Allow the $post array to be modified before updating.
*
* Hook into this filter to change any $post related information before it's updated.
* Useful for changing the post_status of a product or modifying its name
* or description before persisting.
*
* @param array $post Array containing WordPress Post information.
* @param array $product Array containing Datafeedr Product information.
* @param array $set Array containing Product Set information.
* @param string $action Either "update" or "insert" depending on what the Product Set is doing.
*
* @since 0.9.3
*
*/
$post = apply_filters( 'dfrpswc_filter_post_array', $post, $product, $set, $action );
wp_update_post( $post );
return $post;
}
/**
* This inserts a new post.
*
* This should return a FULL $post object in ARRAY_A format.
*/
function dfrpswc_insert_post( $product, $set, $action ) {
$post = array(
'post_title' => isset( $product['name'] ) ? $product['name'] : '',
'post_content' => isset( $product['description'] ) ? $product['description'] : '',
'post_excerpt' => isset( $product['shortdescription'] ) ? $product['shortdescription'] : '',
'post_status' => 'publish',
'post_author' => $set['post_author'],
'post_type' => DFRPSWC_POST_TYPE,
);
/**
* Allow the $post array to be modified before saving.
*
* Hook into this filter to change any $post related information before it's saved.
* Useful for changing the post_status of a product or modifying its name
* or description before persisting.
*
* @param array $post Array containing WordPress Post information.
* @param array $product Array containing Datafeedr Product information.
* @param array $set Array containing Product Set information.
* @param string $action Either "update" or "insert" depending on what the Product Set is doing.
*
* @since 0.9.3
*
*/
$post = apply_filters( 'dfrpswc_filter_post_array', $post, $product, $set, $action );
$id = wp_insert_post( $post );
$post['ID'] = $id;
return $post;
}
/**
* Update the postmeta for this product.
*/
function dfrpswc_update_postmeta( $post, $product, $set, $action ) {
$meta = array();
$meta['_visibility'] = 'visible';
$meta['_stock'] = '';
$meta['_downloadable'] = 'no';
$meta['_virtual'] = 'no';
$meta['_backorders'] = 'no';
$meta['_stock_status'] = 'instock';
$meta['_product_type'] = 'external';
$meta['_product_url'] = $product['url'];
$meta['_sku'] = $product['_id'];
$meta['_dfrps_is_dfrps_product'] = true;
$meta['_dfrps_is_dfrpswc_product'] = true;
$meta['_dfrps_product_id'] = $product['_id'];
$meta['_dfrps_product'] = $product; // This stores all info about the product in 1 array.
// Update image check field.
$meta['_dfrps_product_check_image'] = 1;
// Set featured image url (if there's an image)
if ( @$product['image'] != '' ) {
$meta['_dfrps_featured_image_url'] = @$product['image'];
} elseif ( @$product['thumbnail'] != '' ) {
$meta['_dfrps_featured_image_url'] = @$product['thumbnail'];
}
// Get highest and lowest price for this product.
$highest_price = ( isset( $product['price'] ) ) ? absint( $product['price'] ) : 0;
$lowest_price = ( isset( $product['finalprice'] ) ) ? absint( $product['finalprice'] ) : $highest_price;
// Handle regular price.
if ( $highest_price > 0 ) {
$meta['_regular_price'] = dfrps_int_to_price( $highest_price );
$meta['_price'] = dfrps_int_to_price( $highest_price );
} else {
$meta['_regular_price'] = '';
$meta['_price'] = '';
}
// Handle sale price.
if ( $highest_price > $lowest_price ) {
$meta['_sale_price'] = dfrps_int_to_price( $lowest_price );
$meta['_price'] = dfrps_int_to_price( $lowest_price );
} else {
$meta['_sale_price'] = '';
}
// Handle sale discount.
$meta['_dfrps_salediscount'] = ( isset( $product['salediscount'] ) ) ? $product['salediscount'] : 0;
/**
* Allow the $meta array to be modified before saving/updating.
*
* Hook into this filter to change any postmeta related information before it's saved or updated.
* Useful for modifying pricing information or other product related information.
*
* @param array $meta Array containing postmeta data for this WordPress $post.
* @param array $post Array containing WordPress Post information.
* @param array $product Array containing Datafeedr Product information.
* @param array $set Array containing Product Set information.
* @param string $action Either "update" or "insert" depending on what the Product Set is doing.
*
* @since 0.9.3
*
*/
$meta = apply_filters( 'dfrpswc_filter_postmeta_array', $meta, $post, $product, $set, $action );
foreach ( $meta as $meta_key => $meta_value ) {
update_post_meta( $post['ID'], $meta_key, $meta_value );
}
add_post_meta( $post['ID'], '_dfrps_product_set_id', $set['ID'] );
add_post_meta( $post['ID'], 'total_sales', '0', true );
}
/**
* Update the terms/taxonomy for this product.
*/
function dfrpswc_update_terms( $post, $product, $set, $action ) {
// Get the IDs of the categories this product is associated with.
$terms = dfrpswc_get_all_term_ids_for_product( $post, $set );
// Create an array with key of taxonomy and values of terms
$taxonomies = array(
DFRPSWC_TAXONOMY => $terms,
'product_tag' => '',
'product_type' => 'external',
);
/**
* Allow the $taxonomies array to be modified before saving/updating.
*
* Hook into this filter to change any $taxonomies related information before it's saved or updated.
*
* @param array $taxonomies Array keyed by taxonomy name and having values of taxonomy values.
* @param array $post Array containing WordPress Post information.
* @param array $product Array containing Datafeedr Product information.
* @param array $set Array containing Product Set information.
* @param string $action Either "update" or "insert" depending on what the Product Set is doing.
*
* @since 0.9.3
*
*/
$taxonomies = apply_filters( 'dfrpswc_filter_taxonomy_array', $taxonomies, $post, $product, $set, $action );
// Remove 'product_tag' from array if value is empty.
if ( empty( $taxonomies['product_tag'] ) ) {
unset( $taxonomies['product_tag'] );
}
// Then iterate over the array using wp_set_post_terms()
foreach ( $taxonomies as $taxonomy => $terms ) {
wp_set_post_terms( $post['ID'], $terms, $taxonomy, false );
}
}
/**
* Get all term IDs associated with a specific Product Set.
*
* @param array $post Array containing WordPress Post information.
* @param array $set Array containing Product Set information.
*
* @return array
* @since 1.2.22
*
*/
function dfrpswc_get_all_term_ids_for_product( $post, $set ) {
$terms = [];
// Get all Product Set IDs which added this product. This returns an array of Product Set IDs.
$product_set_ids = get_post_meta( $post['ID'], '_dfrps_product_set_id', false );
if ( ! isset( $product_set_ids ) || empty( $product_set_ids ) ) {
return $terms;
}
foreach ( $product_set_ids as $product_set_id ) {
$terms = array_merge( $terms, dfrps_get_cpt_terms( $product_set_id ) );
}
$terms = array_map( 'intval', $terms ); // Make sure these $terms are integers
$terms = array_unique( $terms );
return $terms;
}