PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.8.2
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.8.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
ArrayableTrait.php 2 years ago BenchmarkTrait.php 2 years ago BooleanTransientTrait.php 5 years ago DatabaseSearchReplaceTrait.php 2 years ago DbRowsGeneratorTrait.php 3 years ago DeveloperTimerTrait.php 4 years ago EndOfLinePlaceholderTrait.php 2 years ago EventLoggerTrait.php 1 year ago FileScanToCacheTrait.php 2 years ago HydrateTrait.php 2 years ago MaintenanceTrait.php 5 years ago MemoryExhaustTrait.php 2 years ago MySQLRowsGeneratorTrait.php 2 years ago NoticesTrait.php 2 years ago PropertyConstructor.php 5 years ago ResourceTrait.php 2 years ago ValueGetterTrait.php 1 year ago
FileScanToCacheTrait.php
145 lines
1 <?php
2
3 namespace WPStaging\Framework\Traits;
4
5 use Exception;
6 use RuntimeException;
7 use WPStaging\Framework\Filesystem\Filesystem;
8 use WPStaging\Framework\Filesystem\FilterableDirectoryIterator;
9 use WPStaging\Framework\Utils\Strings;
10 use WPStaging\Framework\Traits\EndOfLinePlaceholderTrait;
11
12 trait FileScanToCacheTrait
13 {
14 use EndOfLinePlaceholderTrait;
15
16 /** @var bool */
17 protected $isExcludedWpConfig = false;
18
19 /** @var string */
20 protected $pathIdentifier = '';
21
22 /**
23 * @var Strings
24 */
25 protected $strUtils;
26
27 /**
28 * Write contents to a file
29 *
30 * @param resource $handle File handle to write to
31 * @param string $content Content to write to the file
32 * @return int
33 * @throws Exception
34 */
35 abstract public function write($handle, $content);
36
37 /**
38 * Scan Recursively through DirectoryIterator as RecursiveDirectoryIterator is slow
39 *
40 * @param resource $filesHandle
41 * @param string $path
42 * @param bool $isRecursive
43 * @param array $excludePaths absolute path of dir/files to exclude
44 * @param array $excludeSizeRules exclude files by different size comparing rules
45 * @param string $wpRootPath
46 *
47 * @return int count of files path written to cache file
48 * @throws Exception
49 */
50 public function scanToCacheFile($filesHandle, $path, $isRecursive = false, $excludePaths = [], $excludeSizeRules = [], $wpRootPath = ABSPATH)
51 {
52 $filesystem = new Filesystem();
53 $normalizedWpRoot = $filesystem->normalizePath($wpRootPath);
54 if (is_file($path)) {
55 $file = str_replace($normalizedWpRoot, '', $filesystem->normalizePath($path, true));
56 $file = $this->replaceEOLsWithPlaceholders($file) . PHP_EOL;
57 if ($this->write($filesHandle, $file)) {
58 return 1;
59 }
60
61 return 0;
62 }
63
64 $filesWrittenToCache = 0;
65
66 if (!file_exists($path)) {
67 return 0;
68 }
69
70 try {
71 $iterator = (new FilterableDirectoryIterator())
72 ->setDirectory($filesystem->trailingSlashit($path))
73 ->setRecursive(false)
74 ->setDotSkip()
75 ->setExcludePaths($excludePaths)
76 ->setExcludeSizeRules($excludeSizeRules)
77 ->setWpRootPath($wpRootPath)
78 ->get();
79
80 foreach ($iterator as $item) {
81 // Always check link first otherwise it may be treated as directory
82 $itemPath = $item->getPathname();
83 if ($item->isLink()) {
84 // Allow copying of link if the link's source is a directory
85 if (is_dir($item->getRealPath()) && $isRecursive) {
86 $filesWrittenToCache += $this->scanToCacheFile($filesHandle, $itemPath, $isRecursive, $excludePaths, $excludeSizeRules, $wpRootPath);
87 }
88
89 continue;
90 }
91
92 if ($isRecursive && $item->isDir()) {
93 $filesWrittenToCache += $this->scanToCacheFile($filesHandle, $itemPath, $isRecursive, $excludePaths, $excludeSizeRules, $wpRootPath);
94 continue;
95 }
96
97 if ($item->isFile()) {
98 $file = $filesystem->maybeNormalizePath($itemPath);
99 $file = $this->strUtils->replaceStartWith($normalizedWpRoot, '', $file);
100 // One more time with not normalized $wpRootPath in case the file path was not normalized
101 $file = $this->strUtils->replaceStartWith($wpRootPath, '', $file);
102
103 // At the moment will only handle case where wp-config.php is present at root folder of WP
104 if ($file === '/wp-config.php') {
105 $this->setIsExcludedWpConfig(false);
106 }
107
108 $file = $this->replaceEOLsWithPlaceholders($file) . PHP_EOL;
109 if ($this->write($filesHandle, $this->pathIdentifier . ltrim($file, '/'))) {
110 $filesWrittenToCache++;
111 }
112 }
113 }
114 } catch (Exception $e) {
115 throw new RuntimeException($e->getMessage());
116 }
117
118 return $filesWrittenToCache;
119 }
120
121 /**
122 * @param bool $skipped
123 */
124 public function setIsExcludedWpConfig($skipped = true)
125 {
126 $this->isExcludedWpConfig = $skipped;
127 }
128
129 /**
130 * @return bool
131 */
132 public function getIsExcludedWpConfig()
133 {
134 return $this->isExcludedWpConfig;
135 }
136
137 /**
138 * @param string $pathIdentifier
139 */
140 protected function setPathIdentifier($pathIdentifier)
141 {
142 $this->pathIdentifier = $pathIdentifier;
143 }
144 }
145