get_route(); $method = $request->get_method(); foreach ( $mocks as $mock ) { if ( empty( $mock['enabled'] ) ) { continue; } $mock_route = isset( $mock['route'] ) ? $mock['route'] : ''; $mock_method = isset( $mock['method'] ) ? strtoupper( $mock['method'] ) : ''; // Match route (exact or pattern with *). $route_matches = false; if ( $mock_route === $route ) { $route_matches = true; } elseif ( str_contains( $mock_route, '*' ) ) { $pattern = '#^' . str_replace( '\*', '.*', preg_quote( $mock_route, '#' ) ) . '$#'; $route_matches = (bool) preg_match( $pattern, $route ); } if ( ! $route_matches ) { continue; } // Match method (empty = any). if ( ! empty( $mock_method ) && $mock_method !== $method ) { continue; } // Return mock response. $status = isset( $mock['status'] ) ? (int) $mock['status'] : 200; $body = isset( $mock['body'] ) ? $mock['body'] : ''; $headers = isset( $mock['headers'] ) && is_array( $mock['headers'] ) ? $mock['headers'] : array(); $json_body = json_decode( $body, true ); if ( json_last_error() === JSON_ERROR_NONE ) { $response_data = $json_body; } else { $response_data = $body; } $response = new \WP_REST_Response( $response_data, $status ); foreach ( $headers as $key => $value ) { $response->header( $key, $value ); } $response->header( 'X-Aradt-Mock', 'true' ); return $response; } return $result; } /** * AJAX: List all mock definitions. */ public static function ajax_list() { check_ajax_referer( 'aradt_nonce', 'nonce' ); Aradt_Utils::verify_ajax_request(); $mocks = get_option( 'aradt_mock_definitions', array() ); if ( ! is_array( $mocks ) ) { $mocks = array(); } wp_send_json_success( array( 'mocks' => $mocks ) ); } /** * AJAX: Save (create/update) a mock definition. */ public static function ajax_save() { check_ajax_referer( 'aradt_nonce', 'nonce' ); Aradt_Utils::verify_ajax_request(); $mock_id = isset( $_POST['mock_id'] ) ? sanitize_key( wp_unslash( (string) $_POST['mock_id'] ) ) : ''; $route = isset( $_POST['route'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['route'] ) ) : ''; $method = isset( $_POST['method'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['method'] ) ) : ''; $status = isset( $_POST['status'] ) ? absint( $_POST['status'] ) : 200; $body = isset( $_POST['body'] ) ? sanitize_textarea_field( wp_unslash( (string) $_POST['body'] ) ) : ''; $headers = isset( $_POST['headers'] ) ? Aradt_Utils::sanitize_recursive( Aradt_Utils::safe_json_decode( sanitize_textarea_field( wp_unslash( (string) $_POST['headers'] ) ) ) ) : array(); $name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['name'] ) ) : ''; if ( empty( $route ) ) { wp_send_json_error( array( 'message' => esc_html__( 'Route is required.', 'advanced-rest-api-devtools' ) ) ); } if ( ! is_array( $headers ) ) { $headers = array(); } $mocks = get_option( 'aradt_mock_definitions', array() ); if ( ! is_array( $mocks ) ) { $mocks = array(); } $mock_data = array( 'route' => $route, 'method' => strtoupper( $method ), 'status' => $status, 'body' => $body, 'headers' => $headers, 'name' => $name, 'enabled' => true, ); $is_new = true; if ( ! empty( $mock_id ) ) { foreach ( $mocks as &$mock ) { if ( isset( $mock['id'] ) && $mock['id'] === $mock_id ) { $mock = array_merge( $mock, $mock_data ); $is_new = false; break; } } unset( $mock ); } if ( $is_new ) { $mock_id = wp_generate_uuid4(); $mock_data['id'] = $mock_id; $mocks[] = $mock_data; } update_option( 'aradt_mock_definitions', $mocks ); wp_send_json_success( array( 'id' => $mock_id, 'message' => esc_html__( 'Mock saved.', 'advanced-rest-api-devtools' ), ) ); } /** * AJAX: Delete a mock definition. */ public static function ajax_delete() { check_ajax_referer( 'aradt_nonce', 'nonce' ); Aradt_Utils::verify_ajax_request(); $mock_id = sanitize_key( wp_unslash( $_POST['mock_id'] ?? '' ) ); if ( empty( $mock_id ) ) { wp_send_json_error( array( 'message' => esc_html__( 'Invalid mock ID.', 'advanced-rest-api-devtools' ) ) ); } $mocks = get_option( 'aradt_mock_definitions', array() ); if ( ! is_array( $mocks ) ) { $mocks = array(); } $mocks = array_values( array_filter( $mocks, function ( $m ) use ( $mock_id ) { return ! isset( $m['id'] ) || $m['id'] !== $mock_id; } ) ); update_option( 'aradt_mock_definitions', $mocks ); wp_send_json_success( array( 'message' => esc_html__( 'Mock deleted.', 'advanced-rest-api-devtools' ) ) ); } /** * AJAX: Toggle a mock on/off. */ public static function ajax_toggle() { check_ajax_referer( 'aradt_nonce', 'nonce' ); Aradt_Utils::verify_ajax_request(); $mock_id = sanitize_key( wp_unslash( $_POST['mock_id'] ?? '' ) ); if ( empty( $mock_id ) ) { wp_send_json_error( array( 'message' => esc_html__( 'Invalid mock ID.', 'advanced-rest-api-devtools' ) ) ); } $mocks = get_option( 'aradt_mock_definitions', array() ); if ( ! is_array( $mocks ) ) { $mocks = array(); } foreach ( $mocks as &$mock ) { if ( isset( $mock['id'] ) && $mock['id'] === $mock_id ) { $mock['enabled'] = ! $mock['enabled']; break; } } unset( $mock ); update_option( 'aradt_mock_definitions', $mocks ); wp_send_json_success( array( 'message' => esc_html__( 'Mock toggled.', 'advanced-rest-api-devtools' ) ) ); } }