forked from short-pixel-optimizer/enable-media-replace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththumbnail_updater.php
140 lines (110 loc) · 4.18 KB
/
thumbnail_updater.php
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
<?php
if ( ! defined( 'ABSPATH' ) )
exit; // Exit if accessed directly.
use EnableMediaReplace\ShortPixelLogger\ShortPixelLogger as Log;
use EnableMediaReplace\Notices\NoticeController as Notices;
/* Simple class for updating thumbnails.
*/
class ThumbnailUpdater
{
protected $attach_id;
protected $oldMeta = array();
protected $newMeta = array();
protected $convertArray = array();
protected $relPath;
protected $post_table;
public function __construct($id)
{
$this->attach_id = intval($id);
global $wpdb;
$table_name = $wpdb->prefix . "posts";
// $postmeta_table_name = $wpdb->prefix . "postmeta";
$this->post_table = $table_name;
}
public function setOldMetadata($metadata)
{
if (isset($metadata['sizes']))
$this->oldMeta = $metadata;
}
public function setNewMetadata($metadata)
{
if (isset($metadata['sizes']))
$this->newMeta = $metadata;
// extract month prefix to prevent overwriting wrong images.
$file = $metadata['file'];
$pos = strrpos($metadata['file'], '/');
$month_path = substr($file, 0, $pos);
$this->relPath = trailingslashit($month_path);
}
public function updateThumbnails()
{
if (count($this->oldMeta) == 0 || count($this->newMeta) == 0)
return false;
$convertArray = array();
foreach($this->oldMeta['sizes'] as $sizeName => $data)
{
if (isset($this->newMeta['sizes'][$sizeName]))
{
//in some rare cases 'file' is missing
$oldFile = isset($data['file']) ? $data['file'] : null;
if(is_array($oldFile)) { $oldFile = $oldFile[0];} // HelpScout case 709692915
if(empty($oldFile)) {
return false; //make sure we don't replace in this case as we will break the URLs for all the images in the folder.
}
$newFile = $this->newMeta['sizes'][$sizeName]['file'];
// if images are not same size.
if ($oldFile != $newFile)
{
$this->convertArray[] = array('imageFrom' => $this->relPath . $oldFile, 'imageTo' => $this->relPath . $newFile );
}
}
else {
$this->findNearestSize($data, $sizeName);
}
}
$this->updateDatabase();
}
protected function updateDatabase()
{
global $wpdb;
$sql = "UPDATE " . $this->post_table . " set post_content = REPLACE(post_content, %s, %s)";
Log::addDebug('Thumbnail Updater - Converting Thumbnails for sizes', $this->convertArray);
foreach($this->convertArray as $convert_item)
{
$from = $convert_item['imageFrom'];
$to = $convert_item['imageTo'];
$replace_sql = $wpdb->prepare($sql, $from, $to );
$wpdb->query($replace_sql);
}
}
/** FindNearestsize
* This works on the assumption that when the exact image size name is not available, find the nearest width with the smallest possible difference to impact the site the least.
*/
protected function findNearestSize($oldData, $sizeName)
{
$old_width = $oldData['width']; // the width from size not in new image
$new_width = $this->newMeta['width']; // default check - new width on image
$diff = abs($old_width - $new_width);
$closest_file = str_replace($this->relPath, '', $this->newMeta['file']);
foreach($this->newMeta['sizes'] as $sizeName => $data)
{
$thisdiff = abs($old_width - $data['width']);
if ( $thisdiff < $diff )
{
$closest_file = $data['file'];
if(is_array($closest_file)) { $closest_file = $closest_file[0];} // HelpScout case 709692915
if(!empty($closest_file)) {
$diff = $thisdiff;
$found_metasize = true;
}
}
}
if(empty($closest_file)) return;
$oldFile = $oldData['file'];
if(is_array($oldFile)) { $oldFile = $oldFile[0];} // HelpScout case 709692915
if(empty($oldFile)) {
return; //make sure we don't replace in this case as we will break the URLs for all the images in the folder.
}
$this->convertArray[] = array('imageFrom' => $this->relPath . $oldFile, 'imageTo' => $this->relPath . $closest_file);
}
}