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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
<?php /** * WPSEO plugin file. * * @package WPSEO\Internals */
/** * Class WPSEO_Endpoint_Factory. */ class WPSEO_Endpoint_Factory {
/** * The valid HTTP methods. * * @var array */ private $valid_http_methods = array( 'GET', 'PATCH', 'POST', 'PUT', 'DELETE', );
/** * The arguments. * * @var array */ protected $args = array();
/** * The namespace. * * @var string */ private $namespace;
/** * The endpoint URL. * * @var string */ private $endpoint;
/** * The callback to execute if the endpoint is called. * * @var callable */ private $callback;
/** * The permission callback to execute to determine permissions. * * @var callable */ private $permission_callback;
/** * The HTTP method to use. * * @var string */ private $method;
/** * WPSEO_Endpoint_Factory constructor. * * @param string $namespace The endpoint's namespace. * @param string $endpoint The endpoint's URL. * @param callable $callback The callback function to execute. * @param callable $permission_callback The permission callback to execute to determine permissions. * @param string $method The HTTP method to use. Defaults to GET. * * @throws WPSEO_Invalid_Argument_Exception The invalid argument exception. */ public function __construct( $namespace, $endpoint, $callback, $permission_callback, $method = WP_REST_Server::READABLE ) { if ( ! WPSEO_Validator::is_string( $namespace ) ) { throw WPSEO_Invalid_Argument_Exception::invalid_string_parameter( $namespace, 'namespace' ); }
$this->namespace = $namespace;
if ( ! WPSEO_Validator::is_string( $endpoint ) ) { throw WPSEO_Invalid_Argument_Exception::invalid_string_parameter( $endpoint, 'endpoint' ); }
$this->endpoint = $endpoint;
if ( ! is_callable( $callback ) ) { throw WPSEO_Invalid_Argument_Exception::invalid_callable_parameter( $callback, 'callback' ); }
$this->callback = $callback;
if ( ! is_callable( $permission_callback ) ) { throw WPSEO_Invalid_Argument_Exception::invalid_callable_parameter( $permission_callback, 'callback' ); }
$this->permission_callback = $permission_callback;
$this->method = $this->validate_method( $method ); }
/** * Gets the associated arguments. * * @return array The arguments. */ public function get_arguments() { return $this->args; }
/** * Determines whether or not there are any arguments present. * * @return bool Whether or not any arguments are present. */ public function has_arguments() { return count( $this->args ) > 0; }
/** * Registers the endpoint with WordPress. * * @return void */ public function register() { $config = array( 'methods' => $this->method, 'callback' => $this->callback, 'permission_callback' => $this->permission_callback, );
if ( $this->has_arguments() ) { $config['args'] = $this->args; }
register_rest_route( $this->namespace, $this->endpoint, $config ); }
/** * Validates the method parameter. * * @param string $method The set method parameter. * * @return string The validated method. * * @throws WPSEO_Invalid_Argument_Exception The invalid argument exception. * @throws InvalidArgumentException The invalid argument exception. */ protected function validate_method( $method ) { if ( ! WPSEO_Validator::is_string( $method ) ) { throw WPSEO_Invalid_Argument_Exception::invalid_string_parameter( $method, 'method' ); }
if ( ! in_array( $method, $this->valid_http_methods, true ) ) { throw new InvalidArgumentException( sprintf( '%s is not a valid HTTP method', $method ) ); }
return $method; }
/** * Adds an argument to the endpoint. * * @param string $name The name of the argument. * @param string $description The description associated with the argument. * @param string $type The type of value that can be assigned to the argument. * @param bool $required Whether or not it's a required argument. Defaults to true. * * @return void */ protected function add_argument( $name, $description, $type, $required = true ) { if ( in_array( $name, array_keys( $this->args ), true ) ) { return; }
$this->args[ $name ] = array( 'description' => $description, 'type' => $type, 'required' => $required, ); } }
|