| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDataAccess\Drive { |
| 4 |
|
| 5 |
use WPDataAccess\WPDA; |
| 6 |
|
| 7 |
class WPDA_Local extends WPDA_Drive { |
| 8 |
|
| 9 |
public function __construct( $drive = array(), $enabled = false ) { |
| 10 |
|
| 11 |
$this->drive_type = 'local'; |
| 12 |
|
| 13 |
parent::__construct( 'local', $drive, $enabled ); |
| 14 |
|
| 15 |
} |
| 16 |
|
| 17 |
public function authorize( $authorization, $enabled = true ) { |
| 18 |
|
| 19 |
if ( isset( $authorization['path'] ) ) { |
| 20 |
$this->drive = array( |
| 21 |
'path' => $authorization['path'], |
| 22 |
); |
| 23 |
$this->enabled = $enabled; |
| 24 |
$this->save(); |
| 25 |
|
| 26 |
return true; |
| 27 |
} |
| 28 |
|
| 29 |
return false; |
| 30 |
|
| 31 |
} |
| 32 |
|
| 33 |
public function refresh_token() { |
| 34 |
|
| 35 |
return true; // N.A. |
| 36 |
|
| 37 |
} |
| 38 |
|
| 39 |
public function connect() { |
| 40 |
|
| 41 |
return true; // N.A. |
| 42 |
|
| 43 |
} |
| 44 |
|
| 45 |
public function close() { |
| 46 |
|
| 47 |
// N.A. |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
public function upload_file( $local_file, $remote_file ) { |
| 52 |
|
| 53 |
copy( $local_file, $this->drive['path'] . $remote_file ); |
| 54 |
|
| 55 |
} |
| 56 |
|
| 57 |
public function cleanup( $query, $keep ) { |
| 58 |
|
| 59 |
$keep_counting = 0; |
| 60 |
$files_sorted = array(); |
| 61 |
|
| 62 |
$path = '/' === substr( $this->drive['path'], -1 ) ? $this->drive['path'] : $this->drive['path'] . '/'; |
| 63 |
|
| 64 |
foreach ( glob( $path . $query ) as $filename ) { |
| 65 |
array_push( $files_sorted, $filename ); |
| 66 |
} |
| 67 |
rsort( $files_sorted ); |
| 68 |
|
| 69 |
foreach ( $files_sorted as $file ) { |
| 70 |
$keep_counting ++; |
| 71 |
if ( $keep_counting > (int) $keep ) { |
| 72 |
unlink( $file ); // phpcs:ignore |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
} |