wistia-embed-permission-route.php
3.68 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
namespace Yoast\WP\SEO\Introductions\User_Interface;
use Exception;
use WP_Error;
use WP_REST_Request;
use WP_REST_Response;
use Yoast\WP\SEO\Conditionals\No_Conditionals;
use Yoast\WP\SEO\Helpers\User_Helper;
use Yoast\WP\SEO\Introductions\Infrastructure\Wistia_Embed_Permission_Repository;
use Yoast\WP\SEO\Main;
use Yoast\WP\SEO\Routes\Route_Interface;
/**
* Registers a route to offer get/set of the wistia embed permission for a user.
*
* @makePublic
*/
class Wistia_Embed_Permission_Route implements Route_Interface {
use No_Conditionals;
/**
* Represents the prefix.
*
* @var string
*/
const ROUTE_PREFIX = '/wistia_embed_permission';
/**
* Holds the repository.
*
* @var Wistia_Embed_Permission_Repository
*/
private $wistia_embed_permission_repository;
/**
* Holds the user helper.
*
* @var User_Helper
*/
private $user_helper;
/**
* Constructs the class.
*
* @param Wistia_Embed_Permission_Repository $wistia_embed_permission_repository The repository.
* @param User_Helper $user_helper The user helper.
*/
public function __construct(
Wistia_Embed_Permission_Repository $wistia_embed_permission_repository,
User_Helper $user_helper
) {
$this->wistia_embed_permission_repository = $wistia_embed_permission_repository;
$this->user_helper = $user_helper;
}
/**
* Registers routes with WordPress.
*
* @return void
*/
public function register_routes() {
\register_rest_route(
Main::API_V1_NAMESPACE,
self::ROUTE_PREFIX,
[
[
'methods' => 'GET',
'callback' => [ $this, 'get_wistia_embed_permission' ],
'permission_callback' => [ $this, 'permission_edit_posts' ],
],
[
'methods' => 'POST',
'callback' => [ $this, 'set_wistia_embed_permission' ],
'permission_callback' => [ $this, 'permission_edit_posts' ],
'args' => [
'value' => [
'required' => false,
'type' => 'bool',
'default' => true,
],
],
],
]
);
}
/**
* Gets the value of the wistia embed permission.
*
* @return WP_REST_Response|WP_Error The response, or an error.
*/
public function get_wistia_embed_permission() {
try {
$user_id = $this->user_helper->get_current_user_id();
$value = $this->wistia_embed_permission_repository->get_value_for_user( $user_id );
} catch ( Exception $exception ) {
return new WP_Error(
'wpseo_wistia_embed_permission_error',
$exception->getMessage(),
(object) []
);
}
return new WP_REST_Response(
[
'json' => (object) [
'value' => $value,
],
]
);
}
/**
* Sets the value of the wistia embed permission.
*
* @param WP_REST_Request $request The request object.
*
* @return WP_REST_Response|WP_Error The success or failure response.
*/
public function set_wistia_embed_permission( WP_REST_Request $request ) {
$params = $request->get_json_params();
$value = \boolval( $params['value'] );
try {
$user_id = $this->user_helper->get_current_user_id();
$result = $this->wistia_embed_permission_repository->set_value_for_user( $user_id, $value );
} catch ( Exception $exception ) {
return new WP_Error(
'wpseo_wistia_embed_permission_error',
$exception->getMessage(),
(object) []
);
}
return new WP_REST_Response(
[
'json' => (object) [
'success' => $result,
],
],
( $result ) ? 200 : 400
);
}
/**
* Permission callback.
*
* @return bool True when user has 'edit_posts' permission.
*/
public function permission_edit_posts() {
return \current_user_can( 'edit_posts' );
}
}