| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentCart\Api\Resource\ProductDownloadResource; |
| 6 |
use FluentCart\App\Helpers\Helper; |
| 7 |
use FluentCart\App\Http\Requests\ProductDownloadable\ProductDownloadableBulkFileRequest; |
| 8 |
use FluentCart\App\Http\Requests\ProductDownloadable\ProductDownloadableFileRequest; |
| 9 |
use FluentCart\App\Models\Product; |
| 10 |
use FluentCart\App\Models\ProductDetail; |
| 11 |
use FluentCart\App\Models\ProductDownload; |
| 12 |
use FluentCart\App\Services\URL; |
| 13 |
use FluentCart\Framework\Http\Request\Request; |
| 14 |
use FluentCart\Framework\Http\URL as BaseUrl; |
| 15 |
use FluentCart\Framework\Support\Arr; |
| 16 |
use FluentCart\Framework\Support\Str; |
| 17 |
use FluentCart\Framework\Validator\Validator; |
| 18 |
|
| 19 |
class ProductDownloadablesController extends Controller |
| 20 |
{ |
| 21 |
public function syncDownloadableFiles(Request $request): \WP_REST_Response |
| 22 |
{ |
| 23 |
$productDownloadableBulkFileRequest = new ProductDownloadableBulkFileRequest(); |
| 24 |
$validator = Validator::make($request->all(), $productDownloadableBulkFileRequest->rules()); |
| 25 |
$validationErrors = []; |
| 26 |
|
| 27 |
|
| 28 |
if ($validator->fails()) { |
| 29 |
$validationErrors = $validator->errors(); |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
if (count($validationErrors) !== 0) { |
| 34 |
return $this->sendError($validationErrors); |
| 35 |
} |
| 36 |
|
| 37 |
$data = $request->get('downloadable_files'); |
| 38 |
$sanitizedData = []; |
| 39 |
|
| 40 |
foreach ($data as $downloadableFile) { |
| 41 |
|
| 42 |
$variationIds = Arr::get($downloadableFile, 'product_variation_id'); |
| 43 |
if (is_array($variationIds)) { |
| 44 |
$variationIds = array_map('intval', $variationIds); |
| 45 |
} else { |
| 46 |
$variationIds = []; |
| 47 |
} |
| 48 |
|
| 49 |
$sanitizedData[] = [ |
| 50 |
'id' => intval(Arr::get($downloadableFile, 'id')), |
| 51 |
'post_id' => intval(Arr::get($downloadableFile, 'post_id')), |
| 52 |
'product_variation_id' => $variationIds, |
| 53 |
'title' => sanitize_text_field(Arr::get($downloadableFile, 'title')), |
| 54 |
'type' => sanitize_text_field(Arr::get($downloadableFile, 'type')), |
| 55 |
'driver' => sanitize_text_field(Arr::get($downloadableFile, 'driver')), |
| 56 |
'bucket' => sanitize_text_field(Arr::get($downloadableFile, 'bucket')), |
| 57 |
'file_name' => sanitize_text_field(Arr::get($downloadableFile, 'file_name')), |
| 58 |
'file_path' => sanitize_text_field(Arr::get($downloadableFile, 'file_path')), |
| 59 |
'file_url' => sanitize_text_field(Arr::get($downloadableFile, 'file_url')), |
| 60 |
'settings' => Arr::get($downloadableFile, 'settings'), |
| 61 |
'serial' => sanitize_text_field(Arr::get($downloadableFile, 'serial')), |
| 62 |
'file_size' => sanitize_text_field(Arr::get($downloadableFile, 'file_size')), |
| 63 |
]; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
$fileData['downloadable_files'] = $sanitizedData; |
| 68 |
|
| 69 |
|
| 70 |
unset($fileData['downloadable_files']['*']); |
| 71 |
|
| 72 |
$productId = $request->getSafe('postId', 'intval'); |
| 73 |
|
| 74 |
foreach ($fileData['downloadable_files'] as &$file) { |
| 75 |
|
| 76 |
$file['download_identifier'] = Str::uuid(); |
| 77 |
$file['post_id'] = $productId; |
| 78 |
$file['file_path'] = !empty($file['file_path']) ? $file['file_path'] : $file['file_name']; |
| 79 |
$file['file_url'] = !empty($file['file_url']) ? $file['file_url'] : $file['file_name']; |
| 80 |
$file['product_variation_id'] = json_encode( |
| 81 |
Arr::get($file, 'product_variation_id', []) |
| 82 |
); |
| 83 |
|
| 84 |
$fileName = $file['file_name']; |
| 85 |
$fileName = explode('_____fluent-cart_____', $fileName)[0]; |
| 86 |
$fileName = explode('__fluent-cart__', $fileName)[0]; |
| 87 |
$file['file_name'] = $fileName; |
| 88 |
|
| 89 |
$file['settings'] = json_encode( |
| 90 |
Arr::get($file, 'settings', []) |
| 91 |
); |
| 92 |
|
| 93 |
unset($file['id']); |
| 94 |
unset($file['bucket']); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
ProductDetail::query()->where('post_id', $productId)->update([ |
| 99 |
'manage_downloadable' => 1 |
| 100 |
]); |
| 101 |
|
| 102 |
|
| 103 |
$isCreated = ProductDownload::query()->insert($fileData['downloadable_files']); |
| 104 |
if ($isCreated) { |
| 105 |
return $this->sendSuccess([ |
| 106 |
'downloadable_files' => ProductDownloadResource::search(['post_id' => $productId]) |
| 107 |
]); |
| 108 |
} else { |
| 109 |
return $this->sendError([ |
| 110 |
'message' => __('Failed to attach downloadable files', 'fluent-cart') |
| 111 |
]); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
public function getDownloadableUrl($downloadableId) |
| 116 |
{ |
| 117 |
$productDownload = ProductDownload::query()->findOrFail($downloadableId); |
| 118 |
|
| 119 |
|
| 120 |
if (empty($productDownload)) { |
| 121 |
return $this->sendError([ |
| 122 |
'message' => __('Failed to generate downloadable link', 'fluent-cart') |
| 123 |
]); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
return $this->sendSuccess([ |
| 128 |
'url' => Helper::generateDownloadFileLink($productDownload, null, 60 * 60 * 24 * 7, true) |
| 129 |
]); |
| 130 |
|
| 131 |
|
| 132 |
} |
| 133 |
|
| 134 |
public function update(ProductDownloadableFileRequest $request, $downloadId) |
| 135 |
{ |
| 136 |
$productDownload = ProductDownload::query()->findOrFail($downloadId); |
| 137 |
$data = $request->getSafe($request->sanitize()); |
| 138 |
|
| 139 |
|
| 140 |
$fileName = Arr::get($data, 'file_name'); |
| 141 |
|
| 142 |
$filePath = Arr::get($data, 'file_path') ?: $fileName; |
| 143 |
$fileUrl = Arr::get($data, 'file_url') ?: $fileName; |
| 144 |
$productVariationId = Arr::get($data, 'product_variation_id', []); |
| 145 |
$fileName = explode('_____fluent-cart_____', $fileName)[0]; |
| 146 |
$fileName = explode('__fluent-cart__', $fileName)[0]; |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
if (is_array($productVariationId)) { |
| 151 |
$productVariationId = array_values(array_filter(array_map('intval', $productVariationId))); |
| 152 |
} else { |
| 153 |
$productVariationId = []; |
| 154 |
} |
| 155 |
|
| 156 |
$productDownload->product_variation_id = $productVariationId; |
| 157 |
$productDownload->title = Arr::get($data, 'title'); |
| 158 |
$productDownload->type = Arr::get($data, 'type'); |
| 159 |
$productDownload->driver = Arr::get($data, 'driver'); |
| 160 |
$productDownload->file_name = $fileName; |
| 161 |
$productDownload->file_path = $filePath; |
| 162 |
$productDownload->file_url = $fileUrl; |
| 163 |
$productDownload->settings = Arr::get($data, 'settings'); |
| 164 |
$productDownload->serial = Arr::get($data, 'serial'); |
| 165 |
|
| 166 |
if ($productDownload->save()) { |
| 167 |
return $this->sendSuccess([ |
| 168 |
'message' => __('Product downloadable files updated successfully', 'fluent-cart') |
| 169 |
]); |
| 170 |
} else { |
| 171 |
return $this->sendError([ |
| 172 |
'message' => __('Failed to update product downloadable files', 'fluent-cart') |
| 173 |
]); |
| 174 |
} |
| 175 |
|
| 176 |
} |
| 177 |
|
| 178 |
public function delete($downloadableFileId) |
| 179 |
{ |
| 180 |
$response = ProductDownloadResource::delete($downloadableFileId); |
| 181 |
if (is_wp_error($response)) { |
| 182 |
return $this->sendError([ |
| 183 |
'message' => $response->get_error_message() |
| 184 |
]); |
| 185 |
} |
| 186 |
return $this->sendSuccess([ |
| 187 |
'message' => $response['message'] |
| 188 |
]); |
| 189 |
} |
| 190 |
|
| 191 |
public function updateDownloadableFile(Request $request, Product $product, ProductDownload $productDownload) |
| 192 |
{ |
| 193 |
|
| 194 |
} |
| 195 |
} |
| 196 |
|