| 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 |
* Save Volume Entries Ability |
| 18 |
* |
| 19 |
* Saves or updates multiple files in the WindPress Simple File System at once. |
| 20 |
* |
| 21 |
* @since 3.2.0 |
| 22 |
*/ |
| 23 |
class SaveVolumeEntries |
| 24 |
{ |
| 25 |
/** |
| 26 |
* Execute the ability |
| 27 |
* |
| 28 |
* @param array $input Input with entries array |
| 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 |
$entries = $input['entries'] ?? []; |
| 37 |
if (empty($entries) || !is_array($entries)) { |
| 38 |
return new WP_Error('invalid_input', __('Entries array is required.', 'windpress'), ['status' => 400]); |
| 39 |
} |
| 40 |
foreach ($entries as $entry) { |
| 41 |
if (!is_array($entry)) { |
| 42 |
return new WP_Error('invalid_input', __('Each entry must be an object.', 'windpress'), ['status' => 400]); |
| 43 |
} |
| 44 |
if (($entry['content'] ?? null) === '') { |
| 45 |
return new WP_Error('invalid_input', __('Save abilities do not accept empty content because empty content deletes files. Use the delete or reset ability instead.', 'windpress'), ['status' => 400]); |
| 46 |
} |
| 47 |
} |
| 48 |
try { |
| 49 |
$result = Volume::save_entries($entries); |
| 50 |
if (!empty($result['errors']) || !empty($result['skipped'])) { |
| 51 |
return new WP_Error('save_failed', __('Some entries could not be saved.', 'windpress'), ['status' => empty($result['errors']) ? 400 : 500, 'details' => $result]); |
| 52 |
} |
| 53 |
$saved_count = count($result['saved']) + count($result['handled']); |
| 54 |
if ($saved_count === 0) { |
| 55 |
return new WP_Error('save_failed', __('No entries were saved.', 'windpress'), ['status' => 400, 'details' => $result]); |
| 56 |
} |
| 57 |
return ['success' => \true, 'message' => sprintf( |
| 58 |
/* translators: %d: number of entries */ |
| 59 |
_n('%d entry saved successfully.', '%d entries saved successfully.', $saved_count, 'windpress'), |
| 60 |
$saved_count |
| 61 |
)]; |
| 62 |
} catch (\Throwable $throwable) { |
| 63 |
return new WP_Error('save_failed', sprintf( |
| 64 |
/* translators: %s: error message */ |
| 65 |
__('Failed to save entries: %s', 'windpress'), |
| 66 |
$throwable->getMessage() |
| 67 |
), ['status' => 500]); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|