| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\FileSystem; |
| 4 |
|
| 5 |
use FluentCart\Api\StorageDrivers; |
| 6 |
use FluentCart\Framework\Support\Arr; |
| 7 |
|
| 8 |
/** |
| 9 |
* Resolves which bucket a storage driver is configured to use. |
| 10 |
* |
| 11 |
* A downloadable file's bucket is server state: the store configures exactly one |
| 12 |
* bucket per driver, and the admin file browser only ever echoes that same bucket |
| 13 |
* back into the payload. Trusting the submitted value let a product editor point a |
| 14 |
* download at any sibling bucket the store credentials could read, so both the |
| 15 |
* write path (ProductDownloadablesController) and the read path (DownloadService) |
| 16 |
* resolve it here instead. |
| 17 |
*/ |
| 18 |
class StorageBucketResolver |
| 19 |
{ |
| 20 |
/** |
| 21 |
* @param string $driver driver slug, e.g. 'local', 's3', 'r2' |
| 22 |
* @return string|\WP_Error '' for drivers that have no bucket (local); |
| 23 |
* WP_Error when the driver is unknown, disabled, |
| 24 |
* or bucket-backed without a configured bucket |
| 25 |
*/ |
| 26 |
public static function resolve($driver) |
| 27 |
{ |
| 28 |
$driver = sanitize_text_field((string)$driver); |
| 29 |
|
| 30 |
if ($driver === '') { |
| 31 |
return new \WP_Error( |
| 32 |
'fluent_cart_storage_driver_missing', |
| 33 |
__('A storage driver is required.', 'fluent-cart') |
| 34 |
); |
| 35 |
} |
| 36 |
|
| 37 |
$instance = Arr::get((new StorageDrivers())->getActive(true), $driver . '.instance'); |
| 38 |
|
| 39 |
if (empty($instance)) { |
| 40 |
return new \WP_Error( |
| 41 |
'fluent_cart_storage_driver_invalid', |
| 42 |
/* translators: %1$s: the storage driver slug that was submitted */ |
| 43 |
sprintf(__('The storage driver "%1$s" is not enabled.', 'fluent-cart'), $driver) |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
// Third-party drivers only have to implement BaseStorageInterface, which |
| 48 |
// declares neither method; treat those as bucketless rather than fataling. |
| 49 |
if (!method_exists($instance, 'hasBucket') || !$instance->hasBucket()) { |
| 50 |
return ''; |
| 51 |
} |
| 52 |
|
| 53 |
$bucket = method_exists($instance, 'getEffectiveBucket') |
| 54 |
? (string)$instance->getEffectiveBucket() |
| 55 |
: ''; |
| 56 |
|
| 57 |
if ($bucket === '') { |
| 58 |
return new \WP_Error( |
| 59 |
'fluent_cart_storage_bucket_missing', |
| 60 |
/* translators: %1$s: the storage driver slug that was submitted */ |
| 61 |
sprintf(__('No bucket is configured for the "%1$s" storage driver.', 'fluent-cart'), $driver) |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
return $bucket; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Read-path variant: never fails, so a misconfigured driver degrades to the |
| 70 |
* driver's own default bucket instead of breaking an existing download. |
| 71 |
* Returns null when nothing can be resolved, which every driver reads as |
| 72 |
* "use my configured bucket". |
| 73 |
* |
| 74 |
* @param string $driver |
| 75 |
* @return string|null |
| 76 |
*/ |
| 77 |
public static function resolveOrNull($driver) |
| 78 |
{ |
| 79 |
$bucket = static::resolve($driver); |
| 80 |
|
| 81 |
if (is_wp_error($bucket) || $bucket === '') { |
| 82 |
return null; |
| 83 |
} |
| 84 |
|
| 85 |
return $bucket; |
| 86 |
} |
| 87 |
} |
| 88 |
|