WP_REST_Posts_Controller::get_item_schema() WP 4.7.0
Возвращает схему для записей, соответствующую схеме JSON.
Какие данные находятся в схеме смотрите в материале REST для записей WordPress.
Это метод класса: 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();
Код WP REST Posts Controller::get item schema:
wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
VER 5.0.3
<?php
public function get_item_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 object was published, in the site's timezone." ),
'type' => 'string',
'format' => 'date-time',
'context' => array( 'view', 'edit', 'embed' ),
),
'date_gmt' => array(
'description' => __( 'The date the object was published, as GMT.' ),
'type' => 'string',
'format' => 'date-time',
'context' => array( 'view', 'edit' ),
),
'guid' => array(
'description' => __( 'The globally unique identifier for the object.' ),
'type' => 'object',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'properties' => array(
'raw' => array(
'description' => __( 'GUID for the object, as it exists in the database.' ),
'type' => 'string',
'context' => array( 'edit' ),
'readonly' => true,
),
'rendered' => array(
'description' => __( 'GUID for the object, transformed for display.' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
),
'id' => array(
'description' => __( 'Unique identifier for the object.' ),
'type' => 'integer',
'context' => array( 'view', 'edit', 'embed' ),
'readonly' => true,
),
'link' => array(
'description' => __( 'URL to the object.' ),
'type' => 'string',
'format' => 'uri',
'context' => array( 'view', 'edit', 'embed' ),
'readonly' => true,
),
'modified' => array(
'description' => __( "The date the object 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 object was last modified, as GMT.' ),
'type' => 'string',
'format' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'slug' => array(
'description' => __( 'An alphanumeric identifier for the object 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 object.' ),
'type' => 'string',
'enum' => array_keys( get_post_stati( array( 'internal' => false ) ) ),
'context' => array( 'view', 'edit' ),
),
'type' => array(
'description' => __( 'Type of Post for the object.' ),
'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 object.' ),
'type' => 'string',
'context' => array( 'edit' ),
'readonly' => true,
);
$schema['properties']['generated_slug'] = array(
'description' => __( 'Slug automatically generated from the object title.' ),
'type' => 'string',
'context' => array( 'edit' ),
'readonly' => true,
);
}
if ( $post_type_obj->hierarchical ) {
$schema['properties']['parent'] = array(
'description' => __( 'The ID for the parent of the object.' ),
'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',
),
);
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 object.' ),
'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 object, as it exists in the database.' ),
'type' => 'string',
'context' => array( 'edit' ),
),
'rendered' => array(
'description' => __( 'HTML title for the object, transformed for display.' ),
'type' => 'string',
'context' => array( 'view', 'edit', 'embed' ),
'readonly' => true,
),
),
);
break;
case 'editor':
$schema['properties']['content'] = array(
'description' => __( 'The content for the object.' ),
'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 object, as it exists in the database.' ),
'type' => 'string',
'context' => array( 'edit' ),
),
'rendered' => array(
'description' => __( 'HTML content for the object, transformed for display.' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'block_version' => array(
'description' => __( 'Version of the content block format used by the object.' ),
'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 object.' ),
'type' => 'integer',
'context' => array( 'view', 'edit', 'embed' ),
);
break;
case 'excerpt':
$schema['properties']['excerpt'] = array(
'description' => __( 'The excerpt for the object.' ),
'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 object, as it exists in the database.' ),
'type' => 'string',
'context' => array( 'edit' ),
),
'rendered' => array(
'description' => __( 'HTML excerpt for the object, 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 object.' ),
'type' => 'integer',
'context' => array( 'view', 'edit', 'embed' ),
);
break;
case 'comments':
$schema['properties']['comment_status'] = array(
'description' => __( 'Whether or not comments are open on the object.' ),
'type' => 'string',
'enum' => array( 'open', 'closed' ),
'context' => array( 'view', 'edit' ),
);
$schema['properties']['ping_status'] = array(
'description' => __( 'Whether or not the object 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 object in relation to other object of its type.' ),
'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 object.' ),
'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 object should be treated as sticky.' ),
'type' => 'boolean',
'context' => array( 'view', 'edit' ),
);
}
$schema['properties']['template'] = array(
'description' => __( 'The theme file to use to display the object.' ),
'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;
$schema['properties'][ $base ] = array(
/* translators: %s: taxonomy name */
'description' => sprintf( __( 'The terms assigned to the object 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;
}
return $this->add_additional_fields_schema( $schema );
}

campusboy
3218 — youtube.com/c/wpplus
Создатель YouTube канала wp-plus, на котором делюсь своим опытом. Активный пользователь wp-kama.ru. WordPress-разработчик. Разработка сайтов и лендингов. Доработка существующих проектов. Сопровождение ресурсов.
Редакторы: Kama 7294