|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Event Espresso |
|
4
|
|
|
* Manage events, sell tickets, and receive payments from your WordPress website. |
|
5
|
|
|
* Copyright (c) 2008-2019 Event Espresso All Rights Reserved. |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
8
|
|
|
* it under the terms of the GNU General Public License as published by |
|
9
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
10
|
|
|
* (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU General Public License |
|
18
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace EventEspresso\core\domain\services\converters; |
|
22
|
|
|
|
|
23
|
|
|
use DomainException; |
|
24
|
|
|
use EE_Base_Class; |
|
25
|
|
|
use EE_Error; |
|
26
|
|
|
use EED_Core_Rest_Api; |
|
27
|
|
|
use EEM_Base; |
|
28
|
|
|
use EventEspresso\core\exceptions\InvalidDataTypeException; |
|
29
|
|
|
use EventEspresso\core\exceptions\InvalidInterfaceException; |
|
30
|
|
|
use EventEspresso\core\exceptions\ModelConfigurationException; |
|
31
|
|
|
use EventEspresso\core\exceptions\RestPasswordIncorrectException; |
|
32
|
|
|
use EventEspresso\core\exceptions\RestPasswordRequiredException; |
|
33
|
|
|
use EventEspresso\core\exceptions\UnexpectedEntityException; |
|
34
|
|
|
use EventEspresso\core\libraries\rest_api\controllers\model\Read; |
|
35
|
|
|
use EventEspresso\core\libraries\rest_api\RestException; |
|
36
|
|
|
use InvalidArgumentException; |
|
37
|
|
|
use ReflectionException; |
|
38
|
|
|
use WP_REST_Request; |
|
39
|
|
|
use WP_REST_Server; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Class RestApiSpoofer |
|
43
|
|
|
* Description |
|
44
|
|
|
* |
|
45
|
|
|
* @package EventEspresso\core\domain\services\converters\spoofers |
|
46
|
|
|
* @author Brent Christensen |
|
47
|
|
|
* @since $VID:$ |
|
48
|
|
|
*/ |
|
49
|
|
|
class RestApiSpoofer |
|
50
|
|
|
{ |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var WP_REST_Server $wp_rest_server |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $wp_rest_server; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var Read |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $rest_controller; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var EED_Core_Rest_Api $rest_module |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $rest_module; |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* RestApiSpoofer constructor. |
|
70
|
|
|
* |
|
71
|
|
|
* @param WP_REST_Server $wp_rest_server |
|
72
|
|
|
* @param EED_Core_Rest_Api $rest_module |
|
73
|
|
|
* @param Read $rest_api |
|
74
|
|
|
* @param string $api_version |
|
75
|
|
|
*/ |
|
76
|
|
|
public function __construct( |
|
77
|
|
|
WP_REST_Server $wp_rest_server, |
|
78
|
|
|
EED_Core_Rest_Api $rest_module, |
|
79
|
|
|
Read $rest_api, |
|
80
|
|
|
$api_version = '4.8.36' |
|
81
|
|
|
) { |
|
82
|
|
|
$this->wp_rest_server = $wp_rest_server; |
|
83
|
|
|
$this->rest_module = $rest_module; |
|
84
|
|
|
$this->rest_controller = $rest_api; |
|
85
|
|
|
$this->rest_controller->setRequestedVersion($api_version); |
|
86
|
|
|
$this->setUpRestServer(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
private function setUpRestServer() |
|
91
|
|
|
{ |
|
92
|
|
|
/* @var WP_REST_Server $wp_rest_server */ |
|
93
|
|
|
global $wp_rest_server; |
|
94
|
|
|
$wp_rest_server = $this->wp_rest_server; |
|
95
|
|
|
EED_Core_Rest_Api::set_hooks_both(); |
|
96
|
|
|
do_action('rest_api_init', $this->wp_rest_server); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param EEM_Base $model |
|
101
|
|
|
* @param array $query_params |
|
102
|
|
|
* @param string $include |
|
103
|
|
|
* @return array |
|
104
|
|
|
* @throws EE_Error |
|
105
|
|
|
* @throws InvalidArgumentException |
|
106
|
|
|
* @throws InvalidDataTypeException |
|
107
|
|
|
* @throws InvalidInterfaceException |
|
108
|
|
|
* @throws ModelConfigurationException |
|
109
|
|
|
* @throws ReflectionException |
|
110
|
|
|
* @throws RestException |
|
111
|
|
|
* @throws RestPasswordIncorrectException |
|
112
|
|
|
* @throws RestPasswordRequiredException |
|
113
|
|
|
* @throws UnexpectedEntityException |
|
114
|
|
|
* @throws DomainException |
|
115
|
|
|
* @since $VID:$ |
|
116
|
|
|
*/ |
|
117
|
|
|
public function getOneApiResult(EEM_Base $model, array $query_params, $include = '') |
|
118
|
|
|
{ |
|
119
|
|
|
if ( ! array_key_exists('limit', $query_params)) { |
|
120
|
|
|
$query_params['limit'] = 1; |
|
121
|
|
|
} |
|
122
|
|
|
$result = $this->getApiResults($model, $query_params, $include); |
|
123
|
|
|
return is_array($result) && isset($result[0]) ? $result[0] : []; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param EEM_Base $model |
|
128
|
|
|
* @param array $query_params |
|
129
|
|
|
* @param string $include |
|
130
|
|
|
* @return array |
|
131
|
|
|
* @throws EE_Error |
|
132
|
|
|
* @throws InvalidArgumentException |
|
133
|
|
|
* @throws InvalidDataTypeException |
|
134
|
|
|
* @throws InvalidInterfaceException |
|
135
|
|
|
* @throws ModelConfigurationException |
|
136
|
|
|
* @throws ReflectionException |
|
137
|
|
|
* @throws RestException |
|
138
|
|
|
* @throws RestPasswordIncorrectException |
|
139
|
|
|
* @throws RestPasswordRequiredException |
|
140
|
|
|
* @throws UnexpectedEntityException |
|
141
|
|
|
* @throws DomainException |
|
142
|
|
|
* @since $VID:$ |
|
143
|
|
|
*/ |
|
144
|
|
|
public function getApiResults(EEM_Base $model, array $query_params, $include = '') |
|
145
|
|
|
{ |
|
146
|
|
|
if (! array_key_exists('caps', $query_params)) { |
|
147
|
|
|
$query_params['caps'] = EEM_Base::caps_read_admin; |
|
148
|
|
|
} |
|
149
|
|
|
/** @type array $results */ |
|
150
|
|
|
$results = $model->get_all_wpdb_results($query_params); |
|
151
|
|
|
$rest_request = new WP_REST_Request(); |
|
152
|
|
|
$rest_request->set_param('include', $include); |
|
153
|
|
|
$rest_request->set_param('caps', 'edit'); |
|
154
|
|
|
$nice_results = array(); |
|
155
|
|
|
foreach ($results as $result) { |
|
156
|
|
|
$nice_results[] = $this->rest_controller->createEntityFromWpdbResult( |
|
157
|
|
|
$model, |
|
158
|
|
|
$result, |
|
159
|
|
|
$rest_request |
|
160
|
|
|
); |
|
161
|
|
|
} |
|
162
|
|
|
return $nice_results; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @param string $endpoint |
|
168
|
|
|
* @return array |
|
169
|
|
|
* @throws EE_Error |
|
170
|
|
|
* @since $VID:$ |
|
171
|
|
|
*/ |
|
172
|
|
|
public function getModelSchema($endpoint) |
|
173
|
|
|
{ |
|
174
|
|
|
$response = $this->wp_rest_server->dispatch( |
|
175
|
|
|
new WP_REST_Request( |
|
176
|
|
|
'OPTIONS', |
|
177
|
|
|
"/ee/v4.8.36/{$endpoint}" |
|
178
|
|
|
) |
|
179
|
|
|
); |
|
180
|
|
|
return $response->get_data(); |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|