PluginProbe
Polylang / 2.5.2
Polylang v2.5.2
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / include / license.php

license.php in Polylang 2.5.2, at include/license.php

173 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * A class to easily manage licenses for Polylang Pro and addons
5 *
6 * @since 1.9
7 */
8 class PLL_License {
9 public $id, $name, $license_key, $license_data;
10 private $file, $version, $author;
11 private $api_url = 'https://polylang.pro';
12
13 /**
14 * Constructor
15 *
16 * @since 1.9
17 *
18 * @param string $file
19 * @param string $item_name
20 * @param string $version
21 * @param string $author
22 * @param string $api_url optional
23 */
24 public function __construct( $file, $item_name, $version, $author, $api_url = null ) {
25 $this->id = sanitize_title( $item_name );
26 $this->file = $file;
27 $this->name = $item_name;
28 $this->version = $version;
29 $this->author = $author;
30 $this->api_url = empty( $api_url ) ? $this->api_url : $api_url;
31
32 $licenses = get_option( 'polylang_licenses' );
33 $this->license_key = empty( $licenses[ $this->id ]['key'] ) ? '' : $licenses[ $this->id ]['key'];
34 if ( ! empty( $licenses[ $this->id ]['data'] ) ) {
35 $this->license_data = $licenses[ $this->id ]['data'];
36 }
37
38 // Updater
39 add_action( 'admin_init', array( $this, 'auto_updater' ), 0 );
40 add_action( 'cli_init', array( $this, 'auto_updater' ), 0 ); // For WP CLI.
41
42 // Register settings
43 add_filter( 'pll_settings_licenses', array( $this, 'settings' ) );
44
45 // Weekly schedule
46 if ( ! wp_next_scheduled( 'polylang_check_licenses' ) ) {
47 wp_schedule_event( time(), 'weekly', 'polylang_check_licenses' );
48 }
49
50 add_action( 'polylang_check_licenses', array( $this, 'check_license' ) );
51 }
52
53 /**
54 * Auto updater
55 *
56 * @since 1.9
57 */
58 public function auto_updater() {
59 $args = array(
60 'version' => $this->version,
61 'license' => $this->license_key,
62 'author' => $this->author,
63 'item_name' => $this->name,
64 );
65
66 // Setup the updater
67 new PLL_Plugin_Updater( $this->api_url, $this->file, $args );
68 }
69
70 /**
71 * Registers the licence in the Settings
72 *
73 * @since 1.9
74 *
75 * @param array $items
76 * @return array
77 */
78 public function settings( $items ) {
79 $items[ $this->id ] = $this;
80 return $items;
81 }
82
83 /**
84 * Activate the license key
85 *
86 * @since 1.9
87 *
88 * @param string $license_key activation key
89 * @return object updated $this
90 */
91 public function activate_license( $license_key ) {
92 $this->license_key = $license_key;
93 $this->api_request( 'activate_license' );
94
95 // Tell WordPress to look for updates
96 set_site_transient( 'update_plugins', null );
97 return $this;
98 }
99
100
101 /**
102 * Deactivate the license key
103 *
104 * @since 1.9
105 *
106 * @return object updated $this
107 */
108 public function deactivate_license() {
109 $this->api_request( 'deactivate_license' );
110 return $this;
111 }
112
113 /**
114 * Check if license key is valid
115 *
116 * @since 1.9
117 *
118 * @return object updated $this
119 */
120 public function check_license() {
121 $this->api_request( 'check_license' );
122 return $this;
123 }
124
125 /**
126 * Sends an api request to check, activate or deactivate the license
127 * Updates the licenses option according to the status
128 *
129 * @since 1.9
130 *
131 * @param string $request check_license | activate_license | deactivate_license
132 */
133 private function api_request( $request ) {
134 $licenses = get_option( 'polylang_licenses' );
135 unset( $licenses[ $this->id ], $this->license_data );
136
137 if ( ! empty( $this->license_key ) ) {
138 // Data to send in our API request
139 $api_params = array(
140 'edd_action' => $request,
141 'license' => $this->license_key,
142 'item_name' => urlencode( $this->name ),
143 'url' => home_url(),
144 );
145
146 // Call the API
147 $response = wp_remote_post(
148 $this->api_url,
149 array(
150 'timeout' => 15,
151 'sslverify' => false,
152 'body' => $api_params,
153 )
154 );
155
156 // Update the option only if we got a response
157 if ( is_wp_error( $response ) ) {
158 return;
159 }
160
161 // Save new license info
162 $licenses[ $this->id ] = array( 'key' => $this->license_key );
163 $data = json_decode( wp_remote_retrieve_body( $response ) );
164
165 if ( 'deactivated' !== $data->license ) {
166 $licenses[ $this->id ]['data'] = $this->license_data = $data;
167 }
168 }
169
170 update_option( 'polylang_licenses', $licenses ); // FIXME called multiple times when saving all licenses
171 }
172 }
173