\WP_REST_Server::CREATABLE, 'callback' => array( $this, 'hide' ), 'permission_callback' => array( $this, 'check_permissions_write' ), ) ); \register_rest_route( self::NAMESPACE, '/first-run/dismiss-business', array( 'methods' => \WP_REST_Server::CREATABLE, 'callback' => array( $this, 'dismiss_business' ), 'permission_callback' => array( $this, 'check_permissions_write' ), ) ); \register_rest_route( self::NAMESPACE, '/first-run/mark', array( 'methods' => \WP_REST_Server::CREATABLE, 'callback' => array( $this, 'mark' ), 'permission_callback' => array( $this, 'check_permissions_write' ), 'args' => array( 'event' => array( 'type' => 'string', 'required' => true, 'sanitize_callback' => static function ( $value ) { return is_string( $value ) ? sanitize_text_field( $value ) : ''; }, ), ), ) ); } /** * Handle POST aipt/v1/first-run/hide. * * @param \WP_REST_Request $request Incoming request. * @return \WP_REST_Response|\WP_Error */ public function hide( \WP_REST_Request $request ) { unset( $request ); if ( ! FirstRunState::hide( (int) get_current_user_id() ) ) { return new \WP_Error( 'aipt_first_run_no_user', \__( 'You must be logged in to do that.', 'ai-product-tools' ), array( 'status' => 401 ) ); } return new \WP_REST_Response( array( 'success' => true, 'data' => array( 'hidden' => true ), ), 200 ); } /** * Handle POST aipt/v1/first-run/dismiss-business. * * @param \WP_REST_Request $request Incoming request. * @return \WP_REST_Response|\WP_Error */ public function dismiss_business( \WP_REST_Request $request ) { unset( $request ); if ( ! FirstRunState::dismissBusiness( (int) get_current_user_id() ) ) { return new \WP_Error( 'aipt_first_run_no_user', \__( 'You must be logged in to do that.', 'ai-product-tools' ), array( 'status' => 401 ) ); } return new \WP_REST_Response( array( 'success' => true, 'data' => array( 'businessDismissed' => true ), ), 200 ); } /** * Handle POST aipt/v1/first-run/mark. * * @param \WP_REST_Request $request Incoming request. * @return \WP_REST_Response|\WP_Error */ public function mark( \WP_REST_Request $request ) { $event = (string) $request->get_param( 'event' ); if ( ! FunnelMilestones::isClientEvent( $event ) ) { return new \WP_Error( 'aipt_first_run_unknown_event', \__( 'Unknown first-run event.', 'ai-product-tools' ), array( 'status' => 400 ) ); } FunnelMilestones::record( $event ); $first_seen = FunnelMilestones::firstSeen( $event ); if ( null === $first_seen ) { return new \WP_Error( 'aipt_first_run_record_failed', \__( 'The event could not be saved.', 'ai-product-tools' ), array( 'status' => 500 ) ); } return new \WP_REST_Response( array( 'success' => true, 'data' => array( 'event' => $event, 'firstSeen' => $first_seen, ), ), 200 ); } /** * Permission callback for the write routes: manage_aipt_settings plus the * REST nonce for cookie-authenticated requests. * * @param \WP_REST_Request $request Incoming request. * @return bool|\WP_Error */ public function check_permissions_write( \WP_REST_Request $request ) { if ( ! current_user_can( 'manage_aipt_settings' ) ) { return new \WP_Error( 'rest_forbidden', \__( 'Sorry, you are not allowed to do that.', 'ai-product-tools' ), array( 'status' => 403 ) ); } return RestNonceGuard::check( $request, \__( 'Invalid security token. Please refresh and try again.', 'ai-product-tools' ), 'rest_forbidden_nonce' ); } }