PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 2.25.2
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v2.25.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 / Rest / CustomerRestServiceProvider.php
CustomerRestServiceProvider.php
223 lines 6.2 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\Controllers\Rest\CustomerController;
6 use SureCart\Models\User;
7 use SureCart\Rest\RestServiceInterface;
8
9 /**
10 * Service provider for Price Rest Requests
11 */
12 class CustomerRestServiceProvider extends RestServiceProvider implements RestServiceInterface {
13 /**
14 * Endpoint.
15 *
16 * @var string
17 */
18 protected $endpoint = 'customers';
19
20 /**
21 * Rest Controller
22 *
23 * @var string
24 */
25 protected $controller = CustomerController::class;
26
27 /**
28 * Get our sample schema for a post.
29 *
30 * @return array The sample schema for a post
31 */
32 public function get_item_schema() {
33 if ( $this->schema ) {
34 // Since WordPress 5.3, the schema can be cached in the $schema property.
35 return $this->schema;
36 }
37
38 $this->schema = [
39 // This tells the spec of JSON Schema we are using which is draft 4.
40 '$schema' => 'http://json-schema.org/draft-04/schema#',
41 // The title property marks the identity of the resource.
42 'title' => $this->endpoint,
43 'type' => 'object',
44 // In JSON Schema you can specify object properties in the properties attribute.
45 'properties' => [
46 'id' => [
47 'description' => esc_html__( 'Unique identifier for the object.', 'surecart' ),
48 'type' => 'string',
49 'context' => [ 'view', 'edit', 'embed' ],
50 'readonly' => true,
51 ],
52 ],
53 ];
54
55 return $this->schema;
56 }
57
58 /**
59 * Get our sample schema for a post.
60 *
61 * @return array The sample schema for a post
62 */
63 public function sync_schema() {
64 if ( $this->sync_schema ) {
65 // Since WordPress 5.3, the schema can be cached in the $schema property.
66 return $this->sync_schema;
67 }
68
69 $this->schema = [
70 // This tells the spec of JSON Schema we are using which is draft 4.
71 '$schema' => 'http://json-schema.org/draft-04/schema#',
72 // The title property marks the identity of the resource.
73 'title' => $this->endpoint,
74 'type' => 'object',
75 // In JSON Schema you can specify object properties in the properties attribute.
76 'properties' => [
77 'create_user' => [
78 'description' => esc_html__( 'Create the WordPress user.', 'surecart' ),
79 'type' => 'boolean',
80 'context' => [ 'view', 'edit', 'embed' ],
81 'default' => true,
82 ],
83 'run_actions' => [
84 'description' => esc_html__( 'Run any purchase syncing actions.', 'surecart' ),
85 'type' => 'boolean',
86 'context' => [ 'view', 'edit', 'embed' ],
87 'default' => true,
88 ],
89 'dry_run' => [
90 'description' => esc_html__( 'Dry run the sync.', 'surecart' ),
91 'type' => 'boolean',
92 'context' => [ 'view', 'edit', 'embed' ],
93 'default' => false,
94 ],
95 ],
96 ];
97
98 return $this->schema;
99 }
100
101 /**
102 * Register REST Routes
103 *
104 * @return void
105 */
106 public function registerRoutes() {
107 register_rest_route(
108 "$this->name/v$this->version",
109 $this->endpoint . '/(?P<id>\S+)/connect/(?P<user_id>\S+)',
110 [
111 [
112 'methods' => \WP_REST_Server::EDITABLE,
113 'callback' => $this->callback( $this->controller, 'connect' ),
114 'permission_callback' => [ $this, 'connect_permissions_check' ],
115 ],
116 // Register our schema callback.
117 'schema' => [ $this, 'get_item_schema' ],
118 ]
119 );
120
121 // sync with SureCart.
122 register_rest_route(
123 "$this->name/v$this->version",
124 $this->endpoint . '/sync',
125 [
126 [
127 'methods' => \WP_REST_Server::EDITABLE,
128 'callback' => $this->callback( $this->controller, 'sync' ),
129 'permission_callback' => [ $this, 'sync_permissions_check' ],
130 ],
131 // Register our schema callback.
132 'schema' => [ $this, 'sync_schema' ],
133 ]
134 );
135
136 register_rest_route(
137 "$this->name/v$this->version",
138 $this->endpoint . '/(?P<id>\S+)/expose/(?P<media_id>\S+)',
139 [
140 [
141 'methods' => \WP_REST_Server::READABLE,
142 'callback' => $this->callback( $this->controller, 'exposeMedia' ),
143 'permission_callback' => [ $this, 'get_item_permissions_check' ],
144 ],
145 // Register our schema callback.
146 'schema' => [ $this, 'get_item_schema' ],
147 ]
148 );
149 }
150
151 /**
152 * A WordPress user can read their own customer record.
153 *
154 * @param \WP_REST_Request $request Full details about the request.
155 * @return boolean
156 */
157 public function connect_permissions_check( $request ) {
158 return current_user_can( 'edit_sc_customers' );
159 }
160
161 /**
162 * A WordPress user can read their own customer record.
163 *
164 * @param \WP_REST_Request $request Full details about the request.
165 * @return boolean
166 */
167 public function sync_permissions_check( $request ) {
168 return current_user_can( 'edit_sc_customers' );
169 }
170
171
172 /**
173 * A WordPress user can read their own customer record.
174 *
175 * @param \WP_REST_Request $request Full details about the request.
176 * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise.
177 */
178 public function get_item_permissions_check( $request ) {
179 return current_user_can( 'read_sc_customer', $request->get_params() );
180 }
181
182 /**
183 * Read permissions.
184 *
185 * @param \WP_REST_Request $request Full details about the request.
186 * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise.
187 */
188 public function get_items_permissions_check( $request ) {
189 return current_user_can( 'read_sc_customers' );
190 }
191
192
193 /**
194 * Create permissions.
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 create_item_permissions_check( $request ) {
200 return current_user_can( 'publish_sc_customers' );
201 }
202
203 /**
204 * Update permissions.
205 *
206 * @param \WP_REST_Request $request Full details about the request.
207 * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise.
208 */
209 public function update_item_permissions_check( $request ) {
210 return current_user_can( 'edit_sc_customer', $request['id'], $request->get_params() );
211 }
212
213 /**
214 * Nobody can delete.
215 *
216 * @param \WP_REST_Request $request Full details about the request.
217 * @return false
218 */
219 public function delete_item_permissions_check( $request ) {
220 return current_user_can( 'delete_sc_customers' );
221 }
222 }
223