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