PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.25.1
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.25.1
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / controllers / FrmOnboardingWizardController.php

FrmOnboardingWizardController.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.25.1, at classes/controllers/FrmOnboardingWizardController.php

707 lines 19.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Onboarding Wizard Controller class.
4 *
5 * @package Formidable
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 die( 'You are not allowed to call this page directly.' );
10 }
11
12 /**
13 * Handles the Onboarding Wizard page in the admin area.
14 *
15 * @since 6.9
16 */
17 class FrmOnboardingWizardController {
18
19 /**
20 * The slug of the Onboarding Wizard page.
21 *
22 * @var string
23 */
24 const PAGE_SLUG = 'formidable-onboarding-wizard';
25
26 /**
27 * The script handle.
28 *
29 * @var string
30 */
31 const SCRIPT_HANDLE = 'frm-onboarding-wizard';
32
33 /**
34 * The required user capability to view the Onboarding Wizard page.
35 *
36 * @var string
37 */
38 const REQUIRED_CAPABILITY = 'frm_view_forms';
39
40 /**
41 * Transient name used for managing redirection to the Onboarding Wizard page.
42 *
43 * @var string
44 */
45 const TRANSIENT_NAME = 'frm_activation_redirect';
46
47 /**
48 * Transient value associated with the redirection to the Onboarding Wizard page.
49 * Used when activating a single plugin.
50 *
51 * @var string
52 */
53 const TRANSIENT_VALUE = 'formidable-welcome';
54
55 /**
56 * Transient value associated with the redirection to the Onboarding Wizard page.
57 * Used when activating multiple plugins at once.
58 *
59 * @var string
60 */
61 const TRANSIENT_MULTI_VALUE = 'formidable-welcome-multi';
62
63 /**
64 * Option name for storing the redirect status for the Onboarding Wizard page.
65 *
66 * @var string
67 */
68 const REDIRECT_STATUS_OPTION = 'frm_welcome_redirect';
69
70 /**
71 * Option name for tracking if the onboarding wizard was skipped.
72 *
73 * @var string
74 */
75 const ONBOARDING_SKIPPED_OPTION = 'frm_onboarding_skipped';
76
77 /**
78 * Defines the initial step for redirection within the application flow.
79 *
80 * @var string
81 */
82 const INITIAL_STEP = 'consent-tracking';
83
84 /**
85 * Option name to store usage data.
86 *
87 * @var string
88 */
89 const USAGE_DATA_OPTION = 'frm_onboarding_usage_data';
90
91 /**
92 * Holds the URL to access the Onboarding Wizard's page.
93 *
94 * @var string
95 */
96 private static $page_url = '';
97
98 /**
99 * Holds a list of add-ons available for installation.
100 *
101 * @var array
102 */
103 private static $available_addons = array();
104
105 /**
106 * Path to views.
107 *
108 * @var string
109 */
110 private static $view_path = '';
111
112 /**
113 * Upgrade URL.
114 *
115 * @var string
116 */
117 private static $upgrade_link = '';
118
119 /**
120 * Initialize hooks for template page only.
121 *
122 * @since 6.9
123 */
124 public static function load_admin_hooks() {
125 self::set_page_url();
126 add_action( 'admin_init', self::class . '::do_admin_redirects' );
127
128 if ( self::has_onboarding_been_skipped() ) {
129 add_filter( 'option_frm_inbox', self::class . '::add_wizard_to_floating_links' );
130 }
131
132 // Load page if admin page is Onboarding Wizard.
133 self::maybe_load_page();
134 }
135
136 /**
137 * Performs a safe redirect to the welcome screen when the plugin is activated.
138 * On single activation, we will redirect immediately.
139 * When activating multiple plugins, the redirect is delayed until a Formidable page is loaded.
140 *
141 * @return void
142 */
143 public static function do_admin_redirects() {
144 $current_page = FrmAppHelper::simple_get( 'page', 'sanitize_title' );
145
146 // Prevent endless loop.
147 if ( $current_page === self::PAGE_SLUG ) {
148 return;
149 }
150
151 // Only do this for single site installs.
152 if ( is_network_admin() ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
153 self::mark_onboarding_as_skipped();
154 return;
155 }
156
157 if ( self::has_onboarding_been_skipped() || FrmAppHelper::pro_is_connected() ) {
158 return;
159 }
160
161 $transient_value = get_transient( self::TRANSIENT_NAME );
162 if ( ! in_array( $transient_value, array( self::TRANSIENT_VALUE, self::TRANSIENT_MULTI_VALUE ), true ) ) {
163 return;
164 }
165
166 if ( isset( $_GET['activate-multi'] ) ) {
167 /**
168 * $_GET['activate-multi'] is set after activating multiple plugins.
169 * In this case, change the transient value so we know for future checks.
170 */
171 set_transient( self::TRANSIENT_NAME, self::TRANSIENT_MULTI_VALUE, 60 );
172 return;
173 }
174
175 if ( self::TRANSIENT_MULTI_VALUE === $transient_value && ! FrmAppHelper::is_formidable_admin() ) {
176 // For multi-activations we want to only redirect when a user loads a Formidable page.
177 return;
178 }
179
180 set_transient( self::TRANSIENT_NAME, 'no', 60 );
181
182 // Prevent redirect with every activation.
183 if ( self::has_already_redirected() ) {
184 return;
185 }
186
187 // Redirect to the onboarding wizard's initial step.
188 $page_url = add_query_arg( 'step', self::INITIAL_STEP, self::$page_url );
189 if ( wp_safe_redirect( esc_url_raw( $page_url ) ) ) {
190 exit;
191 }
192 }
193
194 /**
195 * Initializes the Onboarding Wizard setup if on its designated admin page.
196 *
197 * @since 6.9
198 *
199 * @return void
200 */
201 public static function maybe_load_page() {
202 if ( self::is_onboarding_wizard_page() ) {
203 // Dismiss the onboarding wizard message so it stops appearing after it is clicked.
204 $message = new FrmInbox();
205 $message->dismiss( 'onboarding_wizard' );
206
207 add_action( 'admin_menu', self::class . '::menu', 99 );
208 add_action( 'admin_init', self::class . '::assign_properties' );
209 add_action( 'admin_enqueue_scripts', self::class . '::enqueue_assets', 15 );
210 add_action( 'admin_head', self::class . '::remove_menu' );
211
212 add_filter( 'admin_body_class', self::class . '::add_admin_body_classes', 999 );
213 add_filter( 'frm_show_footer_links', '__return_false' );
214 }
215 }
216
217 /**
218 * Initializes class properties with essential values for operation.
219 *
220 * @since 6.9
221 *
222 * @return void
223 */
224 public static function assign_properties() {
225 self::$view_path = FrmAppHelper::plugin_path() . '/classes/views/onboarding-wizard/';
226
227 self::$upgrade_link = FrmAppHelper::admin_upgrade_link(
228 array(
229 'medium' => 'onboarding-wizard',
230 'content' => 'upgrade',
231 )
232 );
233
234 self::set_available_addons();
235 }
236
237 /**
238 * Add Onboarding Wizard menu item to sidebar and define index page.
239 *
240 * @since 6.9
241 *
242 * @return void
243 */
244 public static function menu() {
245 if ( ! current_user_can( 'activate_plugins' ) ) {
246 return;
247 }
248
249 $label = __( 'Onboarding Wizard', 'formidable' );
250
251 add_submenu_page(
252 'formidable',
253 'Formidable | ' . $label,
254 $label,
255 self::REQUIRED_CAPABILITY,
256 self::PAGE_SLUG,
257 array( self::class, 'render' )
258 );
259 }
260
261 /**
262 * Renders the Onboarding Wizard page in the WordPress admin area.
263 *
264 * @since 6.9
265 *
266 * @return void
267 */
268 public static function render() {
269 if ( self::has_onboarding_been_skipped() ) {
270 delete_option( self::ONBOARDING_SKIPPED_OPTION );
271 self::has_already_redirected();
272 }
273
274 // Include SVG images for icons.
275 FrmAppHelper::include_svg();
276
277 $view_path = self::get_view_path();
278 $available_addons = self::get_available_addons();
279 $upgrade_link = self::get_upgrade_link();
280 $addons_count = FrmAddonsController::get_addons_count();
281 $license_key = base64_decode( rawurldecode( FrmAppHelper::get_param( 'key', '', 'request', 'sanitize_text_field' ) ) );
282 $pro_is_installed = FrmAppHelper::pro_is_installed();
283
284 // Note: Add step parts in order.
285 $step_parts = array(
286 'consent-tracking' => 'steps/consent-tracking-step.php',
287 'install-addons' => 'steps/install-addons-step.php',
288 'success' => 'steps/success-step.php',
289 'unsuccessful' => 'steps/unsuccessful-step.php',
290 );
291
292 include $view_path . 'index.php';
293 }
294
295 /**
296 * Handle AJAX request to setup the "Never miss an important update" step.
297 *
298 * @since 6.9
299 *
300 * @return void
301 */
302 public static function ajax_consent_tracking() {
303 // Check permission and nonce.
304 FrmAppHelper::permission_check( self::REQUIRED_CAPABILITY );
305 check_ajax_referer( 'frm_ajax', 'nonce' );
306
307 // Update Settings.
308 $frm_settings = FrmAppHelper::get_settings();
309 $frm_settings->update_setting( 'tracking', true, 'rest_sanitize_boolean' );
310
311 // Remove the 'FrmProSettingsController::store' action to avoid PHP errors during AJAX call.
312 remove_action( 'frm_store_settings', 'FrmProSettingsController::store' );
313 $frm_settings->store();
314
315 FrmEmailCollectionHelper::subscribe_to_active_campaign();
316
317 // Send response.
318 wp_send_json_success();
319 }
320
321
322 /**
323 * Handle AJAX request to set up usage data for the Onboarding Wizard.
324 *
325 * @since 6.9
326 *
327 * @return void
328 */
329 public static function setup_usage_data() {
330 // Check permission and nonce.
331 FrmAppHelper::permission_check( self::REQUIRED_CAPABILITY );
332 check_ajax_referer( 'frm_ajax', 'nonce' );
333
334 // Retrieve the current usage data.
335 $usage_data = self::get_usage_data();
336
337 $fields_to_update = array(
338 'allows_tracking' => 'rest_sanitize_boolean',
339 'installed_addons' => 'sanitize_text_field',
340 'processed_steps' => 'sanitize_text_field',
341 'completed_steps' => 'rest_sanitize_boolean',
342 );
343
344 foreach ( $fields_to_update as $field => $sanitize_callback ) {
345 if ( isset( $_POST[ $field ] ) ) {
346 $usage_data[ $field ] = FrmAppHelper::get_post_param( $field, '', $sanitize_callback );
347 }
348 }
349
350 update_option( self::USAGE_DATA_OPTION, $usage_data );
351 wp_send_json_success();
352 }
353
354 /**
355 * Enqueues the Onboarding Wizard page scripts and styles.
356 *
357 * @since 6.9
358 *
359 * @return void
360 */
361 public static function enqueue_assets() {
362 $plugin_url = FrmAppHelper::plugin_url();
363 $version = FrmAppHelper::plugin_version();
364 $js_dependencies = array(
365 'wp-i18n',
366 // This prevents a console error "wp.hooks is undefined" in WP versions older than 5.7.
367 'wp-hooks',
368 'formidable_dom',
369 );
370
371 // Enqueue styles that needed.
372 wp_enqueue_style( 'formidable-admin' );
373 wp_enqueue_style( 'formidable-grids' );
374
375 // Register and enqueue Onboarding Wizard style.
376 wp_register_style( self::SCRIPT_HANDLE, $plugin_url . '/css/admin/onboarding-wizard.css', array(), $version );
377 wp_enqueue_style( self::SCRIPT_HANDLE );
378
379 // Register and enqueue Onboarding Wizard script.
380 wp_register_script( self::SCRIPT_HANDLE, $plugin_url . '/js/onboarding-wizard.js', $js_dependencies, $version, true );
381 wp_localize_script( self::SCRIPT_HANDLE, 'frmOnboardingWizardVars', self::get_js_variables() );
382 wp_enqueue_script( self::SCRIPT_HANDLE );
383
384 /**
385 * Fires after the Onboarding Wizard enqueue assets.
386 *
387 * @since 6.9
388 */
389 do_action( 'frm_onboarding_wizard_enqueue_assets' );
390
391 FrmAppHelper::dequeue_extra_global_scripts();
392 }
393
394 /**
395 * Get the Onboarding Wizard JS variables as an array.
396 *
397 * @since 6.9
398 *
399 * @return array
400 */
401 private static function get_js_variables() {
402 return array(
403 'INITIAL_STEP' => self::INITIAL_STEP,
404 );
405 }
406
407 /**
408 * Remove the Onboarding Wizard submenu page from the formidable parent menu
409 * since it is not necessary to show that link there.
410 *
411 * @since 6.9
412 *
413 * @return void
414 */
415 public static function remove_menu() {
416 remove_submenu_page( 'formidable', self::PAGE_SLUG );
417 }
418
419 /**
420 * Adds custom classes to the existing string of admin body classes.
421 *
422 * The function appends a custom class to the existing admin body classes, enabling full-screen mode for the admin interface.
423 *
424 * @since 6.9
425 *
426 * @param string $classes Existing body classes.
427 * @return string Updated list of body classes, including the newly added classes.
428 */
429 public static function add_admin_body_classes( $classes ) {
430 return $classes . ' frm-admin-full-screen';
431 }
432
433 /**
434 * Checks if the Onboarding Wizard was skipped during the plugin's installation.
435 *
436 * @since 6.9
437 * @return bool True if the Onboarding Wizard was skipped, false otherwise.
438 */
439 public static function has_onboarding_been_skipped() {
440 return get_option( self::ONBOARDING_SKIPPED_OPTION, false );
441 }
442
443 /**
444 * Marks the Onboarding Wizard as skipped to prevent automatic redirects to the wizard.
445 *
446 * @since 6.9
447 * @return void
448 */
449 public static function mark_onboarding_as_skipped() {
450 update_option( self::ONBOARDING_SKIPPED_OPTION, true, 'no' );
451 }
452
453 /**
454 * Adds an Onboarding Wizard welcome message to the floating notifications.
455 *
456 * @since 6.9
457 *
458 * @param array $inbox_messages The array of existing inbox messages.
459 * @return array Configuration for the onboarding wizard slide-in notification.
460 */
461 public static function add_wizard_to_floating_links( $inbox_messages ) {
462 $message = __( 'Welcome to Formidable Forms! Click here to run the Onboarding Wizard and it will guide you through the basic settings and get you started in 2 minutes.', 'formidable' );
463
464 return array(
465 'onboarding_wizard' => array(
466 'subject' => esc_html__( 'Begin With Ease!', 'formidable' ),
467 'message' => esc_html( $message ),
468 'slidein' => esc_html( $message ),
469 'cta' => '<a href="' . esc_url( self::$page_url ) . '" class="button-primary frm-button-primary" target="_blank" rel="noopener noreferrer">' . esc_html__( 'Begin Setup', 'formidable' ) . '</a>',
470 'created' => time(),
471 'key' => 'onboarding_wizard',
472 ),
473 );
474 }
475
476 /**
477 * Check if the current page is the Onboarding Wizard page.
478 *
479 * @since 6.9
480 *
481 * @return bool True if the current page is the Onboarding Wizard page, false otherwise.
482 */
483 public static function is_onboarding_wizard_page() {
484 return FrmAppHelper::is_admin_page( self::PAGE_SLUG );
485 }
486
487 /**
488 * Checks if the plugin has already performed a redirect to avoid repeated redirections.
489 *
490 * @return bool Returns true if already redirected, otherwise false.
491 */
492 private static function has_already_redirected() {
493 if ( get_option( self::REDIRECT_STATUS_OPTION ) ) {
494 return true;
495 }
496
497 update_option( self::REDIRECT_STATUS_OPTION, FrmAppHelper::plugin_version(), 'no' );
498 return false;
499 }
500
501 /**
502 * Get the path to the Onboarding Wizard views.
503 *
504 * @since 6.9
505 *
506 * @return string Path to views.
507 */
508 public static function get_page_url() {
509 return self::$page_url;
510 }
511
512 /**
513 * Set the URL to access the Onboarding Wizard's page.
514 *
515 * @return void
516 */
517 private static function set_page_url() {
518 self::$page_url = admin_url( 'admin.php?page=' . self::PAGE_SLUG );
519 }
520
521 /**
522 * Get the list of add-ons available for installation.
523 *
524 * @since 6.9
525 *
526 * @return array A list of add-ons.
527 */
528 public static function get_available_addons() {
529 return self::$available_addons;
530 }
531
532 /**
533 * Set the list of add-ons available for installation.
534 *
535 * @since 6.9
536 *
537 * @return void
538 */
539 private static function set_available_addons() {
540 $pro_is_installed = FrmAppHelper::pro_is_installed();
541 $plugins = get_plugins();
542
543 // Base add-ons always included.
544 self::$available_addons['spam-protection'] = array(
545 'title' => esc_html__( 'Spam Protection', 'formidable' ),
546 'is-checked' => true,
547 'is-disabled' => true,
548 'help-text' => esc_html__( 'Get anti-spam options like reCAPTCHA, hCaptcha, Akismet, Turnstile and the blocklist.', 'formidable' ),
549 );
550 self::$available_addons['stripe-payments'] = array(
551 'title' => esc_html__( 'Stripe Payments', 'formidable' ),
552 'is-checked' => true,
553 'is-disabled' => true,
554 'help-text' => esc_html__( 'Collect donations and payments with your forms. Offer physical products, digital goods, services, and more.', 'formidable' ),
555 );
556
557 // Add-ons included when Pro is not installed.
558 if ( ! $pro_is_installed ) {
559 self::$available_addons['visual-styler'] = array(
560 'title' => esc_html__( 'Visual Styler', 'formidable' ),
561 'is-checked' => true,
562 'is-disabled' => true,
563 'help-text' => esc_html__( 'Customize form appearance with an intuitive styling interface.', 'formidable' ),
564 );
565 self::$available_addons['save-entries'] = array(
566 'title' => esc_html__( 'Save Entries', 'formidable' ),
567 'is-checked' => true,
568 'is-disabled' => true,
569 'help-text' => esc_html__( 'Save form submissions to your database for future reference and analysis.', 'formidable' ),
570 );
571 }
572
573 // SMTP add-on if wp_mail_smtp is not installed.
574 if ( ! function_exists( 'wp_mail_smtp' ) ) {
575 $wp_mail_smtp_plugin = 'wp-mail-smtp/wp_mail_smtp.php';
576 $is_installed_wp_mail_smtp = array_key_exists( $wp_mail_smtp_plugin, $plugins );
577
578 self::$available_addons['wp-mail-smtp'] = array(
579 'title' => esc_html__( 'SMTP', 'formidable' ),
580 'rel' => $is_installed_wp_mail_smtp ? $wp_mail_smtp_plugin : 'wp-mail-smtp',
581 'is-checked' => false,
582 'is-vendor' => true,
583 'is-installed' => $is_installed_wp_mail_smtp,
584 'help-text' => esc_html__( 'Improve email deliverability by routing WordPress emails through SMTP.', 'formidable' ),
585 );
586 }
587
588 // Add-ons available when Pro is installed.
589 if ( $pro_is_installed ) {
590 $available_pro_addons = array(
591 'formidable-views' => array(
592 'addon_key' => 'views',
593 'title' => __( 'Views', 'formidable' ),
594 'plugin_file' => 'formidable-views/formidable-views.php',
595 ),
596 'formidable-mailchimp' => array(
597 'addon_key' => 'mailchimp',
598 'title' => __( 'Mailchimp', 'formidable' ),
599 'plugin_file' => 'formidable-mailchimp/formidable-mailchimp.php',
600 ),
601 'formidable-registration' => array(
602 'addon_key' => 'registration',
603 'title' => __( 'User Registration', 'formidable' ),
604 'plugin_file' => 'formidable-registration/formidable-registration.php',
605 ),
606 'formidable-api' => array(
607 'addon_key' => 'api',
608 'title' => __( 'Form Rest API', 'formidable' ),
609 'plugin_file' => 'formidable-api/formidable-api.php',
610 ),
611 'formidable-signature' => array(
612 'addon_key' => 'signature',
613 'title' => __( 'Signature Forms', 'formidable' ),
614 'plugin_file' => 'formidable-signature/signature.php',
615 ),
616 );
617
618 // Include ACF Forms add-on if ACF is installed.
619 if ( class_exists( 'ACF' ) ) {
620 $available_pro_addons['formidable-acf'] = array(
621 'addon_key' => 'acf',
622 'title' => __( 'ACF Forms', 'formidable' ),
623 'plugin_file' => 'formidable-acf/formidable-acf.php',
624 );
625 }
626
627 foreach ( $available_pro_addons as $key => $data ) {
628 $addon = FrmAddonsController::get_addon( $data['addon_key'] );
629 $plugin_file = $data['plugin_file'];
630
631 if ( ! is_plugin_active( $plugin_file ) && isset( $addon['url'] ) ) {
632 $is_installed = array_key_exists( $plugin_file, $plugins );
633
634 self::$available_addons[ $key ] = array(
635 'title' => $data['title'],
636 'rel' => $is_installed ? $plugin_file : $addon['url'],
637 'is-checked' => false,
638 'is-installed' => $is_installed,
639 'help-text' => $addon['excerpt'],
640 );
641 }
642 }
643 }//end if
644
645 // Gravity Forms Migrator add-on.
646 $gravity_forms_plugin = 'formidable-gravity-forms-importer/formidable-gravity-forms-importer.php';
647 if ( class_exists( 'GFForms' ) && ! is_plugin_active( $gravity_forms_plugin ) ) {
648 $is_installed_gravity_forms = array_key_exists( $gravity_forms_plugin, $plugins );
649
650 self::$available_addons['formidable-gravity-forms-importer'] = array(
651 'title' => esc_html__( 'Gravity Forms Migrator', 'formidable' ),
652 'rel' => $is_installed_gravity_forms ? $gravity_forms_plugin : 'formidable-gravity-forms-importer',
653 'is-checked' => false,
654 'is-vendor' => true,
655 'is-installed' => $is_installed_gravity_forms,
656 'help-text' => esc_html__( 'Easily migrate your forms from Gravity Forms to Formidable.', 'formidable' ),
657 );
658 }
659 }
660
661 /**
662 * Get the path to the Onboarding Wizard views.
663 *
664 * @since 6.9
665 *
666 * @return string Path to views.
667 */
668 public static function get_view_path() {
669 return self::$view_path;
670 }
671
672 /**
673 * Get the upgrade link.
674 *
675 * @since 6.9
676 *
677 * @return string URL for upgrading accounts.
678 */
679 public static function get_upgrade_link() {
680 return self::$upgrade_link;
681 }
682
683 /**
684 * Retrieves the current Onboarding Wizard usage data, returning an empty array if none exists.
685 *
686 * @since 6.9
687 *
688 * @return array Current usage data.
689 */
690 public static function get_usage_data() {
691 return get_option( self::USAGE_DATA_OPTION, array() );
692 }
693
694 /**
695 * Validates if the Onboarding Wizard page is being displayed.
696 *
697 * @since 6.9
698 * @deprecated 6.16
699 *
700 * @return bool True if the Onboarding Wizard page is displayed, false otherwise.
701 */
702 public static function is_onboarding_wizard_displayed() {
703 _deprecated_function( __METHOD__, '6.16' );
704 return get_transient( self::TRANSIENT_NAME ) === self::TRANSIENT_VALUE;
705 }
706 }
707