class-backups.php
6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
namespace Smush\Core\Backups;
use Smush\Core\File_System;
use Smush\Core\Helper;
use Smush\Core\Media\Media_Item;
use Smush\Core\Media\Media_Item_Optimizer;
use Smush\Core\Settings;
use WDEV_Logger;
class Backups {
/**
* @var WDEV_Logger
*/
private $logger;
/**
* @var Settings|null
*/
private $settings;
/**
* @var File_System
*/
private $fs;
public function __construct() {
$this->logger = Helper::logger()->backup();
$this->settings = Settings::get_instance();
$this->fs = new File_System();
}
public function create_backup_file( $source_image_path ) {
$bak_file_path = $this->generate_unique_bak_file_path( $source_image_path );
$copied = $this->fs->copy( $source_image_path, $bak_file_path );
if ( $copied ) {
return basename( $bak_file_path );
}
return false;
}
private function generate_unique_bak_file_path( $source_image_path ) {
// TODO: why not use the wp_unique_filename method for this?
$path_info = pathinfo( $source_image_path );
$ext = $path_info['extension'];
$bak_ext = ".bak.$ext";
$file_without_ext = trailingslashit( $path_info['dirname'] ) . $path_info['filename'];
$bak_file_path = $file_without_ext . $bak_ext;
if ( ! $this->fs->file_exists( $bak_file_path ) ) {
return $bak_file_path;
}
$count = 1;
$bak_file_path = $file_without_ext . '-' . $count . $bak_ext;
while ( $this->fs->file_exists( $bak_file_path ) ) {
$count ++;
$bak_file_path = $file_without_ext . '-' . $count . $bak_ext;
}
return $bak_file_path;
}
/**
* @param $media_item Media_Item
* @param $optimizer Media_Item_Optimizer
*
* @return bool
*/
public function maybe_create_backup( $media_item, $optimizer ) {
if ( ! $this->settings->is_backup_active() ) {
return false;
}
// Maybe it already exists?
$backup_size = $media_item->get_default_backup_size();
if ( $backup_size && $backup_size->get_file_path() && $this->fs->file_exists( $backup_size->get_file_path() ) ) {
// We have a perfectly viable backup available already
$this->logger->info( sprintf( 'Found an existing backed up file [%s] for attachment [%d].', $backup_size->get_file(), $media_item->get_id() ) );
return false;
}
$size_to_backup = $media_item->get_full_or_scaled_size();
if ( ! $size_to_backup || ! $size_to_backup->file_exists() ) {
$this->logger->warning( sprintf( 'File not found, could not backup up for attachment [%d].', $media_item->get_id() ) );
return false;
}
$create_copy = $optimizer->should_optimize_size( $size_to_backup );
// Create the backup
$file_name = $size_to_backup->get_file_name();
$width = $size_to_backup->get_width();
$height = $size_to_backup->get_height();
if ( $create_copy ) {
$file_name = $this->create_backup_file( $size_to_backup->get_file_path() );
if ( ! $file_name ) {
$this->logger->info( sprintf( 'File operation failed when trying to create backup file [%s] for attachment [%d].', $size_to_backup->get_file_path(), $media_item->get_id() ) );
return false;
}
}
$media_item->add_backup_size( $file_name, $width, $height );
$media_item->save();
return true;
}
/**
* @param $media_item Media_Item
*
* @return bool
*/
public function restore_backup( $media_item ) {
return $this->restore_backup_to_file_path(
$media_item,
$media_item->get_original_image_path() // Directly using the path here because the size object is not available when the file doesn't exist on the disk
);
}
/**
* @param $media_item Media_Item
* @param $file_path
*
* @return bool
*/
public function restore_backup_to_file_path( $media_item, $file_path ) {
$restored = false;
$backup_file_path = '';
$attachment_id = $media_item->get_id();
do {
$backup_size = $media_item->get_default_backup_size();
if ( ! $backup_size ) {
$this->logger->warning( sprintf( 'A restore was attempted for attachment [%d] but we did not find a backup file.', $media_item->get_id() ) );
break;
}
$backup_file_path = $backup_size->get_file_path();
do_action( 'wp_smush_before_restore_backup', $backup_file_path, $attachment_id, $file_path );
if ( ! $this->fs->file_exists( $backup_file_path ) ) {
// Clean up
$media_item->remove_default_backup_size();
$media_item->save();
$this->logger->warning( sprintf( 'A restore was attempted for attachment [%d] but the backup file does not exist.', $media_item->get_id() ) );
break;
}
$is_separate_backup_file = $backup_file_path !== $file_path;
if ( $is_separate_backup_file ) {
$copied = $this->fs->copy( $backup_file_path, $file_path );
if ( $copied ) {
$this->fs->unlink( $backup_file_path );
} else {
$this->logger->warning( sprintf( 'Error copying from [%s] to [%s].', $backup_file_path, $file_path ) );
break;
}
}
wp_generate_attachment_metadata( $attachment_id, $file_path );
/*
* TODO: we might want to follow media_handle_upload which makes an extra update attachment call like this:
* wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
*/
// The metadata has changed because we called wp_generate_attachment_metadata. We need to reset before saving.
$media_item->reset();
$media_item->remove_default_backup_size();
$media_item->save();
$restored = true;
} while ( 0 );
do_action( 'wp_smush_after_restore_backup', $restored, $backup_file_path, $attachment_id, $file_path );
return $restored;
}
/**
* TODO: merge somehow with \Smush\Core\Modules\Backup::get_attachments_with_backups
* @return int
*/
private function count_attachments_with_backups() {
global $wpdb;
$wild = '%';
$backup_key_like = $wild . $wpdb->esc_like( Media_Item::DEFAULT_BACKUP_KEY ) . $wild;
$no_backup_files = $wpdb->get_var(
$wpdb->prepare(
"SELECT count(*)
FROM {$wpdb->postmeta}
WHERE meta_key=%s AND `meta_value` LIKE %s",
Media_Item::BACKUP_SIZES_META_KEY,
$backup_key_like
)
);
return (int) $no_backup_files;
}
public function items_with_backup_exist() {
return $this->count_attachments_with_backups() > 0;
}
}