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/FrmAddon.php +96 -396 6.276.8 View file →
@@ -4,116 +4,38 @@
4 4 }
5 5
6 6 /** @phpstan-consistent-constructor */
7 7 class FrmAddon {
8 -
9 - /**
10 - * @var string
11 - */
12 8 public $store_url = 'https://formidableforms.com';
13 -
14 - /**
15 - * @var int|string|null
16 - */
17 9 public $download_id;
18 -
19 - /**
20 - * @var string|null
21 - */
22 10 public $plugin_file;
23 -
24 - /**
25 - * @var string
26 - */
27 11 public $plugin_folder;
28 -
29 - /**
30 - * @var string
31 - */
32 12 public $plugin_name;
33 -
34 - /**
35 - * @var string
36 - */
37 13 public $plugin_slug;
38 -
39 - /**
40 - * @var string
41 - */
42 14 public $option_name;
43 -
44 - /**
45 - * @var string|null
46 - */
47 15 public $version;
48 -
49 - /**
50 - * @var string
51 - */
52 16 public $author = 'Strategy11';
53 -
54 - /**
55 - * @var bool
56 - */
57 17 public $is_parent_licence = false;
58 -
59 - /**
60 - * @var bool
61 - */
62 18 public $needs_license = true;
63 -
64 - /**
65 - * @var bool
66 - */
67 19 private $is_expired_addon = false;
68 -
69 - /**
70 - * @var string|null
71 - */
72 20 public $license;
73 -
74 - /**
75 - * @var bool
76 - */
77 21 protected $get_beta = false;
78 22
79 23 /**
80 - * @var array|null
81 - */
82 - protected $save_status;
83 -
84 - /**
85 - * This is used to decide whether the license checks should continue.
86 - * The point is to avoid license issues when a site url changes.
87 - *
88 - * @since 6.8.3
89 - *
90 - * @var array
91 - */
92 - private $save_response = array();
93 -
94 - /**
95 24 * This is used to flag other add ons not to send a request.
96 - * We only want to send a single API request per page load.
25 + * We only want to send a single API request per HTTP request.
97 26 *
98 - * @since 6.8.3
27 + * @since 6.7.1
99 28 *
100 - * @var string
101 - */
102 - private $transient_lock_key = 'frm_activate_request_lock';
103 -
104 - /**
105 - * @since 6.8.3
106 - *
107 29 * @var bool
108 30 */
109 - protected $should_clear_cache = true;
31 + private static $sent_mothership_request = false;
110 32
111 33 public function __construct() {
34 +
112 35 if ( empty( $this->plugin_slug ) ) {
113 36 $this->plugin_slug = preg_replace( '/[^a-zA-Z0-9_\s]/', '', str_replace( ' ', '_', strtolower( $this->plugin_name ) ) );
114 37 }
115 -
116 38 if ( empty( $this->option_name ) ) {
117 39 $this->option_name = 'edd_' . $this->plugin_slug . '_license_';
118 40 }
119 41
@@ -123,46 +45,38 @@
123 45 add_filter( 'frm_installed_addons', array( &$this, 'insert_installed_addon' ) );
124 46 $this->edd_plugin_updater();
125 47 }
126 48
127 - /**
128 - * @return void
129 - */
130 49 public static function load_hooks() {
131 50 add_filter( 'frm_include_addon_page', '__return_true' );
132 51 new static();
133 52 }
134 53
135 - /**
136 - * @param array $plugins
137 - *
138 - * @return array
139 - */
140 54 public function insert_installed_addon( $plugins ) {
141 55 $plugins[ $this->plugin_slug ] = $this;
56 +
142 57 return $plugins;
143 58 }
144 59
145 - /**
146 - * @param string $plugin_slug
147 - *
148 - * @return false|FrmAddon
149 - */
150 60 public static function get_addon( $plugin_slug ) {
151 61 $plugins = apply_filters( 'frm_installed_addons', array() );
152 - return $plugins[ $plugin_slug ] ?? false;
62 + $plugin = false;
63 + if ( isset( $plugins[ $plugin_slug ] ) ) {
64 + $plugin = $plugins[ $plugin_slug ];
65 + }
66 +
67 + return $plugin;
153 68 }
154 69
155 - /**
156 - * @return void
157 - */
158 70 public function edd_plugin_updater() {
71 +
159 72 $this->is_license_revoked();
160 73 $license = $this->license;
161 74
162 75 add_action( 'after_plugin_row_' . plugin_basename( $this->plugin_file ), array( $this, 'maybe_show_license_message' ), 10, 2 );
163 76
164 - if ( $license ) {
77 + if ( ! empty( $license ) ) {
78 +
165 79 if ( 'formidable/formidable.php' !== $this->plugin_folder ) {
166 80 add_filter( 'plugins_api', array( &$this, 'plugins_api_filter' ), 10, 3 );
167 81 }
168 82
@@ -170,9 +84,9 @@
170 84 }
171 85 }
172 86
173 87 /**
174 - * Updates information on the "View version 6.15 details" page with custom data.
88 + * Updates information on the "View version x.x details" page with custom data.
175 89 *
176 90 * @uses api_request()
177 91 *
178 92 * @param mixed $_data
@@ -181,22 +95,21 @@
181 95 *
182 96 * @return object $_data
183 97 */
184 98 public function plugins_api_filter( $_data, $_action = '', $_args = null ) {
185 - if ( $_action !== 'plugin_information' ) {
99 +
100 + if ( $_action != 'plugin_information' ) {
186 101 return $_data;
187 102 }
188 103
189 104 $slug = basename( $this->plugin_file, '.php' );
190 105 $slug2 = str_replace( '/' . $slug . '.php', '', $this->plugin_folder );
191 -
192 - if ( empty( $_args->slug ) || ( $_args->slug !== $slug && $_args->slug !== $slug2 ) ) {
106 + if ( empty( $_args->slug ) || ( $_args->slug != $slug && $_args->slug !== $slug2 ) ) {
193 107 return $_data;
194 108 }
195 109
196 110 $item_id = $this->download_id;
197 -
198 - if ( ! $item_id ) {
111 + if ( empty( $item_id ) ) {
199 112 $_data = array(
200 113 'name' => $this->plugin_name,
201 114 'excerpt' => '',
202 115 'changelog' => 'See the full changelog at <a href="' . esc_url( $this->store_url . '/changelog/' ) . '"></a>',
@@ -220,21 +133,16 @@
220 133
221 134 return (object) $_data;
222 135 }
223 136
224 - /**
225 - * @return string
226 - */
227 137 public function get_license() {
228 138 $license = $this->maybe_get_pro_license();
229 -
230 - if ( $license ) {
139 + if ( ! empty( $license ) ) {
231 140 return $license;
232 141 }
233 142
234 143 $license = trim( get_option( $this->option_name . 'key' ) );
235 -
236 - if ( ! $license ) {
144 + if ( empty( $license ) ) {
237 145 $license = $this->activate_defined_license();
238 146 }
239 147
240 148 return $license;
@@ -241,14 +149,12 @@
241 149 }
242 150
243 151 /**
244 152 * @since 3.04.03
245 - *
246 - * @return false|string
247 153 */
248 154 protected function maybe_get_pro_license() {
249 155 // prevent a loop if $this is the pro plugin
250 - $get_license = FrmAppHelper::pro_is_installed() && is_callable( 'FrmProAppHelper::get_updater' ) && $this->plugin_name !== 'Formidable Pro';
156 + $get_license = FrmAppHelper::pro_is_installed() && is_callable( 'FrmProAppHelper::get_updater' ) && $this->plugin_name != 'Formidable Pro';
251 157
252 158 if ( ! $get_license ) {
253 159 return false;
254 160 }
@@ -255,15 +161,13 @@
255 161
256 162 $api = new FrmFormApi();
257 163 $api->get_pro_updater();
258 164 $license = $api->get_license();
259 -
260 - if ( ! $license ) {
165 + if ( empty( $license ) ) {
261 166 return false;
262 167 }
263 168
264 169 $this->get_api_info( $license );
265 -
266 170 if ( ! $this->is_parent_licence ) {
267 171 $license = false;
268 172 }
269 173
@@ -273,17 +177,14 @@
273 177 /**
274 178 * Activate the license in wp-config.php
275 179 *
276 180 * @since 2.04
277 - *
278 - * @return string
279 181 */
280 182 public function activate_defined_license() {
281 183 $license = $this->get_defined_license();
282 -
283 - if ( $license && ! $this->is_active() && ! $this->checked_recently( '1 day' ) ) {
184 + if ( ! empty( $license ) && ! $this->is_active() && $this->is_time_to_auto_activate() ) {
284 185 $response = $this->activate_license( $license );
285 -
186 + $this->set_auto_activate_time();
286 187 if ( ! $response['success'] ) {
287 188 $license = '';
288 189 }
289 190 }
@@ -294,28 +195,35 @@
294 195 /**
295 196 * Check the wp-config.php for the license key
296 197 *
297 198 * @since 2.04
298 - *
299 - * @return false|string
300 199 */
301 200 public function get_defined_license() {
302 - $constant_name = 'FRM_' . strtoupper( $this->plugin_slug ) . '_LICENSE';
303 - return defined( $constant_name ) ? constant( $constant_name ) : false;
201 + $consant_name = 'FRM_' . strtoupper( $this->plugin_slug ) . '_LICENSE';
202 +
203 + return defined( $consant_name ) ? constant( $consant_name ) : false;
304 204 }
305 205
306 - /**
307 - * @param string $license
308 - *
309 - * @return void
310 - */
311 206 public function set_license( $license ) {
312 207 update_option( $this->option_name . 'key', $license );
313 208 }
314 209
315 210 /**
316 - * @return mixed
211 + * If the license is in the config, limit the frequency of checks.
212 + * The license may be entered incorrectly, so we don't want to check on every page load.
213 + *
214 + * @since 2.04
317 215 */
216 + private function is_time_to_auto_activate() {
217 + $last_try = get_option( $this->option_name . 'last_activate' );
218 +
219 + return ( ! $last_try || $last_try < strtotime( '-1 day' ) );
220 + }
221 +
222 + private function set_auto_activate_time() {
223 + update_option( $this->option_name . 'last_activate', time() );
224 + }
225 +
318 226 public function is_active() {
319 227 return get_option( $this->option_name . 'active' );
320 228 }
321 229
@@ -321,63 +229,26 @@
321 229
322 230 /**
323 231 * @since 3.04.03
324 232 *
325 - * @param array|string $error
326 - *
327 - * @return void
233 + * @param array $error
328 234 */
329 235 public function maybe_clear_license( $error ) {
330 - if ( is_array( $error ) && $error['code'] === 'disabled' && $error['license'] === $this->license ) {
236 + if ( $error['code'] === 'disabled' && $error['license'] === $this->license ) {
331 237 $this->clear_license();
332 238 }
333 239 }
334 240
335 - /**
336 - * @return void
337 - */
338 241 public function clear_license() {
339 242 delete_option( $this->option_name . 'active' );
340 243 delete_option( $this->option_name . 'key' );
341 -
342 - if ( $this->should_clear_cache ) {
343 - delete_site_option( $this->transient_key() );
344 - delete_option( $this->transient_key() );
345 - $this->delete_cache();
346 - $this->should_clear_cache = true;
347 - }
244 + delete_site_option( $this->transient_key() );
245 + delete_option( $this->transient_key() );
246 + $this->delete_cache();
348 247 }
349 248
350 - /**
351 - * Don't save an invalid license.
352 - *
353 - * @since 6.8.3
354 - *
355 - * @param bool $is_valid If license activation was successful.
356 - *
357 - * @return void
358 - */
359 - protected function maybe_set_active( $is_valid ) {
360 - update_option( $this->option_name . 'active', $is_valid );
361 -
362 - if ( $is_valid ) {
363 - $this->set_active( $is_valid );
364 - return;
365 - }
366 -
367 - // Don't save the license if it's invalid.
368 - $this->should_clear_cache = false;
369 - $this->clear_license();
370 - delete_option( $this->option_name . 'key' );
371 - $this->license = '';
372 - }
373 -
374 - /**
375 - * @param bool $is_active
376 - *
377 - * @return void
378 - */
379 249 public function set_active( $is_active ) {
250 + update_option( $this->option_name . 'active', $is_active );
380 251 $this->delete_cache();
381 252 FrmAppHelper::save_combined_js();
382 253 $this->update_pro_capabilities();
383 254 }
@@ -385,10 +256,8 @@
385 256 /**
386 257 * Updates roles capabilities after pro license is active.
387 258 *
388 259 * @since 5.0
389 - *
390 - * @return void
391 260 */
392 261 protected function update_pro_capabilities() {
393 262 global $wp_roles;
394 263
@@ -398,19 +267,18 @@
398 267
399 268 $caps = FrmAppHelper::frm_capabilities( 'pro_only' );
400 269 $roles = get_editable_roles();
401 270 $settings = new FrmSettings();
402 -
403 271 foreach ( $caps as $cap => $cap_desc ) {
404 - $cap_roles = (array) ( $settings->$cap ?? 'administrator' );
272 + $cap_roles = (array) ( isset( $settings->$cap ) ? $settings->$cap : 'administrator' );
405 273
406 274 // Make sure administrators always have permissions.
407 - if ( ! in_array( 'administrator', $cap_roles, true ) ) {
275 + if ( ! in_array( 'administrator', $cap_roles ) ) {
408 276 array_push( $cap_roles, 'administrator' );
409 277 }
410 278
411 279 foreach ( $roles as $role => $details ) {
412 - if ( in_array( $role, $cap_roles, true ) ) {
280 + if ( in_array( $role, $cap_roles ) ) {
413 281 $wp_roles->add_cap( $role, $cap );
414 282 } else {
415 283 $wp_roles->remove_cap( $role, $cap );
416 284 }
@@ -419,17 +287,12 @@
419 287 }
420 288
421 289 /**
422 290 * @since 3.04.03
423 - *
424 - * @return void
425 291 */
426 292 protected function delete_cache() {
427 293 delete_transient( 'frm_api_licence' );
428 294
429 - // Cleanup option that has been removed.
430 - delete_option( $this->option_name . 'last_activate' );
431 -
432 295 $api = new FrmFormApi( $this->license );
433 296 $api->reset_cached();
434 297
435 298 $api = new FrmFormTemplateApi( $this->license );
@@ -436,14 +299,8 @@
436 299 $api->reset_cached();
437 300
438 301 $api = new FrmApplicationApi( $this->license );
439 302 $api->reset_cached();
440 -
441 - $api = new FrmSalesApi();
442 - $api->reset_cached();
443 -
444 - $api = new FrmStyleApi();
445 - $api->reset_cached();
446 303 }
447 304
448 305 /**
449 306 * The Pro version includes the show_license_message function.
@@ -449,13 +306,8 @@
449 306 * The Pro version includes the show_license_message function.
450 307 * We need an extra check before we allow it to show a message.
451 308 *
452 309 * @since 3.04.03
453 - *
454 - * @param string $file File path of the plugin.
455 - * @param array $plugin Plugin data.
456 - *
457 - * @return void
458 310 */
459 311 public function maybe_show_license_message( $file, $plugin ) {
460 312 if ( $this->is_expired_addon || isset( $plugin['package'] ) ) {
461 313 // let's not show a ton of duplicate messages
@@ -464,30 +316,22 @@
464 316
465 317 $this->show_license_message( $file, $plugin );
466 318 }
467 319
468 - /**
469 - * @param string $file File path of the plugin.
470 - * @param array $plugin Plugin data.
471 - *
472 - * @return void
473 - */
474 320 public function show_license_message( $file, $plugin ) {
475 321 $message = '';
476 -
477 322 if ( empty( $this->license ) ) {
478 323 /* translators: %1$s: Plugin name, %2$s: Start link HTML, %3$s: end link HTML */
479 - $message = sprintf( esc_html__( 'Your %1$s license key is missing. Please add it on the %2$slicenses page%3$s.', 'formidable' ), esc_html( $this->plugin_name ), '<a href="' . esc_url( admin_url( 'admin.php?page=formidable-settings' ) ) . '">', '</a>' ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
324 + $message = sprintf( esc_html__( 'Your %1$s license key is missing. Please add it on the %2$slicenses page%3$s.', 'formidable' ), esc_html( $this->plugin_name ), '<a href="' . esc_url( admin_url( 'admin.php?page=formidable-settings' ) ) . '">', '</a>' );
480 325 } else {
481 326 $api = new FrmFormApi( $this->license );
482 327 $errors = $api->error_for_license();
483 -
484 - if ( $errors ) {
328 + if ( ! empty( $errors ) ) {
485 329 $message = reset( $errors );
486 330 }
487 331 }
488 332
489 - if ( ! $message ) {
333 + if ( empty( $message ) ) {
490 334 return;
491 335 }
492 336
493 337 $wp_list_table = _get_list_table( 'WP_Plugins_List_Table' );
@@ -492,19 +336,14 @@
492 336
493 337 $wp_list_table = _get_list_table( 'WP_Plugins_List_Table' );
494 338 $id = sanitize_title( $plugin['Name'] ) . '-next';
495 339
496 - echo '<tr class="plugin-update-tr active" id="' . esc_attr( $id ) . '"><td colspan="' . esc_attr( $wp_list_table->get_column_count() ) . '" class="plugin-update colspanchange"><div class="update-message notice error inline notice-error notice-alt"><p>'; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
497 - FrmAppHelper::kses_echo( $message, 'a' );
498 - echo '<script type="text/javascript">var d = document.getElementById("' . esc_attr( $id ) . '").previousSibling;if ( d !== null ){ d.className = d.className + " update"; }</script>'; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
340 + echo '<tr class="plugin-update-tr active" id="' . esc_attr( $id ) . '"><td colspan="' . esc_attr( $wp_list_table->get_column_count() ) . '" class="plugin-update colspanchange"><div class="update-message notice error inline notice-error notice-alt"><p>';
341 + echo FrmAppHelper::kses( $message, 'a' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
342 + echo '<script type="text/javascript">var d = document.getElementById("' . esc_attr( $id ) . '").previousSibling;if ( d !== null ){ d.className = d.className + " update"; }</script>';
499 343 echo '</p></div></td></tr>';
500 344 }
501 345
502 - /**
503 - * @param mixed $transient
504 - *
505 - * @return mixed
506 - */
507 346 public function clear_expired_download( $transient ) {
508 347 if ( ! is_object( $transient ) ) {
509 348 return $transient;
510 349 }
@@ -535,22 +374,18 @@
535 374 *
536 375 * @since 3.04.03
537 376 *
538 377 * @param object $transient The current plugin info saved for update.
539 - *
540 - * @return void
541 378 */
542 379 private function prepare_update_details( &$transient ) {
543 380 $version_info = $transient;
544 - $has_beta_url = ! empty( $version_info->beta );
545 -
381 + $has_beta_url = isset( $version_info->beta ) && ! empty( $version_info->beta );
546 382 if ( $this->get_beta && ! $has_beta_url ) {
547 383 $version_info = (object) $this->get_api_info( $this->license );
548 384 }
549 385
550 - if ( ! empty( $version_info->new_version ) ) {
386 + if ( isset( $version_info->new_version ) && ! empty( $version_info->new_version ) ) {
551 387 $this->clear_old_plugin_version( $version_info );
552 -
553 388 if ( $version_info === false ) {
554 389 // Was cleared with timeout.
555 390 $transient = false;
556 391 } else {
@@ -566,12 +401,8 @@
566 401 /**
567 402 * Get the API info for this plugin
568 403 *
569 404 * @since 3.04.03
570 - *
571 - * @param string $license The license key.
572 - *
573 - * @return array The API info for the plugin.
574 405 */
575 406 protected function get_api_info( $license ) {
576 407 $api = new FrmFormApi( $license );
577 408 $addon = $api->get_addon_for_license( $this );
@@ -592,17 +423,12 @@
592 423 * Make sure transients don't stick around on sites that
593 424 * don't save the transient expiration
594 425 *
595 426 * @since 2.05.05
596 - *
597 - * @param object $version_info The version info for the plugin.
598 - *
599 - * @return void
600 427 */
601 428 private function clear_old_plugin_version( &$version_info ) {
602 - $timeout = ! empty( $version_info->timeout ) ? $version_info->timeout : 0;
603 -
604 - if ( $timeout && time() > $timeout ) {
429 + $timeout = ( isset( $version_info->timeout ) && ! empty( $version_info->timeout ) ) ? $version_info->timeout : 0;
430 + if ( ! empty( $timeout ) && time() > $timeout ) {
605 431 // Cache is expired.
606 432 $version_info = false;
607 433 $api = new FrmFormApi( $this->license );
608 434 $api->reset_cached();
@@ -613,78 +439,60 @@
613 439 * The beta url is always included if the download has a beta.
614 440 * Check if the beta should be downloaded.
615 441 *
616 442 * @since 3.04.03
617 - *
618 - * @param object $version_info The version info for the plugin.
619 - *
620 - * @return void
621 443 */
622 444 private function maybe_use_beta_url( &$version_info ) {
623 - if ( $this->get_beta && ! empty( $version_info->beta ) ) {
445 + if ( $this->get_beta && isset( $version_info->beta ) && ! empty( $version_info->beta ) ) {
624 446 $version_info->new_version = $version_info->beta['version'];
625 447 $version_info->package = $version_info->beta['package'];
626 -
627 - if ( ! empty( $version_info->plugin ) ) {
448 + if ( isset( $version_info->plugin ) && ! empty( $version_info->plugin ) ) {
628 449 $version_info->plugin = $version_info->beta['plugin'];
629 450 }
630 451 }
631 452 }
632 453
633 - /**
634 - * @param object $transient
635 - *
636 - * @return bool
637 - */
638 454 private function is_current_version( $transient ) {
639 455 if ( empty( $transient->checked ) || ! isset( $transient->checked[ $this->plugin_folder ] ) ) {
640 456 return false;
641 457 }
642 458
643 - $response = empty( $transient->response );
644 -
459 + $response = ! isset( $transient->response ) || empty( $transient->response );
645 460 if ( $response ) {
646 461 return true;
647 462 }
648 463
649 - return isset( $transient->response ) && isset( $transient->response[ $this->plugin_folder ] ) && $transient->checked[ $this->plugin_folder ] === $transient->response[ $this->plugin_folder ]->new_version; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
464 + return isset( $transient->response ) && isset( $transient->response[ $this->plugin_folder ] ) && $transient->checked[ $this->plugin_folder ] === $transient->response[ $this->plugin_folder ]->new_version;
650 465 }
651 466
652 - /**
653 - * @return bool
654 - */
655 467 private function has_been_cleared() {
656 468 $last_cleared = get_option( 'frm_last_cleared' );
657 - return $last_cleared && $last_cleared > gmdate( 'Y-m-d H:i:s', strtotime( '-5 minutes' ) );
469 +
470 + return ( $last_cleared && $last_cleared > gmdate( 'Y-m-d H:i:s', strtotime( '-5 minutes' ) ) );
658 471 }
659 472
660 - /**
661 - * @return void
662 - */
663 473 private function cleared_plugins() {
664 474 update_option( 'frm_last_cleared', gmdate( 'Y-m-d H:i:s' ) );
665 475 }
666 476
667 - /**
668 - * @return void
669 - */
670 477 private function is_license_revoked() {
671 - if ( empty( $this->license ) || empty( $this->plugin_slug ) || isset( $_POST['license'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
478 + if ( self::$sent_mothership_request ) {
672 479 return;
673 480 }
674 481
675 - if ( $this->get_defined_license() ) {
676 - // Don't check if the license is defined in wp-config.php since we can't remove it.
482 + if ( empty( $this->license ) || empty( $this->plugin_slug ) || isset( $_POST['license'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
677 483 return;
678 484 }
679 485
680 486 // Only check weekly.
681 - if ( $this->checked_recently( '7 days', 'valid' ) || $this->is_running() ) {
487 + if ( $this->checked_recently( '7 days' ) ) {
682 488 return;
683 489 }
684 490
491 + self::$sent_mothership_request = true;
492 + $this->update_last_checked();
493 +
685 494 $response = $this->get_license_status();
686 -
687 495 if ( 'revoked' === $response['status'] || 'blocked' === $response['status'] || 'disabled' === $response['status'] || 'missing' === $response['status'] ) {
688 496 $this->clear_license();
689 497 }
690 498 }
@@ -691,47 +499,27 @@
691 499
692 500 /**
693 501 * Has this been checked too recently?
694 502 *
695 - * @param string $time ie. '1 day'.
696 - * @param string $required_status Return false if the last check does not match. ie 'valid'.
697 - *
503 + * @param string $time ie. '1 day'.
698 504 * @return bool
699 505 */
700 - private function checked_recently( $time, $required_status = '' ) {
506 + private function checked_recently( $time ) {
701 507 $last_checked = $this->last_checked();
702 - $is_429 = isset( $last_checked['response_code'] ) && 429 === $last_checked['response_code'];
703 -
704 - if ( $is_429 ) {
705 - // If the last check was a a rate limit, we'll need to check again sooner.
706 - $time = '5 minutes';
707 - $required_status = '';
708 - }
709 -
710 - if ( $required_status && ( ! isset( $last_checked['status'] ) || $last_checked['status'] !== $required_status ) ) {
711 - // If the last check was invalid, we don't need to check again.
712 - return true;
713 - }
714 -
715 - $checked_time = $last_checked['time'] ?? false;
716 508 $time_ago = gmdate( 'Y-m-d H:i:s', strtotime( '-' . $time ) );
717 - return $checked_time && $checked_time > $time_ago;
509 + return $last_checked && $last_checked > $time_ago;
718 510 }
719 511
720 512 /**
721 - * @since 6.8.3 Switched to an array to store extra response info.
722 - *
723 - * @return array
513 + * @return bool|string
724 514 */
725 515 private function last_checked() {
726 - $last_checked = is_multisite() ? get_site_option( $this->transient_key() ) : get_option( $this->transient_key() );
727 -
728 - if ( $last_checked && ! is_array( $last_checked ) ) {
729 - // Get string into array for existing values.
730 - $last_checked = array( 'time' => $last_checked );
516 + if ( is_multisite() ) {
517 + $last_checked = get_site_option( $this->transient_key() );
518 + } else {
519 + $last_checked = get_option( $this->transient_key() );
731 520 }
732 -
733 - return $last_checked ? $last_checked : array();
521 + return $last_checked;
734 522 }
735 523
736 524 /**
737 525 * @return void
@@ -736,36 +524,28 @@
736 524 /**
737 525 * @return void
738 526 */
739 527 private function update_last_checked() {
740 - $this->save_response['time'] = gmdate( 'Y-m-d H:i:s' );
741 -
742 528 if ( is_multisite() ) {
743 - update_site_option( $this->transient_key(), $this->save_response );
529 + update_site_option( $this->transient_key(), gmdate( 'Y-m-d H:i:s' ) );
744 530 } else {
745 - update_option( $this->transient_key(), $this->save_response );
531 + update_option( $this->transient_key(), gmdate( 'Y-m-d H:i:s' ) );
746 532 }
747 533 }
748 534
749 535 /**
750 536 * Use a new cache after the license is changed, or Formidable is updated.
751 - *
752 - * @return string
753 537 */
754 538 private function transient_key() {
755 539 return 'frm_' . md5( sanitize_key( $this->license . '_' . $this->plugin_slug ) );
756 540 }
757 541
758 - /**
759 - * @return void
760 - */
761 542 public static function activate() {
762 543 FrmAppHelper::permission_check( 'frm_change_settings' );
763 544 check_ajax_referer( 'frm_ajax', 'nonce' );
764 545
765 546 $license = stripslashes( FrmAppHelper::get_param( 'license', '', 'post', 'sanitize_text_field' ) );
766 -
767 - if ( ! $license ) {
547 + if ( empty( $license ) ) {
768 548 wp_send_json(
769 549 array(
770 550 'message' => __( 'Oops! You forgot to enter your license number.', 'formidable' ),
771 551 'success' => false,
@@ -775,20 +555,13 @@
775 555
776 556 $plugin_slug = FrmAppHelper::get_param( 'plugin', '', 'post', 'sanitize_text_field' );
777 557 $response = self::activate_license_for_plugin( $license, $plugin_slug );
778 558
779 - FrmInbox::clear_cache();
780 -
781 559 wp_send_json( $response );
782 560 }
783 561
784 562 /**
785 563 * @since 4.08
786 - *
787 - * @param string $license The license key.
788 - * @param string $plugin_slug The plugin slug.
789 - *
790 - * @return array The response from the license activation.
791 564 */
792 565 public static function activate_license_for_plugin( $license, $plugin_slug ) {
793 566 $this_plugin = self::get_addon( $plugin_slug );
794 567 return $this_plugin->activate_license( $license );
@@ -793,13 +566,8 @@
793 566 $this_plugin = self::get_addon( $plugin_slug );
794 567 return $this_plugin->activate_license( $license );
795 568 }
796 569
797 - /**
798 - * @param string $license The license key.
799 - *
800 - * @return array The response from the license activation.
801 - */
802 570 private function activate_license( $license ) {
803 571 $this->set_license( $license );
804 572 $this->license = $license;
805 573
@@ -812,9 +580,8 @@
812 580 if ( $response['error'] ) {
813 581 $response['message'] = $response['status'];
814 582 } else {
815 583 $messages = $this->get_messages();
816 -
817 584 if ( is_string( $response['status'] ) && isset( $messages[ $response['status'] ] ) ) {
818 585 $response['message'] = $messages[ $response['status'] ];
819 586 } else {
820 587 $response['message'] = FrmAppHelper::kses( $response['status'], array( 'a' ) );
@@ -820,14 +587,13 @@
820 587 $response['message'] = FrmAppHelper::kses( $response['status'], array( 'a' ) );
821 588 }
822 589
823 590 $is_valid = false;
824 -
825 591 if ( 'valid' === $response['status'] ) {
826 592 $is_valid = 'valid';
827 593 $response['success'] = true;
828 594 }
829 - $this->maybe_set_active( $is_valid );
595 + $this->set_active( $is_valid );
830 596 }
831 597
832 598 $this->update_last_checked();
833 599
@@ -852,21 +618,16 @@
852 618 )
853 619 );
854 620 }
855 621
856 - /**
857 - * @return array
858 - */
859 622 private function get_license_status() {
860 - $this->set_running();
861 -
862 623 $response = array(
863 624 'status' => 'missing',
864 625 'error' => true,
865 626 );
866 -
867 627 if ( empty( $this->license ) ) {
868 628 $response['error'] = false;
629 +
869 630 return $response;
870 631 }
871 632
872 633 try {
@@ -875,10 +636,9 @@
875 636
876 637 // $license_data->license will be either "valid" or "invalid"
877 638 if ( is_array( $license_data ) ) {
878 639 if ( ! empty( $license_data['license'] ) && in_array( $license_data['license'], array( 'valid', 'invalid' ), true ) ) {
879 - $response['status'] = $license_data['license'];
880 - $this->save_status['status'] = $license_data['license'];
640 + $response['status'] = $license_data['license'];
881 641 }
882 642 } else {
883 643 $response['status'] = $license_data;
884 644 }
@@ -885,16 +645,11 @@
885 645 } catch ( Exception $e ) {
886 646 $response['status'] = $e->getMessage();
887 647 }
888 648
889 - $this->update_last_checked();
890 - $this->done_running();
891 649 return $response;
892 650 }
893 651
894 - /**
895 - * @return array
896 - */
897 652 private function get_messages() {
898 653 return array(
899 654 'valid' => __( 'Your license has been activated. Enjoy!', 'formidable' ),
900 655 'invalid' => __( 'That license key is invalid', 'formidable' ),
@@ -907,10 +662,8 @@
907 662 }
908 663
909 664 /**
910 665 * @since 4.03
911 - *
912 - * @return void
913 666 */
914 667 public static function reset_cache() {
915 668 FrmAppHelper::permission_check( 'frm_change_settings' );
916 669 check_ajax_referer( 'frm_ajax', 'nonce' );
@@ -925,11 +678,8 @@
925 678
926 679 wp_send_json( $response );
927 680 }
928 681
929 - /**
930 - * @return void
931 - */
932 682 public static function deactivate() {
933 683 FrmAppHelper::permission_check( 'frm_change_settings' );
934 684 check_ajax_referer( 'frm_ajax', 'nonce' );
935 685
@@ -938,13 +688,11 @@
938 688 $response = array(
939 689 'success' => false,
940 690 'message' => '',
941 691 );
942 -
943 692 try {
944 693 // $license_data->license will be either "deactivated" or "failed"
945 694 $license_data = $this_plugin->send_mothership_request( 'deactivate_license' );
946 -
947 695 if ( is_array( $license_data ) && 'deactivated' === $license_data['license'] ) {
948 696 $response['success'] = true;
949 697 $response['message'] = __( 'That license was removed successfully', 'formidable' );
950 698 } else {
@@ -954,9 +702,8 @@
954 702 $response['message'] = $e->getMessage();
955 703 }
956 704
957 705 $this_plugin->clear_license();
958 - FrmInbox::clear_cache();
959 706
960 707 wp_send_json( $response );
961 708 }
962 709
@@ -961,10 +708,8 @@
961 708 }
962 709
963 710 /**
964 711 * @since 4.03
965 - *
966 - * @return FrmAddon
967 712 */
968 713 private static function set_license_from_post() {
969 714 $plugin_slug = FrmAppHelper::get_param( 'plugin', '', 'post', 'sanitize_text_field' );
970 715 $this_plugin = self::get_addon( $plugin_slug );
@@ -973,10 +718,8 @@
973 718 return $this_plugin;
974 719 }
975 720
976 721 /**
977 - * @param string $action
978 - *
979 722 * @return string
980 723 */
981 724 public function send_mothership_request( $action ) {
982 725 $api_params = array(
@@ -983,9 +726,8 @@
983 726 'edd_action' => $action,
984 727 'license' => $this->license,
985 728 'url' => home_url(),
986 729 );
987 -
988 730 if ( is_numeric( $this->download_id ) ) {
989 731 $api_params['item_id'] = absint( $this->download_id );
990 732 } else {
991 733 $api_params['item_name'] = rawurlencode( $this->plugin_name );
@@ -996,29 +738,30 @@
996 738 'timeout' => 25,
997 739 'user-agent' => $this->plugin_slug . '/' . $this->version . '; ' . get_bloginfo( 'url' ),
998 740 );
999 741
1000 - $resp = wp_remote_post(
742 + $resp = wp_remote_post(
1001 743 $this->store_url . '?l=' . urlencode( base64_encode( $this->license ) ),
1002 744 $arg_array
1003 745 );
1004 - $body = wp_remote_retrieve_body( $resp );
1005 - $this->save_status = array( 'response_code' => wp_remote_retrieve_response_code( $resp ) );
746 + $body = wp_remote_retrieve_body( $resp );
1006 747
1007 748 $message = __( 'Your License Key was invalid', 'formidable' );
1008 -
1009 749 if ( is_wp_error( $resp ) ) {
1010 750 $link = FrmAppHelper::admin_upgrade_link( 'api', 'knowledgebase/why-cant-i-activate-formidable-pro/' );
1011 751 /* translators: %1$s: Start link HTML, %2$s: End link HTML */
1012 - $message = sprintf( __( 'You had an error communicating with the Formidable API. %1$sClick here%2$s for more information.', 'formidable' ), '<a href="' . esc_url( $link ) . '" target="_blank">', '</a>' ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
752 + $message = sprintf( __( 'You had an error communicating with the Formidable API. %1$sClick here%2$s for more information.', 'formidable' ), '<a href="' . esc_url( $link ) . '" target="_blank">', '</a>' );
1013 753 $message .= ' ' . $resp->get_error_message();
1014 754 } elseif ( 'error' === $body || is_wp_error( $body ) ) {
1015 755 $message = __( 'You had an HTTP error connecting to the Formidable API', 'formidable' );
1016 756 } else {
1017 757 $json_res = json_decode( $body, true );
1018 -
1019 758 if ( null !== $json_res ) {
1020 - $message = is_array( $json_res ) && isset( $json_res['error'] ) ? $json_res['error'] : $json_res;
759 + if ( is_array( $json_res ) && isset( $json_res['error'] ) ) {
760 + $message = $json_res['error'];
761 + } else {
762 + $message = $json_res;
763 + }
1021 764 } elseif ( ! empty( $resp['response'] ) && ! empty( $resp['response']['code'] ) ) {
1022 765 $resp['body'] = wp_strip_all_tags( $resp['body'] );
1023 766
1024 767 $message = sprintf(
@@ -1032,11 +775,8 @@
1032 775
1033 776 return $message;
1034 777 }
1035 778
1036 - /**
1037 - * @return void
1038 - */
1039 779 public function manually_queue_update() {
1040 780 $updates = new stdClass();
1041 781 $updates->last_checked = 0;
1042 782 $updates->response = array();
@@ -1043,46 +783,6 @@
1043 783 $updates->translations = array();
1044 784 $updates->no_update = array();
1045 785 $updates->checked = array();
1046 786 set_site_transient( 'update_plugins', $updates );
1047 - }
1048 -
1049 - /**
1050 - * Set the transient key for the lock. It should be unique to the license.
1051 - *
1052 - * @since 6.8.3
1053 - *
1054 - * @return bool
1055 - */
1056 - protected function lock_key() {
1057 - return $this->transient_lock_key . '_' . $this->license;
1058 - }
1059 -
1060 - /**
1061 - * Prevent multiple requests from running at the same time.
1062 - *
1063 - * @since 6.8.3
1064 - *
1065 - * @return bool
1066 - */
1067 - protected function is_running() {
1068 - return get_transient( $this->lock_key() );
1069 - }
1070 -
1071 - /**
1072 - * @since 6.8.3
1073 - *
1074 - * @return void
1075 - */
1076 - protected function set_running() {
1077 - set_transient( $this->lock_key(), true, 2 * MINUTE_IN_SECONDS );
1078 - }
1079 -
1080 - /**
1081 - * @since 6.8.3
1082 - *
1083 - * @return void
1084 - */
1085 - protected function done_running() {
1086 - delete_transient( $this->lock_key() );
1087 787 }
1088 788 }