-
Notifications
You must be signed in to change notification settings - Fork 0
/
Endpoint.php
53 lines (44 loc) · 939 Bytes
/
Endpoint.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
namespace Vanilla;
abstract class Endpoint {
/**
* Action name to be handled
*
* @var string
*/
protected $name;
/**
* Check if Endpoint will be accessible only by authorized users
*
* @var bool
*/
protected $authOnly = false;
/**
* Endpoint handler
*
* @return mixed
*/
abstract public function handle();
/**
* Check if user is authorized to perform the action
*
* @return mixed
*/
protected function authorized()
{
return true;
}
public function name()
{
return $this->name;
}
/**
* Handle the request if authorized to
*/
public function handler() {
if(($this->authOnly && !is_user_logged_in()) || !$this->authorized()) {
wp_send_json(['status' => 'error', 'message' => 'Unauthorized'], 401);
}
$this->handle();
}
}