PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 4.9.2
WP STAGING – WordPress Backup, Restore, Migration & Clone v4.9.2
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 / CloningProcess / ExcludedPlugins.php
wp-staging / Framework / CloningProcess Last commit date
Data 1 week ago Database 7 months ago CloningDto.php 3 years ago ExcludedPlugins.php 5 months ago
ExcludedPlugins.php
170 lines
1 <?php
2
3 namespace WPStaging\Framework\CloningProcess;
4
5 use WPStaging\Core\WPStaging;
6 use WPStaging\Framework\Adapter\Directory;
7 use WPStaging\Framework\Facades\Hooks;
8 use WPStaging\Framework\Filesystem\PathIdentifier;
9 use WPStaging\Staging\CloneOptions;
10
11 /**
12 * Add here the list of excluded plugins to make sure code remain DRY
13 */
14 class ExcludedPlugins
15 {
16 /**
17 * @var string
18 */
19 const EXCLUDED_PLUGINS_KEY = 'excluded_plugins';
20
21 /**
22 * @var array
23 */
24 private $excludedPlugins;
25
26 /** @var string */
27 private $pluginsPath;
28
29 /** @var string */
30 private $absPath;
31
32 /**
33 * Place below the list of plugins to exclude
34 * If any of these below plugins are installed in user site they will be skipped during cloning
35 * And a message will be shown to them about their exclusion
36 * @param Directory|null $dirAdapter
37 */
38 public function __construct($dirAdapter = null)
39 {
40 if ($dirAdapter === null) {
41 $dirAdapter = WPStaging::make(Directory::class);
42 }
43
44 $this->pluginsPath = $dirAdapter->getPluginsDirectory();
45 $this->absPath = $dirAdapter->getAbsPath();
46 $this->excludedPlugins = [
47 'wps-hide-login',
48 'wp-super-cache',
49 'peters-login-redirect',
50 'wp-spamshield',
51 ];
52 }
53
54 /**
55 * Get List of excluded plugins
56 * The array can contain:
57 * - A parent dir of a plugin like `woocommerce`
58 * - A single file plugin like `hello-dolly.php`
59 *
60 * Here single file plugin means the plugins which consist of only a single file i.e. Hello Dolly plugin,
61 * These single file plugins can be placed directly in the plugin path without the need of subfolder.
62 * @see https://developer.wordpress.org/plugins/intro/ see 2nd paragraph under "Why We Make Plugins" to understand single file plugin better.
63 *
64 * @return array
65 */
66 public function getPluginsToExclude()
67 {
68 return $this->excludedPlugins;
69 }
70
71 /**
72 * Get list of excluded plugins prefixed with {wpstg_p} identifier
73 *
74 * @return array
75 */
76 public function getPluginsToExcludeWithIdentifier()
77 {
78 return array_map(function ($plugin) {
79 return PathIdentifier::IDENTIFIER_PLUGINS . $plugin;
80 }, $this->excludedPlugins);
81 }
82
83 /**
84 * Get list of excluded plugins with relative path to wp root
85 *
86 * @return array
87 */
88 public function getPluginsToExcludeWithRelativePath()
89 {
90 $relativePath = str_replace($this->absPath, '/', $this->pluginsPath);
91 $relativePath = trailingslashit($relativePath);
92 return array_map(function ($plugin) use ($relativePath) {
93 return $relativePath . $plugin;
94 }, $this->excludedPlugins);
95 }
96
97 /**
98 * Get list of excluded plugins with full path
99 *
100 * @return array
101 */
102 public function getPluginsToExcludeFullPath()
103 {
104 $pluginsPath = $this->pluginsPath;
105 return array_map(function ($plugin) use ($pluginsPath) {
106 return $pluginsPath . $plugin;
107 }, $this->excludedPlugins);
108 }
109
110 /**
111 * Get List of excluded plugins defined by WP Staging and by excluded path hooks
112 *
113 * @param array $installedPlugins - Used for unit testing
114 *
115 * @return array
116 */
117 public function getFilteredPluginsToExclude($installedPlugins = [])
118 {
119 // Apply filter
120 if (is_multisite()) {
121 $filteredExcludedPlugins = apply_filters(Directory::FILTER_CLONE_MU_EXCLUDED_FOLDERS, $this->getPluginsToExcludeWithRelativePath());
122 } else {
123 $filteredExcludedPlugins = apply_filters(Directory::FILTER_CLONE_EXCLUDED_FOLDERS, $this->getPluginsToExcludeWithRelativePath());
124 }
125
126 if ($installedPlugins === []) {
127 $installedPlugins = get_plugins();
128 $installedPlugins = array_keys($installedPlugins);
129 }
130
131 $relativePath = str_replace($this->absPath, '/', $this->pluginsPath);
132 $relativePath = trailingslashit($relativePath);
133 // Remove all paths other than plugins not in installed plugins
134 $filteredExcludedPlugins = array_filter($filteredExcludedPlugins, function ($path) use ($installedPlugins, $relativePath) {
135 foreach ($installedPlugins as $plugin) {
136 $plugin = $relativePath . explode('/', $plugin)[0];
137 if (strpos($path, $plugin) === 0) {
138 return true;
139 }
140 }
141
142 return false;
143 });
144
145 // Reindex the array
146 $filteredExcludedPlugins = array_values($filteredExcludedPlugins);
147 /*
148 * Remove plugins dir from the paths and
149 * only return plugin dir if inside directory otherwise return file
150 * /wp-content/plugins/some-plugin/some-plugin.php will return some-plugin
151 * /wp-content/plugins/single-file-plugin.php will return single-file-plugin.php
152 * /wp-content/plugins/plugin-dir will return plugin-dir
153 */
154 return array_map(function ($path) use ($relativePath) {
155 $plugin = str_replace($relativePath, '', $path);
156 return explode('/', $plugin)[0];
157 }, $filteredExcludedPlugins);
158 }
159
160 /**
161 * This returns the actual excluded plugins during cloning/updating/resetting
162 *
163 * @return array
164 */
165 public function getExcludedPlugins()
166 {
167 return (new CloneOptions())->get(self::EXCLUDED_PLUGINS_KEY);
168 }
169 }
170