PluginProbe
WindPress – Tailwind CSS integration for WordPress / 3.2.91
WindPress – Tailwind CSS integration for WordPress v3.2.91
3.2.91 3.2.92 3.2.90 3.2.89 3.2.88 3.2.87 3.2.86 3.2.85 3.2.84 3.2.83 3.2.82 3.2.81 trunk 3.0.0 3.0.1 3.0.10 3.0.11 3.0.12 3.0.13 3.0.14 3.0.15 3.0.16 3.0.17 3.0.2 3.0.3 All 146 releases
windpress / src / Abilities / Abilities / SaveVolumeEntries.php

SaveVolumeEntries.php in WindPress – Tailwind CSS integration for WordPress 3.2.91, at src/Abilities/Abilities/SaveVolumeEntries.php

71 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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