| 1 |
<?php |
| 2 |
/** |
| 3 |
* Meta Keys REST API Controller |
| 4 |
* |
| 5 |
* @since 5.0.0 |
| 6 |
* @package elasticpress |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ElasticPress\REST; |
| 10 |
|
| 11 |
use ElasticPress\Features; |
| 12 |
use ElasticPress\Indexables; |
| 13 |
|
| 14 |
/** |
| 15 |
* Meta Keys API controller class. |
| 16 |
* |
| 17 |
* @since 5.0.0 |
| 18 |
* @package elasticpress |
| 19 |
*/ |
| 20 |
class MetaKeys { |
| 21 |
|
| 22 |
/** |
| 23 |
* Register routes. |
| 24 |
* |
| 25 |
* Registers the route using its own endpoint and the previous facets |
| 26 |
* endpoint, for backwards compatibility. |
| 27 |
* |
| 28 |
* @return void |
| 29 |
*/ |
| 30 |
public function register_routes() { |
| 31 |
$args = [ |
| 32 |
'callback' => [ $this, 'get_meta_keys' ], |
| 33 |
'methods' => 'GET', |
| 34 |
'permission_callback' => [ $this, 'check_permission' ], |
| 35 |
]; |
| 36 |
|
| 37 |
register_rest_route( 'elasticpress/v1', 'meta-keys', $args ); |
| 38 |
register_rest_route( 'elasticpress/v1', 'facets/meta/keys', $args ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Check that the request has permission. |
| 43 |
* |
| 44 |
* @return boolean |
| 45 |
*/ |
| 46 |
public function check_permission() { |
| 47 |
return current_user_can( 'edit_theme_options' ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Get indexed meta keys. |
| 52 |
* |
| 53 |
* @return array |
| 54 |
*/ |
| 55 |
public function get_meta_keys() { |
| 56 |
$post_indexable = Indexables::factory()->get( 'post' ); |
| 57 |
|
| 58 |
try { |
| 59 |
$meta_keys = $post_indexable->get_distinct_meta_field_keys(); |
| 60 |
} catch ( \Throwable $th ) { |
| 61 |
$meta_keys = []; |
| 62 |
} |
| 63 |
|
| 64 |
return $meta_keys; |
| 65 |
} |
| 66 |
} |
| 67 |
|