/var/www/html_us/wp-content/plugins/checkout-for-woocommerce/includes/Managers/UpdatesManager.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
<?php
namespace Objectiv\Plugins\Checkout\Managers;

use 
Objectiv\Plugins\Checkout\Managers\Helpers\EDD_SL_Plugin_Updater;
use 
Objectiv\Plugins\Checkout\SingletonAbstract;

/**
 * EDD Software Licensing Magic
 *
 * A drop-in class that magically manages your EDD SL plugin licensing.
 *
 * @author Clifton H. Griffin II
 * @version 0.6.1
 * @copyright Clif Griffin Development, Inc. 2014
 * @license GNU GPL version 3 (or later) {@see license.txt}
 **/
class UpdatesManager extends SingletonAbstract {
    var 
$menu_slug// parent menu slug to attach "License" submenu to
    
var $prefix// prefix for internal settings
    
var $url// plugin host site URL for EDD SL
    
var $version// plugin version for EDD SL
    
var $name// plugin name for EDD SL
    
var $key_statuses// store list of key statuses and messages
    
var $bad_key_statuses// store list of key statuses and messages
    
var $activate_errors// store list of activation errors and error messages
    
var $last_activation_error// because we can't pass variables directly to admin_notice
    
var $theme    false// do we have a theme or a plugin?
    
var $beta     false// is this a beta?
    
var $home_url false;
    var 
$updater;

    
/**
     * Constructor
     *
     * @access public
     * @param string|bool $menu_slug The menu slug of the parent menu you want to attach the License submenu item to. Same syntax as add_submenu_page()  (default: false)
     * @param string|bool $url The URL of your host site (default: false)
     * @param string|bool $version The plugin version (default: false)
     * @param string|bool $name The plugin name (default: false)
     * @param string|bool $theme True if updating a theme.
     * @param string|bool $beta True if beta versions are enabled
     * @return void
     */
    
public function init$beta false ) {
        
add_action'cfw_do_plugin_activation', array( $this'run_on_plugin_activation' ) );
        
add_action'cfw_do_plugin_deactivation', array( $this'run_on_plugin_deactivation' ) );

        
$theme     false;
        
$url       CFW_UPDATE_URL;
        
$version   CFW_VERSION;
        
$name      CFW_NAME;
        
$menu_slug false;
        
$prefix    '_cfw_licensing';
        
$author    'Objectiv';
        
$home_url  $this->get_home_url();

        if ( 
false === $prefix ) {
            
error_log'CGD_EDDSL_Magic: No prefix specified. Aborting.' );
            return;
        }

        if ( 
false === $url || false === $version || false === $name ) {
            
error_log'CGD_EDDSL_Magic: url, version, plugin file, or name parameter was false. Aborting.' );
            return;
        }

        if ( 
false === $home_url ) {
            
$this->home_url get_home_url();
        } else {
            
$this->home_url $home_url;
        }

        
$this->url       trailingslashit$url );
        
$this->version   $version;
        
$this->name      $name;
        
$this->author    $author;
        
$this->menu_slug $menu_slug;
        
$this->prefix    $prefix '_';
        
$this->theme     $theme;
        
$this->beta      $beta;

        
$this->key_statuses = array(
            
'invalid'       => 'The entered license key is not valid.',
            
'expired'       => 'Your key has expired and needs to be renewed.',
            
'inactive'      => 'Your license key is valid, but is not active.',
            
'disabled'      => 'Your license key is currently disabled. Please contact support.',
            
'site_inactive' => 'Your license key is valid, but not active for this site.',
            
'valid'         => 'Your license key is valid and active for this site.',
            
'nolicensekey'  => 'Your license key is missing.',
        );

        
$this->bad_key_statuses = array(
            
'expired',
            
'disabled',
            
'invalid',
            
'nolicensekey'// we made up this status
        
);

        
$this->activate_errors = array(
            
'missing'             => 'The provided license key does not seem to exist.',
            
'revoked'             => 'The provided license key has been revoked. Please contact support.',
            
'no_activations_left' => 'This license key has been activated the maximum number of times.',
            
'expired'             => 'This license key has expired.',
            
'key_mismatch'        => 'An unknown error has occurred: key_mismatch',
        );

        
// Instantiate EDD_SL_Plugin_Updater
        
add_action'admin_init', array( $this'updater_init' ), ); // run first

        // Form Handler
        
add_actionSettingsManager::instance()->prefix '_settings_saved', array( $this'save_settings' ) );

        
// Cron action
        
add_action$this->prefix '_check_license', array( $this'check_license' ) );

        
// Delayed license status update
        
add_action$this->prefix '_edd_sl_delayed_license_status_update', array( $this'delayed_license_update' ) );
    }

    public function 
run_on_plugin_activation() {
        
$this->set_license_check_cron();
    }

    public function 
run_on_plugin_deactivation() {
        
$this->unset_license_check_cron();
    }

    
/**
     * Retrieves the URL for a given site where the front end is accessible.
     *
     * Returns the 'home' option with the appropriate protocol. The protocol will be 'https'
     * if is_ssl() evaluates to true; otherwise, it will be the same as the 'home' option.
     * If `$scheme` is 'http' or 'https', is_ssl() is overridden.
     *
     * Copied from WordPress 5.2.0
     *
     * @since 3.0.0
     *
     * @global string $pagenow
     *
     * @param  int         $blog_id Optional. Site ID. Default null (current site).
     * @param  string      $path    Optional. Path relative to the home URL. Default empty.
     * @param  string|null $scheme  Optional. Scheme to give the home URL context. Accepts
     *                              'http', 'https', 'relative', 'rest', or null. Default null.
     * @return string Home URL link with optional path appended.
     */
    
public function get_home_url$blog_id null$path ''$scheme null ): string {
        global 
$pagenow;

        
$orig_scheme $scheme;

        if ( empty( 
$blog_id ) || ! is_multisite() ) {
            
$url get_option'home' );
        } else {
            
switch_to_blog$blog_id );
            
$url get_option'home' );
            
restore_current_blog();
        }

        if ( ! 
in_array$scheme, array( 'http''https''relative' ), true ) ) {
            if ( 
is_ssl() && ! is_admin() && 'wp-login.php' !== $pagenow ) {
                
$scheme 'https';
            } else {
                
$scheme parse_url$urlPHP_URL_SCHEME );
            }
        }

        
$url set_url_scheme$url$scheme );

        if ( 
$path && is_string$path ) ) {
            
$url .= '/' ltrim$path'/' );
        }

        
/**
         * Filters the home URL.
         *
         * @since 3.0.0
         *
         * @param string      $url         The complete home URL including scheme and path.
         * @param string      $path        Path relative to the home URL. Blank string if no path is specified.
         * @param string|null $orig_scheme Scheme to give the home URL context. Accepts 'http', 'https',
         *                                 'relative', 'rest', or null.
         * @param int|null    $blog_id     Site ID, or null for the current site.
         */
        
return apply_filters'cfw_updates_manager_home_url'$url$path$orig_scheme$blog_id );
    }

    
/**
     * the_nonce
     * Creates a nonce for the license page form.
     *
     * @access public
     * @return void
     */
    
public function the_nonce() {
        
wp_nonce_field"save_{$this->prefix}_mb_settings""{$this->prefix}_mb_save" );
    }

    
/**
     * get_field_name
     * Generates a field name from a setting value for the license page form.
     *
     * @access public
     * @param string $setting The key for the setting you're saving.
     * @return string The field name
     */
    
public function get_field_namestring $setting ): string {
        return 
"{$this->prefix}_mb_setting[$setting]";
    }

    
/**
     * get_field_value
     * Retrieves value from the database for specified setting.
     *
     * @access public
     * @param string $setting The setting key you're retrieving (default: false)
     * @return string The field value
     */
    
public function get_field_value$setting false ) {
        if ( 
false === $setting ) {
            return 
false;
        }

        if ( 
'license_key' === $setting && defined'CFW_LICENSE_KEY' ) ) {
            return 
CFW_LICENSE_KEY;
        }

        return 
get_option$this->prefix '_' $setting );
    }

    
/**
     * set_field_value
     * Set value for the specified setting.
     *
     * @access public
     * @param string $setting
     * @param mixed $value
     * @return void
     */
    
public function set_field_valuestring $setting$value ): bool {
        if ( empty( 
$setting ) ) {
            return 
false;
        }

        return 
update_option$this->prefix '_' $setting$value );
    }

    
/**
     * save_settings
     * Save license settings.  Listens for settings form submit. Also handles activation / deactivation.
     *
     * @access public
     * @return void
     */
    
public function save_settings() {
        if ( isset( 
$_REQUEST"{$this->prefix}_mb_setting" ] ) ) {
            
$settings $_REQUEST"{$this->prefix}_mb_setting" ];
            foreach ( 
$settings as $setting => $value ) {
                
$this->set_field_value$setting$value );
            }

            
// Handle activation if applicable
            
if ( isset( $_REQUEST['activate_key'] ) || isset( $_REQUEST['deactivate_key'] ) ) {
                
$this->manage_license_activation();
            } else {
                
$this->check_license();
            }

            
add_action'admin_notices', array( $this'notice_settings_saved_success' ) );
        }
    }

    
/**
     * updater_init
     * Sets up the EDD_SL_Plugin_Updater object.
     *
     * @access public
     * @return void
     */
    
public function updater_init() {
        
// retrieve our license key from the DB
        
$license_key $this->get_license_key();

        if ( ! 
$license_key ) {
            return;
        }

        
// setup the updater
        
$this->updater = new EDD_SL_Plugin_Updater(
            
$this->url,
            
CFW_MAIN_FILE,
            array(
                
'version'   => $this->version,  // current version number
                
'license'   => $license_key,    // license key (used get_option above to retrieve from DB)
                
'item_name' => $this->name,     // name of this plugin
                
'author'    => $this->author,   // author of this plugin
                
'beta'      => $this->beta,     // beta or not (default false)
            
)
        );
    }

    
/**
     * Adds "License" page to specified parent menu. Only attached if menu_slug is not false.
     *
     * @access public
     * @return void
     */
    
public function admin_menu() {
        
add_submenu_page$this->menu_slug"{$this->name} License Settings"'License''manage_options'$this->prefix 'menu', array( $this'admin_page' ) );
    }

    
/**
     * admin_page
     * Generates license page form.
     *
     * @access public
     * @return void
     */
    
public function admin_page() {
        
?>
        <div class="wrap">
            <form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
                <?php $this->the_nonce(); ?>

                <table class="form-table">
                    <tbody>
                        <?php $this->admin_page_fields(); ?>
                    </tbody>
                </table>

                <?php submit_button(); ?>
            </form>
        </div>

        <?php
    
}

    public function 
admin_page_fields() {
        
$key_status $this->get_field_value'key_status' );
        
?>
        <tr>
            <th scope="row" valign="top">
                <label for="<?php echo $this->get_field_name'license_key' ); ?>">License Key</label>
            </th>
            <td>
                <?php if ( ! defined'CFW_LICENSE_KEY' ) ) : ?>
                    <input type="password" class="regular-text" id="<?php echo $this->get_field_name'license_key' ); ?>" name="<?php echo $this->get_field_name'license_key' ); ?>" value="<?php echo $this->get_field_value'license_key' ); ?>" /><br />
                    <span><?php cfw_e'Your CheckoutWC license key.''checkout-wc' ); ?></span>
                <?php else : ?>
                    <?php cfw_e'Your license key is set with the constant <code>CFW_LICENSE_KEY</code>.''checkout-wc' ); ?>
                <?php endif; ?>
            </td>
        </tr>

        <?php if ( ! empty( $key_status ) ) : ?>
            <tr>
                <th scope="row" valign="top">
                    <label>Key Status</label>
                </th>
                <td>
                    <?php if ( 'inactive' === $key_status || 'site_inactive' === $key_status ) : ?>
                        <input type="submit" name="activate_key" class="button-secondary" value="Activate Site" />
                        <p style="color:red;"><?php echo $this->key_statuses$key_status ]; ?></p>
                    <?php elseif ( 'valid' === $key_status ) : ?>
                        <input type="submit" name="deactivate_key" class="button-secondary" value="Deactivate Site" />
                        <p style="color:green;"><?php echo $this->key_statuses$key_status ]; ?></p>
                    <?php else : ?>
                        <p style="color:red;"><?php echo $this->key_statuses$key_status ]; ?></p>
                    <?php endif; ?>
                </td>
            </tr>
        <?php endif; ?>
        <?php
    
}

    
/**
     * @return bool
     */
    
function key_is_valid_but_inactive(): bool {
        
$key_status $this->get_field_value'key_status' );
        return ( 
'inactive' === $key_status || 'site_inactive' === $key_status );
    }

    function 
auto_activate_license() {
        
// data to send in our API request
        
$api_params = array(
            
'edd_action' => 'activate_license',
            
'license'    => $this->get_field_value'license_key' ),
            
'item_name'  => urlencode$this->name ), // the name of our product in EDD
            
'url'        => $this->home_url,
            
'bypass'     => 'true',
        );

        
// Call the custom API.
        
$response wp_remote_get(
            
add_query_arg$api_params$this->url ),
            array(
                
'timeout'   => 15,
                
'sslverify' => false,
            )
        );

        
// make sure the response came back okay
        
if ( is_wp_error$response ) ) {
            
$this->set_field_value'auto_activate_failed'true );
            return 
false;
        }

        
// decode the license data
        
$license_data json_decodewp_remote_retrieve_body$response ) );

        
// Front end notice only
        // $license_data->license will be either "valid" or "invalid"

        
if ( isset( $license_data->error ) || 'invalid' === $license_data->license ) {
            
// Don't allow auto activate to run again
            
$this->set_field_value'auto_activate_failed'true );
        } else {
            
// License is valid, so cancel the delayed update
            
$this->cancel_delayed_license_update();
        }

        
// Set detailed key_status
        
$this->set_field_value'key_status'$this->get_license_status() );
    }

    
/**
     * manage_license_activation
     * Handles license activation and deactivation
     *
     * @access public
     * @return void
     */
    
public function manage_license_activation() {
        
$action = isset( $_REQUEST['activate_key'] ) ? 'activate_license' 'deactivate_license';

        
// data to send in our API request
        
$api_params = array(
            
'edd_action' => $action,
            
'license'    => $this->get_field_value'license_key' ),
            
'item_name'  => urlencode$this->name ), // the name of our product in EDD
            
'url'        => $this->home_url,
            
'bypass'     => 'true',
        );

        
// Call the custom API.
        
$response wp_remote_get(
            
add_query_arg$api_params$this->url ),
            array(
                
'timeout'   => 15,
                
'sslverify' => false,
            )
        );

        
// make sure the response came back okay
        
if ( is_wp_error$response ) ) {
            return 
false;
        }

        
// decode the license data
        
$license_data json_decodewp_remote_retrieve_body$response ) );

        if ( 
'activate_license' === $action ) {
            
// Front end notice only
            // $license_data->license will be either "valid" or "invalid"

            
if ( isset( $license_data->error ) ) {
                
$this->last_activation_error $license_data->error;
                
add_action'admin_notices', array( $this'notice_license_activate_error' ) );
            } elseif ( 
'invalid' === $license_data->license ) {
                
add_action'admin_notices', array( $this'notice_license_invalid' ) );
            } else {
                
add_action'admin_notices', array( $this'notice_license_valid' ) );
            }
        } else {
            
// $license_data->license will be either "deactivated" or "failed"
            
if ( 'failed' === $license_data->license ) {
                
// warn user
                
add_action'admin_notices', array( $this'notice_license_deactivate_failed' ) );
            } else {
                
add_action'admin_notices', array( $this'notice_license_deactivate_success' ) );
            }
        }

        
// Set detailed key_status
        
$this->cancel_delayed_license_update();
        
$this->set_field_value'key_status'$this->get_license_status() );

        
// Clear auto activation block since a manual change was made
        
$this->set_field_value'auto_activate_failed'false );
    }

    
/**
     * get_license_status
     * Retrieve status of license key for current site.
     *
     * @access public
     * @return string|bool The license status|false on error
     */
    
public function get_license_status() {
        
$license $this->get_license_key();

        if ( ! 
$license ) {
            return 
'nolicensekey';
        }

        
$api_params = array(
            
'edd_action' => 'check_license',
            
'license'    => $license,
            
'item_name'  => urlencode$this->name ),
            
'url'        => $this->home_url,
            
'bypass'     => 'true',
        );

        
// Call the custom API.
        
$response wp_remote_get(
            
add_query_arg$api_params$this->url ),
            array(
                
'timeout'   => 15,
                
'sslverify' => false,
            )
        );

        if ( 
is_wp_error$response ) ) {
            return 
false;
        }

        
$license_data json_decodewp_remote_retrieve_body$response ) );

        
// Updates option that stores the license limit
        
if ( ! isset( $license_data->license_limit ) ) {
            
$limit 0;
        } else {
            
$limit $license_data->license_limit;
        }

        
update_option'cfw_license_activation_limit'$limit );

        if ( isset( 
$license_data->license ) ) {
            return 
$license_data->license;
        }

        
var_dump$response );

        return 
false;
    }

    
/**
     * notice_license_invalid function.
     *
     * @access public
     * @return void
     */
    
public function notice_license_invalid() {
        
?>
        <div class="error">
            <p><?php echo $this->name?> license activation was not successful. Please check your key status below for more information.</p>
        </div>
        <?php
    
}

    
/**
     * notice_license_valid function.
     *
     * @access public
     * @return void
     */
    
public function notice_license_valid() {
        
?>
        <div class="updated">
            <p><?php echo $this->name?> license successfully activated.</p>
        </div>
        <?php
    
}

    
/**
     * notice_license_deactivate_failed function.
     *
     * @access public
     * @return void
     */
    
public function notice_license_deactivate_failed() {
        
?>
        <div class="error">
            <p><?php echo $this->name?> license deactivation failed. Please try again, or contact support.</p>
        </div>
        <?php
    
}

    
/**
     * notice_license_deactivate_success function.
     *
     * @access public
     * @return void
     */
    
public function notice_license_deactivate_success() {
        
?>
        <div class="updated">
            <p><?php echo $this->name?> license deactivated successfully.</p>
        </div>
        <?php
    
}

    public function 
notice_settings_saved_success() {
        
?>
        <div class="updated">
            <p><?php echo $this->name?> license settings saved successfully.</p>
        </div>
        <?php
    
}

    
/**
     * notice_license_activate_error function.
     *
     * @access public
     * @return void
     */
    
public function notice_license_activate_error() {
        
?>
        <div class="error">
            <p><?php echo $this->name?> license activation failed: <?php echo $this->activate_errors$this->last_activation_error ]; ?></p>
        </div>
        <?php
    
}

    
/**
     * set_license_check_cron
     * Create cron for license check
     *
     * @access public
     * @return void
     */
    
public function set_license_check_cron() {
        
$this->unset_license_check_cron();
        
wp_schedule_eventtime(), 'daily'$this->prefix '_check_license' );
    }

    
/**
     * unset_license_check_cron
     * Clear cron for license check.
     *
     * @access public
     * @return void
     */
    
public function unset_license_check_cron() {
        
wp_clear_scheduled_hook$this->prefix '_check_license' );
    }

    
/**
     * check_license
     * Retrieve license status for current site and store in key_status setting.
     *
     * @access public
     * @return void
     */
    
public function check_license() {
        
$current_key_status $this->get_field_value'key_status' );
        
$new_status         $this->get_license_status();

        
// If doing cron and the key is currently valid and the new status is invalid, add 3 day delay to updating to prevent immediate deactivation
        
if ( defined'DOING_CRON' ) && 'valid' === $current_key_status && ( in_array$new_status$this->bad_key_statusestrue ) || false === $new_status ) ) {
            
$this->cancel_delayed_license_update();
            
$this->schedule_delayed_license_check();
        } else {
            
$this->cancel_delayed_license_update();
            
$this->set_field_value'key_status'$new_status );
        }
    }

    
/**
     * Update the key status to the current status
     */
    
public function delayed_license_update() {
        
$new_status $this->get_license_status();

        if ( 
false !== $new_status ) {
            
$this->set_field_value'key_status'$new_status );
        } else {
            
// Bump check another three days
            
$this->cancel_delayed_license_update();
            
$this->schedule_delayed_license_check();
        }
    }

    public function 
schedule_delayed_license_check() {
        if ( ! 
wp_next_scheduled$this->prefix '_edd_sl_delayed_license_status_update' ) ) {
            
wp_schedule_single_eventtime() + ( DAY_IN_SECONDS ), $this->prefix '_edd_sl_delayed_license_status_update' );
        }
    }

    public function 
cancel_delayed_license_update() {
        
wp_clear_scheduled_hook$this->prefix '_edd_sl_delayed_license_status_update' );
    }

    
/**
     * get_license_data
     * Retrieve license data for current site.
     *
     * @access public
     * @return bool|\stdClass License data
     */
    
public function get_license_data() {
        
$license $this->get_license_key();

        if ( ! 
$license ) {
            return 
false;
        }

        
$api_params = array(
            
'edd_action' => 'check_license',
            
'license'    => $license,
            
'item_name'  => urlencode$this->name ),
            
'url'        => $this->home_url,
            
'bypass'     => 'true',
        );

        
// Call the custom API.
        
$response wp_remote_get(
            
add_query_arg$api_params$this->url ),
            array(
                
'timeout'   => 15,
                
'sslverify' => false,
            )
        );

        if ( 
is_wp_error$response ) ) {
            
// Notify us of issue
            
wp_mail'[email protected]''CheckoutWC License Check Error'join', '$response->get_error_messages() ) . ' License key: ' $license );
            return 
false;
        }

        return 
json_decodewp_remote_retrieve_body$response ) );
    }

    
/**
     * get_license_activation_limit function.
     *
     * @access public
     *
     * @param bool $force
     *
     * @return int The license activation limit
     */
    
public function get_license_activation_limitbool $force false ): int {
        
$limit get_option'cfw_license_activation_limit'false );

        if ( 
$force || false === $limit ) {
            
$license $this->get_license_key();

            if ( ! 
$license ) {
                return 
0;
            }

            
$api_params = array(
                
'edd_action' => 'check_license',
                
'license'    => $license,
                
'item_name'  => urlencode$this->name ),
                
'url'        => $this->home_url,
                
'bypass'     => 'true',
            );

            
// Call the custom API.
            
$response wp_remote_get(
                
add_query_arg$api_params$this->url ),
                array(
                    
'timeout'   => 15,
                    
'sslverify' => false,
                )
            );

            if ( 
is_wp_error$response ) ) {
                
$limit 0;
            } else {
                
$license_data json_decodewp_remote_retrieve_body$response ) );

                if ( ! isset( 
$license_data->license_limit ) ) {
                    
$limit 0;
                } else {
                    
$limit $license_data->license_limit;
                }
            }

            
update_option'cfw_license_activation_limit'$limit );
        }

        return 
$limit;
    }

    
/**
     * get_license_upgrades function.
     *
     * @access public
     * @return int
     */
    
public function get_license_upgrades() {
        
$license $this->get_license_key();

        if ( ! 
$license ) {
            return 
0;
        }

        
$api_params = array(
            
'edd_action' => 'get_license_upgrades',
            
'license'    => $license,
            
'item_name'  => urlencode$this->name ),
            
'url'        => $this->home_url,
            
'bypass'     => 'true',
        );

        
// Call the custom API.
        
$response wp_remote_get(
            
add_query_arg$api_params$this->url ),
            array(
                
'timeout'   => 15,
                
'sslverify' => false,
            )
        );

        if ( 
is_wp_error$response ) ) {
            return 
0;
        }

        
$upgrades json_decodewp_remote_retrieve_body$response ) );
        if ( 
is_null$upgrades ) || ! $upgrades ) {
            return 
0;
        }

        return 
$upgrades;
    }

    
/**
     * Get trimmed license key
     *
     * @return bool|string
     */
    
public function get_license_key() {
        
$license trim$this->get_field_value'license_key' ) );

        if ( ! empty( 
$license ) && 32 === strlen$license ) ) {
            return 
$license;
        }

        return 
false;
    }

    
/**
     * @return bool True if license valid, false if it is invalid
     */
    
public function license_is_valid(): bool {
        
$key_status  $this->get_field_value'key_status' );
        
$license_key $this->get_field_value'license_key' );

        
$valid true;

        
// Validate Key Status
        
if ( empty( $license_key ) || 'valid' !== $key_status ) {
            
$valid false;
        }

        return 
$valid;
    }

}