PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 2.8.1
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v2.8.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 / IntegrationsController.php
IntegrationsController.php
121 lines 3.0 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\Integration;
6
7 /**
8 * Handle Price requests through the REST API
9 */
10 class IntegrationsController extends RestController {
11 /**
12 * The model class.
13 *
14 * @var string
15 */
16 protected $class = Integration::class;
17
18 /**
19 * Create a product integration.
20 *
21 * @param \WP_REST_Request $request Rest Request.
22 *
23 * @return \WP_REST_Response|\WP_Error
24 */
25 public function create( \WP_REST_Request $request ) {
26 do_action( 'surecart/integrations/create', $request->get_params() );
27 return Integration::create( $request->get_params() );
28 }
29
30 /**
31 * List all product integrations.
32 *
33 * @param \WP_REST_Request $request Rest Request.
34 *
35 * @return \WP_REST_Response|\WP_Error
36 */
37 public function index( \WP_REST_Request $request ) {
38 $integration = new Integration();
39
40 // integration model ids.
41 if ( $request->get_param( 'integration_ids' ) ) {
42 $integration = $integration->whereIn( 'integration_id', $request->get_param( 'integration_ids' ) );
43 }
44
45 // model ids.
46 if ( $request->get_param( 'model_ids' ) ) {
47 $integration = $integration->whereIn( 'model_id', $request->get_param( 'model_ids' ) );
48 }
49
50 $total = $integration->count();
51 $page = $request->get_param( 'page' ) ? $request->get_param( 'page' ) : 1;
52 $per_page = $request->get_param( 'per_page' ) ? $request->get_param( 'per_page' ) : 10;
53
54 // handle pagination.
55 $items = $integration->paginate(
56 [
57 'page' => $page,
58 'per_page' => $per_page,
59 ]
60 );
61
62 $response = rest_ensure_response( $items );
63 $response->header( 'X-WP-Total', (int) $total );
64 $response->header( 'X-WP-TotalPages', (int) ceil( $total / (int) $per_page ) );
65
66 return $response;
67 }
68
69 /**
70 * Find a specific product integration.
71 *
72 * @param \WP_REST_Request $request Rest Request.
73 *
74 * @return \WP_REST_Response|\WP_Error
75 */
76 public function find( \WP_REST_Request $request ) {
77 $model = $this->middleware( new $this->class(), $request );
78 if ( is_wp_error( $model ) ) {
79 return $model;
80 }
81
82 return $model->where(
83 array_filter(
84 [
85 'model_id' => $request->get_param( 'model_id' ),
86 'integration_id' => $request->get_param( 'integration_id' ),
87 'model_name' => $request->get_param( 'model_name' ),
88 'provider' => $request->get_param( 'provider' ),
89 ]
90 )
91 )->find( $request['id'] );
92 }
93
94 /**
95 * Edit model.
96 *
97 * @param \WP_REST_Request $request Rest Request.
98 *
99 * @return \WP_REST_Response|\WP_Error
100 */
101 public function edit( \WP_REST_Request $request ) {
102 $model = $this->middleware( new $this->class( $request['id'] ), $request );
103 if ( is_wp_error( $model ) ) {
104 return $model;
105 }
106 return $model->where( $request->get_query_params() )->update( $request->get_json_params() );
107 }
108
109 /**
110 * Delete model.
111 *
112 * @param \WP_REST_Request $request Rest Request.
113 *
114 * @return \WP_REST_Response|\WP_Error
115 */
116 public function delete( \WP_REST_Request $request ) {
117 do_action( 'surecart/integrations/delete', $request->get_params() );
118 return ( new $this->class() )->delete( $request['id'] );
119 }
120 }
121