PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 3.4.1
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v3.4.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 / Rest / SubscriptionProtocolRestServiceProvider.php
SubscriptionProtocolRestServiceProvider.php
145 lines 4.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\SubscriptionProtocolController;
6 use SureCart\Models\User;
7 use SureCart\Rest\RestServiceInterface;
8
9 /**
10 * Service provider for Price Rest Requests
11 */
12 class SubscriptionProtocolRestServiceProvider extends RestServiceProvider implements RestServiceInterface {
13 /**
14 * Endpoint.
15 *
16 * @var string
17 */
18 protected $endpoint = 'subscription_protocol';
19
20 /**
21 * Rest Controller
22 *
23 * @var string
24 */
25 protected $controller = SubscriptionProtocolController::class;
26
27 /**
28 * Methods allowed for the model.
29 *
30 * @var array
31 */
32 protected $methods = [];
33
34 /**
35 * Register REST Routes
36 *
37 * @return void
38 */
39 public function registerRoutes() {
40 register_rest_route(
41 "$this->name/v$this->version",
42 "$this->endpoint",
43 array_filter(
44 [
45 [
46 'methods' => \WP_REST_Server::READABLE,
47 'callback' => $this->callback( $this->controller, 'find' ),
48 'permission_callback' => [ $this, 'get_item_permissions_check' ],
49 'args' => $this->get_collection_params(),
50 ],
51 [
52 'methods' => \WP_REST_Server::EDITABLE,
53 'callback' => $this->callback( $this->controller, 'edit' ),
54 'permission_callback' => [ $this, 'update_item_permissions_check' ],
55 ],
56 'schema' => [ $this, 'get_item_schema' ],
57 ]
58 )
59 );
60 }
61
62 /**
63 * Get our sample schema for a post.
64 *
65 * @return array The sample schema for a post
66 */
67 public function get_item_schema() {
68 if ( $this->schema ) {
69 // Since WordPress 5.3, the schema can be cached in the $schema property.
70 return $this->schema;
71 }
72
73 $this->schema = [
74 // This tells the spec of JSON Schema we are using which is draft 4.
75 '$schema' => 'http://json-schema.org/draft-04/schema#',
76 // The title property marks the identity of the resource.
77 'title' => $this->endpoint,
78 'type' => 'object',
79 // In JSON Schema you can specify object properties in the properties attribute.
80 'properties' => [
81 'id' => [
82 'description' => esc_html__( 'Unique identifier for the object.', 'surecart' ),
83 'type' => 'string',
84 'context' => [ 'view', 'edit', 'embed' ],
85 'readonly' => true,
86 ],
87 'created_at' => [
88 'type' => 'integer',
89 'context' => [ 'view', 'edit', 'embed' ],
90 ],
91 'updated_at' => [
92 'type' => 'integer',
93 'context' => [ 'view', 'edit', 'embed' ],
94 ],
95 'payment_retry_window_weeks' => [
96 'description' => esc_html__( 'Payment retry window in weeks.', 'surecart' ),
97 'type' => 'integer',
98 'context' => [ 'view', 'edit', 'embed' ],
99 ],
100 'cancel_behavior' => [
101 'description' => esc_html__( 'Cancel behavior. Either pending or immediate.', 'surecart' ),
102 'type' => 'string',
103 'context' => [ 'view', 'edit', 'embed' ],
104 ],
105 'downgrade_behavior' => [
106 'description' => esc_html__( 'Downgrade behavior. Either pending or immediate.', 'surecart' ),
107 'type' => 'string',
108 'context' => [ 'view', 'edit', 'embed' ],
109 ],
110 'upgrade_behavior' => [
111 'description' => esc_html__( 'Upgrade behavior. Either pending or immediate.', 'surecart' ),
112 'type' => 'string',
113 'context' => [ 'view', 'edit', 'embed' ],
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