PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.18
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.18
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 -371 6.326.18 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
@@ -445,11 +332,8 @@
445 332 $api->reset_cached();
446 333
447 334 $api = new FrmSalesApi();
448 335 $api->reset_cached();
449 -
450 - $api = new FrmStyleApi();
451 - $api->reset_cached();
452 336 }
453 337
454 338 /**
455 339 * The Pro version includes the show_license_message function.
@@ -455,17 +339,12 @@
455 339 * The Pro version includes the show_license_message function.
456 340 * We need an extra check before we allow it to show a message.
457 341 *
458 342 * @since 3.04.03
459 - *
460 - * @param string $file File path of the plugin.
461 - * @param array $plugin Plugin data.
462 - *
463 - * @return void
464 343 */
465 344 public function maybe_show_license_message( $file, $plugin ) {
466 345 if ( $this->is_expired_addon || isset( $plugin['package'] ) ) {
467 - // Let's not show a ton of duplicate messages
346 + // let's not show a ton of duplicate messages
468 347 return;
469 348 }
470 349
471 350 $this->show_license_message( $file, $plugin );
@@ -470,30 +349,22 @@
470 349
471 350 $this->show_license_message( $file, $plugin );
472 351 }
473 352
474 - /**
475 - * @param string $file File path of the plugin.
476 - * @param array $plugin Plugin data.
477 - *
478 - * @return void
479 - */
480 353 public function show_license_message( $file, $plugin ) {
481 354 $message = '';
482 -
483 - 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 {
484 359 $api = new FrmFormApi( $this->license );
485 360 $errors = $api->error_for_license();
486 -
487 - if ( $errors ) {
361 + if ( ! empty( $errors ) ) {
488 362 $message = reset( $errors );
489 363 }
490 - } else {
491 - /* translators: %1$s: Plugin name, %2$s: Start link HTML, %3$s: end link HTML */
492 - $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
493 364 }
494 365
495 - if ( ! $message ) {
366 + if ( empty( $message ) ) {
496 367 return;
497 368 }
498 369
499 370 $wp_list_table = _get_list_table( 'WP_Plugins_List_Table' );
@@ -498,19 +369,14 @@
498 369
499 370 $wp_list_table = _get_list_table( 'WP_Plugins_List_Table' );
500 371 $id = sanitize_title( $plugin['Name'] ) . '-next';
501 372
502 - 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
503 - FrmAppHelper::kses_echo( $message, 'a' );
504 - 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>';
505 376 echo '</p></div></td></tr>';
506 377 }
507 378
508 - /**
509 - * @param mixed $transient
510 - *
511 - * @return mixed
512 - */
513 379 public function clear_expired_download( $transient ) {
514 380 if ( ! is_object( $transient ) ) {
515 381 return $transient;
516 382 }
@@ -522,9 +388,9 @@
522 388 }
523 389 } elseif ( isset( $transient->response ) && isset( $transient->response[ $this->plugin_folder ] ) ) {
524 390 $this->prepare_update_details( $transient->response[ $this->plugin_folder ] );
525 391
526 - // 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
527 393 if ( $transient->response[ $this->plugin_folder ] === false ) {
528 394 if ( ! $this->has_been_cleared() ) {
529 395 $this->cleared_plugins();
530 396 $this->manually_queue_update();
@@ -541,37 +407,29 @@
541 407 *
542 408 * @since 3.04.03
543 409 *
544 410 * @param object $transient The current plugin info saved for update.
545 - *
546 - * @return void
547 411 */
548 412 private function prepare_update_details( &$transient ) {
549 413 $version_info = $transient;
550 414 $has_beta_url = ! empty( $version_info->beta );
551 -
552 415 if ( $this->get_beta && ! $has_beta_url ) {
553 416 $version_info = (object) $this->get_api_info( $this->license );
554 417 }
555 418
556 - if ( empty( $version_info->new_version ) ) {
557 - return;
558 - }
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 );
559 426
560 - $this->clear_old_plugin_version( $version_info );
561 -
562 - if ( $version_info === false ) {
563 - // Was cleared with timeout.
564 - $transient = false;
565 -
566 - return;
427 + if ( version_compare( $version_info->new_version, $this->version, '>' ) ) {
428 + $transient = $version_info;
429 + }
430 + }
567 431 }
568 -
569 - $this->maybe_use_beta_url( $version_info );
570 -
571 - if ( version_compare( $version_info->new_version, $this->version, '>' ) ) {
572 - $transient = $version_info;
573 - }
574 432 }
575 433
576 434 /**
577 435 * Get the API info for this plugin
@@ -576,22 +434,18 @@
576 434 /**
577 435 * Get the API info for this plugin
578 436 *
579 437 * @since 3.04.03
580 - *
581 - * @param string $license The license key.
582 - *
583 - * @return array The API info for the plugin.
584 438 */
585 439 protected function get_api_info( $license ) {
586 440 $api = new FrmFormApi( $license );
587 441 $addon = $api->get_addon_for_license( $this );
588 442
589 - // 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
590 444 if ( isset( $addon['package'] ) ) {
591 445 $this->is_parent_licence = true;
592 446 } elseif ( isset( $addon['error'] ) ) {
593 - // 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
594 448 $this->is_parent_licence = true;
595 449 $this->is_expired_addon = true;
596 450 }
597 451
@@ -602,24 +456,17 @@
602 456 * Make sure transients don't stick around on sites that
603 457 * don't save the transient expiration
604 458 *
605 459 * @since 2.05.05
606 - *
607 - * @param object $version_info The version info for the plugin.
608 - *
609 - * @return void
610 460 */
611 461 private function clear_old_plugin_version( &$version_info ) {
612 462 $timeout = ! empty( $version_info->timeout ) ? $version_info->timeout : 0;
613 -
614 - if ( ! $timeout || time() <= $timeout ) {
615 - 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();
616 468 }
617 -
618 - // Cache is expired.
619 - $version_info = false;
620 - $api = new FrmFormApi( $this->license );
621 - $api->reset_cached();
622 469 }
623 470
624 471 /**
625 472 * The beta url is always included if the download has a beta.
@@ -625,31 +472,19 @@
625 472 * The beta url is always included if the download has a beta.
626 473 * Check if the beta should be downloaded.
627 474 *
628 475 * @since 3.04.03
629 - *
630 - * @param object $version_info The version info for the plugin.
631 - *
632 - * @return void
633 476 */
634 477 private function maybe_use_beta_url( &$version_info ) {
635 - if ( ! $this->get_beta || empty( $version_info->beta ) ) {
636 - 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 + }
637 484 }
638 -
639 - $version_info->new_version = $version_info->beta['version'];
640 - $version_info->package = $version_info->beta['package'];
641 -
642 - if ( ! empty( $version_info->plugin ) ) {
643 - $version_info->plugin = $version_info->beta['plugin'];
644 - }
645 485 }
646 486
647 - /**
648 - * @param object $transient
649 - *
650 - * @return bool
651 - */
652 487 private function is_current_version( $transient ) {
653 488 if ( empty( $transient->checked ) || ! isset( $transient->checked[ $this->plugin_folder ] ) ) {
654 489 return false;
655 490 }
@@ -654,14 +489,13 @@
654 489 return false;
655 490 }
656 491
657 492 $response = empty( $transient->response );
658 -
659 493 if ( $response ) {
660 494 return true;
661 495 }
662 496
663 - 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;
664 498 }
665 499
666 500 /**
667 501 * @return bool
@@ -670,20 +504,14 @@
670 504 $last_cleared = get_option( 'frm_last_cleared' );
671 505 return $last_cleared && $last_cleared > gmdate( 'Y-m-d H:i:s', strtotime( '-5 minutes' ) );
672 506 }
673 507
674 - /**
675 - * @return void
676 - */
677 508 private function cleared_plugins() {
678 509 update_option( 'frm_last_cleared', gmdate( 'Y-m-d H:i:s' ) );
679 510 }
680 511
681 - /**
682 - * @return void
683 - */
684 512 private function is_license_revoked() {
685 - 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
686 514 return;
687 515 }
688 516
689 517 if ( $this->get_defined_license() ) {
@@ -691,19 +519,13 @@
691 519 return;
692 520 }
693 521
694 522 // Only check weekly.
695 - if ( $this->checked_recently( '7 days' ) || $this->is_running() ) {
523 + if ( $this->checked_recently( '7 days', 'valid' ) || $this->is_running() ) {
696 524 return;
697 525 }
698 526
699 527 $response = $this->get_license_status();
700 -
701 - if ( ! empty( $this->save_status['response_code'] ) && 429 === $this->save_status['response_code'] ) {
702 - // If we got a rate limit response, don't clear the license.
703 - return;
704 - }
705 -
706 528 if ( 'revoked' === $response['status'] || 'blocked' === $response['status'] || 'disabled' === $response['status'] || 'missing' === $response['status'] ) {
707 529 $this->clear_license();
708 530 }
709 531 }
@@ -710,45 +532,30 @@
710 532
711 533 /**
712 534 * Has this been checked too recently?
713 535 *
714 - * @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'.
715 538 *
716 - * @return bool True if the last check time was recent.
539 + * @return bool
717 540 */
718 - private function checked_recently( $time ) {
541 + private function checked_recently( $time, $required_status = '' ) {
719 542 $last_checked = $this->last_checked();
720 -
721 - if ( ! $last_checked ) {
722 - return false;
723 - }
724 -
725 - $is_429 = isset( $last_checked['response_code'] ) && 429 === $last_checked['response_code'];
726 -
727 - // 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'];
728 544 if ( $is_429 ) {
729 - // If the time is 2 minutes, do not adjust it so people
730 - // can still try to activate again in 2 minutes (and not 5).
731 - switch ( $time ) {
732 - case '7 days':
733 - // 7 days is used to check for license revocation.
734 - $time = '30 minutes';
735 - break;
736 - case '1 day':
737 - // 1 day is used for defined license constants.
738 - // The response won't change often, so we can check less frequently.
739 - $time = '2 hours';
740 - break;
741 - }
545 + // If the last check was a a rate limit, we'll need to check again sooner.
546 + $time = '5 minutes';
547 + $required_status = '';
742 548 }
743 549
744 - if ( empty( $last_checked['time'] ) ) {
745 - // If we do not have time, data is really old.
746 - 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;
747 553 }
748 554
749 - $time_ago = gmdate( 'Y-m-d H:i:s', strtotime( '-' . $time ) );
750 - 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;
751 558 }
752 559
753 560 /**
754 561 * @since 6.8.3 Switched to an array to store extra response info.
@@ -755,29 +562,25 @@
755 562 *
756 563 * @return array
757 564 */
758 565 private function last_checked() {
759 - $last_checked = is_multisite() ? get_site_option( $this->transient_key() ) : get_option( $this->transient_key() );
760 -
566 + if ( is_multisite() ) {
567 + $last_checked = get_site_option( $this->transient_key() );
568 + } else {
569 + $last_checked = get_option( $this->transient_key() );
570 + }
761 571 if ( $last_checked && ! is_array( $last_checked ) ) {
762 572 // Get string into array for existing values.
763 573 $last_checked = array( 'time' => $last_checked );
764 574 }
765 -
766 - return $last_checked ? $last_checked : array();
575 + return $last_checked ? (array) $last_checked : array();
767 576 }
768 577
769 578 /**
770 - * @since 6.30 Added the $is_valid param.
771 - *
772 - * @param bool $is_valid
773 - *
774 579 * @return void
775 580 */
776 - private function update_last_checked( $is_valid ) {
777 - $this->save_response['time'] = gmdate( 'Y-m-d H:i:s' );
778 - $this->save_response['is_valid'] = $is_valid;
779 -
581 + private function update_last_checked() {
582 + $this->save_response['time'] = gmdate( 'Y-m-d H:i:s' );
780 583 if ( is_multisite() ) {
781 584 update_site_option( $this->transient_key(), $this->save_response );
782 585 } else {
783 586 update_option( $this->transient_key(), $this->save_response );
@@ -785,25 +588,19 @@
785 588 }
786 589
787 590 /**
788 591 * Use a new cache after the license is changed, or Formidable is updated.
789 - *
790 - * @return string
791 592 */
792 593 private function transient_key() {
793 594 return 'frm_' . md5( sanitize_key( $this->license . '_' . $this->plugin_slug ) );
794 595 }
795 596
796 - /**
797 - * @return void
798 - */
799 597 public static function activate() {
800 598 FrmAppHelper::permission_check( 'frm_change_settings' );
801 599 check_ajax_referer( 'frm_ajax', 'nonce' );
802 600
803 601 $license = stripslashes( FrmAppHelper::get_param( 'license', '', 'post', 'sanitize_text_field' ) );
804 -
805 - if ( ! $license ) {
602 + if ( empty( $license ) ) {
806 603 wp_send_json(
807 604 array(
808 605 'message' => __( 'Oops! You forgot to enter your license number.', 'formidable' ),
809 606 'success' => false,
@@ -820,13 +617,8 @@
820 617 }
821 618
822 619 /**
823 620 * @since 4.08
824 - *
825 - * @param string $license The license key.
826 - * @param string $plugin_slug The plugin slug.
827 - *
828 - * @return array The response from the license activation.
829 621 */
830 622 public static function activate_license_for_plugin( $license, $plugin_slug ) {
831 623 $this_plugin = self::get_addon( $plugin_slug );
832 624 return $this_plugin->activate_license( $license );
@@ -831,13 +623,8 @@
831 623 $this_plugin = self::get_addon( $plugin_slug );
832 624 return $this_plugin->activate_license( $license );
833 625 }
834 626
835 - /**
836 - * @param string $license The license key.
837 - *
838 - * @return array The response from the license activation.
839 - */
840 627 private function activate_license( $license ) {
841 628 $this->set_license( $license );
842 629 $this->license = $license;
843 630
@@ -846,15 +633,12 @@
846 633 $response = $this->get_license_status();
847 634 $response['message'] = '';
848 635 $response['success'] = false;
849 636
850 - $is_valid = false;
851 -
852 637 if ( $response['error'] ) {
853 638 $response['message'] = $response['status'];
854 639 } else {
855 640 $messages = $this->get_messages();
856 -
857 641 if ( is_string( $response['status'] ) && isset( $messages[ $response['status'] ] ) ) {
858 642 $response['message'] = $messages[ $response['status'] ];
859 643 } else {
860 644 $response['message'] = FrmAppHelper::kses( $response['status'], array( 'a' ) );
@@ -859,17 +643,17 @@
859 643 } else {
860 644 $response['message'] = FrmAppHelper::kses( $response['status'], array( 'a' ) );
861 645 }
862 646
647 + $is_valid = false;
863 648 if ( 'valid' === $response['status'] ) {
864 649 $is_valid = 'valid';
865 650 $response['success'] = true;
866 651 }
867 -
868 652 $this->maybe_set_active( $is_valid );
869 653 }
870 654
871 - $this->update_last_checked( (bool) $is_valid );
655 + $this->update_last_checked();
872 656
873 657 return $response;
874 658 }
875 659
@@ -891,11 +675,8 @@
891 675 )
892 676 );
893 677 }
894 678
895 - /**
896 - * @return array
897 - */
898 679 private function get_license_status() {
899 680 $this->set_running();
900 681
901 682 $response = array(
@@ -901,16 +682,14 @@
901 682 $response = array(
902 683 'status' => 'missing',
903 684 'error' => true,
904 685 );
686 + if ( empty( $this->license ) ) {
687 + $response['error'] = false;
905 688
906 - if ( ! $this->license ) {
907 - $response['error'] = false;
908 689 return $response;
909 690 }
910 691
911 - $is_valid = false;
912 -
913 692 try {
914 693 $response['error'] = false;
915 694 $license_data = $this->send_mothership_request( 'activate_license' );
916 695
@@ -918,9 +697,8 @@
918 697 if ( is_array( $license_data ) ) {
919 698 if ( ! empty( $license_data['license'] ) && in_array( $license_data['license'], array( 'valid', 'invalid' ), true ) ) {
920 699 $response['status'] = $license_data['license'];
921 700 $this->save_status['status'] = $license_data['license'];
922 - $is_valid = 'valid' === $license_data['license'];
923 701 }
924 702 } else {
925 703 $response['status'] = $license_data;
926 704 }
@@ -927,16 +705,13 @@
927 705 } catch ( Exception $e ) {
928 706 $response['status'] = $e->getMessage();
929 707 }
930 708
931 - $this->update_last_checked( $is_valid );
709 + $this->update_last_checked();
932 710 $this->done_running();
933 711 return $response;
934 712 }
935 713
936 - /**
937 - * @return array
938 - */
939 714 private function get_messages() {
940 715 return array(
941 716 'valid' => __( 'Your license has been activated. Enjoy!', 'formidable' ),
942 717 'invalid' => __( 'That license key is invalid', 'formidable' ),
@@ -949,10 +724,8 @@
949 724 }
950 725
951 726 /**
952 727 * @since 4.03
953 - *
954 - * @return void
955 728 */
956 729 public static function reset_cache() {
957 730 FrmAppHelper::permission_check( 'frm_change_settings' );
958 731 check_ajax_referer( 'frm_ajax', 'nonce' );
@@ -967,11 +740,8 @@
967 740
968 741 wp_send_json( $response );
969 742 }
970 743
971 - /**
972 - * @return void
973 - */
974 744 public static function deactivate() {
975 745 FrmAppHelper::permission_check( 'frm_change_settings' );
976 746 check_ajax_referer( 'frm_ajax', 'nonce' );
977 747
@@ -980,13 +750,11 @@
980 750 $response = array(
981 751 'success' => false,
982 752 'message' => '',
983 753 );
984 -
985 754 try {
986 755 // $license_data->license will be either "deactivated" or "failed"
987 756 $license_data = $this_plugin->send_mothership_request( 'deactivate_license' );
988 -
989 757 if ( is_array( $license_data ) && 'deactivated' === $license_data['license'] ) {
990 758 $response['success'] = true;
991 759 $response['message'] = __( 'That license was removed successfully', 'formidable' );
992 760 } else {
@@ -1003,21 +771,18 @@
1003 771 }
1004 772
1005 773 /**
1006 774 * @since 4.03
1007 - *
1008 - * @return FrmAddon
1009 775 */
1010 776 private static function set_license_from_post() {
1011 777 $plugin_slug = FrmAppHelper::get_param( 'plugin', '', 'post', 'sanitize_text_field' );
1012 778 $this_plugin = self::get_addon( $plugin_slug );
1013 - $this_plugin->license = $this_plugin->get_license();
779 + $license = $this_plugin->get_license();
780 + $this_plugin->license = $license;
1014 781 return $this_plugin;
1015 782 }
1016 783
1017 784 /**
1018 - * @param string $action
1019 - *
1020 785 * @return string
1021 786 */
1022 787 public function send_mothership_request( $action ) {
1023 788 $api_params = array(
@@ -1024,9 +789,8 @@
1024 789 'edd_action' => $action,
1025 790 'license' => $this->license,
1026 791 'url' => home_url(),
1027 792 );
1028 -
1029 793 if ( is_numeric( $this->download_id ) ) {
1030 794 $api_params['item_id'] = absint( $this->download_id );
1031 795 } else {
1032 796 $api_params['item_name'] = rawurlencode( $this->plugin_name );
@@ -1037,31 +801,31 @@
1037 801 'timeout' => 25,
1038 802 'user-agent' => $this->plugin_slug . '/' . $this->version . '; ' . get_bloginfo( 'url' ),
1039 803 );
1040 804
1041 - $resp = wp_remote_post(
805 + $resp = wp_remote_post(
1042 806 $this->store_url . '?l=' . urlencode( base64_encode( $this->license ) ),
1043 807 $arg_array
1044 808 );
1045 - $response_code = wp_remote_retrieve_response_code( $resp );
1046 - $body = wp_remote_retrieve_body( $resp );
1047 - $this->save_status = array( 'response_code' => $response_code );
1048 - $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 ) );
1049 811
1050 812 $message = __( 'Your License Key was invalid', 'formidable' );
1051 -
1052 813 if ( is_wp_error( $resp ) ) {
1053 814 $link = FrmAppHelper::admin_upgrade_link( 'api', 'knowledgebase/why-cant-i-activate-formidable-pro/' );
1054 815 /* translators: %1$s: Start link HTML, %2$s: End link HTML */
1055 - $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>' );
1056 817 $message .= ' ' . $resp->get_error_message();
1057 818 } elseif ( 'error' === $body || is_wp_error( $body ) ) {
1058 819 $message = __( 'You had an HTTP error connecting to the Formidable API', 'formidable' );
1059 820 } else {
1060 821 $json_res = json_decode( $body, true );
1061 -
1062 822 if ( null !== $json_res ) {
1063 - $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 + }
1064 828 } elseif ( ! empty( $resp['response'] ) && ! empty( $resp['response']['code'] ) ) {
1065 829 $resp['body'] = wp_strip_all_tags( $resp['body'] );
1066 830
1067 831 $message = sprintf(
@@ -1075,11 +839,8 @@
1075 839
1076 840 return $message;
1077 841 }
1078 842
1079 - /**
1080 - * @return void
1081 - */
1082 843 public function manually_queue_update() {
1083 844 $updates = new stdClass();
1084 845 $updates->last_checked = 0;
1085 846 $updates->response = array();