PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 3.4.1
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v3.4.1
4.7.2 4.7.1 4.7.0 4.6.6 4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 All 281 releases
surecart / app / src / Rest / RestServiceProvider.php
RestServiceProvider.php
323 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SureCart\Rest;
4
5 use SureCart\Models\Model;
6 use SureCart\Rest\RestServiceInterface;
7
8 /**
9 * Abstract Rest Service Provider interface
10 */
11 abstract class RestServiceProvider extends \WP_REST_Controller implements RestServiceInterface {
12 /**
13 * Mark specific properties that need additional permissions checks
14 * before modifying. We don't want customers being able to modify these.
15 *
16 * @var array
17 */
18 protected $property_permissions = [];
19
20 /**
21 * Plugin namespace.
22 *
23 * @var string
24 */
25 protected $name = 'surecart';
26
27 /**
28 * API Version
29 *
30 * @var string
31 */
32 protected $version = '1';
33
34 /**
35 * Endpoint.
36 *
37 * @var string
38 */
39 protected $endpoint = '';
40
41 /**
42 * Controller class
43 *
44 * @var string
45 */
46 protected $controller = '';
47
48 /**
49 * Methods allowed for the model.
50 *
51 * @var array
52 */
53 protected $methods = [ 'index', 'create', 'find', 'edit', 'delete' ];
54
55 /**
56 * {@inheritDoc}
57 *
58 * @param \Pimple\Container $container Service Container.
59 */
60 public function register( $container ) {
61 // nothing to register.
62 }
63
64 /**
65 * Bootstrap routes
66 *
67 * @param \Pimple\Container $container Service Container.
68 *
69 * @return void
70 */
71 public function bootstrap( $container ) {
72 add_action( 'rest_api_init', [ $this, 'registerModelRoutes' ] );
73 }
74
75 /**
76 * Do we have the method
77 *
78 * @param string $name
79 * @return boolean
80 */
81 public function hasMethod( $name ) {
82 return in_array( $name, $this->methods, true );
83 }
84
85 /**
86 * Do we have all these methods.
87 *
88 * @param array $methods Array of method names.
89 * @return boolean
90 */
91 public function hasAnyMethods( $methods = [] ) {
92 foreach ( $methods as $method ) {
93 if ( $this->hasMethod( $method ) ) {
94 return true;
95 }
96 }
97 return false;
98 }
99
100 /**
101 * Register REST Routes
102 *
103 * @return void
104 */
105 public function registerModelRoutes() {
106 $this->registerRoutes();
107 if ( $this->hasAnyMethods( [ 'index', 'create' ] ) ) {
108 register_rest_route(
109 "$this->name/v$this->version",
110 "$this->endpoint",
111 array_filter(
112 [
113 ( $this->hasMethod( 'index' ) ? [
114 'methods' => \WP_REST_Server::READABLE,
115 'callback' => $this->callback( $this->controller, 'index' ),
116 'permission_callback' => [ $this, 'get_items_permissions_check' ],
117 'args' => $this->get_collection_params(),
118 ] : [] ),
119 ( $this->hasMethod( 'create' ) ? [
120 'methods' => \WP_REST_Server::CREATABLE,
121 'callback' => $this->callback( $this->controller, 'create' ),
122 'permission_callback' => [ $this, 'create_item_permissions_check' ],
123 ] : [] ),
124 'schema' => [ $this, 'get_item_schema' ],
125 ]
126 )
127 );
128 }
129
130 if ( $this->hasAnyMethods( [ 'find', 'edit', 'delete' ] ) ) {
131 register_rest_route(
132 "$this->name/v$this->version",
133 $this->endpoint . '/(?P<id>[^/]+)',
134 array_filter(
135 [
136 ( $this->hasMethod( 'find' ) ? [
137 'methods' => \WP_REST_Server::READABLE,
138 'callback' => $this->callback( $this->controller, 'find' ),
139 'permission_callback' => [ $this, 'get_item_permissions_check' ],
140 ] : [] ),
141 ( $this->hasMethod( 'edit' ) ? [
142 'methods' => \WP_REST_Server::EDITABLE,
143 'callback' => $this->callback( $this->controller, 'edit' ),
144 'permission_callback' => [ $this, 'update_item_permissions_check' ],
145 ] : [] ),
146 ( $this->hasMethod( 'delete' ) ? [
147 'methods' => \WP_REST_Server::DELETABLE,
148 'callback' => $this->callback( $this->controller, 'delete' ),
149 'permission_callback' => [ $this, 'delete_item_permissions_check' ],
150 ] : [] ),
151 // Register our schema callback.
152 'schema' => [ $this, 'get_item_schema' ],
153 ]
154 )
155 );
156 }
157 }
158
159 /**
160 * Additional routes to register for the model.
161 *
162 * @return void
163 */
164 public function registerRoutes() {
165 }
166
167 /**
168 * Get our sample schema for a post.
169 *
170 * @return array The sample schema for a post
171 */
172 public function get_item_schema() {
173 if ( $this->schema ) {
174 // Since WordPress 5.3, the schema can be cached in the $schema property.
175 return $this->schema;
176 }
177
178 $this->schema = [
179 // This tells the spec of JSON Schema we are using which is draft 4.
180 '$schema' => 'http://json-schema.org/draft-04/schema#',
181 // The title property marks the identity of the resource.
182 'title' => $this->endpoint,
183 'type' => 'object',
184 // In JSON Schema you can specify object properties in the properties attribute.
185 'properties' => [
186 'id' => [
187 'description' => esc_html__( 'Unique identifier for the object.', 'surecart' ),
188 'type' => 'string',
189 'context' => [ 'view', 'edit', 'embed' ],
190 'readonly' => true,
191 ],
192 ],
193 ];
194
195 return $this->schema;
196 }
197
198 /**
199 * Process the callback for the route.
200 *
201 * @param string $class Class name.
202 * @param string $method Class method.
203 * @return callback
204 */
205 public function callback( $class, $method ) {
206 // litespeed caching bypass.
207 do_action( 'litespeed_control_set_nocache', 'surecart api request' );
208
209 return function ( $request ) use ( $class, $method ) {
210 // get and call controller with request.
211 $controller = \SureCart::closure()->method( $class, $method );
212 $model = $controller( $request );
213
214 // check and filter context.
215 $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
216
217 if ( is_wp_error( $model ) ) {
218 return $model;
219 }
220
221 // remove wp_created_by to prevent user ids from being leaked.
222 if ( 'edit' !== $context && ! empty( $model->metadata->wp_created_by ) ) {
223 unset( $model->metadata->wp_created_by );
224 }
225
226 $response = rest_ensure_response( $this->filter_response_by_context( is_a( $model, Model::class ) ? $model->toArray() : $model, $context ) );
227
228 if ( is_a( $model, Model::class ) && ! empty( $model->getCacheStatus() ) ) {
229 $response->header( 'X-SURECART-CACHE-STATUS', $model->getCacheStatus() );
230 }
231
232 return $response;
233 };
234 }
235
236 /**
237 * Check permissions for specific properties of the request.
238 *
239 * @param \WP_REST_Request $request Full details about the request.
240 * @param array $keys Keys to check.
241 *
242 * @return boolean
243 */
244 protected function requestOnlyHasKeys( $request, $keys ) {
245 $keys = array_merge( $keys, [ 'context', '_locale', 'rest_route', 'id', 'expand' ] );
246 foreach ( $request->get_params() as $key => $value ) {
247 if ( ! in_array( $key, $keys, true ) ) {
248 return false;
249 }
250 }
251 return true;
252 }
253
254 /**
255 * Retrieves the query params for collections.
256 *
257 * @return array
258 */
259 public function get_collection_params() {
260 return [];
261 }
262
263 /**
264 * Set these all as false by default
265 * in case parent class doesn't implement them.
266 *
267 * @param \WP_REST_Request $request Full details about the request.
268 *
269 * @return false
270 */
271 public function get_item_permissions_check( $request ) {
272 return false;
273 }
274
275 /**
276 * Set these all as false by default
277 * in case parent class doesn't implement them.
278 *
279 * @param \WP_REST_Request $request Full details about the request.
280 *
281 * @return false
282 */
283 public function get_items_permissions_check( $request ) {
284 return false;
285 }
286
287 /**
288 * Set these all as false by default
289 * in case parent class doesn't implement them.
290 *
291 * @param \WP_REST_Request $request Full details about the request.
292 *
293 * @return false
294 */
295 public function create_item_permissions_check( $request ) {
296 return false;
297 }
298
299 /**
300 * Set these all as false by default
301 * in case parent class doesn't implement them.
302 *
303 * @param \WP_REST_Request $request Full details about the request.
304 *
305 * @return false
306 */
307 public function update_item_permissions_check( $request ) {
308 return false;
309 }
310
311 /**
312 * Set these all as false by default
313 * in case parent class doesn't implement them.
314 *
315 * @param \WP_REST_Request $request Full details about the request.
316 *
317 * @return false
318 */
319 public function delete_item_permissions_check( $request ) {
320 return false;
321 }
322 }
323