# elasticpress/5.0.2/includes/classes/REST/MetaKeys.php

ElasticPress, version 5.0.2. 68 lines.

- Page: https://pluginprobe.com/plugins/elasticpress/5.0.2/code/includes/classes/REST/MetaKeys.php
- Raw: https://pluginprobe.com/plugins/elasticpress/5.0.2/raw/includes/classes/REST/MetaKeys.php
- Modified: 2023-11-01T18:08:28+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/elasticpress/5.0.2/code/includes/classes/REST/MetaKeys.php#L10-L20`.

```php
<?php
/**
 * Meta Keys REST API Controller
 *
 * @since 5.0.0
 * @package elasticpress
 */

namespace ElasticPress\REST;

use ElasticPress\Features;
use ElasticPress\Indexables;

/**
 * Meta Keys API controller class.
 *
 * @since 5.0.0
 * @package elasticpress
 */
class MetaKeys {

	/**
	 * Register routes.
	 *
	 * Registers the route using its own endpoint and the previous facets
	 * endpoint, for backwards compatibility.
	 *
	 * @return void
	 */
	public function register_routes() {
		$args = [
			'callback'            => [ $this, 'get_meta_keys' ],
			'methods'             => 'GET',
			'permission_callback' => [ $this, 'check_permission' ],
		];

		register_rest_route( 'elasticpress/v1', 'meta-keys', $args );
		register_rest_route( 'elasticpress/v1', 'facets/meta/keys', $args );
	}

	/**
	 * Check that the request has permission.
	 *
	 * @return boolean
	 */
	public function check_permission() {
		return current_user_can( 'edit_theme_options' );
	}

	/**
	 * Get indexed meta keys.
	 *
	 * @param \WP_REST_Request $request Full details about the request.
	 * @return array
	 */
	public function get_meta_keys( \WP_REST_Request $request ) {
		$post_indexable = Indexables::factory()->get( 'post' );

		try {
			$meta_keys = $post_indexable->get_distinct_meta_field_keys();
		} catch ( \Throwable $th ) {
			$meta_keys = [];
		}

		return $meta_keys;
	}
}

```
