| @@ -27,21 +27,9 @@ | ||
| 27 | 27 | */ |
| 28 | 28 | protected $new_days = 90; |
| 29 | 29 | |
| 30 | 30 | /** |
| 31 | - * If true, calls to get_api_info will bypass cache. | |
| 32 | - * This is set true by calling force_api_request. | |
| 33 | - * | |
| 34 | - * @var bool | |
| 35 | - */ | |
| 36 | - protected $force = false; | |
| 37 | - | |
| 38 | - /** | |
| 39 | 31 | * @since 3.06 |
| 40 | - * | |
| 41 | - * @param string|null $license The license key. | |
| 42 | - * | |
| 43 | - * @return void | |
| 44 | 32 | */ |
| 45 | 33 | public function __construct( $license = null ) { |
| 46 | 34 | $this->set_license( $license ); |
| 47 | 35 | $this->set_cache_key(); |
| @@ -49,17 +37,14 @@ | ||
| 49 | 37 | |
| 50 | 38 | /** |
| 51 | 39 | * @since 3.06 |
| 52 | 40 | * |
| 53 | - * @param string|null $license The license key. | |
| 54 | - * | |
| 55 | 41 | * @return void |
| 56 | 42 | */ |
| 57 | 43 | private function set_license( $license ) { |
| 58 | 44 | if ( $license === null ) { |
| 59 | 45 | $edd_update = $this->get_pro_updater(); |
| 60 | - | |
| 61 | - if ( $edd_update ) { | |
| 46 | + if ( ! empty( $edd_update ) ) { | |
| 62 | 47 | $license = $edd_update->license; |
| 63 | 48 | } |
| 64 | 49 | } |
| 65 | 50 | $this->license = $license; |
| @@ -66,9 +51,8 @@ | ||
| 66 | 51 | } |
| 67 | 52 | |
| 68 | 53 | /** |
| 69 | 54 | * @since 3.06 |
| 70 | - * | |
| 71 | 55 | * @return string |
| 72 | 56 | */ |
| 73 | 57 | public function get_license() { |
| 74 | 58 | return $this->license; |
| @@ -79,14 +63,13 @@ | ||
| 79 | 63 | * |
| 80 | 64 | * @return void |
| 81 | 65 | */ |
| 82 | 66 | protected function set_cache_key() { |
| 83 | - $this->cache_key = 'frm_addons_l' . ( $this->license ? md5( $this->license ) : '' ); | |
| 67 | + $this->cache_key = 'frm_addons_l' . ( empty( $this->license ) ? '' : md5( $this->license ) ); | |
| 84 | 68 | } |
| 85 | 69 | |
| 86 | 70 | /** |
| 87 | 71 | * @since 3.06 |
| 88 | - * | |
| 89 | 72 | * @return string |
| 90 | 73 | */ |
| 91 | 74 | public function get_cache_key() { |
| 92 | 75 | return $this->cache_key; |
| @@ -92,38 +75,18 @@ | ||
| 92 | 75 | return $this->cache_key; |
| 93 | 76 | } |
| 94 | 77 | |
| 95 | 78 | /** |
| 96 | - * Flag the force property as true, so the next API request bypasses cache. | |
| 97 | - * This is used to pull API data for change logs, which are excluded from the cached data. | |
| 98 | - * | |
| 99 | - * @since 6.28 | |
| 100 | - * | |
| 101 | - * @return void | |
| 102 | - */ | |
| 103 | - public function force_api_request() { | |
| 104 | - $this->force = true; | |
| 105 | - } | |
| 106 | - | |
| 107 | - /** | |
| 108 | 79 | * @since 3.06 |
| 109 | - * | |
| 110 | 80 | * @return array |
| 111 | 81 | */ |
| 112 | 82 | public function get_api_info() { |
| 113 | 83 | $url = $this->api_url(); |
| 114 | - | |
| 115 | - if ( $this->license ) { | |
| 84 | + if ( ! empty( $this->license ) ) { | |
| 116 | 85 | $url .= '?l=' . urlencode( base64_encode( $this->license ) ); |
| 117 | 86 | } |
| 118 | 87 | |
| 119 | - if ( $this->force ) { | |
| 120 | - $addons = false; | |
| 121 | - $this->force = false; | |
| 122 | - } else { | |
| 123 | - $addons = $this->get_cached(); | |
| 124 | - } | |
| 125 | - | |
| 88 | + $addons = $this->get_cached(); | |
| 126 | 89 | if ( is_array( $addons ) ) { |
| 127 | 90 | return $addons; |
| 128 | 91 | } |
| 129 | 92 | |
| @@ -135,9 +98,8 @@ | ||
| 135 | 98 | $this->set_running(); |
| 136 | 99 | |
| 137 | 100 | // We need to know the version number to allow different downloads. |
| 138 | 101 | $agent = 'formidable/' . FrmAppHelper::plugin_version(); |
| 139 | - | |
| 140 | 102 | if ( class_exists( 'FrmProDb' ) ) { |
| 141 | 103 | $agent = 'formidable-pro/' . FrmProDb::$plug_version; |
| 142 | 104 | } |
| 143 | 105 | |
| @@ -165,10 +127,9 @@ | ||
| 165 | 127 | } |
| 166 | 128 | |
| 167 | 129 | if ( isset( $addon['categories'] ) ) { |
| 168 | 130 | $cats = array_intersect( $this->skip_categories(), $addon['categories'] ); |
| 169 | - | |
| 170 | - if ( $cats ) { | |
| 131 | + if ( ! empty( $cats ) ) { | |
| 171 | 132 | unset( $addons[ $k ] ); |
| 172 | 133 | continue; |
| 173 | 134 | } |
| 174 | 135 | } |
| @@ -204,9 +165,8 @@ | ||
| 204 | 165 | * @return void |
| 205 | 166 | */ |
| 206 | 167 | protected function set_running() { |
| 207 | 168 | $expires = 2 * MINUTE_IN_SECONDS; |
| 208 | - | |
| 209 | 169 | if ( $this->run_as_multisite() ) { |
| 210 | 170 | set_site_transient( $this->transient_key(), true, $expires ); |
| 211 | 171 | return; |
| 212 | 172 | } |
| @@ -234,9 +194,9 @@ | ||
| 234 | 194 | * |
| 235 | 195 | * @return bool |
| 236 | 196 | */ |
| 237 | 197 | protected function run_as_multisite() { |
| 238 | - return is_multisite() && ! $this->license; | |
| 198 | + return is_multisite() && empty( $this->license ); | |
| 239 | 199 | } |
| 240 | 200 | |
| 241 | 201 | /** |
| 242 | 202 | * @since 6.8.3 |
| @@ -243,9 +203,9 @@ | ||
| 243 | 203 | * |
| 244 | 204 | * @return string |
| 245 | 205 | */ |
| 246 | 206 | protected function transient_key() { |
| 247 | - return strtolower( self::class ) . '_request_lock'; | |
| 207 | + return strtolower( __CLASS__ ) . '_request_lock'; | |
| 248 | 208 | } |
| 249 | 209 | |
| 250 | 210 | /** |
| 251 | 211 | * @since 3.06 |
| @@ -252,9 +212,9 @@ | ||
| 252 | 212 | * |
| 253 | 213 | * @return string |
| 254 | 214 | */ |
| 255 | 215 | protected function api_url() { |
| 256 | - if ( ! $this->license ) { | |
| 216 | + if ( empty( $this->license ) ) { | |
| 257 | 217 | // Direct traffic to Cloudflare worker when there is no license. |
| 258 | 218 | return 'https://plapi.formidableforms.com/list/'; |
| 259 | 219 | } |
| 260 | 220 | return 'https://formidableforms.com/wp-json/s11edd/v1/updates/'; |
| @@ -277,18 +237,16 @@ | ||
| 277 | 237 | * |
| 278 | 238 | * @return array |
| 279 | 239 | */ |
| 280 | 240 | public function get_addon_for_license( $license_plugin, $addons = array() ) { |
| 281 | - if ( ! $addons ) { | |
| 241 | + if ( empty( $addons ) ) { | |
| 282 | 242 | $addons = $this->get_api_info(); |
| 283 | 243 | } |
| 284 | - | |
| 285 | 244 | $download_id = $license_plugin->download_id; |
| 286 | 245 | $plugin = array(); |
| 287 | - | |
| 288 | - if ( ! $download_id && $addons ) { | |
| 246 | + if ( empty( $download_id ) && ! empty( $addons ) ) { | |
| 289 | 247 | foreach ( $addons as $addon ) { |
| 290 | - if ( is_array( $addon ) && ! empty( $addon['title'] ) && 0 === strcasecmp( $license_plugin->plugin_name, $addon['title'] ) ) { | |
| 248 | + if ( is_array( $addon ) && ! empty( $addon['title'] ) && strtolower( $license_plugin->plugin_name ) === strtolower( $addon['title'] ) ) { | |
| 291 | 249 | return $addon; |
| 292 | 250 | } |
| 293 | 251 | } |
| 294 | 252 | } elseif ( isset( $addons[ $download_id ] ) ) { |
| @@ -299,10 +257,8 @@ | ||
| 299 | 257 | } |
| 300 | 258 | |
| 301 | 259 | /** |
| 302 | 260 | * @since 3.06 |
| 303 | - * | |
| 304 | - * @return false|object | |
| 305 | 261 | */ |
| 306 | 262 | public function get_pro_updater() { |
| 307 | 263 | if ( FrmAppHelper::pro_is_installed() && is_callable( 'FrmProAppHelper::get_updater' ) ) { |
| 308 | 264 | $updater = FrmProAppHelper::get_updater(); |
| @@ -320,27 +276,30 @@ | ||
| 320 | 276 | * @return array|bool |
| 321 | 277 | */ |
| 322 | 278 | protected function get_cached() { |
| 323 | 279 | $cache = $this->get_cached_option(); |
| 324 | - | |
| 325 | - if ( ! $cache ) { | |
| 280 | + if ( empty( $cache ) ) { | |
| 326 | 281 | return false; |
| 327 | 282 | } |
| 328 | 283 | |
| 329 | - $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 | + } | |
| 330 | 290 | |
| 331 | - if ( ! $is_expired && isset( $cache['version'] ) && $cache['version'] !== FrmAppHelper::plugin_version() ) { | |
| 332 | - $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 | + } | |
| 333 | 297 | } |
| 334 | 298 | |
| 335 | - // Avoid old cached data, unless we're currently trying to query for new data. | |
| 336 | - // The call to $this->is_running likely triggers a database query, so only call if if we're expired. | |
| 337 | - // (Rather than the other way around, which is less efficient). | |
| 338 | - if ( $is_expired && ! $this->is_running() ) { | |
| 339 | - return false; | |
| 340 | - } | |
| 299 | + $values = json_decode( $cache['value'], true ); | |
| 341 | 300 | |
| 342 | - return json_decode( $cache['value'], true ); | |
| 301 | + return $values; | |
| 343 | 302 | } |
| 344 | 303 | |
| 345 | 304 | /** |
| 346 | 305 | * Get the cache for the network if multisite. |
| @@ -351,9 +310,8 @@ | ||
| 351 | 310 | */ |
| 352 | 311 | protected function get_cached_option() { |
| 353 | 312 | if ( is_multisite() ) { |
| 354 | 313 | $cached = get_site_option( $this->cache_key ); |
| 355 | - | |
| 356 | 314 | if ( $cached ) { |
| 357 | 315 | return $cached; |
| 358 | 316 | } |
| 359 | 317 | } |
| @@ -368,10 +326,8 @@ | ||
| 368 | 326 | * |
| 369 | 327 | * @return void |
| 370 | 328 | */ |
| 371 | 329 | protected function set_cached( $addons ) { |
| 372 | - $addons = $this->reduce_addon_data_before_caching( $addons ); | |
| 373 | - | |
| 374 | 330 | $data = array( |
| 375 | 331 | 'timeout' => strtotime( $this->get_cache_timeout( $addons ), time() ), |
| 376 | 332 | 'value' => wp_json_encode( $addons ), |
| 377 | 333 | 'version' => FrmAppHelper::plugin_version(), |
| @@ -379,83 +335,13 @@ | ||
| 379 | 335 | |
| 380 | 336 | if ( is_multisite() ) { |
| 381 | 337 | update_site_option( $this->cache_key, $data ); |
| 382 | 338 | } else { |
| 383 | - // Autoload the license cache because it gets called everywhere. | |
| 384 | - $autoload = str_starts_with( $this->cache_key, 'frm_addons_l' ); | |
| 385 | - update_option( $this->cache_key, $data, $autoload ); | |
| 339 | + update_option( $this->cache_key, $data, 'no' ); | |
| 386 | 340 | } |
| 387 | 341 | } |
| 388 | 342 | |
| 389 | 343 | /** |
| 390 | - * Remove certain add-on API data that we don't need to cache. | |
| 391 | - * This is to help keep the option data (which is auto-loaded) small. | |
| 392 | - * | |
| 393 | - * @since 6.28 | |
| 394 | - * | |
| 395 | - * @param array $addons | |
| 396 | - * | |
| 397 | - * @return array | |
| 398 | - */ | |
| 399 | - private function reduce_addon_data_before_caching( $addons ) { | |
| 400 | - if ( is_subclass_of( $this, 'FrmFormApi' ) ) { | |
| 401 | - // We only want to modify FrmFormApi. Leave the other APIs alone for now. | |
| 402 | - return $addons; | |
| 403 | - } | |
| 404 | - | |
| 405 | - $reduced_addons = array(); | |
| 406 | - | |
| 407 | - foreach ( $addons as $key => $addon ) { | |
| 408 | - if ( ! is_array( $addon ) ) { | |
| 409 | - $reduced_addons[ $key ] = $addon; | |
| 410 | - continue; | |
| 411 | - } | |
| 412 | - | |
| 413 | - if ( ! $this->should_include_addon_in_cached_data( $addon ) ) { | |
| 414 | - continue; | |
| 415 | - } | |
| 416 | - | |
| 417 | - if ( isset( $addon['changelog'] ) ) { | |
| 418 | - unset( $addon['changelog'], $addon['banners'] ); | |
| 419 | - } | |
| 420 | - | |
| 421 | - $reduced_addons[ $key ] = $addon; | |
| 422 | - } | |
| 423 | - | |
| 424 | - return $reduced_addons; | |
| 425 | - } | |
| 426 | - | |
| 427 | - /** | |
| 428 | - * @since 6.28 | |
| 429 | - * | |
| 430 | - * @param array $addon | |
| 431 | - * | |
| 432 | - * @return bool True if the add-on should be included in cached data. | |
| 433 | - */ | |
| 434 | - private function should_include_addon_in_cached_data( $addon ) { | |
| 435 | - if ( isset( $addon['version'] ) && '' === $addon['version'] ) { | |
| 436 | - // If version is set but blank, the plugin is not actually live. | |
| 437 | - return false; | |
| 438 | - } | |
| 439 | - | |
| 440 | - if ( isset( $addon['categories'] ) ) { | |
| 441 | - if ( isset( $addon['slug'] ) && 'views' === $addon['slug'] ) { | |
| 442 | - // Legacy views has no categories set, but we should still | |
| 443 | - // Include it in cache since it is a valid add-on. | |
| 444 | - return true; | |
| 445 | - } | |
| 446 | - | |
| 447 | - $categories_are_empty = ! $addon['categories'] || $addon['categories'] === array( 'Strategy11' ); | |
| 448 | - | |
| 449 | - if ( $categories_are_empty ) { | |
| 450 | - return false; | |
| 451 | - } | |
| 452 | - } | |
| 453 | - | |
| 454 | - return true; | |
| 455 | - } | |
| 456 | - | |
| 457 | - /** | |
| 458 | 344 | * If the last check was a a rate limit, we'll need to check again sooner. |
| 459 | 345 | * |
| 460 | 346 | * @since 6.8.3 |
| 461 | 347 | * |
| @@ -463,12 +349,13 @@ | ||
| 463 | 349 | * |
| 464 | 350 | * @return string |
| 465 | 351 | */ |
| 466 | 352 | protected function get_cache_timeout( $addons ) { |
| 353 | + $timeout = $this->cache_timeout; | |
| 467 | 354 | if ( isset( $addons['response_code'] ) && 429 === $addons['response_code'] ) { |
| 468 | - return '+5 minutes'; | |
| 355 | + $timeout = '+5 minutes'; | |
| 469 | 356 | } |
| 470 | - return $this->cache_timeout; | |
| 357 | + return $timeout; | |
| 471 | 358 | } |
| 472 | 359 | |
| 473 | 360 | /** |
| 474 | 361 | * @since 3.06 |
| @@ -485,41 +372,38 @@ | ||
| 485 | 372 | } |
| 486 | 373 | |
| 487 | 374 | /** |
| 488 | 375 | * @since 3.06 |
| 489 | - * | |
| 490 | 376 | * @return array |
| 491 | 377 | */ |
| 492 | 378 | public function error_for_license() { |
| 493 | - return $this->license ? $this->get_error_from_response() : array(); | |
| 379 | + $errors = array(); | |
| 380 | + if ( ! empty( $this->license ) ) { | |
| 381 | + $errors = $this->get_error_from_response(); | |
| 382 | + } | |
| 383 | + | |
| 384 | + return $errors; | |
| 494 | 385 | } |
| 495 | 386 | |
| 496 | 387 | /** |
| 497 | 388 | * @since 3.06 |
| 498 | - * | |
| 499 | - * @param array $addons | |
| 500 | - * | |
| 501 | 389 | * @return array |
| 502 | 390 | */ |
| 503 | 391 | public function get_error_from_response( $addons = array() ) { |
| 504 | - if ( ! $addons ) { | |
| 392 | + if ( empty( $addons ) ) { | |
| 505 | 393 | $addons = $this->get_api_info(); |
| 506 | 394 | } |
| 507 | - | |
| 508 | 395 | $errors = array(); |
| 396 | + if ( isset( $addons['error'] ) ) { | |
| 397 | + if ( is_string( $addons['error'] ) ) { | |
| 398 | + $errors[] = $addons['error']; | |
| 399 | + } elseif ( ! empty( $addons['error']['message'] ) ) { | |
| 400 | + $errors[] = $addons['error']['message']; | |
| 401 | + } | |
| 509 | 402 | |
| 510 | - if ( ! isset( $addons['error'] ) ) { | |
| 511 | - return $errors; | |
| 403 | + do_action( 'frm_license_error', $addons['error'] ); | |
| 512 | 404 | } |
| 513 | 405 | |
| 514 | - if ( is_string( $addons['error'] ) ) { | |
| 515 | - $errors[] = $addons['error']; | |
| 516 | - } elseif ( ! empty( $addons['error']['message'] ) ) { | |
| 517 | - $errors[] = $addons['error']['message']; | |
| 518 | - } | |
| 519 | - | |
| 520 | - do_action( 'frm_license_error', $addons['error'] ); | |
| 521 | - | |
| 522 | 406 | return $errors; |
| 523 | 407 | } |
| 524 | 408 | |
| 525 | 409 | /** |
| @@ -527,9 +411,8 @@ | ||
| 527 | 411 | * |
| 528 | 412 | * @since 6.0 |
| 529 | 413 | * |
| 530 | 414 | * @param array $addon |
| 531 | - * | |
| 532 | 415 | * @return bool |
| 533 | 416 | */ |
| 534 | 417 | protected function is_new( $addon ) { |
| 535 | 418 | return strtotime( $addon['released'] ) > strtotime( '-' . $this->new_days . ' days' ); |