| @@ -27,8 +27,26 @@ | ||
| 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 | + * The most recent decode of each cache key's payload. | |
| 40 | + * | |
| 41 | + * Each entry is array( 'raw' => string, 'decoded' => mixed ). The raw JSON is kept so a reuse | |
| 42 | + * can be checked against what is stored right now, rather than assumed to still be current. | |
| 43 | + * | |
| 44 | + * @var array<string,array> | |
| 45 | + */ | |
| 46 | + private static $decoded_cache = array(); | |
| 47 | + | |
| 48 | + /** | |
| 31 | 49 | * @since 3.06 |
| 32 | 50 | * |
| 33 | 51 | * @param string|null $license The license key. |
| 34 | 52 | * |
| @@ -49,9 +67,9 @@ | ||
| 49 | 67 | private function set_license( $license ) { |
| 50 | 68 | if ( $license === null ) { |
| 51 | 69 | $edd_update = $this->get_pro_updater(); |
| 52 | 70 | |
| 53 | - if ( ! empty( $edd_update ) ) { | |
| 71 | + if ( $edd_update ) { | |
| 54 | 72 | $license = $edd_update->license; |
| 55 | 73 | } |
| 56 | 74 | } |
| 57 | 75 | $this->license = $license; |
| @@ -71,9 +89,9 @@ | ||
| 71 | 89 | * |
| 72 | 90 | * @return void |
| 73 | 91 | */ |
| 74 | 92 | protected function set_cache_key() { |
| 75 | - $this->cache_key = 'frm_addons_l' . ( empty( $this->license ) ? '' : md5( $this->license ) ); | |
| 93 | + $this->cache_key = 'frm_addons_l' . ( $this->license ? md5( $this->license ) : '' ); | |
| 76 | 94 | } |
| 77 | 95 | |
| 78 | 96 | /** |
| 79 | 97 | * @since 3.06 |
| @@ -84,8 +102,20 @@ | ||
| 84 | 102 | return $this->cache_key; |
| 85 | 103 | } |
| 86 | 104 | |
| 87 | 105 | /** |
| 106 | + * Flag the force property as true, so the next API request bypasses cache. | |
| 107 | + * This is used to pull API data for change logs, which are excluded from the cached data. | |
| 108 | + * | |
| 109 | + * @since 6.28 | |
| 110 | + * | |
| 111 | + * @return void | |
| 112 | + */ | |
| 113 | + public function force_api_request() { | |
| 114 | + $this->force = true; | |
| 115 | + } | |
| 116 | + | |
| 117 | + /** | |
| 88 | 118 | * @since 3.06 |
| 89 | 119 | * |
| 90 | 120 | * @return array |
| 91 | 121 | */ |
| @@ -91,13 +121,18 @@ | ||
| 91 | 121 | */ |
| 92 | 122 | public function get_api_info() { |
| 93 | 123 | $url = $this->api_url(); |
| 94 | 124 | |
| 95 | - if ( ! empty( $this->license ) ) { | |
| 125 | + if ( $this->license ) { | |
| 96 | 126 | $url .= '?l=' . urlencode( base64_encode( $this->license ) ); |
| 97 | 127 | } |
| 98 | 128 | |
| 99 | - $addons = $this->get_cached(); | |
| 129 | + if ( $this->force ) { | |
| 130 | + $addons = false; | |
| 131 | + $this->force = false; | |
| 132 | + } else { | |
| 133 | + $addons = $this->get_cached(); | |
| 134 | + } | |
| 100 | 135 | |
| 101 | 136 | if ( is_array( $addons ) ) { |
| 102 | 137 | return $addons; |
| 103 | 138 | } |
| @@ -141,9 +176,9 @@ | ||
| 141 | 176 | |
| 142 | 177 | if ( isset( $addon['categories'] ) ) { |
| 143 | 178 | $cats = array_intersect( $this->skip_categories(), $addon['categories'] ); |
| 144 | 179 | |
| 145 | - if ( ! empty( $cats ) ) { | |
| 180 | + if ( $cats ) { | |
| 146 | 181 | unset( $addons[ $k ] ); |
| 147 | 182 | continue; |
| 148 | 183 | } |
| 149 | 184 | } |
| @@ -209,9 +244,9 @@ | ||
| 209 | 244 | * |
| 210 | 245 | * @return bool |
| 211 | 246 | */ |
| 212 | 247 | protected function run_as_multisite() { |
| 213 | - return is_multisite() && empty( $this->license ); | |
| 248 | + return is_multisite() && ! $this->license; | |
| 214 | 249 | } |
| 215 | 250 | |
| 216 | 251 | /** |
| 217 | 252 | * @since 6.8.3 |
| @@ -218,9 +253,9 @@ | ||
| 218 | 253 | * |
| 219 | 254 | * @return string |
| 220 | 255 | */ |
| 221 | 256 | protected function transient_key() { |
| 222 | - return strtolower( self::class ) . '_request_lock'; | |
| 257 | + return strtolower( static::class ) . '_request_lock'; | |
| 223 | 258 | } |
| 224 | 259 | |
| 225 | 260 | /** |
| 226 | 261 | * @since 3.06 |
| @@ -227,9 +262,9 @@ | ||
| 227 | 262 | * |
| 228 | 263 | * @return string |
| 229 | 264 | */ |
| 230 | 265 | protected function api_url() { |
| 231 | - if ( empty( $this->license ) ) { | |
| 266 | + if ( ! $this->license ) { | |
| 232 | 267 | // Direct traffic to Cloudflare worker when there is no license. |
| 233 | 268 | return 'https://plapi.formidableforms.com/list/'; |
| 234 | 269 | } |
| 235 | 270 | return 'https://formidableforms.com/wp-json/s11edd/v1/updates/'; |
| @@ -252,9 +287,9 @@ | ||
| 252 | 287 | * |
| 253 | 288 | * @return array |
| 254 | 289 | */ |
| 255 | 290 | public function get_addon_for_license( $license_plugin, $addons = array() ) { |
| 256 | - if ( empty( $addons ) ) { | |
| 291 | + if ( ! $addons ) { | |
| 257 | 292 | $addons = $this->get_api_info(); |
| 258 | 293 | } |
| 259 | 294 | |
| 260 | 295 | $download_id = $license_plugin->download_id; |
| @@ -259,11 +294,11 @@ | ||
| 259 | 294 | |
| 260 | 295 | $download_id = $license_plugin->download_id; |
| 261 | 296 | $plugin = array(); |
| 262 | 297 | |
| 263 | - if ( empty( $download_id ) && ! empty( $addons ) ) { | |
| 298 | + if ( ! $download_id && $addons ) { | |
| 264 | 299 | foreach ( $addons as $addon ) { |
| 265 | - if ( is_array( $addon ) && ! empty( $addon['title'] ) && strtolower( $license_plugin->plugin_name ) === strtolower( $addon['title'] ) ) { | |
| 300 | + if ( is_array( $addon ) && ! empty( $addon['title'] ) && 0 === strcasecmp( $license_plugin->plugin_name, $addon['title'] ) ) { | |
| 266 | 301 | return $addon; |
| 267 | 302 | } |
| 268 | 303 | } |
| 269 | 304 | } elseif ( isset( $addons[ $download_id ] ) ) { |
| @@ -296,31 +331,58 @@ | ||
| 296 | 331 | */ |
| 297 | 332 | protected function get_cached() { |
| 298 | 333 | $cache = $this->get_cached_option(); |
| 299 | 334 | |
| 300 | - if ( empty( $cache ) ) { | |
| 335 | + if ( ! $cache ) { | |
| 301 | 336 | return false; |
| 302 | 337 | } |
| 303 | 338 | |
| 304 | - // If the api call is running, we can use the expired cache. | |
| 305 | - if ( ! $this->is_running() ) { | |
| 306 | - if ( empty( $cache['timeout'] ) || time() > $cache['timeout'] ) { | |
| 307 | - // Cache is expired. | |
| 308 | - return false; | |
| 309 | - } | |
| 339 | + $is_expired = empty( $cache['timeout'] ) || time() > $cache['timeout']; | |
| 310 | 340 | |
| 311 | - $version = FrmAppHelper::plugin_version(); | |
| 312 | - $for_current = isset( $cache['version'] ) && $cache['version'] == $version; | |
| 341 | + if ( ! $is_expired && isset( $cache['version'] ) && $cache['version'] !== FrmAppHelper::plugin_version() ) { | |
| 342 | + $is_expired = true; | |
| 343 | + } | |
| 313 | 344 | |
| 314 | - if ( ! $for_current ) { | |
| 315 | - // Force a new check. | |
| 316 | - return false; | |
| 317 | - } | |
| 345 | + // Avoid old cached data, unless we're currently trying to query for new data. | |
| 346 | + // The call to $this->is_running likely triggers a database query, so only call if if we're expired. | |
| 347 | + // (Rather than the other way around, which is less efficient). | |
| 348 | + if ( $is_expired && ! $this->is_running() ) { | |
| 349 | + return false; | |
| 318 | 350 | } |
| 319 | 351 | |
| 320 | - $values = json_decode( $cache['value'], true ); | |
| 352 | + return $this->decode_cached( $cache['value'] ); | |
| 353 | + } | |
| 321 | 354 | |
| 322 | - return $values; | |
| 355 | + /** | |
| 356 | + * Decode a cached API payload, reusing the previous decode of an unchanged payload. | |
| 357 | + * | |
| 358 | + * The payload runs to tens of kilobytes of JSON, and decoding it costs far more than reading | |
| 359 | + * the option it came from. That matters because a single form builder page decodes it once per | |
| 360 | + * upsell it renders. The reuse is keyed on the raw JSON rather than on the cache key, so | |
| 361 | + * anything that changes what is stored -- a new license, a cleared cache, a fresh API response, | |
| 362 | + * a test replacing the option -- is picked up on the very next read instead of being masked. | |
| 363 | + * | |
| 364 | + * @since 6.35 | |
| 365 | + * | |
| 366 | + * @param string $value The cached JSON. | |
| 367 | + * | |
| 368 | + * @return mixed | |
| 369 | + */ | |
| 370 | + private function decode_cached( $value ) { | |
| 371 | + $previous = self::$decoded_cache[ $this->cache_key ] ?? false; | |
| 372 | + | |
| 373 | + if ( $previous && $previous['raw'] === $value ) { | |
| 374 | + return $previous['decoded']; | |
| 375 | + } | |
| 376 | + | |
| 377 | + $decoded = json_decode( $value, true ); | |
| 378 | + | |
| 379 | + self::$decoded_cache[ $this->cache_key ] = array( | |
| 380 | + 'raw' => $value, | |
| 381 | + 'decoded' => $decoded, | |
| 382 | + ); | |
| 383 | + | |
| 384 | + return $decoded; | |
| 323 | 385 | } |
| 324 | 386 | |
| 325 | 387 | /** |
| 326 | 388 | * Get the cache for the network if multisite. |
| @@ -348,8 +410,10 @@ | ||
| 348 | 410 | * |
| 349 | 411 | * @return void |
| 350 | 412 | */ |
| 351 | 413 | protected function set_cached( $addons ) { |
| 414 | + $addons = $this->reduce_addon_data_before_caching( $addons ); | |
| 415 | + | |
| 352 | 416 | $data = array( |
| 353 | 417 | 'timeout' => strtotime( $this->get_cache_timeout( $addons ), time() ), |
| 354 | 418 | 'value' => wp_json_encode( $addons ), |
| 355 | 419 | 'version' => FrmAppHelper::plugin_version(), |
| @@ -357,13 +421,83 @@ | ||
| 357 | 421 | |
| 358 | 422 | if ( is_multisite() ) { |
| 359 | 423 | update_site_option( $this->cache_key, $data ); |
| 360 | 424 | } else { |
| 361 | - update_option( $this->cache_key, $data, 'no' ); | |
| 425 | + // Autoload the license cache because it gets called everywhere. | |
| 426 | + $autoload = str_starts_with( $this->cache_key, 'frm_addons_l' ); | |
| 427 | + update_option( $this->cache_key, $data, $autoload ); | |
| 362 | 428 | } |
| 363 | 429 | } |
| 364 | 430 | |
| 365 | 431 | /** |
| 432 | + * Remove certain add-on API data that we don't need to cache. | |
| 433 | + * This is to help keep the option data (which is auto-loaded) small. | |
| 434 | + * | |
| 435 | + * @since 6.28 | |
| 436 | + * | |
| 437 | + * @param array $addons | |
| 438 | + * | |
| 439 | + * @return array | |
| 440 | + */ | |
| 441 | + private function reduce_addon_data_before_caching( $addons ) { | |
| 442 | + if ( is_subclass_of( $this, 'FrmFormApi' ) ) { | |
| 443 | + // We only want to modify FrmFormApi. Leave the other APIs alone for now. | |
| 444 | + return $addons; | |
| 445 | + } | |
| 446 | + | |
| 447 | + $reduced_addons = array(); | |
| 448 | + | |
| 449 | + foreach ( $addons as $key => $addon ) { | |
| 450 | + if ( ! is_array( $addon ) ) { | |
| 451 | + $reduced_addons[ $key ] = $addon; | |
| 452 | + continue; | |
| 453 | + } | |
| 454 | + | |
| 455 | + if ( ! $this->should_include_addon_in_cached_data( $addon ) ) { | |
| 456 | + continue; | |
| 457 | + } | |
| 458 | + | |
| 459 | + if ( isset( $addon['changelog'] ) ) { | |
| 460 | + unset( $addon['changelog'], $addon['banners'] ); | |
| 461 | + } | |
| 462 | + | |
| 463 | + $reduced_addons[ $key ] = $addon; | |
| 464 | + } | |
| 465 | + | |
| 466 | + return $reduced_addons; | |
| 467 | + } | |
| 468 | + | |
| 469 | + /** | |
| 470 | + * @since 6.28 | |
| 471 | + * | |
| 472 | + * @param array $addon | |
| 473 | + * | |
| 474 | + * @return bool True if the add-on should be included in cached data. | |
| 475 | + */ | |
| 476 | + private function should_include_addon_in_cached_data( $addon ) { | |
| 477 | + if ( isset( $addon['version'] ) && '' === $addon['version'] ) { | |
| 478 | + // If version is set but blank, the plugin is not actually live. | |
| 479 | + return false; | |
| 480 | + } | |
| 481 | + | |
| 482 | + if ( isset( $addon['categories'] ) ) { | |
| 483 | + if ( isset( $addon['slug'] ) && 'views' === $addon['slug'] ) { | |
| 484 | + // Legacy views has no categories set, but we should still | |
| 485 | + // Include it in cache since it is a valid add-on. | |
| 486 | + return true; | |
| 487 | + } | |
| 488 | + | |
| 489 | + $categories_are_empty = ! $addon['categories'] || $addon['categories'] === array( 'Strategy11' ); | |
| 490 | + | |
| 491 | + if ( $categories_are_empty ) { | |
| 492 | + return false; | |
| 493 | + } | |
| 494 | + } | |
| 495 | + | |
| 496 | + return true; | |
| 497 | + } | |
| 498 | + | |
| 499 | + /** | |
| 366 | 500 | * If the last check was a a rate limit, we'll need to check again sooner. |
| 367 | 501 | * |
| 368 | 502 | * @since 6.8.3 |
| 369 | 503 | * |
| @@ -371,14 +505,12 @@ | ||
| 371 | 505 | * |
| 372 | 506 | * @return string |
| 373 | 507 | */ |
| 374 | 508 | protected function get_cache_timeout( $addons ) { |
| 375 | - $timeout = $this->cache_timeout; | |
| 376 | - | |
| 377 | 509 | if ( isset( $addons['response_code'] ) && 429 === $addons['response_code'] ) { |
| 378 | - $timeout = '+5 minutes'; | |
| 510 | + return '+5 minutes'; | |
| 379 | 511 | } |
| 380 | - return $timeout; | |
| 512 | + return $this->cache_timeout; | |
| 381 | 513 | } |
| 382 | 514 | |
| 383 | 515 | /** |
| 384 | 516 | * @since 3.06 |
| @@ -399,15 +531,9 @@ | ||
| 399 | 531 | * |
| 400 | 532 | * @return array |
| 401 | 533 | */ |
| 402 | 534 | public function error_for_license() { |
| 403 | - $errors = array(); | |
| 404 | - | |
| 405 | - if ( ! empty( $this->license ) ) { | |
| 406 | - $errors = $this->get_error_from_response(); | |
| 407 | - } | |
| 408 | - | |
| 409 | - return $errors; | |
| 535 | + return $this->license ? $this->get_error_from_response() : array(); | |
| 410 | 536 | } |
| 411 | 537 | |
| 412 | 538 | /** |
| 413 | 539 | * @since 3.06 |
| @@ -416,23 +542,25 @@ | ||
| 416 | 542 | * |
| 417 | 543 | * @return array |
| 418 | 544 | */ |
| 419 | 545 | public function get_error_from_response( $addons = array() ) { |
| 420 | - if ( empty( $addons ) ) { | |
| 546 | + if ( ! $addons ) { | |
| 421 | 547 | $addons = $this->get_api_info(); |
| 422 | 548 | } |
| 423 | 549 | |
| 424 | 550 | $errors = array(); |
| 425 | 551 | |
| 426 | - if ( isset( $addons['error'] ) ) { | |
| 427 | - if ( is_string( $addons['error'] ) ) { | |
| 428 | - $errors[] = $addons['error']; | |
| 429 | - } elseif ( ! empty( $addons['error']['message'] ) ) { | |
| 430 | - $errors[] = $addons['error']['message']; | |
| 431 | - } | |
| 552 | + if ( ! isset( $addons['error'] ) ) { | |
| 553 | + return $errors; | |
| 554 | + } | |
| 432 | 555 | |
| 433 | - do_action( 'frm_license_error', $addons['error'] ); | |
| 556 | + if ( is_string( $addons['error'] ) ) { | |
| 557 | + $errors[] = $addons['error']; | |
| 558 | + } elseif ( ! empty( $addons['error']['message'] ) ) { | |
| 559 | + $errors[] = $addons['error']['message']; | |
| 434 | 560 | } |
| 561 | + | |
| 562 | + do_action( 'frm_license_error', $addons['error'] ); | |
| 435 | 563 | |
| 436 | 564 | return $errors; |
| 437 | 565 | } |
| 438 | 566 | |