1 | <?php |
---|
2 | /* |
---|
3 | * Phoenix Media Rename main class |
---|
4 | * |
---|
5 | */ |
---|
6 | |
---|
7 | require_once('class-pmr-options.php'); |
---|
8 | |
---|
9 | #region constants |
---|
10 | |
---|
11 | define("pluginArchivarixExternalImagesImporter", "archivarix-external-images-importer/archivarix-external-images-importer.php"); |
---|
12 | define("pluginAmazonS3AndCloudfront", "amazon-s3-and-cloudfront/wordpress-s3.php"); |
---|
13 | define("pluginSmartSlider3", "smart-slider-3/smart-slider-3.php"); |
---|
14 | define("pluginShortpixelImageOptimiser", "shortpixel-image-optimiser/wp-shortpixel.php"); |
---|
15 | define("pluginWPML", "sitepress-multilingual-cms/sitepress.php"); |
---|
16 | define("pluginRedirection", "redirection/redirection.php"); |
---|
17 | define("pluginRankMath", "seo-by-rank-math/rank-math.php"); |
---|
18 | define("pluginElementor", "elementor/elementor.php"); |
---|
19 | define("pluginAltTextAI", "alttext-ai/atai.php"); |
---|
20 | |
---|
21 | abstract class elementor_element |
---|
22 | { |
---|
23 | const css = 0; |
---|
24 | const data = 1; |
---|
25 | } |
---|
26 | |
---|
27 | #endregion |
---|
28 | |
---|
29 | if (pmr_plugins::is_plugin_active(constant("pluginAltTextAI"))){ |
---|
30 | $options = new pmr_options(); |
---|
31 | |
---|
32 | if ($options->option_enable_alttext_integration){ |
---|
33 | add_action('atai_alttext_generated', 'pmr_on_alttext_generated', 10, 2); |
---|
34 | } |
---|
35 | |
---|
36 | function pmr_on_alttext_generated($attachment_id, $alt_text) { |
---|
37 | Phoenix_Media_Rename::do_rename($attachment_id, $alt_text); |
---|
38 | } |
---|
39 | } |
---|
40 | |
---|
41 | class pmr_plugins{ |
---|
42 | |
---|
43 | /** |
---|
44 | * check if plugin is active |
---|
45 | * |
---|
46 | * @param string $plugin_name |
---|
47 | * @return boolean |
---|
48 | */ |
---|
49 | static function is_plugin_active($plugin_name){ |
---|
50 | if(in_array($plugin_name, apply_filters('active_plugins', get_option('active_plugins')))){ |
---|
51 | return true; |
---|
52 | } else { |
---|
53 | return false; |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | #region Smart Slider compatibility |
---|
58 | |
---|
59 | /** |
---|
60 | * Update Smart Slider 3 custom table |
---|
61 | * |
---|
62 | * @param string $old_filename |
---|
63 | * @param string $new_filename |
---|
64 | * @param string $extension |
---|
65 | * @return void |
---|
66 | */ |
---|
67 | static function update_smartslider($old_filename, $new_filename, $extension){ |
---|
68 | global $wpdb; |
---|
69 | |
---|
70 | //compose file names |
---|
71 | $old_filename = $old_filename . '.' . $extension; |
---|
72 | $new_filename = $new_filename . '.' . $extension; |
---|
73 | |
---|
74 | if(empty($old_filename) || empty($new_filename)) |
---|
75 | { |
---|
76 | return false; |
---|
77 | } |
---|
78 | if ($old_filename == ''){ |
---|
79 | return false; |
---|
80 | } |
---|
81 | |
---|
82 | //escape filename for use in LIKE statement |
---|
83 | $old_filename = $wpdb->esc_like($old_filename); |
---|
84 | |
---|
85 | $filter = '%/'. $old_filename; |
---|
86 | |
---|
87 | //compose Smart Slider table name |
---|
88 | $tablename = $wpdb->prefix . 'nextend2_smartslider3_slides'; |
---|
89 | |
---|
90 | if (! pmr_lib::table_exist($tablename)){ |
---|
91 | //if table does not exist, exit and return false |
---|
92 | return false; |
---|
93 | }else{ |
---|
94 | //if table exist, change file name |
---|
95 | $sqlQuery = "UPDATE ". $tablename ." SET thumbnail = REPLACE(thumbnail, %s, %s), params = REPLACE(params, %s, %s) WHERE thumbnail LIKE %s"; |
---|
96 | |
---|
97 | $updated = $wpdb->query( |
---|
98 | $wpdb->prepare( |
---|
99 | $sqlQuery, $old_filename, $new_filename, $old_filename, $new_filename, $filter |
---|
100 | )); |
---|
101 | } |
---|
102 | |
---|
103 | $tablename = $wpdb->prefix . 'nextend2_image_storage'; |
---|
104 | |
---|
105 | if (pmr_lib::table_exist($tablename)){ |
---|
106 | //if table exist, change file name (unnecessary table, does not exit if table is missing) |
---|
107 | $sqlQuery = "UPDATE ". $tablename ." SET image = REPLACE(image, %s, %s) WHERE image LIKE %s"; |
---|
108 | |
---|
109 | $updated = $wpdb->query( |
---|
110 | $wpdb->prepare( |
---|
111 | $sqlQuery, $old_filename, $new_filename, $filter |
---|
112 | )); |
---|
113 | } |
---|
114 | |
---|
115 | return true; |
---|
116 | } |
---|
117 | |
---|
118 | #endregion |
---|
119 | |
---|
120 | #region WPML compatibility |
---|
121 | |
---|
122 | /** |
---|
123 | * Update Smart WPML custom table |
---|
124 | * |
---|
125 | * @param string $extension |
---|
126 | * @return void |
---|
127 | */ |
---|
128 | static function update_wpml($post_id){ |
---|
129 | // Get "trid" of the file |
---|
130 | $trid = apply_filters('wpml_element_trid', NULL, $post_id, 'post_attachment'); |
---|
131 | |
---|
132 | if (empty($trid)) { |
---|
133 | //translation not found |
---|
134 | } else { |
---|
135 | //get all translations |
---|
136 | $translations = apply_filters('wpml_get_element_translations', NULL, $trid); |
---|
137 | |
---|
138 | //iterates through translations to update attachment metadata |
---|
139 | foreach ($translations as $translation) { |
---|
140 | if ($post_id == $translation->element_id) { |
---|
141 | //update filename |
---|
142 | update_post_meta($translation->element_id, '_wp_attached_file', get_post_meta($translation->element_id, '_wp_attached_file', true)); |
---|
143 | |
---|
144 | //update metadata |
---|
145 | update_post_meta($translation->element_id, '_wp_attachment_metadata', get_post_meta($translation->element_id, '_wp_attachment_metadata', true)); |
---|
146 | } |
---|
147 | } |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | #endregion |
---|
152 | |
---|
153 | #region Redirection compatibility |
---|
154 | |
---|
155 | /** |
---|
156 | * Add a redirection from the old URL to the NEW one using Redirection plugin |
---|
157 | * |
---|
158 | * @param string $old_filename |
---|
159 | * @param string $new_filename |
---|
160 | * @param string $extension |
---|
161 | * @param boolean $option_create_redirection |
---|
162 | * @return void |
---|
163 | */ |
---|
164 | static function add_redirection($old_filename, $new_filename, $extension, $file_subfolder, $option_create_redirection, $plugin){ |
---|
165 | if ($option_create_redirection){ |
---|
166 | //option is active |
---|
167 | |
---|
168 | //fix file name |
---|
169 | if ($file_subfolder){ |
---|
170 | $old_filename = $file_subfolder . $old_filename . '.' . $extension; |
---|
171 | $new_filename = $file_subfolder . $new_filename .'.' . $extension; |
---|
172 | } else { |
---|
173 | $old_filename = $old_filename . '.' . $extension; |
---|
174 | $new_filename = $new_filename .'.' . $extension; |
---|
175 | } |
---|
176 | |
---|
177 | //add upload folder |
---|
178 | if (defined('UPLOADS')) { |
---|
179 | $upload_folder = UPLOADS; |
---|
180 | |
---|
181 | $old_filename = get_site_url() . '/' . $upload_folder . '/' . $old_filename; |
---|
182 | $new_filename = get_site_url() . '/' . $upload_folder . '/' . $new_filename; |
---|
183 | } else { |
---|
184 | $upload_folder = wp_upload_dir()['baseurl'] . '/'; |
---|
185 | |
---|
186 | $old_filename = $upload_folder . $old_filename; |
---|
187 | $new_filename = $upload_folder . $new_filename; |
---|
188 | } |
---|
189 | |
---|
190 | $old_filename = str_replace('\\', '/' , $old_filename); |
---|
191 | $new_filename = str_replace('\\', '/' , $new_filename); |
---|
192 | |
---|
193 | switch ($plugin){ |
---|
194 | case constant("pluginRedirection"): |
---|
195 | //Redirection |
---|
196 | try{ |
---|
197 | if (class_exists('Red_Item')){ |
---|
198 | //include Redirection code |
---|
199 | require_once WP_PLUGIN_DIR . '/redirection/models/group.php'; |
---|
200 | |
---|
201 | $details = [ |
---|
202 | 'url' => $old_filename, |
---|
203 | 'action_data' => [ 'url' => $new_filename ], |
---|
204 | 'action_type' => 'url', |
---|
205 | 'title' => 'Phoenix Media Rename', |
---|
206 | 'status' => 'enabled', |
---|
207 | 'regex' => false, |
---|
208 | 'group_id' => 2, //set group to "updated posts" |
---|
209 | 'match_type' => 'url', |
---|
210 | ]; |
---|
211 | |
---|
212 | //add redirection via Redirection's functions |
---|
213 | $result = Red_Item::create($details); |
---|
214 | } |
---|
215 | }catch(exception $e){ |
---|
216 | } |
---|
217 | |
---|
218 | break; |
---|
219 | case constant("pluginRankMath"): |
---|
220 | //Rank Math SEO |
---|
221 | try{ |
---|
222 | $redirection = RankMath\Redirections\Redirection::from( |
---|
223 | [ |
---|
224 | 'url_to' => $new_filename, |
---|
225 | 'header_code' => '301', |
---|
226 | ] |
---|
227 | ); |
---|
228 | |
---|
229 | $redirection->set_nocache(true); |
---|
230 | $redirection->add_source($old_filename, 'exact'); |
---|
231 | $redirection->add_destination($new_filename); |
---|
232 | $redirection->save(); |
---|
233 | }catch(exception $e){ |
---|
234 | } |
---|
235 | break; |
---|
236 | } |
---|
237 | } |
---|
238 | } |
---|
239 | |
---|
240 | #endregion |
---|
241 | |
---|
242 | #region ShortPixel compatibility |
---|
243 | |
---|
244 | /** |
---|
245 | * Update all ShortPixel metadata to avoid another compression of the image after the renaming process |
---|
246 | * |
---|
247 | * @param array $result |
---|
248 | * @param string $old_filename |
---|
249 | * @param string $new_filename |
---|
250 | * @param integer $attachment_id |
---|
251 | * @param string $file_path |
---|
252 | * |
---|
253 | * @return array |
---|
254 | */ |
---|
255 | static function update_shortpixel_metadata($result, $old_filename, $new_filename, $attachment_id, $file_path){ |
---|
256 | if (pmr_plugins::is_plugin_active(constant("pluginShortpixelImageOptimiser"))) { |
---|
257 | //change filename in thumnail list |
---|
258 | $shortpixelKey = 'thumbsOptList'; |
---|
259 | $result = self::update_single_shortpixel_metadata($result, $shortpixelKey, $old_filename, $new_filename); |
---|
260 | |
---|
261 | //change filename in exclusion list |
---|
262 | $shortpixelKey = 'excludeSizes'; |
---|
263 | $result = self::update_single_shortpixel_metadata($result, $shortpixelKey, $old_filename, $new_filename); |
---|
264 | |
---|
265 | self::update_shortpixel_filenames($attachment_id, $old_filename, $new_filename, $file_path); |
---|
266 | |
---|
267 | //update shortpixel custom tables |
---|
268 | self::update_single_shortpixel_table($attachment_id, $old_filename, $new_filename); |
---|
269 | } |
---|
270 | |
---|
271 | //return result even if plugin is inactive |
---|
272 | return $result; |
---|
273 | } |
---|
274 | |
---|
275 | /** |
---|
276 | * Update shortpixel custom tables |
---|
277 | * |
---|
278 | * @param integer $attachment_id |
---|
279 | * @param string $old_filename |
---|
280 | * @param string $new_filename |
---|
281 | * @param string $file_path |
---|
282 | * @return void |
---|
283 | */ |
---|
284 | static function update_shortpixel_filenames($attachment_id, $old_filename, $new_filename, $file_path){ |
---|
285 | global $wpdb; |
---|
286 | |
---|
287 | try { |
---|
288 | $table_name = $wpdb->prefix . 'shortpixel_postmeta'; |
---|
289 | |
---|
290 | $query = $wpdb->prepare('SELECT extra_info |
---|
291 | FROM ' . $table_name . ' |
---|
292 | WHERE parent = %d |
---|
293 | OR attach_id = %d', |
---|
294 | $attachment_id, $attachment_id |
---|
295 | ); |
---|
296 | |
---|
297 | $thumbnails = $wpdb->get_results($query, ARRAY_A); |
---|
298 | |
---|
299 | foreach ($thumbnails as $thumbnail){ |
---|
300 | try{ |
---|
301 | //get the webp filename |
---|
302 | $image = json_decode($thumbnail['extra_info'])->webp; |
---|
303 | |
---|
304 | //rename the file |
---|
305 | self::rename($file_path, $image, $new_filename); |
---|
306 | |
---|
307 | //get the avif filename |
---|
308 | $image = json_decode($thumbnail['extra_info'])->avif; |
---|
309 | |
---|
310 | //rename the file |
---|
311 | self::rename($file_path, $image, $new_filename); |
---|
312 | } catch (exception $e){ |
---|
313 | |
---|
314 | } |
---|
315 | } |
---|
316 | |
---|
317 | } catch(exception $e){ |
---|
318 | |
---|
319 | } |
---|
320 | } |
---|
321 | |
---|
322 | /** |
---|
323 | * Renames a file |
---|
324 | * |
---|
325 | * @param string $path |
---|
326 | * @param string $old_filename |
---|
327 | * @param string $new_filename |
---|
328 | * |
---|
329 | * @return void |
---|
330 | */ |
---|
331 | private static function rename($path, $old_filename, $new_filename){ |
---|
332 | //get filename |
---|
333 | $file_parts = pathinfo($old_filename); |
---|
334 | $namepart = $file_parts['filename']; |
---|
335 | $pattern = "/[-][0-9]+[x][0-9]+$/i"; |
---|
336 | |
---|
337 | if (preg_match($pattern, $namepart)){ |
---|
338 | //filename ends with resolution, it is a thumbnail |
---|
339 | $position = strrpos($namepart, "-"); |
---|
340 | |
---|
341 | //remove the resolution to get the real name |
---|
342 | $real_name = substr($namepart, 0, $position); |
---|
343 | } else { |
---|
344 | //filename doesn't ends with resolution, it is the main file |
---|
345 | $real_name = $namepart; |
---|
346 | } |
---|
347 | |
---|
348 | //create full filenames |
---|
349 | $full_old_filename = $path . $old_filename; |
---|
350 | $full_new_filename = $path . str_replace($real_name, $new_filename, $old_filename); |
---|
351 | |
---|
352 | //create the new file |
---|
353 | if (!copy($full_old_filename, $full_new_filename)) return __('File renaming error! Tried to copy ' . $full_old_filename . ' to ' . $full_new_filename); |
---|
354 | |
---|
355 | //delete old media file, thumbnails will be deleted later |
---|
356 | if (!unlink($full_old_filename)) return __('File renaming error! Tried to delete ' . $full_old_filename); |
---|
357 | } |
---|
358 | |
---|
359 | /** |
---|
360 | * Update shortpixel custom tables |
---|
361 | * |
---|
362 | * @param integer $attachment_id |
---|
363 | * @param string $old_filename |
---|
364 | * @param string $new_filename |
---|
365 | * @return void |
---|
366 | */ |
---|
367 | static function update_single_shortpixel_table($attachment_id, $old_filename, $new_filename){ |
---|
368 | global $wpdb; |
---|
369 | |
---|
370 | $table_name = $wpdb->prefix . 'shortpixel_postmeta'; |
---|
371 | |
---|
372 | try { |
---|
373 | $wpdb->query( |
---|
374 | $wpdb->prepare('UPDATE ' . $table_name . ' |
---|
375 | SET extra_info = REPLACE (extra_info, %s, %s) |
---|
376 | WHERE parent = %d |
---|
377 | OR attach_id = %d', |
---|
378 | array(str_replace('/', '\/', $old_filename) , str_replace('/', '\/', $new_filename), $attachment_id, $attachment_id) |
---|
379 | ) |
---|
380 | ); |
---|
381 | } catch(exception $e){ |
---|
382 | |
---|
383 | } |
---|
384 | } |
---|
385 | |
---|
386 | /** |
---|
387 | * Update single ShortPixel metadata |
---|
388 | * |
---|
389 | * @param array $result |
---|
390 | * @param string $key: metadata to update |
---|
391 | * @param string $old_filename |
---|
392 | * @param string $new_filename |
---|
393 | * |
---|
394 | * @return array |
---|
395 | */ |
---|
396 | static function update_single_shortpixel_metadata($result, $key, $old_filename, $new_filename){ |
---|
397 | //check if Shortpixel data contains thumbs data |
---|
398 | try{ |
---|
399 | //check if result contains ShortPixel data |
---|
400 | if (array_key_exists('ShortPixel', $result)){ |
---|
401 | //check if ShortPixel data contains key |
---|
402 | if (array_key_exists($key, $result['ShortPixel'])){ |
---|
403 | //iterates through ShortPixel data to update filename |
---|
404 | for ($i = 0; $i < count($result['ShortPixel'][$key]); $i++){ |
---|
405 | try{ |
---|
406 | $shortpixel_meta = $result['ShortPixel'][$key][$i]; |
---|
407 | |
---|
408 | $new_shortpixel_meta = str_replace($old_filename, $new_filename, $shortpixel_meta); |
---|
409 | |
---|
410 | if ($shortpixel_meta != $new_shortpixel_meta){ |
---|
411 | $result['ShortPixel'][$key][$i] = $new_shortpixel_meta; |
---|
412 | } |
---|
413 | }catch(exception $e){ |
---|
414 | |
---|
415 | } |
---|
416 | } |
---|
417 | } |
---|
418 | } |
---|
419 | } catch(exception $e) { |
---|
420 | |
---|
421 | } |
---|
422 | |
---|
423 | return $result; |
---|
424 | } |
---|
425 | #endregion |
---|
426 | |
---|
427 | #region Elementor compatibility |
---|
428 | |
---|
429 | /** |
---|
430 | * update elementor data, it will be used by Elementor to regenerate css file |
---|
431 | * |
---|
432 | * @param integer $post_id |
---|
433 | * @param string $key |
---|
434 | * @param array $searches |
---|
435 | * @param array $replaces |
---|
436 | * @return void |
---|
437 | */ |
---|
438 | static function update_elementor_data($post_id, $key = '', $searches = '', $replaces = ''){ |
---|
439 | global $wpdb; |
---|
440 | |
---|
441 | if (pmr_plugins::is_plugin_active(constant("pluginElementor"))) { |
---|
442 | $table_name = $wpdb->prefix . 'postmeta'; |
---|
443 | |
---|
444 | switch ($key){ |
---|
445 | case '_elementor_css': |
---|
446 | //delete elementor css, it will be generated at first page visit |
---|
447 | try { |
---|
448 | $wpdb->query( |
---|
449 | $wpdb->prepare('DELETE FROM ' . $table_name . ' |
---|
450 | WHERE post_id = %d |
---|
451 | AND meta_key = %s', |
---|
452 | array($post_id, '_elementor_css') |
---|
453 | ) |
---|
454 | ); |
---|
455 | } catch(exception $e){ |
---|
456 | |
---|
457 | } |
---|
458 | break; |
---|
459 | case '_wp_page_template': |
---|
460 | try{ |
---|
461 | //do nothing |
---|
462 | } catch(exception $e){ |
---|
463 | |
---|
464 | } |
---|
465 | break; |
---|
466 | case '_elementor_data': |
---|
467 | try{ |
---|
468 | for ($i = 0; $i < sizeof($searches); $i++){ |
---|
469 | $wpdb->query( |
---|
470 | $wpdb->prepare('UPDATE ' . $table_name . ' |
---|
471 | SET meta_value = REPLACE (meta_value, %s, %s) |
---|
472 | WHERE post_id = %d |
---|
473 | AND meta_key = %s', |
---|
474 | array(str_replace('/', '\/', $searches[$i]) , str_replace('/', '\/', $replaces[$i]), $post_id, $key) |
---|
475 | ) |
---|
476 | ); |
---|
477 | } |
---|
478 | } catch(exception $e){ |
---|
479 | |
---|
480 | } |
---|
481 | break; |
---|
482 | default: |
---|
483 | //update wp_postmeta |
---|
484 | // $meta[0] = pmr_lib::unserialize_deep($meta[0]); |
---|
485 | // $new_meta = pmr_lib::replace_media_urls($meta[0], $searches, $replaces); |
---|
486 | // //there is an issue with Elementor, check when _wp_page_template changes |
---|
487 | // if ($new_meta != $meta[0]) update_post_meta($post_id, $key, $new_meta, $meta[0]); |
---|
488 | } |
---|
489 | } |
---|
490 | } |
---|
491 | |
---|
492 | #endregion |
---|
493 | |
---|
494 | } |
---|
495 | |
---|