1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Controllers\Api\Version1; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Contracts\ShortcodeContract; |
6
|
|
|
|
7
|
|
|
class RestShortcodeController extends \WP_REST_Controller |
8
|
|
|
{ |
9
|
|
|
public function __construct() |
10
|
|
|
{ |
11
|
|
|
$this->namespace = glsr()->id.'/v1'; |
12
|
|
|
$this->rest_base = 'shortcode'; |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param \WP_REST_Request $request |
17
|
|
|
* |
18
|
|
|
* @return \WP_REST_Response|\WP_Error |
19
|
|
|
*/ |
20
|
|
|
public function get_items($request) |
21
|
|
|
{ |
22
|
|
|
$shortcode = glsr()->shortcode($request['shortcode']); |
|
|
|
|
23
|
|
|
return rest_ensure_response( |
24
|
|
|
$shortcode->apiFetchResponse($request) |
|
|
|
|
25
|
|
|
); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param \WP_REST_Request $request |
30
|
|
|
* |
31
|
|
|
* @return true|\WP_Error |
32
|
|
|
*/ |
33
|
|
|
public function get_items_permissions_check($request) |
34
|
|
|
{ |
35
|
|
|
if (!is_user_logged_in()) { |
36
|
|
|
$error = _x('Sorry, you are not allowed to do that.', 'admin-text', 'site-reviews'); |
37
|
|
|
return new \WP_Error('rest_forbidden_context', $error, [ |
38
|
|
|
'status' => rest_authorization_required_code(), |
39
|
|
|
]); |
40
|
|
|
} |
41
|
|
|
if (!glsr()->shortcode($request['shortcode'])) { |
|
|
|
|
42
|
|
|
return new \WP_Error('rest_invalid_shortcode', 'Invalid shortcode', [ |
43
|
|
|
'status' => 400, |
44
|
|
|
]); |
45
|
|
|
} |
46
|
|
|
return true; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
public function register_routes() |
53
|
|
|
{ |
54
|
|
|
register_rest_route($this->namespace, "/{$this->rest_base}/(?P<shortcode>[a-z_]+)", [ |
55
|
|
|
[ |
56
|
|
|
'callback' => [$this, 'get_items'], |
57
|
|
|
'methods' => \WP_REST_Server::READABLE, |
58
|
|
|
'permission_callback' => [$this, 'get_items_permissions_check'], |
59
|
|
|
], |
60
|
|
|
]); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|