'GET', 'callback' => 'addifect_get_site_options', 'permission_callback' => 'addifect_can_manage_site_settings', ) ); // POST update site options. register_rest_route( $namespace, $endpoint, array( 'methods' => 'POST', 'callback' => 'addifect_update_site_options', 'permission_callback' => 'addifect_can_manage_site_settings', 'args' => array( 'site_options' => array( 'type' => 'object', 'required' => true, 'sanitize_callback' => 'addifect_recursive_sanitize_array', // Automatically sanitizes input. ), ), ) ); } ); /** * Get Addifect site options. * * @return WP_REST_Response */ function addifect_get_site_options() { $site_options = get_option( 'addifect_site_options' ); if ( false === $site_options ) { return new WP_Error( 'no_site_options', 'Site options not found', array( 'status' => 404 ) ); } // Fetch the consolidated assets. $assets = get_option( 'addifect_captured_assets', array( 'scripts' => array(), 'styles' => array(), ) ); return new WP_REST_Response( array( 'site_options' => $site_options, 'site_options_data' => array( // Send them back as separate keys. 'registered_styles' => $assets['styles'], 'registered_scripts' => $assets['scripts'], ), ), 200 ); } /** * Update Addifect site options. * * @param WP_REST_Request $request The REST request object. * @return WP_REST_Response */ function addifect_update_site_options( WP_REST_Request $request ) { // Input is already sanitized by the 'sanitize_callback' in register_rest_route. $new_options = $request->get_param( 'site_options' ); // update_option returns true on success, false on failure OR if value is unchanged. $update_status = update_option( 'addifect_site_options', $new_options ); return new WP_REST_Response( array(), 200 ); }