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