AbandonedCheckoutProtocolRestServiceProvider.php
3 years ago
AbandonedCheckoutRestServiceProvider.php
2 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
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
2 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
ProductCollectionsRestServiceProvider.php
2 years ago
ProductGroupsRestServiceProvider.php
3 years ago
ProductMediaRestServiceProvider.php
3 years ago
ProductsRestServiceProvider.php
2 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
ReturnItemsRestServiceProvider.php
2 years ago
ReturnReasonsRestServiceProvider.php
2 years ago
ReturnRequestsRestServiceProvider.php
2 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
VariantOptionsRestServiceProvider.php
2 years ago
VariantValuesRestServiceProvider.php
2 years ago
VariantsRestServiceProvider.php
2 years ago
VerificationCodeRestServiceProvider.php
3 years ago
WebhooksRestServiceProvider.php
3 years ago
PortalProtocolRestServiceProvider.php
145 lines
| 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 |