AbandonedCheckoutProtocolController.php
2 years ago
AbandonedCheckoutsController.php
3 years ago
AccountController.php
2 years ago
ActivationsController.php
3 years ago
AffiliationProductsController.php
2 years ago
AffiliationProtocolController.php
2 years ago
AffiliationRequestsController.php
2 years ago
AffiliationsController.php
2 years ago
AutoFeeProtocolController.php
5 months ago
AutoFeesController.php
5 months ago
BalanceTransactionsController.php
3 years ago
BatchesController.php
1 week ago
BrandController.php
4 months ago
BumpsController.php
5 days ago
BundleItemsController.php
5 days ago
CancellationActsController.php
3 years ago
CancellationReasonsController.php
3 years ago
ChargesController.php
2 years ago
CheckEmailController.php
11 months ago
CheckoutsController.php
5 months ago
ClicksController.php
2 years ago
CouponsController.php
4 years ago
CustomerController.php
1 month ago
CustomerNotificationProtocolController.php
2 years ago
CustomerPortalProtocolController.php
1 year ago
DisplayCurrencyController.php
1 year ago
DisputesController.php
9 months ago
DownloadsController.php
4 years ago
DraftCheckoutsController.php
2 years ago
ExportsController.php
2 years ago
FulfillmentsController.php
3 years ago
ImportRowsController.php
2 months ago
IncomingWebhooksController.php
2 months ago
IntegrationCatalogController.php
1 year ago
IntegrationProvidersController.php
4 years ago
IntegrationsController.php
2 months ago
InvoicesController.php
1 year ago
LicensesController.php
3 years ago
LineItemsController.php
1 year ago
LoginController.php
1 month ago
ManualPaymentMethodsController.php
3 years ago
MediasController.php
4 years ago
OrderController.php
1 year ago
OrderProtocolController.php
2 years ago
ParcelTemplateController.php
4 months ago
PaymentIntentsController.php
2 years ago
PaymentMethodsController.php
2 years ago
PayoutGroupsController.php
2 years ago
PayoutsController.php
2 years ago
PeriodsController.php
3 years ago
PluginInstallerController.php
1 week ago
PricesController.php
5 days ago
ProcessorController.php
3 years ago
ProductCollectionsController.php
5 days ago
ProductGroupsController.php
5 days ago
ProductMediaController.php
5 days ago
ProductsController.php
5 days ago
PromotionsController.php
4 years ago
ProvisionalAccountController.php
3 years ago
PurchasesController.php
4 years ago
ReferralItemsController.php
2 years ago
ReferralsController.php
2 years ago
RefundsController.php
4 years ago
RegisteredWebhookController.php
2 years ago
RestController.php
1 week ago
ReturnItemsController.php
2 years ago
ReturnReasonsController.php
2 years ago
ReturnRequestsController.php
2 years ago
ReviewProtocolController.php
5 months ago
ReviewsController.php
1 week ago
RuleSchemaController.php
5 months ago
SettingsController.php
5 days ago
ShippingMethodController.php
3 years ago
ShippingProfileController.php
3 years ago
ShippingProtocolController.php
3 years ago
ShippingRateController.php
3 years ago
ShippingZoneController.php
3 years ago
StatisticsController.php
2 years ago
SubscriptionProtocolController.php
2 years ago
SubscriptionsController.php
2 years ago
SwapsController.php
1 year ago
TaxOverrideController.php
2 years ago
TaxProtocolController.php
2 years ago
TaxRegistrationController.php
4 years ago
TaxZoneController.php
4 years ago
UploadsController.php
4 years ago
UpsellFunnelsController.php
2 years ago
UpsellsController.php
5 days ago
VariantOptionsController.php
5 days ago
VariantValuesController.php
5 days ago
VariantsController.php
5 days ago
VerificationCodeController.php
5 days ago
WebhookController.php
4 years ago
ProductMediaController.php
77 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Controllers\Rest; |
| 4 | |
| 5 | use SureCart\Concerns\RestrictsAnonymousReads; |
| 6 | use SureCart\Models\ProductMedia; |
| 7 | |
| 8 | /** |
| 9 | * Handle ProductMedia requests through the REST API |
| 10 | */ |
| 11 | class ProductMediaController extends RestController { |
| 12 | use RestrictsAnonymousReads; |
| 13 | |
| 14 | /** |
| 15 | * Always fetch with these subcollections. |
| 16 | * |
| 17 | * @var array |
| 18 | */ |
| 19 | protected $with = [ 'media' ]; |
| 20 | |
| 21 | /** |
| 22 | * Class to make the requests. |
| 23 | * |
| 24 | * @var string |
| 25 | */ |
| 26 | protected $class = ProductMedia::class; |
| 27 | |
| 28 | /** |
| 29 | * Capability that unlocks unrestricted reads. |
| 30 | * |
| 31 | * @var string |
| 32 | */ |
| 33 | protected $edit_capability = 'edit_sc_products'; |
| 34 | |
| 35 | /** |
| 36 | * Expands safe to forward for anonymous callers. |
| 37 | * |
| 38 | * @var array |
| 39 | */ |
| 40 | protected $anonymous_expands = [ 'media' ]; |
| 41 | |
| 42 | /** |
| 43 | * Query filters forced for anonymous callers. |
| 44 | * |
| 45 | * @var array |
| 46 | */ |
| 47 | protected $anonymous_scope = []; |
| 48 | |
| 49 | /** |
| 50 | * Download a media file. |
| 51 | * |
| 52 | * @param \WP_REST_Request $request Request object. |
| 53 | * |
| 54 | * @return integer|\WP_Error |
| 55 | */ |
| 56 | public function download( \WP_REST_Request $request ) { |
| 57 | $model = $this->middleware( new $this->class(), $request ); |
| 58 | if ( is_wp_error( $model ) ) { |
| 59 | return $model; |
| 60 | } |
| 61 | |
| 62 | $media = $model->with( [ 'media' ] )->find( $request['id'] ); |
| 63 | |
| 64 | if ( is_wp_error( $media ) ) { |
| 65 | return $media; |
| 66 | } |
| 67 | |
| 68 | $attachment_id = $media->download(); |
| 69 | |
| 70 | if ( is_wp_error( $attachment_id ) ) { |
| 71 | return $attachment_id; |
| 72 | } |
| 73 | |
| 74 | return $attachment_id; |
| 75 | } |
| 76 | } |
| 77 |