PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / trunk
WP STAGING – WordPress Backup, Restore, Migration & Clone vtrunk
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 / Filesystem / LegacyFileRulesTrait.php
wp-staging / Framework / Filesystem Last commit date
Filters 7 months ago Scanning 5 years ago AbstractFileObject.php 1 year ago AbstractFilesystemScanner.php 2 months ago DebugLogReader.php 2 years ago DirectoryListing.php 5 months ago DiskWriteCheck.php 5 months ago FileObject.php 1 year ago Filesystem.php 7 months ago FilesystemExceptions.php 5 years ago FilesystemScanner.php 1 week ago FilesystemScannerDto.php 1 week ago FilterableDirectoryIterator.php 1 year ago LegacyFileRulesTrait.php 1 week ago LogCleanup.php 5 months ago LogFiles.php 1 year ago MissingFileException.php 3 years ago OPcache.php 5 months ago PartIdentifier.php 8 months ago PathChecker.php 2 years ago PathIdentifier.php 7 months ago Permissions.php 1 week ago WpUploadsFolderSymlinker.php 1 week ago
LegacyFileRulesTrait.php
133 lines
1 <?php
2
3 namespace WPStaging\Framework\Filesystem;
4
5 /**
6 * Normalizes legacy file exclusion rules for filesystem scanners.
7 */
8 trait LegacyFileRulesTrait
9 {
10 /**
11 * @param array $fileRules
12 * @return array
13 */
14 protected function extractLegacyFileNameRules(array $fileRules): array
15 {
16 $normalizedRules = [];
17
18 foreach ($fileRules as $fileRule) {
19 if (!is_string($fileRule)) {
20 continue;
21 }
22
23 $fileRule = trim($fileRule);
24 if ($fileRule === '') {
25 continue;
26 }
27
28 $fileRule = $this->reduceLegacyPathRuleToFileName($fileRule);
29
30 if ($fileRule !== '') {
31 $normalizedRules[] = $fileRule;
32 }
33 }
34
35 return array_values(array_unique($normalizedRules));
36 }
37
38 /**
39 * @param array $fileRules
40 * @return array
41 */
42 protected function extractFileExtensions(array $fileRules): array
43 {
44 $extensions = [];
45
46 foreach ($fileRules as $fileRule) {
47 if (!is_string($fileRule)) {
48 continue;
49 }
50
51 $fileRule = trim($fileRule);
52 if ($this->getLegacyVcsDirectoryName($fileRule) !== '') {
53 continue;
54 }
55
56 // Only glob extension rules (`*.log`) exclude by extension; a filename rule like
57 // `wp-staging-optimizer.php` must not turn `.php` into an ignored extension.
58 if (strpos($fileRule, '*.') !== 0) {
59 continue;
60 }
61
62 $fileRule = ltrim($fileRule, '*');
63 $position = strrpos($fileRule, '.');
64 if ($position === false || $position === strlen($fileRule) - 1) {
65 continue;
66 }
67
68 $extension = strtolower(substr($fileRule, $position + 1));
69 if ($extension !== '') {
70 $extensions[] = $extension;
71 }
72 }
73
74 return array_values(array_unique($extensions));
75 }
76
77 /**
78 * @param array $fileRules
79 * @return array
80 */
81 protected function extractLegacyFolderNameRules(array $fileRules): array
82 {
83 $folderRules = [];
84
85 foreach ($fileRules as $fileRule) {
86 if (!is_string($fileRule)) {
87 continue;
88 }
89
90 $vcsDirectoryName = $this->getLegacyVcsDirectoryName($fileRule);
91 if ($vcsDirectoryName === '') {
92 continue;
93 }
94
95 $folderRules[] = 'name_exact_matches ' . $vcsDirectoryName;
96 }
97
98 return array_values(array_unique($folderRules));
99 }
100
101 private function getLegacyVcsDirectoryName(string $fileRule): string
102 {
103 $fileRule = trim($fileRule);
104 if ($fileRule === '') {
105 return '';
106 }
107
108 $fileRule = strtolower($this->reduceLegacyPathRuleToFileName($fileRule));
109 if (strpos($fileRule, '*.') === 0) {
110 $directoryName = substr($fileRule, 2);
111 } elseif (strpos($fileRule, '.') === 0) {
112 $directoryName = substr($fileRule, 1);
113 } else {
114 return '';
115 }
116
117 if (!in_array($directoryName, ['git', 'svn', 'hg'], true)) {
118 return '';
119 }
120
121 return '.' . $directoryName;
122 }
123
124 private function reduceLegacyPathRuleToFileName(string $fileRule): string
125 {
126 if (strpos($fileRule, '/') !== false || strpos($fileRule, '\\') !== false) {
127 return basename(str_replace('\\', '/', $fileRule));
128 }
129
130 return $fileRule;
131 }
132 }
133