PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 4.4.2
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v4.4.2
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 / ProductsRestServiceProvider.php
ProductsRestServiceProvider.php
310 lines 9.3 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\Rest\RestServiceInterface;
6 use SureCart\Controllers\Rest\ProductsController;
7
8 /**
9 * Service provider for Price Rest Requests
10 */
11 class ProductsRestServiceProvider extends RestServiceProvider implements RestServiceInterface {
12 /**
13 * Endpoint.
14 *
15 * @var string
16 */
17 protected $endpoint = 'products';
18
19 /**
20 * Rest Controller
21 *
22 * @var string
23 */
24 protected $controller = ProductsController::class;
25
26 /**
27 * Register Additional REST Routes
28 *
29 * @return void
30 */
31 public function registerRoutes() {
32 register_rest_route(
33 "$this->name/v$this->version",
34 $this->endpoint . '/(?P<id>\S+)/sync/',
35 [
36 [
37 'methods' => \WP_REST_Server::EDITABLE,
38 'callback' => $this->callback( $this->controller, 'sync' ),
39 'permission_callback' => [ $this, 'update_item_permissions_check' ],
40 ],
41 // Register our schema callback.
42 'schema' => [ $this, 'get_item_schema' ],
43 ]
44 );
45 register_rest_route(
46 "$this->name/v$this->version",
47 $this->endpoint . '/sync_all',
48 [
49 [
50 'methods' => \WP_REST_Server::EDITABLE,
51 'callback' => $this->callback( $this->controller, 'syncAll' ),
52 'permission_callback' => [ $this, 'update_item_permissions_check' ],
53 ],
54 // Register our schema callback.
55 'schema' => [ $this, 'get_item_schema' ],
56 ]
57 );
58 register_rest_route(
59 "$this->name/v$this->version",
60 $this->endpoint . '/(?P<id>\S+)/duplicate',
61 [
62 'methods' => \WP_REST_Server::CREATABLE,
63 'callback' => $this->callback( $this->controller, 'duplicate' ),
64 'permission_callback' => [ $this, 'create_item_permissions_check' ],
65 ]
66 );
67 register_rest_route(
68 "$this->name/v$this->version",
69 $this->endpoint . '/import_woocommerce',
70 [
71 [
72 'methods' => \WP_REST_Server::CREATABLE,
73 'callback' => $this->callback( $this->controller, 'importWooCommerce' ),
74 'permission_callback' => [ $this, 'update_item_permissions_check' ],
75 ],
76 ]
77 );
78 register_rest_route(
79 "$this->name/v$this->version",
80 $this->endpoint . '/woocommerce_count',
81 [
82 [
83 'methods' => \WP_REST_Server::READABLE,
84 'callback' => $this->callback( $this->controller, 'wooCommerceCount' ),
85 // Uses edit-level permission (edit_sc_products) intentionally: admin-only import feature, only product editors should see the WooCommerce count.
86 'permission_callback' => [ $this, 'update_item_permissions_check' ],
87 ],
88 ]
89 );
90 }
91
92 /**
93 * Get our sample schema for a post.
94 *
95 * @return array The sample schema for a post
96 */
97 public function get_item_schema() {
98 if ( $this->schema ) {
99 // Since WordPress 5.3, the schema can be cached in the $schema property.
100 return $this->schema;
101 }
102
103 $this->schema = [
104 // This tells the spec of JSON Schema we are using which is draft 4.
105 '$schema' => 'http://json-schema.org/draft-04/schema#',
106 // The title property marks the identity of the resource.
107 'title' => $this->endpoint,
108 'type' => 'object',
109 // In JSON Schema you can specify object properties in the properties attribute.
110 'properties' => [
111 'id' => [
112 'description' => esc_html__( 'Unique identifier for the object.', 'surecart' ),
113 'type' => 'string',
114 'context' => [ 'view', 'edit', 'embed' ],
115 'readonly' => true,
116 ],
117 'file_upload_ids' => [
118 'description' => esc_html__( 'Files attached to the product.', 'surecart' ),
119 'context' => [ 'edit' ],
120 'type' => 'array',
121 'items' => [
122 'type' => 'string',
123 ],
124 ],
125 'metadata' => [
126 'description' => esc_html__( 'Stored product metadata', 'surecart' ),
127 'type' => 'object',
128 'properties' => [
129 'wp_created_by' => [
130 'type' => 'integer',
131 'context' => [ 'edit' ],
132 'readonly' => true,
133 ],
134 ],
135 ],
136 'metrics' => [
137 'description' => esc_html__( 'Top level metrics for the product.', 'surecart' ),
138 'readonly' => true,
139 'type' => 'object',
140 'properties' => [
141 'currency' => [
142 'type' => 'string',
143 ],
144 'max_price_amount' => [
145 'type' => 'integer',
146 ],
147 'min_price_amount' => [
148 'type' => 'integer',
149 ],
150 'prices_count' => [
151 'type' => 'integer',
152 ],
153 ],
154 'context' => [ 'edit' ],
155 ],
156 ],
157 ];
158
159 return $this->schema;
160 }
161
162 /**
163 * Get the collection params.
164 *
165 * @return array
166 */
167 public function get_collection_params() {
168 return [
169 'archived' => [
170 'description' => esc_html__( 'Whether to get archived products or not.', 'surecart' ),
171 'type' => 'boolean',
172 ],
173 'recurring' => [
174 'description' => esc_html__( 'Only return products that are recurring or not recurring (one time).', 'surecart' ),
175 'type' => 'boolean',
176 ],
177 'query' => [
178 'description' => __( 'The query to be used for full text search of this collection.', 'surecart' ),
179 'type' => 'string',
180 ],
181 'ids' => [
182 'description' => __( 'Ensure result set excludes specific IDs.', 'surecart' ),
183 'type' => 'array',
184 'items' => [
185 'type' => 'string',
186 ],
187 'default' => [],
188 ],
189 'product_group_ids' => [
190 'description' => __( 'Only return objects that belong to the given product groups.', 'surecart' ),
191 'type' => 'array',
192 'items' => [
193 'type' => 'string',
194 ],
195 'default' => [],
196 ],
197 'product_collection_ids' => [
198 'description' => __( 'Only return objects that belong to the given product collections.', 'surecart' ),
199 'type' => 'array',
200 'items' => [
201 'type' => 'string',
202 ],
203 'default' => [],
204 ],
205 'page' => [
206 'description' => esc_html__( 'The page of items you want returned.', 'surecart' ),
207 'type' => 'integer',
208 ],
209 'per_page' => [
210 'description' => esc_html__( 'A limit on the number of items to be returned, between 1 and 100.', 'surecart' ),
211 'type' => 'integer',
212 ],
213 ];
214 }
215
216 /**
217 * Anyone can get a specific product.
218 *
219 * @param \WP_REST_Request $request Full details about the request.
220 * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise.
221 */
222 public function get_item_permissions_check( $request ) {
223 if ( 'edit' === $request['context'] && ! current_user_can( 'edit_sc_products' ) ) {
224 return new \WP_Error(
225 'rest_forbidden_context',
226 __( 'Sorry, you are not allowed to edit products.', 'surecart' ),
227 array( 'status' => rest_authorization_required_code() )
228 );
229 }
230
231 return true;
232 }
233
234 /**
235 * Who can list products?
236 *
237 * @param \WP_REST_Request $request Full details about the request.
238 * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise.
239 */
240 public function get_items_permissions_check( $request ) {
241 if ( 'edit' === $request['context'] && ! current_user_can( 'edit_sc_products' ) ) {
242 return new \WP_Error(
243 'rest_forbidden_context',
244 __( 'Sorry, you are not allowed to edit products.', 'surecart' ),
245 array( 'status' => rest_authorization_required_code() )
246 );
247 }
248
249 if ( $request['archived'] ) {
250 return current_user_can( 'edit_sc_products' );
251 }
252
253 return true;
254 }
255
256 /**
257 * Create model.
258 *
259 * @param \WP_REST_Request $request Full details about the request.
260 * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise.
261 */
262 public function create_item_permissions_check( $request ) {
263 $request['context'] = 'edit';
264 return current_user_can( 'publish_sc_products' );
265 }
266
267 /**
268 * Update model.
269 *
270 * @param \WP_REST_Request $request Full details about the request.
271 * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise.
272 */
273 public function update_item_permissions_check( $request ) {
274 $request['context'] = 'edit';
275 return current_user_can( 'edit_sc_products' );
276 }
277
278 /**
279 * Delete model.
280 *
281 * @param \WP_REST_Request $request Full details about the request.
282 * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise.
283 */
284 public function delete_item_permissions_check( $request ) {
285 $request['context'] = 'edit';
286 return current_user_can( 'delete_sc_products' );
287 }
288
289 /**
290 * If we are editing, let's make sure the data comes back directly.
291 *
292 * @param \SureCart\Models\Product $model Product model.
293 * @param string $context The context of the request.
294 *
295 * @return array The filtered response.
296 */
297 public function filter_response_by_context( $model, $context ) {
298 $response = parent::filter_response_by_context( $model, $context );
299
300 if ( 'edit' === $context && is_array( $response ) && ! empty( $response['id'] ) ) {
301 // Process the variants, it's in a data column, so we need to pull it out.
302 $response['variants'] = ! empty( $response['variants']['data'] ) ? $response['variants']['data'] : [];
303 // Process the variant_options, it's in a data column, so we need to pull it out.
304 $response['variant_options'] = ! empty( $response['variant_options']['data'] ) ? $response['variant_options']['data'] : [];
305 }
306
307 return $response;
308 }
309 }
310