-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjigoshop-software.php
2010 lines (1761 loc) · 76.8 KB
/
jigoshop-software.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: JigoShop - Software Add-On
Plugin URI: https://github.com/jkudish/JigoShop-Software-Add-on/
Description: Extends JigoShop to a full-blown software shop, including license activation, license retrieval, activation e-mails and more
Version: 2.7
Author: Joachim Kudish
Author URI: http://jkudish.com
License: GPL v2
Text Domain: jigoshop-software
*/
/**
* @version 2.6
* @author Joachim Kudish <info@jkudish.com>
* @link http://jkudish.com
* @uses JigoShop @link http://jigoshop.com
* @uses WordPress Github Plugin Updater @link https://github.com/jkudish/WordPress-GitHub-Plugin-Updater
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @copyright Copyright (c) 2011, Joachim Kudish
*
* GNU General Public License, Free Software Foundation
* <http://creativecommons.org/licenses/GPL/2.0/>
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
* defines the constants we need for the plugin
*
* @since 1.3
* @return void
*/
function jgs_define_constants() {
if ( ! defined( 'JIGOSHOP_SOFTWARE_PATH' ) )
define( 'JIGOSHOP_SOFTWARE_PATH', dirname( __FILE__ ) );
if ( ! defined( 'JIGOSHOP_SOFTWARE_SLUG' ) )
define( 'JIGOSHOP_SOFTWARE_SLUG', plugin_basename( __FILE__ ) );
if ( ! defined( 'JIGOSHOP_SOFTWARE_VERSION' ) )
define( 'JIGOSHOP_SOFTWARE_VERSION', 2.5 );
if ( ! defined( 'JIGOSHOP_SOFTWARE_PROPER_NAME' ) )
define( 'JIGOSHOP_SOFTWARE_PROPER_NAME', 'jigoshop-software' );
if ( ! defined( 'JIGOSHOP_SOFTWARE_GITHUB_URL' ) )
define( 'JIGOSHOP_SOFTWARE_GITHUB_URL', 'https://github.com/jkudish/JigoShop-Software-Add-on' );
if ( ! defined( 'JIGOSHOP_SOFTWARE_GITHUB_ZIP_URL' ) )
define( 'JIGOSHOP_SOFTWARE_GITHUB_ZIP_URL', 'https://github.com/jkudish/JigoShop-Software-Add-on/zipball/master' );
if ( ! defined( 'JIGOSHOP_SOFTWARE_GITHUB_API_URL' ) )
define( 'JIGOSHOP_SOFTWARE_GITHUB_API_URL', 'https://api.github.com/repos/jkudish/JigoShop-Software-Add-on' );
if ( ! defined( 'JIGOSHOP_SOFTWARE_GITHUB_RAW_URL' ) )
define( 'JIGOSHOP_SOFTWARE_GITHUB_RAW_URL', 'https://raw.github.com/jkudish/JigoShop-Software-Add-on/master' );
if ( ! defined( 'JIGOSHOP_SOFTWARE_REQUIRES_WP' ) )
define( 'JIGOSHOP_SOFTWARE_REQUIRES_WP', '3.3' );
if ( ! defined( 'JIGOSHOP_SOFTWARE_TESTED_WP' ) )
define( 'JIGOSHOP_SOFTWARE_TESTED_WP', '3.4.2' );
}
if ( ! class_exists( 'Jigoshop_Software' ) ) {
class Jigoshop_Software {
/**
* the product fields
* @var array
*/
public $product_fields;
/**
* the order fields
* @var array
*/
public $order_fields;
/*
* helpers used for the upgrades pages
*/
private $looking_for_upgrades;
private $possible_upgrades_found ;
private $upgrade_error ;
/**
* class constructor
* plugin activation, hooks & filters, etc..
*
* @since 1.0
* @return void
*/
function __construct() {
jgs_define_constants();
/**
* hooks
*/
add_action( 'init', array( $this, 'set_timezone' ) );
add_action( 'init', array( $this, 'load_lang' ) );
// backend stuff
add_action( 'product_write_panel_tabs', array( $this, 'product_write_panel_tab' ) );
add_action( 'product_write_panels', array( $this, 'product_write_panel' ) );
add_filter( 'jigoshop_process_product_meta', array( $this, 'product_save_data' ) );
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
add_action( 'jigoshop_process_shop_order_meta', array( $this, 'order_save_data' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue' ) );
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action( 'wp_ajax_nopriv_jgs_import', array( $this, 'import_ajax' ) );
add_action( 'wp_ajax_jgs_import', array( $this, 'import_ajax' ) );
add_action( 'wp_ajax_nopriv_jgs_do_import', array( $this, 'import' ) );
add_action( 'wp_ajax_jgs_do_import', array( $this, 'import' ) );
add_action( 'get_search_query', array( $this, 'order_get_search_query' ) );
// frontend stuff
remove_action( 'simple_add_to_cart', 'jigoshop_simple_add_to_cart' );
remove_action( 'virtual_add_to_cart', 'jigoshop_simple_add_to_cart' );
remove_action( 'downloadable_add_to_cart', 'jigoshop_downloadable_add_to_cart' );
add_action( 'grouped_add_to_cart', 'jigoshop_grouped_add_to_cart' );
remove_action( 'jigoshop_after_shop_loop_item', 'jigoshop_template_loop_add_to_cart', 10, 2 );
add_action( 'simple_add_to_cart', array( $this, 'add_to_cart' ) );
add_action( 'virtual_add_to_cart', array( $this, 'add_to_cart' ) );
add_action( 'downloadable_add_to_cart', array( $this, 'add_to_cart' ) );
add_action( 'grouped_add_to_cart', array( $this, 'add_to_cart' ) );
add_action( 'jigoshop_after_shop_loop_item', array( $this, 'loop_add_to_cart' ), 10, 2 );
add_action( 'wp_enqueue_scripts', array( $this, 'frontend_enqueue' ) );
add_action( 'wp_head', array( $this, 'redirect_away_from_cart' ) );
add_action( 'wp_ajax_nopriv_jgs_checkout', array( $this, 'ajax_jgs_checkout' ) );
add_action( 'wp_ajax_jgs_checkout', array( $this, 'ajax_jgs_checkout' ) );
add_action( 'wp_ajax_nopriv_jgs_lost_license', array( $this, 'ajax_jgs_lost_license' ) );
add_action( 'wp_ajax_jgs_lost_license', array( $this, 'ajax_jgs_lost_license' ) );
add_action( 'wp_ajax_nopriv_jgs_upgrade', array( $this, 'ajax_jgs_upgrade' ) );
add_action( 'wp_ajax_jgs_upgrade', array( $this, 'ajax_jgs_upgrade' ) );
add_action( 'wp_ajax_nopriv_jgs_activation_subscribe', array( $this, 'ajax_jgs_activation_subscribe' ) );
add_action( 'wp_ajax_jgs_activation_subscribe', array( $this, 'ajax_jgs_activation_subscribe' ) );
add_action( 'wp_ajax_nopriv_jgs_activation_unsubscribe', array( $this, 'ajax_jgs_activation_unsubscribe' ) );
add_action( 'wp_ajax_jgs_activation_unsubscribe', array( $this, 'ajax_jgs_activation_unsubscribe' ) );
// payment stuff
add_action( 'init', array( $this, 'init_actions' ), 1 );
add_action( 'jigoshop_payment_complete', array( $this, 'post_paypal_payment' ) );
add_action( 'order_status_cancelled', array( $this, 'cancel_order' ) );
// email stuff
remove_action( 'order_status_pending_to_processing', 'jigoshop_new_order_notification' );
remove_action( 'order_status_pending_to_completed', 'jigoshop_new_order_notification' );
remove_action( 'order_status_pending_to_on-hold', 'jigoshop_new_order_notification' );
remove_action( 'order_status_completed', 'jigoshop_completed_order_customer_notification' );
remove_action( 'order_status_pending_to_processing', 'jigoshop_processing_order_customer_notification' );
remove_action( 'order_status_pending_to_on-hold', 'jigoshop_processing_order_customer_notification' );
remove_action( 'order_status_completed', 'jigoshop_completed_order_customer_notification' );
add_action( 'order_status_completed', array( $this, 'completed_order' ) );
// filters
add_filter( 'add_to_cart_redirect', array( $this, 'add_to_cart_redirect' ) );
add_filter( 'page_template', array( $this, 'locate_api_template' ), 10, 1 );
}
/**
* set the correct timezone from the WP options
*
* @since 2.4
* @return void
*/
function set_timezone() {
$timezone = get_option( 'timezone_string' );
if ( ! empty( $timezone ) )
date_default_timezone_set( $timezone );
}
/**
* defines the fields used in the plugin
*
* @since 2.1
* @return void
*/
function define_fields() {
// define the product metadata fields used by this plugin
$this->product_fields = array(
array( 'id' => 'is_software', 'label' => __( 'This product is Software', 'jigoshop-software' ), 'title' => __( 'This product is Software', 'jigoshop-software' ), 'placeholder' => '', 'type' => 'checkbox', 'never_hide' => true ),
array( 'id' => 'is_upgrade', 'label' => __( 'This product is solely an upgrade', 'jigoshop-software' ), 'title' => __( 'This product is solely an upgrade', 'jigoshop-software' ), 'placeholder' => '', 'type' => 'checkbox', 'never_hide' => true ),
array( 'id' => 'soft_product_id', 'label' => __( 'Product ID to use for API', 'jigoshop-software' ), 'title' => __( 'Product ID to use for API', 'jigoshop-software' ), 'placeholder' => __( 'ex: PRODUCT1', 'jigoshop-software' ), 'type' => 'text' ),
array( 'id' => 'license_key_prefix', 'label' => __( 'Prefix for License Key', 'jigoshop-software' ), 'title' => __( 'Optional prefix for the license key', 'jigoshop-software' ), 'placeholder' => __( 'ex: SC-', 'jigoshop-software' ), 'type' => 'text' ),
array( 'id' => 'secret_product_key', 'label' => __( 'Secret Product Key to use for API', 'jigoshop-software' ), 'title' => __( 'Secret Product Key to use for API', 'jigoshop-software' ), 'placeholder' => __( 'any random string', 'jigoshop-software' ), 'type' => 'text' ),
array( 'id' => 'version', 'label' => __( 'Version Number', 'jigoshop-software' ), 'title' => __( 'Version Number', 'jigoshop-software' ), 'placeholder' => __( 'ex: 1.0', 'jigoshop-software' ), 'type' => 'text' ),
array( 'id' => 'activations', 'label' => __( 'Amount of activations possible', 'jigoshop-software' ), 'title' => __( 'Amount of activations possible', 'jigoshop-software' ), 'placeholder' => __( 'ex: 5', 'jigoshop-software' ), 'type' => 'text' ),
array( 'id' => 'trial', 'label' => __( 'Trial Period (amount of days or hours)', 'jigoshop-software' ), 'title' => __( 'Trial Period (amount of days or hours)', 'jigoshop-software' ), 'placeholder' => __( 'ex: 15', 'jigoshop-software' ), 'type' => 'text' ),
array( 'id' => 'trial_unit', 'label' => __( 'Trial Units', 'jigoshop-software' ), 'title' => __( 'Trial Units', 'jigoshop-software' ), 'type' => 'select', 'values' => array( 'days' => 'Days', 'hours' => 'Hours' ) ),
array( 'id' => 'upgrade_from', 'label' => __( 'Upgrade from', 'jigoshop-software' ), 'title' => __( 'Upgrade from', 'jigoshop-software' ), 'placeholder' => '', 'type' => 'select', 'values' => $this->get_product_upgrade_dropdown(), 'upgrade_field' => true ),
array( 'id' => 'upgrade_to', 'label' => __( 'Upgrade to', 'jigoshop-software' ), 'title' => __( 'Upgrade to', 'jigoshop-software' ), 'placeholder' => '', 'type' => 'select', 'values' => $this->get_product_upgrade_dropdown(), 'upgrade_field' => true ),
array( 'id' => 'upgrade_date_since', 'label' => __( 'Upgrade Date Threshold', 'jigoshop-software' ), 'title' => __( 'Original purchase must have occurred on or after the following date', 'jigoshop-software' ), 'placeholder' => 'ex: 2012-06-01', 'type' => 'text', 'upgrade_field' => true ),
array( 'id' => 'paypal_name', 'label' => __( 'Paypal Name to show on transaction receipts', 'jigoshop-software' ), 'title' => __( 'Paypal Name to show on transaction receipts', 'jigoshop-software' ), 'placeholder' => __( 'ex: Google Inc.', 'jigoshop-software' ), 'type' => 'text' ),
);
$this->order_fields = array(
array( 'id' => 'activation_email', 'label' => __( 'Activation Email', 'jigoshop-software' ), 'title' => __( 'Activation Email', 'jigoshop-software' ), 'placeholder' => '', 'type' => 'text' ),
array( 'id' => 'activation_email_optout', 'label' => __( 'Opt out of activation emails', 'jigoshop-software' ), 'title' => __( 'Opt out of activation emails', 'jigoshop-software' ), 'placeholder' => 'optout', 'type' => 'checkbox' ),
array( 'id' => 'license_key', 'label' => __( 'License Key', 'jigoshop-software' ), 'title' => __( 'License Key', 'jigoshop-software' ), 'placeholder' => '', 'type' => 'text' ),
array( 'id' => 'paypal_name', 'label' => __( 'Paypal Name to show on transaction receipts', 'jigoshop-software' ), 'title' => __( 'Paypal Name to show on transaction receipts', 'jigoshop-software' ), 'placeholder' => __( 'ex: Google Inc.', 'jigoshop-software' ), 'type' => 'text' ),
array( 'id' => 'transaction_id', 'label' => __( 'Transaction ID', 'jigoshop-software' ), 'title' => __( 'Transaction ID', 'jigoshop-software' ), 'placeholder' => '', 'type' => 'text' ),
array( 'id' => 'productid', 'label' => __( 'Product ID', 'jigoshop-software' ), 'title' => __( 'Product ID', 'jigoshop-software' ), 'placeholder' => '', 'type' => 'text' ),
array( 'id' => 'activations_possible', 'label' => __( 'Max Activations Allowed', 'jigoshop-software' ), 'title' => __( 'Max Activations Allowed', 'jigoshop-software' ), 'placeholder' => '', 'type' => 'text' ),
array( 'id' => 'remaining_activations', 'label' => __( 'Remaining Activations', 'jigoshop-software' ), 'title' => __( 'Remaining Activations', 'jigoshop-software' ), 'placeholder' => '', 'type' => 'text' ),
array( 'id' => 'secret_product_key', 'label' => __( 'Secret Product Key to use for API', 'jigoshop-software' ), 'title' => __( 'Secret Product Key to use for API', 'jigoshop-software' ), 'placeholder' => __( 'any random string', 'jigoshop-software' ), 'type' => 'text' ),
array( 'id' => 'version', 'label' => __( 'Version', 'jigoshop-software' ), 'title' => __( 'Version', 'jigoshop-software' ), 'placeholder' => '', 'type' => 'text' ),
array( 'id' => 'old_order_id', 'label' => __( 'Legacy order ID', 'jigoshop-software' ), 'title' => __( 'Legacy order ID', 'jigoshop-software' ), 'placeholder' => '', 'type' => 'text' ),
array( 'id' => 'is_upgrade', 'label' => __( 'This is an upgrade if checked', 'jigoshop-software' ), 'title' => __( 'This is an upgrade if checked', 'jigoshop-software' ), 'placeholder' => '', 'type' => 'checkbox' ),
array( 'id' => 'upgrade_name', 'label' => __( 'Upgraded from', 'jigoshop-software' ), 'title' => __( 'Upgraded from', 'jigoshop-software' ), 'placeholder' => '', 'type' => 'text' ),
array( 'id' => 'upgraded_via', 'label' => __( 'Upgraded Using', 'jigoshop-software' ), 'title' => __( 'Upgraded Using', 'jigoshop-software' ), 'placeholder' => '', 'type' => 'text' ),
array( 'id' => 'upgraded_to', 'label' => __( 'Upgraded To', 'jigoshop-software' ), 'title' => __( 'Upgraded To', 'jigoshop-software' ), 'placeholder' => '', 'type' => 'text' ),
array( 'id' => 'upgrade_price', 'label' => __( 'Upgrade price ($)', 'jigoshop-software' ), 'title' => __( 'Upgrade price ($)', 'jigoshop-software' ), 'placeholder' => '', 'type' => 'text' ),
array( 'id' => 'upgraded_from_order_id', 'label' => __( 'Order ID of the original purchase', 'jigoshop-software' ), 'title' => __( 'Order ID of the original purchase', 'jigoshop-software' ), 'placeholder' => '', 'type' => 'text' ),
array( 'id' => 'has_been_upgraded', 'label' => __( 'This order has been used as an upgrade to another upgrade if checked', 'jigoshop-software' ), 'title' => __( 'This order has been used as an upgrade to another upgrade if checked', 'jigoshop-software' ), 'placeholder' => '', 'type' => 'checkbox' ),
array( 'id' => 'upgraded_to_order_id', 'label' => __( 'Order ID of the upgrade purchase', 'jigoshop-software' ), 'title' => __( 'Order ID of the upgrade purchase', 'jigoshop-software' ), 'placeholder' => '', 'type' => 'text' ),
array( 'id' => 'original_price', 'label' => __( 'Original price ($)', 'jigoshop-software' ), 'title' => __( 'Original price ($)', 'jigoshop-software' ), 'placeholder' => '', 'type' => 'text' ),
);
}
function load_lang() {
load_plugin_textdomain( 'jigoshop-software', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* runs various functions when the plugin first activates
*
* @see register_activation_hook()
* @link http://codex.wordpress.org/Function_Reference/register_activation_hook
* @since 1.0
* @return void
*/
function activation() {
// checks if the jigoshop plugin is running and disables this plugin if it's not (and displays a message)
if ( ! is_plugin_active( 'jigoshop/jigoshop.php' ) ) {
deactivate_plugins( plugin_basename( __FILE__ ) );
wp_die( sprintf( _x( 'The JigoShop Software Add-On requires %s to be activated in order to work. Please activate %s first.', 'A link to JigoShop is provided in the placeholders', 'jigoshop-software' ), '<a href="http://jigoshop.com" target="_blank">JigoShop</a>', '<a href="http://jigoshop.com" target="_blank">JigoShop</a>' ) . '<a href="'. esc_url( admin_url( 'plugins.php' ) ) . '"> <br> « ' . _x( 'Go Back', 'Activation failed, so go back to the plugins page', 'jigoshop-software' ) . '</a>' );
}
// creates the lost license page with the right shortcode in it
$lost_license_page_id = get_option( 'jigoshop_lost_license_page_id' );
if ( empty( $lost_license_page_id ) ) {
$lost_license_page = array(
'post_title' => _x( 'Lost License', 'title of a page', 'jigoshop-software' ),
'post_content' => '[jigoshop_software_lost_license]',
'post_status' => 'publish',
'post_type' => 'page',
);
$lost_license_page_id = wp_insert_post( $lost_license_page );
update_option( 'jigoshop_lost_license_page_id', $lost_license_page_id );
}
// creates the API page
$jigoshop_api_page_id = get_option( 'jigoshop_api_page_id' );
if ( empty( $jigoshop_api_page_id ) ) {
$api_page = array(
'post_title' => _x( 'API', 'title of a page', 'jigoshop-software' ),
'post_content' => '',
'post_status' => 'publish',
'post_type' => 'page',
);
$jigoshop_api_page_id = wp_insert_post( $api_page );
update_option( 'jigoshop_api_page_id', $jigoshop_api_page_id );
}
// creates the activation notification SUBSCRIBE page
$jigoshop_activation_notification_subscribe_page_id = get_option( 'jigoshop_activation_notification_subscribe_page_id' );
if ( empty( $jigoshop_activation_notification_subscribe_page_id ) ) {
$jigoshop_activation_notification_subscribe_page = array(
'post_title' => _x( 'License Activation Notification Subscription', 'title of a page', 'jigoshop-software' ),
'post_content' => '[jigoshop_software_activation_subscribe]',
'post_status' => 'publish',
'post_type' => 'page',
);
$jigoshop_activation_notification_subscribe_page_id = wp_insert_post( $jigoshop_activation_notification_subscribe_page );
update_option( 'jigoshop_activation_notification_subscribe', $jigoshop_activation_notification_subscribe_page_id );
}
// creates the activation notification UN-SUBSCRIBE page
$jigoshop_activation_notification_unsubscribe_page_id = get_option( 'jigoshop_activation_notification_unsubscribe_page_id' );
if ( empty( $jigoshop_activation_notification_unsubscribe_page_id ) ) {
$jigoshop_activation_notification_unsubscribe_page = array(
'post_title' => _x( 'License Activation Notification Unsubscribe', 'title of a page', 'jigoshop-software' ),
'post_content' => '[jigoshop_software_activation_unsubscribe]',
'post_status' => 'publish',
'post_type' => 'page',
);
$jigoshop_activation_notification_subscribe_page_id = wp_insert_post( $jigoshop_activation_notification_unsubscribe_page );
update_option( 'jigoshop_activation_notification_subscribe', $jigoshop_activation_notification_unsubscribe_page_id );
}
}
/* =======================================
meta boxes
==========================================*/
/**
* gets array of values for the upgrade products dropdown
*
* @since 2.2
* @return array the array of product IDs and product Names
*/
function get_product_upgrade_dropdown( $get_the_upgrades = false ) {
$transient_key = ( $get_the_upgrades ) ? 'jigoshop_software_get_products_which_are_upgrades' : 'jigoshop_software_get_product_upgrade_dropdown';
$return = get_transient( $transient_key );
if ( empty( $return ) ) {
$query_args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'post__not_in' => array( get_queried_object_id() ),
);
$products = get_posts( $query_args );
$return = array( 0 => 'none' );
if ( !empty( $products ) ) {
foreach ( $products as $product ) {
$data = get_post_meta( $product->ID, 'product_data', true );
if ( $get_the_upgrades && ! empty( $data['is_upgrade'] ) ) {
$return[$product->ID] = $product->post_title;
} elseif ( ! $get_the_upgrades && empty( $data['is_upgrade'] ) ) {
$return[$product->ID] = $product->post_title;
}
}
}
wp_reset_query();
set_transient( $transient_key, $return );
}
return $return;
}
/**
* registers meta boxes
*
* @since 1.0
* @return void
*/
function add_meta_boxes() {
add_meta_box( 'jigoshop-software-order-data', __( 'Software Purchase Details', 'jigoshop-software' ), array( $this, 'order_meta_box' ), 'shop_order', 'normal', 'high' );
add_meta_box( 'jigoshop-software-activation-data', __( 'Activations', 'jigoshop-software' ), array( $this, 'activation_meta_box' ), 'shop_order', 'normal', 'high' );
add_meta_box( 'jigoshop-software-further-actions', __( 'Further Actions', 'jigoshop-software' ), array( $this, 'order_further_actions_meta_box' ), 'shop_order', 'side', 'low' );
}
/**
* adds a new tab to the product interface
*
* @since 1.0
* @return void
*/
function product_write_panel_tab() {
printf( '<li><a href="#software_data">%s</a></li>', __( 'Software', 'jigoshop-software' ) );
}
/**
* adds the panel to the product interface
*
* @since 1.0
* @return void
*/
function product_write_panel() {
global $post;
$data = get_post_meta( $post->ID, 'product_data', true );
$this->define_fields();
echo '<div id="software_data" class="panel jigoshop_options_panel">';
foreach ( $this->product_fields as $field) {
// determine what the value should be
if ( ! empty( $field['id'] ) && 'soft_product_id' == $field['id'] )
$value = get_post_meta( $post->ID, 'soft_product_id', true );
elseif ( ! empty( $field['id'] ) && in_array( $field['id'], array( 'up_license_keys', 'used_license_keys' ) ) )
$value = $this->un_array_ify_keys( $data[$field['id']] );
else
$value = $data[$field['id']];
$field_classes = array( 'form-field', 'jgs-product-field' );
if ( ! empty( $field['upgrade_field'] ) && $field['upgrade_field'] )
$field_classes[] = 'jgs-upgrade-field';
if ( ! empty( $field['never_hide'] ) && $field['never_hide'] )
$field_classes[] = 'jgs-never-hide';
$this->admin_field_helper( $field, $value, $field_classes );
}
echo '</div>';
?>
<script>
(function($) {
function upgrade_checkboxes() {
if ( $( '#is_upgrade' ).prop( 'checked' ) ) {
$( '.jgs-upgrade-field' ).show();
$( '.jgs-product-field' ).not( '.jgs-never-hide' ).not( '.jgs-upgrade-field' ).hide();
} else {
$( '.jgs-upgrade-field' ).hide();
$( '.jgs-product-field' ).show();
}
}
upgrade_checkboxes();
$( '#is_upgrade' ).change( function(){ upgrade_checkboxes() });
})(jQuery);
</script>
<?php
}
/**
* saves the data inputed into the product boxes into a serialized array
*
* @since 2.1
* @return void
*/
function product_save_data() {
global $post;
$this->define_fields();
delete_transient( 'jigoshop_software_get_product_upgrade_dropdown' );
delete_transient( 'jigoshop_software_get_products_which_are_upgrades' );
$data = get_post_meta( $post->ID, 'product_data', true );
foreach ( $this->product_fields as $field ) {
if ( in_array( $field['id'], array( 'up_license_keys', 'used_license_keys' ) ) ) {
$data[$field['id']] = $this->array_ify_keys( strip_tags( $_POST[$field['id']] ) );
} elseif ( 'soft_product_id' == $field['id'] ) {
update_post_meta( $post->ID, 'soft_product_id', sanitize_text_field( $_POST[$field['id']] ) );
} else {
$data[$field['id']] = sanitize_text_field( $_POST[$field['id']] );
}
}
update_post_meta( $post->ID, 'product_data', $data );
$this->get_product_upgrade_dropdown();
}
/**
* adds meta fields to the order screens
*
* @since 1.0
* @return void
*/
function order_meta_box() {
global $post;
$data = (array) get_post_meta( $post->ID, 'order_data', true );
$this->define_fields();
?>
<div class="panel-wrap jigoshop">
<div id="order_software_data" class="panel jigoshop_options_panel">
<?php
foreach ( $this->order_fields as $field ) {
if ( 'activation_email' == $field['id'] )
$value = get_post_meta( $post->ID, 'activation_email', true );
elseif ( 'transaction_id' == $field['id'] )
$value = get_post_meta( $post->ID, 'transaction_id', true );
elseif ( 'old_order_id' == $field['id'] )
$value = get_post_meta( $post->ID, 'old_order_id', true );
elseif ( isset( $data[$field['id']] ) )
$value = $data[$field['id']];
else
$value = null;
$this->admin_field_helper( $field, $value, array( 'form-field', 'jgs-order-field' ) );
}
?>
</div>
</div>
<?php
}
/**
* admin helper to build out fields used inside meta boxes
* helps reduce code duplication
* echos/prints the field
*
* @param $field, field object to build
* @param $value, the current value of the field
* @return void
*/
function admin_field_helper( $field, $value, $field_classes ) {
printf( '<p class="%s">', esc_attr( implode( ' ', array_map( 'sanitize_html_class', $field_classes ) ) ) );
printf( '<label for="%s">%s</label>', esc_attr( $field['id'] ), esc_html( $field['label'] ) );
switch ( $field['type'] ) {
case 'text' :
case 'number' :
printf( '<input type="%s" id="%s" name="%s" value="%s" placeholder="%s"/>', esc_attr( $field['type'] ), esc_attr( $field['id'] ), esc_attr( $field['id'] ), esc_attr( $value ), esc_attr( $field['placeholder'] ) );
break;
case 'textarea' :
printf( '<textarea id="%s" name="%s" placeholder="%s">%s</textarea>', esc_attr( $field['id'] ), esc_attr( $field['id'] ), esc_attr( $field['placeholder'] ), esc_textarea( $value ) );
break;
case 'checkbox' :
printf( '<input type="checkbox" id="%s" name="%s" value="on"%s', esc_attr( $field['id'] ), esc_attr( $field['id'] ), checked( $value, 'on', false ) );
break;
case 'select' :
printf( '<select id="%s" name="%s">', esc_attr( $field['id'] ), esc_attr( $field['id'] ) );
foreach ( $field['values'] as $value_to_save => $value_nice_name )
printf( '<option value="%s"%s>%s</option>', esc_attr( $value_to_save ), selected( $value_to_save, $value, false ), esc_html( $value_nice_name ) );
echo '</select>';
break;
}
echo '</p>';
}
/**
* adds activations meta box
*
* @since 1.0
* @param object $post the current post object
* @return void
*/
function activation_meta_box( $post ) {
$activations = get_post_meta( $post->ID, 'activations', true );
if ( ! empty( $activations ) ) : ?>
<table id="activations-table" class="widefat">
<thead>
<tr>
<th><?php _e( 'Instance', 'jigoshop-software' ) ?></th>
<th><?php _e( 'Status', 'jigoshop-software' ) ?></th>
<th><?php _e( 'Date & Time', 'jigoshop-software' ) ?></th>
<th><?php _e( 'Version', 'jigoshop-software' ) ?></th>
<th><?php _e( 'Operating System', 'jigoshop-software' ) ?></th>
</tr>
</thead>
<tfoot>
<tr>
<th><?php _e( 'Instance', 'jigoshop-software' ) ?></th>
<th><?php _e( 'Status', 'jigoshop-software' ) ?></th>
<th><?php _e( 'Date & Time', 'jigoshop-software' ) ?></th>
<th><?php _e( 'Version', 'jigoshop-software' ) ?></th>
<th><?php _e( 'Operating System', 'jigoshop-software' ) ?></th>
</tr>
</tfoot>
<tbody>
<?php $i = 0; foreach ($activations as $activation) : $i++ ?>
<tr<?php if ( $i / 2 == 1 ) echo ' class="alternate"' ?>>
<td><?php echo esc_html( $activation['instance'] ); ?></td>
<td><?php echo ( $activation['active'] ) ? __( 'Activated', 'jigoshop-software' ) : __( 'Deactivated', 'jigoshop-software' ) ?></td>
<td><?php echo esc_html( sprintf( _x( '%s at %s', 'date and time of the activation', 'jigoshop-software' ), date( 'D j M Y', $activation['time'] ), date( 'h:ia T', $activation['time'] ) ) ); ?></td>
<td><?php echo esc_html( $activation['version'] ); ?></td>
<td><?php echo esc_html( ucwords( $activation['os'] ) ); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php else : ?>
<p><?php _e( 'No activations yet', 'jigoshop-software' ) ?></p>
<?php endif;
}
/**
* saves the data inputed into the order boxes
*
* @see order_meta_box()
* @since 1.0
* @return void
*/
function order_save_data() {
global $post;
$this->define_fields();
$data = get_post_meta( $post->ID, 'order_data', true );
foreach ( $this->order_fields as $field ) {
if ( isset( $_POST[$field['id']] ) ) {
if ( $field['id'] == 'activation_email' ) {
update_post_meta( $post->ID, 'activation_email', sanitize_text_field( $_POST['activation_email'] ) );
} elseif ( $field['id'] == 'transaction_id' ) {
update_post_meta( $post->ID, 'transaction_id', sanitize_text_field( $_POST['transaction_id'] ) );
} elseif ( $field['id'] == 'old_order_id' ) {
update_post_meta( $post->ID, 'old_order_id', sanitize_text_field( $_POST['old_order_id'] ) );
} else {
$data[$field['id']] = ( is_array( $_POST[$field['id']] ) ) ? array_map( 'sanitize_text_field', $_POST[$field['id']] ) : sanitize_text_field( $_POST[$field['id']] );
}
}
}
if ( empty( $_POST['is_upgrade'] ) )
unset( $data['is_upgrade'] );
if ( empty( $_POST['has_been_upgraded'] ) )
unset( $data['has_been_upgraded'] );
if ( empty( $_POST['activation_email_optout'] ) )
unset( $data['activation_email_optout'] );
update_post_meta( $post->ID, 'order_data', $data );
if ( isset( $_POST['resend_email'] ) )
$this->process_email( $post->ID, 'completed_purchase' );
}
/**
* displays the meta box which allows further actions to be taken
*
* @since 1.7
* @return void
*/
function order_further_actions_meta_box() { ?>
<ul class="order_actions">
<li><input type="submit" class="button button-primary" name="resend_email" value="<?php esc_attr_e( 'Resend Email', 'jigoshop-software' ); ?>" /> — <?php _e( 'Resend Purchase Email' , 'jigoshop-software' ); ?></li>
</ul>
<?php
}
/**
* adds css to the back-end
*
* @since 2.1
* @return void
*/
function admin_enqueue() {
wp_enqueue_style( 'jigoshop_software_backend', plugins_url( 'inc/back-end.css', __FILE__ ), array(), JIGOSHOP_SOFTWARE_VERSION );
}
/**
* filter the text displayed when searching for orders
*
* @since 2.4
* @ret
*/
function order_get_search_query( $search_query ) {
global $pagenow;
if ( 'edit.php' != $pagenow && 'shop_order' != get_post_type() )
return $search_query;
$search_query = esc_html( $_GET['s'] );
return $search_query;
}
/**
* registers the import page
* @since 1.0
* @return void
*/
function admin_menu() {
add_submenu_page( 'jigoshop', __( 'Import', 'jigoshop-software' ), __( 'Import', 'jigoshop-software' ) , 'manage_options', 'jgs_import', array( $this, 'import_page' ) );
}
/* =======================================
filter add to cart & other jigoshop internal functions
==========================================*/
/**
* enqueue scripts and styles on the frontend
*
* @since 2.3
* @return void
*/
function frontend_enqueue() {
wp_enqueue_style( 'jigoshop_software', plugins_url( 'inc/front-end.css', __FILE__ ), array(), JIGOSHOP_SOFTWARE_VERSION );
}
/**
* replace the default jigoshop add to cart button
*
* @see downloadable_add_to_cart()
* @since 1.0
* @return void
*/
function add_to_cart() {
global $_product; $availability = $_product->get_availability();
if ($availability['availability']) : ?><p class="stock <?php echo $availability['class'] ?>"><?php echo $availability['availability']; ?></p><?php endif; ?>
<form action="<?php echo $_product->add_to_cart_url(); ?>" class="cart" method="post">
<button type="submit" class="button-alt"><?php _e( 'Buy Now', 'jigoshop-software' ); ?></button>
<?php do_action( 'jigoshop_add_to_cart_form' ); ?>
</form>
<?php
}
/**
* replace the default jigoshop add to cart button
*
* @see jigoshop_template_loop_add_to_cart()
* @since 1.0
* @param object $post the current post object
* @param object $_product the current product object
* @return void
*/
function loop_add_to_cart( $post, $_product ) {
?><a href="<?php echo $_product->add_to_cart_url(); ?>" class="button"><?php _e( 'Buy Now' , 'jigoshop-software' ); ?></a><?php
}
/**
* empty the cart when an order is cancelled
*
* @param int the order id
* @return void
*/
function cancel_order( $order_id ) {
jigoshop_cart::empty_cart();
}
/**
* redirect the user to checkout after they've clicked "buy now"
*
* @see jigoshop_add_to_cart_action()
* @since 1.0
* @return void
*/
function add_to_cart_redirect() {
return jigoshop_cart::get_checkout_url();
}
/**
* filters the template for the api page so that it just does the json stuff
*
* @since 1.0
* @param string $template the template file
* @return string filtered template file location
*/
function locate_api_template( $template ) {
global $post;
if ( isset( $post->ID ) && get_option( 'jigoshop_api_page_id' ) == $post->ID )
return JIGOSHOP_SOFTWARE_PATH . '/inc/api.php';
}
/**
* runs an output buffer on the price sent to paypal when upgrading
* empties the cart before something is added to it
*
* @see jigoshop_software_filter_price_paypal
* @since 2.1.4
* @return void
*/
function init_actions() {
if ( !empty( $_GET['add-to-cart'] ) && jigoshop::verify_nonce( 'add_to_cart', '_GET' ) ) {
jigoshop_cart::empty_cart();
}
if ( isset( $_GET['order'] ) && isset( $_GET['key'] ) ) {
ob_start( array( $this, 'filter_price_paypal' ) );
}
}
/**
* very hacky way to filter out the price sent to paypal when it's an upgrade, but the only way to do it w/out changing core jigoshop
*
* @see $this->init_output_buffer()
* @todo find a better a way to do this
* @since 1.0
* @param string $buffer what's being sent to paypal
* @return string $buffer what's being sent to paypal
*/
function filter_price_paypal( $buffer ) {
$order_id = $_GET['order'];
$data = get_post_meta( $order_id, 'order_data', true );
if ( ! empty( $data['original_price'] ) ) {
$original_price = number_format( $data['original_price'], 2 );
$correct_price = number_format( $data['order_total'], 2 );
if ( ! empty( $original_price ) ) {
$buffer = str_replace( '"amount_1" value="' . $original_price . '"', '"amount_1" value="' . $correct_price . '"', $buffer );
}
}
return $buffer;
}
/**
* process the ajax request for the upgrade page
*
* @since 2.6
* @return void
*/
function ajax_jgs_upgrade() {
$messages = null; // reset in case this a second attempt
$success = null;
$message = null;
// nonce verification
if ( empty( $_POST['jgs_upgrade_nonce'] ) || ! wp_verify_nonce( $_POST['jgs_upgrade_nonce'], 'jgs_upgrade' ) ) {
wp_send_json_error( __( 'An error has occurred, please try again.', 'jigoshop-software' ) );
return;
}
// email validation
if ( empty( $_POST['jgs_email'] ) || ! is_email( $_POST['jgs_email'] ) ) {
wp_send_json_error( __( 'Please enter a valid email address.', 'jigoshop-software' ) );
return;
}
if ( empty( $_POST['jgs_license_key'] ) ) {
wp_send_json_error( __( 'Please enter a license key.', 'jigoshop-software' ) );
return;
}
$license_key = sanitize_key( $_POST['jgs_license_key'] );
$email_address = sanitize_email( $_POST['jgs_email'] );
$possible_upgrade_ids = $this->get_possible_upgrades_for_order( $license_key, $email_address );
if ( is_wp_error( $possible_upgrade_ids ) ) {
wp_send_json_error( $possible_upgrade_ids->get_error_message() );
return;
} elseif ( empty( $possible_upgrade_ids ) ) {
wp_send_json_error( __( 'No possible upgrades found with the provided details.' ) );
return;
}
$this->set_upgrade_cookie( $possible_upgrade_ids, $license_key, $email_address );
wp_send_json_success( array(
'success_message' => sprintf( __( 'Here are the possible upgrades for license key %s. When upgrading, your license key will deactivate. You will be sent a new license key to re-activate your installs. Please press "Buy Now" for the upgrade you want.', 'jigoshop-software' ), esc_html( $_POST['jgs_license_key'] ) ),
'possible_upgrade_products' => do_shortcode( '[products ids="' . implode( ',', $possible_upgrade_ids ) . '"]' ),
) );
}
/**
* set a cookie for pre-filling the upgrade screen
*
* @param $possible_upgrade_ids
* @param $license_key
* @param $email_address
*/
function set_upgrade_cookie( $possible_upgrade_ids, $license_key, $email_address ) {
if ( ! empty( $_COOKIE['jgs_upgrade_prefill'] ) ) {
$cookie = json_decode( $_COOKIE['jgs_upgrade_prefill'] );
}
if ( empty( $cookie ) || ! is_array( $cookie ) ) {
$cookie = array();
}
foreach( $possible_upgrade_ids as $upgrade_id ) {
$cookie[$upgrade_id] = array(
'license_key' => $license_key,
'email_address' => $email_address,
);
}
$cookie = json_encode( $cookie );
$expire = time() + DAY_IN_SECONDS;
setcookie( 'jgs_upgrade_prefill', $cookie, $expire, '/' );
}
/**
* set a cookie for pre-filling the upgrade screen
*
* @param $possible_upgrade_ids
* @param $license_key
* @param $email_address
*/
function get_upgrade_prefill_from_cookie( $upgrade_id ) {
if ( empty( $_COOKIE['jgs_upgrade_prefill'] ) )
return false;
$cookie = json_decode( stripslashes( $_COOKIE['jgs_upgrade_prefill'] ) );
if ( empty( $cookie ) || ! is_object( $cookie ) )
return false;
if ( empty( $cookie->{$upgrade_id} ) )
return false;
$prefill_values = (array) $cookie->{$upgrade_id};
return $prefill_values;
}
/**
* transforms a comma separated list of license keys into an array in order to store in the DB
*
* @since 1.0
* @param string $keys a comma separated list of keys
* @return array $keys_array an array of keys
*/
function array_ify_keys( $keys = null ) {
$keys = esc_attr( $keys );
if ( is_string( $keys ) ) {
$keys_array = explode( ',', $keys );
return $keys_array;
}
return false;
}
/**
* transforms an array of license keys into a comma separated list in order to display it
*
* @since 1.0
* @param array $keys the array of keys
* @return string $keys_string the string of keys
*/
function un_array_ify_keys( $keys = null ) {
$i = 0;
$keys_string = '';
if ( is_array( $keys ) ) {
foreach ( $keys as $key ) {
$i++;
if ( $i != 1 ) $keys_string .= ',';
$keys_string .= $key;
}
$keys_string = ltrim( $keys_string, ',' ); // filter out a comma if there is one in the first character
$keys_string = ltrim( $keys_string, ' ' ); // filter out a space if there is one in the first character
return $keys_string;
}
return false;
}
/**
* checks if a key is a valid key for a particular product
*
* @since 2.2
* @param string $license_key the key to validate
* @param string $email_address the email address asssociated with the purchase
* @param int $product_id the product to validate for
* @param string $date date after which purchase must have occurred
* @param bool $return_order_id if true, the function will return the order ID that matches the license key, instead of just a bool
* @param bool $validate_product_id if true, the function will validate the provided product ID for the license key
* @param bool $return_details if true, the function will return details about the order, such as the product ID that matches this license key, note that $return_order_id takes presedence
* @return bool|int valid key or not|order_id if $return_order_id is set to true
*/
function is_valid_license_key( $license_key = null, $email_address = null, $product_id = null, $date = null, $return_order_id = false, $validate_product_id = true, $return_details = false ) {
if ( empty( $license_key ) || empty( $email_address ) || ( $validate_product_id && empty( $product_id ) ) )
return false;
$orders = get_posts(
array(
'post_type' => 'shop_order',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'activation_email',
'value' => $email_address,
),
),
)
);
if ( !empty( $orders ) ) {
foreach ( $orders as $order ) {
$data = get_post_meta( $order->ID, 'order_data', true );
if ( ( ! $validate_product_id || ( isset( $data['productid'] ) && $data['productid'] == $product_id ) ) && isset( $data['license_key'] ) && $data['license_key'] == $license_key ) {
// we have a match, let's make sure it's a completed sale
$order_status = wp_get_post_terms( $order->ID, 'shop_order_status' );
$order_status = $order_status[0]->slug;
if ( $order_status == 'completed' ) {
// let's make sure the date is within the threshold
if ( empty( $date ) || get_the_time( 'U', $order->ID ) >= strtotime( $date ) ) {
// finaly let's make sure it hasn't already been upgraded
if ( empty( $data['has_been_upgraded'] ) || 'on' != $data['has_been_upgraded'] ) {
if ( $return_order_id ) {
return $order->ID;
} elseif ( $return_details ) {
return array(
'order_id' => $order->ID,
'order_data' => $data,
);
} else {
return true;
}
}
}
}
}
}
}
return false;
}
/**
* checks if the given product is an upgrade for another product
*
* @since 2.2
* @param $product_id the potential upgrade product
* @return bool
*/
function is_upgradeable_product( $product_id ) {
$data = get_post_meta( $product_id, 'product_data', true );
return ( !empty( $data['is_upgrade'] ) && $data['is_upgrade'] );
}
/**
* returns the ID of the product for which the given product is an upgrade from
*
* @since 2.2
* @param $product_id the potential upgrade product
* @return bool
*/
function get_upgrade_from_product_id( $product_id ) {
if ( !$this->is_upgradeable_product( $product_id ) )
return false;
$data = get_post_meta( $product_id, 'product_data', true );
if ( empty( $data['upgrade_from'] ) ) {
return false;
} else {
return $data['upgrade_from'];
}
}
/**
* returns the ID of the product for which the given product is an upgrade to
*
* @since 2.2