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