'GET', 'callback' => [__CLASS__, 'fetch_options'], 'permission_callback' => function () { return current_user_can('manage_options'); } ], [ 'methods' => 'POST', 'callback' => [__CLASS__, 'update_options'], 'permission_callback' => function () { return current_user_can('manage_options'); } ] ]); } public static function fetch_options($request) { $all_options = get_option('access_control_options', []); // Check if section parameter is provided $section = $request->get_param('section'); if ($section) { // If section is requested, return just that section $section_parts = explode('.', $section); $result = $all_options; // Navigate through the nested structure foreach ($section_parts as $part) { if (isset($result[$part])) { $result = $result[$part]; } else { // Section not found return new WP_Error( 'section_not_found', sprintf('Section %s not found in options', $section), ['status' => 404] ); } } // Return the requested section data // return $result; return new WP_REST_Response($result, 200); } // If no section parameter, return all options // return $all_options; return new WP_REST_Response($all_options, 200); } public static function update_options($request) { // Get current options from database $current_options = get_option('access_control_options', []); // Get options data from request and sanitize $updated_options = map_deep(wp_unslash($request->get_param('access_control_options')), 'sanitize_text_field'); if ($updated_options) { // Helper function to merge deeply nested arrays $deep_merge = function ($original, $update) use (&$deep_merge) { foreach ($update as $key => $value) { // If the value is an array and the key exists in original and is also an array if (is_array($value) && isset($original[$key]) && is_array($original[$key])) { $original[$key] = $deep_merge($original[$key], $value); } else { // Otherwise just assign the new value $original[$key] = $value; } } return $original; }; // Merge the updated options with current options $current_options = $deep_merge($current_options, $updated_options); // Save the updated options back to the database update_option('access_control_options', $current_options); $response = [ 'success' => true, 'msg' => esc_html__('Settings saved successfully.', 'access-control') ]; } else { $response = [ 'success' => false, 'msg' => esc_html__('No data received.', 'access-control') ]; } return new WP_REST_Response($response, 200); } }