| @@ -1,5 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * @package Polylang | |
| 4 | + */ | |
| 2 | 5 | |
| 3 | 6 | /** |
| 4 | 7 | * A class to easily manage licenses for Polylang Pro and addons |
| 5 | 8 | * |
| @@ -5,10 +8,69 @@ | ||
| 5 | 8 | * |
| 6 | 9 | * @since 1.9 |
| 7 | 10 | */ |
| 8 | 11 | class PLL_License { |
| 9 | - public $id, $name, $license_key, $license_data; | |
| 10 | - private $file, $version, $author; | |
| 12 | + /** | |
| 13 | + * URL to Polylang's account page. | |
| 14 | + * | |
| 15 | + * @var string | |
| 16 | + */ | |
| 17 | + public const ACCOUNT_URL = 'https://polylang.pro/my-account/'; | |
| 18 | + | |
| 19 | + /** | |
| 20 | + * Sanitized plugin name. | |
| 21 | + * | |
| 22 | + * @var string | |
| 23 | + */ | |
| 24 | + public $id; | |
| 25 | + | |
| 26 | + /** | |
| 27 | + * Plugin name. | |
| 28 | + * | |
| 29 | + * @var string | |
| 30 | + */ | |
| 31 | + public $name; | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * License key. | |
| 35 | + * | |
| 36 | + * @var string | |
| 37 | + */ | |
| 38 | + public $license_key; | |
| 39 | + | |
| 40 | + /** | |
| 41 | + * License data, obtained from the API request. | |
| 42 | + * | |
| 43 | + * @var stdClass|null | |
| 44 | + */ | |
| 45 | + public $license_data; | |
| 46 | + | |
| 47 | + /** | |
| 48 | + * Main plugin file. | |
| 49 | + * | |
| 50 | + * @var string | |
| 51 | + */ | |
| 52 | + private $file; | |
| 53 | + | |
| 54 | + /** | |
| 55 | + * Current plugin version. | |
| 56 | + * | |
| 57 | + * @var string | |
| 58 | + */ | |
| 59 | + private $version; | |
| 60 | + | |
| 61 | + /** | |
| 62 | + * Plugin author. | |
| 63 | + * | |
| 64 | + * @var string | |
| 65 | + */ | |
| 66 | + private $author; | |
| 67 | + | |
| 68 | + /** | |
| 69 | + * API url. | |
| 70 | + * | |
| 71 | + * @var string. | |
| 72 | + */ | |
| 11 | 73 | private $api_url = 'https://polylang.pro'; |
| 12 | 74 | |
| 13 | 75 | /** |
| 14 | 76 | * Constructor |
| @@ -14,13 +76,13 @@ | ||
| 14 | 76 | * Constructor |
| 15 | 77 | * |
| 16 | 78 | * @since 1.9 |
| 17 | 79 | * |
| 18 | - * @param string $file | |
| 19 | - * @param string $item_name | |
| 20 | - * @param string $version | |
| 21 | - * @param string $author | |
| 22 | - * @param string $api_url optional | |
| 80 | + * @param string $file The plugin file. | |
| 81 | + * @param string $item_name The plugin name. | |
| 82 | + * @param string $version The plugin version. | |
| 83 | + * @param string $author Author name. | |
| 84 | + * @param string $api_url Optional url of the site managing the license. | |
| 23 | 85 | */ |
| 24 | 86 | public function __construct( $file, $item_name, $version, $author, $api_url = null ) { |
| 25 | 87 | $this->id = sanitize_title( $item_name ); |
| 26 | 88 | $this->file = $file; |
| @@ -28,17 +90,18 @@ | ||
| 28 | 90 | $this->version = $version; |
| 29 | 91 | $this->author = $author; |
| 30 | 92 | $this->api_url = empty( $api_url ) ? $this->api_url : $api_url; |
| 31 | 93 | |
| 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']; | |
| 94 | + $licenses = (array) get_option( 'polylang_licenses', array() ); | |
| 95 | + $license = isset( $licenses[ $this->id ] ) && is_array( $licenses[ $this->id ] ) ? $licenses[ $this->id ] : array(); | |
| 96 | + $this->license_key = ! empty( $license['key'] ) ? (string) $license['key'] : ''; | |
| 97 | + | |
| 98 | + if ( ! empty( $license['data'] ) ) { | |
| 99 | + $this->license_data = (object) $license['data']; | |
| 36 | 100 | } |
| 37 | 101 | |
| 38 | 102 | // Updater |
| 39 | - add_action( 'admin_init', array( $this, 'auto_updater' ), 0 ); | |
| 40 | - add_action( 'cli_init', array( $this, 'auto_updater' ), 0 ); // For WP CLI. | |
| 103 | + $this->auto_updater(); | |
| 41 | 104 | |
| 42 | 105 | // Register settings |
| 43 | 106 | add_filter( 'pll_settings_licenses', array( $this, 'settings' ) ); |
| 44 | 107 | |
| @@ -53,8 +116,10 @@ | ||
| 53 | 116 | /** |
| 54 | 117 | * Auto updater |
| 55 | 118 | * |
| 56 | 119 | * @since 1.9 |
| 120 | + * | |
| 121 | + * @return void | |
| 57 | 122 | */ |
| 58 | 123 | public function auto_updater() { |
| 59 | 124 | $args = array( |
| 60 | 125 | 'version' => $this->version, |
| @@ -67,14 +132,14 @@ | ||
| 67 | 132 | new PLL_Plugin_Updater( $this->api_url, $this->file, $args ); |
| 68 | 133 | } |
| 69 | 134 | |
| 70 | 135 | /** |
| 71 | - * Registers the licence in the Settings | |
| 136 | + * Registers the licence in the Settings. | |
| 72 | 137 | * |
| 73 | 138 | * @since 1.9 |
| 74 | 139 | * |
| 75 | - * @param array $items | |
| 76 | - * @return array | |
| 140 | + * @param PLL_License[] $items Array of objects allowing to manage a license. | |
| 141 | + * @return PLL_License[] | |
| 77 | 142 | */ |
| 78 | 143 | public function settings( $items ) { |
| 79 | 144 | $items[ $this->id ] = $this; |
| 80 | 145 | return $items; |
| @@ -80,31 +145,31 @@ | ||
| 80 | 145 | return $items; |
| 81 | 146 | } |
| 82 | 147 | |
| 83 | 148 | /** |
| 84 | - * Activate the license key | |
| 149 | + * Activates the license key. | |
| 85 | 150 | * |
| 86 | 151 | * @since 1.9 |
| 87 | 152 | * |
| 88 | - * @param string $license_key activation key | |
| 89 | - * @return object updated $this | |
| 153 | + * @param string $license_key Activation key. | |
| 154 | + * @return PLL_License Updated PLL_License object. | |
| 90 | 155 | */ |
| 91 | 156 | public function activate_license( $license_key ) { |
| 92 | 157 | $this->license_key = $license_key; |
| 93 | 158 | $this->api_request( 'activate_license' ); |
| 94 | 159 | |
| 95 | - // Tell WordPress to look for updates | |
| 96 | - set_site_transient( 'update_plugins', null ); | |
| 160 | + // Tell WordPress to look for updates. | |
| 161 | + delete_site_transient( 'update_plugins' ); | |
| 97 | 162 | return $this; |
| 98 | 163 | } |
| 99 | 164 | |
| 100 | 165 | |
| 101 | 166 | /** |
| 102 | - * Deactivate the license key | |
| 167 | + * Deactivates the license key. | |
| 103 | 168 | * |
| 104 | 169 | * @since 1.9 |
| 105 | 170 | * |
| 106 | - * @return object updated $this | |
| 171 | + * @return PLL_License Updated PLL_License object. | |
| 107 | 172 | */ |
| 108 | 173 | public function deactivate_license() { |
| 109 | 174 | $this->api_request( 'deactivate_license' ); |
| 110 | 175 | return $this; |
| @@ -110,17 +175,16 @@ | ||
| 110 | 175 | return $this; |
| 111 | 176 | } |
| 112 | 177 | |
| 113 | 178 | /** |
| 114 | - * Check if license key is valid | |
| 179 | + * Checks if the license key is valid. | |
| 115 | 180 | * |
| 116 | 181 | * @since 1.9 |
| 117 | 182 | * |
| 118 | - * @return object updated $this | |
| 183 | + * @return void | |
| 119 | 184 | */ |
| 120 | 185 | public function check_license() { |
| 121 | 186 | $this->api_request( 'check_license' ); |
| 122 | - return $this; | |
| 123 | 187 | } |
| 124 | 188 | |
| 125 | 189 | /** |
| 126 | 190 | * Sends an api request to check, activate or deactivate the license |
| @@ -128,8 +192,9 @@ | ||
| 128 | 192 | * |
| 129 | 193 | * @since 1.9 |
| 130 | 194 | * |
| 131 | 195 | * @param string $request check_license | activate_license | deactivate_license |
| 196 | + * @return void | |
| 132 | 197 | */ |
| 133 | 198 | private function api_request( $request ) { |
| 134 | 199 | $licenses = get_option( 'polylang_licenses' ); |
| 135 | 200 | |
| @@ -165,11 +230,11 @@ | ||
| 165 | 230 | } |
| 166 | 231 | |
| 167 | 232 | // Save new license info |
| 168 | 233 | $licenses[ $this->id ] = array( 'key' => $this->license_key ); |
| 169 | - $data = json_decode( wp_remote_retrieve_body( $response ) ); | |
| 234 | + $data = (object) json_decode( wp_remote_retrieve_body( $response ) ); | |
| 170 | 235 | |
| 171 | - if ( 'deactivated' !== $data->license ) { | |
| 236 | + if ( isset( $data->license ) && 'deactivated' !== $data->license ) { | |
| 172 | 237 | $licenses[ $this->id ]['data'] = $this->license_data = $data; |
| 173 | 238 | } |
| 174 | 239 | } |
| 175 | 240 | |
| @@ -187,13 +252,14 @@ | ||
| 187 | 252 | if ( ! empty( $this->license_data ) ) { |
| 188 | 253 | $license = $this->license_data; |
| 189 | 254 | } |
| 190 | 255 | |
| 191 | - $class = 'license-null'; | |
| 256 | + $class = 'license-null'; | |
| 257 | + $message = ''; | |
| 192 | 258 | |
| 193 | 259 | $out = sprintf( |
| 194 | 260 | '<td><label for="pll-licenses[%1$s]">%2$s</label></td>' . |
| 195 | - '<td><input name="licenses[%1$s]" id="pll-licenses[%1$s]" type="text" value="%3$s" class="regular-text code" />', | |
| 261 | + '<td><input name="licenses[%1$s]" id="pll-licenses[%1$s]" type="password" value="%3$s" class="regular-text code" />', | |
| 196 | 262 | esc_attr( $this->id ), |
| 197 | 263 | esc_attr( $this->name ), |
| 198 | 264 | esc_html( $this->license_key ) |
| 199 | 265 | ); |
| @@ -216,9 +282,9 @@ | ||
| 216 | 282 | $message = sprintf( |
| 217 | 283 | /* translators: %1$s is a date, %2$s is link start tag, %3$s is link end tag. */ |
| 218 | 284 | esc_html__( 'Your license key expired on %1$s. Please %2$srenew your license key%3$s.', 'polylang' ), |
| 219 | 285 | esc_html( date_i18n( get_option( 'date_format' ), $expiration ) ), |
| 220 | - sprintf( '<a href="%s" target="_blank">', esc_url( 'https://polylang.pro/checkout/?edd_license_key=' . $this->license_key ) ), | |
| 286 | + sprintf( '<a href="%s" target="_blank">', self::ACCOUNT_URL ), | |
| 221 | 287 | '</a>' |
| 222 | 288 | ); |
| 223 | 289 | break; |
| 224 | 290 | |
| @@ -230,9 +296,9 @@ | ||
| 230 | 296 | case 'missing': |
| 231 | 297 | $message = sprintf( |
| 232 | 298 | /* translators: %1$s is link start tag, %2$s is link end tag. */ |
| 233 | 299 | esc_html__( 'Invalid license. Please %1$svisit your account page%2$s and verify it.', 'polylang' ), |
| 234 | - sprintf( '<a href="%s" target="_blank">', 'https://polylang.pro/account' ), | |
| 300 | + sprintf( '<a href="%s" target="_blank">', self::ACCOUNT_URL ), | |
| 235 | 301 | '</a>' |
| 236 | 302 | ); |
| 237 | 303 | break; |
| 238 | 304 | |
| @@ -241,9 +307,9 @@ | ||
| 241 | 307 | $message = sprintf( |
| 242 | 308 | /* translators: %1$s is a product name, %2$s is link start tag, %3$s is link end tag. */ |
| 243 | 309 | esc_html__( 'Your %1$s license key is not active for this URL. Please %2$svisit your account page%3$s to manage your license key URLs.', 'polylang' ), |
| 244 | 310 | esc_html( $this->name ), |
| 245 | - sprintf( '<a href="%s" target="_blank">', 'https://polylang.pro/account' ), | |
| 311 | + sprintf( '<a href="%s" target="_blank">', self::ACCOUNT_URL ), | |
| 246 | 312 | '</a>' |
| 247 | 313 | ); |
| 248 | 314 | break; |
| 249 | 315 | |
| @@ -255,9 +321,9 @@ | ||
| 255 | 321 | case 'no_activations_left': |
| 256 | 322 | $message = sprintf( |
| 257 | 323 | /* translators: %1$s is link start tag, %2$s is link end tag */ |
| 258 | 324 | esc_html__( 'Your license key has reached its activation limit. %1$sView possible upgrades%2$s now.', 'polylang' ), |
| 259 | - sprintf( '<a href="%s" target="_blank">', 'https://polylang.pro/account' ), | |
| 325 | + sprintf( '<a href="%s" target="_blank">', self::ACCOUNT_URL ), | |
| 260 | 326 | '</a>' |
| 261 | 327 | ); |
| 262 | 328 | break; |
| 263 | 329 | } |
| @@ -271,11 +337,11 @@ | ||
| 271 | 337 | } elseif ( $expiration > $now && $expiration - $now < ( DAY_IN_SECONDS * 30 ) ) { |
| 272 | 338 | $class = 'notice-warning notice-alt'; |
| 273 | 339 | $message = sprintf( |
| 274 | 340 | /* translators: %1$s is a date, %2$s is link start tag, %3$s is link end tag. */ |
| 275 | - esc_html__( 'Your license key will expire soon! Precisely, it will expire on %1$s. %2$sRenew your license key today!%3$s.', 'polylang' ), | |
| 341 | + esc_html__( 'Your license key will expire soon! Precisely, it will expire on %1$s. %2$sRenew your license key today!%3$s', 'polylang' ), | |
| 276 | 342 | esc_html( date_i18n( get_option( 'date_format' ), $expiration ) ), |
| 277 | - sprintf( '<a href="%s" target="_blank">', esc_url( 'https://polylang.pro/checkout/?edd_license_key=' . $this->license_key ) ), | |
| 343 | + sprintf( '<a href="%s" target="_blank">', self::ACCOUNT_URL ), | |
| 278 | 344 | '</a>' |
| 279 | 345 | ); |
| 280 | 346 | } else { |
| 281 | 347 | $message = sprintf( |