prepare_item_for_response example

// 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 */
// Non-draft posts: create or update the post autosave.             $autosave_id = $this->create_post_autosave( (array) $prepared_post );
        }

        if ( is_wp_error( $autosave_id ) ) {
            return $autosave_id;
        }

        $autosave = get_post( $autosave_id );
        $request->set_param( 'context', 'edit' );

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

        return $response;
    }

    /** * Get the autosave, if the ID is valid. * * @since 5.0.0 * * @param WP_REST_Request $request Full details about the request. * @return WP_Post|WP_Error Revision post object if ID is valid, WP_Error otherwise. */
update_post_parent_caches( $query_result );

        if ( post_type_supports( $this->post_type, 'thumbnail' ) ) {
            update_post_thumbnail_cache( $posts_query );
        }

        foreach ( $query_result as $post ) {
            if ( ! $this->check_read_permission( $post ) ) {
                continue;
            }

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

        // Reset filter.         if ( 'edit' === $request['context'] ) {
            remove_filter( 'post_password_required', array( $this, 'check_password_required' ) );
        }

        $page        = (int) $query_args['paged'];
        $total_posts = $posts_query->found_posts;

        

        } else {
            $revisions       = array();
            $total_revisions = 0;
            $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 );

        
$data              = array();
        $statuses          = get_post_stati( array( 'internal' => false ), 'object' );
        $statuses['trash'] = get_post_status_object( 'trash' );

        foreach ( $statuses as $slug => $obj ) {
            $ret = $this->check_read_permission( $obj );

            if ( ! $ret ) {
                continue;
            }

            $status             = $this->prepare_item_for_response( $obj$request );
            $data[ $obj->name ] = $this->prepare_response_for_collection( $status );
        }

        return rest_ensure_response( $data );
    }

    /** * Checks if a given request has access to read a post status. * * @since 4.7.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. */
$user = $this->get_user( $request );

        if ( is_wp_error( $user ) ) {
            return $user;
        }

        $passwords = WP_Application_Passwords::get_user_application_passwords( $user->ID );
        $response  = array();

        foreach ( $passwords as $password ) {
            $response[] = $this->prepare_response_for_collection(
                $this->prepare_item_for_response( $password$request )
            );
        }

        return new WP_REST_Response( $response );
    }

    /** * Checks if a given request has access to get a specific application password. * * @since 5.6.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_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. */
$namespace = $request['namespace'];
        }

        foreach ( $block_types as $slug => $obj ) {
            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. */

        } else {
            $revisions       = array();
            $total_revisions = 0;
            $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 );

        

    public function get_items( $request ) {
        $data  = array();
        $types = get_post_types( array( 'show_in_rest' => true ), 'objects' );

        foreach ( $types as $type ) {
            if ( 'edit' === $request['context'] && ! current_user_can( $type->cap->edit_posts ) ) {
                continue;
            }

            $post_type           = $this->prepare_item_for_response( $type$request );
            $data[ $type->name ] = $this->prepare_response_for_collection( $post_type );
        }

        return rest_ensure_response( $data );
    }

    /** * Retrieves a specific post type. * * @since 4.7.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. */
'rest_search_handler_error',
                __( 'Internal search handler error.' ),
                array( 'status' => 500 )
            );
        }

        $ids = $result[ WP_REST_Search_Handler::RESULT_IDS ];

        $results = array();

        foreach ( $ids as $id ) {
            $data      = $this->prepare_item_for_response( $id$request );
            $results[] = $this->prepare_response_for_collection( $data );
        }

        $total     = (int) $result[ WP_REST_Search_Handler::RESULT_TOTAL ];
        $page      = (int) $request['page'];
        $per_page  = (int) $request['per_page'];
        $max_pages = ceil( $total / $per_page );

        if ( $page > $max_pages && $total > 0 ) {
            return new WP_Error(
                'rest_search_invalid_page_number',
                
foreach ( get_plugins() as $file => $data ) {
            if ( is_wp_error( $this->check_read_permission( $file ) ) ) {
                continue;
            }

            $data['_file'] = $file;

            if ( ! $this->does_plugin_match_request( $request$data ) ) {
                continue;
            }

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

        return new WP_REST_Response( $plugins );
    }

    /** * Checks if a given request has access to get a specific plugin. * * @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. */
/** * Prepares a single term output for response. * * @since 5.9.0 * * @param WP_Term $term Term object. * @param WP_REST_Request $request Request object. * @return WP_REST_Response Response object. */
    public function prepare_item_for_response( $term$request ) {
        $nav_menu = wp_get_nav_menu_object( $term );
        $response = parent::prepare_item_for_response( $nav_menu$request );

        $fields = $this->get_fields_for_response( $request );
        $data   = $response->get_data();

        if ( rest_is_field_included( 'locations', $fields ) ) {
            $data['locations'] = $this->get_menu_locations( $nav_menu->term_id );
        }

        if ( rest_is_field_included( 'auto_add', $fields ) ) {
            $data['auto_add'] = $this->get_menu_auto_add( $nav_menu->term_id );
        }

        
if ( is_wp_error( $raw_patterns ) ) {
            $raw_patterns->add_data( array( 'status' => 500 ) );

            return $raw_patterns;
        }

        $response = array();

        if ( $raw_patterns ) {
            foreach ( $raw_patterns as $pattern ) {
                $response[] = $this->prepare_response_for_collection(
                    $this->prepare_item_for_response( $pattern$request )
                );
            }
        }

        return new WP_REST_Response( $response );
    }

    /** * Prepare a raw block pattern before it gets output in a REST API response. * * @since 5.8.0 * @since 5.9.0 Renamed `$raw_pattern` to `$item` to match parent class for PHP 8 named parameter support. * * @param object $item Raw pattern from api.wordpress.org, before any changes. * @param WP_REST_Request $request Request object. * @return WP_REST_Response */

    public function get_item( $request ) {
        $post = $this->get_post( $request['id'] );
        if ( is_wp_error( $post ) ) {
            return $post;
        }

        return $this->prepare_item_for_response( $post$request );
    }

    /** * Checks if a given request has access to write a single global styles config. * * @since 5.9.0 * * @param WP_REST_Request $request Full details about the request. * @return true|WP_Error True if the request has write access for the item, WP_Error object otherwise. */
    public function update_item_permissions_check( $request ) {
        
Home | Imprint | This part of the site doesn't use cookies.