| @@ -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 | * |
| @@ -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 | } |
| @@ -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/'; |
| @@ -259,11 +294,11 @@ | ||
| 259 | 294 | |
| 260 | 295 | $download_id = $license_plugin->download_id; |
| 261 | 296 | $plugin = array(); |
| 262 | 297 | |
| 263 | - if ( ! $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 ] ) ) { |
| @@ -313,12 +348,44 @@ | ||
| 313 | 348 | if ( $is_expired && ! $this->is_running() ) { |
| 314 | 349 | return false; |
| 315 | 350 | } |
| 316 | 351 | |
| 317 | - return json_decode( $cache['value'], true ); | |
| 352 | + return $this->decode_cached( $cache['value'] ); | |
| 318 | 353 | } |
| 319 | 354 | |
| 320 | 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; | |
| 385 | + } | |
| 386 | + | |
| 387 | + /** | |
| 321 | 388 | * Get the cache for the network if multisite. |
| 322 | 389 | * |
| 323 | 390 | * @since 6.8.3 |
| 324 | 391 | * |
| @@ -343,8 +410,10 @@ | ||
| 343 | 410 | * |
| 344 | 411 | * @return void |
| 345 | 412 | */ |
| 346 | 413 | protected function set_cached( $addons ) { |
| 414 | + $addons = $this->reduce_addon_data_before_caching( $addons ); | |
| 415 | + | |
| 347 | 416 | $data = array( |
| 348 | 417 | 'timeout' => strtotime( $this->get_cache_timeout( $addons ), time() ), |
| 349 | 418 | 'value' => wp_json_encode( $addons ), |
| 350 | 419 | 'version' => FrmAppHelper::plugin_version(), |
| @@ -359,8 +428,76 @@ | ||
| 359 | 428 | } |
| 360 | 429 | } |
| 361 | 430 | |
| 362 | 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 | + /** | |
| 363 | 500 | * If the last check was a a rate limit, we'll need to check again sooner. |
| 364 | 501 | * |
| 365 | 502 | * @since 6.8.3 |
| 366 | 503 | * |
| @@ -394,12 +531,9 @@ | ||
| 394 | 531 | * |
| 395 | 532 | * @return array |
| 396 | 533 | */ |
| 397 | 534 | public function error_for_license() { |
| 398 | - if ( ! empty( $this->license ) ) { | |
| 399 | - return $this->get_error_from_response(); | |
| 400 | - } | |
| 401 | - return array(); | |
| 535 | + return $this->license ? $this->get_error_from_response() : array(); | |
| 402 | 536 | } |
| 403 | 537 | |
| 404 | 538 | /** |
| 405 | 539 | * @since 3.06 |
| @@ -414,17 +548,19 @@ | ||
| 414 | 548 | } |
| 415 | 549 | |
| 416 | 550 | $errors = array(); |
| 417 | 551 | |
| 418 | - if ( isset( $addons['error'] ) ) { | |
| 419 | - if ( is_string( $addons['error'] ) ) { | |
| 420 | - $errors[] = $addons['error']; | |
| 421 | - } elseif ( ! empty( $addons['error']['message'] ) ) { | |
| 422 | - $errors[] = $addons['error']['message']; | |
| 423 | - } | |
| 552 | + if ( ! isset( $addons['error'] ) ) { | |
| 553 | + return $errors; | |
| 554 | + } | |
| 424 | 555 | |
| 425 | - 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']; | |
| 426 | 560 | } |
| 561 | + | |
| 562 | + do_action( 'frm_license_error', $addons['error'] ); | |
| 427 | 563 | |
| 428 | 564 | return $errors; |
| 429 | 565 | } |
| 430 | 566 | |