filter_response_by_context example

public function prepare_item_for_response( $item$request ) {
        $data = array();

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

        if ( rest_is_field_included( 'id', $fields ) ) {
            $data['id'] = (int) $item->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 );

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

        return $response;
    }

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

        if ( rest_is_field_included( 'menu', $fields ) ) {
            $data['menu'] = (int) $menu;
        }

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

        /** * Filters menu location data returned from the REST API. * * @since 5.9.0 * * @param WP_REST_Response $response The response object. * @param object $location The original location object. * @param WP_REST_Request $request Request used to generate the response. */
'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. */
    
$data   = array();
        foreach ( $schema['properties'] as $property_id => $property ) {
            if ( isset( $sidebar[ $property_id ] ) && true === rest_validate_value_from_schema( $sidebar[ $property_id ]$property ) ) {
                $data[ $property_id ] = $sidebar[ $property_id ];
            } elseif ( isset( $property['default'] ) ) {
                $data[ $property_id ] = $property['default'];
            }
        }

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

        /** * Filters the REST API response for a sidebar. * * @since 5.8.0 * * @param WP_REST_Response $response The response object. * @param array $raw_sidebar The raw sidebar data. * @param WP_REST_Request $request The request object. */
 elseif ( array_key_exists( 'default', $schema['properties'][ $extra_field ] ) ) {
                $field = $schema['properties'][ $extra_field ]['default'];
            } else {
                $field = '';
            }

            $data[ $extra_field ] = rest_sanitize_value_from_schema( $field$schema['properties'][ $extra_field ] );
        }

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

        /** * Filters the REST API response for a widget type. * * @since 5.8.0 * * @param WP_REST_Response $response The response object. * @param array $widget_type The array of widget data. * @param WP_REST_Request $request The request object. */
if ( rest_is_field_included( 'settings', $fields ) ) {
            $data['settings'] = ! empty( $config['settings'] ) && $is_global_styles_user_theme_json ? $config['settings'] : new stdClass();
        }

        if ( rest_is_field_included( 'styles', $fields ) ) {
            $data['styles'] = ! empty( $config['styles'] ) && $is_global_styles_user_theme_json ? $config['styles'] : new stdClass();
        }

        $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( $post->ID );
            $response->add_links( $links );
            if ( ! empty( $links['self']['href'] ) ) {
                $actions = $this->get_available_actions();
                $self    = $links['self']['href'];
                foreach ( $actions as $rel ) {
                    
$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 ) ),
            );
            $response->add_links( $links );
        }

        
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'];
                foreach ( $actions as $rel ) {
                    


        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. */
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. */
    
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. */
'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. */
if ( rest_is_field_included( 'rest_base', $fields ) ) {
            $data['rest_base'] = $base;
        }

        if ( rest_is_field_included( 'rest_namespace', $fields ) ) {
            $data['rest_namespace'] = $namespace;
        }

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

        /** * Filters a post type returned from the REST API. * * Allows modification of the post type data right before it is returned. * * @since 4.7.0 * * @param WP_REST_Response $response The response object. * @param WP_Post_Type $post_type The original post type object. * @param WP_REST_Request $request Request used to generate the response. */
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. */
$data['permalink_template'] = $sample_permalink[0];
                }

                if ( $generated_slug_requested ) {
                    $data['generated_slug'] = $sample_permalink[1];
                }
            }
        }

        $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( $post );
            $response->add_links( $links );

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

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