| 1 |
<?php |
| 2 |
|
| 3 |
namespace Texty\Api; |
| 4 |
|
| 5 |
use Texty\Notifications as TextyNotifications; |
| 6 |
use WP_REST_Server; |
| 7 |
|
| 8 |
class Notifications extends Base { |
| 9 |
|
| 10 |
/** |
| 11 |
* Initialize |
| 12 |
* |
| 13 |
* @return void |
| 14 |
*/ |
| 15 |
public function __construct() { |
| 16 |
$this->namespace = 'texty/v1'; |
| 17 |
$this->rest_base = 'notifications'; |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Registers the routes for the objects of the controller. |
| 22 |
* |
| 23 |
* @return void |
| 24 |
*/ |
| 25 |
public function register_routes() { |
| 26 |
register_rest_route( |
| 27 |
$this->namespace, |
| 28 |
'/' . $this->rest_base, |
| 29 |
[ |
| 30 |
[ |
| 31 |
'methods' => WP_REST_Server::READABLE, |
| 32 |
'callback' => [ $this, 'get_items' ], |
| 33 |
'permission_callback' => [ $this, 'admin_permissions_check' ], |
| 34 |
'args' => [], |
| 35 |
], |
| 36 |
[ |
| 37 |
'methods' => WP_REST_Server::EDITABLE, |
| 38 |
'callback' => [ $this, 'update_items' ], |
| 39 |
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
| 40 |
'permission_callback' => [ $this, 'admin_permissions_check' ], |
| 41 |
], |
| 42 |
'schema' => [ $this, 'get_item_schema' ], |
| 43 |
] |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Retrieves a list of items. |
| 49 |
* |
| 50 |
* @param WP_Rest_Request $request |
| 51 |
* |
| 52 |
* @return WP_Rest_Response|WP_Error |
| 53 |
*/ |
| 54 |
public function get_items( $request ) { |
| 55 |
if ( isset( $request['context'] ) && $request['context'] === 'edit' ) { |
| 56 |
$notifications = texty()->notifications()->all(); |
| 57 |
|
| 58 |
$response = [ |
| 59 |
'groups' => texty()->notifications()->get_groups(), |
| 60 |
'roles' => $this->get_roles(), |
| 61 |
'notifications' => array_map( function ( $notifier ) { // phpcs:ignore |
| 62 |
$obj = new $notifier(); |
| 63 |
|
| 64 |
return [ |
| 65 |
'id' => $obj->get_id(), |
| 66 |
'enabled' => $obj->enabled(), |
| 67 |
'title' => $obj->get_title(), |
| 68 |
'type' => $obj->get_type(), |
| 69 |
'message' => $obj->get_message_raw(), |
| 70 |
'route' => 'sms', |
| 71 |
'group' => $obj->get_group(), |
| 72 |
'recipients' => $obj->get_recipients_raw(), |
| 73 |
'replacements' => array_merge( |
| 74 |
array_keys( $obj->replacement_keys() ), |
| 75 |
array_keys( $obj->global_replacement_keys() ) |
| 76 |
), |
| 77 |
]; |
| 78 |
}, $notifications ), // phpcs:ignore |
| 79 |
]; |
| 80 |
} else { |
| 81 |
$response = texty()->notifications()->settings(); |
| 82 |
} |
| 83 |
|
| 84 |
return rest_ensure_response( $response ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Updates item from the collection. |
| 89 |
* |
| 90 |
* @param WP_REST_Request $request |
| 91 |
* |
| 92 |
* @return WP_Error|WP_REST_Response |
| 93 |
*/ |
| 94 |
public function update_items( $request ) { |
| 95 |
$settings = []; |
| 96 |
$notifications = texty()->notifications()->all(); |
| 97 |
|
| 98 |
foreach ( $notifications as $class ) { |
| 99 |
$obj = new $class(); |
| 100 |
|
| 101 |
if ( $request->has_param( $obj->get_id() ) ) { |
| 102 |
$settings[ $obj->get_id() ] = $request->get_param( $obj->get_id() ); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
update_option( TextyNotifications::OPTION_KEY, $settings, false ); |
| 107 |
|
| 108 |
$request->set_param( 'context', 'edit' ); |
| 109 |
|
| 110 |
return $this->get_items( $request ); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Get user roles |
| 115 |
* |
| 116 |
* @return array |
| 117 |
*/ |
| 118 |
public function get_roles() { |
| 119 |
$roles = []; |
| 120 |
|
| 121 |
foreach ( wp_roles()->get_names() as $value => $label ) { |
| 122 |
$roles[] = [ |
| 123 |
'label' => $label, |
| 124 |
'value' => $value, |
| 125 |
]; |
| 126 |
} |
| 127 |
|
| 128 |
return $roles; |
| 129 |
} |
| 130 |
} |
| 131 |
|