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