get_fields_for_response example


    protected function add_additional_fields_to_object( $response_data$request ) {

        $additional_fields = $this->get_additional_fields();

        $requested_fields = $this->get_fields_for_response( $request );

        foreach ( $additional_fields as $field_name => $field_options ) {
            if ( ! $field_options['get_callback'] ) {
                continue;
            }

            if ( ! rest_is_field_included( $field_name$requested_fields ) ) {
                continue;
            }

            $response_data[ $field_name ] = call_user_func(
                

    public function prepare_item_for_response( $item$request ) {
        // Restores the more descriptive, specific name for use within this method.         $post     = $item;
        $response = $this->revisions_controller->prepare_item_for_response( $post$request );

        $fields = $this->get_fields_for_response( $request );

        if ( in_array( 'preview_link', $fields, true ) ) {
            $parent_id          = wp_is_post_autosave( $post );
            $preview_post_id    = false === $parent_id ? $post->ID : $parent_id;
            $preview_query_args = array();

            if ( false !== $parent_id ) {
                $preview_query_args['preview_id']    = $parent_id;
                $preview_query_args['preview_nonce'] = wp_create_nonce( 'post_preview_' . $parent_id );
            }

            

    public function prepare_item_for_response( $item$request ) {
        // Restores the more descriptive, specific name for use within this method.         $post     = $item;
        $response = parent::prepare_item_for_response( $post$request );
        $fields   = $this->get_fields_for_response( $request );
        $data     = $response->get_data();

        if ( in_array( 'description', $fields, true ) ) {
            $data['description'] = array(
                'raw'      => $post->post_content,
                /** This filter is documented in wp-includes/post-template.php */
                'rendered' => apply_filters( 'the_content', $post->post_content ),
            );
        }

        if ( in_array( 'caption', $fields, true ) ) {
            

    public function prepare_item_for_response( $item$request ) {
        // Restores the more descriptive, specific name for use within this method.         $item_id = $item;
        $handler = $this->get_search_handler( $request );
        if ( is_wp_error( $handler ) ) {
            return new WP_REST_Response();
        }

        $fields = $this->get_fields_for_response( $request );

        $data = $handler->prepare_item( $item_id$fields );
        $data = $this->add_additional_fields_to_object( $data$request );

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

        $response = rest_ensure_response( $data );

        if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
            $links               = $handler->prepare_item_links( $item_id );
            

    public function prepare_item_for_response( $item$request ) {
        // Restores the more descriptive, specific name for use within this method.         $comment = $item;
        $fields  = $this->get_fields_for_response( $request );
        $data    = array();

        if ( in_array( 'id', $fields, true ) ) {
            $data['id'] = (int) $comment->comment_ID;
        }

        if ( in_array( 'post', $fields, true ) ) {
            $data['post'] = (int) $comment->comment_post_ID;
        }

        if ( in_array( 'parent', $fields, true ) ) {
            

    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 );
        }

        $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
        

    public function prepare_item_for_response( $item$request ) {
        // Restores the more descriptive, specific name for use within this method.         $post_type  = $item;
        $taxonomies = wp_list_filter( get_object_taxonomies( $post_type->name, 'objects' ), array( 'show_in_rest' => true ) );
        $taxonomies = wp_list_pluck( $taxonomies, 'name' );
        $base       = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name;
        $namespace  = ! empty( $post_type->rest_namespace ) ? $post_type->rest_namespace : 'wp/v2';
        $supports   = get_all_post_type_supports( $post_type->name );

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

        if ( rest_is_field_included( 'capabilities', $fields ) ) {
            $data['capabilities'] = $post_type->cap;
        }

        if ( rest_is_field_included( 'description', $fields ) ) {
            $data['description'] = $post_type->description;
        }

        if ( rest_is_field_included( 'hierarchical', $fields ) ) {
            

    public function prepare_item_for_response( $item$request ) {
        // Restores the more descriptive, specific name for use within this method.         $post            = $item;
        $GLOBALS['post'] = $post;

        setup_postdata( $post );

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

        if ( in_array( 'author', $fields, true ) ) {
            $data['author'] = (int) $post->post_author;
        }

        if ( in_array( 'date', $fields, true ) ) {
            $data['date'] = $this->prepare_date_response( $post->post_date_gmt, $post->post_date );
        }

        if ( in_array( 'date_gmt', $fields, true ) ) {
            

    public function prepare_item_for_response( $item$request ) {
        // Restores the more descriptive, specific name for use within this method.         $location  = $item;
        $locations = get_nav_menu_locations();
        $menu      = isset( $locations[ $location->name ] ) ? $locations[ $location->name ] : 0;

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

        if ( rest_is_field_included( 'name', $fields ) ) {
            $data['name'] = $location->name;
        }

        if ( rest_is_field_included( 'description', $fields ) ) {
            $data['description'] = $location->description;
        }

        if ( rest_is_field_included( 'menu', $fields ) ) {
            
/** * Prepare a raw block pattern before it gets output in a REST API response. * * @since 6.0.0 * @since 6.3.0 Added `source` property. * * @param array $item Raw pattern 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. */
    public function prepare_item_for_response( $item$request ) {
        $fields = $this->get_fields_for_response( $request );
        $keys   = array(
            'name'          => 'name',
            'title'         => 'title',
            'content'       => 'content',
            'description'   => 'description',
            'viewportWidth' => 'viewport_width',
            'inserter'      => 'inserter',
            'categories'    => 'categories',
            'keywords'      => 'keywords',
            'blockTypes'    => 'block_types',
            'postTypes'     => 'post_types',
            
/** * 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. */
    public function prepare_item_for_response( $item$request ) {
        $fields = $this->get_fields_for_response( $request );
        $keys   = array( 'name', 'label', 'description' );
        $data   = array();
        foreach ( $keys as $key ) {
            if ( isset( $item[ $key ] ) && rest_is_field_included( $key$fields ) ) {
                $data[ $key ] = $item[ $key ];
            }
        }

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

        

    public function prepare_item_for_response( $item$request ) {
        // Restores the more descriptive, specific name for use within this method.         $widget_type = $item;
        $fields      = $this->get_fields_for_response( $request );
        $data        = array(
            'id' => $widget_type['id'],
        );

        $schema       = $this->get_item_schema();
        $extra_fields = array(
            'name',
            'description',
            'is_multi',
            'classname',
            'widget_class',
            
if ( ! isset( $wp_registered_widgets[ $widget_id ] ) ) {
            return new WP_Error(
                'rest_invalid_widget',
                __( 'The requested widget is invalid.' ),
                array( 'status' => 500 )
            );
        }

        $widget    = $wp_registered_widgets[ $widget_id ];
        $parsed_id = wp_parse_widget_id( $widget_id );
        $fields    = $this->get_fields_for_response( $request );

        $prepared = array(
            'id'            => $widget_id,
            'id_base'       => $parsed_id['id_base'],
            'sidebar'       => $sidebar_id,
            'rendered'      => '',
            'rendered_form' => null,
            'instance'      => null,
        );

        if (
            

    public function prepare_item_for_response( $item$request ) {
        $user = $this->get_user( $request );

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

        $fields = $this->get_fields_for_response( $request );

        $prepared = array(
            'uuid'      => $item['uuid'],
            'app_id'    => empty( $item['app_id'] ) ? '' : $item['app_id'],
            'name'      => $item['name'],
            'created'   => gmdate( 'Y-m-d\TH:i:s', $item['created'] ),
            'last_used' => $item['last_used'] ? gmdate( 'Y-m-d\TH:i:s', $item['last_used'] ) : null,
            'last_ip'   => $item['last_ip'] ? $item['last_ip'] : null,
        );

        if ( isset( $item['new_password'] ) ) {
            
/** * Prepares a single post output for response. * * @since 5.9.0 * * @param WP_Post $item Post object. * @param WP_REST_Request $request Request object. * @return WP_REST_Response Response object. */
    public function prepare_item_for_response( $item$request ) {
        // Base fields for every post.         $fields    = $this->get_fields_for_response( $request );
        $menu_item = $this->get_nav_menu_item( $item->ID );
        $data      = array();

        if ( rest_is_field_included( 'id', $fields ) ) {
            $data['id'] = $menu_item->ID;
        }

        if ( rest_is_field_included( 'title', $fields ) ) {
            $data['title'] = array();
        }

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