| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the WindPress package. |
| 5 |
* |
| 6 |
* (c) Joshua Gugun Siagian <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
declare (strict_types=1); |
| 12 |
namespace WindPress\WindPress\Abilities\Abilities; |
| 13 |
|
| 14 |
use WindPress\WindPress\Core\Volume; |
| 15 |
use WP_Error; |
| 16 |
/** |
| 17 |
* Delete Volume Entry Ability |
| 18 |
* |
| 19 |
* Deletes a file from the WindPress Simple File System. |
| 20 |
* |
| 21 |
* @since 3.2.0 |
| 22 |
*/ |
| 23 |
class DeleteVolumeEntry |
| 24 |
{ |
| 25 |
/** |
| 26 |
* Execute the ability |
| 27 |
* |
| 28 |
* @param array $input Input with relative_path and signature |
| 29 |
* @return array|WP_Error Success status or error |
| 30 |
*/ |
| 31 |
public static function execute($input) |
| 32 |
{ |
| 33 |
if (!is_array($input)) { |
| 34 |
return new WP_Error('invalid_input', __('Input must be an object.', 'windpress'), ['status' => 400]); |
| 35 |
} |
| 36 |
$relative_path = $input['relative_path'] ?? ''; |
| 37 |
$signature = $input['signature'] ?? ''; |
| 38 |
if (empty($relative_path)) { |
| 39 |
return new WP_Error('invalid_input', __('Relative path is required.', 'windpress'), ['status' => 400]); |
| 40 |
} |
| 41 |
if (empty($signature)) { |
| 42 |
return new WP_Error('invalid_input', __('Signature is required for security verification.', 'windpress'), ['status' => 400]); |
| 43 |
} |
| 44 |
try { |
| 45 |
// To delete a file, we save an entry with empty content |
| 46 |
// The Volume::save_entries method handles deletion when content is empty |
| 47 |
$entry_to_delete = [ |
| 48 |
'name' => basename($relative_path), |
| 49 |
'relative_path' => $relative_path, |
| 50 |
'content' => '', |
| 51 |
// Empty content triggers deletion |
| 52 |
'handler' => 'internal', |
| 53 |
'signature' => $signature, |
| 54 |
]; |
| 55 |
$result = Volume::save_entries([$entry_to_delete]); |
| 56 |
if (!empty($result['errors']) || !empty($result['skipped']) || empty($result['deleted'])) { |
| 57 |
return new WP_Error('delete_failed', __('The entry could not be deleted. Verify the signature and relative path.', 'windpress'), ['status' => empty($result['errors']) ? 400 : 500, 'details' => $result]); |
| 58 |
} |
| 59 |
return ['success' => \true, 'message' => sprintf( |
| 60 |
/* translators: %s: file path */ |
| 61 |
__('File "%s" deleted successfully.', 'windpress'), |
| 62 |
$relative_path |
| 63 |
)]; |
| 64 |
} catch (\Throwable $throwable) { |
| 65 |
return new WP_Error('delete_failed', sprintf( |
| 66 |
/* translators: %s: error message */ |
| 67 |
__('Failed to delete entry: %s', 'windpress'), |
| 68 |
$throwable->getMessage() |
| 69 |
), ['status' => 500]); |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|