PluginProbe
WindPress – Tailwind CSS integration for WordPress / 3.2.90
WindPress – Tailwind CSS integration for WordPress v3.2.90
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 3.0.4 3.0.5 All 144 releases
windpress / src / Abilities / Abilities / ResetVolumeEntry.php

ResetVolumeEntry.php in WindPress – Tailwind CSS integration for WordPress 3.2.90, at src/Abilities/Abilities/ResetVolumeEntry.php

105 lines 4.4 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 <suabahasa@gmail.com>
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 WIND_PRESS;
15 use WindPress\WindPress\Core\Runtime;
16 use WindPress\WindPress\Core\Volume;
17 use WP_Error;
18 /**
19 * Reset Volume Entry Ability
20 *
21 * Resets main.css, tailwind.config.js, or wizard files to their default content.
22 *
23 * @since 3.2.0
24 */
25 class ResetVolumeEntry
26 {
27 /**
28 * Execute the ability
29 *
30 * @param array $input Input with relative_path
31 * @return array|WP_Error Success status with default content or error
32 */
33 public static function execute($input)
34 {
35 if (!is_array($input)) {
36 return new WP_Error('invalid_input', __('Input must be an object.', 'windpress'), ['status' => 400]);
37 }
38 $relative_path = $input['relative_path'] ?? '';
39 // List of files that can be reset
40 $resettable_files = ['main.css', 'tailwind.config.js', 'wizard.js', 'wizard.css'];
41 if (!in_array($relative_path, $resettable_files, \true)) {
42 return new WP_Error('invalid_file', sprintf(
43 /* translators: %s: comma-separated list of file names */
44 __('Only these files can be reset: %s', 'windpress'),
45 implode(', ', $resettable_files)
46 ), ['status' => 400]);
47 }
48 $tailwind_version = Runtime::tailwindcss_version();
49 $stubs_dir = dirname(WIND_PRESS::FILE) . '/stubs/tailwindcss-v' . $tailwind_version;
50 // Determine the stub file path
51 $stub_file = $stubs_dir . '/' . $relative_path;
52 if (!file_exists($stub_file)) {
53 return new WP_Error('stub_not_found', sprintf(
54 /* translators: %s: file name */
55 __('Default content for "%s" is not available for Tailwind CSS v%d.', 'windpress'),
56 $relative_path,
57 $tailwind_version
58 ), ['status' => 404]);
59 }
60 try {
61 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local file
62 $default_content = file_get_contents($stub_file);
63 if ($default_content === \false) {
64 return new WP_Error('stub_not_readable', sprintf(
65 /* translators: %s: file name */
66 __('Default content for "%s" could not be read.', 'windpress'),
67 $relative_path
68 ), ['status' => 500]);
69 }
70 // Get the current entry to retrieve its signature
71 $entries = Volume::get_entries();
72 $current_entry = null;
73 foreach ($entries as $entry) {
74 if ($entry['relative_path'] === $relative_path) {
75 $current_entry = $entry;
76 break;
77 }
78 }
79 // If entry doesn't exist, create a new signature
80 if (!$current_entry) {
81 $signature = wp_create_nonce(sprintf('%s:%s', WIND_PRESS::WP_OPTION, $relative_path));
82 } else {
83 $signature = $current_entry['signature'];
84 }
85 // Save the entry with default content
86 $entry_to_reset = ['name' => basename($relative_path), 'relative_path' => $relative_path, 'content' => $default_content, 'handler' => 'internal', 'signature' => $signature];
87 $result = Volume::save_entries([$entry_to_reset]);
88 if (!empty($result['errors']) || !empty($result['skipped']) || empty($result['saved'])) {
89 return new WP_Error('reset_failed', __('The entry could not be reset.', 'windpress'), ['status' => empty($result['errors']) ? 400 : 500, 'details' => $result]);
90 }
91 return ['success' => \true, 'message' => sprintf(
92 /* translators: %s: file name */
93 __('File "%s" reset to default successfully.', 'windpress'),
94 $relative_path
95 ), 'content' => $default_content];
96 } catch (\Throwable $throwable) {
97 return new WP_Error('reset_failed', sprintf(
98 /* translators: %s: error message */
99 __('Failed to reset entry: %s', 'windpress'),
100 $throwable->getMessage()
101 ), ['status' => 500]);
102 }
103 }
104 }
105