class-backups-backward-compatibility.php
7.38 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
namespace Smush\Core\Backups;
use Smush\Core\Controller;
use Smush\Core\File_System;
use Smush\Core\Media\Media_Item;
use Smush\Core\Media\Media_Item_Cache;
class Backups_Backward_Compatibility extends Controller {
const ORIGINAL_FILE_META_KEY = 'wp-smush-original_file';
const PNG_PATH_INDEX = 'smush_png_path';
private $media_item_cache;
/**
* @var File_System
*/
private $fs;
public function __construct() {
$this->media_item_cache = Media_Item_Cache::get_instance();
$this->fs = new File_System();
// TODO: deprecate the png2jpg backup hook in favor of the fallback size hook
}
public function init() {
parent::init();
add_action( 'delete_attachment', array( $this, 'delete_old_backup_files' ) );
$this->add_backup_sizes_filter();
}
public function stop() {
$this->remove_backup_sizes_filter();
remove_action( 'delete_attachment', array( $this, 'delete_old_backup_files' ) );
parent::stop();
}
public function maybe_use_deprecated_backup_sizes_meta( $original, $attachment_id, $meta_key ) {
if ( $meta_key !== Media_Item::BACKUP_SIZES_META_KEY ) {
return $original;
}
$media_item = $this->media_item_cache->get( $attachment_id );
if ( ! $media_item->is_image() ) {
return $original;
}
$actual_backup_sizes_meta = $this->get_unfiltered_backup_sizes_meta( $attachment_id );
if ( ! empty( $actual_backup_sizes_meta[ Media_Item::DEFAULT_BACKUP_KEY ] ) ) {
/**
* If {@see Media_Item::DEFAULT_BACKUP_KEY} is already set then we don't want to overwrite it with an older, potentially inaccurate value
*/
return $original;
}
$new_meta = $this->maybe_use_original_file_meta( $media_item, $actual_backup_sizes_meta );
if ( $new_meta ) {
return array( $new_meta );
}
$new_meta = $this->maybe_use_smush_png_path( $media_item, $actual_backup_sizes_meta );
if ( $new_meta ) {
return array( $new_meta );
}
return $original;
}
private function maybe_use_original_file_meta( $media_item, $backup_sizes_meta ) {
$attachment_id = $media_item->get_id();
$meta_key = self::ORIGINAL_FILE_META_KEY;
$original_file_meta = get_post_meta( $attachment_id, $meta_key, true );
if ( empty( $original_file_meta ) ) {
return false;
}
delete_post_meta( $attachment_id, $meta_key );
$original_file_meta_path = $this->get_original_file_path( $original_file_meta );
if ( ! $this->fs->file_exists( $original_file_meta_path ) ) {
return false;
}
$new_backup_meta = $this->make_new_backup_meta(
$backup_sizes_meta,
$original_file_meta_path
);
$this->update_backup_sizes_meta( $attachment_id, $new_backup_meta );
return $new_backup_meta;
}
private function maybe_use_smush_png_path( $media_item, $backup_sizes_meta ) {
$attachment_id = $media_item->get_id();
$png_path_key = self::PNG_PATH_INDEX;
if ( empty( $backup_sizes_meta[ $png_path_key ]['file'] ) ) {
return false;
}
$smush_png_file = $backup_sizes_meta[ $png_path_key ]['file'];
$smush_png_path = $this->file_name_to_path( $media_item, $smush_png_file );
unset( $backup_sizes_meta[ $png_path_key ] );
$backup_sizes_meta = $this->make_new_backup_meta( $backup_sizes_meta, $smush_png_path );
$this->update_backup_sizes_meta( $attachment_id, $backup_sizes_meta );
if ( $this->fs->file_exists( $smush_png_path ) ) {
return $backup_sizes_meta;
} else {
return false;
}
}
private function make_new_backup_meta( $existing_backup_meta, $file_path ) {
list( $width, $height ) = $this->fs->getimagesize( $file_path );
$existing_backup_meta[ Media_Item::DEFAULT_BACKUP_KEY ] = array(
'file' => basename( $file_path ),
'width' => $width,
'height' => $height,
);
return $existing_backup_meta;
}
/**
* @param $attachment_id
*
* @return mixed
*/
private function get_unfiltered_backup_sizes_meta( $attachment_id ) {
$this->remove_backup_sizes_filter();
$post_meta = get_post_meta( $attachment_id, Media_Item::BACKUP_SIZES_META_KEY, true );
$this->add_backup_sizes_filter();
return empty( $post_meta ) ? array() : $post_meta;
}
private function update_backup_sizes_meta( $attachment_id, $meta_value ) {
$this->remove_backup_sizes_filter();
if ( empty( $meta_value ) ) {
delete_post_meta( $attachment_id, Media_Item::BACKUP_SIZES_META_KEY );
} else {
update_post_meta( $attachment_id, Media_Item::BACKUP_SIZES_META_KEY, $meta_value );
}
$this->add_backup_sizes_filter();
}
/**
* @return void
*/
private function add_backup_sizes_filter() {
add_action( 'get_post_metadata', array( $this, 'maybe_use_deprecated_backup_sizes_meta' ), 10, 3 );
}
/**
* @return void
*/
private function remove_backup_sizes_filter() {
remove_filter( 'get_post_metadata', array( $this, 'maybe_use_deprecated_backup_sizes_meta' ) );
}
public function delete_old_backup_files( $attachment_id ) {
$this->remove_backup_sizes_filter();
$this->_delete_old_backup_files( $attachment_id );
$this->add_backup_sizes_filter();
}
private function _delete_old_backup_files( $attachment_id ) {
$media_item = $this->media_item_cache->get( $attachment_id );
if ( $media_item->is_valid() ) {
$this->delete_original_file( $attachment_id, $media_item );
$this->delete_png_meta_value_and_file( $media_item );
$this->delete_backup_files_for_sizes( $media_item );
}
}
/**
* @param $attachment_id
* @param Media_Item $media_item
*
* @return void
*/
private function delete_original_file( $attachment_id, $media_item ) {
$meta_key = self::ORIGINAL_FILE_META_KEY;
$original_file_meta = get_post_meta( $attachment_id, $meta_key, true );
if ( ! empty( $original_file_meta ) ) {
$original_file_meta_path = $this->get_original_file_path( $original_file_meta );
if ( $this->fs->file_exists( $original_file_meta_path ) ) {
$this->fs->unlink( $original_file_meta_path );
}
}
}
private function get_original_file_path( $original_file_meta ) {
$upload_dir = wp_upload_dir();
$basedir = trailingslashit( $upload_dir['basedir'] );
return path_join( $basedir, $original_file_meta );
}
private function delete_png_meta_value_and_file( $media_item ) {
$attachment_id = $media_item->get_id();
$png_path_key = self::PNG_PATH_INDEX;
$backup_sizes_meta = $this->get_unfiltered_backup_sizes_meta( $attachment_id );
if ( empty( $backup_sizes_meta[ $png_path_key ]['file'] ) ) {
return;
}
$smush_png_file = $backup_sizes_meta[ $png_path_key ]['file'];
$smush_png_path = $this->file_name_to_path( $media_item, $smush_png_file );
unset( $backup_sizes_meta[ $png_path_key ] );
$backup_sizes_meta = $this->make_new_backup_meta( $backup_sizes_meta, $smush_png_path );
$this->update_backup_sizes_meta( $attachment_id, $backup_sizes_meta );
if ( $this->fs->file_exists( $smush_png_path ) ) {
$this->fs->unlink( $smush_png_path );
}
}
/**
* @param Media_Item $media_item
* @param $original_file_meta
*
* @return string
*/
private function file_name_to_path( $media_item, $original_file_meta ) {
return path_join( $media_item->get_dir(), $original_file_meta );
}
/**
* @param $media_item Media_Item
*
* @return void
*/
private function delete_backup_files_for_sizes( $media_item ) {
foreach ( $media_item->get_sizes() as $size ) {
$backup_file_path = $size->get_dir() . $size->get_file_name_without_extension() . '.bak.' . $size->get_extension();
if ( $this->fs->file_exists( $backup_file_path ) ) {
$this->fs->unlink( $backup_file_path );
}
}
}
}