AbandonedCheckoutProtocolRestServiceProvider.php
3 years ago
AbandonedCheckoutRestServiceProvider.php
2 years ago
AccountRestServiceProvider.php
4 years ago
ActivationRestServiceProvider.php
1 year ago
AffiliationProductsRestServiceProvider.php
2 years ago
AffiliationProtocolRestServiceProvider.php
2 years ago
AffiliationRequestsRestServiceProvider.php
2 years ago
AffiliationsRestServiceProvider.php
11 months ago
AutoFeeProtocolRestServiceProvider.php
5 months ago
AutoFeeRestServiceProvider.php
5 months ago
BalanceTransactionRestServiceProvider.php
3 years ago
BatchesRestServiceProvider.php
1 week ago
BlockPatternsRestServiceProvider.php
3 years ago
BrandRestServiceProvider.php
5 days ago
BumpRestServiceProvider.php
5 days ago
BundleItemsRestServiceProvider.php
5 days ago
CancellationActRestServiceProvider.php
1 year ago
CancellationReasonRestServiceProvider.php
1 year ago
ChargesRestServiceProvider.php
4 years ago
CheckEmailRestServiceProvider.php
3 years ago
CheckoutRestServiceProvider.php
1 year ago
ClicksRestServiceProvider.php
2 years ago
CouponRestServiceProvider.php
4 years ago
CustomerNotificationProtocolRestServiceProvider.php
4 years ago
CustomerPortalProtocolRestServiceProvider.php
1 year ago
CustomerRestServiceProvider.php
1 month ago
DisplayCurrencyRestServiceProvider.php
1 year ago
DisputesRestServiceProvider.php
9 months ago
DownloadRestServiceProvider.php
3 months ago
DraftCheckoutRestServiceProvider.php
1 year ago
ExportsRestServiceProvider.php
2 years ago
FulfillmentRestServiceProvider.php
3 years ago
ImportRowsRestServiceProvider.php
2 months ago
IncomingWebhooksRestServiceProvider.php
2 years ago
IntegrationProvidersRestServiceProvider.php
4 years ago
IntegrationsCatalogRestServiceProvider.php
1 year ago
IntegrationsRestServiceProvider.php
4 years ago
InvoicesRestServiceProvider.php
1 year ago
LicenseRestServiceProvider.php
1 year ago
LineItemsRestServiceProvider.php
1 year ago
LoginRestServiceProvider.php
1 month ago
ManualPaymentMethodsRestServiceProvider.php
3 years ago
MediaRestServiceProvider.php
1 year ago
OrderProtocolRestServiceProvider.php
1 year ago
OrderRestServiceProvider.php
1 year ago
ParcelTemplateRestServiceProvider.php
4 months ago
PaymentIntentsRestServiceProvider.php
4 years ago
PaymentMethodsRestServiceProvider.php
2 years ago
PayoutGroupsRestServiceProvider.php
2 years ago
PayoutsRestServiceProvider.php
5 months ago
PeriodRestServiceProvider.php
3 years ago
PluginInstallerRestServiceProvider.php
1 week ago
PriceRestServiceProvider.php
5 days ago
ProcessorRestServiceProvider.php
3 years ago
ProductCollectionsRestServiceProvider.php
5 days ago
ProductGroupsRestServiceProvider.php
5 days ago
ProductMediaRestServiceProvider.php
5 days ago
ProductsRestServiceProvider.php
5 days ago
PromotionRestServiceProvider.php
4 years ago
ProvisionalAccountRestServiceProvider.php
3 years ago
PurchasesRestServiceProvider.php
4 years ago
ReferralItemsRestServiceProvider.php
2 years ago
ReferralsRestServiceProvider.php
2 years ago
RefundsRestServiceProvider.php
4 years ago
RegisteredWebhookRestServiceProvider.php
2 years ago
RestServiceInterface.php
4 years ago
RestServiceProvider.php
5 days ago
ReturnItemsRestServiceProvider.php
2 years ago
ReturnReasonsRestServiceProvider.php
2 years ago
ReturnRequestsRestServiceProvider.php
2 years ago
ReviewProtocolRestServiceProvider.php
5 months ago
ReviewsRestServiceProvider.php
1 week ago
RuleSchemaRestServiceProvider.php
5 months ago
SettingsRestServiceProvider.php
1 year ago
ShippingMethodRestServiceProvider.php
3 years ago
ShippingProfileRestServiceProvider.php
3 years ago
ShippingProtocolRestServiceProvider.php
1 year ago
ShippingRateRestServiceProvider.php
3 years ago
ShippingZoneRestServiceProvider.php
3 years ago
SiteHealthRestServiceProvider.php
2 years ago
StatisticRestServiceProvider.php
3 years ago
SubscriptionProtocolRestServiceProvider.php
5 months ago
SubscriptionRestServiceProvider.php
2 years ago
SwapRestServiceProvider.php
1 year ago
TaxOverrideRestServiceProvider.php
1 year ago
TaxProtocolRestServiceProvider.php
1 year ago
TaxRegistrationRestServiceProvider.php
1 year ago
TaxZoneRestServiceProvider.php
1 year ago
UploadsRestServiceProvider.php
4 years ago
UpsellFunnelRestServiceProvider.php
1 year ago
UpsellRestServiceProvider.php
5 days ago
VariantOptionsRestServiceProvider.php
5 days ago
VariantValuesRestServiceProvider.php
5 days ago
VariantsRestServiceProvider.php
5 days ago
VerificationCodeRestServiceProvider.php
3 years ago
WebhooksRestServiceProvider.php
4 years ago
ProductsRestServiceProvider.php
430 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Rest; |
| 4 | |
| 5 | use SureCart\Concerns\StripsPrivateCatalogFields; |
| 6 | use SureCart\Rest\RestServiceInterface; |
| 7 | use SureCart\Controllers\Rest\ProductsController; |
| 8 | |
| 9 | /** |
| 10 | * Service provider for Price Rest Requests |
| 11 | */ |
| 12 | class ProductsRestServiceProvider extends RestServiceProvider implements RestServiceInterface { |
| 13 | use StripsPrivateCatalogFields; |
| 14 | |
| 15 | /** |
| 16 | * Endpoint. |
| 17 | * |
| 18 | * @var string |
| 19 | */ |
| 20 | protected $endpoint = 'products'; |
| 21 | |
| 22 | /** |
| 23 | * Rest Controller |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | protected $controller = ProductsController::class; |
| 28 | |
| 29 | /** |
| 30 | * Filter index list items by schema context. |
| 31 | * |
| 32 | * @var boolean |
| 33 | */ |
| 34 | protected $filters_list_items = true; |
| 35 | |
| 36 | /** |
| 37 | * Register Additional REST Routes |
| 38 | * |
| 39 | * @return void |
| 40 | */ |
| 41 | public function registerRoutes() { |
| 42 | register_rest_route( |
| 43 | "$this->name/v$this->version", |
| 44 | $this->endpoint . '/(?P<id>\S+)/sync/', |
| 45 | [ |
| 46 | [ |
| 47 | 'methods' => \WP_REST_Server::EDITABLE, |
| 48 | 'callback' => $this->callback( $this->controller, 'sync' ), |
| 49 | 'permission_callback' => [ $this, 'update_item_permissions_check' ], |
| 50 | ], |
| 51 | // Register our schema callback. |
| 52 | 'schema' => [ $this, 'get_item_schema' ], |
| 53 | ] |
| 54 | ); |
| 55 | register_rest_route( |
| 56 | "$this->name/v$this->version", |
| 57 | $this->endpoint . '/sync_all', |
| 58 | [ |
| 59 | [ |
| 60 | 'methods' => \WP_REST_Server::EDITABLE, |
| 61 | 'callback' => $this->callback( $this->controller, 'syncAll' ), |
| 62 | 'permission_callback' => [ $this, 'update_item_permissions_check' ], |
| 63 | ], |
| 64 | // Register our schema callback. |
| 65 | 'schema' => [ $this, 'get_item_schema' ], |
| 66 | ] |
| 67 | ); |
| 68 | register_rest_route( |
| 69 | "$this->name/v$this->version", |
| 70 | $this->endpoint . '/(?P<id>\S+)/duplicate', |
| 71 | [ |
| 72 | 'methods' => \WP_REST_Server::CREATABLE, |
| 73 | 'callback' => $this->callback( $this->controller, 'duplicate' ), |
| 74 | 'permission_callback' => [ $this, 'create_item_permissions_check' ], |
| 75 | ] |
| 76 | ); |
| 77 | register_rest_route( |
| 78 | "$this->name/v$this->version", |
| 79 | $this->endpoint . '/import_woocommerce', |
| 80 | [ |
| 81 | [ |
| 82 | 'methods' => \WP_REST_Server::CREATABLE, |
| 83 | 'callback' => $this->callback( $this->controller, 'importWooCommerce' ), |
| 84 | 'permission_callback' => [ $this, 'update_item_permissions_check' ], |
| 85 | ], |
| 86 | ] |
| 87 | ); |
| 88 | register_rest_route( |
| 89 | "$this->name/v$this->version", |
| 90 | $this->endpoint . '/woocommerce_count', |
| 91 | [ |
| 92 | [ |
| 93 | 'methods' => \WP_REST_Server::READABLE, |
| 94 | 'callback' => $this->callback( $this->controller, 'wooCommerceCount' ), |
| 95 | // Uses edit-level permission (edit_sc_products) intentionally: admin-only import feature, only product editors should see the WooCommerce count. |
| 96 | 'permission_callback' => [ $this, 'update_item_permissions_check' ], |
| 97 | ], |
| 98 | ] |
| 99 | ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Get our sample schema for a post. |
| 104 | * |
| 105 | * @return array The sample schema for a post |
| 106 | */ |
| 107 | public function get_item_schema() { |
| 108 | if ( $this->schema ) { |
| 109 | // Since WordPress 5.3, the schema can be cached in the $schema property. |
| 110 | return $this->schema; |
| 111 | } |
| 112 | |
| 113 | $this->schema = [ |
| 114 | // This tells the spec of JSON Schema we are using which is draft 4. |
| 115 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 116 | // The title property marks the identity of the resource. |
| 117 | 'title' => $this->endpoint, |
| 118 | 'type' => 'object', |
| 119 | // In JSON Schema you can specify object properties in the properties attribute. |
| 120 | 'properties' => [ |
| 121 | 'id' => [ |
| 122 | 'description' => esc_html__( 'Unique identifier for the object.', 'surecart' ), |
| 123 | 'type' => 'string', |
| 124 | 'context' => [ 'view', 'edit', 'embed' ], |
| 125 | 'readonly' => true, |
| 126 | ], |
| 127 | 'file_upload_ids' => [ |
| 128 | 'description' => esc_html__( 'Files attached to the product.', 'surecart' ), |
| 129 | 'context' => [ 'edit' ], |
| 130 | 'type' => 'array', |
| 131 | 'items' => [ |
| 132 | 'type' => 'string', |
| 133 | ], |
| 134 | ], |
| 135 | 'metadata' => [ |
| 136 | 'description' => esc_html__( 'Stored product metadata', 'surecart' ), |
| 137 | 'type' => 'object', |
| 138 | 'context' => [ 'edit' ], |
| 139 | 'properties' => [ |
| 140 | 'wp_created_by' => [ |
| 141 | 'type' => 'integer', |
| 142 | 'context' => [ 'edit' ], |
| 143 | 'readonly' => true, |
| 144 | ], |
| 145 | ], |
| 146 | ], |
| 147 | 'available_stock' => [ |
| 148 | 'description' => esc_html__( 'The available stock for the product.', 'surecart' ), |
| 149 | 'type' => 'integer', |
| 150 | 'context' => [ 'edit' ], |
| 151 | ], |
| 152 | 'held_stock' => [ |
| 153 | 'description' => esc_html__( 'The held stock for the product.', 'surecart' ), |
| 154 | 'type' => 'integer', |
| 155 | 'context' => [ 'edit' ], |
| 156 | ], |
| 157 | 'stock' => [ |
| 158 | 'description' => esc_html__( 'The stock for the product.', 'surecart' ), |
| 159 | 'type' => 'integer', |
| 160 | 'context' => [ 'edit' ], |
| 161 | ], |
| 162 | 'status' => [ |
| 163 | 'description' => esc_html__( 'The status of the product.', 'surecart' ), |
| 164 | 'type' => 'string', |
| 165 | 'context' => [ 'edit' ], |
| 166 | ], |
| 167 | 'archived' => [ |
| 168 | 'description' => esc_html__( 'Whether the product is archived.', 'surecart' ), |
| 169 | 'type' => 'boolean', |
| 170 | 'context' => [ 'edit' ], |
| 171 | ], |
| 172 | 'archived_at' => [ |
| 173 | 'description' => esc_html__( 'Archived at timestamp.', 'surecart' ), |
| 174 | 'type' => 'integer', |
| 175 | 'context' => [ 'edit' ], |
| 176 | ], |
| 177 | 'discarded_at' => [ |
| 178 | 'description' => esc_html__( 'Discarded at timestamp.', 'surecart' ), |
| 179 | 'type' => 'integer', |
| 180 | 'context' => [ 'edit' ], |
| 181 | ], |
| 182 | 'cataloged_at' => [ |
| 183 | 'description' => esc_html__( 'Cataloged at timestamp.', 'surecart' ), |
| 184 | 'type' => 'integer', |
| 185 | 'context' => [ 'edit' ], |
| 186 | ], |
| 187 | 'sku' => [ |
| 188 | 'description' => esc_html__( 'The product SKU.', 'surecart' ), |
| 189 | 'type' => 'string', |
| 190 | 'context' => [ 'edit' ], |
| 191 | ], |
| 192 | 'dimensions' => [ |
| 193 | 'description' => esc_html__( 'The product shipping dimensions.', 'surecart' ), |
| 194 | 'type' => 'object', |
| 195 | 'context' => [ 'edit' ], |
| 196 | ], |
| 197 | 'weight' => [ |
| 198 | 'description' => esc_html__( 'The product shipping weight.', 'surecart' ), |
| 199 | 'type' => 'number', |
| 200 | 'context' => [ 'edit' ], |
| 201 | ], |
| 202 | 'weight_unit' => [ |
| 203 | 'description' => esc_html__( 'The product shipping weight unit.', 'surecart' ), |
| 204 | 'type' => 'string', |
| 205 | 'context' => [ 'edit' ], |
| 206 | ], |
| 207 | 'tax_category' => [ |
| 208 | 'description' => esc_html__( 'The product tax category.', 'surecart' ), |
| 209 | 'type' => 'string', |
| 210 | 'context' => [ 'edit' ], |
| 211 | ], |
| 212 | 'tax_enabled' => [ |
| 213 | 'description' => esc_html__( 'Whether tax is enabled for the product.', 'surecart' ), |
| 214 | 'type' => 'boolean', |
| 215 | 'context' => [ 'edit' ], |
| 216 | ], |
| 217 | 'purchase_limit' => [ |
| 218 | 'description' => esc_html__( 'The purchase limit for the product.', 'surecart' ), |
| 219 | 'type' => 'integer', |
| 220 | 'context' => [ 'edit' ], |
| 221 | ], |
| 222 | 'metrics' => [ |
| 223 | 'description' => esc_html__( 'Top level metrics for the product.', 'surecart' ), |
| 224 | 'readonly' => true, |
| 225 | 'type' => 'object', |
| 226 | 'properties' => [ |
| 227 | 'currency' => [ |
| 228 | 'type' => 'string', |
| 229 | ], |
| 230 | 'max_price_amount' => [ |
| 231 | 'type' => 'integer', |
| 232 | ], |
| 233 | 'min_price_amount' => [ |
| 234 | 'type' => 'integer', |
| 235 | ], |
| 236 | 'prices_count' => [ |
| 237 | 'type' => 'integer', |
| 238 | ], |
| 239 | ], |
| 240 | // public price aggregates — the product list renders price ranges from these. |
| 241 | 'context' => [ 'view', 'edit', 'embed' ], |
| 242 | ], |
| 243 | ], |
| 244 | ]; |
| 245 | |
| 246 | return $this->schema; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Get the collection params. |
| 251 | * |
| 252 | * @return array |
| 253 | */ |
| 254 | public function get_collection_params() { |
| 255 | $args = [ |
| 256 | 'archived' => [ |
| 257 | 'description' => esc_html__( 'Whether to get archived products or not.', 'surecart' ), |
| 258 | 'type' => 'boolean', |
| 259 | ], |
| 260 | 'recurring' => [ |
| 261 | 'description' => esc_html__( 'Only return products that are recurring or not recurring (one time).', 'surecart' ), |
| 262 | 'type' => 'boolean', |
| 263 | ], |
| 264 | 'bundle' => [ |
| 265 | 'description' => esc_html__( 'Only return bundle products (true) or non-bundle products (false).', 'surecart' ), |
| 266 | 'type' => 'boolean', |
| 267 | ], |
| 268 | 'query' => [ |
| 269 | 'description' => __( 'The query to be used for full text search of this collection.', 'surecart' ), |
| 270 | 'type' => 'string', |
| 271 | ], |
| 272 | 'sort' => [ |
| 273 | 'description' => __( 'Sort directive in the form `field:direction`, e.g. `cataloged_at:desc`.', 'surecart' ), |
| 274 | 'type' => 'string', |
| 275 | ], |
| 276 | 'expand' => [ |
| 277 | 'description' => __( 'Relations to expand on each returned product.', 'surecart' ), |
| 278 | 'type' => 'array', |
| 279 | 'items' => [ |
| 280 | 'type' => 'string', |
| 281 | ], |
| 282 | 'default' => [], |
| 283 | ], |
| 284 | 'expand_mode' => [ |
| 285 | 'description' => __( 'Whether `expand` augments the default relations (`merge`) or replaces them (`replace`).', 'surecart' ), |
| 286 | 'type' => 'string', |
| 287 | 'enum' => [ 'merge', 'replace' ], |
| 288 | 'default' => 'merge', |
| 289 | ], |
| 290 | 'ids' => [ |
| 291 | 'description' => __( 'Ensure result set excludes specific IDs.', 'surecart' ), |
| 292 | 'type' => 'array', |
| 293 | 'items' => [ |
| 294 | 'type' => 'string', |
| 295 | ], |
| 296 | 'default' => [], |
| 297 | ], |
| 298 | 'product_group_ids' => [ |
| 299 | 'description' => __( 'Only return objects that belong to the given product groups.', 'surecart' ), |
| 300 | 'type' => 'array', |
| 301 | 'items' => [ |
| 302 | 'type' => 'string', |
| 303 | ], |
| 304 | 'default' => [], |
| 305 | ], |
| 306 | 'product_collection_ids' => [ |
| 307 | 'description' => __( 'Only return objects that belong to the given product collections.', 'surecart' ), |
| 308 | 'type' => 'array', |
| 309 | 'items' => [ |
| 310 | 'type' => 'string', |
| 311 | ], |
| 312 | 'default' => [], |
| 313 | ], |
| 314 | 'page' => [ |
| 315 | 'description' => esc_html__( 'The page of items you want returned.', 'surecart' ), |
| 316 | 'type' => 'integer', |
| 317 | ], |
| 318 | 'per_page' => [ |
| 319 | 'description' => esc_html__( 'A limit on the number of items to be returned, between 1 and 100.', 'surecart' ), |
| 320 | 'type' => 'integer', |
| 321 | ], |
| 322 | ]; |
| 323 | |
| 324 | /** |
| 325 | * Filter the products REST collection params. |
| 326 | * |
| 327 | * Allows plugins to register additional query args (e.g. custom |
| 328 | * metadata filters) without forking the controller. |
| 329 | * |
| 330 | * @param array $args Args keyed by param name. |
| 331 | * @return array |
| 332 | */ |
| 333 | return apply_filters( 'surecart/products/rest_args', $args ); |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Anyone can get a specific product. |
| 338 | * |
| 339 | * @param \WP_REST_Request $request Full details about the request. |
| 340 | * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise. |
| 341 | */ |
| 342 | public function get_item_permissions_check( $request ) { |
| 343 | $check = $this->forbidEditContextWithout( $request, 'edit_sc_products' ); |
| 344 | if ( is_wp_error( $check ) ) { |
| 345 | return $check; |
| 346 | } |
| 347 | |
| 348 | return true; |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Who can list products? |
| 353 | * |
| 354 | * @param \WP_REST_Request $request Full details about the request. |
| 355 | * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise. |
| 356 | */ |
| 357 | public function get_items_permissions_check( $request ) { |
| 358 | $check = $this->forbidEditContextWithout( $request, 'edit_sc_products' ); |
| 359 | if ( is_wp_error( $check ) ) { |
| 360 | return $check; |
| 361 | } |
| 362 | |
| 363 | if ( $request['archived'] ) { |
| 364 | return current_user_can( 'edit_sc_products' ); |
| 365 | } |
| 366 | |
| 367 | return true; |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * Create model. |
| 372 | * |
| 373 | * @param \WP_REST_Request $request Full details about the request. |
| 374 | * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise. |
| 375 | */ |
| 376 | public function create_item_permissions_check( $request ) { |
| 377 | $request['context'] = 'edit'; |
| 378 | return current_user_can( 'publish_sc_products' ); |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * Update model. |
| 383 | * |
| 384 | * @param \WP_REST_Request $request Full details about the request. |
| 385 | * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise. |
| 386 | */ |
| 387 | public function update_item_permissions_check( $request ) { |
| 388 | $request['context'] = 'edit'; |
| 389 | return current_user_can( 'edit_sc_products' ); |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Delete model. |
| 394 | * |
| 395 | * @param \WP_REST_Request $request Full details about the request. |
| 396 | * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise. |
| 397 | */ |
| 398 | public function delete_item_permissions_check( $request ) { |
| 399 | $request['context'] = 'edit'; |
| 400 | return current_user_can( 'delete_sc_products' ); |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * If we are editing, let's make sure the data comes back directly. |
| 405 | * |
| 406 | * @param \SureCart\Models\Product $model Product model. |
| 407 | * @param string $context The context of the request. |
| 408 | * |
| 409 | * @return array The filtered response. |
| 410 | */ |
| 411 | public function filter_response_by_context( $model, $context ) { |
| 412 | $response = parent::filter_response_by_context( $model, $context ); |
| 413 | |
| 414 | if ( ! is_array( $response ) || empty( $response['id'] ) ) { |
| 415 | return $response; |
| 416 | } |
| 417 | |
| 418 | if ( 'edit' === $context ) { |
| 419 | // Process the variants, it's in a data column, so we need to pull it out. |
| 420 | $response['variants'] = ! empty( $response['variants']['data'] ) ? $response['variants']['data'] : []; |
| 421 | // Process the variant_options, it's in a data column, so we need to pull it out. |
| 422 | $response['variant_options'] = ! empty( $response['variant_options']['data'] ) ? $response['variant_options']['data'] : []; |
| 423 | return $response; |
| 424 | } |
| 425 | |
| 426 | // view/embed: strip private data the schema can't reach on expansions. |
| 427 | return $this->stripPrivateProductFields( $response ); |
| 428 | } |
| 429 | } |
| 430 |