add_additional_fields_to_object example

$data['visibility'] = array(
                'public'             => (bool) $taxonomy->public,
                'publicly_queryable' => (bool) $taxonomy->publicly_queryable,
                'show_admin_column'  => (bool) $taxonomy->show_admin_column,
                'show_in_nav_menus'  => (bool) $taxonomy->show_in_nav_menus,
                'show_in_quick_edit' => (bool) $taxonomy->show_in_quick_edit,
                'show_ui'            => (bool) $taxonomy->show_ui,
            );
        }

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

        // Wrap the data in a response object.         $response = rest_ensure_response( $data );

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

        /** * Filters a taxonomy returned from the REST API. * * Allows modification of the taxonomy data right before it is returned. * * @since 4.7.0 * * @param WP_REST_Response $response The response object. * @param WP_Taxonomy $item The original taxonomy object. * @param WP_REST_Request $request Request used to generate the response. */
$prepared['instance']['encoded'] = base64_encode( $serialized_instance );
                $prepared['instance']['hash']    = wp_hash( $serialized_instance );

                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. */
$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 );
            }

            $response->data['preview_link'] = get_preview_post_link( $preview_post_id$preview_query_args );
        }

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

        /** * Filters a revision returned from the REST API. * * Allows modification of the revision right before it is returned. * * @since 5.0.0 * * @param WP_REST_Response $response The response object. * @param WP_Post $post The original revision object. * @param WP_REST_Request $request Request used to generate the response. */

        }

        if ( rest_is_field_included( 'styles', $fields ) ) {
            $styles         = $this->style_registry->get_registered_styles_for_block( $block_type->name );
            $styles         = array_values( $styles );
            $data['styles'] = wp_parse_args( $styles$data['styles'] );
            $data['styles'] = array_filter( $data['styles'] );
        }

        $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
        $data    = $this->add_additional_fields_to_object( $data$request );
        $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 ) ) {
            $response->add_links( $this->prepare_links( $block_type ) );
        }

        /** * Filters a block type returned from the REST API. * * Allows modification of the block type data right before it is returned. * * @since 5.5.0 * * @param WP_REST_Response $response The response object. * @param WP_Block_Type $block_type The original block type object. * @param WP_REST_Request $request Request used to generate the response. */
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 );
            $links['collection'] = array(
                'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
            );
            
'templateTypes' => 'template_types',
            'source'        => 'source',
        );
        $data   = array();
        foreach ( $keys as $item_key => $rest_key ) {
            if ( isset( $item[ $item_key ] ) && rest_is_field_included( $rest_key$fields ) ) {
                $data[ $rest_key ] = $item[ $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 );
        return rest_ensure_response( $data );
    }

    /** * Retrieves the block pattern schema, conforming to JSON Schema. * * @since 6.0.0 * @since 6.3.0 Added `source` property. * * @return array Item schema data. */


        if ( rest_is_field_included( 'area', $fields ) && 'wp_template_part' === $template->type ) {
            $data['area'] = $template->area;
        }

        if ( rest_is_field_included( 'modified', $fields ) ) {
            $data['modified'] = mysql_to_rfc3339( $template->modified );
        }

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

        // Wrap the data in a response object.         $response = rest_ensure_response( $data );

        if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
            $links = $this->prepare_links( $template->id );
            $response->add_links( $links );
            if ( ! empty( $links['self']['href'] ) ) {
                $actions = $this->get_available_actions();
                $self    = $links['self']['href'];
                


        if ( in_array( 'parent', $fields, true ) ) {
            $data['parent'] = (int) $item->parent;
        }

        if ( in_array( 'meta', $fields, true ) ) {
            $data['meta'] = $this->meta->get_value( $item->term_id, $request );
        }

        $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
        $data    = $this->add_additional_fields_to_object( $data$request );
        $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 ) ) {
            $response->add_links( $this->prepare_links( $item ) );
        }

        /** * Filters the term data for a REST API response. * * The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug. * * Possible hook names include: * * - `rest_prepare_category` * - `rest_prepare_post_tag` * * Allows modification of the term data right before it is returned. * * @since 4.7.0 * * @param WP_REST_Response $response The response object. * @param WP_Term $item The original term object. * @param WP_REST_Request $request Request used to generate the response. */


        if ( in_array( 'slug', $fields, true ) ) {
            $data['slug'] = $status->name;
        }

        if ( in_array( 'date_floating', $fields, true ) ) {
            $data['date_floating'] = $status->date_floating;
        }

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

        $response = rest_ensure_response( $data );

        $rest_url = rest_url( rest_get_route_for_post_type_items( 'post' ) );
        if ( 'publish' === $status->name ) {
            $response->add_link( 'archives', $rest_url );
        } else {
            $response->add_link( 'archives', add_query_arg( 'status', $status->name, $rest_url ) );
        }

        


        if ( rest_is_field_included( 'modified_gmt', $fields ) ) {
            $data['modified_gmt'] = $this->prepare_date_response( $post->post_modified_gmt );
        }

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

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

        return rest_ensure_response( $data );
    }

    /** * Retrieves the revision's schema, conforming to JSON Schema. * * @since 6.3.0 * * @return array Item schema data. */
'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'] ) ) {
            $prepared['password'] = $item['new_password'];
        }

        $prepared = $this->add_additional_fields_to_object( $prepared$request );
        $prepared = $this->filter_response_by_context( $prepared$request['context'] );

        $response = new WP_REST_Response( $prepared );

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

        /** * Filters the REST API response for an application password. * * @since 5.6.0 * * @param WP_REST_Response $response The response object. * @param array $item The application password array. * @param WP_REST_Request $request The request object. */
continue;
                }

                $data['theme_supports'][ $name ] = $prepared;
            }
        }

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

        $data = $this->add_additional_fields_to_object( $data$request );

        // Wrap the data in a response object.         $response = rest_ensure_response( $data );

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

        /** * Filters theme data returned from the REST API. * * @since 5.0.0 * * @param WP_REST_Response $response The response object. * @param WP_Theme $theme Theme object used to create response. * @param WP_REST_Request $request Request object. */


        if ( in_array( 'author_avatar_urls', $fields, true ) ) {
            $data['author_avatar_urls'] = rest_get_avatar_urls( $comment );
        }

        if ( in_array( 'meta', $fields, true ) ) {
            $data['meta'] = $this->meta->get_value( $comment->comment_ID, $request );
        }

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

        // Wrap the data in a response object.         $response = rest_ensure_response( $data );

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

        /** * Filters a comment returned from the REST API. * * Allows modification of the comment right before it is returned. * * @since 4.7.0 * * @param WP_REST_Response $response The response object. * @param WP_Comment $comment The original comment object. * @param WP_REST_Request $request Request used to generate the response. */

        }

        if ( in_array( 'excerpt', $fields, true ) ) {
            $data['excerpt'] = array(
                'raw'      => $post->post_excerpt,
                'rendered' => $this->prepare_excerpt_response( $post->post_excerpt, $post ),
            );
        }

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

        if ( ! empty( $data['parent'] ) ) {
            $response->add_link( 'parent', rest_url( rest_get_route_for_post( $data['parent'] ) ) );
        }

        /** * Filters a revision returned from the REST API. * * Allows modification of the revision right before it is returned. * * @since 4.7.0 * * @param WP_REST_Response $response The response object. * @param WP_Post $post The original revision object. * @param WP_REST_Request $request Request used to generate the response. */

                $term_ids = $terms ? array_values( wp_list_pluck( $terms, 'term_id' ) ) : array();
                if ( 'nav_menu' === $taxonomy->name ) {
                    $data[ $base ] = $term_ids ? array_shift( $term_ids ) : 0;
                } else {
                    $data[ $base ] = $term_ids;
                }
            }
        }

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

        // Wrap the data in a response object.         $response = rest_ensure_response( $data );

        if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
            $links = $this->prepare_links( $item );
            $response->add_links( $links );

            if ( ! empty( $links['self']['href'] ) ) {
                $actions = $this->get_available_actions( $item$request );

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