, additionalProperties, not propertyNames, not dependencies, not patternProperties, not required * * array: only lists, not tuples - items, not minItems, not maxItems, not uniqueItems, not contains * * enum * * format * * date-time * * email * * ip * * uri * As of WordPress 5.0, Core does not support: * * Multiple type: `type: [ 'string', 'integer' ]` * * $ref, allOf, anyOf, oneOf, not, const * * @return array */ public function get_schema() { /* translators: %s: get_schema() */ _doing_it_wrong( 'WPCOM_REST_API_V2_Field_Controller::get_schema', sprintf( __( "Method '%s' must be overridden.", 'jetpack' ), __METHOD__ ), 'Jetpack 6.8' ); } /** * @param array $schema * @param string $context REST API Request context * @return bool */ private function is_valid_for_context( $schema, $context ) { return empty( $schema['context'] ) || in_array( $context, $schema['context'], true ); } /** * Removes properties that should not appear in the current * request's context * * $context is a Core REST API Framework request attribute that is * always one of: * * view (what you see on the blog) * * edit (what you see in an editor) * * embed (what you see in, e.g., an oembed) * * Fields (and sub-fields, and sub-sub-...) can be flagged for a * set of specific contexts via the field's schema. * * The Core API will filter out top-level fields with the wrong * context, but will not recurse deeply enough into arrays/objects * to remove all levels of sub-fields with the wrong context. * * This function handles that recursion. * * @param mixed $value * @param array $schema * @param string $context REST API Request context * @return mixed Filtered $value */ final public function filter_response_by_context( $value, $schema, $context ) { if ( ! $this->is_valid_for_context( $schema, $context ) ) { // We use this intentionally odd looking WP_Error object // internally only in this recursive function (see below // in the `object` case). It will never be output by the REST API. // If we return this for the top level object, Core // correctly remove the top level object from the response // for us. return new WP_Error( '__wrong-context__' ); } switch ( $schema['type'] ) { case 'array': if ( ! isset( $schema['items'] ) ) { return $value; } // Shortcircuit if we know none of the items are valid for this context. // This would only happen in a strangely written schema. if ( ! $this->is_valid_for_context( $schema['items'], $context ) ) { return array(); } // Recurse to prune sub-properties of each item. foreach ( $value as $key => $item ) { $value[ $key ] = $this->filter_response_by_context( $item, $schema['items'], $context ); } return $value; case 'object': if ( ! isset( $schema['properties'] ) ) { return $value; } foreach ( $value as $field_name => $field_value ) { if ( isset( $schema['properties'][ $field_name ] ) ) { $field_value = $this->filter_response_by_context( $field_value, $schema['properties'][ $field_name ], $context ); if ( is_wp_error( $field_value ) && '__wrong-context__' === $field_value->get_error_code() ) { unset( $value[ $field_name ] ); } else { // Respect recursion that pruned sub-properties of each property. $value[ $field_name ] = $field_value; } } } return (object) $value; } return $value; } }