PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 2.11.0
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v2.11.0
2.12.6 2.12.5 2.12.4 2.12.3 2.12.2 2.12.1 2.12.0 2.11.1 2.11.0 2.10.1 2.10.0 2.9.1 2.9.0 2.8.2 2.8.1 2.7.0 2.7.1 2.8.0 trunk 0.0.10 0.0.11 0.0.12 0.0.13 0.0.2 0.0.3 All 96 releases
sureforms / inc / onboarding.php
onboarding.php
146 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Onboarding Class
4 *
5 * Handles the onboarding process for the SureForms plugin.
6 *
7 * @package sureforms
8 */
9
10 namespace SRFM\Inc;
11
12 use SRFM\Inc\Traits\Get_Instance;
13 use WP_Error;
14 use WP_REST_Request;
15 use WP_REST_Response;
16
17 if ( ! defined( 'ABSPATH' ) ) {
18 exit; // Exit if accessed directly.
19 }
20
21 /**
22 * Onboarding Class
23 *
24 * Handles the onboarding process for the SureForms plugin.
25 */
26 class Onboarding {
27 use Get_Instance;
28
29 /**
30 * User-meta key recording that the user dismissed the migration
31 * banner on the Forms listing page. The banner stays gone until a
32 * fresh install or a manual delete of the meta.
33 *
34 * @since 2.11.0
35 */
36 public const MIGRATION_BANNER_DISMISSED_META_KEY = 'srfm_onboarding_migration_banner_dismissed';
37
38 /**
39 * Onboarding completion setting key.
40 *
41 * @var string
42 */
43 private $onboarding_status_key = 'onboarding_completed';
44
45 /**
46 * Constructor — wire the dismiss-migration-banner REST endpoint via
47 * the existing `srfm_rest_api_endpoints` filter. Keeps the route
48 * registration consistent with the migrator + rest-api modules.
49 *
50 * @since 2.11.0
51 */
52 public function __construct() {
53 add_filter( 'srfm_rest_api_endpoints', [ $this, 'register_routes' ] );
54 }
55
56 /**
57 * Append onboarding-related REST routes to the SureForms registry.
58 *
59 * @since 2.11.0
60 *
61 * @param array<string,array<string,mixed>> $endpoints Existing registry.
62 * @return array<string,array<string,mixed>>
63 */
64 public function register_routes( $endpoints ) {
65 if ( ! is_array( $endpoints ) ) {
66 $endpoints = [];
67 }
68 $endpoints['user-meta/dismiss-migration-banner'] = [
69 'methods' => 'POST',
70 'callback' => [ $this, 'rest_dismiss_migration_banner' ],
71 // Dismissing one's own banner is a per-user UX-preference write, so a
72 // logged-in check is the right scope (not the heavier manage_options
73 // the migrator endpoints need to create posts) — review #6.
74 'permission_callback' => static function () {
75 return is_user_logged_in();
76 },
77 ];
78 return $endpoints;
79 }
80
81 /**
82 * REST callback — persist the dismissal flag on the current user's
83 * meta so the migration banner stays gone across reloads.
84 *
85 * @since 2.11.0
86 *
87 * @param WP_REST_Request $request REST request.
88 * @return WP_REST_Response|WP_Error
89 */
90 public function rest_dismiss_migration_banner( $request ) {
91 // Verify the REST nonce, matching the migrator callbacks' convention
92 // (the JS sends X-WP-Nonce via apiFetch) — review #3.
93 $nonce = $request->get_header( 'X-WP-Nonce' );
94 if ( ! is_string( $nonce ) || ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
95 return new WP_Error(
96 'srfm_invalid_nonce',
97 __( 'Invalid or missing security token.', 'sureforms' ),
98 [ 'status' => 403 ]
99 );
100 }
101 // permission_callback (is_user_logged_in) guarantees a real user here.
102 update_user_meta( get_current_user_id(), self::MIGRATION_BANNER_DISMISSED_META_KEY, '1' );
103 return new WP_REST_Response(
104 [ 'dismissed' => true ],
105 200
106 );
107 }
108
109 /**
110 * Whether the current user has dismissed the migration banner.
111 * Surfaced to the admin React app via `srfm_admin.migration_banner_dismissed`.
112 *
113 * @since 2.11.0
114 *
115 * @return bool
116 */
117 public function is_migration_banner_dismissed() {
118 $user_id = get_current_user_id();
119 if ( 0 === $user_id ) {
120 return false;
121 }
122 return (bool) get_user_meta( $user_id, self::MIGRATION_BANNER_DISMISSED_META_KEY, true );
123 }
124
125 /**
126 * Set onboarding completion status.
127 *
128 * @since 1.9.1
129 * @param string $completed Whether the onboarding is completed.
130 * @return void
131 */
132 public function set_onboarding_status( $completed = 'no' ) {
133 Helper::update_srfm_option( $this->onboarding_status_key, $completed );
134 }
135
136 /**
137 * Get onboarding completion status.
138 *
139 * @since 1.9.1
140 * @return bool
141 */
142 public function get_onboarding_status() {
143 return Helper::get_srfm_option( $this->onboarding_status_key, 'no' ) === 'yes';
144 }
145 }
146