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