# assistant/0.3.1/backend/src/Controllers/LabelsController.php

Assistant – Every Day Productivity Apps, version 0.3.1. 70 lines.

- Page: https://pluginprobe.com/plugins/assistant/0.3.1/code/backend/src/Controllers/LabelsController.php
- Raw: https://pluginprobe.com/plugins/assistant/0.3.1/raw/backend/src/Controllers/LabelsController.php
- Modified: 2020-03-25T19:33:50+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/assistant/0.3.1/code/backend/src/Controllers/LabelsController.php#L10-L20`.

```php
<?php

namespace FL\Assistant\Controllers;

use FL\Assistant\Data\Repository\LabelsRepository;
use FL\Assistant\Data\Transformers\LabelsTransformer;
use FL\Assistant\System\Contracts\ControllerAbstract;
use WP_REST_Server;

/**
 * REST API logic for notation labels.
 */
class LabelsController extends ControllerAbstract {

	/**
	 * @var LabelsRepository
	 */
	protected $labels;
	protected $transformer;

	/**
	 * LabelsController constructor.
	 *
	 * @param LabelsRepository $labels
	 */
	public function __construct( LabelsRepository $labels, LabelsTransformer $transformer ) {
		$this->labels = $labels;
		$this->transformer = $transformer;
	}

	/**
	 * Register routes.
	 */
	public function register_routes() {
		$this->route(
			'/labels', [
				[
					'methods'             => WP_REST_Server::READABLE,
					'callback'            => [ $this, 'get_labels' ],
					'permission_callback' => function () {
						return current_user_can( 'edit_others_posts' );
					},
				],
			]
		);
	}

	/**
	 * Returns all labels.
	 *
	 * @param \WP_REST_Request $request
	 *
	 * @return mixed|\WP_REST_Response
	 */
	public function get_labels( \WP_REST_Request $request ) {
		$response = [];
		$terms = $this->labels->query(
			[
				'hide_empty' => false,
			]
		)->get_terms();

		foreach ( $terms as $term ) {
			$response[] = call_user_func( $this->transformer, $term );
		}

		return rest_ensure_response( $response );
	}
}

```
