WP_REST_Posts_Controller::get_item_schema PHP Method

get_item_schema() public method

Retrieves the post's schema, conforming to JSON Schema.
Since: 4.7.0
public get_item_schema ( ) : array
return array Item schema data.
    public function get_item_schema()
    {
        $schema = array('$schema' => 'http://json-schema.org/schema#', 'title' => $this->post_type, 'type' => 'object', '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('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 ($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), '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), '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), '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), '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'));
                    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':
                    $supports_formats = get_theme_support('post-formats');
                    $schema['properties']['format'] = array('description' => __('The format for the object.'), 'type' => 'string', 'enum' => array_merge(array('standard'), $supports_formats ? array_values($supports_formats[0]) : array()), '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', 'enum' => array_merge(array_keys(wp_get_theme()->get_page_templates(null, $this->post_type)), array('')), 'context' => array('view', 'edit'));
        $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('description' => sprintf(__('The terms assigned to the object in the %s taxonomy.'), $taxonomy->name), 'type' => 'array', 'items' => array('type' => 'integer'), 'context' => array('view', 'edit'));
        }
        return $this->add_additional_fields_schema($schema);
    }

Usage Example

 /**
  * Get the Post's schema, conforming to JSON Schema.
  *
  * @return array
  */
 public function get_item_schema()
 {
     $schema = parent::get_item_schema();
     $schema['properties']['content'] = array('description' => __('Object mapping setting ID to an object of setting params, including value.', 'customize-snapshots'), 'type' => 'object', 'context' => array('view', 'edit'));
     return $schema;
 }
All Usage Examples Of WP_REST_Posts_Controller::get_item_schema