PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 4.3.3
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v4.3.3
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 / LineItemsRestServiceProvider.php
LineItemsRestServiceProvider.php
203 lines 5.5 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\LineItemsController;
7
8 /**
9 * Service provider for Price Rest Requests
10 */
11 class LineItemsRestServiceProvider extends RestServiceProvider implements RestServiceInterface {
12 /**
13 * Endpoint.
14 *
15 * @var string
16 */
17 protected $endpoint = 'line_items';
18
19 /**
20 * Rest Controller
21 *
22 * @var string
23 */
24 protected $controller = LineItemsController::class;
25
26 /**
27 * Whether the rest service provider converts currency.
28 *
29 * @var boolean
30 */
31 protected $converts_currency = true;
32
33 /**
34 * Methods allowed for the model.
35 *
36 * @var array
37 */
38 protected $methods = [ 'index', 'edit', 'create', 'find', 'delete' ];
39
40 /**
41 * Register REST Routes.
42 *
43 * @return void
44 */
45 public function registerRoutes() {
46 register_rest_route(
47 "$this->name/v$this->version",
48 $this->endpoint . '/upsell/',
49 [
50 [
51 'methods' => \WP_REST_Server::EDITABLE,
52 'callback' => $this->callback( $this->controller, 'upsell' ),
53 'permission_callback' => [ $this, 'update_item_permissions_check' ],
54 ],
55 // Register our schema callback.
56 'schema' => [ $this, 'get_item_schema' ],
57 ]
58 );
59 register_rest_route(
60 "$this->name/v$this->version",
61 $this->endpoint . '/(?P<id>\S+)/swap/',
62 [
63 [
64 'methods' => \WP_REST_Server::EDITABLE,
65 'callback' => $this->callback( $this->controller, 'swap' ),
66 'permission_callback' => [ $this, 'update_item_permissions_check' ],
67 ],
68 // Register our schema callback.
69 'schema' => [ $this, 'get_item_schema' ],
70 ]
71 );
72 register_rest_route(
73 "$this->name/v$this->version",
74 $this->endpoint . '/(?P<id>\S+)/unswap/',
75 [
76 [
77 'methods' => \WP_REST_Server::EDITABLE,
78 'callback' => $this->callback( $this->controller, 'unswap' ),
79 'permission_callback' => [ $this, 'update_item_permissions_check' ],
80 ],
81 // Register our schema callback.
82 'schema' => [ $this, 'get_item_schema' ],
83 ]
84 );
85 }
86
87 /**
88 * Get our sample schema for a post.
89 *
90 * @return array The sample schema for a post
91 */
92 public function get_item_schema() {
93 if ( $this->schema ) {
94 // Since WordPress 5.3, the schema can be cached in the $schema property.
95 return $this->schema;
96 }
97
98 $this->schema = [
99 // This tells the spec of JSON Schema we are using which is draft 4.
100 '$schema' => 'http://json-schema.org/draft-04/schema#',
101 // The title property marks the identity of the resource.
102 'title' => $this->endpoint,
103 'type' => 'object',
104 // In JSON Schema you can specify object properties in the properties attribute.
105 'properties' => [
106 'id' => [
107 'description' => esc_html__( 'Unique identifier for the object.', 'surecart' ),
108 'type' => 'string',
109 'context' => [ 'view', 'edit', 'embed' ],
110 'readonly' => true,
111 ],
112 'checkout' => [
113 'description' => esc_html__( 'The checkout for the line item.', 'surecart' ),
114 'type' => 'object',
115 'context' => [ 'view', 'edit', 'embed' ],
116 'properties' => [
117 'customer' => [
118 'description' => esc_html__( 'The customer associated with the checkout.', 'surecart' ),
119 'type' => 'object',
120 'context' => [ 'edit' ], // customer is only available in the edit context.
121 ],
122 ],
123 ],
124 ],
125 ];
126
127 return $this->schema;
128 }
129
130 /**
131 * Get the collection params.
132 *
133 * @return array
134 */
135 public function get_collection_params() {
136 return [
137 'page' => [
138 'description' => esc_html__( 'The page of items you want returned.', 'surecart' ),
139 'type' => 'integer',
140 ],
141 'per_page' => [
142 'description' => esc_html__( 'A limit on the number of items to be returned, between 1 and 100.', 'surecart' ),
143 'type' => 'integer',
144 ],
145 ];
146 }
147
148 /**
149 * Anyone can get a specific price.
150 *
151 * @param \WP_REST_Request $request Full details about the request.
152 * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise.
153 */
154 public function get_item_permissions_check( $request ) {
155 return true;
156 }
157
158 /**
159 * Who can list checkouts
160 *
161 * @param \WP_REST_Request $request Full details about the request.
162 * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise.
163 */
164 public function get_items_permissions_check( $request ) {
165 // if not listing based on a single checkout, the person must be able to list prices.
166 if ( empty( $request['checkout_ids'] ) || count( $request['checkout_ids'] ) > 1 ) {
167 return current_user_can( 'read_sc_prices' );
168 }
169
170 return true;
171 }
172
173 /**
174 * Anyone can create line items.
175 *
176 * @param \WP_REST_Request $request Full details about the request.
177 * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise.
178 */
179 public function create_item_permissions_check( $request ) {
180 return true;
181 }
182
183 /**
184 * Update model.
185 *
186 * @param \WP_REST_Request $request Full details about the request.
187 * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise.
188 */
189 public function update_item_permissions_check( $request ) {
190 return true;
191 }
192
193 /**
194 * Delete model.
195 *
196 * @param \WP_REST_Request $request Full details about the request.
197 * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise.
198 */
199 public function delete_item_permissions_check( $request ) {
200 return true;
201 }
202 }
203