From b41a94f80473934599e0aa969a87e123b074a0b3 Mon Sep 17 00:00:00 2001 From: Altis Assistant Date: Wed, 13 May 2026 11:50:26 +0000 Subject: [PATCH] Add REST save for dashboard notification settings --- library/Falcon.php | 3 + library/Falcon/Manager.php | 117 ++++++++++++++++++++++++++++++++++++- 2 files changed, 117 insertions(+), 3 deletions(-) diff --git a/library/Falcon.php b/library/Falcon.php index c2f8476..c74e60c 100755 --- a/library/Falcon.php +++ b/library/Falcon.php @@ -12,6 +12,9 @@ public static function bootstrap() { if (is_admin()) { Falcon_Admin::bootstrap(); } + else { + Falcon_Manager::bootstrap(); + } try { // Check for a handler first diff --git a/library/Falcon/Manager.php b/library/Falcon/Manager.php index 6abcd71..aa4c765 100644 --- a/library/Falcon/Manager.php +++ b/library/Falcon/Manager.php @@ -122,12 +122,123 @@ public static function register_dashboard_widget( $widgets ) { wp_add_dashboard_widget( 'falcon_notification_settings', __( 'Notification Settings', 'falcon' ), array( get_class(), 'output_dashboard_widget' ) ); } + /** + * @wp-action rest_api_init + */ + public static function register_rest_routes() { + register_rest_route( 'falcon/v1', '/notification-settings', array( + 'methods' => 'POST', + 'callback' => array( get_class(), 'update_current_user_notification_settings' ), + 'permission_callback' => array( get_class(), 'can_update_current_user_notification_settings' ), + 'args' => array( + 'settings' => array( + 'required' => true, + 'type' => 'object', + ), + ), + ) ); + } + + public static function can_update_current_user_notification_settings() { + return is_user_logged_in() && current_user_can( 'read' ) && Falcon::is_enabled_for_site(); + } + + public static function update_current_user_notification_settings( WP_REST_Request $request ) { + $settings = $request->get_param( 'settings' ); + + if ( ! is_array( $settings ) ) { + return new WP_Error( + 'falcon_invalid_notification_settings', + __( 'Notification settings must be an object.', 'falcon' ), + array( 'status' => 400 ) + ); + } + + $args = array(); + foreach ( $settings as $key => $value ) { + if ( ! is_scalar( $value ) ) { + continue; + } + + $key = sanitize_text_field( wp_unslash( $key ) ); + $value = sanitize_text_field( wp_unslash( $value ) ); + + // Falcon_Connector::save_profile_settings() expects the same mangled + // field names PHP creates for normal form submissions. + $args[ str_replace( '.', '_', $key ) ] = $value; + } + + do_action( 'falcon.manager.save_profile_fields', get_current_user_id(), $args ); + + return rest_ensure_response( array( + 'success' => true, + ) ); + } + public static function output_dashboard_widget() { $user = wp_get_current_user(); ?> - - -
+
+ + +
+ +

+ + + +

+
+ +