/var/www/html_us/wp-content/plugins/elementor/data/base/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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php

namespace Elementor\Data\Base;

use 
Elementor\Data\Manager;
use 
WP_REST_Server;

abstract class 
Endpoint {

    const 
AVAILABLE_METHODS = [
        
WP_REST_Server::READABLE,
        
WP_REST_Server::CREATABLE,
        
WP_REST_Server::EDITABLE,
        
WP_REST_Server::DELETABLE,
        
WP_REST_Server::ALLMETHODS,
    ];

    
/**
     * Controller of current endpoint.
     *
     * @var \Elementor\Data\Base\Controller
     */
    
protected $controller;

    
/**
     * Loaded sub endpoint(s).
     *
     * @var \Elementor\Data\Base\SubEndpoint[]
     */
    
private $sub_endpoints = [];

    
/**
     * Get format suffix.
     *
     * Examples:
     * '{one_parameter_name}'.
     * '{one_parameter_name}/{two_parameter_name}/'.
     * '{one_parameter_name}/whatever/anything/{two_parameter_name}/' and so on for each endpoint or sub-endpoint.
     *
     * @return string current location will later be added automatically.
     */
    
public static function get_format() {
        return 
'';
    }

    
/**
     * Endpoint constructor.
     *
     * run `$this->>register()`.
     *
     * @param \Elementor\Data\Base\Controller $controller
     *
     * @throws \Exception
     */
    
public function __construct$controller ) {
        if ( ! ( 
$controller instanceof Controller ) ) {
            throw new 
\Exception'Invalid controller.' );
        }

        
$this->controller $controller;
        
$this->register();
    }

    
/**
     * Get endpoint name.
     *
     * @return string
     */
    
abstract public function get_name();

    
/**
     * Get base route.
     *
     * Removing 'index' from endpoint.
     *
     * @return string
     */
    
public function get_base_route() {
        
$endpoint_name $this->get_name();

        
// TODO: Allow this only for internal routes.
        // TODO: Make difference between internal and external endpoints.
        
if ( 'index' === $endpoint_name ) {
            
$endpoint_name '';
        }

        return 
'/' $this->controller->get_rest_base() . '/' $endpoint_name;
    }

    
/**
     * Register the endpoint.
     *
     * By default: register get items route.
     *
     * @throws \Exception
     */
    
protected function register() {
        
$this->register_items_route();
    }

    
/**
     * Register sub endpoint.
     *
     * @param string $route
     * @param string $endpoint_class
     *
     * @return \Elementor\Data\Base\SubEndpoint
     * @throws \Exception
     */
    
protected function register_sub_endpoint$route$endpoint_class ) {
        
$endpoint_instance = new $endpoint_class$route$this );

        if ( ! ( 
$endpoint_instance instanceof SubEndpoint ) ) {
            throw new 
\Exception'Invalid endpoint instance.' );
        }

        
$endpoint_route $route '/' $endpoint_instance->get_name();

        
$this->sub_endpoints$endpoint_route ] = $endpoint_instance;

        
$component_name $endpoint_instance->controller->get_rest_base();
        
$parent_instance $endpoint_instance->get_parent();
        
$parent_name $endpoint_instance->get_name();
        
$parent_format_suffix $parent_instance::get_format();
        
$current_format_suffix $endpoint_instance::get_format();

        
$command $component_name '/' $parent_name;
        
$format $component_name '/' $parent_format_suffix '/' $parent_name '/' $current_format_suffix;

        
Manager::instance()->register_endpoint_format$command$format );

        return 
$endpoint_instance;
    }

    
/**
     * Base callback.
     *
     * All reset requests from the client should pass this function.
     *
     * @param string $methods
     * @param \WP_REST_Request $request
     * @param bool $is_multi
     *
     * @return mixed|\WP_Error|\WP_HTTP_Response|\WP_REST_Response
     * @throws \Exception
     */
    
public function base_callback$methods$request$is_multi false ) {
        
// TODO: Find better solution.
        
$json_params $request->get_json_params();

        if ( 
$json_params ) {
            
$request->set_body_params$json_params );
        }

        
// TODO: Handle permission callback.
        
switch ( $methods ) {
            case 
WP_REST_Server::READABLE:
                
$result $is_multi $this->get_items$request ) : $this->get_item$request->get_param'id' ), $request );
                break;

            case 
WP_REST_Server::CREATABLE:
                
$result $is_multi $this->create_items$request ) : $this->create_item$request->get_param'id' ), $request );
                break;

            case 
WP_REST_Server::EDITABLE:
                
$result $is_multi $this->update_items$request ) : $this->update_item$request->get_param'id' ), $request );
                break;

            case 
WP_REST_Server::DELETABLE:
                
$result $is_multi $this->delete_items$request ) : $this->delete_item$request->get_param'id' ), $request );
                break;

            default:
                throw new 
\Exception'Invalid method.' );
        }

        return 
rest_ensure_response$result );
    }

    
/**
     * Retrieves a collection of items.
     *
     * @param \WP_REST_Request $request Full data about the request.
     *
     * @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure.
     */
    
public function get_items$request ) {
        return 
$this->controller->get_items$request );
    }

    
/**
     * Retrieves one item from the collection.
     *
     * @param string $id
     * @param \WP_REST_Request $request Full data about the request.
     *
     * @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure.
     */
    
public function get_item$id$request ) {
        return 
$this->controller->get_item$request );
    }

    
/**
     * Get permission callback.
     *
     * By default get permission callback from the controller.
     *
     * @param \WP_REST_Request $request Full data about the request.
     *
     * @return boolean
     */
    
public function get_permission_callback$request ) {
        return 
$this->controller->get_permission_callback$request );
    }

    
/**
     * Creates one item.
     *
     * @param string $id id of request item.
     * @param \WP_REST_Request $request Full data about the request.
     *
     * @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure.
     */
    
public function create_item$id$request ) {
        return 
$this->controller->create_item$request );
    }

    
/**
     * Creates multiple items.
     *
     * @param \WP_REST_Request $request Full data about the request.
     *
     * @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure.
     */
    
public function create_items$request ) {
        return 
$this->controller->create_items$request );
    }

    
/**
     * Updates one item.
     *
     * @param string $id id of request item.
     * @param \WP_REST_Request $request Full data about the request.
     *
     * @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure.
     */
    
public function update_item$id$request ) {
        return 
$this->controller->update_item$request );
    }

    
/**
     * Updates multiple items.
     *
     * @param \WP_REST_Request $request Full data about the request.
     *
     * @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure.
     */
    
public function update_items$request ) {
        return 
$this->controller->update_items$request );
    }

    
/**
     * Delete one item.
     *
     * @param string $id id of request item.
     * @param \WP_REST_Request $request Full data about the request.
     *
     * @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure.
     */
    
public function delete_item$id$request ) {
        return 
$this->controller->delete_item$request );
    }

    
/**
     * Delete multiple items.
     *
     * @param \WP_REST_Request $request Full data about the request.
     *
     * @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure.
     */
    
public function delete_items$request ) {
        return 
$this->controller->delete_items$request );
    }

    
/**
     * Register item route.
     *
     * @param array $args
     * @param string $route
     * @param string $methods
     *
     * @throws \Exception
     */
    
public function register_item_route$methods WP_REST_Server::READABLE$args = [], $route '/' ) {
        
$args array_merge( [
            
'id' => [
                
'description' => 'Unique identifier for the object.',
                
'type' => 'string',
            ],
        ], 
$args );

        if ( isset( 
$args['id'] ) && $args['id'] ) {
            
$route .= '(?P<id>[\w]+)/';
        }

        
$this->register_route$route$methods, function ( $request ) use ( $methods ) {
            return 
$this->base_callback$methods$request );
        }, 
$args );
    }

    
/**
     * Register items route.
     *
     * @param string $methods
     *
     * @throws \Exception
     */
    
public function register_items_route$methods WP_REST_Server::READABLE ) {
        
$this->register_route''$methods, function ( $request ) use ( $methods ) {
            return 
$this->base_callback$methods$requesttrue );
        } );
    }

    
/**
     * Register route.
     *
     * @param string $route
     * @param string $methods
     * @param null $callback
     * @param array $args
     *
     * @return bool
     * @throws \Exception
     */
    
public function register_route$route ''$methods WP_REST_Server::READABLE$callback null$args = [] ) {
        if ( ! 
in_array$methodsself::AVAILABLE_METHODStrue ) ) {
            throw new 
\Exception'Invalid method.' );
        }

        
$route $this->get_base_route() . $route;

        return 
register_rest_route$this->controller->get_namespace(), $route, [
            [
                
'args' => $args,
                
'methods' => $methods,
                
'callback' => $callback,
                
'permission_callback' => function ( $request ) {
                    return 
$this->get_permission_callback$request );
                },
            ],
        ] );
    }
}