PluginProbe ʕ •ᴥ•ʔ
Folders – Unlimited Folders to Organize Media Library Folder, Pages, Posts, File Manager / 2.7
Folders – Unlimited Folders to Organize Media Library Folder, Pages, Posts, File Manager v2.7
3.1.9 3.1.8 3.1.7 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 2.9.8 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 trunk 1.3.7 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6 2.6.1 2.6.2 2.6.3 2.6.4 2.6.5 2.6.6 2.6.7 2.6.8 2.6.9 2.7 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9 2.9.1 2.9.2
folders / includes / media.replace.php
folders / includes Last commit date
class-affiliate.php 5 years ago class-polylang.php 5 years ago class-review-box.php 5 years ago class-wpml.php 5 years ago folders.class.php 5 years ago form.class.php 5 years ago media.replace.php 5 years ago plugin.updates.php 5 years ago plugins.class.php 5 years ago tree.class.php 5 years ago
media.replace.php
722 lines
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) exit;
3
4 class folders_replace_media {
5
6 public $button_color;
7
8 public $is_enabled = false;
9
10 public $upgradeLink;
11
12 function __construct() {
13
14 $customize_folders = get_option('customize_folders');
15
16 $this->button_color = isset($customize_folders['media_replace_button'])?$customize_folders['media_replace_button']:"#FA166B";
17
18 $this->is_enabled = isset($customize_folders['folders_enable_replace_media'])?$customize_folders['folders_enable_replace_media']:"yes";
19
20 $this->is_enabled = ($this->is_enabled == "yes")?true:false;
21
22 if($this->is_enabled) {
23
24 add_action('admin_menu', array($this, 'admin_menu'));
25
26 add_filter('media_row_actions', array($this, 'add_media_action'), 10, 2);
27
28 add_action('add_meta_boxes', function () {
29 add_meta_box('folders-replace-box', esc_html__('Replace Media', 'folders'), array($this, 'replace_meta_box'), 'attachment', 'side', 'low');
30 });
31 add_filter('attachment_fields_to_edit', array($this, 'attachment_editor'), 10, 2);
32
33 add_action('admin_enqueue_scripts', array($this, 'folders_admin_css_and_js'));
34
35 add_action('admin_init', array($this, 'handle_folders_file_upload'));
36 }
37
38 $customize_folders = get_option("customize_folders");
39 if(isset($customize_folders['show_folder_in_settings']) && $customize_folders['show_folder_in_settings'] == "yes") {
40 $this->upgradeLink = admin_url("options-general.php?page=wcp_folders_settings&setting_page=upgrade-to-pro");
41 } else {
42 $this->upgradeLink = admin_url("admin.php?page=folders-upgrade-to-pro");
43 }
44
45 /* to replace file name */
46 add_action('add_meta_boxes', function () {
47 add_meta_box('folders-replace-file-name', esc_html__('Change file name', 'folders'), array($this, 'change_file_name_box'), 'attachment', 'side', 'core');
48 });
49
50 add_filter('attachment_fields_to_edit', array($this, 'attachment_replace_name_with_title'), 10, 2);
51
52 add_action('admin_head', array($this, 'premio_replace_file_CSS'));
53
54 add_action('wp_enqueue_media', array($this, 'replace_media_file_script'));
55
56 add_action('admin_notices', array($this, 'admin_notices'));
57 }
58
59 public function admin_notices() {
60 if(isset($_REQUEST['premio_message']) && $_REQUEST['premio_message'] == "success") { ?>
61 <div class="notice notice-success is-dismissible">
62 <p><b><?php esc_html_e( 'File successfully replaced', 'folders' ); ?></b></p>
63 <p><?php esc_html_e( 'The file has been successfully replaced using the file replacement feature', 'folders' ); ?></p>
64 </div>
65
66 <style>
67 .folders-undo-notification {
68 position: fixed;
69 right: -500px;
70 bottom: 25px;
71 width: 280px;
72 background: #fff;
73 padding: 15px;
74 -webkit-box-shadow: 0 3px 6px -4px rgb(0 0 0 / 12%), 0 6px 16px 0 rgb(0 0 0 / 8%), 0 9px 28px 8px rgb(0 0 0 / 5%);
75 box-shadow: 0 3px 6px -4px rgb(0 0 0 / 12%), 0 6px 16px 0 rgb(0 0 0 / 8%), 0 9px 28px 8px rgb(0 0 0 / 5%);
76 transition: all .25s linear;
77 z-index: 250010;
78 }
79 .folders-undo-body {
80 position: relative;
81 font-size: 13px;
82 padding: 0 0 5px 0;
83 }
84 .close-undo-box {
85 position: absolute;
86 right: -10px;
87 top: 0;
88 width: 16px;
89 height: 16px;
90 transition: all .25s linear;
91 }
92 .close-undo-box span {
93 display: block;
94 position: relative;
95 width: 16px;
96 height: 16px;
97 transition: all .2s linear;
98 }
99 .close-undo-box span:after, .close-undo-box span:before {
100 content: "";
101 position: absolute;
102 width: 12px;
103 height: 2px;
104 background-color: #333;
105 display: block;
106 border-radius: 2px;
107 transform: rotate(45deg);
108 top: 7px;
109 left: 2px;
110 }
111 .close-undo-box span:after {
112 transform: rotate(-45deg);
113 }
114 .folders-undo-header {
115 font-weight: 500;
116 font-size: 14px;
117 padding: 0 0 3px 0;
118 color: #014737;
119 }
120 .folders-undo-notification.success {
121 border-left: solid 3px #70C6A3;
122 }
123 html[dir="rtl"] .folders-undo-notification {
124 right: auto;
125 left: -500px
126 }
127 html[dir="rtl"] .folders-undo-notification.active {
128 left: 25px;
129 }
130 html[dir="rtl"] .folders-undo-notification.success {
131 border-left: none;
132 border-right: solid 3px #70C6A3;
133 }
134 html[dir="rtl"] .close-undo-box {
135 right: auto;
136 left: -10px;
137 }
138 </style>
139 <div class="folders-undo-notification success" id="media-success">
140 <div class="folders-undo-body">
141 <a href="javascript:;" class="close-undo-box"><span></span></a>
142 <div class="folders-undo-header"><?php esc_html_e( 'File successfully replaced', 'folders' ); ?></div>
143 <div class="folders-undo-body" style="padding:0"><?php esc_html_e( 'The file has been successfully replaced using the file replacement feature', 'folders' ); ?></div>
144 </div>
145 </div>
146 <script>
147 jQuery(document).ready(function(){
148 jQuery("#media-success").addClass("active");
149 setTimeout(function(){
150 jQuery("#media-success").removeClass("active");
151 }, 5000);
152
153 jQuery(document).on("click", ".close-undo-box", function(){
154 jQuery("#media-success").removeClass("active");
155 });
156 });
157 </script>
158 <?php }
159 }
160
161 public function change_file_name_box($post) { ?>
162 <p class="upgrade-bottom">
163 <label for="change_file_name"><input disabled type="checkbox" id="change_file_name" name="premio_change_file_name" value="yes"> <?php esc_html_e("Change file name according to title", "folders") ?></label>
164 </p>
165 <div class="upgrade-box">
166 <a href="<?php echo esc_url($this->upgradeLink) ?>" target="_blank"><?php esc_html_e("Upgrade to Pro", "folders"); ?></a>
167 </div>
168 <?php
169 }
170
171 public function replace_media_file_script() {
172 wp_enqueue_script('folders-replace-media', WCP_FOLDER_URL . 'assets/js/replace-file-name.js', array('jquery'), WCP_FOLDER_VERSION, true);
173 wp_localize_script('folders-replace-media', 'replace_media_options', array(
174 'ajax_url' => admin_url("admin-ajax.php"),
175 ));
176 }
177
178 public function premio_replace_file_CSS() {
179 echo '<style>
180 .compat-field-replace_file_name th.label {display: none;}
181 .compat-field-replace_file_name td.field {width: 100%; border-top: solid 1px #c0c0c0; padding:10px 0 0 0;margin: 0;float: none;}
182 .compat-field-replace_file_name td.field label {width: 100%; display: block;padding:0 0 10px 0;}
183 .compat-field-replace_file_name td.field label input[type="checkbox"] {margin: 0 4px 0 2px;}
184 .compat-field-replace_file_name td.field a.update-name-with-title {display: none;}
185 .compat-field-replace_file_name td.field a.update-name-with-title.show {display: inline-block;}
186
187 .compat-field-folders th.label {width: 100%; text-align: left; padding: 0 0 10px 0; margin: 0; border-top: solid 1px #c0c0c0;float: none;}
188 .compat-field-folders th.label .alignleft {float: none; text-align: left; font-weight: bold;}
189 .compat-field-folders th.label br {display: none;}
190 .compat-field-folders td.field {width: 100%; padding: 0; margin: 0;float: none;}
191 .folders-undo-notification{position:fixed;right:-500px;bottom:25px;width:280px;background:#fff;padding:15px;-webkit-box-shadow:0 3px 6px -4px rgb(0 0 0 / 12%),0 6px 16px 0 rgb(0 0 0 / 8%),0 9px 28px 8px rgb(0 0 0 / 5%);box-shadow:0 3px 6px -4px rgb(0 0 0 / 12%),0 6px 16px 0 rgb(0 0 0 / 8%),0 9px 28px 8px rgb(0 0 0 / 5%);transition:all .25s linear;z-index:250010}.folders-undo-notification.active{right:25px}.folders-undo-header{font-weight:500;font-size:14px;padding:0 0 3px 0}.folders-undo-body{font-size:13px;padding:0 0 5px 0}.folders-undo-footer{text-align:right;padding:5px 0 0 0}.folders-undo-footer .undo-button{background:#1da1f4;border:none;color:#fff;padding:3px 10px;font-size:12px;border-radius:2px;cursor:pointer}.folders-undo-body{position:relative}.close-undo-box{position:absolute;right:-10px;top:0;width:16px;height:16px;transition:all .25s linear}.close-undo-box:hover{transform:rotate(180deg)}.close-undo-box span{display:block;position:relative;width:16px;height:16px;transition:all .2s linear}.close-undo-box span:after,.close-undo-box span:before{content:"";position:absolute;width:12px;height:2px;background-color:#333;display:block;border-radius:2px;transform:rotate(45deg);top:7px;left:2px}.close-undo-box span:after{transform:rotate(-45deg)}
192 .folders-undo-notification.no .folders-undo-header { color: #dd0000; }
193 .folders-undo-notification.yes .folders-undo-header { color: #014737; }
194 .update-name-with-title .spinner {display: none; visibility: visible; margin-right: 0;}
195 .update-name-with-title.in-progress .spinner {display: inline-block;}
196
197 #folders-replace-file-name .inside {position: relative;padding:0;margin:0}
198 #folders-replace-file-name .inside p {padding: 1em; margin: 0;}
199 #folders-replace-file-name .upgrade-box {position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.3); z-index: 1;display: none;}
200 #folders-replace-file-name:hover .upgrade-box { display: block; }
201 #folders-replace-file-name:hover p {filter: blur(1.2px);}
202 #folders-replace-file-name:hover .upgrade-box a {display: inline-block; position: absolute; left: 0; right: 0; width: 100px; margin: 0 auto; top: 50%; padding: 5px 10px; text-decoration: none; background: #fa166b; color: #fff; border-radius: 4px; text-align: center; margin-top: -14px;}
203 </style>';
204 }
205
206 public function attachment_replace_name_with_title($form_fields, $post)
207 {
208 $screen = null;
209 if (function_exists('get_current_screen'))
210 {
211 $screen = get_current_screen();
212
213 if(! is_null($screen) && $screen->id == 'attachment') // hide on edit attachment screen.
214 return $form_fields;
215 }
216
217 $form_fields["replace_file_name"] = array(
218 "label" => esc_html__("Replace media", "folders"),
219 "input" => "html",
220 "html" => "<label for='attachment_title_{$post->ID}' data-post='{$post->ID}' data-nonce='".wp_create_nonce('change_attachment_title_'.$post->ID)."'><input id='attachment_title_{$post->ID}' type='checkbox' class='folder-replace-checkbox' value='{$post->ID}'>".esc_html__("Update file name with title")."</label><a href='".$this->upgradeLink."' target='_blank' style='background: {$this->button_color}; border-color: {$this->button_color}; color:#ffffff' type='button' class='button update-name-with-title' >".esc_html__("Upgrade to Pro", "folders")."</a>",
221 "helps" => ""
222 );
223
224 return $form_fields;
225 }
226
227 public function folders_admin_css_and_js($page) {
228 if($page == "media_page_folders-replace-media" || $page == "admin_page_folders-replace-media") {
229 wp_enqueue_style('folders-media', plugin_dir_url(dirname(__FILE__)) . 'assets/css/replace-media.css', array(), WCP_FOLDER_VERSION);
230 wp_enqueue_script('folders-media', plugin_dir_url(dirname(__FILE__)) . 'assets/js/replace-media.js', array(), WCP_FOLDER_VERSION);
231 }
232 }
233
234 public function admin_menu() {
235 add_submenu_page(null,
236 esc_html__("Replace media", "folders"),
237 esc_html__("Replace media", "folders"),
238 'upload_files',
239 'folders-replace-media',
240 array($this, 'folders_replace_media')
241 );
242 }
243
244 public function folders_replace_media() {
245 global $plugin_page;
246 $action = isset($_GET['action']) ? sanitize_text_field($_GET['action']) : '';
247 $attachment_id = isset($_GET['attachment_id']) ? sanitize_text_field($_GET['attachment_id']) : '';
248 $nonce = isset($_GET['nonce']) ? sanitize_text_field($_GET['nonce']) : '';
249 if (!wp_verify_nonce($nonce, "folders-replace-media-".$attachment_id)) {
250 echo 'Invalid Nonce';
251 exit;
252 }
253 $attachment = get_post($attachment_id);
254 if(empty($attachment) || !isset($attachment->guid)) {
255 echo 'Invalid URL';
256 exit;
257 }
258 $guid = $attachment->guid;
259 $guid = explode(".", $guid);
260 if($guid == $attachment->guid) {
261 echo 'Invalid URL';
262 exit;
263 }
264 $form_action = $this->getMediaReplaceURL($attachment_id);
265 include_once dirname(dirname(__FILE__)) . WCP_DS . "/templates" . WCP_DS . "admin" . WCP_DS . "media-replace.php";
266 }
267
268 public function add_media_action($actions, $post) {
269 if(!$this->is_enabled) {
270 return array_merge($actions);
271 }
272 if (wp_attachment_is('image', $post->ID)) {
273 $link = $this->getMediaReplaceURL( $post->ID );
274
275 $newaction['replace_media'] = '<a style="color: ' . $this->button_color . '" href="' . $link . '" rel="permalink">' . esc_html__( "Replace media", "folders" ) . '</a>';
276
277 return array_merge( $actions, $newaction );
278 } else {
279 $link = $this->getMediaReplaceURL( $post->ID );
280
281 $newaction['replace_media'] = '<a style="color: ' . $this->button_color . '" target="_blank" href="' . $this->upgradeLink . '" rel="permalink">' . esc_html__( "Replace Media 🔑", "folders" ) . '</a>';
282
283 return array_merge( $actions, $newaction );
284 }
285 return $actions;
286 }
287
288 public function getMediaReplaceURL($attach_id) {
289 $url = admin_url( "upload.php");
290 $url = add_query_arg(array(
291 'page' => 'folders-replace-media',
292 'action' => 'folders_replace_media',
293 'attachment_id' => $attach_id,
294 'nonce' => wp_create_nonce("folders-replace-media-".$attach_id)
295 ), $url);
296
297 return $url;
298 }
299
300 public function replace_meta_box($post) {
301 if (wp_attachment_is('image', $post->ID)) {
302 $link = $this->getMediaReplaceURL($post->ID);
303 echo "<p><a style='background: {$this->button_color}; border-color: {$this->button_color}; color:#ffffff' href='" . $link . "' class='button-secondary'>" . esc_html__( "Upload a new file", "folders" ) . "</a></p><p>" . esc_html__( "Click on the button to replace the file with another file", "folders" ) . "</p>";
304 } else {
305 echo "<p><a style='color: {$this->button_color}; font-weight: 500' target='_blank' href='" . $this->upgradeLink . "' >" . esc_html__( "Upgrade to Pro", "folders" ) . "</a> ".esc_html__( "to replace any kind of files while uploading including pdf/svg/docx/etc & more.", "folders" ) . "</p>";
306 }
307 }
308
309 public function attachment_editor($form_fields, $post)
310 {
311 $screen = null;
312 if (function_exists('get_current_screen'))
313 {
314 $screen = get_current_screen();
315
316 if(! is_null($screen) && $screen->id == 'attachment') // hide on edit attachment screen.
317 return $form_fields;
318 }
319 if (wp_attachment_is('image', $post->ID)) {
320 $link = $this->getMediaReplaceURL( $post->ID );
321 $form_fields["folders"] = array(
322 "label" => esc_html__( "Replace media", "folders" ),
323 "input" => "html",
324 "html" => "<a style='background: {$this->button_color}; border-color: {$this->button_color}; color:#ffffff' href='" . $link . "' class='button-secondary'>" . esc_html__( "Upload a new file", "folders" ) . "</a>",
325 "helps" => esc_html__( "Click on the button to replace the file with another file", "folders" )
326 );
327 } else {
328 $form_fields["folders"] = array(
329 "label" => esc_html__( "Replace media", "folders" ),
330 "input" => "html",
331 "html" => "<div style='border: solid 1px #c0c0c0; padding: 10px; border-radius: 2px; background: #ececec;'><a style='color: {$this->button_color}; font-weight: 500' target='_blank' href='" . $this->upgradeLink . "' >" . esc_html__( "Upgrade to Pro", "folders" ) . "</a> ".esc_html__( "to replace media files other than images", "folders" ) . "</div>",
332 "helps" => esc_html__( "Click on the button to replace the file with another file", "folders" )
333 );
334 }
335
336 return $form_fields;
337 }
338
339 public function getFileSize($attachment_id) {
340 $size = filesize( get_attached_file( $attachment_id ));
341 if($size > 1000000) {
342 $size = ($size/1000000);
343 return number_format((float)$size, 2, ".", ",")." MB";
344 } else if($size > 1000) {
345 $size = ($size/1000);
346 return number_format((float)$size, 2, ".", ",")." KB";
347 }
348 return $size." B";
349 }
350
351 public $old_file_ext;
352 public $old_file_path;
353 public $old_file_url;
354 public $new_file_path;
355 public $new_file_url;
356
357 public $new_file_name;
358
359 public $current_upload_data;
360
361 public $mode = "rename-file";
362
363 public $old_image_meta;
364 public $new_image_meta;
365 public $upload_dir;
366
367 public $is_old_image = 0;
368 public $is_new_image = 0;
369 public $attachment_id;
370
371 public function handle_folders_file_upload() {
372 global $wpdb;
373 if(isset($_FILES['new_media_file'])) {
374 if($_FILES['new_media_file']['error'] == 0) {
375 $attachment_id = isset($_GET['attachment_id']) ? sanitize_text_field($_GET['attachment_id']) : '';
376 $nonce = isset($_GET['nonce']) ? sanitize_text_field($_GET['nonce']) : '';
377 if (!wp_verify_nonce($nonce, "folders-replace-media-" . $attachment_id)) {
378 return;
379 }
380 $attachment = get_post($attachment_id);
381 if (empty($attachment) || !isset($attachment->guid)) {
382 return;
383 }
384 $attachment_url = $attachment->guid;
385 $url = wp_get_attachment_url($attachment_id);
386 if(!empty($url)) {
387 $attachment_url = $url;
388 }
389 $guid = explode(".", $attachment_url);
390 $guid = array_pop($guid);
391 if ($guid == $attachment->guid) {
392 return;
393 }
394
395 $this->attachment_id = $attachment_id;
396
397 $file = $_FILES['new_media_file'];
398 $file_name = $file['name'];
399 $file_ext = explode(".", $file_name);
400 $file_ext = array_pop($file_ext);
401
402 if ($guid == $file_ext) {
403 $this->mode = "replace-file";
404 }
405
406 if (wp_attachment_is('image', $attachment_id)) {
407 $this->is_old_image = 1;
408 }
409 $this->old_file_url = $attachment_url;
410
411 $new_file = $file['tmp_name'];
412
413 $file_parts = pathinfo($attachment_url);
414
415 $upload_dir = wp_upload_dir($attachment->post_date_gmt);
416 $this->current_upload_data = $upload_dir;
417 $upload_path = $upload_dir['path'];
418
419 $this->upload_dir = $upload_dir;
420
421 $this->old_file_path = $upload_dir['path'] . "/" . $file_parts['basename'];
422
423 $this->old_image_meta = wp_get_attachment_metadata($attachment_id);
424 if (!is_dir($upload_path)) {
425 mkdir($upload_path, 755, true);
426 }
427 if (is_dir($upload_path)) {
428 $file_name = $this->checkForFileName($file['name'], $upload_path.DIRECTORY_SEPARATOR);
429 $upload_file = $upload_path . DIRECTORY_SEPARATOR . $file_name;
430 $status = move_uploaded_file($new_file, $upload_file);
431
432 $this->new_file_path = $upload_dir['path'] . "/" . $file_name;
433
434 $this->new_file_url = $upload_dir['url']."/".$file_name;
435
436 if ($status) {
437
438 if(file_exists($this->upload_dir['path'].DIRECTORY_SEPARATOR.$file_parts['basename'])) {
439 @unlink($this->upload_dir['path'].DIRECTORY_SEPARATOR.$file_parts['basename']);
440 }
441
442 update_attached_file($attachment->ID, $this->new_file_path);
443
444 $update_array = array();
445 $update_array['ID'] = $attachment->ID;
446 $update_array['post_title'] = $file_parts['filename'];
447 $update_array['post_name'] = sanitize_title($file_parts['filename']);
448 $update_array['guid'] = $this->new_file_path; //wp_get_attachment_url($this->post_id);
449 $update_array['post_mime_type'] = $file['type'];
450 $post_id = \wp_update_post($update_array, true);
451
452 // update post doesn't update GUID on updates.
453 $wpdb->update($wpdb->posts, array('guid' => $this->new_file_path), array('ID' => $attachment->ID));
454
455 $this->removeThumbImages();
456
457 $metadata = wp_generate_attachment_metadata($attachment->ID, $this->new_file_path);
458 wp_update_attachment_metadata($attachment->ID, $metadata);
459
460 $this->new_image_meta = wp_get_attachment_metadata($attachment_id);
461
462 $this->searchAndReplace();
463
464 wp_redirect(admin_url("post.php?post=" . $attachment_id . "&action=edit&premio_message=success"));
465 exit;
466 } else {
467 wp_die("Error during uploading file");
468 }
469
470 } else {
471 wp_die("Permission issue, Unable to create directory");
472 }
473 }
474 }
475 }
476
477 public function checkForFileName($fileName, $filePath, $postFix = 0) {
478 $new_file_name = $fileName;
479 if(!empty($postFix)) {
480 $file_array = explode(".", $fileName);
481 $file_ext = array_pop($file_array);
482 $new_file_name = implode(".", $file_array)."-".$postFix.".".$file_ext;
483 }
484 if(!file_exists($filePath.$new_file_name)) {
485 return $new_file_name;
486 }
487 return $this->checkForFileName($fileName, $filePath, ($postFix+1));
488 }
489
490 public $replace_items = array();
491
492 public function removeThumbImages() {
493 if(!empty($this->old_image_meta) && isset($this->old_image_meta['sizes']) && !empty($this->upload_dir) && isset($this->upload_dir['path'])) {
494 $path = $this->upload_dir['path'].DIRECTORY_SEPARATOR;
495 foreach ($this->old_image_meta['sizes'] as $image) {
496 if(file_exists($path.$image['file'])) {
497 @unlink($path . $image['file']);
498 }
499 }
500 }
501 }
502
503 public function searchAndReplace() {
504 if (wp_attachment_is('image', $this->attachment_id)) {
505 $this->is_new_image = 1;
506 }
507 if($this->old_file_url != $this->new_file_url) {
508 $replace = array(
509 'search' => $this->old_file_url,
510 'replace' => $this->new_file_url,
511 );
512 $this->replace_items[] = $replace;
513 }
514
515 $base_url = $this->upload_dir['url'];
516 $base_url = trim($base_url, "/")."/";
517 $new_url = $this->new_file_url;
518
519 if(isset($this->old_image_meta['sizes']) && !empty($this->old_image_meta['sizes'])) {
520 if(!isset($this->new_image_meta['sizes']) || empty($this->new_image_meta['sizes'])) {
521 foreach ($this->old_image_meta['sizes'] as $key=>$image) {
522 $replace = array(
523 'search' => $base_url.$image['file'],
524 'replace' => $new_url,
525 );
526 $this->replace_items[] = $replace;
527 }
528 } else if(isset($this->new_image_meta['sizes']) && !empty($this->new_image_meta['sizes'])) {
529 $new_size = $this->new_image_meta['sizes'];
530 foreach ($this->old_image_meta['sizes'] as $key=>$image) {
531 $new_replace_url = $new_url;
532 if(isset($new_size[$key])) {
533 $new_replace_url = $base_url.$new_size[$key]['file'];
534 }
535 $replace = array(
536 'search' => $base_url.$image['file'],
537 'replace' => $new_replace_url,
538 );
539 $this->replace_items[] = $replace;
540 }
541 }
542 }
543
544 if(!empty($this->replace_items)) {
545 $replace_items = array();
546 foreach($this->replace_items as $args) {
547 if($args['search'] != $args['replace']) {
548 $replace_items[] = $args;
549 }
550 }
551 $this->replace_items = $replace_items;
552 $this->replaceURL();
553 }
554 }
555
556 function replaceURL() {
557 /* check in post content */
558 $this->checkInPostContent();
559
560 /* check in options */
561 $this->checkInOptions();
562
563 /* check in meta */
564 $this->checkInMetaData();
565
566 if(function_exists('folders_clear_all_caches')) {
567 folders_clear_all_caches();
568 }
569 }
570
571 function checkInPostContent() {
572 global $wpdb;
573 $post_table = $wpdb->prefix."posts";
574 if(!empty($this->replace_items)) {
575 $query = "SELECT ID, post_content FROM {$post_table} WHERE post_content LIKE %s";
576 $update_query = "UPDATE {$post_table} SET post_content = %s WHERE ID = %d";
577 foreach ($this->replace_items as $args) {
578 if($args['search'] != $args['replace']) {
579 $sql_query = $wpdb->prepare($query, "%".$args['search']."%");
580 $results = $wpdb->get_results($sql_query, ARRAY_A );
581 if(!empty($results)) {
582 foreach ($results AS $row) {
583 $content = $this->findAndReplaceContent($row['post_content'], $args['search'], $args['replace']);
584 $update_post_query = $wpdb->prepare($update_query, $content, $row['ID']);
585 $result = $wpdb->query($update_post_query);
586 }
587 }
588 }
589 }
590 }
591 }
592
593 function checkInOptions() {
594 global $wpdb;
595 $post_table = $wpdb->prefix."options";
596 if(!empty($this->replace_items)) {
597 $query = "SELECT option_id, option_value FROM {$post_table} WHERE option_value LIKE %s";
598 $update_query = "UPDATE {$post_table} SET option_value = %s WHERE option_id = %d";
599 foreach ($this->replace_items as $args) {
600 if($args['search'] != $args['replace']) {
601 $sql_query = $wpdb->prepare($query, "%".$args['search']."%");
602 $results = $wpdb->get_results($sql_query, ARRAY_A );
603 if(!empty($results)) {
604 foreach ($results AS $row) {
605 $content = $this->findAndReplaceContent($row['post_content'], $args['search'], $args['replace']);
606 $update_post_query = $wpdb->prepare($update_query, $content, $row['ID']);
607 $result = $wpdb->query($update_post_query);
608 }
609 }
610 }
611 }
612 }
613 }
614
615 function checkInMetaData() {
616 $tables = array(
617 array(
618 'table_name' => 'usermeta',
619 'primary_key' => 'umeta_id',
620 'search_key' => 'meta_value'
621 ),
622 array(
623 'table_name' => 'termmeta',
624 'primary_key' => 'meta_id',
625 'search_key' => 'meta_value'
626 ),
627 array(
628 'table_name' => 'postmeta',
629 'primary_key' => 'meta_id',
630 'search_key' => 'meta_value'
631 ),
632 array(
633 'table_name' => 'commentmeta',
634 'primary_key' => 'meta_id',
635 'search_key' => 'meta_value'
636 )
637 );
638 global $wpdb;
639 foreach ($tables as $table) {
640 $post_table = $wpdb->prefix . $table['table_name'];
641 if (!empty($this->replace_items)) {
642 $query = "SELECT {$table['primary_key']}, {$table['search_key']} FROM {$post_table} WHERE {$table['search_key']} LIKE %s";
643 $update_query = "UPDATE {$post_table} SET {$table['search_key']} = %s WHERE {$table['primary_key']} = %d";
644 foreach ($this->replace_items as $args) {
645 if ($args['search'] != $args['replace']) {
646 $sql_query = $wpdb->prepare($query, "%" . $args['search'] . "%");
647 $results = $wpdb->get_results($sql_query, ARRAY_A);
648 if (!empty($results)) {
649 foreach ($results as $row) {
650 $content = $this->findAndReplaceContent($row[$table['search_key']], $args['search'], $args['replace']);
651 $update_post_query = $wpdb->prepare($update_query, $content, $row[$table['primary_key']]);
652 $result = $wpdb->query($update_post_query);
653 }
654 }
655 }
656 }
657 }
658 }
659 }
660
661 function findAndReplaceContent($content, $search, $replace, $in_deep = false) {
662 $content = maybe_unserialize($content);
663 $isJson = $this->isJSON($content);
664
665 if ($isJson) {
666 $content = json_decode($content);
667 }
668
669 if (is_string($content)) {
670 $content = str_replace($search, $replace, $content);
671 }
672 else if(is_wp_error($content)) {
673
674 }
675 else if(is_array($content)) {
676 foreach($content as $index => $value) {
677 $content[$index] = $this->findAndReplaceContent($value, $search, $replace, true);
678 if (is_string($index)) {
679 $index_replaced = $this->findAndReplaceContent($index, $search, $replace, true);
680 if ($index_replaced !== $index)
681 $content = $this->changeArrayKey($content, array($index => $index_replaced));
682 }
683 }
684 }
685 else if(is_object($content)) {
686 foreach($content as $key => $value) {
687 $content->{$key} = $this->findAndReplaceContent($value, $search, $replace, true);
688 }
689 }
690
691 if ($isJson && $in_deep === false) {
692 $content = json_encode($content, JSON_UNESCAPED_SLASHES);
693 }
694 else if($in_deep === false && (is_array($content) || is_object($content))) {
695 $content = maybe_serialize($content);
696 }
697
698 return $content;
699 }
700
701 function changeArrayKey($array, $set) {
702 if (is_array($array) && is_array($set)) {
703 $newArr = array();
704 foreach ($array as $k => $v) {
705 $key = array_key_exists( $k, $set) ? $set[$k] : $k;
706 $newArr[$key] = is_array($v) ? $this->changeArrayKey($v, $set) : $v;
707 }
708 return $newArr;
709 }
710 return $array;
711 }
712
713 function isJSON($content)
714 {
715 if (is_array($content) || is_object($content))
716 return false;
717
718 $json = json_decode($content);
719 return $json && $json != $content;
720 }
721 }
722 $folders_replace_media = new folders_replace_media();