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