PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.26.1
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.26.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.26.1, at classes/controllers/FrmOnboardingWizardController.php

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