WP_REST_Request example

/** * Filter to disable remote block patterns. * * @since 5.8.0 * * @param bool $should_load_remote */
    $should_load_remote = apply_filters( 'should_load_remote_block_patterns', true );

    if ( $supports_core_patterns && $should_load_remote ) {
        $request         = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' );
        $core_keyword_id = 11; // 11 is the ID for "core".         $request->set_param( 'keyword', $core_keyword_id );
        $response = rest_do_request( $request );
        if ( $response->is_error() ) {
            return;
        }
        $patterns = $response->get_data();

        foreach ( $patterns as $pattern ) {
            $pattern['source']  = 'pattern-directory/core';
            $normalized_pattern = wp_normalize_remote_block_pattern( $pattern );
            

        }

        if ( empty( $path ) ) {
            if ( isset( $_SERVER['PATH_INFO'] ) ) {
                $path = $_SERVER['PATH_INFO'];
            } else {
                $path = '/';
            }
        }

        $request = new WP_REST_Request( $_SERVER['REQUEST_METHOD']$path );

        $request->set_query_params( wp_unslash( $_GET ) );
        $request->set_body_params( wp_unslash( $_POST ) );
        $request->set_file_params( $_FILES );
        $request->set_headers( $this->get_headers( wp_unslash( $_SERVER ) ) );
        $request->set_body( self::get_raw_data() );

        /* * HTTP method override for clients that can't use PUT/PATCH/DELETE. First, we check * $_GET['_method']. If that is not set, we check for the HTTP_X_HTTP_METHOD_OVERRIDE * header. */
// Pretty permalinks on, and URL is under the API root.             $api_url_part = substr( $urlstrlen( untrailingslashit( $api_root ) ) );
            $route        = parse_url( $api_url_part, PHP_URL_PATH );
        } elseif ( ! empty( $query_params['rest_route'] ) ) {
            // ?rest_route=... set directly.             $route = $query_params['rest_route'];
            unset( $query_params['rest_route'] );
        }

        $request = false;
        if ( ! empty( $route ) ) {
            $request = new WP_REST_Request( 'GET', $route );
            $request->set_query_params( $query_params );
        }

        /** * Filters the REST API request generated from a URL. * * @since 4.5.0 * * @param WP_REST_Request|false $request Generated request object, or false if URL * could not be parsed. * @param string $url URL the request was generated from. */

function rest_ensure_request( $request ) {
    if ( $request instanceof WP_REST_Request ) {
        return $request;
    }

    if ( is_string( $request ) ) {
        return new WP_REST_Request( 'GET', $request );
    }

    return new WP_REST_Request( 'GET', '', $request );
}

/** * Ensures a REST response is a response object (for consistency). * * This implements WP_REST_Response, allowing usage of `set_status`/`header`/etc * without needing to double-check the object. Will also allow WP_Error to indicate error * responses, so users should immediately check for this value. * * @since 4.4.0 * * @param WP_REST_Response|WP_Error|WP_HTTP_Response|mixed $response Response to check. * @return WP_REST_Response|WP_Error If response generated an error, WP_Error, if response * is already an instance, WP_REST_Response, otherwise * returns a new WP_REST_Response instance. */
Home | Imprint | This part of the site doesn't use cookies.