PluginProbe
WindPress – Tailwind CSS integration for WordPress / 3.0.12
WindPress – Tailwind CSS integration for WordPress v3.0.12
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 3.0.6 All 143 releases
windpress / src / Core / Volume.php

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

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