tenweb-speed-optimizer
/
vendor
/
10web-utils
/
tenweb-image-optimizer
/
src
/
TenWebIO
/
Backup.php
tenweb-speed-optimizer
/
vendor
/
10web-utils
/
tenweb-image-optimizer
/
src
/
TenWebIO
Last commit date
Exceptions
4 years ago
Queue
2 years ago
Views
2 years ago
Api.php
2 years ago
Attachments.php
2 years ago
Backup.php
2 years ago
CLI.php
3 years ago
Compress.php
2 years ago
CompressDataService.php
3 years ago
CompressService.php
3 years ago
Config.php
2 years ago
Init.php
2 years ago
LastCompress.php
3 years ago
Logs.php
2 years ago
PreInit.php
3 years ago
Rest.php
2 years ago
Settings.php
3 years ago
Utils.php
1 year ago
Backup.php
110 lines
| 1 | <?php |
| 2 | |
| 3 | namespace TenWebIO; |
| 4 | |
| 5 | class Backup |
| 6 | { |
| 7 | public $upload_dir; |
| 8 | public $backup_dir; |
| 9 | |
| 10 | /** |
| 11 | * @param $type |
| 12 | */ |
| 13 | public function __construct($type = 'bulk') |
| 14 | { |
| 15 | $upload_dir = wp_get_upload_dir(); |
| 16 | $this->upload_dir = $upload_dir['basedir']; |
| 17 | $this->backup_dir = $this->upload_dir . "/tenweb_io_backup/" . $type; |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * @param $original_img |
| 22 | * @param $wp_folder |
| 23 | * |
| 24 | * @return void |
| 25 | */ |
| 26 | public function backupBeforeReplace($original_img, $wp_folder) |
| 27 | { |
| 28 | if (!is_dir($this->backup_dir . $wp_folder) && |
| 29 | !mkdir($this->backup_dir . $wp_folder, 0777, true)) { |
| 30 | Logs::setLog("backup:error", 'Error creating folder. Backup dir: ' . $this->backup_dir, 'error'); |
| 31 | } |
| 32 | |
| 33 | if (file_exists($original_img)) { |
| 34 | rename($original_img, $this->backup_dir . "/" . $original_img); |
| 35 | Logs::setLog("backup:log", 'Keep original file. Destination: ' . $this->backup_dir . "/" . $original_img); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @param $source |
| 41 | * @param $destination |
| 42 | * @param $subDir |
| 43 | * |
| 44 | * @return void |
| 45 | */ |
| 46 | public function restoreOriginalImages($source = '', $destination = '', $subDir = '') |
| 47 | { |
| 48 | if (empty($source)) { |
| 49 | $source = $this->backup_dir; |
| 50 | } |
| 51 | if (empty($destination)) { |
| 52 | $destination = $this->upload_dir; |
| 53 | } |
| 54 | $directory = opendir($source); |
| 55 | |
| 56 | if (is_dir($destination) === false) { |
| 57 | mkdir($destination); |
| 58 | } |
| 59 | |
| 60 | if ($subDir !== '') { |
| 61 | if (is_dir("$destination/$subDir") === false) { |
| 62 | mkdir("$destination/$subDir"); |
| 63 | } |
| 64 | |
| 65 | while (($file = readdir($directory)) !== false) { |
| 66 | if ($file === '.' || $file === '..') { |
| 67 | continue; |
| 68 | } |
| 69 | |
| 70 | if (is_dir("$source/$file") === true) { |
| 71 | $this->restoreOriginalImages("$source/$file", "$destination/$subDir/$file"); |
| 72 | } else { |
| 73 | rename("$source/$file", "$destination/$subDir/$file"); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | closedir($directory); |
| 78 | |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | while (($file = readdir($directory)) !== false) { |
| 83 | if ($file === '.' || $file === '..') { |
| 84 | continue; |
| 85 | } |
| 86 | |
| 87 | if (is_dir("$source/$file") === true) { |
| 88 | $this->restoreOriginalImages("$source/$file", "$destination/$file"); |
| 89 | } else { |
| 90 | rename("$source/$file", "$destination/$file"); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | closedir($directory); |
| 95 | } |
| 96 | |
| 97 | public function deleteBackup() |
| 98 | { |
| 99 | foreach (glob("{$this->backup_dir}/*") as $file) { |
| 100 | if (is_dir($file)) { |
| 101 | $this->deleteBackup($file); |
| 102 | } else { |
| 103 | unlink($file); |
| 104 | } |
| 105 | } |
| 106 | rmdir($this->backup_dir); |
| 107 | } |
| 108 | |
| 109 | } |
| 110 |