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 / Controllers / Rest / PricesController.php
PricesController.php
51 lines 1.4 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\Price;
6 use SureCart\Models\Product;
7
8 /**
9 * Handle Price requests through the REST API
10 */
11 class PricesController extends RestController {
12 /**
13 * Class to make the requests.
14 *
15 * @var string
16 */
17 protected $class = Price::class;
18
19 /**
20 * Run some middleware to run before request.
21 *
22 * @param \SureCart\Models\Model $class Model class instance.
23 * @param \WP_REST_Request $request Request object.
24 *
25 * @return \SureCart\Models\Model
26 */
27 protected function middleware( $class, \WP_REST_Request $request ) {
28 // get the expands from the product for syncing.
29 $expands = array_merge( [ 'product' ], array_map( fn( $expand ) => strpos( $expand, '.' ) !== false ? $expand : 'product.' . $expand, Product::getSyncExpands() ) );
30 // If we are updating or creating, always return the product for syncing.
31 if ( in_array( $request->get_method(), [ 'POST', 'PUT', 'PATCH', 'DELETE' ] ) ) {
32 $class->with( array_unique( array_filter( array_merge( $expands, $request['expand'] ?? [] ) ) ) );
33 }
34 return parent::middleware( $class, $request );
35 }
36
37 /**
38 * Duplicate model.
39 *
40 * @param \WP_REST_Request $request Rest Request.
41 * @return \WP_REST_Response|\WP_Error
42 */
43 public function duplicate( \WP_REST_Request $request ) {
44 $model = $this->middleware( new $this->class(), $request );
45 if ( is_wp_error( $model ) ) {
46 return $model;
47 }
48 return $model->duplicate( $request['id'] );
49 }
50 }
51