| 1 |
<?php |
| 2 |
|
| 3 |
defined('ABSPATH') || exit; |
| 4 |
|
| 5 |
|
| 6 |
class Blockspare_TB_Conditions_Controller extends WP_REST_Controller |
| 7 |
{ |
| 8 |
|
| 9 |
protected $namespace = 'stbldr/v1'; |
| 10 |
protected $rest_base = 'conditions'; |
| 11 |
|
| 12 |
/** |
| 13 |
* @var Blockspare_TB_Condition_Registry |
| 14 |
*/ |
| 15 |
private $registry; |
| 16 |
|
| 17 |
/** |
| 18 |
* @param Blockspare_TB_Condition_Registry $registry Shared condition registry. |
| 19 |
*/ |
| 20 |
public function __construct(Blockspare_TB_Condition_Registry $registry) |
| 21 |
{ |
| 22 |
$this->registry = $registry; |
| 23 |
} |
| 24 |
|
| 25 |
public function register_routes() |
| 26 |
{ |
| 27 |
register_rest_route( |
| 28 |
$this->namespace, |
| 29 |
'/' . $this->rest_base, |
| 30 |
array( |
| 31 |
array( |
| 32 |
'methods' => WP_REST_Server::READABLE, |
| 33 |
'callback' => array($this, 'get_items'), |
| 34 |
'permission_callback' => array($this, 'get_items_permissions_check'), |
| 35 |
), |
| 36 |
) |
| 37 |
); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Only users who can edit templates need to know what conditions exist. |
| 42 |
*/ |
| 43 |
public function get_items_permissions_check($request) |
| 44 |
{ |
| 45 |
return current_user_can('edit_pages'); |
| 46 |
} |
| 47 |
|
| 48 |
public function get_items($request) |
| 49 |
{ |
| 50 |
$items = array(); |
| 51 |
|
| 52 |
foreach ($this->registry->all() as $condition) { |
| 53 |
$items[] = array( |
| 54 |
'slug' => $condition->get_slug(), |
| 55 |
'label' => $condition->get_label(), |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
return rest_ensure_response($items); |
| 60 |
} |
| 61 |
} |
| 62 |
|