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 / RestController.php
RestController.php
152 lines 3.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 /**
6 * Rest controller base class.
7 */
8 abstract class RestController {
9 /**
10 * Always fetch with these subcollections.
11 *
12 * @var array
13 */
14 protected $with = [];
15
16 /**
17 * Class to make the requests.
18 *
19 * @var string
20 */
21 protected $class = '';
22
23 /**
24 * Run some middleware to run before request.
25 *
26 * @param \SureCart\Models\Model $class Model class instance.
27 * @param \WP_REST_Request $request Request object.
28 *
29 * @return \SureCart\Models\Model
30 */
31 protected function middleware( $class, \WP_REST_Request $request ) {
32 return apply_filters( 'surecart/request/model', $class, $request );
33 }
34
35 /**
36 * Create model.
37 *
38 * @param \WP_REST_Request $request Rest Request.
39 *
40 * @return \WP_REST_Response|\WP_Error
41 */
42 public function create( \WP_REST_Request $request ) {
43 $model = $this->middleware( new $this->class(), $request );
44 if ( is_wp_error( $model ) ) {
45 return $model;
46 }
47
48 if ( ! empty( $this->with ) ) {
49 $model = $model->with( $this->with );
50 }
51
52 return $model->where( $request->get_query_params() )->create( $request->get_json_params() );
53 }
54
55 /**
56 * Index model.
57 *
58 * @param \WP_REST_Request $request Rest Request.
59 *
60 * @return \WP_REST_Response|\WP_Error
61 */
62 public function index( \WP_REST_Request $request ) {
63 $model = $this->middleware( new $this->class(), $request );
64 if ( is_wp_error( $model ) ) {
65 return $model;
66 }
67
68 if ( ! empty( $this->with ) ) {
69 $model = $model->with( $this->with );
70 }
71
72 $items = $model->where( $request->get_params() )->paginate(
73 [
74 'per_page' => $request->get_param( 'per_page' ),
75 'page' => $request->get_param( 'page' ),
76 ]
77 );
78
79 // check for error.
80 if ( is_wp_error( $items ) ) {
81 return $items;
82 }
83
84 $response = rest_ensure_response( $items->data );
85 $response->header( 'X-WP-Total', (int) ( $items->pagination->count ?? 0 ) );
86 $max_pages = ceil( ( $items->pagination->count ?? 0 ) / ( $items->pagination->limit ?? 1 ) );
87 $response->header( 'X-WP-TotalPages', (int) $max_pages );
88
89 return $response;
90 }
91
92 /**
93 * Find model.
94 *
95 * @param \WP_REST_Request $request Rest Request.
96 *
97 * @return \SureCart\Models\Model|\WP_Error
98 */
99 public function find( \WP_REST_Request $request ) {
100 $model = $this->middleware( new $this->class(), $request );
101 if ( is_wp_error( $model ) ) {
102 return $model;
103 }
104
105 if ( ! empty( $this->with ) ) {
106 $model = $model->with( $this->with );
107 }
108
109 return $model->where( $request->get_query_params() )->find( $request['id'] );
110 }
111
112 /**
113 * Edit model.
114 *
115 * @param \WP_REST_Request $request Rest Request.
116 *
117 * @return \WP_REST_Response|\WP_Error
118 */
119 public function edit( \WP_REST_Request $request ) {
120 $model = $this->middleware( new $this->class( $request['id'] ), $request );
121 if ( is_wp_error( $model ) ) {
122 return $model;
123 }
124
125 if ( ! empty( $this->with ) ) {
126 $model = $model->with( $this->with );
127 }
128
129 return $model->where( $request->get_query_params() )->update( $request->get_json_params() );
130 }
131
132 /**
133 * Delete model.
134 *
135 * @param \WP_REST_Request $request Rest Request.
136 *
137 * @return \WP_REST_Response|\WP_Error
138 */
139 public function delete( \WP_REST_Request $request ) {
140 $model = $this->middleware( new $this->class(), $request );
141 if ( is_wp_error( $model ) ) {
142 return $model;
143 }
144
145 if ( ! empty( $this->with ) ) {
146 $model = $model->with( $this->with );
147 }
148
149 return $model->delete( $request['id'] );
150 }
151 }
152