PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 4.11.0
WP STAGING – WordPress Backups, Restore, Migration & Clone v4.11.0
4.11.0 4.10.0 4.9.5 4.9.4 4.9.3 4.9.2 4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Framework / Traits / FileScanToCacheTrait.php
wp-staging / Framework / Traits Last commit date
ApplyFiltersTrait.php 1 day ago ArrayableTrait.php 1 day ago BatchSizeCalculateTrait.php 1 day ago BearerTokenTrait.php 1 day ago BenchmarkTrait.php 1 day ago BooleanTransientTrait.php 1 day ago DatabaseSearchReplaceTrait.php 1 day ago DbRowsGeneratorTrait.php 1 day ago DebugLogTrait.php 1 day ago DeveloperTimerTrait.php 1 day ago EndOfLinePlaceholderTrait.php 1 day ago EventLoggerTrait.php 1 day ago FileScanToCacheTrait.php 1 day ago FormatTrait.php 1 day ago HttpRequestTrait.php 1 day ago HydrateTrait.php 1 day ago I18nTrait.php 1 day ago IpResolverTrait.php 1 day ago JobResponseTrait.php 1 day ago MaintenanceTrait.php 7 months ago MemoryExhaustTrait.php 1 day ago MySQLRowsGeneratorTrait.php 1 day ago NoticesTrait.php 1 day ago PagesTrait.php 1 day ago PropertyConstructor.php 1 day ago RenameTmpDirectoryTrait.php 1 day ago ResourceTrait.php 1 day ago RestRequestTrait.php 1 day ago RestoreFileExclusionTrait.php 1 day ago SafeFileInfoTrait.php 1 day ago SerializeTrait.php 1 day ago SetTimeLimitTrait.php 1 day ago SlashTrait.php 1 day ago SqlCommentTrait.php 1 day ago TablePrefixValidator.php 5 months ago UrlTrait.php 1 day ago ValueGetterTrait.php 1 day ago WindowsOsTrait.php 1 day ago
FileScanToCacheTrait.php
196 lines
1 <?php
2
3 namespace WPStaging\Framework\Traits;
4
5 use Exception;
6 use RuntimeException;
7 use WPStaging\Core\Utils\Logger;
8 use WPStaging\Core\WPStaging;
9 use WPStaging\Framework\Filesystem\Filesystem;
10 use WPStaging\Framework\Filesystem\FilterableDirectoryIterator;
11 use WPStaging\Framework\Utils\Strings;
12 use WPStaging\Framework\Traits\EndOfLinePlaceholderTrait;
13 use WPStaging\Framework\Traits\SafeFileInfoTrait;
14
15 trait FileScanToCacheTrait
16 {
17 use EndOfLinePlaceholderTrait;
18 use SafeFileInfoTrait;
19
20
21 protected $isExcludedWpConfig = false;
22
23
24 protected $pathIdentifier = '';
25
26
27
28
29 protected $strUtils;
30
31
32
33
34 protected $logger;
35
36
37
38
39
40
41
42
43
44 abstract public function write($handle, $content);
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 public function scanToCacheFile($filesHandle, $path, $isRecursive = false, $excludePaths = [], $excludeSizeRules = [], $wpRootPath = ABSPATH, bool $shouldScanEmptyDirs = true)
61 {
62 if (!is_readable($path)) {
63 $this->log(sprintf('Skipping! The path "%s" is not readable.', $path), Logger::TYPE_WARNING);
64 return 0;
65 }
66
67 $filesystem = WPStaging::make(Filesystem::class);
68 $normalizedWpRoot = $filesystem->normalizePath($wpRootPath);
69
70 if (is_file($path)) {
71 $file = str_replace($normalizedWpRoot, '', $filesystem->normalizePath($path, true));
72 $file = $this->replaceEOLsWithPlaceholders($file) . PHP_EOL;
73 if ($this->write($filesHandle, $file)) {
74 return 1;
75 }
76
77 return 0;
78 }
79
80 $filesWrittenToCache = 0;
81 try {
82 $iterator = (new FilterableDirectoryIterator())
83 ->setDirectory($filesystem->trailingSlashit($path))
84 ->setRecursive(false)
85 ->setDotSkip()
86 ->setExcludePaths($excludePaths)
87 ->setExcludeSizeRules($excludeSizeRules)
88 ->setWpRootPath($wpRootPath)
89 ->get();
90
91 foreach ($iterator as $item) {
92
93 $itemPath = $item->getPathname();
94
95 $isLink = $this->isLinkSafely($item);
96 if ($isLink === null) {
97 continue;
98 }
99
100 if ($isLink) {
101
102 $linkTarget = $this->getRealPathSafely($item);
103 if ($linkTarget !== false && is_dir($linkTarget) && $isRecursive) {
104 $filesWrittenToCache += $this->scanToCacheFile($filesHandle, $itemPath, $isRecursive, $excludePaths, $excludeSizeRules, $wpRootPath, $shouldScanEmptyDirs);
105 }
106
107 continue;
108 }
109
110 if ($isRecursive) {
111 $isDir = $this->isDirSafely($item);
112 if ($isDir === null) {
113 continue;
114 }
115
116 if ($isDir) {
117 $filesWrittenToCache += $this->scanToCacheFile($filesHandle, $itemPath, $isRecursive, $excludePaths, $excludeSizeRules, $wpRootPath, $shouldScanEmptyDirs);
118 continue;
119 }
120 }
121
122 $isFile = $this->isFileSafely($item);
123 if ($isFile === null) {
124 continue;
125 }
126
127 if ($isFile) {
128 $file = $filesystem->maybeNormalizePath($itemPath);
129 $file = $this->strUtils->replaceStartWith($normalizedWpRoot, '', $file);
130
131 $file = $this->strUtils->replaceStartWith($wpRootPath, '', $file);
132
133
134 if ($file === '/wp-config.php') {
135 $this->setIsExcludedWpConfig(false);
136 }
137
138 $file = $this->replaceEOLsWithPlaceholders($file) . PHP_EOL;
139 if ($this->write($filesHandle, $this->pathIdentifier . ltrim($file, '/'))) {
140 $filesWrittenToCache++;
141 }
142 }
143 }
144 } catch (Exception $e) {
145 throw new RuntimeException($e->getMessage());
146 }
147
148 if ($shouldScanEmptyDirs && $filesWrittenToCache === 0 && is_dir($path)) {
149 $pathPart = $this->strUtils->replaceStartWith($normalizedWpRoot, '', $path) . PHP_EOL;
150 $this->write($filesHandle, $this->pathIdentifier . ltrim($pathPart, '/'));
151 $filesWrittenToCache++;
152 }
153
154 return $filesWrittenToCache;
155 }
156
157
158
159
160 public function setIsExcludedWpConfig($skipped = true)
161 {
162 $this->isExcludedWpConfig = $skipped;
163 }
164
165
166
167
168 public function getIsExcludedWpConfig()
169 {
170 return $this->isExcludedWpConfig;
171 }
172
173
174
175
176 protected function setPathIdentifier($pathIdentifier)
177 {
178 $this->pathIdentifier = $pathIdentifier;
179 }
180
181
182
183
184
185
186
187 public function log($msg, $type = Logger::TYPE_INFO)
188 {
189 if ($this->logger === null) {
190 $this->logger = WPStaging::make(Logger::class);
191 }
192
193 $this->logger->add($msg, $type);
194 }
195 }
196