WP_REST_Posts_Controller::get_item_schema() │ public │ WP 4.7.0
Возвращает схему для записей, соответствующую схеме JSON.
Какие данные находятся в схеме смотрите в материале REST для записей WordPress.
Это метод класса: WP_REST_Posts_Controller
Метод класса: WP_REST_Posts_Controller{}
Хуки из метода
Возвращает
Массив. Данные схемы объекта.
Пример данных на чистой установки WordPress Ru:
Array
(
[$schema] => http://json-schema.org/draft-04/schema#
[title] => post
[type] => object
[properties] => Array
(
[date] => Array
(
[description] => Дата публикации объекта, по временной зоне сайта.
[type] => string
[format] => date-time
[context] => Array
(
[0] => view
[1] => edit
[2] => embed
)
)
[date_gmt] => Array
(
[description] => Время публикации объекта, по GMT.
[type] => string
[format] => date-time
[context] => Array
(
[0] => view
[1] => edit
)
)
[guid] => Array
(
[description] => Глобальный уникальный идентификатор для объекта.
[type] => object
[context] => Array
(
[0] => view
[1] => edit
)
[readonly] => 1
[properties] => Array
(
[raw] => Array
(
[description] => GUID для объекта, как он существует в базе данных.
[type] => string
[context] => Array
(
[0] => edit
)
[readonly] => 1
)
[rendered] => Array
(
[description] => GUID для объекта, преобразованный для показа.
[type] => string
[context] => Array
(
[0] => view
[1] => edit
)
[readonly] => 1
)
)
)
[id] => Array
(
[description] => Уникальный идентификатор для объекта.
[type] => integer
[context] => Array
(
[0] => view
[1] => edit
[2] => embed
)
[readonly] => 1
)
[link] => Array
(
[description] => URL объекта.
[type] => string
[format] => uri
[context] => Array
(
[0] => view
[1] => edit
[2] => embed
)
[readonly] => 1
)
[modified] => Array
(
[description] => Дата последнего изменения объекта, по временной зоне сайта.
[type] => string
[format] => date-time
[context] => Array
(
[0] => view
[1] => edit
)
[readonly] => 1
)
[modified_gmt] => Array
(
[description] => Дата последнего изменения объекта, в GMT.
[type] => string
[format] => date-time
[context] => Array
(
[0] => view
[1] => edit
)
[readonly] => 1
)
[slug] => Array
(
[description] => Буквенно-цифровой идентификатор для объекта уникальный для его типа.
[type] => string
[context] => Array
(
[0] => view
[1] => edit
[2] => embed
)
[arg_options] => Array
(
[sanitize_callback] => Array
(
[0] => WP_REST_Posts_Controller Object
(
[post_type:protected] => post
[meta:protected] => WP_REST_Post_Meta_Fields Object
(
[post_type:protected] => post
)
[namespace:protected] => wp/v2
[rest_base:protected] => posts
)
[1] => sanitize_slug
)
)
)
[status] => Array
(
[description] => Именованный статус для объекта.
[type] => string
[enum] => Array
(
[0] => publish
[1] => future
[2] => draft
[3] => pending
[4] => private
)
[context] => Array
(
[0] => view
[1] => edit
)
)
[type] => Array
(
[description] => Тип записи для объекта.
[type] => string
[context] => Array
(
[0] => view
[1] => edit
[2] => embed
)
[readonly] => 1
)
[password] => Array
(
[description] => Пароль для защиты содержания и отрывка.
[type] => string
[context] => Array
(
[0] => edit
)
)
[title] => Array
(
[description] => Название для объекта.
[type] => object
[context] => Array
(
[0] => view
[1] => edit
[2] => embed
)
[arg_options] => Array
(
[sanitize_callback] =>
[validate_callback] =>
)
[properties] => Array
(
[raw] => Array
(
[description] => Название объекта как оно существует в базе данных.
[type] => string
[context] => Array
(
[0] => edit
)
)
[rendered] => Array
(
[description] => HTML название объекта, преобразованное для показа.
[type] => string
[context] => Array
(
[0] => view
[1] => edit
[2] => embed
)
[readonly] => 1
)
)
)
[content] => Array
(
[description] => Содержимое объекта.
[type] => object
[context] => Array
(
[0] => view
[1] => edit
)
[arg_options] => Array
(
[sanitize_callback] =>
[validate_callback] =>
)
[properties] => Array
(
[raw] => Array
(
[description] => Содержимое объекта как оно существует в базе данных.
[type] => string
[context] => Array
(
[0] => edit
)
)
[rendered] => Array
(
[description] => HTML содержимое объекта преобразованное для показа.
[type] => string
[context] => Array
(
[0] => view
[1] => edit
)
[readonly] => 1
)
[protected] => Array
(
[description] => Защищено ли содержимое паролем.
[type] => boolean
[context] => Array
(
[0] => view
[1] => edit
[2] => embed
)
[readonly] => 1
)
)
)
[author] => Array
(
[description] => ID автора объекта.
[type] => integer
[context] => Array
(
[0] => view
[1] => edit
[2] => embed
)
)
[excerpt] => Array
(
[description] => Отрывок объекта.
[type] => object
[context] => Array
(
[0] => view
[1] => edit
[2] => embed
)
[arg_options] => Array
(
[sanitize_callback] =>
[validate_callback] =>
)
[properties] => Array
(
[raw] => Array
(
[description] => Отрывок объекта как он существует в базе данных.
[type] => string
[context] => Array
(
[0] => edit
)
)
[rendered] => Array
(
[description] => HTML отрывок объекта, преобразованный для показа.
[type] => string
[context] => Array
(
[0] => view
[1] => edit
[2] => embed
)
[readonly] => 1
)
[protected] => Array
(
[description] => Защищать ли паролем отрывок.
[type] => boolean
[context] => Array
(
[0] => view
[1] => edit
[2] => embed
)
[readonly] => 1
)
)
)
[featured_media] => Array
(
[description] => ID избранного медиа для объекта.
[type] => integer
[context] => Array
(
[0] => view
[1] => edit
[2] => embed
)
)
[comment_status] => Array
(
[description] => Открыты ли комментарии для объекта.
[type] => string
[enum] => Array
(
[0] => open
[1] => closed
)
[context] => Array
(
[0] => view
[1] => edit
)
)
[ping_status] => Array
(
[description] => Принимает ли объект уведомления.
[type] => string
[enum] => Array
(
[0] => open
[1] => closed
)
[context] => Array
(
[0] => view
[1] => edit
)
)
[format] => Array
(
[description] => Формат для объекта.
[type] => string
[enum] => Array
(
[0] => standard
[1] => aside
[2] => chat
[3] => gallery
[4] => link
[5] => image
[6] => quote
[7] => status
[8] => video
[9] => audio
)
[context] => Array
(
[0] => view
[1] => edit
)
)
[meta] => Array
(
[description] => Мета поля.
[type] => object
[context] => Array
(
[0] => view
[1] => edit
)
[properties] => Array
(
)
[arg_options] => Array
(
[sanitize_callback] =>
[validate_callback] => Array
(
[0] => WP_REST_Post_Meta_Fields Object
(
[post_type:protected] => post
)
[1] => check_meta_is_array
)
)
)
[sticky] => Array
(
[description] => Считать ли объект прилепленным или нет.
[type] => boolean
[context] => Array
(
[0] => view
[1] => edit
)
)
[template] => Array
(
[description] => Файл темы используемый для показа объекта.
[type] => string
[context] => Array
(
[0] => view
[1] => edit
)
[arg_options] => Array
(
[validate_callback] => Array
(
[0] => WP_REST_Posts_Controller Object
(
[post_type:protected] => post
[meta:protected] => WP_REST_Post_Meta_Fields Object
(
[post_type:protected] => post
)
[namespace:protected] => wp/v2
[rest_base:protected] => posts
)
[1] => check_template
)
)
)
[categories] => Array
(
[description] => Элементы назначенные объекту в таксономии category.
[type] => array
[items] => Array
(
[type] => integer
)
[context] => Array
(
[0] => view
[1] => edit
)
)
[tags] => Array
(
[description] => Элементы назначенные объекту в таксономии post_tag.
[type] => array
[items] => Array
(
[type] => integer
)
[context] => Array
(
[0] => view
[1] => edit
)
)
)
[links] => Array
(
[0] => Array
(
[rel] => https://api.w.org/action-publish
[title] => Текущий пользователь может опубликовать эту запись.
[href] => http://wp-test.ru/wp-json/wp/v2/posts/{id}
[targetSchema] => Array
(
[type] => object
[properties] => Array
(
[status] => Array
(
[type] => string
[enum] => Array
(
[0] => publish
[1] => future
)
)
)
)
)
[1] => Array
(
[rel] => https://api.w.org/action-sticky
[title] => Текущий пользователь может сделать эту запись прикрепленной.
[href] => http://wp-test.ru/wp-json/wp/v2/posts/{id}
[targetSchema] => Array
(
[type] => object
[properties] => Array
(
[sticky] => Array
(
[type] => boolean
)
)
)
)
[2] => Array
(
[rel] => https://api.w.org/action-assign-author
[title] => Текущий пользователь может сменить автора для этой записи.
[href] => http://wp-test.ru/wp-json/wp/v2/posts/{id}
[targetSchema] => Array
(
[type] => object
[properties] => Array
(
[author] => Array
(
[type] => integer
)
)
)
)
[3] => Array
(
[rel] => https://api.w.org/action-assign-categories
[title] => Текущий пользователь может назначать элементы в таксономии category.
[href] => http://wp-test.ru/wp-json/wp/v2/posts/{id}
[targetSchema] => Array
(
[type] => object
[properties] => Array
(
[categories] => Array
(
[type] => array
[items] => Array
(
[type] => integer
)
)
)
)
)
[4] => Array
(
[rel] => https://api.w.org/action-create-categories
[title] => Текущий пользователь может создавать элементы в таксономии category.
[href] => http://wp-test.ru/wp-json/wp/v2/posts/{id}
[targetSchema] => Array
(
[type] => object
[properties] => Array
(
[categories] => Array
(
[type] => array
[items] => Array
(
[type] => integer
)
)
)
)
)
[5] => Array
(
[rel] => https://api.w.org/action-assign-tags
[title] => Текущий пользователь может назначать элементы в таксономии post_tag.
[href] => http://wp-test.ru/wp-json/wp/v2/posts/{id}
[targetSchema] => Array
(
[type] => object
[properties] => Array
(
[tags] => Array
(
[type] => array
[items] => Array
(
[type] => integer
)
)
)
)
)
[6] => Array
(
[rel] => https://api.w.org/action-create-tags
[title] => Текущий пользователь может создавать элементы в таксономии post_tag.
[href] => http://wp-test.ru/wp-json/wp/v2/posts/{id}
[targetSchema] => Array
(
[type] => object
[properties] => Array
(
[tags] => Array
(
[type] => array
[items] => Array
(
[type] => integer
)
)
)
)
)
)
)
Использование
$WP_REST_Posts_Controller = new WP_REST_Posts_Controller(); $var = $WP_REST_Posts_Controller->get_item_schema();
Примеры
0
#1 Пример из ядра WP
Метод WP_REST_Menus_Controller::prepare_item_for_database:
...
/**
* 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;
}
...
Список изменений
| С версии 4.7.0 | Введена. |
Код WP_REST_Posts_Controller::get_item_schema() WP REST Posts Controller::get item schema WP 6.9.1
public function get_item_schema() {
if ( $this->schema ) {
return $this->add_additional_fields_schema( $this->schema );
}
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => $this->post_type,
'type' => 'object',
// Base properties for every Post.
'properties' => array(
'date' => array(
'description' => __( "The date the post was published, in the site's timezone." ),
'type' => array( 'string', 'null' ),
'format' => 'date-time',
'context' => array( 'view', 'edit', 'embed' ),
),
'date_gmt' => array(
'description' => __( 'The date the post was published, as GMT.' ),
'type' => array( 'string', 'null' ),
'format' => 'date-time',
'context' => array( 'view', 'edit' ),
),
'guid' => array(
'description' => __( 'The globally unique identifier for the post.' ),
'type' => 'object',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'properties' => array(
'raw' => array(
'description' => __( 'GUID for the post, as it exists in the database.' ),
'type' => 'string',
'context' => array( 'edit' ),
'readonly' => true,
),
'rendered' => array(
'description' => __( 'GUID for the post, transformed for display.' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
),
'id' => array(
'description' => __( 'Unique identifier for the post.' ),
'type' => 'integer',
'context' => array( 'view', 'edit', 'embed' ),
'readonly' => true,
),
'link' => array(
'description' => __( 'URL to the post.' ),
'type' => 'string',
'format' => 'uri',
'context' => array( 'view', 'edit', 'embed' ),
'readonly' => true,
),
'modified' => array(
'description' => __( "The date the post was last modified, in the site's timezone." ),
'type' => 'string',
'format' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'modified_gmt' => array(
'description' => __( 'The date the post was last modified, as GMT.' ),
'type' => 'string',
'format' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'slug' => array(
'description' => __( 'An alphanumeric identifier for the post unique to its type.' ),
'type' => 'string',
'context' => array( 'view', 'edit', 'embed' ),
'arg_options' => array(
'sanitize_callback' => array( $this, 'sanitize_slug' ),
),
),
'status' => array(
'description' => __( 'A named status for the post.' ),
'type' => 'string',
'enum' => array_keys( get_post_stati( array( 'internal' => false ) ) ),
'context' => array( 'view', 'edit' ),
'arg_options' => array(
'validate_callback' => array( $this, 'check_status' ),
),
),
'type' => array(
'description' => __( 'Type of post.' ),
'type' => 'string',
'context' => array( 'view', 'edit', 'embed' ),
'readonly' => true,
),
'password' => array(
'description' => __( 'A password to protect access to the content and excerpt.' ),
'type' => 'string',
'context' => array( 'edit' ),
),
),
);
$post_type_obj = get_post_type_object( $this->post_type );
if ( is_post_type_viewable( $post_type_obj ) && $post_type_obj->public ) {
$schema['properties']['permalink_template'] = array(
'description' => __( 'Permalink template for the post.' ),
'type' => 'string',
'context' => array( 'edit' ),
'readonly' => true,
);
$schema['properties']['generated_slug'] = array(
'description' => __( 'Slug automatically generated from the post title.' ),
'type' => 'string',
'context' => array( 'edit' ),
'readonly' => true,
);
$schema['properties']['class_list'] = array(
'description' => __( 'An array of the class names for the post container element.' ),
'type' => 'array',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'items' => array(
'type' => 'string',
),
);
}
if ( $post_type_obj->hierarchical ) {
$schema['properties']['parent'] = array(
'description' => __( 'The ID for the parent of the post.' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
);
}
$post_type_attributes = array(
'title',
'editor',
'author',
'excerpt',
'thumbnail',
'comments',
'revisions',
'page-attributes',
'post-formats',
'custom-fields',
);
$fixed_schemas = array(
'post' => array(
'title',
'editor',
'author',
'excerpt',
'thumbnail',
'comments',
'revisions',
'post-formats',
'custom-fields',
),
'page' => array(
'title',
'editor',
'author',
'excerpt',
'thumbnail',
'comments',
'revisions',
'page-attributes',
'custom-fields',
),
'attachment' => array(
'title',
'author',
'comments',
'revisions',
'custom-fields',
'thumbnail',
),
);
foreach ( $post_type_attributes as $attribute ) {
if ( isset( $fixed_schemas[ $this->post_type ] ) && ! in_array( $attribute, $fixed_schemas[ $this->post_type ], true ) ) {
continue;
} elseif ( ! isset( $fixed_schemas[ $this->post_type ] ) && ! post_type_supports( $this->post_type, $attribute ) ) {
continue;
}
switch ( $attribute ) {
case 'title':
$schema['properties']['title'] = array(
'description' => __( 'The title for the post.' ),
'type' => 'object',
'context' => array( 'view', 'edit', 'embed' ),
'arg_options' => array(
'sanitize_callback' => null, // Note: sanitization implemented in self::prepare_item_for_database().
'validate_callback' => null, // Note: validation implemented in self::prepare_item_for_database().
),
'properties' => array(
'raw' => array(
'description' => __( 'Title for the post, as it exists in the database.' ),
'type' => 'string',
'context' => array( 'edit' ),
),
'rendered' => array(
'description' => __( 'HTML title for the post, transformed for display.' ),
'type' => 'string',
'context' => array( 'view', 'edit', 'embed' ),
'readonly' => true,
),
),
);
break;
case 'editor':
$schema['properties']['content'] = array(
'description' => __( 'The content for the post.' ),
'type' => 'object',
'context' => array( 'view', 'edit' ),
'arg_options' => array(
'sanitize_callback' => null, // Note: sanitization implemented in self::prepare_item_for_database().
'validate_callback' => null, // Note: validation implemented in self::prepare_item_for_database().
),
'properties' => array(
'raw' => array(
'description' => __( 'Content for the post, as it exists in the database.' ),
'type' => 'string',
'context' => array( 'edit' ),
),
'rendered' => array(
'description' => __( 'HTML content for the post, transformed for display.' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'block_version' => array(
'description' => __( 'Version of the content block format used by the post.' ),
'type' => 'integer',
'context' => array( 'edit' ),
'readonly' => true,
),
'protected' => array(
'description' => __( 'Whether the content is protected with a password.' ),
'type' => 'boolean',
'context' => array( 'view', 'edit', 'embed' ),
'readonly' => true,
),
),
);
break;
case 'author':
$schema['properties']['author'] = array(
'description' => __( 'The ID for the author of the post.' ),
'type' => 'integer',
'context' => array( 'view', 'edit', 'embed' ),
);
break;
case 'excerpt':
$schema['properties']['excerpt'] = array(
'description' => __( 'The excerpt for the post.' ),
'type' => 'object',
'context' => array( 'view', 'edit', 'embed' ),
'arg_options' => array(
'sanitize_callback' => null, // Note: sanitization implemented in self::prepare_item_for_database().
'validate_callback' => null, // Note: validation implemented in self::prepare_item_for_database().
),
'properties' => array(
'raw' => array(
'description' => __( 'Excerpt for the post, as it exists in the database.' ),
'type' => 'string',
'context' => array( 'edit' ),
),
'rendered' => array(
'description' => __( 'HTML excerpt for the post, transformed for display.' ),
'type' => 'string',
'context' => array( 'view', 'edit', 'embed' ),
'readonly' => true,
),
'protected' => array(
'description' => __( 'Whether the excerpt is protected with a password.' ),
'type' => 'boolean',
'context' => array( 'view', 'edit', 'embed' ),
'readonly' => true,
),
),
);
break;
case 'thumbnail':
$schema['properties']['featured_media'] = array(
'description' => __( 'The ID of the featured media for the post.' ),
'type' => 'integer',
'context' => array( 'view', 'edit', 'embed' ),
);
break;
case 'comments':
$schema['properties']['comment_status'] = array(
'description' => __( 'Whether or not comments are open on the post.' ),
'type' => 'string',
'enum' => array( 'open', 'closed' ),
'context' => array( 'view', 'edit' ),
);
$schema['properties']['ping_status'] = array(
'description' => __( 'Whether or not the post can be pinged.' ),
'type' => 'string',
'enum' => array( 'open', 'closed' ),
'context' => array( 'view', 'edit' ),
);
break;
case 'page-attributes':
$schema['properties']['menu_order'] = array(
'description' => __( 'The order of the post in relation to other posts.' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
);
break;
case 'post-formats':
// Get the native post formats and remove the array keys.
$formats = array_values( get_post_format_slugs() );
$schema['properties']['format'] = array(
'description' => __( 'The format for the post.' ),
'type' => 'string',
'enum' => $formats,
'context' => array( 'view', 'edit' ),
);
break;
case 'custom-fields':
$schema['properties']['meta'] = $this->meta->get_field_schema();
break;
}
}
if ( 'post' === $this->post_type ) {
$schema['properties']['sticky'] = array(
'description' => __( 'Whether or not the post should be treated as sticky.' ),
'type' => 'boolean',
'context' => array( 'view', 'edit' ),
);
}
$schema['properties']['template'] = array(
'description' => __( 'The theme file to use to display the post.' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'arg_options' => array(
'validate_callback' => array( $this, 'check_template' ),
),
);
$taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) );
foreach ( $taxonomies as $taxonomy ) {
$base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
if ( array_key_exists( $base, $schema['properties'] ) ) {
$taxonomy_field_name_with_conflict = ! empty( $taxonomy->rest_base ) ? 'rest_base' : 'name';
_doing_it_wrong(
'register_taxonomy',
sprintf(
/* translators: 1: The taxonomy name, 2: The property name, either 'rest_base' or 'name', 3: The conflicting value. */
__( 'The "%1$s" taxonomy "%2$s" property (%3$s) conflicts with an existing property on the REST API Posts Controller. Specify a custom "rest_base" when registering the taxonomy to avoid this error.' ),
$taxonomy->name,
$taxonomy_field_name_with_conflict,
$base
),
'5.4.0'
);
}
$schema['properties'][ $base ] = array(
/* translators: %s: Taxonomy name. */
'description' => sprintf( __( 'The terms assigned to the post in the %s taxonomy.' ), $taxonomy->name ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'context' => array( 'view', 'edit' ),
);
}
$schema_links = $this->get_schema_links();
if ( $schema_links ) {
$schema['links'] = $schema_links;
}
// Take a snapshot of which fields are in the schema pre-filtering.
$schema_fields = array_keys( $schema['properties'] );
/**
* Filters the post's schema.
*
* The dynamic portion of the filter, `$this->post_type`, refers to the
* post type slug for the controller.
*
* Possible hook names include:
*
* - `rest_post_item_schema`
* - `rest_page_item_schema`
* - `rest_attachment_item_schema`
*
* @since 5.4.0
*
* @param array $schema Item schema data.
*/
$schema = apply_filters( "rest_{$this->post_type}_item_schema", $schema );
// Emit a _doing_it_wrong warning if user tries to add new properties using this filter.
$new_fields = array_diff( array_keys( $schema['properties'] ), $schema_fields );
if ( count( $new_fields ) > 0 ) {
_doing_it_wrong(
__METHOD__,
sprintf(
/* translators: %s: register_rest_field */
__( 'Please use %s to add new schema properties.' ),
'register_rest_field'
),
'5.4.0'
);
}
$this->schema = $schema;
return $this->add_additional_fields_schema( $this->schema );
}