WP_REST_Meta_Fields::get_registered_fields(): array

In this article

Retrieves all the registered meta fields.

Return

array Registered fields.

Source

protected function get_registered_fields() {
	$registered = array();

	$meta_type    = $this->get_meta_type();
	$meta_subtype = $this->get_meta_subtype();

	$meta_keys = get_registered_meta_keys( $meta_type );
	if ( ! empty( $meta_subtype ) ) {
		$meta_keys = array_merge( $meta_keys, get_registered_meta_keys( $meta_type, $meta_subtype ) );
	}

	foreach ( $meta_keys as $name => $args ) {
		if ( empty( $args['show_in_rest'] ) ) {
			continue;
		}

		$rest_args = array();

		if ( is_array( $args['show_in_rest'] ) ) {
			$rest_args = $args['show_in_rest'];
		}

		$default_args = array(
			'name'             => $name,
			'single'           => $args['single'],
			'type'             => ! empty( $args['type'] ) ? $args['type'] : null,
			'schema'           => array(),
			'prepare_callback' => array( $this, 'prepare_value' ),
		);

		$default_schema = array(
			'type'        => $default_args['type'],
			'description' => empty( $args['description'] ) ? '' : $args['description'],
			'default'     => isset( $args['default'] ) ? $args['default'] : null,
		);

		$rest_args           = array_merge( $default_args, $rest_args );
		$rest_args['schema'] = array_merge( $default_schema, $rest_args['schema'] );

		$type = ! empty( $rest_args['type'] ) ? $rest_args['type'] : null;
		$type = ! empty( $rest_args['schema']['type'] ) ? $rest_args['schema']['type'] : $type;

		if ( null === $rest_args['schema']['default'] ) {
			$rest_args['schema']['default'] = static::get_empty_value_for_type( $type );
		}

		$rest_args['schema'] = rest_default_additional_properties_to_false( $rest_args['schema'] );

		if ( ! in_array( $type, array( 'string', 'boolean', 'integer', 'number', 'array', 'object' ), true ) ) {
			continue;
		}

		if ( empty( $rest_args['single'] ) ) {
			$rest_args['schema'] = array(
				'type'  => 'array',
				'items' => $rest_args['schema'],
			);
		}

		$registered[ $name ] = $rest_args;
	}

	return $registered;
}

Changelog

VersionDescription
4.7.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.