-
Notifications
You must be signed in to change notification settings - Fork 0
/
wc-am-client.php
964 lines (842 loc) · 38.4 KB
/
wc-am-client.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
<?php
/**
* The WooCommerce API Manager PHP Client Library is designed to be droppped into a WordPress plugin or theme.
* This version is designed to be used with the WooCommerce API Manager version 2.x.
*
* Intellectual Property rights, and copyright, reserved by Todd Lahman, LLC as allowed by law include,
* but are not limited to, the working concept, function, and behavior of this software,
* the logical code structure and expression as written.
*
* @version 2.7
* @author Todd Lahman LLC https://www.toddlahman.com/
* @copyright Copyright (c) Todd Lahman LLC ([email protected])
* @package WooCommerce API Manager plugin and theme library
* @license Copyright Todd Lahman LLC
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WC_AM_Client_2_7' ) ) {
class WC_AM_Client_2_7 {
/**
* Class args
*
* @var string
*/
public $api_url = '';
public $data_key = '';
public $file = '';
public $plugin_name = '';
public $plugin_or_theme = '';
public $product_id = '';
public $slug = '';
public $software_title = '';
public $software_version = '';
public $text_domain = ''; // For language translation.
/**
* Class properties.
*
* @var string
*/
public $data = array();
public $identifier = '';
public $no_product_id = false;
public $product_id_chosen = 0;
public $wc_am_activated_key = '';
public $wc_am_activation_tab_key = '';
public $wc_am_api_key_key = '';
public $wc_am_deactivate_checkbox_key = '';
public $wc_am_deactivation_tab_key = '';
public $wc_am_domain = '';
public $wc_am_instance_id = '';
public $wc_am_instance_key = '';
public $wc_am_menu_tab_activation_title = '';
public $wc_am_menu_tab_deactivation_title = '';
public $wc_am_plugin_name = '';
public $wc_am_product_id = '';
public $wc_am_renew_license_url = '';
public $wc_am_settings_menu_title = '';
public $wc_am_settings_title = '';
public $wc_am_software_version = '';
public function __construct( $file, $product_id, $software_version, $plugin_or_theme, $api_url, $software_title = '', $text_domain = '' ) {
$this->no_product_id = empty( $product_id ) ? true : false;
$this->plugin_or_theme = esc_attr( $plugin_or_theme );
if ( $this->no_product_id ) {
$this->identifier = 'plugin' === $this->plugin_or_theme ? dirname( untrailingslashit( plugin_basename( $file ) ) ) : get_stylesheet();
$product_id = strtolower( str_ireplace( array( ' ', '_', '&', '?', '-' ), '_', $this->identifier ) );
$this->wc_am_product_id = 'wc_am_product_id_' . $product_id;
$this->product_id_chosen = get_option( $this->wc_am_product_id );
} else {
/**
* Preserve the value of $product_id to use for API requests. Pre 2.0 product_id is a string, and >= 2.0 is an integer.
*/
if ( is_int( $product_id ) ) {
$this->product_id = absint( $product_id );
} else {
$this->product_id = esc_attr( $product_id );
}
}
// If the product_id was not provided, but was saved by the customer, used the saved product_id.
if ( empty( $this->product_id ) && ! empty( $this->product_id_chosen ) ) {
$this->product_id = $this->product_id_chosen;
}
$this->file = $file;
$this->software_title = esc_attr( $software_title );
$this->software_version = esc_attr( $software_version );
$this->api_url = esc_url( $api_url );
$this->text_domain = esc_attr( $text_domain );
/**
* If the product_id is a pre 2.0 string, format it to be used as an option key, otherwise it will be an integer if >= 2.0.
*/
$this->data_key = 'wc_am_client_' . strtolower( str_ireplace( array( ' ', '_', '&', '?', '-' ), '_', $product_id ) );
$this->wc_am_activated_key = $this->data_key . '_activated';
if ( is_admin() ) {
if ( ! empty( $this->plugin_or_theme ) && 'theme' === $this->plugin_or_theme ) {
add_action( 'admin_init', array( $this, 'activation' ) );
}
if ( ! empty( $this->plugin_or_theme ) && 'plugin' === $this->plugin_or_theme ) {
register_activation_hook( $this->file, array( $this, 'activation' ) );
}
// add_action( 'admin_menu', array( $this, 'register_menu' ) );
// add_action( 'admin_init', array( $this, 'load_settings' ) );
// Check for external connection blocking.
add_action( 'admin_notices', array( $this, 'check_external_blocking' ) );
/**
* Set all data defaults here
*/
$this->wc_am_api_key_key = $this->data_key . '_api_key';
$this->wc_am_instance_key = $this->data_key . '_instance';
/**
* Set all admin menu data
*/
$this->wc_am_deactivate_checkbox_key = $this->data_key . '_deactivate_checkbox';
$this->wc_am_activation_tab_key = $this->data_key . '_dashboard';
$this->wc_am_deactivation_tab_key = $this->data_key . '_deactivation';
$this->wc_am_settings_menu_title = $this->software_title . esc_html__( ' Activation', $this->text_domain ); // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain
$this->wc_am_settings_title = $this->software_title . esc_html__( ' API Key Activation', $this->text_domain ); // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain
$this->wc_am_menu_tab_activation_title = esc_html__( 'API Key Activation', $this->text_domain ); // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain
$this->wc_am_menu_tab_deactivation_title = esc_html__( 'API Key Deactivation', $this->text_domain ); // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain
/**
* Set all software update data here
*/
$this->data = get_option( $this->data_key );
$this->wc_am_plugin_name = 'plugin' === $this->plugin_or_theme ? untrailingslashit( plugin_basename( $this->file ) ) : get_stylesheet(); // same as plugin slug. if a theme use a theme name like 'twentyeleven'.
$this->wc_am_renew_license_url = $this->api_url . 'my-account'; // URL to renew an API Key. Trailing slash in the upgrade_url is required.
$this->wc_am_instance_id = get_option( $this->wc_am_instance_key ); // Instance ID (unique to each blog activation).
/**
* Some web hosts have security policies that block the : (colon) and // (slashes) in http://,
* so only the host portion of the URL can be sent. For example the host portion might be
* www.example.com or example.com. http://www.example.com includes the scheme http,
* and the host www.example.com.
* Sending only the host also eliminates issues when a client site changes from http to https,
* but their activation still uses the original scheme.
* To send only the host, use a line like the one below:
*
* $this->wc_am_domain = str_ireplace( array( 'http://', 'https://' ), '', home_url() ); // blog domain name
*/
$this->wc_am_domain = str_ireplace( array( 'http://', 'https://' ), '', home_url() ); // blog domain name.
$this->wc_am_software_version = $this->software_version; // The software version.
// Check if data has been migrated from pre-2.0.
$this->migrate_pre_2_0_data( $product_id, $software_title );
/**
* Check for software updates
*/
$this->check_for_update();
if ( ! empty( $this->wc_am_activated_key ) && 'Activated' !== get_option( $this->wc_am_activated_key ) ) {
// add_action( 'admin_notices', array( $this, 'inactive_notice' ) );
}
}
/**
* Deletes all data if plugin deactivated
*/
if ( 'plugin' === $this->plugin_or_theme ) {
register_deactivation_hook( $this->file, array( $this, 'uninstall' ) );
}
if ( 'theme' === $this->plugin_or_theme ) {
add_action( 'switch_theme', array( $this, 'uninstall' ) );
}
}
/**
* Migrates pre 2.0 data to prevent breaking old software activations.
*
* @since 2.0
*
* @param int $product_id
* @param string $software_title
*/
public function migrate_pre_2_0_data( $product_id, $software_title ) {
$upraded_postfix = strtolower( str_ireplace( array( ' ', '_', '&', '?', '-' ), '_', $product_id ) );
$upraded = get_option( 'wc_client_20_ugrade_attempt_' . $upraded_postfix );
if ( 'yes' !== $upraded ) {
$title = is_int( $product_id ) ? strtolower( $software_title ) : strtolower( $product_id );
$title = str_ireplace( array( ' ', '_', '&', '?' ), '_', $title );
$old_data_key = $title . '_data';
$data = get_option( $old_data_key );
$instance = get_option( $title . '_instance' );
if ( ! empty( $data ) && ! empty( $instance ) ) {
$api_key = array(
$this->wc_am_api_key_key => $data['api_key'],
);
update_option( $this->data_key, $api_key );
update_option( $this->wc_am_instance_key, $instance );
! empty( $instance ) ? update_option( $this->wc_am_deactivate_checkbox_key, 'off' ) : update_option( $this->wc_am_deactivate_checkbox_key, 'on' );
! empty( $instance ) ? update_option( $this->wc_am_activated_key, 'Activated' ) : update_option( $this->wc_am_activated_key, 'Deactivated' );
// Success!
update_option( 'wc_client_20_ugrade_attempt_' . $upraded_postfix, 'yes' );
} else {
if ( empty( $this->wc_am_instance_id ) ) {
// Failed migration. :( Cue the violins to play a sad song.
add_action( 'admin_notices', array( $this, 'migrate_error_notice' ) );
}
}
}
}
/**
* Provides one-time instructions for customer to reactivate the API Key if the migration fails.
*
* @since 2.0
*/
public function migrate_error_notice() { ?>
<div class="notice notice-error">
<p>
<?php esc_html_e( 'Attempt to migrate data failed. Deactivate then reactive this plugin or theme, then enter your API Key on the settings screen to receive software updates. Contact support if assistance is required.', $this->text_domain ); // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain ?>
</p>
</div>
<?php
}
/**
* License_key_deactivation
* Register submenu specific to this product.
*/
public function register_menu() {
add_options_page( esc_html__( $this->wc_am_settings_menu_title, $this->text_domain ), esc_html__( $this->wc_am_settings_menu_title, $this->text_domain ), 'manage_options', $this->wc_am_activation_tab_key, array( // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.NonSingularStringLiteralDomain
$this,
'config_page',
) );
}
/**
* Generate the default data.
*/
public function activation() {
$instance_exists = get_option( $this->wc_am_instance_key );
if ( false === get_option( $this->data_key ) || false === $instance_exists ) {
if ( false === $instance_exists ) {
update_option( $this->wc_am_instance_key, wp_generate_password( 12, false ) );
}
update_option( $this->wc_am_deactivate_checkbox_key, 'on' );
update_option( $this->wc_am_activated_key, 'Deactivated' );
}
}
/**
* Deletes all data if plugin deactivated
*/
public function uninstall() {
/**
* @since 2.5.1
*
* Filter wc_am_client_uninstall_disable
* If set to false uninstall() method will be disabled.
*/
if ( apply_filters( 'wc_am_client_uninstall_disable', true ) ) {
global $blog_id;
$this->license_key_deactivation();
// Remove options pre API Manager 2.0.
if ( is_multisite() ) {
switch_to_blog( $blog_id );
foreach (
array(
$this->wc_am_instance_key,
$this->wc_am_deactivate_checkbox_key,
$this->wc_am_activated_key,
) as $option
) {
delete_option( $option );
}
restore_current_blog();
} else {
foreach (
array(
$this->wc_am_instance_key,
$this->wc_am_deactivate_checkbox_key,
$this->wc_am_activated_key,
) as $option
) {
delete_option( $option );
}
}
}
}
/**
* Deactivates the license on the API server
*/
public function license_key_deactivation() {
$activation_status = get_option( $this->wc_am_activated_key );
$api_key = $this->data[ $this->wc_am_api_key_key ];
$args = array(
'api_key' => $api_key,
);
if ( 'Activated' === $activation_status && '' !== $api_key ) {
$this->deactivate( $args ); // reset API Key activation.
}
}
/**
* Displays an inactive notice when the software is inactive.
*/
public function inactive_notice() { ?>
<?php
/**
* @since 2.5.1
*
* Filter wc_am_client_inactive_notice_override
* If set to false inactive_notice() method will be disabled.
*/ ?>
<?php if ( apply_filters( 'wc_am_client_inactive_notice_override', true ) ) { ?>
<?php if ( ! current_user_can( 'manage_options' ) ) {
return;
} ?>
<?php if ( isset( $_GET['page'] ) && $this->wc_am_activation_tab_key === $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
return;
} ?>
<div class="notice notice-error">
<p><?php printf( __( 'The <strong>%1$s</strong> API Key has not been activated, so the %2$s is inactive! %3$sClick here%4$s to activate <strong>%5$s</strong>.', $this->text_domain ), esc_attr( $this->software_title ), esc_attr( $this->plugin_or_theme ), '<a href="' . esc_url( admin_url( 'options-general.php?page=' . $this->wc_am_activation_tab_key ) ) . '">', '</a>', esc_attr( $this->software_title ) ); // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain ?></p>
</div>
<?php }
}
/**
* Check for external blocking contstant.
*/
public function check_external_blocking() {
// show notice if external requests are blocked through the WP_HTTP_BLOCK_EXTERNAL constant.
if ( defined( 'WP_HTTP_BLOCK_EXTERNAL' ) && WP_HTTP_BLOCK_EXTERNAL === true ) {
// check if our API endpoint is in the allowed hosts.
$host = parse_url( $this->api_url, PHP_URL_HOST ); // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url
if ( ! defined( 'WP_ACCESSIBLE_HOSTS' ) || stristr( WP_ACCESSIBLE_HOSTS, $host ) === false ) {
?>
<div class="notice notice-error">
<p><?php printf( __( '<b>Warning!</b> You\'re blocking external requests which means you won\'t be able to get %1$s updates. Please add %2$s to %3$s.', $this->text_domain ), $this->software_title, '<strong>' . $host . '</strong>', '<code>WP_ACCESSIBLE_HOSTS</code>' ); // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain ?></p>
</div>
<?php
}
}
}
/**
* Draw option page
*
* @access public
*/
public function config_page() {
$settings_tabs = array(
$this->wc_am_activation_tab_key => esc_html__( $this->wc_am_menu_tab_activation_title, $this->text_domain ), // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain, WordPress.WP.I18n.NonSingularStringLiteralText
$this->wc_am_deactivation_tab_key => esc_html__( $this->wc_am_menu_tab_deactivation_title, $this->text_domain ), // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain, WordPress.WP.I18n.NonSingularStringLiteralText
);
$current_tab = isset( $_GET['tab'] ) ? $_GET['tab'] : $this->wc_am_activation_tab_key; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$tab = isset( $_GET['tab'] ) ? $_GET['tab'] : $this->wc_am_activation_tab_key; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
?>
<div class='wrap'>
<h2><?php esc_html_e( $this->wc_am_settings_title, $this->text_domain ); // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain, WordPress.WP.I18n.NonSingularStringLiteralText ?></h2>
<h2 class="nav-tab-wrapper">
<?php
foreach ( $settings_tabs as $tab_page => $tab_name ) {
$active_tab = $current_tab === $tab_page ? 'nav-tab-active' : '';
echo '<a class="nav-tab ' . esc_attr( $active_tab ) . '" href="?page=' . esc_attr( $this->wc_am_activation_tab_key ) . '&tab=' . esc_attr( $tab_page ) . '">' . esc_attr( $tab_name ) . '</a>';
}
?>
</h2>
<form action='options.php' method='post'>
<div class="main">
<?php
if ( $tab === $this->wc_am_activation_tab_key ) {
settings_fields( $this->data_key );
do_settings_sections( $this->wc_am_activation_tab_key );
submit_button( esc_html__( 'Save Changes', $this->text_domain ) ); // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain
} else {
settings_fields( $this->wc_am_deactivate_checkbox_key );
do_settings_sections( $this->wc_am_deactivation_tab_key );
submit_button( esc_html__( 'Save Changes', $this->text_domain ) ); // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain
}
?>
</div>
</form>
</div>
<?php
}
/**
* Register settings
*
* @access public
*/
public function load_settings() {
register_setting( $this->data_key, $this->data_key, array( $this, 'validate_options' ) );
// API Key.
add_settings_section( $this->wc_am_api_key_key, esc_html__( 'API Key Activation', $this->text_domain ), array( // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain
$this,
'wc_am_api_key_text',
), $this->wc_am_activation_tab_key );
add_settings_field( $this->wc_am_api_key_key, esc_html__( 'API Key', $this->text_domain ), array( // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain
$this,
'wc_am_api_key_field',
), $this->wc_am_activation_tab_key, $this->wc_am_api_key_key );
/**
* @since 2.3
*/
if ( $this->no_product_id ) {
add_settings_field( 'product_id', esc_html__( 'Product ID', $this->text_domain ), array( // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain
$this,
'wc_am_product_id_field',
), $this->wc_am_activation_tab_key, $this->wc_am_api_key_key );
}
add_settings_field( 'status', esc_html__( 'API Key Status', $this->text_domain ), array( // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain
$this,
'wc_am_api_key_status',
), $this->wc_am_activation_tab_key, $this->wc_am_api_key_key );
// Activation settings.
register_setting( $this->wc_am_deactivate_checkbox_key, $this->wc_am_deactivate_checkbox_key, array( // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain
$this,
'wc_am_license_key_deactivation',
) );
add_settings_section( 'deactivate_button', esc_html__( 'API Deactivation', $this->text_domain ), array( // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain
$this,
'wc_am_deactivate_text',
), $this->wc_am_deactivation_tab_key );
add_settings_field( 'deactivate_button', esc_html__( 'Deactivate API Key', $this->text_domain ), array( // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain
$this,
'wc_am_deactivate_textarea',
), $this->wc_am_deactivation_tab_key, 'deactivate_button' );
}
/**
* Provides text for api key section
*
* @access public
*/
public function wc_am_api_key_text() { }
/**
* Returns the API Key status from the WooCommerce API Manager on the server.
*
* @access public
*/
public function wc_am_api_key_status() {
$license_status_check = '';
if ( $this->get_api_key_status( true ) ) {
$license_status_check = esc_html__( 'Activated', $this->text_domain ); // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain
update_option( $this->wc_am_activated_key, 'Activated' );
update_option( $this->wc_am_deactivate_checkbox_key, 'off' );
} else {
$license_status_check = esc_html__( 'Deactivated', $this->text_domain ); // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain
}
echo esc_attr( $license_status_check );
}
/**
* Returns the API Key status by querying the Status API function from the WooCommerce API Manager on the server.
*
* @return array|mixed|object
*/
public function license_key_status() {
return json_decode( $this->status(), true );
}
/**
* Returns true if the API Key status is Activated.
*
* @since 2.1
*
* @param bool $live Do not set to true if using to activate software. True is for live status checks after activation.
*
* @return bool
*/
public function get_api_key_status( $live = false ) {
/**
* Real-time result.
*
* @since 2.5.1
*/
if ( $live ) {
$license_status = $this->license_key_status();
return ! empty( $license_status['data']['activated'] ) && $license_status['data']['activated'];
}
/**
* If $live === false.
*
* Stored result when first activating software.
*/
return 'Activated' === get_option( $this->wc_am_activated_key );
}
/**
* Returns API Key text field
*
* @access public
*/
public function wc_am_api_key_field() {
if ( ! empty( $this->data[ $this->wc_am_api_key_key ] ) ) {
echo "<input id='api_key' name='" . esc_attr( $this->data_key ) . '[' . esc_attr( $this->wc_am_api_key_key ) . "]' size='25' type='text' value='" . esc_attr( $this->data[ $this->wc_am_api_key_key ] ) . "' />";
} else {
echo "<input id='api_key' name='" . esc_attr( $this->data_key ) . '[' . esc_attr( $this->wc_am_api_key_key ) . "]' size='25' type='text' value='' />";
}
}
/**
* @since 2.3
*/
public function wc_am_product_id_field() {
$product_id = get_option( $this->wc_am_product_id );
if ( ! empty( $product_id ) ) {
$this->product_id = $product_id;
}
if ( ! empty( $product_id ) ) {
echo "<input id='product_id' name='" . esc_attr( $this->wc_am_product_id ) . "' size='25' type='text' value='" . absint( $this->product_id ) . "' />";
} else {
echo "<input id='product_id' name='" . esc_attr( $this->wc_am_product_id ) . "' size='25' type='text' value='' />";
}
}
/**
* Sanitizes and validates all input and output for Dashboard
*
* @since 2.0
*
* @param array $input Input.
*
* @return mixed|string
*/
public function validate_options( $input ) {
// Load existing options, validate, and update with changes from input before returning.
$options = $this->data;
$options[ $this->wc_am_api_key_key ] = trim( $input[ $this->wc_am_api_key_key ] );
$api_key = trim( $input[ $this->wc_am_api_key_key ] );
$activation_status = get_option( $this->wc_am_activated_key );
$checkbox_status = get_option( $this->wc_am_deactivate_checkbox_key );
$current_api_key = $this->data[ $this->wc_am_api_key_key ];
/**
* @since 2.3
*/
if ( $this->no_product_id ) {
$new_product_id = absint( $_REQUEST[ $this->wc_am_product_id ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotValidated
if ( ! empty( $new_product_id ) ) {
update_option( $this->wc_am_product_id, $new_product_id );
$this->product_id = $new_product_id;
}
}
// Should match the settings_fields() value.
if ( ! empty( $_REQUEST['option_page'] ) && $_REQUEST['option_page'] !== $this->wc_am_deactivate_checkbox_key ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( 'Deactivated' === $activation_status || '' === $activation_status || '' === $api_key || 'on' === $checkbox_status || $current_api_key !== $api_key ) {
/**
* If this is a new key, and an existing key already exists in the database,
* deactivate the existing key before activating the new key.
*/
if ( $current_api_key !== $api_key ) {
$this->replace_license_key( $current_api_key );
}
$args = array(
'api_key' => $api_key,
);
$activate_results = json_decode( $this->activate( $args ), true );
if ( true === $activate_results['success'] && true === $activate_results['activated'] ) {
add_settings_error( 'activate_text', 'activate_msg', sprintf( __( '%s activated. ', $this->text_domain ), esc_attr( $this->software_title ) ) . esc_attr( "{$activate_results['message']}." ), 'updated' ); // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain
update_option( $this->wc_am_activated_key, 'Activated' );
update_option( $this->wc_am_deactivate_checkbox_key, 'off' );
}
if ( false === $activate_results && ! empty( $this->data ) && ! empty( $this->wc_am_activated_key ) ) {
add_settings_error( 'api_key_check_text', 'api_key_check_error', esc_html__( 'Connection failed to the License Key API server. Try again later. There may be a problem on your server preventing outgoing requests, or the store is blocking your request to activate the plugin/theme.', $this->text_domain ), 'error' ); // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain
update_option( $this->data[ $this->wc_am_activated_key ], 'Deactivated' );
}
if ( isset( $activate_results['data']['error_code'] ) && ! empty( $this->data ) && ! empty( $this->wc_am_activated_key ) ) {
add_settings_error( 'wc_am_client_error_text', 'wc_am_client_error', esc_attr( "{$activate_results['data']['error']}" ), 'error' );
update_option( $this->data[ $this->wc_am_activated_key ], 'Deactivated' );
}
} // End Plugin Activation
}
return $options;
}
/**
* Deactivates the API Key to allow key to be used on another blog.
*
* @param string $input Input.
*
* @access public
* @return string|boolean
*/
public function wc_am_license_key_deactivation( $input ) {
$activation_status = get_option( $this->wc_am_activated_key );
$options = ( 'on' === $input ? 'on' : 'off' );
$args = array(
'api_key' => $this->data[ $this->wc_am_api_key_key ],
);
if ( 'on' === $options && 'Activated' === $activation_status && '' !== $this->data[ $this->wc_am_api_key_key ] ) {
// deactivates API Key key activation.
$activate_results = json_decode( $this->deactivate( $args ), true );
if ( true === $activate_results['success'] && true === $activate_results['deactivated'] ) {
if ( ! empty( $this->wc_am_activated_key ) ) {
update_option( $this->wc_am_activated_key, 'Deactivated' );
add_settings_error( 'wc_am_deactivate_text', 'deactivate_msg', esc_html__( 'API Key deactivated. ', $this->text_domain ) . esc_attr( "{$activate_results['activations_remaining']}." ), 'updated' ); // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain
}
return $options;
}
if ( isset( $activate_results['data']['error_code'] ) && ! empty( $this->data ) && ! empty( $this->wc_am_activated_key ) ) {
add_settings_error( 'wc_am_client_error_text', 'wc_am_client_error', esc_attr( "{$activate_results['data']['error']}" ), 'error' );
update_option( $this->data[ $this->wc_am_activated_key ], 'Deactivated' );
}
} else {
return $options;
}
return false;
}
/**
* Deactivate the current API Key before activating the new API Key
*
* @param string $current_api_key
*
* @return bool
*/
public function replace_license_key( $current_api_key ) {
$args = array(
'api_key' => $current_api_key,
);
$reset = $this->deactivate( $args ); // reset API Key activation.
if ( true === $reset ) {
return true;
}
add_settings_error( 'not_deactivated_text', 'not_deactivated_error', esc_html__( 'The API Key could not be deactivated. Use the API Key Deactivation tab to manually deactivate the API Key before activating a new API Key. If all else fails, go to Plugins, then deactivate and reactivate this plugin, or if a theme change themes, then change back to this theme, then go to the Settings for this plugin/theme and enter the API Key information again to activate it. Also check the My Account dashboard to see if the API Key for this site was still active before the error message was displayed.', $this->text_domain ), 'updated' ); // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain
return false;
}
public function wc_am_deactivate_text() { }
public function wc_am_deactivate_textarea() {
echo '<input type="checkbox" id="' . esc_attr( $this->wc_am_deactivate_checkbox_key ) . '" name="' . esc_attr( $this->wc_am_deactivate_checkbox_key ) . '" value="on"';
echo checked( get_option( $this->wc_am_deactivate_checkbox_key ), 'on' );
echo '/>';
?><span class="description"><?php esc_html_e( 'Deactivates an API Key so it can be used on another blog.', $this->text_domain ); // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralDomain ?></span>
<?php
}
/**
* Builds the URL containing the API query string for activation, deactivation, and status requests.
*
* @param array $args
*
* @return string
*/
public function create_software_api_url( $args ) {
return add_query_arg( 'wc-api', 'wc-am-api', $this->api_url ) . '&' . http_build_query( $args );
}
/**
* Sends the request to activate to the API Manager.
*
* @param array $args
*
* @return bool|string
*/
public function activate( $args ) {
$defaults = array(
'wc_am_action' => 'activate',
'product_id' => $this->product_id,
'instance' => $this->wc_am_instance_id,
'object' => $this->wc_am_domain,
'software_version' => $this->wc_am_software_version,
);
$args = wp_parse_args( $defaults, $args );
$target_url = esc_url_raw( $this->create_software_api_url( $args ) );
$request = wp_safe_remote_post( $target_url, array( 'timeout' => 15 ) );
if ( is_wp_error( $request ) || 200 !== wp_remote_retrieve_response_code( $request ) ) {
// Request failed.
return false;
}
$response = wp_remote_retrieve_body( $request );
return $response;
}
/**
* Sends the request to deactivate to the API Manager.
*
* @param array $args
*
* @return bool|string
*/
public function deactivate( $args ) {
$defaults = array(
'wc_am_action' => 'deactivate',
'product_id' => $this->product_id,
'instance' => $this->wc_am_instance_id,
'object' => $this->wc_am_domain,
);
$args = wp_parse_args( $defaults, $args );
$target_url = esc_url_raw( $this->create_software_api_url( $args ) );
$request = wp_safe_remote_post( $target_url, array( 'timeout' => 15 ) );
if ( is_wp_error( $request ) || 200 !== wp_remote_retrieve_response_code( $request ) ) {
// Request failed.
return false;
}
$response = wp_remote_retrieve_body( $request );
return $response;
}
/**
* Sends the status check request to the API Manager.
*
* @return bool|string
*/
public function status() {
$defaults = array(
'wc_am_action' => 'status',
'api_key' => $this->data[ $this->wc_am_api_key_key ],
'product_id' => $this->product_id,
'instance' => $this->wc_am_instance_id,
'object' => $this->wc_am_domain,
);
$target_url = esc_url_raw( $this->create_software_api_url( $defaults ) );
$request = wp_safe_remote_post( $target_url, array( 'timeout' => 15 ) );
if ( is_wp_error( $request ) || 200 !== wp_remote_retrieve_response_code( $request ) ) {
// Request failed.
return false;
}
$response = wp_remote_retrieve_body( $request );
return $response;
}
/**
* Check for software updates.
*/
public function check_for_update() {
$this->plugin_name = $this->wc_am_plugin_name;
// Slug should be the same as the plugin/theme directory name.
if ( strpos( $this->plugin_name, '.php' ) !== 0 ) {
$this->slug = dirname( $this->plugin_name );
} else {
$this->slug = $this->plugin_name;
}
/*********************************************************************
* The plugin and theme filters should not be active at the same time
*/ /**
* More info:
* function set_site_transient moved from wp-includes/functions.php
* to wp-includes/option.php in WordPress 3.4
*
* Set_site_transient() contains the pre_set_site_transient_{$transient} filter
* {$transient} is either update_plugins or update_themes
*
* Transient data for plugins and themes exist in the Options table:
* _site_transient_update_themes
* _site_transient_update_plugins
*/
// uses the flag above to determine if this is a plugin or a theme update request.
if ( 'plugin' === $this->plugin_or_theme ) {
/**
* Plugin Updates
*/
add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'update_check' ) );
// Check For Plugin Information to display on the update details page.
add_filter( 'plugins_api', array( $this, 'information_request' ), 10, 3 );
} elseif ( 'theme' === $this->plugin_or_theme ) {
/**
* Theme Updates
*/
add_filter( 'pre_set_site_transient_update_themes', array( $this, 'update_check' ) );
// Check For Theme Information to display on the update details page.
// add_filter( 'themes_api', array( $this, 'information_request' ), 10, 3 ).
}
}
/**
* Sends and receives data to and from the server API
*
* @since 2.0
*
* @param array $args
*
* @return bool|string $response
*/
public function send_query( $args ) {
$target_url = esc_url_raw( add_query_arg( 'wc-api', 'wc-am-api', $this->api_url ) . '&' . http_build_query( $args ) );
$request = wp_safe_remote_post( $target_url, array( 'timeout' => 15 ) );
if ( is_wp_error( $request ) || 200 !== wp_remote_retrieve_response_code( $request ) ) {
return false;
}
$response = wp_remote_retrieve_body( $request );
return ! empty( $response ) ? $response : false;
}
/**
* Check for updates against the remote server.
*
* @since 2.0
*
* @param object $transient
*
* @return object $transient
*/
public function update_check( $transient ) {
if ( empty( $transient->checked ) ) {
return $transient;
}
$args = array(
'wc_am_action' => 'update',
'slug' => $this->slug,
'plugin_name' => $this->plugin_name,
'version' => $this->wc_am_software_version,
'product_id' => $this->product_id,
'api_key' => $this->data[ $this->wc_am_api_key_key ],
'instance' => $this->wc_am_instance_id,
);
// Check for a plugin update.
$response = json_decode( $this->send_query( $args ), true );
// Displays an admin error message in the WordPress dashboard.
// $this->check_response_for_errors( $response ).
if ( isset( $response['data']['error_code'] ) ) {
add_settings_error( 'wc_am_client_error_text', 'wc_am_client_error', "{$response['data']['error']}", 'error' );
}
if ( false !== $response && true === $response['success'] ) {
// New plugin version from the API.
$new_ver = (string) $response['data']['package']['new_version'];
// Current installed plugin version.
$curr_ver = (string) $this->wc_am_software_version;
$package = array(
'id' => $response['data']['package']['id'],
'slug' => $response['data']['package']['slug'],
'plugin' => $response['data']['package']['plugin'],
'new_version' => $response['data']['package']['new_version'],
'url' => $response['data']['package']['url'],
'tested' => $response['data']['package']['tested'],
'package' => $response['data']['package']['package'],
'upgrade_notice' => $response['data']['package']['upgrade_notice'],
);
if ( isset( $new_ver ) && isset( $curr_ver ) ) {
if ( false !== $response && version_compare( $new_ver, $curr_ver, '>' ) ) {
if ( 'plugin' === $this->plugin_or_theme ) {
$transient->response[ $this->plugin_name ] = (object) $package;
unset( $transient->no_update[ $this->plugin_name ] );
} elseif ( 'theme' === $this->plugin_or_theme ) {
$transient->response[ $this->plugin_name ]['new_version'] = $response['data']['package']['new_version'];
$transient->response[ $this->plugin_name ]['url'] = $response['data']['package']['url'];
$transient->response[ $this->plugin_name ]['package'] = $response['data']['package']['package'];
}
}
}
}
return $transient;
}
/**
* API request for informatin.
*
* If `$action` is 'query_plugins' or 'plugin_information', an object MUST be passed.
* If `$action` is 'hot_tags` or 'hot_categories', an array should be passed.
*
* @param false|object|array $result The result object or array. Default false.
* @param string $action The type of information being requested from the Plugin Install API.
* @param object $args
*
* @return object
*/
public function information_request( $result, $action, $args ) {
// Check if this plugins API is about this plugin.
if ( isset( $args->slug ) ) {
if ( $args->slug !== $this->slug ) {
return $result;
}
} else {
return $result;
}
$args = array(
'wc_am_action' => 'plugininformation',
'plugin_name' => $this->plugin_name,
'version' => $this->wc_am_software_version,
'product_id' => $this->product_id,
'api_key' => $this->data[ $this->wc_am_api_key_key ],
'instance' => $this->wc_am_instance_id,
'object' => $this->wc_am_domain,
);
$response = unserialize( $this->send_query( $args ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize
if ( isset( $response ) && is_object( $response ) && false !== $response ) {
return $response;
}
return $result;
}
}
}