Providers.php
178 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 | class Providers |
| 15 | { |
| 16 | protected $storages = []; |
| 17 | |
| 18 | public function __construct() |
| 19 | { |
| 20 | $this->storages = [ |
| 21 | [ |
| 22 | 'id' => 'googleDrive', |
| 23 | 'cli' => 'google-drive', |
| 24 | 'name' => esc_html__('Google Drive', 'wp-staging'), |
| 25 | 'enabled' => true, |
| 26 | 'authClass' => $this->filterAuthClassForPro(GoogleDriveAuth::class), |
| 27 | 'settingsPath' => $this->getStorageAdminPage('googleDrive'), |
| 28 | ], |
| 29 | [ |
| 30 | 'id' => 'amazonS3', |
| 31 | 'cli' => 'amazon-s3', |
| 32 | 'name' => esc_html__('Amazon S3', 'wp-staging'), |
| 33 | 'enabled' => true, |
| 34 | 'authClass' => $this->filterAuthClassForPro(AmazonS3Auth::class), |
| 35 | 'settingsPath' => $this->getStorageAdminPage('amazonS3'), |
| 36 | ], |
| 37 | [ |
| 38 | 'id' => 'dropbox', |
| 39 | 'cli' => 'dropbox', |
| 40 | 'name' => esc_html__('Dropbox', 'wp-staging'), |
| 41 | 'enabled' => true, |
| 42 | 'authClass' => $this->filterAuthClassForPro(DropboxAuth::class), |
| 43 | 'settingsPath' => $this->getStorageAdminPage('dropbox'), |
| 44 | ], |
| 45 | [ |
| 46 | 'id' => 'oneDrive', |
| 47 | 'cli' => 'one-drive', |
| 48 | 'name' => esc_html__('One Drive', 'wp-staging'), |
| 49 | 'enabled' => false, |
| 50 | 'authClass' => '', |
| 51 | 'settingsPath' => $this->getStorageAdminPage('onedrive'), |
| 52 | ], |
| 53 | [ |
| 54 | 'id' => 'sftp', |
| 55 | 'cli' => 'sftp', |
| 56 | 'name' => esc_html__('FTP / SFTP', 'wp-staging'), |
| 57 | 'enabled' => true, |
| 58 | 'authClass' => $this->filterAuthClassForPro(SftpAuth::class), |
| 59 | 'settingsPath' => $this->getStorageAdminPage('sftp'), |
| 60 | ], |
| 61 | [ |
| 62 | 'id' => 'digitalocean-spaces', |
| 63 | 'cli' => 'digitalocean-spaces', |
| 64 | 'name' => esc_html__('DigitalOcean Spaces', 'wp-staging'), |
| 65 | 'enabled' => true, |
| 66 | 'authClass' => $this->filterAuthClassForPro(DSOAuth::class), |
| 67 | 'settingsPath' => $this->getStorageAdminPage('digitalocean-spaces'), |
| 68 | ], |
| 69 | [ |
| 70 | 'id' => 'wasabi-s3', |
| 71 | 'cli' => 'wasabi-s3', |
| 72 | 'name' => esc_html__('Wasabi S3', 'wp-staging'), |
| 73 | 'enabled' => true, |
| 74 | 'authClass' => $this->filterAuthClassForPro(WasabiAuth::class), |
| 75 | 'settingsPath' => $this->getStorageAdminPage('wasabi-s3'), |
| 76 | ], |
| 77 | [ |
| 78 | 'id' => 'generic-s3', |
| 79 | 'cli' => 'generic-s3', |
| 80 | 'name' => esc_html__('Generic S3', 'wp-staging'), |
| 81 | 'enabled' => true, |
| 82 | 'authClass' => $this->filterAuthClassForPro(GenericS3Auth::class), |
| 83 | 'settingsPath' => $this->getStorageAdminPage('generic-s3'), |
| 84 | ], |
| 85 | ]; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @param null|bool $isEnabled. Default null |
| 90 | * Use null for all storages, |
| 91 | * Use true for enabled storages, |
| 92 | * Use false for disabled storages |
| 93 | * |
| 94 | * @return array |
| 95 | */ |
| 96 | public function getStorageIds($isEnabled = null) |
| 97 | { |
| 98 | return array_map(function ($storage) { |
| 99 | return $storage['id']; |
| 100 | }, $this->getStorages($isEnabled)); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @param null|bool $isEnabled. Default null |
| 105 | * Use null for all storages, |
| 106 | * Use true for enabled storages, |
| 107 | * Use false for disabled storages |
| 108 | * |
| 109 | * @return array |
| 110 | */ |
| 111 | public function getStorages($isEnabled = null) |
| 112 | { |
| 113 | if ($isEnabled === null) { |
| 114 | return $this->storages; |
| 115 | } |
| 116 | |
| 117 | return array_filter($this->storages, function ($storage) use ($isEnabled) { |
| 118 | return $storage['enabled'] === $isEnabled; |
| 119 | }); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @param string $id |
| 124 | * @param string $property |
| 125 | * @param null|bool $isEnabled. Default null |
| 126 | * Use null for all storages, |
| 127 | * Use true for enabled storages, |
| 128 | * Use false for disabled storages |
| 129 | * |
| 130 | * @return mixed |
| 131 | */ |
| 132 | public function getStorageProperty($id, $property, $isEnabled = null) |
| 133 | { |
| 134 | foreach ($this->getStorages($isEnabled) as $storage) { |
| 135 | if ($storage['id'] === $id) { |
| 136 | if (array_key_exists($property, $storage)) { |
| 137 | return $storage[$property]; |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * @param string $class |
| 147 | * @return bool |
| 148 | */ |
| 149 | public function isActivated($class) |
| 150 | { |
| 151 | if (empty($class)) { |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | /** @see WPStaging\Backup\Storage\AbstractStorage */ |
| 156 | $storage = WPStaging::make($class); |
| 157 | return $storage->isAuthenticated(); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * @param string $id |
| 162 | * @return string |
| 163 | */ |
| 164 | protected function filterAuthClassForPro($id) |
| 165 | { |
| 166 | if (empty($id) || !WPStaging::isPro()) { |
| 167 | return ''; |
| 168 | } |
| 169 | |
| 170 | return $id; |
| 171 | } |
| 172 | |
| 173 | private function getStorageAdminPage($storageTab) |
| 174 | { |
| 175 | return admin_url('admin.php?page=wpstg-settings&tab=remote-storages&sub=' . $storageTab); |
| 176 | } |
| 177 | } |
| 178 |