PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / trunk
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More vtrunk
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 +461 -132 6.26 → trunk View file →
@@ -107,15 +107,41 @@
107 107 * @var bool
108 108 */
109 109 protected $should_clear_cache = true;
110 110
111 + /**
112 + * Cached for the request, since every add-on asks for the same values.
113 + *
114 + * @since 6.35
115 + *
116 + * @var array<string,string>|null
117 + */
118 + private static $lite_requirements;
119 +
120 + /**
121 + * Add-ons that require more than Lite does, keyed by plugin slug.
122 + *
123 + * Nearly every add-on is plain PHP with no dependency of its own, so it runs anywhere Lite
124 + * runs and Lite's own requirements describe it. The exceptions bundle a third party
125 + * library, which sets a floor of its own. PDFs bundles Dompdf, which needs PHP 7.1.
126 + *
127 + * Values here are floors, never ceilings, so raising Lite's minimum still raises every
128 + * add-on's. Keys match the names WordPress uses, requires and requires_php.
129 + *
130 + * @since 6.35
131 + *
132 + * @var array<string,array<string,string>>
133 + */
134 + private static $addon_requirements = array(
135 + 'pdfs' => array( 'requires_php' => '7.1' ),
136 + );
137 +
111 138 public function __construct() {
112 -
113 - if ( empty( $this->plugin_slug ) ) {
139 + if ( ! $this->plugin_slug ) {
114 140 $this->plugin_slug = preg_replace( '/[^a-zA-Z0-9_\s]/', '', str_replace( ' ', '_', strtolower( $this->plugin_name ) ) );
115 141 }
116 142
117 - if ( empty( $this->option_name ) ) {
143 + if ( ! $this->option_name ) {
118 144 $this->option_name = 'edd_' . $this->plugin_slug . '_license_';
119 145 }
120 146
121 147 $this->plugin_folder = plugin_basename( $this->plugin_file );
@@ -139,9 +165,8 @@
139 165 * @return array
140 166 */
141 167 public function insert_installed_addon( $plugins ) {
142 168 $plugins[ $this->plugin_slug ] = $this;
143 -
144 169 return $plugins;
145 170 }
146 171
147 172 /**
@@ -150,15 +175,9 @@
150 175 * @return false|FrmAddon
151 176 */
152 177 public static function get_addon( $plugin_slug ) {
153 178 $plugins = apply_filters( 'frm_installed_addons', array() );
154 - $plugin = false;
155 -
156 - if ( isset( $plugins[ $plugin_slug ] ) ) {
157 - $plugin = $plugins[ $plugin_slug ];
158 - }
159 -
160 - return $plugin;
179 + return $plugins[ $plugin_slug ] ?? false;
161 180 }
162 181
163 182 /**
164 183 * @return void
@@ -163,22 +182,32 @@
163 182 /**
164 183 * @return void
165 184 */
166 185 public function edd_plugin_updater() {
167 -
168 186 $this->is_license_revoked();
169 187 $license = $this->license;
170 188
171 189 add_action( 'after_plugin_row_' . plugin_basename( $this->plugin_file ), array( $this, 'maybe_show_license_message' ), 10, 2 );
172 190
173 - if ( ! empty( $license ) ) {
191 + $is_addon = 'formidable/formidable.php' !== $this->plugin_folder;
174 192
175 - if ( 'formidable/formidable.php' !== $this->plugin_folder ) {
176 - add_filter( 'plugins_api', array( &$this, 'plugins_api_filter' ), 10, 3 );
177 - }
193 + if ( $is_addon ) {
194 + // Lite gets its tested and required versions from wordpress.org, add-ons have no
195 + // such source. Added before the license check, since a site without a license
196 + // still needs to know what its add-ons support.
197 + add_filter( 'site_transient_update_plugins', array( $this, 'add_version_requirements' ), 20 );
198 + add_filter( 'plugins_api', array( $this, 'add_requirements_to_plugin_info' ), 20, 3 );
199 + }
178 200
179 - add_filter( 'site_transient_update_plugins', array( &$this, 'clear_expired_download' ) );
201 + if ( ! $license ) {
202 + return;
180 203 }
204 +
205 + if ( $is_addon ) {
206 + add_filter( 'plugins_api', array( &$this, 'plugins_api_filter' ), 10, 3 );
207 + }
208 +
209 + add_filter( 'site_transient_update_plugins', array( &$this, 'clear_expired_download' ) );
181 210 }
182 211
183 212 /**
184 213 * Updates information on the "View version 6.15 details" page with custom data.
@@ -184,30 +213,40 @@
184 213 * Updates information on the "View version 6.15 details" page with custom data.
185 214 *
186 215 * @uses api_request()
187 216 *
188 - * @param mixed $_data
189 - * @param string $_action
190 - * @param object $_args
217 + * @param mixed $_data
218 + * @param string $_action
219 + * @param object|null $_args
191 220 *
192 - * @return object $_data
221 + * @return object Data.
193 222 */
194 223 public function plugins_api_filter( $_data, $_action = '', $_args = null ) {
195 -
196 - if ( $_action != 'plugin_information' ) {
224 + if ( $_action !== 'plugin_information' ) {
197 225 return $_data;
198 226 }
199 227
200 - $slug = basename( $this->plugin_file, '.php' );
201 - $slug2 = str_replace( '/' . $slug . '.php', '', $this->plugin_folder );
202 -
203 - if ( empty( $_args->slug ) || ( $_args->slug != $slug && $_args->slug !== $slug2 ) ) {
228 + if ( empty( $_args->slug ) || ! $this->is_plugin_slug( $_args->slug ) ) {
204 229 return $_data;
205 230 }
206 231
207 232 $item_id = $this->download_id;
208 233
209 - if ( empty( $item_id ) ) {
234 + if ( $item_id ) {
235 + $api = new FrmFormApi( $this->license );
236 +
237 + // Force new API info so we can pull changelog data.
238 + // Change log data is intentionally omitted from the cached API response
239 + // To help reduce the size of the autoloaded option.
240 + $api->force_api_request();
241 + $plugins = $api->get_api_info();
242 +
243 + if ( $plugins ) {
244 + $_data = $plugins[ $item_id ];
245 + }
246 + }
247 +
248 + if ( empty( $plugins ) ) {
210 249 $_data = array(
211 250 'name' => $this->plugin_name,
212 251 'excerpt' => '',
213 252 'changelog' => 'See the full changelog at <a href="' . esc_url( $this->store_url . '/changelog/' ) . '"></a>',
@@ -215,12 +254,8 @@
215 254 'high' => '',
216 255 'low' => 'https://ps.w.org/formidable/assets/banner-1544x500.png',
217 256 ),
218 257 );
219 - } else {
220 - $api = new FrmFormApi( $this->license );
221 - $plugins = $api->get_api_info();
222 - $_data = $plugins[ $item_id ];
223 258 }
224 259
225 260 $_data['sections'] = array(
226 261 'description' => $_data['excerpt'],
@@ -232,24 +267,284 @@
232 267 return (object) $_data;
233 268 }
234 269
235 270 /**
271 + * Checks if a requested plugin_information slug refers to this plugin.
272 + *
273 + * Either the plugin file name or the plugin folder may be used as an add-on's slug.
274 + *
275 + * @since 6.35
276 + *
277 + * @param string $slug The requested slug.
278 + *
279 + * @return bool
280 + */
281 + private function is_plugin_slug( $slug ) {
282 + $file_slug = basename( $this->plugin_file, '.php' );
283 + $folder_slug = str_replace( '/' . $file_slug . '.php', '', $this->plugin_folder );
284 +
285 + return $slug === $file_slug || $slug === $folder_slug;
286 + }
287 +
288 + /**
289 + * Adds the versions this add-on supports to its update data.
290 + *
291 + * The API reports no tested or required versions, so anything reading the update transient
292 + * has no way to tell whether an add-on has been tried with the WordPress release in use, or
293 + * whether the site meets what the add-on needs.
294 + *
295 + * @since 6.35
296 + *
297 + * @param mixed $transient The update_plugins site transient.
298 + *
299 + * @return mixed
300 + */
301 + public function add_version_requirements( $transient ) {
302 + if ( ! is_object( $transient ) ) {
303 + return $transient;
304 + }
305 +
306 + $requirements = $this->get_version_requirements();
307 +
308 + if ( ! $requirements ) {
309 + return $transient;
310 + }
311 +
312 + foreach ( array( 'response', 'no_update' ) as $group ) {
313 + $plugins = $transient->$group ?? false;
314 +
315 + if ( ! is_array( $plugins ) || empty( $plugins[ $this->plugin_folder ] ) ) {
316 + continue;
317 + }
318 +
319 + // The entries are objects, so this updates the transient in place.
320 + $plugin = $plugins[ $this->plugin_folder ];
321 +
322 + if ( ! is_object( $plugin ) ) {
323 + continue;
324 + }
325 +
326 + foreach ( $requirements as $key => $value ) {
327 + if ( empty( $plugin->$key ) ) {
328 + $plugin->$key = $value;
329 + }
330 + }
331 + }
332 +
333 + return $transient;
334 + }
335 +
336 + /**
337 + * Adds the versions this add-on supports to its plugin information.
338 + *
339 + * This is where WordPress itself reads the values, for the "Compatible up to" and
340 + * "Requires PHP" lines and the compatibility warnings in the plugin details modal. It is
341 + * separate from plugins_api_filter because that filter is only added when a license is set.
342 + *
343 + * @since 6.35
344 + *
345 + * @param mixed $data Plugin information from an earlier filter, or false if none.
346 + * @param string $action The requested action.
347 + * @param object|null $args Arguments for the request, including the slug.
348 + *
349 + * @return mixed
350 + */
351 + public function add_requirements_to_plugin_info( $data, $action = '', $args = null ) {
352 + if ( 'plugin_information' !== $action || empty( $args->slug ) || ! $this->is_plugin_slug( $args->slug ) ) {
353 + return $data;
354 + }
355 +
356 + $requirements = $this->get_version_requirements();
357 +
358 + if ( ! $requirements ) {
359 + return $data;
360 + }
361 +
362 + $is_object = is_object( $data ) && ! is_wp_error( $data );
363 +
364 + if ( ! $is_object && ! is_array( $data ) ) {
365 + return $data;
366 + }
367 +
368 + foreach ( $requirements as $key => $value ) {
369 + if ( ! $is_object ) {
370 + if ( empty( $data[ $key ] ) ) {
371 + $data[ $key ] = $value;
372 + }
373 + continue;
374 + }
375 +
376 + if ( empty( $data->$key ) ) {
377 + $data->$key = $value;
378 + }
379 + }
380 +
381 + return $data;
382 + }
383 +
384 + /**
385 + * Gets the versions this add-on reports to WordPress.
386 + *
387 + * Lite's own values describe every add-on, apart from the few that bundle a library with a
388 + * higher floor of its own.
389 + *
390 + * @since 6.35
391 + *
392 + * @return array<string,string> Keyed by the names WordPress uses, tested, requires and requires_php.
393 + */
394 + public function get_version_requirements() {
395 + $requirements = self::get_lite_requirements();
396 + $addon = self::$addon_requirements[ $this->plugin_slug ] ?? array();
397 +
398 + foreach ( $addon as $key => $version ) {
399 + $is_higher = empty( $requirements[ $key ] ) || version_compare( $version, $requirements[ $key ], '>' );
400 +
401 + if ( $is_higher ) {
402 + $requirements[ $key ] = $version;
403 + }
404 + }
405 +
406 + /**
407 + * Filters the versions a single add-on reports to WordPress.
408 + *
409 + * @since 6.35
410 + *
411 + * @param array<string,string> $requirements Keyed by tested, requires and requires_php.
412 + * @param string $plugin_slug The add-on's slug, for example pdfs.
413 + */
414 + $requirements = apply_filters( 'frm_addon_version_requirements', $requirements, $this->plugin_slug );
415 +
416 + return is_array( $requirements ) ? array_filter( $requirements ) : array();
417 + }
418 +
419 + /**
420 + * Gets the WordPress version the add-ons report as tested.
421 + *
422 + * @since 6.35
423 + *
424 + * @return string The tested version, or an empty string if the readme has none.
425 + */
426 + public static function get_tested_wp_version() {
427 + return self::get_lite_requirements()['tested'];
428 + }
429 +
430 + /**
431 + * Gets the versions Lite declares, which the add-ons report as their own.
432 + *
433 + * Read from Lite's readme, which is the same place wordpress.org reads them from, so the
434 + * add-ons report what Lite reports without anything being added to the API.
435 + *
436 + * @since 6.35
437 + *
438 + * @return array<string,string> Keyed by tested, requires and requires_php. Values may be empty.
439 + */
440 + public static function get_lite_requirements() {
441 + if ( null !== self::$lite_requirements ) {
442 + return self::$lite_requirements;
443 + }
444 +
445 + $headers = array(
446 + 'tested' => 'Tested up to',
447 + 'requires' => 'Requires at least',
448 + 'requires_php' => 'Requires PHP',
449 + );
450 +
451 + $versions = array(
452 + 'tested' => '',
453 + 'requires' => '',
454 + 'requires_php' => '',
455 + );
456 +
457 + $readme = FrmAppHelper::plugin_path() . '/readme.txt';
458 +
459 + if ( is_readable( $readme ) ) {
460 + $data = get_file_data( $readme, $headers );
461 +
462 + foreach ( $versions as $key => $version ) {
463 + $versions[ $key ] = isset( $data[ $key ] ) ? trim( $data[ $key ] ) : '';
464 + }
465 + }
466 +
467 + /**
468 + * Filters the versions read from Lite's readme before the add-ons report them.
469 + *
470 + * @since 6.35
471 + *
472 + * @param array<string,string> $versions Keyed by tested, requires and requires_php.
473 + */
474 + $versions = apply_filters( 'frm_lite_version_requirements', $versions );
475 +
476 + if ( ! is_array( $versions ) ) {
477 + $versions = array();
478 + }
479 +
480 + $versions['tested'] = self::expand_tested_branch( $versions['tested'] ?? '' );
481 +
482 + self::$lite_requirements = $versions;
483 +
484 + return self::$lite_requirements;
485 + }
486 +
487 + /**
488 + * Expands a tested release branch to the release running on this site.
489 + *
490 + * A readme names a branch, like 7.0, while wordpress.org reports the newest release in
491 + * that branch, like 7.0.4. The difference matters because WordPress compares the tested
492 + * version to the current one exactly, so a site on 7.0.4 reading 7.0 is warned that the
493 + * plugin is untested when it is not. Only versions within the tested branch are expanded,
494 + * so a genuinely older tested version is still reported as older.
495 + *
496 + * @since 6.35
497 + *
498 + * @param string $tested The tested version from the readme.
499 + *
236 500 * @return string
237 501 */
502 + private static function expand_tested_branch( $tested ) {
503 + if ( ! $tested ) {
504 + return $tested;
505 + }
506 +
507 + // Drop any pre-release suffix, since 7.1-RC4 is not a release to report as tested.
508 + $current = preg_replace( '/[^0-9.].*$/', '', get_bloginfo( 'version' ) );
509 +
510 + if ( self::get_version_branch( $current ) !== self::get_version_branch( $tested ) ) {
511 + return $tested;
512 + }
513 +
514 + return version_compare( $current, $tested, '>' ) ? $current : $tested;
515 + }
516 +
517 + /**
518 + * Reduces a version to its major.minor release branch.
519 + *
520 + * @since 6.35
521 + *
522 + * @param string $version A WordPress version.
523 + *
524 + * @return string The branch, or an empty string if the version cannot be read.
525 + */
526 + private static function get_version_branch( $version ) {
527 + if ( ! preg_match( '/^(\d+)\.(\d+)/', $version, $matches ) ) {
528 + return '';
529 + }
530 +
531 + return $matches[1] . '.' . $matches[2];
532 + }
533 +
534 + /**
535 + * @return string
536 + */
238 537 public function get_license() {
239 538 $license = $this->maybe_get_pro_license();
240 539
241 - if ( ! empty( $license ) ) {
540 + if ( $license ) {
242 541 return $license;
243 542 }
244 543
245 544 $license = trim( get_option( $this->option_name . 'key' ) );
246 545
247 - if ( empty( $license ) ) {
248 - $license = $this->activate_defined_license();
249 - }
250 -
251 - return $license;
546 + return $license ? $license : $this->activate_defined_license();
252 547 }
253 548
254 549 /**
255 550 * @since 3.04.03
@@ -256,10 +551,10 @@
256 551 *
257 552 * @return false|string
258 553 */
259 554 protected function maybe_get_pro_license() {
260 - // prevent a loop if $this is the pro plugin
261 - $get_license = FrmAppHelper::pro_is_installed() && is_callable( 'FrmProAppHelper::get_updater' ) && $this->plugin_name != 'Formidable Pro';
555 + // Prevent a loop if $this is the pro plugin
556 + $get_license = FrmAppHelper::pro_is_installed() && is_callable( 'FrmProAppHelper::get_updater' ) && $this->plugin_name !== 'Formidable Pro';
262 557
263 558 if ( ! $get_license ) {
264 559 return false;
265 560 }
@@ -267,19 +562,15 @@
267 562 $api = new FrmFormApi();
268 563 $api->get_pro_updater();
269 564 $license = $api->get_license();
270 565
271 - if ( empty( $license ) ) {
566 + if ( ! $license ) {
272 567 return false;
273 568 }
274 569
275 570 $this->get_api_info( $license );
276 571
277 - if ( ! $this->is_parent_licence ) {
278 - $license = false;
279 - }
280 -
281 - return $license;
572 + return $this->is_parent_licence ? $license : false;
282 573 }
283 574
284 575 /**
285 576 * Activate the license in wp-config.php
@@ -290,9 +581,9 @@
290 581 */
291 582 public function activate_defined_license() {
292 583 $license = $this->get_defined_license();
293 584
294 - if ( ! empty( $license ) && ! $this->is_active() && ! $this->checked_recently( '1 day' ) ) {
585 + if ( $license && ! $this->is_active() && ! $this->checked_recently( '1 day' ) ) {
295 586 $response = $this->activate_license( $license );
296 587
297 588 if ( ! $response['success'] ) {
298 589 $license = '';
@@ -310,9 +601,8 @@
310 601 * @return false|string
311 602 */
312 603 public function get_defined_license() {
313 604 $constant_name = 'FRM_' . strtoupper( $this->plugin_slug ) . '_LICENSE';
314 -
315 605 return defined( $constant_name ) ? constant( $constant_name ) : false;
316 606 }
317 607
318 608 /**
@@ -350,14 +640,16 @@
350 640 public function clear_license() {
351 641 delete_option( $this->option_name . 'active' );
352 642 delete_option( $this->option_name . 'key' );
353 643
354 - if ( $this->should_clear_cache ) {
355 - delete_site_option( $this->transient_key() );
356 - delete_option( $this->transient_key() );
357 - $this->delete_cache();
358 - $this->should_clear_cache = true;
644 + if ( ! $this->should_clear_cache ) {
645 + return;
359 646 }
647 +
648 + delete_site_option( $this->transient_key() );
649 + delete_option( $this->transient_key() );
650 + $this->delete_cache();
651 + $this->should_clear_cache = true;
360 652 }
361 653
362 654 /**
363 655 * Don't save an invalid license.
@@ -363,9 +655,9 @@
363 655 * Don't save an invalid license.
364 656 *
365 657 * @since 6.8.3
366 658 *
367 - * @param bool $is_valid If license activation was successful.
659 + * @param bool|string $is_valid If license activation was successful. May be a string 'valid'.
368 660 *
369 661 * @return void
370 662 */
371 663 protected function maybe_set_active( $is_valid ) {
@@ -383,9 +675,9 @@
383 675 $this->license = '';
384 676 }
385 677
386 678 /**
387 - * @param bool $is_active
679 + * @param bool|string $is_active Value may include 'valid'.
388 680 *
389 681 * @return void
390 682 */
391 683 public function set_active( $is_active ) {
@@ -420,9 +712,9 @@
420 712 array_push( $cap_roles, 'administrator' );
421 713 }
422 714
423 715 foreach ( $roles as $role => $details ) {
424 - if ( in_array( $role, $cap_roles ) ) {
716 + if ( in_array( $role, $cap_roles, true ) ) {
425 717 $wp_roles->add_cap( $role, $cap );
426 718 } else {
427 719 $wp_roles->remove_cap( $role, $cap );
428 720 }
@@ -449,10 +741,19 @@
449 741
450 742 $api = new FrmApplicationApi( $this->license );
451 743 $api->reset_cached();
452 744
745 + // The Sales API tries to pull API data in the constructor.
746 + // But we do not need sales data here.
747 + FrmSalesApi::prevent_new_sales_requests();
748 +
453 749 $api = new FrmSalesApi();
454 750 $api->reset_cached();
751 +
752 + FrmSalesApi::allow_new_sales_requests();
753 +
754 + $api = new FrmStyleApi();
755 + $api->reset_cached();
455 756 }
456 757
457 758 /**
458 759 * The Pro version includes the show_license_message function.
@@ -466,9 +767,9 @@
466 767 * @return void
467 768 */
468 769 public function maybe_show_license_message( $file, $plugin ) {
469 770 if ( $this->is_expired_addon || isset( $plugin['package'] ) ) {
470 - // let's not show a ton of duplicate messages
771 + // Let's not show a ton of duplicate messages
471 772 return;
472 773 }
473 774
474 775 $this->show_license_message( $file, $plugin );
@@ -482,21 +783,21 @@
482 783 */
483 784 public function show_license_message( $file, $plugin ) {
484 785 $message = '';
485 786
486 - if ( empty( $this->license ) ) {
487 - /* translators: %1$s: Plugin name, %2$s: Start link HTML, %3$s: end link HTML */
488 - $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>' );
489 - } else {
787 + if ( $this->license ) {
490 788 $api = new FrmFormApi( $this->license );
491 789 $errors = $api->error_for_license();
492 790
493 - if ( ! empty( $errors ) ) {
791 + if ( $errors ) {
494 792 $message = reset( $errors );
495 793 }
794 + } else {
795 + /* translators: %1$s: Plugin name, %2$s: Start link HTML, %3$s: end link HTML */
796 + $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
496 797 }
497 798
498 - if ( empty( $message ) ) {
799 + if ( ! $message ) {
499 800 return;
500 801 }
501 802
502 803 $wp_list_table = _get_list_table( 'WP_Plugins_List_Table' );
@@ -501,11 +802,11 @@
501 802
502 803 $wp_list_table = _get_list_table( 'WP_Plugins_List_Table' );
503 804 $id = sanitize_title( $plugin['Name'] ) . '-next';
504 805
505 - 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>';
806 + 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
506 807 FrmAppHelper::kses_echo( $message, 'a' );
507 - echo '<script type="text/javascript">var d = document.getElementById("' . esc_attr( $id ) . '").previousSibling;if ( d !== null ){ d.className = d.className + " update"; }</script>';
808 + 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
508 809 echo '</p></div></td></tr>';
509 810 }
510 811
511 812 /**
@@ -525,9 +826,9 @@
525 826 }
526 827 } elseif ( isset( $transient->response ) && isset( $transient->response[ $this->plugin_folder ] ) ) {
527 828 $this->prepare_update_details( $transient->response[ $this->plugin_folder ] );
528 829
529 - // if the transient has expired, clear the update and trigger it again
830 + // If the transient has expired, clear the update and trigger it again
530 831 if ( $transient->response[ $this->plugin_folder ] === false ) {
531 832 if ( ! $this->has_been_cleared() ) {
532 833 $this->cleared_plugins();
533 834 $this->manually_queue_update();
@@ -555,22 +856,26 @@
555 856 if ( $this->get_beta && ! $has_beta_url ) {
556 857 $version_info = (object) $this->get_api_info( $this->license );
557 858 }
558 859
559 - if ( ! empty( $version_info->new_version ) ) {
560 - $this->clear_old_plugin_version( $version_info );
860 + if ( empty( $version_info->new_version ) ) {
861 + return;
862 + }
561 863
562 - if ( $version_info === false ) {
563 - // Was cleared with timeout.
564 - $transient = false;
565 - } else {
566 - $this->maybe_use_beta_url( $version_info );
864 + $this->clear_old_plugin_version( $version_info );
567 865
568 - if ( version_compare( $version_info->new_version, $this->version, '>' ) ) {
569 - $transient = $version_info;
570 - }
571 - }
866 + if ( $version_info === false ) {
867 + // Was cleared with timeout.
868 + $transient = false;
869 +
870 + return;
572 871 }
872 +
873 + $this->maybe_use_beta_url( $version_info );
874 +
875 + if ( version_compare( $version_info->new_version, $this->version, '>' ) ) {
876 + $transient = $version_info;
877 + }
573 878 }
574 879
575 880 /**
576 881 * Get the API info for this plugin
@@ -584,13 +889,13 @@
584 889 protected function get_api_info( $license ) {
585 890 $api = new FrmFormApi( $license );
586 891 $addon = $api->get_addon_for_license( $this );
587 892
588 - // if there is no download url, this license does not apply to the addon
893 + // If there is no download url, this license does not apply to the addon
589 894 if ( isset( $addon['package'] ) ) {
590 895 $this->is_parent_licence = true;
591 896 } elseif ( isset( $addon['error'] ) ) {
592 - // if the license is expired, we must assume all add-ons were packaged
897 + // If the license is expired, we must assume all add-ons were packaged
593 898 $this->is_parent_licence = true;
594 899 $this->is_expired_addon = true;
595 900 }
596 901
@@ -609,14 +914,16 @@
609 914 */
610 915 private function clear_old_plugin_version( &$version_info ) {
611 916 $timeout = ! empty( $version_info->timeout ) ? $version_info->timeout : 0;
612 917
613 - if ( ! empty( $timeout ) && time() > $timeout ) {
614 - // Cache is expired.
615 - $version_info = false;
616 - $api = new FrmFormApi( $this->license );
617 - $api->reset_cached();
918 + if ( ! $timeout || time() <= $timeout ) {
919 + return;
618 920 }
921 +
922 + // Cache is expired.
923 + $version_info = false;
924 + $api = new FrmFormApi( $this->license );
925 + $api->reset_cached();
619 926 }
620 927
621 928 /**
622 929 * The beta url is always included if the download has a beta.
@@ -628,15 +935,17 @@
628 935 *
629 936 * @return void
630 937 */
631 938 private function maybe_use_beta_url( &$version_info ) {
632 - if ( $this->get_beta && ! empty( $version_info->beta ) ) {
633 - $version_info->new_version = $version_info->beta['version'];
634 - $version_info->package = $version_info->beta['package'];
939 + if ( ! $this->get_beta || empty( $version_info->beta ) ) {
940 + return;
941 + }
635 942
636 - if ( ! empty( $version_info->plugin ) ) {
637 - $version_info->plugin = $version_info->beta['plugin'];
638 - }
943 + $version_info->new_version = $version_info->beta['version'];
944 + $version_info->package = $version_info->beta['package'];
945 +
946 + if ( ! empty( $version_info->plugin ) ) {
947 + $version_info->plugin = $version_info->beta['plugin'];
639 948 }
640 949 }
641 950
642 951 /**
@@ -654,9 +963,9 @@
654 963 if ( $response ) {
655 964 return true;
656 965 }
657 966
658 - return isset( $transient->response ) && isset( $transient->response[ $this->plugin_folder ] ) && $transient->checked[ $this->plugin_folder ] === $transient->response[ $this->plugin_folder ]->new_version;
967 + 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
659 968 }
660 969
661 970 /**
662 971 * @return bool
@@ -676,9 +985,9 @@
676 985 /**
677 986 * @return void
678 987 */
679 988 private function is_license_revoked() {
680 - if ( empty( $this->license ) || empty( $this->plugin_slug ) || isset( $_POST['license'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
989 + if ( ! $this->license || ! $this->plugin_slug || isset( $_POST['license'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
681 990 return;
682 991 }
683 992
684 993 if ( $this->get_defined_license() ) {
@@ -686,14 +995,19 @@
686 995 return;
687 996 }
688 997
689 998 // Only check weekly.
690 - if ( $this->checked_recently( '7 days', 'valid' ) || $this->is_running() ) {
999 + if ( $this->checked_recently( '7 days' ) || $this->is_running() ) {
691 1000 return;
692 1001 }
693 1002
694 1003 $response = $this->get_license_status();
695 1004
1005 + if ( ! empty( $this->save_status['response_code'] ) && 429 === $this->save_status['response_code'] ) {
1006 + // If we got a rate limit response, don't clear the license.
1007 + return;
1008 + }
1009 +
696 1010 if ( 'revoked' === $response['status'] || 'blocked' === $response['status'] || 'disabled' === $response['status'] || 'missing' === $response['status'] ) {
697 1011 $this->clear_license();
698 1012 }
699 1013 }
@@ -700,31 +1014,45 @@
700 1014
701 1015 /**
702 1016 * Has this been checked too recently?
703 1017 *
704 - * @param string $time ie. '1 day'.
705 - * @param string $required_status Return false if the last check does not match. ie 'valid'.
1018 + * @param string $time ie. '1 day'.
706 1019 *
707 - * @return bool
1020 + * @return bool True if the last check time was recent.
708 1021 */
709 - private function checked_recently( $time, $required_status = '' ) {
1022 + private function checked_recently( $time ) {
710 1023 $last_checked = $this->last_checked();
711 - $is_429 = isset( $last_checked['response_code'] ) && 429 === $last_checked['response_code'];
712 1024
1025 + if ( ! $last_checked ) {
1026 + return false;
1027 + }
1028 +
1029 + $is_429 = isset( $last_checked['response_code'] ) && 429 === $last_checked['response_code'];
1030 +
1031 + // If the last check was a a rate limit error, adjust the check time.
713 1032 if ( $is_429 ) {
714 - // If the last check was a a rate limit, we'll need to check again sooner.
715 - $time = '5 minutes';
716 - $required_status = '';
1033 + // If the time is 2 minutes, do not adjust it so people
1034 + // can still try to activate again in 2 minutes (and not 5).
1035 + switch ( $time ) {
1036 + case '7 days':
1037 + // 7 days is used to check for license revocation.
1038 + $time = '30 minutes';
1039 + break;
1040 + case '1 day':
1041 + // 1 day is used for defined license constants.
1042 + // The response won't change often, so we can check less frequently.
1043 + $time = '2 hours';
1044 + break;
1045 + }
717 1046 }
718 1047
719 - if ( $required_status && ( ! isset( $last_checked['status'] ) || $last_checked['status'] !== $required_status ) ) {
720 - // If the last check was invalid, we don't need to check again.
721 - return true;
1048 + if ( empty( $last_checked['time'] ) ) {
1049 + // If we do not have time, data is really old.
1050 + return false;
722 1051 }
723 1052
724 - $checked_time = $last_checked['time'] ?? false;
725 - $time_ago = gmdate( 'Y-m-d H:i:s', strtotime( '-' . $time ) );
726 - return $checked_time && $checked_time > $time_ago;
1053 + $time_ago = gmdate( 'Y-m-d H:i:s', strtotime( '-' . $time ) );
1054 + return $last_checked['time'] > $time_ago;
727 1055 }
728 1056
729 1057 /**
730 1058 * @since 6.8.3 Switched to an array to store extra response info.
@@ -731,13 +1059,9 @@
731 1059 *
732 1060 * @return array
733 1061 */
734 1062 private function last_checked() {
735 - if ( is_multisite() ) {
736 - $last_checked = get_site_option( $this->transient_key() );
737 - } else {
738 - $last_checked = get_option( $this->transient_key() );
739 - }
1063 + $last_checked = is_multisite() ? get_site_option( $this->transient_key() ) : get_option( $this->transient_key() );
740 1064
741 1065 if ( $last_checked && ! is_array( $last_checked ) ) {
742 1066 // Get string into array for existing values.
743 1067 $last_checked = array( 'time' => $last_checked );
@@ -746,12 +1070,17 @@
746 1070 return $last_checked ? $last_checked : array();
747 1071 }
748 1072
749 1073 /**
1074 + * @since 6.30 Added the $is_valid param.
1075 + *
1076 + * @param bool $is_valid
1077 + *
750 1078 * @return void
751 1079 */
752 - private function update_last_checked() {
753 - $this->save_response['time'] = gmdate( 'Y-m-d H:i:s' );
1080 + private function update_last_checked( $is_valid ) {
1081 + $this->save_response['time'] = gmdate( 'Y-m-d H:i:s' );
1082 + $this->save_response['is_valid'] = $is_valid;
754 1083
755 1084 if ( is_multisite() ) {
756 1085 update_site_option( $this->transient_key(), $this->save_response );
757 1086 } else {
@@ -776,9 +1105,9 @@
776 1105 check_ajax_referer( 'frm_ajax', 'nonce' );
777 1106
778 1107 $license = stripslashes( FrmAppHelper::get_param( 'license', '', 'post', 'sanitize_text_field' ) );
779 1108
780 - if ( empty( $license ) ) {
1109 + if ( ! $license ) {
781 1110 wp_send_json(
782 1111 array(
783 1112 'message' => __( 'Oops! You forgot to enter your license number.', 'formidable' ),
784 1113 'success' => false,
@@ -821,8 +1150,10 @@
821 1150 $response = $this->get_license_status();
822 1151 $response['message'] = '';
823 1152 $response['success'] = false;
824 1153
1154 + $is_valid = false;
1155 +
825 1156 if ( $response['error'] ) {
826 1157 $response['message'] = $response['status'];
827 1158 } else {
828 1159 $messages = $this->get_messages();
@@ -832,18 +1163,17 @@
832 1163 } else {
833 1164 $response['message'] = FrmAppHelper::kses( $response['status'], array( 'a' ) );
834 1165 }
835 1166
836 - $is_valid = false;
837 -
838 1167 if ( 'valid' === $response['status'] ) {
839 1168 $is_valid = 'valid';
840 1169 $response['success'] = true;
841 1170 }
1171 +
842 1172 $this->maybe_set_active( $is_valid );
843 1173 }
844 1174
845 - $this->update_last_checked();
1175 + $this->update_last_checked( (bool) $is_valid );
846 1176
847 1177 return $response;
848 1178 }
849 1179
@@ -876,14 +1206,15 @@
876 1206 'status' => 'missing',
877 1207 'error' => true,
878 1208 );
879 1209
880 - if ( empty( $this->license ) ) {
1210 + if ( ! $this->license ) {
881 1211 $response['error'] = false;
882 -
883 1212 return $response;
884 1213 }
885 1214
1215 + $is_valid = false;
1216 +
886 1217 try {
887 1218 $response['error'] = false;
888 1219 $license_data = $this->send_mothership_request( 'activate_license' );
889 1220
@@ -891,8 +1222,9 @@
891 1222 if ( is_array( $license_data ) ) {
892 1223 if ( ! empty( $license_data['license'] ) && in_array( $license_data['license'], array( 'valid', 'invalid' ), true ) ) {
893 1224 $response['status'] = $license_data['license'];
894 1225 $this->save_status['status'] = $license_data['license'];
1226 + $is_valid = 'valid' === $license_data['license'];
895 1227 }
896 1228 } else {
897 1229 $response['status'] = $license_data;
898 1230 }
@@ -899,9 +1231,9 @@
899 1231 } catch ( Exception $e ) {
900 1232 $response['status'] = $e->getMessage();
901 1233 }
902 1234
903 - $this->update_last_checked();
1235 + $this->update_last_checked( $is_valid );
904 1236 $this->done_running();
905 1237 return $response;
906 1238 }
907 1239
@@ -981,10 +1313,9 @@
981 1313 */
982 1314 private static function set_license_from_post() {
983 1315 $plugin_slug = FrmAppHelper::get_param( 'plugin', '', 'post', 'sanitize_text_field' );
984 1316 $this_plugin = self::get_addon( $plugin_slug );
985 - $license = $this_plugin->get_license();
986 - $this_plugin->license = $license;
1317 + $this_plugin->license = $this_plugin->get_license();
987 1318 return $this_plugin;
988 1319 }
989 1320
990 1321 /**
@@ -1010,14 +1341,16 @@
1010 1341 'timeout' => 25,
1011 1342 'user-agent' => $this->plugin_slug . '/' . $this->version . '; ' . get_bloginfo( 'url' ),
1012 1343 );
1013 1344
1014 - $resp = wp_remote_post(
1345 + $resp = wp_remote_post(
1015 1346 $this->store_url . '?l=' . urlencode( base64_encode( $this->license ) ),
1016 1347 $arg_array
1017 1348 );
1018 - $body = wp_remote_retrieve_body( $resp );
1019 - $this->save_status = array( 'response_code' => wp_remote_retrieve_response_code( $resp ) );
1349 + $response_code = wp_remote_retrieve_response_code( $resp );
1350 + $body = wp_remote_retrieve_body( $resp );
1351 + $this->save_status = array( 'response_code' => $response_code );
1352 + $this->save_response['response_code'] = $response_code;
1020 1353
1021 1354 $message = __( 'Your License Key was invalid', 'formidable' );
1022 1355
1023 1356 if ( is_wp_error( $resp ) ) {
@@ -1022,9 +1355,9 @@
1022 1355
1023 1356 if ( is_wp_error( $resp ) ) {
1024 1357 $link = FrmAppHelper::admin_upgrade_link( 'api', 'knowledgebase/why-cant-i-activate-formidable-pro/' );
1025 1358 /* translators: %1$s: Start link HTML, %2$s: End link HTML */
1026 - $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>' );
1359 + $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
1027 1360 $message .= ' ' . $resp->get_error_message();
1028 1361 } elseif ( 'error' === $body || is_wp_error( $body ) ) {
1029 1362 $message = __( 'You had an HTTP error connecting to the Formidable API', 'formidable' );
1030 1363 } else {
@@ -1030,13 +1363,9 @@
1030 1363 } else {
1031 1364 $json_res = json_decode( $body, true );
1032 1365
1033 1366 if ( null !== $json_res ) {
1034 - if ( is_array( $json_res ) && isset( $json_res['error'] ) ) {
1035 - $message = $json_res['error'];
1036 - } else {
1037 - $message = $json_res;
1038 - }
1367 + $message = is_array( $json_res ) && isset( $json_res['error'] ) ? $json_res['error'] : $json_res;
1039 1368 } elseif ( ! empty( $resp['response'] ) && ! empty( $resp['response']['code'] ) ) {
1040 1369 $resp['body'] = wp_strip_all_tags( $resp['body'] );
1041 1370
1042 1371 $message = sprintf(