get_item_schema example


        do_action( 'rest_insert_comment', $comment$request, true );

        $schema = $this->get_item_schema();

        if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
            $meta_update = $this->meta->update_value( $request['meta']$comment_id );

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

        $fields_update = $this->update_additional_fields_for_object( $comment$request );

        
/** * Fires after a single menu item is created or updated via the REST API. * * @since 5.9.0 * * @param object $nav_menu_item Inserted or updated menu item object. * @param WP_REST_Request $request Request object. * @param bool $creating True when creating a menu item, false when updating. */
        do_action( 'rest_insert_nav_menu_item', $nav_menu_item$request, true );

        $schema = $this->get_item_schema();

        if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
            $meta_update = $this->meta->update_value( $request['meta']$nav_menu_item_id );

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

        $nav_menu_item = $this->get_nav_menu_item( $nav_menu_item_id );
        $fields_update = $this->update_additional_fields_for_object( $nav_menu_item$request );

        
'type'        => 'integer',
                    'context'     => array( 'view', 'edit', 'embed' ),
                ),
                'slug'         => array(
                    'description' => __( 'An alphanumeric identifier for the revision unique to its type.' ),
                    'type'        => 'string',
                    'context'     => array( 'view', 'edit', 'embed' ),
                ),
            ),
        );

        $parent_schema = $this->parent_controller->get_item_schema();

        if ( ! empty( $parent_schema['properties']['title'] ) ) {
            $schema['properties']['title'] = $parent_schema['properties']['title'];
        }

        if ( ! empty( $parent_schema['properties']['content'] ) ) {
            $schema['properties']['content'] = $parent_schema['properties']['content'];
        }

        if ( ! empty( $parent_schema['properties']['excerpt'] ) ) {
            $schema['properties']['excerpt'] = $parent_schema['properties']['excerpt'];
        }

    public function get_item_schema() {
        if ( $this->schema ) {
            return $this->add_additional_fields_schema( $this->schema );
        }

        // Do not cache this schema because all properties are derived from parent controller.         $schema = parent::get_item_schema();

        /* * Allow all contexts to access `title.raw` and `content.raw`. Clients always * need the raw markup of a reusable block to do anything useful, e.g. parse * it or display it in an editor. */
        $schema['properties']['title']['properties']['raw']['context']   = array( 'view', 'edit' );
        $schema['properties']['content']['properties']['raw']['context'] = array( 'view', 'edit' );

        /* * Remove `title.rendered` and `content.rendered` from the schema. It doesn’t * make sense for a reusable block to have rendered content on its own, since * rendering a block requires it to be inside a post or a page. */
$sidebars = wp_get_sidebars_widgets();
            $widgets  = array_filter(
                isset( $sidebars[ $sidebar['id'] ] ) ? $sidebars[ $sidebar['id'] ] : array(),
                static function D $widget_id ) use ( $wp_registered_widgets ) {
                    return isset( $wp_registered_widgets[ $widget_id ] );
                }
            );

            $sidebar['widgets'] = array_values( $widgets );
        }

        $schema = $this->get_item_schema();
        $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 );
        

    public function get_item_schema() {
        if ( $this->schema ) {
            return $this->add_additional_fields_schema( $this->schema );
        }

        $schema = $this->revisions_controller->get_item_schema();

        $schema['properties']['preview_link'] = array(
            'description' => __( 'Preview link for the post.' ),
            'type'        => 'string',
            'format'      => 'uri',
            'context'     => array( 'edit' ),
            'readonly'    => true,
        );

        $this->schema = $schema;

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

        if ( rest_is_field_included( 'attributes', $fields ) ) {
            $data['attributes'] = $block_type->get_attributes();
        }

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

        $schema = $this->get_item_schema();
        // Fields deprecated in WordPress 6.1, but left in the schema for backwards compatibility.         $deprecated_fields = array(
            'editor_script',
            'script',
            'view_script',
            'editor_style',
            'style',
        );
        $extra_fields      = array_merge(
            array(
                'api_version',
                
/** * Filters a response based on the context defined in the schema. * * @since 4.7.0 * * @param array $response_data Response data to filter. * @param string $context Context defined in the schema. * @return array Filtered response. */
    public function filter_response_by_context( $response_data$context ) {

        $schema = $this->get_item_schema();

        return rest_filter_response_by_context( $response_data$schema$context );
    }

    /** * Retrieves the item's schema, conforming to JSON Schema. * * @since 4.7.0 * * @return array Item schema data. */
    
__( 'Invalid parent type.' ),
                array( 'status' => 400 )
            );
        }

        $insert = $this->insert_attachment( $request );

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

        $schema = $this->get_item_schema();

        // Extract by name.         $attachment_id = $insert['attachment_id'];
        $file          = $insert['file'];

        if ( isset( $request['alt_text'] ) ) {
            update_post_meta( $attachment_id, '_wp_attachment_image_alt', sanitize_text_field( $request['alt_text'] ) );
        }

        if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
            $meta_update = $this->meta->update_value( $request['meta']$attachment_id );

            
array(
                    'methods'             => WP_REST_Server::CREATABLE,
                    'callback'            => array( $this, 'create_item' ),
                    'permission_callback' => array( $this, 'create_item_permissions_check' ),
                    'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
                ),
                'allow_batch' => $this->allow_batch,
                'schema'      => array( $this, 'get_public_item_schema' ),
            )
        );

        $schema        = $this->get_item_schema();
        $get_item_args = array(
            'context' => $this->get_context_param( array( 'default' => 'view' ) ),
        );
        if ( isset( $schema['properties']['password'] ) ) {
            $get_item_args['password'] = array(
                'description' => __( 'The password for the post if it is password protected.' ),
                'type'        => 'string',
            );
        }
        register_rest_route(
            $this->namespace,
            

        do_action( "rest_insert_{$this->taxonomy}", $term$request, true );

        $schema = $this->get_item_schema();
        if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
            $meta_update = $this->meta->update_value( $request['meta']$term->term_id );

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

        $fields_update = $this->update_additional_fields_for_object( $term$request );

        if ( is_wp_error( $fields_update ) ) {
            

    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',
            'option_name',
            'customize_selective_refresh',
        );

        foreach ( $extra_fields as $extra_field ) {
            
/** * Prepares a single term for create or update. * * @since 5.9.0 * * @param WP_REST_Request $request Request object. * @return object Prepared term data. */
    public function prepare_item_for_database( $request ) {
        $prepared_term = parent::prepare_item_for_database( $request );

        $schema = $this->get_item_schema();

        if ( isset( $request['name'] ) && ! empty( $schema['properties']['name'] ) ) {
            $prepared_term->{'menu-name'} = $request['name'];
        }

        return $prepared_term;
    }

    /** * Creates a single term in a taxonomy. * * @since 5.9.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. */

    public function create_item( $request ) {
        if ( ! empty( $request['id'] ) ) {
            return new WP_Error(
                'rest_user_exists',
                __( 'Cannot create existing user.' ),
                array( 'status' => 400 )
            );
        }

        $schema = $this->get_item_schema();

        if ( ! empty( $request['roles'] ) && ! empty( $schema['properties']['roles'] ) ) {
            $check_permission = $this->check_role_update( $request['id']$request['roles'] );

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

        $user = $this->prepare_item_for_database( $request );

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