AbandonedCheckoutProtocolRestServiceProvider.php
3 years ago
AbandonedCheckoutRestServiceProvider.php
2 years ago
AccountRestServiceProvider.php
3 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
4 days ago
BlockPatternsRestServiceProvider.php
3 years ago
BrandRestServiceProvider.php
4 months ago
BumpRestServiceProvider.php
1 year ago
CancellationActRestServiceProvider.php
1 year ago
CancellationReasonRestServiceProvider.php
1 year ago
ChargesRestServiceProvider.php
3 years ago
CheckEmailRestServiceProvider.php
3 years ago
CheckoutRestServiceProvider.php
1 year ago
ClicksRestServiceProvider.php
2 years ago
CouponRestServiceProvider.php
3 years ago
CustomerNotificationProtocolRestServiceProvider.php
3 years ago
CustomerPortalProtocolRestServiceProvider.php
1 year ago
CustomerRestServiceProvider.php
1 month ago
DisplayCurrencyRestServiceProvider.php
1 year ago
DisputesRestServiceProvider.php
9 months ago
DownloadRestServiceProvider.php
2 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
3 years ago
IntegrationsCatalogRestServiceProvider.php
1 year ago
IntegrationsRestServiceProvider.php
3 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
3 months ago
PaymentIntentsRestServiceProvider.php
3 years ago
PaymentMethodsRestServiceProvider.php
2 years ago
PayoutGroupsRestServiceProvider.php
2 years ago
PayoutsRestServiceProvider.php
4 months ago
PeriodRestServiceProvider.php
3 years ago
PluginInstallerRestServiceProvider.php
4 days ago
PriceRestServiceProvider.php
1 year ago
ProcessorRestServiceProvider.php
3 years ago
ProductCollectionsRestServiceProvider.php
4 days ago
ProductGroupsRestServiceProvider.php
3 years ago
ProductMediaRestServiceProvider.php
1 year ago
ProductsRestServiceProvider.php
4 days ago
PromotionRestServiceProvider.php
3 years ago
ProvisionalAccountRestServiceProvider.php
3 years ago
PurchasesRestServiceProvider.php
3 years ago
ReferralItemsRestServiceProvider.php
2 years ago
ReferralsRestServiceProvider.php
2 years ago
RefundsRestServiceProvider.php
3 years ago
RegisteredWebhookRestServiceProvider.php
2 years ago
RestServiceInterface.php
3 years ago
RestServiceProvider.php
1 year ago
ReturnItemsRestServiceProvider.php
2 years ago
ReturnReasonsRestServiceProvider.php
2 years ago
ReturnRequestsRestServiceProvider.php
2 years ago
ReviewProtocolRestServiceProvider.php
4 months ago
ReviewsRestServiceProvider.php
4 days 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
3 years ago
UpsellFunnelRestServiceProvider.php
1 year ago
UpsellRestServiceProvider.php
1 year 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
ProductsRestServiceProvider.php
339 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Rest; |
| 4 | |
| 5 | use SureCart\Rest\RestServiceInterface; |
| 6 | use SureCart\Controllers\Rest\ProductsController; |
| 7 | |
| 8 | /** |
| 9 | * Service provider for Price Rest Requests |
| 10 | */ |
| 11 | class ProductsRestServiceProvider extends RestServiceProvider implements RestServiceInterface { |
| 12 | /** |
| 13 | * Endpoint. |
| 14 | * |
| 15 | * @var string |
| 16 | */ |
| 17 | protected $endpoint = 'products'; |
| 18 | |
| 19 | /** |
| 20 | * Rest Controller |
| 21 | * |
| 22 | * @var string |
| 23 | */ |
| 24 | protected $controller = ProductsController::class; |
| 25 | |
| 26 | /** |
| 27 | * Register Additional REST Routes |
| 28 | * |
| 29 | * @return void |
| 30 | */ |
| 31 | public function registerRoutes() { |
| 32 | register_rest_route( |
| 33 | "$this->name/v$this->version", |
| 34 | $this->endpoint . '/(?P<id>\S+)/sync/', |
| 35 | [ |
| 36 | [ |
| 37 | 'methods' => \WP_REST_Server::EDITABLE, |
| 38 | 'callback' => $this->callback( $this->controller, 'sync' ), |
| 39 | 'permission_callback' => [ $this, 'update_item_permissions_check' ], |
| 40 | ], |
| 41 | // Register our schema callback. |
| 42 | 'schema' => [ $this, 'get_item_schema' ], |
| 43 | ] |
| 44 | ); |
| 45 | register_rest_route( |
| 46 | "$this->name/v$this->version", |
| 47 | $this->endpoint . '/sync_all', |
| 48 | [ |
| 49 | [ |
| 50 | 'methods' => \WP_REST_Server::EDITABLE, |
| 51 | 'callback' => $this->callback( $this->controller, 'syncAll' ), |
| 52 | 'permission_callback' => [ $this, 'update_item_permissions_check' ], |
| 53 | ], |
| 54 | // Register our schema callback. |
| 55 | 'schema' => [ $this, 'get_item_schema' ], |
| 56 | ] |
| 57 | ); |
| 58 | register_rest_route( |
| 59 | "$this->name/v$this->version", |
| 60 | $this->endpoint . '/(?P<id>\S+)/duplicate', |
| 61 | [ |
| 62 | 'methods' => \WP_REST_Server::CREATABLE, |
| 63 | 'callback' => $this->callback( $this->controller, 'duplicate' ), |
| 64 | 'permission_callback' => [ $this, 'create_item_permissions_check' ], |
| 65 | ] |
| 66 | ); |
| 67 | register_rest_route( |
| 68 | "$this->name/v$this->version", |
| 69 | $this->endpoint . '/import_woocommerce', |
| 70 | [ |
| 71 | [ |
| 72 | 'methods' => \WP_REST_Server::CREATABLE, |
| 73 | 'callback' => $this->callback( $this->controller, 'importWooCommerce' ), |
| 74 | 'permission_callback' => [ $this, 'update_item_permissions_check' ], |
| 75 | ], |
| 76 | ] |
| 77 | ); |
| 78 | register_rest_route( |
| 79 | "$this->name/v$this->version", |
| 80 | $this->endpoint . '/woocommerce_count', |
| 81 | [ |
| 82 | [ |
| 83 | 'methods' => \WP_REST_Server::READABLE, |
| 84 | 'callback' => $this->callback( $this->controller, 'wooCommerceCount' ), |
| 85 | // Uses edit-level permission (edit_sc_products) intentionally: admin-only import feature, only product editors should see the WooCommerce count. |
| 86 | 'permission_callback' => [ $this, 'update_item_permissions_check' ], |
| 87 | ], |
| 88 | ] |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get our sample schema for a post. |
| 94 | * |
| 95 | * @return array The sample schema for a post |
| 96 | */ |
| 97 | public function get_item_schema() { |
| 98 | if ( $this->schema ) { |
| 99 | // Since WordPress 5.3, the schema can be cached in the $schema property. |
| 100 | return $this->schema; |
| 101 | } |
| 102 | |
| 103 | $this->schema = [ |
| 104 | // This tells the spec of JSON Schema we are using which is draft 4. |
| 105 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 106 | // The title property marks the identity of the resource. |
| 107 | 'title' => $this->endpoint, |
| 108 | 'type' => 'object', |
| 109 | // In JSON Schema you can specify object properties in the properties attribute. |
| 110 | 'properties' => [ |
| 111 | 'id' => [ |
| 112 | 'description' => esc_html__( 'Unique identifier for the object.', 'surecart' ), |
| 113 | 'type' => 'string', |
| 114 | 'context' => [ 'view', 'edit', 'embed' ], |
| 115 | 'readonly' => true, |
| 116 | ], |
| 117 | 'file_upload_ids' => [ |
| 118 | 'description' => esc_html__( 'Files attached to the product.', 'surecart' ), |
| 119 | 'context' => [ 'edit' ], |
| 120 | 'type' => 'array', |
| 121 | 'items' => [ |
| 122 | 'type' => 'string', |
| 123 | ], |
| 124 | ], |
| 125 | 'metadata' => [ |
| 126 | 'description' => esc_html__( 'Stored product metadata', 'surecart' ), |
| 127 | 'type' => 'object', |
| 128 | 'properties' => [ |
| 129 | 'wp_created_by' => [ |
| 130 | 'type' => 'integer', |
| 131 | 'context' => [ 'edit' ], |
| 132 | 'readonly' => true, |
| 133 | ], |
| 134 | ], |
| 135 | ], |
| 136 | 'metrics' => [ |
| 137 | 'description' => esc_html__( 'Top level metrics for the product.', 'surecart' ), |
| 138 | 'readonly' => true, |
| 139 | 'type' => 'object', |
| 140 | 'properties' => [ |
| 141 | 'currency' => [ |
| 142 | 'type' => 'string', |
| 143 | ], |
| 144 | 'max_price_amount' => [ |
| 145 | 'type' => 'integer', |
| 146 | ], |
| 147 | 'min_price_amount' => [ |
| 148 | 'type' => 'integer', |
| 149 | ], |
| 150 | 'prices_count' => [ |
| 151 | 'type' => 'integer', |
| 152 | ], |
| 153 | ], |
| 154 | 'context' => [ 'edit' ], |
| 155 | ], |
| 156 | ], |
| 157 | ]; |
| 158 | |
| 159 | return $this->schema; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Get the collection params. |
| 164 | * |
| 165 | * @return array |
| 166 | */ |
| 167 | public function get_collection_params() { |
| 168 | $args = [ |
| 169 | 'archived' => [ |
| 170 | 'description' => esc_html__( 'Whether to get archived products or not.', 'surecart' ), |
| 171 | 'type' => 'boolean', |
| 172 | ], |
| 173 | 'recurring' => [ |
| 174 | 'description' => esc_html__( 'Only return products that are recurring or not recurring (one time).', 'surecart' ), |
| 175 | 'type' => 'boolean', |
| 176 | ], |
| 177 | 'query' => [ |
| 178 | 'description' => __( 'The query to be used for full text search of this collection.', 'surecart' ), |
| 179 | 'type' => 'string', |
| 180 | ], |
| 181 | 'sort' => [ |
| 182 | 'description' => __( 'Sort directive in the form `field:direction`, e.g. `cataloged_at:desc`.', 'surecart' ), |
| 183 | 'type' => 'string', |
| 184 | ], |
| 185 | 'expand' => [ |
| 186 | 'description' => __( 'Relations to expand on each returned product.', 'surecart' ), |
| 187 | 'type' => 'array', |
| 188 | 'items' => [ |
| 189 | 'type' => 'string', |
| 190 | ], |
| 191 | 'default' => [], |
| 192 | ], |
| 193 | 'expand_mode' => [ |
| 194 | 'description' => __( 'Whether `expand` augments the default relations (`merge`) or replaces them (`replace`).', 'surecart' ), |
| 195 | 'type' => 'string', |
| 196 | 'enum' => [ 'merge', 'replace' ], |
| 197 | 'default' => 'merge', |
| 198 | ], |
| 199 | 'ids' => [ |
| 200 | 'description' => __( 'Ensure result set excludes specific IDs.', 'surecart' ), |
| 201 | 'type' => 'array', |
| 202 | 'items' => [ |
| 203 | 'type' => 'string', |
| 204 | ], |
| 205 | 'default' => [], |
| 206 | ], |
| 207 | 'product_group_ids' => [ |
| 208 | 'description' => __( 'Only return objects that belong to the given product groups.', 'surecart' ), |
| 209 | 'type' => 'array', |
| 210 | 'items' => [ |
| 211 | 'type' => 'string', |
| 212 | ], |
| 213 | 'default' => [], |
| 214 | ], |
| 215 | 'product_collection_ids' => [ |
| 216 | 'description' => __( 'Only return objects that belong to the given product collections.', 'surecart' ), |
| 217 | 'type' => 'array', |
| 218 | 'items' => [ |
| 219 | 'type' => 'string', |
| 220 | ], |
| 221 | 'default' => [], |
| 222 | ], |
| 223 | 'page' => [ |
| 224 | 'description' => esc_html__( 'The page of items you want returned.', 'surecart' ), |
| 225 | 'type' => 'integer', |
| 226 | ], |
| 227 | 'per_page' => [ |
| 228 | 'description' => esc_html__( 'A limit on the number of items to be returned, between 1 and 100.', 'surecart' ), |
| 229 | 'type' => 'integer', |
| 230 | ], |
| 231 | ]; |
| 232 | |
| 233 | /** |
| 234 | * Filter the products REST collection params. |
| 235 | * |
| 236 | * Allows plugins to register additional query args (e.g. custom |
| 237 | * metadata filters) without forking the controller. |
| 238 | * |
| 239 | * @param array $args Args keyed by param name. |
| 240 | * @return array |
| 241 | */ |
| 242 | return apply_filters( 'surecart/products/rest_args', $args ); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Anyone can get a specific product. |
| 247 | * |
| 248 | * @param \WP_REST_Request $request Full details about the request. |
| 249 | * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise. |
| 250 | */ |
| 251 | public function get_item_permissions_check( $request ) { |
| 252 | if ( 'edit' === $request['context'] && ! current_user_can( 'edit_sc_products' ) ) { |
| 253 | return new \WP_Error( |
| 254 | 'rest_forbidden_context', |
| 255 | __( 'Sorry, you are not allowed to edit products.', 'surecart' ), |
| 256 | array( 'status' => rest_authorization_required_code() ) |
| 257 | ); |
| 258 | } |
| 259 | |
| 260 | return true; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Who can list products? |
| 265 | * |
| 266 | * @param \WP_REST_Request $request Full details about the request. |
| 267 | * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise. |
| 268 | */ |
| 269 | public function get_items_permissions_check( $request ) { |
| 270 | if ( 'edit' === $request['context'] && ! current_user_can( 'edit_sc_products' ) ) { |
| 271 | return new \WP_Error( |
| 272 | 'rest_forbidden_context', |
| 273 | __( 'Sorry, you are not allowed to edit products.', 'surecart' ), |
| 274 | array( 'status' => rest_authorization_required_code() ) |
| 275 | ); |
| 276 | } |
| 277 | |
| 278 | if ( $request['archived'] ) { |
| 279 | return current_user_can( 'edit_sc_products' ); |
| 280 | } |
| 281 | |
| 282 | return true; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Create model. |
| 287 | * |
| 288 | * @param \WP_REST_Request $request Full details about the request. |
| 289 | * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise. |
| 290 | */ |
| 291 | public function create_item_permissions_check( $request ) { |
| 292 | $request['context'] = 'edit'; |
| 293 | return current_user_can( 'publish_sc_products' ); |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Update model. |
| 298 | * |
| 299 | * @param \WP_REST_Request $request Full details about the request. |
| 300 | * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise. |
| 301 | */ |
| 302 | public function update_item_permissions_check( $request ) { |
| 303 | $request['context'] = 'edit'; |
| 304 | return current_user_can( 'edit_sc_products' ); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Delete model. |
| 309 | * |
| 310 | * @param \WP_REST_Request $request Full details about the request. |
| 311 | * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise. |
| 312 | */ |
| 313 | public function delete_item_permissions_check( $request ) { |
| 314 | $request['context'] = 'edit'; |
| 315 | return current_user_can( 'delete_sc_products' ); |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * If we are editing, let's make sure the data comes back directly. |
| 320 | * |
| 321 | * @param \SureCart\Models\Product $model Product model. |
| 322 | * @param string $context The context of the request. |
| 323 | * |
| 324 | * @return array The filtered response. |
| 325 | */ |
| 326 | public function filter_response_by_context( $model, $context ) { |
| 327 | $response = parent::filter_response_by_context( $model, $context ); |
| 328 | |
| 329 | if ( 'edit' === $context && is_array( $response ) && ! empty( $response['id'] ) ) { |
| 330 | // Process the variants, it's in a data column, so we need to pull it out. |
| 331 | $response['variants'] = ! empty( $response['variants']['data'] ) ? $response['variants']['data'] : []; |
| 332 | // Process the variant_options, it's in a data column, so we need to pull it out. |
| 333 | $response['variant_options'] = ! empty( $response['variant_options']['data'] ) ? $response['variant_options']['data'] : []; |
| 334 | } |
| 335 | |
| 336 | return $response; |
| 337 | } |
| 338 | } |
| 339 |