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