PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.8
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.8
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
← All changes | classes/models/FrmFormApi.php +33 -291 6.326.8 View file →
@@ -4,44 +4,21 @@
4 4 }
5 5
6 6 class FrmFormApi {
7 7
8 - /**
9 - * @var string
10 - */
11 8 protected $license = '';
12 -
13 - /**
14 - * @var string
15 - */
16 9 protected $cache_key = '';
17 -
18 - /**
19 - * @var string
20 - */
21 10 protected $cache_timeout = '+6 hours';
22 11
23 12 /**
24 13 * The number of days an add-on is new.
25 14 *
26 - * @var int
15 + * @var int $new_days
27 16 */
28 17 protected $new_days = 90;
29 18
30 19 /**
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 20 * @since 3.06
40 - *
41 - * @param string|null $license The license key.
42 - *
43 - * @return void
44 21 */
45 22 public function __construct( $license = null ) {
46 23 $this->set_license( $license );
47 24 $this->set_cache_key();
@@ -49,17 +26,14 @@
49 26
50 27 /**
51 28 * @since 3.06
52 29 *
53 - * @param string|null $license The license key.
54 - *
55 30 * @return void
56 31 */
57 32 private function set_license( $license ) {
58 33 if ( $license === null ) {
59 34 $edd_update = $this->get_pro_updater();
60 -
61 - if ( $edd_update ) {
35 + if ( ! empty( $edd_update ) ) {
62 36 $license = $edd_update->license;
63 37 }
64 38 }
65 39 $this->license = $license;
@@ -66,9 +40,8 @@
66 40 }
67 41
68 42 /**
69 43 * @since 3.06
70 - *
71 44 * @return string
72 45 */
73 46 public function get_license() {
74 47 return $this->license;
@@ -79,14 +52,13 @@
79 52 *
80 53 * @return void
81 54 */
82 55 protected function set_cache_key() {
83 - $this->cache_key = 'frm_addons_l' . ( $this->license ? md5( $this->license ) : '' );
56 + $this->cache_key = 'frm_addons_l' . ( empty( $this->license ) ? '' : md5( $this->license ) );
84 57 }
85 58
86 59 /**
87 60 * @since 3.06
88 - *
89 61 * @return string
90 62 */
91 63 public function get_cache_key() {
92 64 return $this->cache_key;
@@ -92,52 +64,24 @@
92 64 return $this->cache_key;
93 65 }
94 66
95 67 /**
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 68 * @since 3.06
109 - *
110 69 * @return array
111 70 */
112 71 public function get_api_info() {
113 72 $url = $this->api_url();
114 -
115 - if ( $this->license ) {
73 + if ( ! empty( $this->license ) ) {
116 74 $url .= '?l=' . urlencode( base64_encode( $this->license ) );
117 75 }
118 76
119 - if ( $this->force ) {
120 - $addons = false;
121 - $this->force = false;
122 - } else {
123 - $addons = $this->get_cached();
124 - }
125 -
77 + $addons = $this->get_cached();
126 78 if ( is_array( $addons ) ) {
127 79 return $addons;
128 80 }
129 81
130 - if ( $this->is_running() ) {
131 - // If there's no saved cache, we'll need to wait for the current request to finish.
132 - return array();
133 - }
134 -
135 - $this->set_running();
136 -
137 82 // We need to know the version number to allow different downloads.
138 83 $agent = 'formidable/' . FrmAppHelper::plugin_version();
139 -
140 84 if ( class_exists( 'FrmProDb' ) ) {
141 85 $agent = 'formidable-pro/' . FrmProDb::$plug_version;
142 86 }
143 87
@@ -156,10 +100,8 @@
156 100 if ( ! is_array( $addons ) ) {
157 101 $addons = array();
158 102 }
159 103
160 - $addons['response_code'] = wp_remote_retrieve_response_code( $response );
161 -
162 104 foreach ( $addons as $k => $addon ) {
163 105 if ( ! is_array( $addon ) ) {
164 106 continue;
165 107 }
@@ -165,10 +107,9 @@
165 107 }
166 108
167 109 if ( isset( $addon['categories'] ) ) {
168 110 $cats = array_intersect( $this->skip_categories(), $addon['categories'] );
169 -
170 - if ( $cats ) {
111 + if ( ! empty( $cats ) ) {
171 112 unset( $addons[ $k ] );
172 113 continue;
173 114 }
174 115 }
@@ -178,86 +119,18 @@
178 119 }
179 120 }
180 121
181 122 $this->set_cached( $addons );
182 - $this->done_running();
183 123
184 124 return $addons;
185 125 }
186 126
187 127 /**
188 - * Prevent multiple requests from running at the same time.
189 - *
190 - * @since 6.8.3
191 - *
192 - * @return bool
193 - */
194 - protected function is_running() {
195 - if ( $this->run_as_multisite() ) {
196 - return get_site_transient( $this->transient_key() );
197 - }
198 - return get_transient( $this->transient_key() );
199 - }
200 -
201 - /**
202 - * @since 6.8.3
203 - *
204 - * @return void
205 - */
206 - protected function set_running() {
207 - $expires = 2 * MINUTE_IN_SECONDS;
208 -
209 - if ( $this->run_as_multisite() ) {
210 - set_site_transient( $this->transient_key(), true, $expires );
211 - return;
212 - }
213 -
214 - set_transient( $this->transient_key(), true, $expires );
215 - }
216 -
217 - /**
218 - * @since 6.8.3
219 - *
220 - * @return void
221 - */
222 - protected function done_running() {
223 - if ( $this->run_as_multisite() ) {
224 - delete_site_transient( $this->transient_key() );
225 - }
226 - delete_transient( $this->transient_key() );
227 - }
228 -
229 - /**
230 - * Only allow one site in the network to make the api request at a time.
231 - * If there is a license for the request, run individually.
232 - *
233 - * @since 6.8.3
234 - *
235 - * @return bool
236 - */
237 - protected function run_as_multisite() {
238 - return is_multisite() && ! $this->license;
239 - }
240 -
241 - /**
242 - * @since 6.8.3
243 - *
244 - * @return string
245 - */
246 - protected function transient_key() {
247 - return strtolower( self::class ) . '_request_lock';
248 - }
249 -
250 - /**
251 128 * @since 3.06
252 129 *
253 130 * @return string
254 131 */
255 132 protected function api_url() {
256 - if ( ! $this->license ) {
257 - // Direct traffic to Cloudflare worker when there is no license.
258 - return 'https://plapi.formidableforms.com/list/';
259 - }
260 133 return 'https://formidableforms.com/wp-json/s11edd/v1/updates/';
261 134 }
262 135
263 136 /**
@@ -277,18 +150,16 @@
277 150 *
278 151 * @return array
279 152 */
280 153 public function get_addon_for_license( $license_plugin, $addons = array() ) {
281 - if ( ! $addons ) {
154 + if ( empty( $addons ) ) {
282 155 $addons = $this->get_api_info();
283 156 }
284 -
285 157 $download_id = $license_plugin->download_id;
286 158 $plugin = array();
287 -
288 - if ( ! $download_id && $addons ) {
159 + if ( empty( $download_id ) && ! empty( $addons ) ) {
289 160 foreach ( $addons as $addon ) {
290 - if ( is_array( $addon ) && ! empty( $addon['title'] ) && 0 === strcasecmp( $license_plugin->plugin_name, $addon['title'] ) ) {
161 + if ( strtolower( $license_plugin->plugin_name ) == strtolower( $addon['title'] ) ) {
291 162 return $addon;
292 163 }
293 164 }
294 165 } elseif ( isset( $addons[ $download_id ] ) ) {
@@ -299,10 +170,8 @@
299 170 }
300 171
301 172 /**
302 173 * @since 3.06
303 - *
304 - * @return false|object
305 174 */
306 175 public function get_pro_updater() {
307 176 if ( FrmAppHelper::pro_is_installed() && is_callable( 'FrmProAppHelper::get_updater' ) ) {
308 177 $updater = FrmProAppHelper::get_updater();
@@ -315,28 +184,24 @@
315 184 }
316 185
317 186 /**
318 187 * @since 3.06
319 - *
320 - * @return array|bool
188 + * @return array
321 189 */
322 190 protected function get_cached() {
323 - $cache = $this->get_cached_option();
191 + $cache = get_option( $this->cache_key );
324 192
325 - if ( ! $cache ) {
193 + FrmAppHelper::filter_gmt_offset();
194 +
195 + if ( empty( $cache ) || empty( $cache['timeout'] ) || current_time( 'timestamp' ) > $cache['timeout'] ) {
196 + // Cache is expired.
326 197 return false;
327 198 }
328 199
329 - $is_expired = empty( $cache['timeout'] ) || time() > $cache['timeout'];
330 -
331 - if ( ! $is_expired && isset( $cache['version'] ) && $cache['version'] !== FrmAppHelper::plugin_version() ) {
332 - $is_expired = true;
333 - }
334 -
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() ) {
200 + $version = FrmAppHelper::plugin_version();
201 + $for_current = isset( $cache['version'] ) && $cache['version'] == $version;
202 + if ( ! $for_current ) {
203 + // Force a new check.
339 204 return false;
340 205 }
341 206
342 207 return json_decode( $cache['value'], true );
@@ -342,27 +207,8 @@
342 207 return json_decode( $cache['value'], true );
343 208 }
344 209
345 210 /**
346 - * Get the cache for the network if multisite.
347 - *
348 - * @since 6.8.3
349 - *
350 - * @return mixed
351 - */
352 - protected function get_cached_option() {
353 - if ( is_multisite() ) {
354 - $cached = get_site_option( $this->cache_key );
355 -
356 - if ( $cached ) {
357 - return $cached;
358 - }
359 - }
360 -
361 - return get_option( $this->cache_key );
362 - }
363 -
364 - /**
365 211 * @since 3.06
366 212 *
367 213 * @param array $addons
368 214 *
@@ -368,158 +214,55 @@
368 214 *
369 215 * @return void
370 216 */
371 217 protected function set_cached( $addons ) {
372 - $addons = $this->reduce_addon_data_before_caching( $addons );
218 + FrmAppHelper::filter_gmt_offset();
373 219
374 220 $data = array(
375 - 'timeout' => strtotime( $this->get_cache_timeout( $addons ), time() ),
376 - 'value' => wp_json_encode( $addons ),
221 + 'timeout' => strtotime( $this->cache_timeout, current_time( 'timestamp' ) ),
222 + 'value' => json_encode( $addons ),
377 223 'version' => FrmAppHelper::plugin_version(),
378 224 );
379 225
380 - if ( is_multisite() ) {
381 - update_site_option( $this->cache_key, $data );
382 - } 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 );
386 - }
226 + update_option( $this->cache_key, $data, 'no' );
387 227 }
388 228
389 229 /**
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 - * If the last check was a a rate limit, we'll need to check again sooner.
459 - *
460 - * @since 6.8.3
461 - *
462 - * @param array $addons
463 - *
464 - * @return string
465 - */
466 - protected function get_cache_timeout( $addons ) {
467 - if ( isset( $addons['response_code'] ) && 429 === $addons['response_code'] ) {
468 - return '+5 minutes';
469 - }
470 - return $this->cache_timeout;
471 - }
472 -
473 - /**
474 230 * @since 3.06
475 231 *
476 232 * @return void
477 233 */
478 234 public function reset_cached() {
479 - if ( is_multisite() ) {
480 - delete_site_option( $this->cache_key );
481 - } else {
482 - delete_option( $this->cache_key );
483 - }
484 - $this->done_running();
235 + delete_option( $this->cache_key );
485 236 }
486 237
487 238 /**
488 239 * @since 3.06
489 - *
490 240 * @return array
491 241 */
492 242 public function error_for_license() {
493 - return $this->license ? $this->get_error_from_response() : array();
243 + $errors = array();
244 + if ( ! empty( $this->license ) ) {
245 + $errors = $this->get_error_from_response();
246 + }
247 +
248 + return $errors;
494 249 }
495 250
496 251 /**
497 252 * @since 3.06
498 - *
499 - * @param array $addons
500 - *
501 253 * @return array
502 254 */
503 255 public function get_error_from_response( $addons = array() ) {
504 - if ( ! $addons ) {
256 + if ( empty( $addons ) ) {
505 257 $addons = $this->get_api_info();
506 258 }
507 -
508 259 $errors = array();
509 -
510 - if ( ! isset( $addons['error'] ) ) {
511 - return $errors;
512 - }
513 -
514 - if ( is_string( $addons['error'] ) ) {
515 - $errors[] = $addons['error'];
516 - } elseif ( ! empty( $addons['error']['message'] ) ) {
260 + if ( isset( $addons['error'] ) ) {
517 261 $errors[] = $addons['error']['message'];
262 + do_action( 'frm_license_error', $addons['error'] );
518 263 }
519 264
520 - do_action( 'frm_license_error', $addons['error'] );
521 -
522 265 return $errors;
523 266 }
524 267
525 268 /**
@@ -527,9 +270,8 @@
527 270 *
528 271 * @since 6.0
529 272 *
530 273 * @param array $addon
531 - *
532 274 * @return bool
533 275 */
534 276 protected function is_new( $addon ) {
535 277 return strtotime( $addon['released'] ) > strtotime( '-' . $this->new_days . ' days' );