| @@ -9,8 +9,15 @@ | ||
| 9 | 9 | * @since 1.9 |
| 10 | 10 | */ |
| 11 | 11 | class PLL_License { |
| 12 | 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 | + /** | |
| 13 | 20 | * Sanitized plugin name. |
| 14 | 21 | * |
| 15 | 22 | * @var string |
| 16 | 23 | */ |
| @@ -32,9 +39,9 @@ | ||
| 32 | 39 | |
| 33 | 40 | /** |
| 34 | 41 | * License data, obtained from the API request. |
| 35 | 42 | * |
| 36 | - * @var stdClass | |
| 43 | + * @var stdClass|null | |
| 37 | 44 | */ |
| 38 | 45 | public $license_data; |
| 39 | 46 | |
| 40 | 47 | /** |
| @@ -69,13 +76,13 @@ | ||
| 69 | 76 | * Constructor |
| 70 | 77 | * |
| 71 | 78 | * @since 1.9 |
| 72 | 79 | * |
| 73 | - * @param string $file | |
| 74 | - * @param string $item_name | |
| 75 | - * @param string $version | |
| 76 | - * @param string $author | |
| 77 | - * @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. | |
| 78 | 85 | */ |
| 79 | 86 | public function __construct( $file, $item_name, $version, $author, $api_url = null ) { |
| 80 | 87 | $this->id = sanitize_title( $item_name ); |
| 81 | 88 | $this->file = $file; |
| @@ -83,17 +90,18 @@ | ||
| 83 | 90 | $this->version = $version; |
| 84 | 91 | $this->author = $author; |
| 85 | 92 | $this->api_url = empty( $api_url ) ? $this->api_url : $api_url; |
| 86 | 93 | |
| 87 | - $licenses = get_option( 'polylang_licenses' ); | |
| 88 | - $this->license_key = empty( $licenses[ $this->id ]['key'] ) ? '' : $licenses[ $this->id ]['key']; | |
| 89 | - if ( ! empty( $licenses[ $this->id ]['data'] ) ) { | |
| 90 | - $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']; | |
| 91 | 100 | } |
| 92 | 101 | |
| 93 | 102 | // Updater |
| 94 | - add_action( 'admin_init', array( $this, 'auto_updater' ), 0 ); | |
| 95 | - add_action( 'cli_init', array( $this, 'auto_updater' ), 0 ); // For WP CLI. | |
| 103 | + $this->auto_updater(); | |
| 96 | 104 | |
| 97 | 105 | // Register settings |
| 98 | 106 | add_filter( 'pll_settings_licenses', array( $this, 'settings' ) ); |
| 99 | 107 | |
| @@ -124,14 +132,14 @@ | ||
| 124 | 132 | new PLL_Plugin_Updater( $this->api_url, $this->file, $args ); |
| 125 | 133 | } |
| 126 | 134 | |
| 127 | 135 | /** |
| 128 | - * Registers the licence in the Settings | |
| 136 | + * Registers the licence in the Settings. | |
| 129 | 137 | * |
| 130 | 138 | * @since 1.9 |
| 131 | 139 | * |
| 132 | - * @param array $items | |
| 133 | - * @return array | |
| 140 | + * @param PLL_License[] $items Array of objects allowing to manage a license. | |
| 141 | + * @return PLL_License[] | |
| 134 | 142 | */ |
| 135 | 143 | public function settings( $items ) { |
| 136 | 144 | $items[ $this->id ] = $this; |
| 137 | 145 | return $items; |
| @@ -137,31 +145,31 @@ | ||
| 137 | 145 | return $items; |
| 138 | 146 | } |
| 139 | 147 | |
| 140 | 148 | /** |
| 141 | - * Activate the license key | |
| 149 | + * Activates the license key. | |
| 142 | 150 | * |
| 143 | 151 | * @since 1.9 |
| 144 | 152 | * |
| 145 | - * @param string $license_key activation key | |
| 146 | - * @return object updated $this | |
| 153 | + * @param string $license_key Activation key. | |
| 154 | + * @return PLL_License Updated PLL_License object. | |
| 147 | 155 | */ |
| 148 | 156 | public function activate_license( $license_key ) { |
| 149 | 157 | $this->license_key = $license_key; |
| 150 | 158 | $this->api_request( 'activate_license' ); |
| 151 | 159 | |
| 152 | - // Tell WordPress to look for updates | |
| 153 | - set_site_transient( 'update_plugins', null ); | |
| 160 | + // Tell WordPress to look for updates. | |
| 161 | + delete_site_transient( 'update_plugins' ); | |
| 154 | 162 | return $this; |
| 155 | 163 | } |
| 156 | 164 | |
| 157 | 165 | |
| 158 | 166 | /** |
| 159 | - * Deactivate the license key | |
| 167 | + * Deactivates the license key. | |
| 160 | 168 | * |
| 161 | 169 | * @since 1.9 |
| 162 | 170 | * |
| 163 | - * @return object updated $this | |
| 171 | + * @return PLL_License Updated PLL_License object. | |
| 164 | 172 | */ |
| 165 | 173 | public function deactivate_license() { |
| 166 | 174 | $this->api_request( 'deactivate_license' ); |
| 167 | 175 | return $this; |
| @@ -167,17 +175,16 @@ | ||
| 167 | 175 | return $this; |
| 168 | 176 | } |
| 169 | 177 | |
| 170 | 178 | /** |
| 171 | - * Check if license key is valid | |
| 179 | + * Checks if the license key is valid. | |
| 172 | 180 | * |
| 173 | 181 | * @since 1.9 |
| 174 | 182 | * |
| 175 | - * @return object updated $this | |
| 183 | + * @return void | |
| 176 | 184 | */ |
| 177 | 185 | public function check_license() { |
| 178 | 186 | $this->api_request( 'check_license' ); |
| 179 | - return $this; | |
| 180 | 187 | } |
| 181 | 188 | |
| 182 | 189 | /** |
| 183 | 190 | * Sends an api request to check, activate or deactivate the license |
| @@ -223,11 +230,11 @@ | ||
| 223 | 230 | } |
| 224 | 231 | |
| 225 | 232 | // Save new license info |
| 226 | 233 | $licenses[ $this->id ] = array( 'key' => $this->license_key ); |
| 227 | - $data = json_decode( wp_remote_retrieve_body( $response ) ); | |
| 234 | + $data = (object) json_decode( wp_remote_retrieve_body( $response ) ); | |
| 228 | 235 | |
| 229 | - if ( 'deactivated' !== $data->license ) { | |
| 236 | + if ( isset( $data->license ) && 'deactivated' !== $data->license ) { | |
| 230 | 237 | $licenses[ $this->id ]['data'] = $this->license_data = $data; |
| 231 | 238 | } |
| 232 | 239 | } |
| 233 | 240 | |
| @@ -275,9 +282,9 @@ | ||
| 275 | 282 | $message = sprintf( |
| 276 | 283 | /* translators: %1$s is a date, %2$s is link start tag, %3$s is link end tag. */ |
| 277 | 284 | esc_html__( 'Your license key expired on %1$s. Please %2$srenew your license key%3$s.', 'polylang' ), |
| 278 | 285 | esc_html( date_i18n( get_option( 'date_format' ), $expiration ) ), |
| 279 | - sprintf( '<a href="%s" target="_blank">', 'https://polylang.pro/account/' ), | |
| 286 | + sprintf( '<a href="%s" target="_blank">', self::ACCOUNT_URL ), | |
| 280 | 287 | '</a>' |
| 281 | 288 | ); |
| 282 | 289 | break; |
| 283 | 290 | |
| @@ -289,9 +296,9 @@ | ||
| 289 | 296 | case 'missing': |
| 290 | 297 | $message = sprintf( |
| 291 | 298 | /* translators: %1$s is link start tag, %2$s is link end tag. */ |
| 292 | 299 | esc_html__( 'Invalid license. Please %1$svisit your account page%2$s and verify it.', 'polylang' ), |
| 293 | - sprintf( '<a href="%s" target="_blank">', 'https://polylang.pro/account/' ), | |
| 300 | + sprintf( '<a href="%s" target="_blank">', self::ACCOUNT_URL ), | |
| 294 | 301 | '</a>' |
| 295 | 302 | ); |
| 296 | 303 | break; |
| 297 | 304 | |
| @@ -300,9 +307,9 @@ | ||
| 300 | 307 | $message = sprintf( |
| 301 | 308 | /* translators: %1$s is a product name, %2$s is link start tag, %3$s is link end tag. */ |
| 302 | 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' ), |
| 303 | 310 | esc_html( $this->name ), |
| 304 | - sprintf( '<a href="%s" target="_blank">', 'https://polylang.pro/account/' ), | |
| 311 | + sprintf( '<a href="%s" target="_blank">', self::ACCOUNT_URL ), | |
| 305 | 312 | '</a>' |
| 306 | 313 | ); |
| 307 | 314 | break; |
| 308 | 315 | |
| @@ -314,9 +321,9 @@ | ||
| 314 | 321 | case 'no_activations_left': |
| 315 | 322 | $message = sprintf( |
| 316 | 323 | /* translators: %1$s is link start tag, %2$s is link end tag */ |
| 317 | 324 | esc_html__( 'Your license key has reached its activation limit. %1$sView possible upgrades%2$s now.', 'polylang' ), |
| 318 | - sprintf( '<a href="%s" target="_blank">', 'https://polylang.pro/account/' ), | |
| 325 | + sprintf( '<a href="%s" target="_blank">', self::ACCOUNT_URL ), | |
| 319 | 326 | '</a>' |
| 320 | 327 | ); |
| 321 | 328 | break; |
| 322 | 329 | } |
| @@ -330,11 +337,11 @@ | ||
| 330 | 337 | } elseif ( $expiration > $now && $expiration - $now < ( DAY_IN_SECONDS * 30 ) ) { |
| 331 | 338 | $class = 'notice-warning notice-alt'; |
| 332 | 339 | $message = sprintf( |
| 333 | 340 | /* translators: %1$s is a date, %2$s is link start tag, %3$s is link end tag. */ |
| 334 | - 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' ), | |
| 335 | 342 | esc_html( date_i18n( get_option( 'date_format' ), $expiration ) ), |
| 336 | - sprintf( '<a href="%s" target="_blank">', 'https://polylang.pro/account/' ), | |
| 343 | + sprintf( '<a href="%s" target="_blank">', self::ACCOUNT_URL ), | |
| 337 | 344 | '</a>' |
| 338 | 345 | ); |
| 339 | 346 | } else { |
| 340 | 347 | $message = sprintf( |