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