prepare_response_for_collection example

continue;
            }

            if ( is_wp_error( $permissions_check ) && ! $this->check_read_sidebar_permission( $sidebar_id ) ) {
                continue;
            }

            foreach ( $widget_ids as $widget_id ) {
                $response = $this->prepare_item_for_response( compact( 'sidebar_id', 'widget_id' )$request );

                if ( ! is_wp_error( $response ) ) {
                    $prepared[] = $this->prepare_response_for_collection( $response );
                }
            }
        }

        return new WP_REST_Response( $prepared );
    }

    /** * Checks if a given request has access to get a widget. * * @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. */
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_items( $request ) {
        $data = array();

        foreach ( get_registered_nav_menus() as $name => $description ) {
            $location              = new stdClass();
            $location->name        = $name;
            $location->description = $description;

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

        return rest_ensure_response( $data );
    }

    /** * Checks if a given request has access to read a menu location. * * @since 5.9.0 * * @param WP_REST_Request $request Full details about the request. * @return WP_Error|bool True if the request has read access for the item, WP_Error object otherwise. */

        $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();

        
__( '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',
                __( 'The page number requested is larger than the number of pages available.' ),
                
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;

        if ( $total_posts < 1 && $page > 1 ) {
            
$query        = new WP_Comment_Query();
        $query_result = $query->query( $prepared_args );

        $comments = array();

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

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

        $total_comments = (int) $query->found_comments;
        $max_pages      = (int) $query->max_num_pages;

        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;

            
$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. */
public function get_items( $request ) {
        $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. */
$taxonomies = get_taxonomies( '', 'objects' );
        }

        $data = array();

        foreach ( $taxonomies as $tax_type => $value ) {
            if ( empty( $value->show_in_rest ) || ( 'edit' === $request['context'] && ! current_user_can( $value->cap->assign_terms ) ) ) {
                continue;
            }

            $tax               = $this->prepare_item_for_response( $value$request );
            $tax               = $this->prepare_response_for_collection( $tax );
            $data[ $tax_type ] = $tax;
        }

        if ( empty( $data ) ) {
            // Response should still be returned as a JSON object when it is empty.             $data = (object) $data;
        }

        return rest_ensure_response( $data );
    }

    

    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. */
if ( is_wp_error( $parent ) ) {
            return $parent;
        }

        $response  = array();
        $parent_id = $parent->ID;
        $revisions = wp_get_post_revisions( $parent_id, array( 'check_enabled' => false ) );

        foreach ( $revisions as $revision ) {
            if ( str_contains( $revision->post_name, "{$parent_id}-autosave" ) ) {
                $data       = $this->prepare_item_for_response( $revision$request );
                $response[] = $this->prepare_response_for_collection( $data );
            }
        }

        return rest_ensure_response( $response );
    }


    /** * Retrieves the autosave's schema, conforming to JSON Schema. * * @since 5.0.0 * * @return array Item schema data. */
$active_themes = wp_get_themes();
        $current_theme = wp_get_theme();
        $status        = $request['status'];

        foreach ( $active_themes as $theme_name => $theme ) {
            $theme_status = ( $this->is_same_theme( $theme$current_theme ) ) ? 'active' : 'inactive';
            if ( is_array( $status ) && ! in_array( $theme_status$status, true ) ) {
                continue;
            }

            $prepared = $this->prepare_item_for_response( $theme$request );
            $themes[] = $this->prepare_response_for_collection( $prepared );
        }

        $response = rest_ensure_response( $themes );

        $response->header( 'X-WP-Total', count( $themes ) );
        $response->header( 'X-WP-TotalPages', 1 );

        return $response;
    }

    /** * Prepares a single theme output for response. * * @since 5.0.0 * @since 5.9.0 Renamed `$theme` to `$item` to match parent class for PHP 8 named parameter support. * * @param WP_Theme $item Theme object. * @param WP_REST_Request $request Request object. * @return WP_REST_Response Response object. */
 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. */
Home | Imprint | This part of the site doesn't use cookies.