PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 4.2.2
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v4.2.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 / SubscriptionProtocolRestServiceProvider.php
SubscriptionProtocolRestServiceProvider.php
160 lines 5.0 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 'remind_after_days' => [
116 'description' => esc_html__( 'Minimum days between reminders.', 'surecart' ),
117 'type' => 'integer',
118 'context' => [ 'view', 'edit', 'embed' ],
119 ],
120 'remind_at_period_percent_remaining' => [
121 'description' => esc_html__( 'Percentage of period remaining when reminder should be sent.', 'surecart' ),
122 'type' => 'integer',
123 'context' => [ 'view', 'edit', 'embed' ],
124 ],
125 'reschedule_existing_reminders' => [
126 'description' => esc_html__( 'Whether or not existing subscription reminders should be recalculated. Only applicable when remind_at_period_percent_remaining or remind_after_days are changed.', 'surecart' ),
127 'type' => 'boolean',
128 'context' => [ 'edit' ],
129 ],
130 ],
131 ];
132
133 return $this->schema;
134 }
135
136 /**
137 * Anyone can get the protocols.
138 *
139 * @param \WP_REST_Request $request Full details about the request.
140 * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise.
141 */
142 public function get_item_permissions_check( $request ) {
143 if ( current_user_can( 'read_sc_subscriptions', $request->get_params() ) ) {
144 return true;
145 }
146 // user must be a customer to get the protocols.
147 return is_user_logged_in() && User::current()->isCustomer();
148 }
149
150 /**
151 * Need priveleges to update.
152 *
153 * @param \WP_REST_Request $request Full details about the request.
154 * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise.
155 */
156 public function update_item_permissions_check( $request ) {
157 return current_user_can( 'edit_sc_subscriptions' );
158 }
159 }
160