PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 3.06.06
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v3.06.06
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 +28 -277 6.26 → 3.06.06 View file →
@@ -1,39 +1,14 @@
1 1 <?php
2 -if ( ! defined( 'ABSPATH' ) ) {
3 - die( 'You are not allowed to call this page directly.' );
4 -}
5 2
6 3 class FrmFormApi {
7 4
8 - /**
9 - * @var string
10 - */
11 - protected $license = '';
12 -
13 - /**
14 - * @var string
15 - */
5 + protected $license = '';
16 6 protected $cache_key = '';
17 -
18 - /**
19 - * @var string
20 - */
21 7 protected $cache_timeout = '+6 hours';
22 8
23 9 /**
24 - * The number of days an add-on is new.
25 - *
26 - * @var int
27 - */
28 - protected $new_days = 90;
29 -
30 - /**
31 10 * @since 3.06
32 - *
33 - * @param string|null $license The license key.
34 - *
35 - * @return void
36 11 */
37 12 public function __construct( $license = null ) {
38 13 $this->set_license( $license );
39 14 $this->set_cache_key();
@@ -40,17 +15,12 @@
40 15 }
41 16
42 17 /**
43 18 * @since 3.06
44 - *
45 - * @param string|null $license The license key.
46 - *
47 - * @return void
48 19 */
49 20 private function set_license( $license ) {
50 21 if ( $license === null ) {
51 22 $edd_update = $this->get_pro_updater();
52 -
53 23 if ( ! empty( $edd_update ) ) {
54 24 $license = $edd_update->license;
55 25 }
56 26 }
@@ -58,9 +28,8 @@
58 28 }
59 29
60 30 /**
61 31 * @since 3.06
62 - *
63 32 * @return string
64 33 */
65 34 public function get_license() {
66 35 return $this->license;
@@ -67,10 +36,8 @@
67 36 }
68 37
69 38 /**
70 39 * @since 3.06
71 - *
72 - * @return void
73 40 */
74 41 protected function set_cache_key() {
75 42 $this->cache_key = 'frm_addons_l' . ( empty( $this->license ) ? '' : md5( $this->license ) );
76 43 }
@@ -76,9 +43,8 @@
76 43 }
77 44
78 45 /**
79 46 * @since 3.06
80 - *
81 47 * @return string
82 48 */
83 49 public function get_cache_key() {
84 50 return $this->cache_key;
@@ -85,161 +51,53 @@
85 51 }
86 52
87 53 /**
88 54 * @since 3.06
89 - *
90 55 * @return array
91 56 */
92 57 public function get_api_info() {
93 58 $url = $this->api_url();
94 -
95 59 if ( ! empty( $this->license ) ) {
96 60 $url .= '?l=' . urlencode( base64_encode( $this->license ) );
97 61 }
98 62
99 63 $addons = $this->get_cached();
100 -
101 - if ( is_array( $addons ) ) {
64 + if ( ! empty( $addons ) ) {
102 65 return $addons;
103 66 }
104 67
105 - if ( $this->is_running() ) {
106 - // If there's no saved cache, we'll need to wait for the current request to finish.
107 - return array();
108 - }
109 -
110 - $this->set_running();
111 -
112 - // We need to know the version number to allow different downloads.
113 - $agent = 'formidable/' . FrmAppHelper::plugin_version();
114 -
115 - if ( class_exists( 'FrmProDb' ) ) {
116 - $agent = 'formidable-pro/' . FrmProDb::$plug_version;
117 - }
118 -
119 - $response = wp_remote_get(
120 - $url,
121 - array(
122 - 'timeout' => 25,
123 - 'user-agent' => $agent . '; ' . get_bloginfo( 'url' ),
124 - )
125 - );
126 -
68 + $response = wp_remote_get( $url );
127 69 if ( is_array( $response ) && ! is_wp_error( $response ) ) {
128 - $addons = $response['body'] ? json_decode( $response['body'], true ) : array();
129 - }
70 + $addons = $response['body'];
71 + if ( ! empty( $addons ) ) {
72 + $addons = json_decode( $addons, true );
130 73
131 - if ( ! is_array( $addons ) ) {
132 - $addons = array();
133 - }
134 -
135 - $addons['response_code'] = wp_remote_retrieve_response_code( $response );
136 -
137 - foreach ( $addons as $k => $addon ) {
138 - if ( ! is_array( $addon ) ) {
139 - continue;
140 - }
141 -
142 - if ( isset( $addon['categories'] ) ) {
143 - $cats = array_intersect( $this->skip_categories(), $addon['categories'] );
144 -
145 - if ( ! empty( $cats ) ) {
146 - unset( $addons[ $k ] );
147 - continue;
74 + foreach ( $addons as $k => $addon ) {
75 + if ( ! isset( $addon['categories'] ) ) {
76 + continue;
77 + }
78 + $cats = array_intersect( $this->skip_categories(), $addon['categories'] );
79 + if ( ! empty( $cats ) ) {
80 + unset( $addons[ $k ] );
81 + }
148 82 }
149 - }
150 83
151 - if ( ! array_key_exists( 'is_new', $addon ) && array_key_exists( 'released', $addon ) ) {
152 - $addons[ $k ]['is_new'] = $this->is_new( $addon );
84 + $this->set_cached( $addons );
153 85 }
154 86 }
155 87
156 - $this->set_cached( $addons );
157 - $this->done_running();
158 -
159 88 return $addons;
160 89 }
161 90
162 91 /**
163 - * Prevent multiple requests from running at the same time.
164 - *
165 - * @since 6.8.3
166 - *
167 - * @return bool
168 - */
169 - protected function is_running() {
170 - if ( $this->run_as_multisite() ) {
171 - return get_site_transient( $this->transient_key() );
172 - }
173 - return get_transient( $this->transient_key() );
174 - }
175 -
176 - /**
177 - * @since 6.8.3
178 - *
179 - * @return void
180 - */
181 - protected function set_running() {
182 - $expires = 2 * MINUTE_IN_SECONDS;
183 -
184 - if ( $this->run_as_multisite() ) {
185 - set_site_transient( $this->transient_key(), true, $expires );
186 - return;
187 - }
188 -
189 - set_transient( $this->transient_key(), true, $expires );
190 - }
191 -
192 - /**
193 - * @since 6.8.3
194 - *
195 - * @return void
196 - */
197 - protected function done_running() {
198 - if ( $this->run_as_multisite() ) {
199 - delete_site_transient( $this->transient_key() );
200 - }
201 - delete_transient( $this->transient_key() );
202 - }
203 -
204 - /**
205 - * Only allow one site in the network to make the api request at a time.
206 - * If there is a license for the request, run individually.
207 - *
208 - * @since 6.8.3
209 - *
210 - * @return bool
211 - */
212 - protected function run_as_multisite() {
213 - return is_multisite() && empty( $this->license );
214 - }
215 -
216 - /**
217 - * @since 6.8.3
218 - *
219 - * @return string
220 - */
221 - protected function transient_key() {
222 - return strtolower( self::class ) . '_request_lock';
223 - }
224 -
225 - /**
226 92 * @since 3.06
227 - *
228 - * @return string
229 93 */
230 94 protected function api_url() {
231 - if ( empty( $this->license ) ) {
232 - // Direct traffic to Cloudflare worker when there is no license.
233 - return 'https://plapi.formidableforms.com/list/';
234 - }
235 95 return 'https://formidableforms.com/wp-json/s11edd/v1/updates/';
236 96 }
237 97
238 98 /**
239 99 * @since 3.06
240 - *
241 - * @return string[]
242 100 */
243 101 protected function skip_categories() {
244 102 return array( 'WordPress Form Templates', 'WordPress Form Style Templates' );
245 103 }
@@ -245,12 +103,9 @@
245 103 }
246 104
247 105 /**
248 106 * @since 3.06
249 - *
250 - * @param object $license_plugin The FrmAddon object.
251 - * @param array $addons
252 - *
107 + * @param object $license_plugin The FrmAddon object
253 108 * @return array
254 109 */
255 110 public function get_addon_for_license( $license_plugin, $addons = array() ) {
256 111 if ( empty( $addons ) ) {
@@ -255,15 +110,13 @@
255 110 public function get_addon_for_license( $license_plugin, $addons = array() ) {
256 111 if ( empty( $addons ) ) {
257 112 $addons = $this->get_api_info();
258 113 }
259 -
260 114 $download_id = $license_plugin->download_id;
261 - $plugin = array();
262 -
115 + $plugin = array();
263 116 if ( empty( $download_id ) && ! empty( $addons ) ) {
264 117 foreach ( $addons as $addon ) {
265 - if ( is_array( $addon ) && ! empty( $addon['title'] ) && strtolower( $license_plugin->plugin_name ) === strtolower( $addon['title'] ) ) {
118 + if ( strtolower( $license_plugin->plugin_name ) == strtolower( $addon['title'] ) ) {
266 119 return $addon;
267 120 }
268 121 }
269 122 } elseif ( isset( $addons[ $download_id ] ) ) {
@@ -274,16 +127,13 @@
274 127 }
275 128
276 129 /**
277 130 * @since 3.06
278 - *
279 - * @return false|object
280 131 */
281 132 public function get_pro_updater() {
282 133 if ( FrmAppHelper::pro_is_installed() && is_callable( 'FrmProAppHelper::get_updater' ) ) {
283 134 $updater = FrmProAppHelper::get_updater();
284 135 $this->set_license( $updater->license );
285 -
286 136 return $updater;
287 137 }
288 138
289 139 return false;
@@ -290,131 +140,53 @@
290 140 }
291 141
292 142 /**
293 143 * @since 3.06
294 - *
295 - * @return array|bool
144 + * @return array
296 145 */
297 146 protected function get_cached() {
298 - $cache = $this->get_cached_option();
147 + $cache = get_option( $this->cache_key );
299 148
300 - if ( empty( $cache ) ) {
301 - return false;
149 + if ( empty( $cache ) || empty( $cache['timeout'] ) || current_time( 'timestamp' ) > $cache['timeout'] ) {
150 + return false; // Cache is expired
302 151 }
303 152
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 - }
310 -
311 - $version = FrmAppHelper::plugin_version();
312 - $for_current = isset( $cache['version'] ) && $cache['version'] == $version;
313 -
314 - if ( ! $for_current ) {
315 - // Force a new check.
316 - return false;
317 - }
318 - }
319 -
320 - $values = json_decode( $cache['value'], true );
321 -
322 - return $values;
153 + return json_decode( $cache['value'], true );
323 154 }
324 155
325 156 /**
326 - * Get the cache for the network if multisite.
327 - *
328 - * @since 6.8.3
329 - *
330 - * @return mixed
331 - */
332 - protected function get_cached_option() {
333 - if ( is_multisite() ) {
334 - $cached = get_site_option( $this->cache_key );
335 -
336 - if ( $cached ) {
337 - return $cached;
338 - }
339 - }
340 -
341 - return get_option( $this->cache_key );
342 - }
343 -
344 - /**
345 157 * @since 3.06
346 - *
347 - * @param array $addons
348 - *
349 - * @return void
350 158 */
351 159 protected function set_cached( $addons ) {
352 160 $data = array(
353 - 'timeout' => strtotime( $this->get_cache_timeout( $addons ), time() ),
354 - 'value' => wp_json_encode( $addons ),
355 - 'version' => FrmAppHelper::plugin_version(),
161 + 'timeout' => strtotime( $this->cache_timeout, current_time( 'timestamp' ) ),
162 + 'value' => json_encode( $addons ),
356 163 );
357 164
358 - if ( is_multisite() ) {
359 - update_site_option( $this->cache_key, $data );
360 - } else {
361 - update_option( $this->cache_key, $data, 'no' );
362 - }
165 + update_option( $this->cache_key, $data, 'no' );
363 166 }
364 167
365 168 /**
366 - * If the last check was a a rate limit, we'll need to check again sooner.
367 - *
368 - * @since 6.8.3
369 - *
370 - * @param array $addons
371 - *
372 - * @return string
373 - */
374 - protected function get_cache_timeout( $addons ) {
375 - $timeout = $this->cache_timeout;
376 -
377 - if ( isset( $addons['response_code'] ) && 429 === $addons['response_code'] ) {
378 - $timeout = '+5 minutes';
379 - }
380 - return $timeout;
381 - }
382 -
383 - /**
384 169 * @since 3.06
385 - *
386 - * @return void
387 170 */
388 171 public function reset_cached() {
389 - if ( is_multisite() ) {
390 - delete_site_option( $this->cache_key );
391 - } else {
392 - delete_option( $this->cache_key );
393 - }
394 - $this->done_running();
172 + delete_option( $this->cache_key );
395 173 }
396 174
397 175 /**
398 176 * @since 3.06
399 - *
400 177 * @return array
401 178 */
402 179 public function error_for_license() {
403 180 $errors = array();
404 -
405 181 if ( ! empty( $this->license ) ) {
406 182 $errors = $this->get_error_from_response();
407 183 }
408 -
409 184 return $errors;
410 185 }
411 186
412 187 /**
413 188 * @since 3.06
414 - *
415 - * @param array $addons
416 - *
417 189 * @return array
418 190 */
419 191 public function get_error_from_response( $addons = array() ) {
420 192 if ( empty( $addons ) ) {
@@ -419,33 +191,12 @@
419 191 public function get_error_from_response( $addons = array() ) {
420 192 if ( empty( $addons ) ) {
421 193 $addons = $this->get_api_info();
422 194 }
423 -
424 195 $errors = array();
425 -
426 196 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 - }
432 -
197 + $errors[] = $addons['error']['message'];
433 198 do_action( 'frm_license_error', $addons['error'] );
434 199 }
435 -
436 200 return $errors;
437 - }
438 -
439 - /**
440 - * Check if a template is new.
441 - *
442 - * @since 6.0
443 - *
444 - * @param array $addon
445 - *
446 - * @return bool
447 - */
448 - protected function is_new( $addon ) {
449 - return strtotime( $addon['released'] ) > strtotime( '-' . $this->new_days . ' days' );
450 201 }
451 202 }