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 | |
| 13 | |
| 14 | class ExcludedPlugins |
| 15 | { |
| 16 | |
| 17 | |
| 18 | |
| 19 | const EXCLUDED_PLUGINS_KEY = 'excluded_plugins'; |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | private $excludedPlugins; |
| 25 | |
| 26 | |
| 27 | private $pluginsPath; |
| 28 | |
| 29 | |
| 30 | private $absPath; |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 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 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | public function getPluginsToExclude() |
| 67 | { |
| 68 | return $this->excludedPlugins; |
| 69 | } |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 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 | |
| 85 | |
| 86 | |
| 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 | |
| 99 | |
| 100 | |
| 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 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | public function getFilteredPluginsToExclude($installedPlugins = []) |
| 118 | { |
| 119 | |
| 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 | |
| 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 | |
| 146 | $filteredExcludedPlugins = array_values($filteredExcludedPlugins); |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 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 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | public function getExcludedPlugins() |
| 166 | { |
| 167 | return (new CloneOptions())->get(self::EXCLUDED_PLUGINS_KEY); |
| 168 | } |
| 169 | } |
| 170 |