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 +443 -96 6.27 → trunk View file →
@@ -107,14 +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 - if ( empty( $this->plugin_slug ) ) {
139 + if ( ! $this->plugin_slug ) {
113 140 $this->plugin_slug = preg_replace( '/[^a-zA-Z0-9_\s]/', '', str_replace( ' ', '_', strtolower( $this->plugin_name ) ) );
114 141 }
115 142
116 - if ( empty( $this->option_name ) ) {
143 + if ( ! $this->option_name ) {
117 144 $this->option_name = 'edd_' . $this->plugin_slug . '_license_';
118 145 }
119 146
120 147 $this->plugin_folder = plugin_basename( $this->plugin_file );
@@ -160,15 +187,27 @@
160 187 $license = $this->license;
161 188
162 189 add_action( 'after_plugin_row_' . plugin_basename( $this->plugin_file ), array( $this, 'maybe_show_license_message' ), 10, 2 );
163 190
164 - if ( $license ) {
165 - if ( 'formidable/formidable.php' !== $this->plugin_folder ) {
166 - add_filter( 'plugins_api', array( &$this, 'plugins_api_filter' ), 10, 3 );
167 - }
191 + $is_addon = 'formidable/formidable.php' !== $this->plugin_folder;
168 192
169 - add_filter( 'site_transient_update_plugins', array( &$this, 'clear_expired_download' ) );
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 );
170 199 }
200 +
201 + if ( ! $license ) {
202 + return;
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' ) );
171 210 }
172 211
173 212 /**
174 213 * Updates information on the "View version 6.15 details" page with custom data.
@@ -174,13 +213,13 @@
174 213 * Updates information on the "View version 6.15 details" page with custom data.
175 214 *
176 215 * @uses api_request()
177 216 *
178 - * @param mixed $_data
179 - * @param string $_action
180 - * @param object $_args
217 + * @param mixed $_data
218 + * @param string $_action
219 + * @param object|null $_args
181 220 *
182 - * @return object $_data
221 + * @return object Data.
183 222 */
184 223 public function plugins_api_filter( $_data, $_action = '', $_args = null ) {
185 224 if ( $_action !== 'plugin_information' ) {
186 225 return $_data;
@@ -185,18 +224,29 @@
185 224 if ( $_action !== 'plugin_information' ) {
186 225 return $_data;
187 226 }
188 227
189 - $slug = basename( $this->plugin_file, '.php' );
190 - $slug2 = str_replace( '/' . $slug . '.php', '', $this->plugin_folder );
191 -
192 - if ( empty( $_args->slug ) || ( $_args->slug !== $slug && $_args->slug !== $slug2 ) ) {
228 + if ( empty( $_args->slug ) || ! $this->is_plugin_slug( $_args->slug ) ) {
193 229 return $_data;
194 230 }
195 231
196 232 $item_id = $this->download_id;
197 233
198 - if ( ! $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 ) ) {
199 249 $_data = array(
200 250 'name' => $this->plugin_name,
201 251 'excerpt' => '',
202 252 'changelog' => 'See the full changelog at <a href="' . esc_url( $this->store_url . '/changelog/' ) . '"></a>',
@@ -204,12 +254,8 @@
204 254 'high' => '',
205 255 'low' => 'https://ps.w.org/formidable/assets/banner-1544x500.png',
206 256 ),
207 257 );
208 - } else {
209 - $api = new FrmFormApi( $this->license );
210 - $plugins = $api->get_api_info();
211 - $_data = $plugins[ $item_id ];
212 258 }
213 259
214 260 $_data['sections'] = array(
215 261 'description' => $_data['excerpt'],
@@ -221,10 +267,274 @@
221 267 return (object) $_data;
222 268 }
223 269
224 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 + *
225 500 * @return string
226 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 + */
227 537 public function get_license() {
228 538 $license = $this->maybe_get_pro_license();
229 539
230 540 if ( $license ) {
@@ -232,13 +542,9 @@
232 542 }
233 543
234 544 $license = trim( get_option( $this->option_name . 'key' ) );
235 545
236 - if ( ! $license ) {
237 - $license = $this->activate_defined_license();
238 - }
239 -
240 - return $license;
546 + return $license ? $license : $this->activate_defined_license();
241 547 }
242 548
243 549 /**
244 550 * @since 3.04.03
@@ -245,9 +551,9 @@
245 551 *
246 552 * @return false|string
247 553 */
248 554 protected function maybe_get_pro_license() {
249 - // prevent a loop if $this is the pro plugin
555 + // Prevent a loop if $this is the pro plugin
250 556 $get_license = FrmAppHelper::pro_is_installed() && is_callable( 'FrmProAppHelper::get_updater' ) && $this->plugin_name !== 'Formidable Pro';
251 557
252 558 if ( ! $get_license ) {
253 559 return false;
@@ -262,13 +568,9 @@
262 568 }
263 569
264 570 $this->get_api_info( $license );
265 571
266 - if ( ! $this->is_parent_licence ) {
267 - $license = false;
268 - }
269 -
270 - return $license;
572 + return $this->is_parent_licence ? $license : false;
271 573 }
272 574
273 575 /**
274 576 * Activate the license in wp-config.php
@@ -338,14 +640,16 @@
338 640 public function clear_license() {
339 641 delete_option( $this->option_name . 'active' );
340 642 delete_option( $this->option_name . 'key' );
341 643
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;
644 + if ( ! $this->should_clear_cache ) {
645 + return;
347 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;
348 652 }
349 653
350 654 /**
351 655 * Don't save an invalid license.
@@ -351,9 +655,9 @@
351 655 * Don't save an invalid license.
352 656 *
353 657 * @since 6.8.3
354 658 *
355 - * @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'.
356 660 *
357 661 * @return void
358 662 */
359 663 protected function maybe_set_active( $is_valid ) {
@@ -371,9 +675,9 @@
371 675 $this->license = '';
372 676 }
373 677
374 678 /**
375 - * @param bool $is_active
679 + * @param bool|string $is_active Value may include 'valid'.
376 680 *
377 681 * @return void
378 682 */
379 683 public function set_active( $is_active ) {
@@ -437,11 +741,17 @@
437 741
438 742 $api = new FrmApplicationApi( $this->license );
439 743 $api->reset_cached();
440 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 +
441 749 $api = new FrmSalesApi();
442 750 $api->reset_cached();
443 751
752 + FrmSalesApi::allow_new_sales_requests();
753 +
444 754 $api = new FrmStyleApi();
445 755 $api->reset_cached();
446 756 }
447 757
@@ -457,9 +767,9 @@
457 767 * @return void
458 768 */
459 769 public function maybe_show_license_message( $file, $plugin ) {
460 770 if ( $this->is_expired_addon || isset( $plugin['package'] ) ) {
461 - // let's not show a ton of duplicate messages
771 + // Let's not show a ton of duplicate messages
462 772 return;
463 773 }
464 774
465 775 $this->show_license_message( $file, $plugin );
@@ -473,12 +783,9 @@
473 783 */
474 784 public function show_license_message( $file, $plugin ) {
475 785 $message = '';
476 786
477 - if ( empty( $this->license ) ) {
478 - /* 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
480 - } else {
787 + if ( $this->license ) {
481 788 $api = new FrmFormApi( $this->license );
482 789 $errors = $api->error_for_license();
483 790
484 791 if ( $errors ) {
@@ -483,8 +790,11 @@
483 790
484 791 if ( $errors ) {
485 792 $message = reset( $errors );
486 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
487 797 }
488 798
489 799 if ( ! $message ) {
490 800 return;
@@ -516,9 +826,9 @@
516 826 }
517 827 } elseif ( isset( $transient->response ) && isset( $transient->response[ $this->plugin_folder ] ) ) {
518 828 $this->prepare_update_details( $transient->response[ $this->plugin_folder ] );
519 829
520 - // 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
521 831 if ( $transient->response[ $this->plugin_folder ] === false ) {
522 832 if ( ! $this->has_been_cleared() ) {
523 833 $this->cleared_plugins();
524 834 $this->manually_queue_update();
@@ -546,22 +856,26 @@
546 856 if ( $this->get_beta && ! $has_beta_url ) {
547 857 $version_info = (object) $this->get_api_info( $this->license );
548 858 }
549 859
550 - if ( ! empty( $version_info->new_version ) ) {
551 - $this->clear_old_plugin_version( $version_info );
860 + if ( empty( $version_info->new_version ) ) {
861 + return;
862 + }
552 863
553 - if ( $version_info === false ) {
554 - // Was cleared with timeout.
555 - $transient = false;
556 - } else {
557 - $this->maybe_use_beta_url( $version_info );
864 + $this->clear_old_plugin_version( $version_info );
558 865
559 - if ( version_compare( $version_info->new_version, $this->version, '>' ) ) {
560 - $transient = $version_info;
561 - }
562 - }
866 + if ( $version_info === false ) {
867 + // Was cleared with timeout.
868 + $transient = false;
869 +
870 + return;
563 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 + }
564 878 }
565 879
566 880 /**
567 881 * Get the API info for this plugin
@@ -575,13 +889,13 @@
575 889 protected function get_api_info( $license ) {
576 890 $api = new FrmFormApi( $license );
577 891 $addon = $api->get_addon_for_license( $this );
578 892
579 - // 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
580 894 if ( isset( $addon['package'] ) ) {
581 895 $this->is_parent_licence = true;
582 896 } elseif ( isset( $addon['error'] ) ) {
583 - // 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
584 898 $this->is_parent_licence = true;
585 899 $this->is_expired_addon = true;
586 900 }
587 901
@@ -600,14 +914,16 @@
600 914 */
601 915 private function clear_old_plugin_version( &$version_info ) {
602 916 $timeout = ! empty( $version_info->timeout ) ? $version_info->timeout : 0;
603 917
604 - if ( $timeout && time() > $timeout ) {
605 - // Cache is expired.
606 - $version_info = false;
607 - $api = new FrmFormApi( $this->license );
608 - $api->reset_cached();
918 + if ( ! $timeout || time() <= $timeout ) {
919 + return;
609 920 }
921 +
922 + // Cache is expired.
923 + $version_info = false;
924 + $api = new FrmFormApi( $this->license );
925 + $api->reset_cached();
610 926 }
611 927
612 928 /**
613 929 * The beta url is always included if the download has a beta.
@@ -619,15 +935,17 @@
619 935 *
620 936 * @return void
621 937 */
622 938 private function maybe_use_beta_url( &$version_info ) {
623 - if ( $this->get_beta && ! empty( $version_info->beta ) ) {
624 - $version_info->new_version = $version_info->beta['version'];
625 - $version_info->package = $version_info->beta['package'];
939 + if ( ! $this->get_beta || empty( $version_info->beta ) ) {
940 + return;
941 + }
626 942
627 - if ( ! empty( $version_info->plugin ) ) {
628 - $version_info->plugin = $version_info->beta['plugin'];
629 - }
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'];
630 948 }
631 949 }
632 950
633 951 /**
@@ -667,9 +985,9 @@
667 985 /**
668 986 * @return void
669 987 */
670 988 private function is_license_revoked() {
671 - 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
672 990 return;
673 991 }
674 992
675 993 if ( $this->get_defined_license() ) {
@@ -677,14 +995,19 @@
677 995 return;
678 996 }
679 997
680 998 // Only check weekly.
681 - if ( $this->checked_recently( '7 days', 'valid' ) || $this->is_running() ) {
999 + if ( $this->checked_recently( '7 days' ) || $this->is_running() ) {
682 1000 return;
683 1001 }
684 1002
685 1003 $response = $this->get_license_status();
686 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 +
687 1010 if ( 'revoked' === $response['status'] || 'blocked' === $response['status'] || 'disabled' === $response['status'] || 'missing' === $response['status'] ) {
688 1011 $this->clear_license();
689 1012 }
690 1013 }
@@ -691,31 +1014,45 @@
691 1014
692 1015 /**
693 1016 * Has this been checked too recently?
694 1017 *
695 - * @param string $time ie. '1 day'.
696 - * @param string $required_status Return false if the last check does not match. ie 'valid'.
1018 + * @param string $time ie. '1 day'.
697 1019 *
698 - * @return bool
1020 + * @return bool True if the last check time was recent.
699 1021 */
700 - private function checked_recently( $time, $required_status = '' ) {
1022 + private function checked_recently( $time ) {
701 1023 $last_checked = $this->last_checked();
702 - $is_429 = isset( $last_checked['response_code'] ) && 429 === $last_checked['response_code'];
703 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.
704 1032 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 = '';
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 + }
708 1046 }
709 1047
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;
1048 + if ( empty( $last_checked['time'] ) ) {
1049 + // If we do not have time, data is really old.
1050 + return false;
713 1051 }
714 1052
715 - $checked_time = $last_checked['time'] ?? false;
716 - $time_ago = gmdate( 'Y-m-d H:i:s', strtotime( '-' . $time ) );
717 - 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;
718 1055 }
719 1056
720 1057 /**
721 1058 * @since 6.8.3 Switched to an array to store extra response info.
@@ -733,12 +1070,17 @@
733 1070 return $last_checked ? $last_checked : array();
734 1071 }
735 1072
736 1073 /**
1074 + * @since 6.30 Added the $is_valid param.
1075 + *
1076 + * @param bool $is_valid
1077 + *
737 1078 * @return void
738 1079 */
739 - private function update_last_checked() {
740 - $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;
741 1083
742 1084 if ( is_multisite() ) {
743 1085 update_site_option( $this->transient_key(), $this->save_response );
744 1086 } else {
@@ -808,8 +1150,10 @@
808 1150 $response = $this->get_license_status();
809 1151 $response['message'] = '';
810 1152 $response['success'] = false;
811 1153
1154 + $is_valid = false;
1155 +
812 1156 if ( $response['error'] ) {
813 1157 $response['message'] = $response['status'];
814 1158 } else {
815 1159 $messages = $this->get_messages();
@@ -819,18 +1163,17 @@
819 1163 } else {
820 1164 $response['message'] = FrmAppHelper::kses( $response['status'], array( 'a' ) );
821 1165 }
822 1166
823 - $is_valid = false;
824 -
825 1167 if ( 'valid' === $response['status'] ) {
826 1168 $is_valid = 'valid';
827 1169 $response['success'] = true;
828 1170 }
1171 +
829 1172 $this->maybe_set_active( $is_valid );
830 1173 }
831 1174
832 - $this->update_last_checked();
1175 + $this->update_last_checked( (bool) $is_valid );
833 1176
834 1177 return $response;
835 1178 }
836 1179
@@ -863,13 +1206,15 @@
863 1206 'status' => 'missing',
864 1207 'error' => true,
865 1208 );
866 1209
867 - if ( empty( $this->license ) ) {
1210 + if ( ! $this->license ) {
868 1211 $response['error'] = false;
869 1212 return $response;
870 1213 }
871 1214
1215 + $is_valid = false;
1216 +
872 1217 try {
873 1218 $response['error'] = false;
874 1219 $license_data = $this->send_mothership_request( 'activate_license' );
875 1220
@@ -877,8 +1222,9 @@
877 1222 if ( is_array( $license_data ) ) {
878 1223 if ( ! empty( $license_data['license'] ) && in_array( $license_data['license'], array( 'valid', 'invalid' ), true ) ) {
879 1224 $response['status'] = $license_data['license'];
880 1225 $this->save_status['status'] = $license_data['license'];
1226 + $is_valid = 'valid' === $license_data['license'];
881 1227 }
882 1228 } else {
883 1229 $response['status'] = $license_data;
884 1230 }
@@ -885,9 +1231,9 @@
885 1231 } catch ( Exception $e ) {
886 1232 $response['status'] = $e->getMessage();
887 1233 }
888 1234
889 - $this->update_last_checked();
1235 + $this->update_last_checked( $is_valid );
890 1236 $this->done_running();
891 1237 return $response;
892 1238 }
893 1239
@@ -967,10 +1313,9 @@
967 1313 */
968 1314 private static function set_license_from_post() {
969 1315 $plugin_slug = FrmAppHelper::get_param( 'plugin', '', 'post', 'sanitize_text_field' );
970 1316 $this_plugin = self::get_addon( $plugin_slug );
971 - $license = $this_plugin->get_license();
972 - $this_plugin->license = $license;
1317 + $this_plugin->license = $this_plugin->get_license();
973 1318 return $this_plugin;
974 1319 }
975 1320
976 1321 /**
@@ -996,14 +1341,16 @@
996 1341 'timeout' => 25,
997 1342 'user-agent' => $this->plugin_slug . '/' . $this->version . '; ' . get_bloginfo( 'url' ),
998 1343 );
999 1344
1000 - $resp = wp_remote_post(
1345 + $resp = wp_remote_post(
1001 1346 $this->store_url . '?l=' . urlencode( base64_encode( $this->license ) ),
1002 1347 $arg_array
1003 1348 );
1004 - $body = wp_remote_retrieve_body( $resp );
1005 - $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;
1006 1353
1007 1354 $message = __( 'Your License Key was invalid', 'formidable' );
1008 1355
1009 1356 if ( is_wp_error( $resp ) ) {