PluginProbe
WindPress – Tailwind CSS integration for WordPress / 3.0.13
WindPress – Tailwind CSS integration for WordPress v3.0.13
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 3.0.6 3.0.7 All 142 releases
windpress / src / Core / Volume.php

Volume.php in WindPress – Tailwind CSS integration for WordPress 3.0.13, at src/Core/Volume.php

109 lines 5.0 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\Core;
13
14 use WindPressDeps\Symfony\Component\Finder\Finder;
15 use WIND_PRESS;
16 use WindPress\WindPress\Utils\Common;
17 /**
18 * @since 3.1.11
19 */
20 class Volume
21 {
22 public static function get_entries() : array
23 {
24 $entries = [];
25 $data_dir = \wp_upload_dir()['basedir'] . WIND_PRESS::DATA_DIR;
26 if (!\file_exists($data_dir)) {
27 \wp_mkdir_p($data_dir);
28 }
29 $finder = new Finder();
30 $finder->ignoreUnreadableDirs()->in($data_dir)->files()->followLinks()->name(['*.css', '*.js']);
31 \do_action('f!windpress/core/volume:get_entries.finder', $finder);
32 foreach ($finder as $file) {
33 if (!\is_readable($file->getPathname())) {
34 continue;
35 }
36 $entries[] = ['name' => $file->getFilename(), 'relative_path' => $file->getRelativePathname(), 'content' => $file->getContents(), 'handler' => \strpos($file->getPathname(), $data_dir) === \false ? 'read-only' : 'internal', 'signature' => \wp_create_nonce(\sprintf('%s:%s', WIND_PRESS::WP_OPTION, $file->getRelativePathname()))];
37 }
38 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local file
39 $stubs_main_css = \file_get_contents(\dirname(WIND_PRESS::FILE) . '/stubs/main.css');
40 // check if 'main.css' already exists and content is not empty, else use the stubs
41 $main_css_key = \array_search('main.css', \array_column($entries, 'name'), \true);
42 if ($main_css_key === \false) {
43 $entries[] = ['file_name' => 'main.css', 'relative_path' => 'main.css', 'content' => $stubs_main_css, 'handler' => 'internal', 'signature' => \wp_create_nonce(\sprintf('%s:%s', WIND_PRESS::WP_OPTION, 'main.css'))];
44 } elseif (empty($entries[$main_css_key]['content'])) {
45 $entries[$main_css_key]['content'] = $stubs_main_css;
46 }
47 /**
48 * @param array $entries The list of volume's entries. Each volume have `name`, `relative_path`, `content`, `handler`, and `signature` keys.
49 */
50 return \apply_filters('f!windpress/core/volume:get_entries.entries', $entries);
51 }
52 public static function save_entries($entries)
53 {
54 if (!\is_array($entries)) {
55 return;
56 }
57 $data_dir = \wp_upload_dir()['basedir'] . WIND_PRESS::DATA_DIR;
58 foreach ($entries as $entry) {
59 // if doesn't have any of the following keys, skip: name, relative_path, content, handler
60 if (!isset($entry['name'], $entry['relative_path'], $entry['content'], $entry['handler'])) {
61 continue;
62 }
63 if ($entry['handler'] === 'read-only') {
64 continue;
65 }
66 if ($entry['handler'] !== 'internal') {
67 // only if the signature is set
68 if (isset($entry['signature'])) {
69 // the handler only accept alphanumeric, hyphens, and underscores
70 if (!\preg_match('/^[a-zA-Z0-9_-]+$/', $entry['handler'])) {
71 continue;
72 }
73 \do_action('f!windpress/core/volume:save_entries.entry', $entry);
74 // use specific handler instead for efficient handling
75 \do_action('f!windpress/core/volume:save_entries.entry.' . $entry['handler'], $entry);
76 }
77 continue;
78 }
79 // if the signature is not set, it is a new entry.
80 if (!isset($entry['signature'])) {
81 // sanitize the file name.
82 $file_name = \sanitize_file_name(\pathinfo($entry['name'], \PATHINFO_BASENAME));
83 // only handle a css and js files.
84 if (!\in_array(\pathinfo($file_name, \PATHINFO_EXTENSION), ['css', 'js'], \true)) {
85 continue;
86 }
87 $entry['relative_path'] = 'custom/' . $file_name;
88 $entry['signature'] = \wp_create_nonce(\sprintf('%s:%s', WIND_PRESS::WP_OPTION, $entry['relative_path']));
89 }
90 // verify the signature
91 if (!\wp_verify_nonce($entry['signature'], \sprintf('%s:%s', WIND_PRESS::WP_OPTION, $entry['relative_path']))) {
92 continue;
93 }
94 try {
95 // if the relative path is starts with 'custom/', it is a custom file.
96 if (empty($entry['content']) && \strpos($entry['relative_path'], 'custom/') === 0) {
97 Common::delete_file($data_dir . $entry['relative_path']);
98 } else {
99 Common::save_file($entry['content'], $data_dir . $entry['relative_path']);
100 }
101 } catch (\Throwable $th) {
102 if (\WP_DEBUG_LOG) {
103 \error_log($th->__toString());
104 }
105 }
106 }
107 }
108 }
109