| @@ -9,8 +9,11 @@ | ||
| 9 | 9 | |
| 10 | 10 | namespace SRFM\Inc; |
| 11 | 11 | |
| 12 | 12 | use SRFM\Inc\Traits\Get_Instance; |
| 13 | +use WP_Error; | |
| 14 | +use WP_REST_Request; | |
| 15 | +use WP_REST_Response; | |
| 13 | 16 | |
| 14 | 17 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | 18 | exit; // Exit if accessed directly. |
| 16 | 19 | } |
| @@ -23,13 +26,102 @@ | ||
| 23 | 26 | class Onboarding { |
| 24 | 27 | use Get_Instance; |
| 25 | 28 | |
| 26 | 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 | + /** | |
| 27 | 39 | * Onboarding completion setting key. |
| 28 | 40 | * |
| 29 | 41 | * @var string |
| 30 | 42 | */ |
| 31 | 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 | + } | |
| 32 | 124 | |
| 33 | 125 | /** |
| 34 | 126 | * Set onboarding completion status. |
| 35 | 127 | * |