helper.php
122 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ShopEngine\Libs\License; |
| 4 | |
| 5 | use ShopEngine\Traits\Singleton; |
| 6 | |
| 7 | defined('ABSPATH') || exit; |
| 8 | |
| 9 | /** |
| 10 | * Allows plugins to use their own update API. |
| 11 | * |
| 12 | * @version 1.1.4 |
| 13 | */ |
| 14 | class Helper { |
| 15 | |
| 16 | use Singleton; |
| 17 | |
| 18 | public function activate($key) { |
| 19 | |
| 20 | $data = [ |
| 21 | 'key' => $key, |
| 22 | 'id' => \ShopEngine_Pro::product_id(), |
| 23 | ]; |
| 24 | |
| 25 | $response = $this->check_license($data); |
| 26 | |
| 27 | if(isset($response->validate) && $response->validate == 1) { |
| 28 | |
| 29 | update_option('__shopengine_oppai__', $response->oppai); |
| 30 | update_option('__shopengine_license_key__', $response->key); |
| 31 | |
| 32 | $response->is_activated = true; |
| 33 | |
| 34 | return $response; |
| 35 | } |
| 36 | |
| 37 | return $response; |
| 38 | } |
| 39 | |
| 40 | public function deactivate() { |
| 41 | |
| 42 | delete_option('__shopengine_oppai__'); |
| 43 | update_option('__shopengine_license_key__', ''); |
| 44 | |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | |
| 49 | public function check_license($data = []) { |
| 50 | |
| 51 | if(strlen($data['key']) < 28) { |
| 52 | $data['error'] = 'yes'; |
| 53 | $data['message'] = 'Invalid license key'; |
| 54 | |
| 55 | return (object)$data; |
| 56 | } |
| 57 | |
| 58 | $data['oppai'] = get_option('__shopengine_oppai__'); |
| 59 | $data['action'] = 'activate'; |
| 60 | $data['marketplace'] = \ShopEngine_Pro::store_name(); |
| 61 | $data['author_name'] = \ShopEngine_Pro::author_name(); |
| 62 | $data['v'] = \ShopEngine_Pro::version(); |
| 63 | |
| 64 | $url = \ShopEngine_Pro::api_url() . 'license?' . http_build_query($data); |
| 65 | |
| 66 | $args = [ |
| 67 | 'timeout' => 60, |
| 68 | 'redirection' => 3, |
| 69 | 'httpversion' => '1.0', |
| 70 | 'blocking' => true, |
| 71 | 'sslverify' => true, |
| 72 | ]; |
| 73 | |
| 74 | |
| 75 | $res = wp_remote_get($url, $args); |
| 76 | |
| 77 | return (object)json_decode((string)$res['body']); |
| 78 | } |
| 79 | |
| 80 | public function status() { |
| 81 | |
| 82 | $cached = wp_cache_get('shopengine_pro__license_status'); |
| 83 | |
| 84 | if(false !== $cached) { |
| 85 | return $cached; |
| 86 | } |
| 87 | |
| 88 | $oppai = get_option('__shopengine_oppai__', ''); |
| 89 | $key = get_option('__shopengine_license_key__', ''); |
| 90 | $status = 'invalid'; |
| 91 | |
| 92 | if($oppai != '' && $key != '') { |
| 93 | $status = 'valid'; |
| 94 | } |
| 95 | |
| 96 | wp_cache_set('shopengine_pro__license_status', $status); |
| 97 | |
| 98 | return $status; |
| 99 | } |
| 100 | |
| 101 | public function get_license() { |
| 102 | |
| 103 | $cached = wp_cache_get('shopengine_pro__license_key'); |
| 104 | |
| 105 | if(false !== $cached) { |
| 106 | return $cached; |
| 107 | } |
| 108 | |
| 109 | $oppai = get_option('__shopengine_oppai__'); |
| 110 | $key = get_option('__shopengine_license_key__'); |
| 111 | |
| 112 | $license = [ |
| 113 | 'checksum' => $oppai, |
| 114 | 'key' => $key, |
| 115 | ]; |
| 116 | |
| 117 | wp_cache_set('shopengine_pro__license_key', $license); |
| 118 | |
| 119 | return $license; |
| 120 | } |
| 121 | } |
| 122 |