| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDataAccess\Drive { |
| 4 |
|
| 5 |
use WPDataAccess\WPDA; |
| 6 |
|
| 7 |
class WPDA_Drives { |
| 8 |
|
| 9 |
const OPTION_DRIVES = 'wpda_drives'; |
| 10 |
|
| 11 |
static private $drives = array(); |
| 12 |
static private $is_initialized = false; |
| 13 |
|
| 14 |
public static function init() { |
| 15 |
|
| 16 |
if ( ! static::$is_initialized ) { |
| 17 |
$drives = get_option(self::OPTION_DRIVES); |
| 18 |
|
| 19 |
if (false !== $drives) { |
| 20 |
self::$drives = $drives; |
| 21 |
} |
| 22 |
|
| 23 |
self::$is_initialized = true; |
| 24 |
} |
| 25 |
|
| 26 |
} |
| 27 |
|
| 28 |
public static function save() { |
| 29 |
|
| 30 |
self::init(); |
| 31 |
|
| 32 |
update_option( self::OPTION_DRIVES, self::$drives ); |
| 33 |
|
| 34 |
} |
| 35 |
|
| 36 |
public static function get_drive_names( $enabled = false ) { |
| 37 |
|
| 38 |
self::init(); |
| 39 |
|
| 40 |
$drives = array_filter( |
| 41 |
array_keys( self::$drives ), |
| 42 |
function( $drive_name ) { |
| 43 |
return self::$drives[ $drive_name ]['enabled'] === true; |
| 44 |
} |
| 45 |
); |
| 46 |
|
| 47 |
sort( $drives ); |
| 48 |
|
| 49 |
return $drives; |
| 50 |
|
| 51 |
} |
| 52 |
|
| 53 |
public static function get_drives() { |
| 54 |
|
| 55 |
self::init(); |
| 56 |
|
| 57 |
return self::$drives; |
| 58 |
|
| 59 |
} |
| 60 |
|
| 61 |
public static function get_drive( $drive_name, $enabled = false ) { |
| 62 |
|
| 63 |
self::init(); |
| 64 |
|
| 65 |
if ( |
| 66 |
isset( |
| 67 |
self::$drives[ $drive_name ]['drive'], |
| 68 |
self::$drives[ $drive_name ]['type'], |
| 69 |
self::$drives[ $drive_name ]['enabled'] |
| 70 |
) && |
| 71 |
( |
| 72 |
self::$drives[ $drive_name ]['enabled'] === $enabled || |
| 73 |
false === $enabled |
| 74 |
) |
| 75 |
) { |
| 76 |
switch ( self::$drives[ $drive_name ]['type'] ) { |
| 77 |
case 'local': |
| 78 |
return new WPDA_Local( |
| 79 |
self::$drives[ $drive_name ]['drive'], |
| 80 |
self::$drives[ $drive_name ]['enabled'] |
| 81 |
); |
| 82 |
case 'ftp': |
| 83 |
return new WPDA_Ftp( |
| 84 |
'ftp', |
| 85 |
self::$drives[ $drive_name ]['drive'], |
| 86 |
self::$drives[ $drive_name ]['enabled'] |
| 87 |
); |
| 88 |
case 'sftp': |
| 89 |
return new WPDA_Sftp( |
| 90 |
'sftp', |
| 91 |
self::$drives[ $drive_name ]['drive'], |
| 92 |
self::$drives[ $drive_name ]['enabled'] |
| 93 |
); |
| 94 |
case 'dropbox': |
| 95 |
return new WPDA_Dropbox( |
| 96 |
self::$drives[ $drive_name ]['drive'], |
| 97 |
self::$drives[ $drive_name ]['enabled'] |
| 98 |
); |
| 99 |
case 'google_drive': |
| 100 |
return new WPDA_Google_Drive( |
| 101 |
self::$drives[ $drive_name ]['drive'], |
| 102 |
self::$drives[ $drive_name ]['enabled'] |
| 103 |
); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
return false; |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
public static function set_drive( $drive_name, $drive, $drive_type, $enabled ) { |
| 112 |
|
| 113 |
self::init(); |
| 114 |
|
| 115 |
// TODO: Check if name is already used |
| 116 |
|
| 117 |
self::$drives[ $drive_name ] = array( |
| 118 |
'drive' => $drive, |
| 119 |
'type' => $drive_type, |
| 120 |
'enabled' => $enabled, |
| 121 |
); |
| 122 |
|
| 123 |
} |
| 124 |
|
| 125 |
public static function delete_drive( $drive_name ) { |
| 126 |
|
| 127 |
self::init(); |
| 128 |
|
| 129 |
unset( self::$drives[ $drive_name ] ); |
| 130 |
|
| 131 |
} |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
} |