| @@ -28,12 +28,8 @@ | ||
| 28 | 28 | protected $new_days = 90; |
| 29 | 29 | |
| 30 | 30 | /** |
| 31 | 31 | * @since 3.06 |
| 32 | - * | |
| 33 | - * @param string|null $license The license key. | |
| 34 | - * | |
| 35 | - * @return void | |
| 36 | 32 | */ |
| 37 | 33 | public function __construct( $license = null ) { |
| 38 | 34 | $this->set_license( $license ); |
| 39 | 35 | $this->set_cache_key(); |
| @@ -41,17 +37,14 @@ | ||
| 41 | 37 | |
| 42 | 38 | /** |
| 43 | 39 | * @since 3.06 |
| 44 | 40 | * |
| 45 | - * @param string|null $license The license key. | |
| 46 | - * | |
| 47 | 41 | * @return void |
| 48 | 42 | */ |
| 49 | 43 | private function set_license( $license ) { |
| 50 | 44 | if ( $license === null ) { |
| 51 | 45 | $edd_update = $this->get_pro_updater(); |
| 52 | - | |
| 53 | - if ( $edd_update ) { | |
| 46 | + if ( ! empty( $edd_update ) ) { | |
| 54 | 47 | $license = $edd_update->license; |
| 55 | 48 | } |
| 56 | 49 | } |
| 57 | 50 | $this->license = $license; |
| @@ -58,9 +51,8 @@ | ||
| 58 | 51 | } |
| 59 | 52 | |
| 60 | 53 | /** |
| 61 | 54 | * @since 3.06 |
| 62 | - * | |
| 63 | 55 | * @return string |
| 64 | 56 | */ |
| 65 | 57 | public function get_license() { |
| 66 | 58 | return $this->license; |
| @@ -76,9 +68,8 @@ | ||
| 76 | 68 | } |
| 77 | 69 | |
| 78 | 70 | /** |
| 79 | 71 | * @since 3.06 |
| 80 | - * | |
| 81 | 72 | * @return string |
| 82 | 73 | */ |
| 83 | 74 | public function get_cache_key() { |
| 84 | 75 | return $this->cache_key; |
| @@ -85,20 +76,17 @@ | ||
| 85 | 76 | } |
| 86 | 77 | |
| 87 | 78 | /** |
| 88 | 79 | * @since 3.06 |
| 89 | - * | |
| 90 | 80 | * @return array |
| 91 | 81 | */ |
| 92 | 82 | public function get_api_info() { |
| 93 | 83 | $url = $this->api_url(); |
| 94 | - | |
| 95 | 84 | if ( ! empty( $this->license ) ) { |
| 96 | 85 | $url .= '?l=' . urlencode( base64_encode( $this->license ) ); |
| 97 | 86 | } |
| 98 | 87 | |
| 99 | 88 | $addons = $this->get_cached(); |
| 100 | - | |
| 101 | 89 | if ( is_array( $addons ) ) { |
| 102 | 90 | return $addons; |
| 103 | 91 | } |
| 104 | 92 | |
| @@ -110,9 +98,8 @@ | ||
| 110 | 98 | $this->set_running(); |
| 111 | 99 | |
| 112 | 100 | // We need to know the version number to allow different downloads. |
| 113 | 101 | $agent = 'formidable/' . FrmAppHelper::plugin_version(); |
| 114 | - | |
| 115 | 102 | if ( class_exists( 'FrmProDb' ) ) { |
| 116 | 103 | $agent = 'formidable-pro/' . FrmProDb::$plug_version; |
| 117 | 104 | } |
| 118 | 105 | |
| @@ -140,10 +127,9 @@ | ||
| 140 | 127 | } |
| 141 | 128 | |
| 142 | 129 | if ( isset( $addon['categories'] ) ) { |
| 143 | 130 | $cats = array_intersect( $this->skip_categories(), $addon['categories'] ); |
| 144 | - | |
| 145 | - if ( $cats ) { | |
| 131 | + if ( ! empty( $cats ) ) { | |
| 146 | 132 | unset( $addons[ $k ] ); |
| 147 | 133 | continue; |
| 148 | 134 | } |
| 149 | 135 | } |
| @@ -179,9 +165,8 @@ | ||
| 179 | 165 | * @return void |
| 180 | 166 | */ |
| 181 | 167 | protected function set_running() { |
| 182 | 168 | $expires = 2 * MINUTE_IN_SECONDS; |
| 183 | - | |
| 184 | 169 | if ( $this->run_as_multisite() ) { |
| 185 | 170 | set_site_transient( $this->transient_key(), true, $expires ); |
| 186 | 171 | return; |
| 187 | 172 | } |
| @@ -218,9 +203,9 @@ | ||
| 218 | 203 | * |
| 219 | 204 | * @return string |
| 220 | 205 | */ |
| 221 | 206 | protected function transient_key() { |
| 222 | - return strtolower( self::class ) . '_request_lock'; | |
| 207 | + return strtolower( __CLASS__ ) . '_request_lock'; | |
| 223 | 208 | } |
| 224 | 209 | |
| 225 | 210 | /** |
| 226 | 211 | * @since 3.06 |
| @@ -252,16 +237,14 @@ | ||
| 252 | 237 | * |
| 253 | 238 | * @return array |
| 254 | 239 | */ |
| 255 | 240 | public function get_addon_for_license( $license_plugin, $addons = array() ) { |
| 256 | - if ( ! $addons ) { | |
| 241 | + if ( empty( $addons ) ) { | |
| 257 | 242 | $addons = $this->get_api_info(); |
| 258 | 243 | } |
| 259 | - | |
| 260 | 244 | $download_id = $license_plugin->download_id; |
| 261 | 245 | $plugin = array(); |
| 262 | - | |
| 263 | - if ( ! $download_id && ! empty( $addons ) ) { | |
| 246 | + if ( empty( $download_id ) && ! empty( $addons ) ) { | |
| 264 | 247 | foreach ( $addons as $addon ) { |
| 265 | 248 | if ( is_array( $addon ) && ! empty( $addon['title'] ) && strtolower( $license_plugin->plugin_name ) === strtolower( $addon['title'] ) ) { |
| 266 | 249 | return $addon; |
| 267 | 250 | } |
| @@ -274,10 +257,8 @@ | ||
| 274 | 257 | } |
| 275 | 258 | |
| 276 | 259 | /** |
| 277 | 260 | * @since 3.06 |
| 278 | - * | |
| 279 | - * @return false|object | |
| 280 | 261 | */ |
| 281 | 262 | public function get_pro_updater() { |
| 282 | 263 | if ( FrmAppHelper::pro_is_installed() && is_callable( 'FrmProAppHelper::get_updater' ) ) { |
| 283 | 264 | $updater = FrmProAppHelper::get_updater(); |
| @@ -295,27 +276,30 @@ | ||
| 295 | 276 | * @return array|bool |
| 296 | 277 | */ |
| 297 | 278 | protected function get_cached() { |
| 298 | 279 | $cache = $this->get_cached_option(); |
| 299 | - | |
| 300 | - if ( ! $cache ) { | |
| 280 | + if ( empty( $cache ) ) { | |
| 301 | 281 | return false; |
| 302 | 282 | } |
| 303 | 283 | |
| 304 | - $is_expired = empty( $cache['timeout'] ) || time() > $cache['timeout']; | |
| 284 | + // If the api call is running, we can use the expired cache. | |
| 285 | + if ( ! $this->is_running() ) { | |
| 286 | + if ( empty( $cache['timeout'] ) || time() > $cache['timeout'] ) { | |
| 287 | + // Cache is expired. | |
| 288 | + return false; | |
| 289 | + } | |
| 305 | 290 | |
| 306 | - if ( ! $is_expired && isset( $cache['version'] ) && $cache['version'] !== FrmAppHelper::plugin_version() ) { | |
| 307 | - $is_expired = true; | |
| 291 | + $version = FrmAppHelper::plugin_version(); | |
| 292 | + $for_current = isset( $cache['version'] ) && $cache['version'] == $version; | |
| 293 | + if ( ! $for_current ) { | |
| 294 | + // Force a new check. | |
| 295 | + return false; | |
| 296 | + } | |
| 308 | 297 | } |
| 309 | 298 | |
| 310 | - // Avoid old cached data, unless we're currently trying to query for new data. | |
| 311 | - // The call to $this->is_running likely triggers a database query, so only call if if we're expired. | |
| 312 | - // (Rather than the other way around, which is less efficient). | |
| 313 | - if ( $is_expired && ! $this->is_running() ) { | |
| 314 | - return false; | |
| 315 | - } | |
| 299 | + $values = json_decode( $cache['value'], true ); | |
| 316 | 300 | |
| 317 | - return json_decode( $cache['value'], true ); | |
| 301 | + return $values; | |
| 318 | 302 | } |
| 319 | 303 | |
| 320 | 304 | /** |
| 321 | 305 | * Get the cache for the network if multisite. |
| @@ -326,9 +310,8 @@ | ||
| 326 | 310 | */ |
| 327 | 311 | protected function get_cached_option() { |
| 328 | 312 | if ( is_multisite() ) { |
| 329 | 313 | $cached = get_site_option( $this->cache_key ); |
| 330 | - | |
| 331 | 314 | if ( $cached ) { |
| 332 | 315 | return $cached; |
| 333 | 316 | } |
| 334 | 317 | } |
| @@ -352,11 +335,9 @@ | ||
| 352 | 335 | |
| 353 | 336 | if ( is_multisite() ) { |
| 354 | 337 | update_site_option( $this->cache_key, $data ); |
| 355 | 338 | } else { |
| 356 | - // Autoload the license cache because it gets called everywhere. | |
| 357 | - $autoload = str_starts_with( $this->cache_key, 'frm_addons_l' ); | |
| 358 | - update_option( $this->cache_key, $data, $autoload ); | |
| 339 | + update_option( $this->cache_key, $data, 'no' ); | |
| 359 | 340 | } |
| 360 | 341 | } |
| 361 | 342 | |
| 362 | 343 | /** |
| @@ -368,12 +349,13 @@ | ||
| 368 | 349 | * |
| 369 | 350 | * @return string |
| 370 | 351 | */ |
| 371 | 352 | protected function get_cache_timeout( $addons ) { |
| 353 | + $timeout = $this->cache_timeout; | |
| 372 | 354 | if ( isset( $addons['response_code'] ) && 429 === $addons['response_code'] ) { |
| 373 | - return '+5 minutes'; | |
| 355 | + $timeout = '+5 minutes'; | |
| 374 | 356 | } |
| 375 | - return $this->cache_timeout; | |
| 357 | + return $timeout; | |
| 376 | 358 | } |
| 377 | 359 | |
| 378 | 360 | /** |
| 379 | 361 | * @since 3.06 |
| @@ -390,32 +372,28 @@ | ||
| 390 | 372 | } |
| 391 | 373 | |
| 392 | 374 | /** |
| 393 | 375 | * @since 3.06 |
| 394 | - * | |
| 395 | 376 | * @return array |
| 396 | 377 | */ |
| 397 | 378 | public function error_for_license() { |
| 379 | + $errors = array(); | |
| 398 | 380 | if ( ! empty( $this->license ) ) { |
| 399 | - return $this->get_error_from_response(); | |
| 381 | + $errors = $this->get_error_from_response(); | |
| 400 | 382 | } |
| 401 | - return array(); | |
| 383 | + | |
| 384 | + return $errors; | |
| 402 | 385 | } |
| 403 | 386 | |
| 404 | 387 | /** |
| 405 | 388 | * @since 3.06 |
| 406 | - * | |
| 407 | - * @param array $addons | |
| 408 | - * | |
| 409 | 389 | * @return array |
| 410 | 390 | */ |
| 411 | 391 | public function get_error_from_response( $addons = array() ) { |
| 412 | - if ( ! $addons ) { | |
| 392 | + if ( empty( $addons ) ) { | |
| 413 | 393 | $addons = $this->get_api_info(); |
| 414 | 394 | } |
| 415 | - | |
| 416 | 395 | $errors = array(); |
| 417 | - | |
| 418 | 396 | if ( isset( $addons['error'] ) ) { |
| 419 | 397 | if ( is_string( $addons['error'] ) ) { |
| 420 | 398 | $errors[] = $addons['error']; |
| 421 | 399 | } elseif ( ! empty( $addons['error']['message'] ) ) { |
| @@ -433,9 +411,8 @@ | ||
| 433 | 411 | * |
| 434 | 412 | * @since 6.0 |
| 435 | 413 | * |
| 436 | 414 | * @param array $addon |
| 437 | - * | |
| 438 | 415 | * @return bool |
| 439 | 416 | */ |
| 440 | 417 | protected function is_new( $addon ) { |
| 441 | 418 | return strtotime( $addon['released'] ) > strtotime( '-' . $this->new_days . ' days' ); |