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