PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 2.1.3
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v2.1.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 / PortalProtocolRestServiceProvider.php
PortalProtocolRestServiceProvider.php
145 lines 4.3 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\PortalProtocolController;
6 use SureCart\Controllers\Rest\SubscriptionProtocolController;
7 use SureCart\Models\User;
8 use SureCart\Rest\RestServiceInterface;
9
10 /**
11 * Service provider for Price Rest Requests
12 */
13 class PortalProtocolRestServiceProvider extends RestServiceProvider implements RestServiceInterface {
14 /**
15 * Endpoint.
16 *
17 * @var string
18 */
19 protected $endpoint = 'portal_protocol';
20
21 /**
22 * Rest Controller
23 *
24 * @var string
25 */
26 protected $controller = PortalProtocolController::class;
27
28 /**
29 * Methods allowed for the model.
30 *
31 * @var array
32 */
33 protected $methods = [];
34
35 /**
36 * Register REST Routes
37 *
38 * @return void
39 */
40 public function registerRoutes() {
41 register_rest_route(
42 "$this->name/v$this->version",
43 "$this->endpoint",
44 array_filter(
45 [
46 [
47 'methods' => \WP_REST_Server::READABLE,
48 'callback' => $this->callback( $this->controller, 'find' ),
49 'permission_callback' => [ $this, 'get_item_permissions_check' ],
50 'args' => $this->get_collection_params(),
51 ],
52 [
53 'methods' => \WP_REST_Server::EDITABLE,
54 'callback' => $this->callback( $this->controller, 'edit' ),
55 'permission_callback' => [ $this, 'update_item_permissions_check' ],
56 ],
57 'schema' => [ $this, 'get_item_schema' ],
58 ]
59 )
60 );
61 }
62
63 /**
64 * Get our sample schema for a post.
65 *
66 * @return array The sample schema for a post
67 */
68 public function get_item_schema() {
69 if ( $this->schema ) {
70 // Since WordPress 5.3, the schema can be cached in the $schema property.
71 return $this->schema;
72 }
73
74 $this->schema = [
75 // This tells the spec of JSON Schema we are using which is draft 4.
76 '$schema' => 'http://json-schema.org/draft-04/schema#',
77 // The title property marks the identity of the resource.
78 'title' => $this->endpoint,
79 'type' => 'object',
80 // In JSON Schema you can specify object properties in the properties attribute.
81 'properties' => [
82 'id' => [
83 'description' => esc_html__( 'Unique identifier for the object.', 'surecart' ),
84 'type' => 'string',
85 'context' => [ 'edit' ],
86 'readonly' => true,
87 ],
88 'created_at' => [
89 'type' => 'integer',
90 'context' => [ 'edit' ],
91 ],
92 'updated_at' => [
93 'type' => 'integer',
94 'context' => [ 'edit' ],
95 ],
96 'subscription_cancellations_enabled' => [
97 'description' => esc_html__( 'Whether or not customers can cancel subscriptions from the customer portal.', 'surecart' ),
98 'type' => 'boolean',
99 'context' => [ 'view', 'edit' ],
100 ],
101 'subscription_updates_enabled' => [
102 'description' => esc_html__( 'Whether or not customers can make subscription changes from the customer portal.', 'surecart' ),
103 'type' => 'boolean',
104 'context' => [ 'view', 'edit' ],
105 ],
106 'subscription_quantity_updates_enabled' => [
107 'description' => esc_html__( 'Whether or not customers can change subscription quantities from the customer portal.', 'surecart' ),
108 'type' => 'boolean',
109 'context' => [ 'view', 'edit' ],
110 ],
111 'terms_url' => [
112 'description' => esc_html__( 'The terms of service link that is shown to customers on the customer portal.', 'surecart' ),
113 'type' => 'string',
114 ],
115 ],
116 ];
117
118 return $this->schema;
119 }
120
121 /**
122 * Anyone can get the protocols.
123 *
124 * @param \WP_REST_Request $request Full details about the request.
125 * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise.
126 */
127 public function get_item_permissions_check( $request ) {
128 if ( current_user_can( 'read_sc_subscriptions', $request->get_params() ) ) {
129 return true;
130 }
131 // user must be a customer to get the protocols.
132 return is_user_logged_in() && User::current()->isCustomer();
133 }
134
135 /**
136 * Need priveleges to update.
137 *
138 * @param \WP_REST_Request $request Full details about the request.
139 * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise.
140 */
141 public function update_item_permissions_check( $request ) {
142 return current_user_can( 'edit_sc_subscriptions' );
143 }
144 }
145