PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 4.2.1
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v4.2.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 / Controllers / Rest / ProductsController.php
ProductsController.php
105 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SureCart\Controllers\Rest;
4
5 use SureCart\Models\Product;
6
7 /**
8 * Handle Product requests through the REST API
9 */
10 class ProductsController extends RestController {
11 /**
12 * Class to make the requests.
13 *
14 * @var string
15 */
16 protected $class = Product::class;
17
18 /**
19 * Run some middleware to run before request.
20 *
21 * @param \SureCart\Models\Model $class Model class instance.
22 * @param \WP_REST_Request $request Request object.
23 *
24 * @return \SureCart\Models\Model
25 */
26 protected function middleware( $class, \WP_REST_Request $request ) {
27 // if we are in edit context, we want to fetch the variants, variant options and prices.
28 if ( 'edit' === $request->get_param( 'context' ) || in_array( $request->get_method(), [ 'POST', 'PUT', 'PATCH', 'DELETE' ] ) ) {
29 $class->with( array_unique( array_filter( array_merge( [ 'variants', 'variant_options', 'variants.image', 'prices', 'product_collections', 'commission_structure', 'product_medias', 'product_media.media' ], $request['expand'] ?? [] ) ) ) );
30 }
31 return parent::middleware( $class, $request );
32 }
33
34 /**
35 * Edit model.
36 *
37 * Filter out variations which statuses are draft.
38 *
39 * @param \WP_REST_Request $request Rest Request.
40 *
41 * @return \WP_REST_Response|\WP_Error
42 */
43 public function edit( \WP_REST_Request $request ) {
44 // if cataloged_at is set for future date, return error.
45 if ( ! empty( $request['cataloged_at'] ) && $request['cataloged_at'] > time() ) {
46 return new \WP_Error( 'invalid_cataloged_at', __( 'The Cataloged at date cannot be in the future. Please provide a valid date.', 'surecart' ), [ 'status' => 400 ] );
47 }
48
49 // Stop if we are not editing a variable product.
50 if ( empty( $request['variants'] ) ) {
51 return parent::edit( $request );
52 }
53
54 // Filter draft variations.
55 $request['variants'] = array_values(
56 array_filter(
57 $request['variants'],
58 function ( $variation ) {
59 return ! in_array( $variation['status'] ?? 'publish', [ 'draft', 'deleted' ] );
60 }
61 )
62 );
63
64 return parent::edit( $request );
65 }
66
67 /**
68 * Sync model.
69 *
70 * @param \WP_REST_Request $request Rest Request.
71 *
72 * @return \WP_REST_Response|\WP_Error
73 */
74 public function sync( \WP_REST_Request $request ) {
75 $model = $this->middleware( new $this->class(), $request );
76 if ( is_wp_error( $model ) ) {
77 return $model;
78 }
79 return $model->sync( $request['id'] );
80 }
81
82 /**
83 * Sync model.
84 *
85 * @return \WP_REST_Response|\WP_Error
86 */
87 public function syncAll() {
88 return \SureCart::sync()->products()->dispatch();
89 }
90
91 /**
92 * Duplicate model.
93 *
94 * @param \WP_REST_Request $request Rest Request.
95 * @return \WP_REST_Response|\WP_Error
96 */
97 public function duplicate( \WP_REST_Request $request ) {
98 $model = $this->middleware( new $this->class(), $request );
99 if ( is_wp_error( $model ) ) {
100 return $model;
101 }
102 return $model->duplicate( $request['id'] );
103 }
104 }
105