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