WP_REST_Posts_Controller::get_item_schema() publicWP 4.7.0

Повертає схему для записів, що відповідає схемі JSON.

Які дані перебувають у схемі дивіться у матеріалі REST для записів WordPress .

Це метод класу: WP_REST_Posts_Controller

{} Це метод класу: WP_REST_Posts_Controller{}

Хуки з методу
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] => галерея [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] => Елементи призначені об'єкту в таксономії категорії. [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] => Поточний користувач може призначати елементи в таксономії категорії. [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] => Поточний користувач може створювати елементи в таксономії категорії. [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 = новий 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 6.0.2

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' => __( "Закінчити цю пошту було згодом змінено, в зоні часу." ),
				'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 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,
		);
	}

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

	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 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 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' або 'name', 3: The conflicting value. */
					__( 'The '%1$s' taxonomy '%2$s' property (%3$s) conflicts with existing property on the REST API Posts Controller. .'),
					$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;
	}

	/ / Таке стопку з яких fields є в 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 до 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 );
}

Залишити відповідь

Ваша e-mail адреса не оприлюднюватиметься. Обов’язкові поля позначені *