# elasticpress/5.0.0/includes/classes/REST/Taxonomies.php

ElasticPress, version 5.0.0. 84 lines.

- Page: https://pluginprobe.com/plugins/elasticpress/5.0.0/code/includes/classes/REST/Taxonomies.php
- Raw: https://pluginprobe.com/plugins/elasticpress/5.0.0/raw/includes/classes/REST/Taxonomies.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.0/code/includes/classes/REST/Taxonomies.php#L10-L20`.

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

namespace ElasticPress\REST;

use ElasticPress\Features;

/**
 * Taxonomies API controller class.
 *
 * @since 5.0.0
 * @package elasticpress
 */
class Taxonomies {

	/**
	 * 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_taxonomies' ],
			'methods'             => 'GET',
			'permission_callback' => [ $this, 'check_permission' ],
		];

		register_rest_route( 'elasticpress/v1', 'taxonomies', $args );
		register_rest_route( 'elasticpress/v1', 'facets/taxonomies', $args );
	}

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

	/**
	 * Get filterable taxonomies.
	 *
	 * @param \WP_REST_Request $request Full details about the request.
	 * @return array
	 */
	public function get_taxonomies( \WP_REST_Request $request ) {
		$filterable_taxonomies = Features::factory()->get_registered_feature( 'facets' )->types['taxonomy']->get_facetable_taxonomies();

		$taxonomies = [];

		foreach ( $filterable_taxonomies as $slug => $taxonomy ) {
			$terms_sample = get_terms(
				[
					'taxonomy' => $slug,
					'number'   => 20,
				]
			);
			if ( is_array( $terms_sample ) ) {
				// This way we make sure it will be an array in the outputted JSON.
				$terms_sample = array_values( $terms_sample );
			} else {
				$terms_sample = [];
			}

			$taxonomies[ $slug ] = [
				'label'  => $taxonomy->labels->singular_name,
				'plural' => $taxonomy->labels->name,
				'terms'  => $terms_sample,
			];
		}

		return $taxonomies;
	}
}

```
