ExcludedPlugins.php
168 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\Framework\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 | */ |
| 36 | public function __construct(Directory $dirAdapter = null) |
| 37 | { |
| 38 | if ($dirAdapter === null) { |
| 39 | $dirAdapter = WPStaging::make(Directory::class); |
| 40 | } |
| 41 | |
| 42 | $this->pluginsPath = $dirAdapter->getPluginsDirectory(); |
| 43 | $this->absPath = $dirAdapter->getAbsPath(); |
| 44 | $this->excludedPlugins = [ |
| 45 | 'wps-hide-login', |
| 46 | 'wp-super-cache', |
| 47 | 'peters-login-redirect', |
| 48 | 'wp-spamshield', |
| 49 | ]; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Get List of excluded plugins |
| 54 | * The array can contain: |
| 55 | * - A parent dir of a plugin like `woocommerce` |
| 56 | * - A single file plugin like `hello-dolly.php` |
| 57 | * |
| 58 | * Here single file plugin means the plugins which consist of only a single file i.e. Hello Dolly plugin, |
| 59 | * These single file plugins can be placed directly in the plugin path without the need of subfolder. |
| 60 | * @see https://developer.wordpress.org/plugins/intro/ see 2nd paragraph under "Why We Make Plugins" to understand single file plugin better. |
| 61 | * |
| 62 | * @return array |
| 63 | */ |
| 64 | public function getPluginsToExclude() |
| 65 | { |
| 66 | return $this->excludedPlugins; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Get list of excluded plugins prefixed with {wpstg_p} identifier |
| 71 | * |
| 72 | * @return array |
| 73 | */ |
| 74 | public function getPluginsToExcludeWithIdentifier() |
| 75 | { |
| 76 | return array_map(function ($plugin) { |
| 77 | return PathIdentifier::IDENTIFIER_PLUGINS . $plugin; |
| 78 | }, $this->excludedPlugins); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Get list of excluded plugins with relative path to wp root |
| 83 | * |
| 84 | * @return array |
| 85 | */ |
| 86 | public function getPluginsToExcludeWithRelativePath() |
| 87 | { |
| 88 | $relativePath = str_replace($this->absPath, '/', $this->pluginsPath); |
| 89 | $relativePath = trailingslashit($relativePath); |
| 90 | return array_map(function ($plugin) use ($relativePath) { |
| 91 | return $relativePath . $plugin; |
| 92 | }, $this->excludedPlugins); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get list of excluded plugins with full path |
| 97 | * |
| 98 | * @return array |
| 99 | */ |
| 100 | public function getPluginsToExcludeFullPath() |
| 101 | { |
| 102 | $pluginsPath = $this->pluginsPath; |
| 103 | return array_map(function ($plugin) use ($pluginsPath) { |
| 104 | return $pluginsPath . $plugin; |
| 105 | }, $this->excludedPlugins); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Get List of excluded plugins defined by WP Staging and by excluded path hooks |
| 110 | * |
| 111 | * @param array $installedPlugins - Used for unit testing |
| 112 | * |
| 113 | * @return array |
| 114 | */ |
| 115 | public function getFilteredPluginsToExclude($installedPlugins = []) |
| 116 | { |
| 117 | // Apply filter |
| 118 | if (is_multisite()) { |
| 119 | $filteredExcludedPlugins = apply_filters('wpstg_clone_mu_excl_folders', $this->getPluginsToExcludeWithRelativePath()); |
| 120 | } else { |
| 121 | $filteredExcludedPlugins = apply_filters('wpstg_clone_excl_folders', $this->getPluginsToExcludeWithRelativePath()); |
| 122 | } |
| 123 | |
| 124 | if ($installedPlugins === []) { |
| 125 | $installedPlugins = get_plugins(); |
| 126 | $installedPlugins = array_keys($installedPlugins); |
| 127 | } |
| 128 | |
| 129 | $relativePath = str_replace($this->absPath, '/', $this->pluginsPath); |
| 130 | $relativePath = trailingslashit($relativePath); |
| 131 | // Remove all paths other than plugins not in installed plugins |
| 132 | $filteredExcludedPlugins = array_filter($filteredExcludedPlugins, function ($path) use ($installedPlugins, $relativePath) { |
| 133 | foreach ($installedPlugins as $plugin) { |
| 134 | $plugin = $relativePath . explode('/', $plugin)[0]; |
| 135 | if (strpos($path, $plugin) === 0) { |
| 136 | return true; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | return false; |
| 141 | }); |
| 142 | |
| 143 | // Reindex the array |
| 144 | $filteredExcludedPlugins = array_values($filteredExcludedPlugins); |
| 145 | /* |
| 146 | * Remove plugins dir from the paths and |
| 147 | * only return plugin dir if inside directory otherwise return file |
| 148 | * /wp-content/plugins/some-plugin/some-plugin.php will return some-plugin |
| 149 | * /wp-content/plugins/single-file-plugin.php will return single-file-plugin.php |
| 150 | * /wp-content/plugins/plugin-dir will return plugin-dir |
| 151 | */ |
| 152 | return array_map(function ($path) use ($relativePath) { |
| 153 | $plugin = str_replace($relativePath, '', $path); |
| 154 | return explode('/', $plugin)[0]; |
| 155 | }, $filteredExcludedPlugins); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * This returns the actual excluded plugins during cloning/updating/resetting |
| 160 | * |
| 161 | * @return array |
| 162 | */ |
| 163 | public function getExcludedPlugins() |
| 164 | { |
| 165 | return (new CloneOptions())->get(self::EXCLUDED_PLUGINS_KEY); |
| 166 | } |
| 167 | } |
| 168 |