AbandonedCheckoutProtocolRestServiceProvider.php
3 years ago
AbandonedCheckoutRestServiceProvider.php
3 years ago
AccountRestServiceProvider.php
3 years ago
ActivationRestServiceProvider.php
3 years ago
BalanceTransactionRestServiceProvider.php
3 years ago
BlockPatternsRestServiceProvider.php
3 years ago
BrandRestServiceProvider.php
3 years ago
BumpRestServiceProvider.php
3 years ago
CancellationActRestServiceProvider.php
3 years ago
CancellationReasonRestServiceProvider.php
3 years ago
ChargesRestServiceProvider.php
3 years ago
CheckEmailRestServiceProvider.php
3 years ago
CheckoutRestServiceProvider.php
3 years ago
CouponRestServiceProvider.php
3 years ago
CustomerLinksRestServiceProvider.php
3 years ago
CustomerNotificationProtocolRestServiceProvider.php
3 years ago
CustomerRestServiceProvider.php
3 years ago
DownloadRestServiceProvider.php
3 years ago
DraftCheckoutRestServiceProvider.php
2 years ago
FulfillmentRestServiceProvider.php
3 years ago
IncomingWebhooksRestServiceProvider.php
2 years ago
IntegrationProvidersRestServiceProvider.php
3 years ago
IntegrationsRestServiceProvider.php
3 years ago
InvoicesRestServiceProvider.php
3 years ago
LicenseRestServiceProvider.php
3 years ago
LineItemsRestServiceProvider.php
3 years ago
LoginRestServiceProvider.php
3 years ago
ManualPaymentMethodsRestServiceProvider.php
3 years ago
MediaRestServiceProvider.php
3 years ago
OrderProtocolRestServiceProvider.php
3 years ago
OrderRestServiceProvider.php
3 years ago
PaymentIntentsRestServiceProvider.php
3 years ago
PaymentMethodsRestServiceProvider.php
2 years ago
PeriodRestServiceProvider.php
3 years ago
PortalProtocolRestServiceProvider.php
3 years ago
PriceRestServiceProvider.php
3 years ago
ProcessorRestServiceProvider.php
3 years ago
ProductGroupsRestServiceProvider.php
3 years ago
ProductMediaRestServiceProvider.php
3 years ago
ProductsRestServiceProvider.php
3 years ago
PromotionRestServiceProvider.php
3 years ago
ProvisionalAccountRestServiceProvider.php
3 years ago
PurchasesRestServiceProvider.php
3 years ago
RefundsRestServiceProvider.php
3 years ago
RegisteredWebhookRestServiceProvider.php
2 years ago
RestServiceInterface.php
3 years ago
RestServiceProvider.php
3 years ago
SettingsRestServiceProvider.php
3 years ago
ShippingMethodRestServiceProvider.php
3 years ago
ShippingProfileRestServiceProvider.php
3 years ago
ShippingProtocolRestServiceProvider.php
3 years ago
ShippingRateRestServiceProvider.php
3 years ago
ShippingZoneRestServiceProvider.php
3 years ago
SiteHealthRestServiceProvider.php
2 years ago
StatisticRestServiceProvider.php
3 years ago
SubscriptionProtocolRestServiceProvider.php
3 years ago
SubscriptionRestServiceProvider.php
3 years ago
TaxProtocolRestServiceProvider.php
3 years ago
TaxRegistrationRestServiceProvider.php
3 years ago
TaxZoneRestServiceProvider.php
3 years ago
UploadsRestServiceProvider.php
3 years ago
VerificationCodeRestServiceProvider.php
3 years ago
WebhooksRestServiceProvider.php
3 years ago
CustomerRestServiceProvider.php
223 lines
| 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 |