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
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
IntegrationProvidersRestServiceProvider.php
3 years ago
IntegrationsRestServiceProvider.php
3 years ago
InvoicesRestServiceProvider.php
3 years ago
LicenseRestServiceProvider.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
3 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
ProductsRestServiceProvider.php
3 years ago
PromotionRestServiceProvider.php
3 years ago
PurchasesRestServiceProvider.php
3 years ago
RefundsRestServiceProvider.php
3 years ago
RestServiceInterface.php
3 years ago
RestServiceProvider.php
3 years ago
SettingsRestServiceProvider.php
3 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
154 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 | * Register REST Routes |
| 60 | * |
| 61 | * @return void |
| 62 | */ |
| 63 | public function registerRoutes() { |
| 64 | register_rest_route( |
| 65 | "$this->name/v$this->version", |
| 66 | $this->endpoint . '/(?P<id>\S+)/connect/(?P<user_id>\S+)', |
| 67 | [ |
| 68 | [ |
| 69 | 'methods' => \WP_REST_Server::EDITABLE, |
| 70 | 'callback' => $this->callback( $this->controller, 'connect' ), |
| 71 | 'permission_callback' => [ $this, 'connect_permissions_check' ], |
| 72 | ], |
| 73 | // Register our schema callback. |
| 74 | 'schema' => [ $this, 'get_item_schema' ], |
| 75 | ] |
| 76 | ); |
| 77 | |
| 78 | register_rest_route( |
| 79 | "$this->name/v$this->version", |
| 80 | $this->endpoint . '/(?P<id>\S+)/expose/(?P<media_id>\S+)', |
| 81 | [ |
| 82 | [ |
| 83 | 'methods' => \WP_REST_Server::READABLE, |
| 84 | 'callback' => $this->callback( $this->controller, 'exposeMedia' ), |
| 85 | 'permission_callback' => [ $this, 'get_item_permissions_check' ], |
| 86 | ], |
| 87 | // Register our schema callback. |
| 88 | 'schema' => [ $this, 'get_item_schema' ], |
| 89 | ] |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * A WordPress user can read their own customer record. |
| 95 | * |
| 96 | * @param \WP_REST_Request $request Full details about the request. |
| 97 | * @return boolean |
| 98 | */ |
| 99 | public function connect_permissions_check( $request ) { |
| 100 | return current_user_can( 'edit_sc_customers' ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * A WordPress user can read their own customer record. |
| 105 | * |
| 106 | * @param \WP_REST_Request $request Full details about the request. |
| 107 | * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise. |
| 108 | */ |
| 109 | public function get_item_permissions_check( $request ) { |
| 110 | return current_user_can( 'read_sc_customer', $request->get_params() ); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Read permissions. |
| 115 | * |
| 116 | * @param \WP_REST_Request $request Full details about the request. |
| 117 | * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise. |
| 118 | */ |
| 119 | public function get_items_permissions_check( $request ) { |
| 120 | return current_user_can( 'read_sc_customers' ); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | /** |
| 125 | * Create permissions. |
| 126 | * |
| 127 | * @param \WP_REST_Request $request Full details about the request. |
| 128 | * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise. |
| 129 | */ |
| 130 | public function create_item_permissions_check( $request ) { |
| 131 | return current_user_can( 'publish_sc_customers' ); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Update permissions. |
| 136 | * |
| 137 | * @param \WP_REST_Request $request Full details about the request. |
| 138 | * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise. |
| 139 | */ |
| 140 | public function update_item_permissions_check( $request ) { |
| 141 | return current_user_can( 'edit_sc_customer', $request['id'], $request->get_params() ); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Nobody can delete. |
| 146 | * |
| 147 | * @param \WP_REST_Request $request Full details about the request. |
| 148 | * @return false |
| 149 | */ |
| 150 | public function delete_item_permissions_check( $request ) { |
| 151 | return current_user_can( 'delete_sc_customers' ); |
| 152 | } |
| 153 | } |
| 154 |