rest_ensure_response example


    public function get_items( $request ) {
        $data = array();
        foreach ( $this->get_widgets() as $widget ) {
            $widget_type = $this->prepare_item_for_response( $widget$request );
            $data[]      = $this->prepare_response_for_collection( $widget_type );
        }

        return rest_ensure_response( $data );
    }

    /** * Checks if a given request has access to read a widget type. * * @since 5.8.0 * * @param WP_REST_Request $request Full details about the request. * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise. */
    public function get_item_permissions_check( $request ) {
        
'blockName'    => $request['name'],
            'attrs'        => $attributes,
            'innerHTML'    => '',
            'innerContent' => array(),
        );

        // Render using render_block to ensure all relevant filters are used.         $data = array(
            'rendered' => render_block( $block ),
        );

        return rest_ensure_response( $data );
    }

    /** * Retrieves block's output schema, conforming to JSON Schema. * * @since 5.0.0 * * @return array Item schema data. */
    public function get_item_schema() {
        if ( $this->schema ) {
            
// Include media and image functions to get access to wp_generate_attachment_metadata().         require_once ABSPATH . 'wp-admin/includes/media.php';
        require_once ABSPATH . 'wp-admin/includes/image.php';

        /* * Post-process the upload (create image sub-sizes, make PDF thumbnails, etc.) and insert attachment meta. * At this point the server may run out of resources and post-processing of uploaded images may fail. */
        wp_update_attachment_metadata( $attachment_idwp_generate_attachment_metadata( $attachment_id$file ) );

        $response = $this->prepare_item_for_response( $attachment$request );
        $response = rest_ensure_response( $response );
        $response->set_status( 201 );
        $response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $attachment_id ) ) );

        return $response;
    }

    /** * Inserts the attachment post in the database. Does not update the attachment meta. * * @since 5.3.0 * * @param WP_REST_Request $request * @return array|WP_Error */
if ( ! empty( $widget_object->widget_options['show_instance_in_rest'] ) ) {
                    // Use new stdClass so that JSON result is {} and not [].                     $prepared['instance']['raw'] = empty( $instance ) ? new stdClass() : $instance;
                }
            }
        }

        $context  = ! empty( $request['context'] ) ? $request['context'] : 'view';
        $prepared = $this->add_additional_fields_to_object( $prepared$request );
        $prepared = $this->filter_response_by_context( $prepared$context );

        $response = rest_ensure_response( $prepared );

        if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
            $response->add_links( $this->prepare_links( $prepared ) );
        }

        /** * Filters the REST API response for a widget. * * @since 5.8.0 * * @param WP_REST_Response|WP_Error $response The response object, or WP_Error object on failure. * @param array $widget The registered widget data. * @param WP_REST_Request $request Request used to generate the response. */
$prepared_args = apply_filters( 'rest_user_query', $prepared_args$request );

        $query = new WP_User_Query( $prepared_args );

        $users = array();

        foreach ( $query->results as $user ) {
            $data    = $this->prepare_item_for_response( $user$request );
            $users[] = $this->prepare_response_for_collection( $data );
        }

        $response = rest_ensure_response( $users );

        // Store pagination values for headers then unset for count query.         $per_page = (int) $prepared_args['number'];
        $page     = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 );

        $prepared_args['fields'] = 'ID';

        $total_users = $query->get_total();

        if ( $total_users < 1 ) {
            // Out-of-bounds, run the query again without LIMIT for total count.
$request = new WP_REST_Request( $method$path_parts['path'] );
    if ( ! empty( $path_parts['query'] ) ) {
        parse_str( $path_parts['query']$query_params );
        $request->set_query_params( $query_params );
    }

    $response = rest_do_request( $request );
    if ( 200 === $response->status ) {
        $server = rest_get_server();
        /** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */
        $response = apply_filters( 'rest_post_dispatch', rest_ensure_response( $response )$server$request );
        $embed    = $request->has_param( '_embed' ) ? rest_parse_embed_param( $request['_embed'] ) : false;
        $data     = (array) $server->response_to_data( $response$embed );

        if ( 'OPTIONS' === $method ) {
            $memo[ $method ][ $path ] = array(
                'body'    => $data,
                'headers' => $response->headers,
            );
        } else {
            $memo[ $path ] = array(
                'body'    => $data,
                
$post          = get_post( $request['id'] );
        $fields_update = $this->update_additional_fields_for_object( $post$request );
        if ( is_wp_error( $fields_update ) ) {
            return $fields_update;
        }

        wp_after_insert_post( $post, true, $post_before );

        $response = $this->prepare_item_for_response( $post$request );

        return rest_ensure_response( $response );
    }

    /** * Prepares a single global styles config for update. * * @since 5.9.0 * @since 6.2.0 Added validation of styles.css property. * * @param WP_REST_Request $request Request object. * @return stdClass|WP_Error Prepared item on success. WP_Error on when the custom CSS is not valid. */
    

    public function get_item( $request ) {
        $post = WP_Navigation_Fallback::get_fallback();

        if ( empty( $post ) ) {
            return rest_ensure_response( new WP_Error( 'no_fallback_menu', __( 'No fallback menu found.' ), array( 'status' => 404 ) ) );
        }

        $response = $this->prepare_item_for_response( $post$request );

        return $response;
    }

    /** * Retrieves the fallbacks' schema, conforming to JSON Schema. * * @since 6.3.0 * * @return array Item schema data. */


            if ( is_wp_error( $permissions_check ) && ! $this->check_read_permission( $sidebar ) ) {
                continue;
            }

            $data[] = $this->prepare_response_for_collection(
                $this->prepare_item_for_response( $sidebar$request )
            );
        }

        return rest_ensure_response( $data );
    }

    /** * Checks if a given request has access to get a single sidebar. * * @since 5.8.0 * * @param WP_REST_Request $request Full details about the request. * @return true|WP_Error True if the request has read access, WP_Error object otherwise. */
    public function get_item_permissions_check( $request ) {
        
if ( $namespace ) {
                list ( $block_namespace ) = explode( '/', $obj->name );

                if ( $namespace !== $block_namespace ) {
                    continue;
                }
            }
            $block_type = $this->prepare_item_for_response( $obj$request );
            $data[]     = $this->prepare_response_for_collection( $block_type );
        }

        return rest_ensure_response( $data );
    }

    /** * Checks if a given request has access to read a block type. * * @since 5.5.0 * * @param WP_REST_Request $request Full details about the request. * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise. */
    public function get_item_permissions_check( $request ) {
        
if ( $total_comments < 1 ) {
            // Out-of-bounds, run the query again without LIMIT for total count.             unset( $prepared_args['number']$prepared_args['offset'] );

            $query                  = new WP_Comment_Query();
            $prepared_args['count'] = true;

            $total_comments = $query->query( $prepared_args );
            $max_pages      = ceil( $total_comments / $request['per_page'] );
        }

        $response = rest_ensure_response( $comments );
        $response->header( 'X-WP-Total', $total_comments );
        $response->header( 'X-WP-TotalPages', $max_pages );

        $base = add_query_arg( urlencode_deep( $request->get_query_params() )rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) );

        if ( $request['page'] > 1 ) {
            $prev_page = $request['page'] - 1;

            if ( $prev_page > $max_pages ) {
                $prev_page = $max_pages;
            }

            

        do_action( 'rest_after_insert_nav_menu_item', $nav_menu_item$request, true );

        $post = get_post( $nav_menu_item_id );
        wp_after_insert_post( $post, false, null );

        $response = $this->prepare_item_for_response( $post$request );
        $response = rest_ensure_response( $response );

        $response->set_status( 201 );
        $response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $nav_menu_item_id ) ) );

        return $response;
    }

    /** * Updates a single nav menu item. * * @since 5.9.0 * * @param WP_REST_Request $request Full details about the request. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */

    public function get_items( $request ) {
        $response   = array();
        $categories = WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered();
        foreach ( $categories as $category ) {
            $prepared_category = $this->prepare_item_for_response( $category$request );
            $response[]        = $this->prepare_response_for_collection( $prepared_category );
        }

        return rest_ensure_response( $response );
    }

    /** * Prepare a raw block pattern category before it gets output in a REST API response. * * @since 6.0.0 * * @param array $item Raw category as registered, before any changes. * @param WP_REST_Request $request Request object. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */
    
$max_pages       = 0;
            $page            = (int) $request['page'];
        }

        $response = array();

        foreach ( $revisions as $revision ) {
            $data       = $this->prepare_item_for_response( $revision$request );
            $response[] = $this->prepare_response_for_collection( $data );
        }

        $response = rest_ensure_response( $response );

        $response->header( 'X-WP-Total', (int) $total_revisions );
        $response->header( 'X-WP-TotalPages', (int) $max_pages );

        $request_params = $request->get_query_params();
        $base_path      = rest_url( sprintf( '%s/%s/%d/%s', $this->namespace, $this->parent_base, $request['parent']$this->rest_base ) );
        $base           = add_query_arg( urlencode_deep( $request_params )$base_path );

        if ( $page > 1 ) {
            $prev_page = $page - 1;

            
$max_pages = ceil( $total_posts / (int) $posts_query->query_vars['posts_per_page'] );

        if ( $page > $max_pages && $total_posts > 0 ) {
            return new WP_Error(
                'rest_post_invalid_page_number',
                __( 'The page number requested is larger than the number of pages available.' ),
                array( 'status' => 400 )
            );
        }

        $response = rest_ensure_response( $posts );

        $response->header( 'X-WP-Total', (int) $total_posts );
        $response->header( 'X-WP-TotalPages', (int) $max_pages );

        $request_params = $request->get_query_params();
        $collection_url = rest_url( rest_get_route_for_post_type_items( $this->post_type ) );
        $base           = add_query_arg( urlencode_deep( $request_params )$collection_url );

        if ( $page > 1 ) {
            $prev_page = $page - 1;

            
Home | Imprint | This part of the site doesn't use cookies.