Providers.php
211 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Backup\Storage; |
| 4 | |
| 5 | use WPStaging\Core\WPStaging; |
| 6 | use WPStaging\Pro\Backup\Storage\Storages\Amazon\S3 as AmazonS3Auth; |
| 7 | use WPStaging\Pro\Backup\Storage\Storages\DigitalOceanSpaces\Auth as DSOAuth; |
| 8 | use WPStaging\Pro\Backup\Storage\Storages\GenericS3\Auth as GenericS3Auth; |
| 9 | use WPStaging\Pro\Backup\Storage\Storages\GoogleDrive\Auth as GoogleDriveAuth; |
| 10 | use WPStaging\Pro\Backup\Storage\Storages\Dropbox\Auth as DropboxAuth; |
| 11 | use WPStaging\Pro\Backup\Storage\Storages\SFTP\Auth as SftpAuth; |
| 12 | use WPStaging\Pro\Backup\Storage\Storages\Wasabi\Auth as WasabiAuth; |
| 13 | |
| 14 | use function WPStaging\functions\debug_log; |
| 15 | |
| 16 | class Providers |
| 17 | { |
| 18 | /** @var array |
| 19 | * |
| 20 | * @example [ |
| 21 | * 'storageIdentifier' => 'storageId' |
| 22 | * ] |
| 23 | */ |
| 24 | const STORAGE_IDS_BY_IDENTIFIERS = [ |
| 25 | 'googledrive' => 'googleDrive', |
| 26 | 'amazons3' => 'amazonS3', |
| 27 | 'dropbox' => 'dropbox', |
| 28 | 'sftp' => 'sftp', |
| 29 | 'digitalocean-spaces' => 'digitalocean-spaces', |
| 30 | 'wasabi' => 'wasabi-s3', |
| 31 | 'generic-s3' => 'generic-s3', |
| 32 | ]; |
| 33 | |
| 34 | protected $storages = []; |
| 35 | |
| 36 | public function __construct() |
| 37 | { |
| 38 | $this->storages = [ |
| 39 | [ |
| 40 | 'id' => 'googleDrive', |
| 41 | 'cli' => 'google-drive', |
| 42 | 'name' => esc_html__('Google Drive', 'wp-staging'), |
| 43 | 'enabled' => true, |
| 44 | 'authClass' => $this->filterAuthClassForPro(GoogleDriveAuth::class), |
| 45 | 'settingsPath' => $this->getStorageAdminPage('googleDrive'), |
| 46 | ], |
| 47 | [ |
| 48 | 'id' => 'amazonS3', |
| 49 | 'cli' => 'amazon-s3', |
| 50 | 'name' => esc_html__('Amazon S3', 'wp-staging'), |
| 51 | 'enabled' => true, |
| 52 | 'authClass' => $this->filterAuthClassForPro(AmazonS3Auth::class), |
| 53 | 'settingsPath' => $this->getStorageAdminPage('amazonS3'), |
| 54 | ], |
| 55 | [ |
| 56 | 'id' => 'dropbox', |
| 57 | 'cli' => 'dropbox', |
| 58 | 'name' => esc_html__('Dropbox', 'wp-staging'), |
| 59 | 'enabled' => true, |
| 60 | 'authClass' => $this->filterAuthClassForPro(DropboxAuth::class), |
| 61 | 'settingsPath' => $this->getStorageAdminPage('dropbox'), |
| 62 | ], |
| 63 | [ |
| 64 | 'id' => 'oneDrive', |
| 65 | 'cli' => 'one-drive', |
| 66 | 'name' => esc_html__('One Drive', 'wp-staging'), |
| 67 | 'enabled' => false, |
| 68 | 'authClass' => '', |
| 69 | 'settingsPath' => $this->getStorageAdminPage('onedrive'), |
| 70 | ], |
| 71 | [ |
| 72 | 'id' => 'sftp', |
| 73 | 'cli' => 'sftp', |
| 74 | 'name' => esc_html__('FTP / SFTP', 'wp-staging'), |
| 75 | 'enabled' => true, |
| 76 | 'authClass' => $this->filterAuthClassForPro(SftpAuth::class), |
| 77 | 'settingsPath' => $this->getStorageAdminPage('sftp'), |
| 78 | ], |
| 79 | [ |
| 80 | 'id' => 'digitalocean-spaces', |
| 81 | 'cli' => 'digitalocean-spaces', |
| 82 | 'name' => esc_html__('DigitalOcean Spaces', 'wp-staging'), |
| 83 | 'enabled' => true, |
| 84 | 'authClass' => $this->filterAuthClassForPro(DSOAuth::class), |
| 85 | 'settingsPath' => $this->getStorageAdminPage('digitalocean-spaces'), |
| 86 | ], |
| 87 | [ |
| 88 | 'id' => 'wasabi-s3', |
| 89 | 'cli' => 'wasabi-s3', |
| 90 | 'name' => esc_html__('Wasabi S3', 'wp-staging'), |
| 91 | 'enabled' => true, |
| 92 | 'authClass' => $this->filterAuthClassForPro(WasabiAuth::class), |
| 93 | 'settingsPath' => $this->getStorageAdminPage('wasabi-s3'), |
| 94 | ], |
| 95 | [ |
| 96 | 'id' => 'generic-s3', |
| 97 | 'cli' => 'generic-s3', |
| 98 | 'name' => esc_html__('Generic S3', 'wp-staging'), |
| 99 | 'enabled' => true, |
| 100 | 'authClass' => $this->filterAuthClassForPro(GenericS3Auth::class), |
| 101 | 'settingsPath' => $this->getStorageAdminPage('generic-s3'), |
| 102 | ], |
| 103 | ]; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @param null|bool $isEnabled. Default null |
| 108 | * Use null for all storages, |
| 109 | * Use true for enabled storages, |
| 110 | * Use false for disabled storages |
| 111 | * |
| 112 | * @return array |
| 113 | */ |
| 114 | public function getStorageIds($isEnabled = null) |
| 115 | { |
| 116 | return array_map(function ($storage) { |
| 117 | return $storage['id']; |
| 118 | }, $this->getStorages($isEnabled)); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @param null|bool $isEnabled. Default null |
| 123 | * Use null for all storages, |
| 124 | * Use true for enabled storages, |
| 125 | * Use false for disabled storages |
| 126 | * |
| 127 | * @return array |
| 128 | */ |
| 129 | public function getStorages($isEnabled = null) |
| 130 | { |
| 131 | if ($isEnabled === null) { |
| 132 | return $this->storages; |
| 133 | } |
| 134 | |
| 135 | return array_filter($this->storages, function ($storage) use ($isEnabled) { |
| 136 | return $storage['enabled'] === $isEnabled; |
| 137 | }); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * @param string $id |
| 142 | * @param string $property |
| 143 | * @param null|bool $isEnabled. Default null |
| 144 | * Use null for all storages, |
| 145 | * Use true for enabled storages, |
| 146 | * Use false for disabled storages |
| 147 | * |
| 148 | * @return mixed |
| 149 | */ |
| 150 | public function getStorageProperty($id, $property, $isEnabled = null) |
| 151 | { |
| 152 | foreach ($this->getStorages($isEnabled) as $storage) { |
| 153 | if ($storage['id'] === $id) { |
| 154 | if (array_key_exists($property, $storage)) { |
| 155 | return $storage[$property]; |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * @param string $class |
| 165 | * @return bool |
| 166 | */ |
| 167 | public function isActivated($class) |
| 168 | { |
| 169 | if (empty($class)) { |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | /** @see WPStaging\Backup\Storage\AbstractStorage */ |
| 174 | $storage = WPStaging::make($class); |
| 175 | return $storage->isAuthenticated(); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * @param string $identifier |
| 180 | * |
| 181 | * @return string|false |
| 182 | */ |
| 183 | public function getStorageByIdentifier(string $identifier) |
| 184 | { |
| 185 | if (!isset(self::STORAGE_IDS_BY_IDENTIFIERS[$identifier])) { |
| 186 | debug_log('Failed to find storage id by identifier.'); |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | return self::STORAGE_IDS_BY_IDENTIFIERS[$identifier]; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * @param string $id |
| 195 | * @return string |
| 196 | */ |
| 197 | protected function filterAuthClassForPro($id) |
| 198 | { |
| 199 | if (empty($id) || !WPStaging::isPro()) { |
| 200 | return ''; |
| 201 | } |
| 202 | |
| 203 | return $id; |
| 204 | } |
| 205 | |
| 206 | private function getStorageAdminPage($storageTab) |
| 207 | { |
| 208 | return admin_url('admin.php?page=wpstg-settings&tab=remote-storages&sub=' . $storageTab); |
| 209 | } |
| 210 | } |
| 211 |