PluginProbe ʕ •ᴥ•ʔ
Folders – Unlimited Folders to Organize Media Library Folder, Pages, Posts, File Manager / 2.7.2
Folders – Unlimited Folders to Organize Media Library Folder, Pages, Posts, File Manager v2.7.2
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 4 years ago form.class.php 5 years ago media.replace.php 4 years ago plugin.updates.php 5 years ago plugins.class.php 5 years ago tree.class.php 5 years ago
media.replace.php
733 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
430 $db_file_name = $file_parts['basename'];
431 $db_file_array = explode(".", $db_file_name);
432 array_pop($db_file_array);
433 $db_file_name = implode(".", $db_file_array).".";
434 $db_file_name .= $file_ext;
435
436 $file_name = $db_file_name;
437
438 $upload_file = $upload_path . DIRECTORY_SEPARATOR . $file_name;
439 $status = move_uploaded_file($new_file, $upload_file);
440
441 $this->new_file_path = trim($upload_dir['path'], "/") . "/" . $file_name;
442
443 $this->new_file_url = trim($upload_dir['url'], "/")."/".$file_name;
444
445 if ($status) {
446
447 // if(file_exists($this->upload_dir['path'].DIRECTORY_SEPARATOR.$file_parts['basename'])) {
448 // @unlink($this->upload_dir['path'].DIRECTORY_SEPARATOR.$file_parts['basename']);
449 // }
450
451 $this->removeThumbImages();
452
453 update_attached_file($attachment->ID, $this->new_file_path);
454
455 $update_array = array();
456 $update_array['ID'] = $attachment->ID;
457 $update_array['post_title'] = $file_parts['filename'];
458 $update_array['post_name'] = sanitize_title($file_parts['filename']);
459 $update_array['guid'] = $this->new_file_path; //wp_get_attachment_url($this->post_id);
460 $update_array['post_mime_type'] = $file['type'];
461 $post_id = \wp_update_post($update_array, true);
462
463 // update post doesn't update GUID on updates.
464 $wpdb->update($wpdb->posts, array('guid' => $this->new_file_path), array('ID' => $attachment->ID));
465
466 $metadata = wp_generate_attachment_metadata($attachment->ID, $this->new_file_path);
467 wp_update_attachment_metadata($attachment->ID, $metadata);
468
469 $this->new_image_meta = wp_get_attachment_metadata($attachment_id);
470
471 $this->searchAndReplace();
472
473 wp_redirect(admin_url("post.php?post=" . $attachment_id . "&action=edit&premio_message=success"));
474
475
476 exit;
477 } else {
478 wp_die("Error during uploading file");
479 }
480
481 } else {
482 wp_die("Permission issue, Unable to create directory");
483 }
484 }
485 }
486 }
487
488 public function checkForFileName($fileName, $filePath, $postFix = 0) {
489 $new_file_name = $fileName;
490 if(!empty($postFix)) {
491 $file_array = explode(".", $fileName);
492 $file_ext = array_pop($file_array);
493 $new_file_name = implode(".", $file_array)."-".$postFix.".".$file_ext;
494 }
495 if(!file_exists($filePath.$new_file_name)) {
496 return $new_file_name;
497 }
498 return $this->checkForFileName($fileName, $filePath, ($postFix+1));
499 }
500
501 public $replace_items = array();
502
503 public function removeThumbImages() {
504 if(!empty($this->old_image_meta) && isset($this->old_image_meta['sizes']) && !empty($this->upload_dir) && isset($this->upload_dir['path'])) {
505 $path = $this->upload_dir['path'].DIRECTORY_SEPARATOR;
506 foreach ($this->old_image_meta['sizes'] as $image) {
507 if(file_exists($path.$image['file'])) {
508 @unlink($path . $image['file']);
509 }
510 }
511 }
512 }
513
514 public function searchAndReplace() {
515 if (wp_attachment_is('image', $this->attachment_id)) {
516 $this->is_new_image = 1;
517 }
518 if($this->old_file_url != $this->new_file_url) {
519 $replace = array(
520 'search' => $this->old_file_url,
521 'replace' => $this->new_file_url,
522 );
523 $this->replace_items[] = $replace;
524 }
525
526 $base_url = $this->upload_dir['url'];
527 $base_url = trim($base_url, "/")."/";
528 $new_url = $this->new_file_url;
529
530 if(isset($this->old_image_meta['sizes']) && !empty($this->old_image_meta['sizes'])) {
531 if(!isset($this->new_image_meta['sizes']) || empty($this->new_image_meta['sizes'])) {
532 foreach ($this->old_image_meta['sizes'] as $key=>$image) {
533 $replace = array(
534 'search' => $base_url.$image['file'],
535 'replace' => $new_url,
536 );
537 $this->replace_items[] = $replace;
538 }
539 } else if(isset($this->new_image_meta['sizes']) && !empty($this->new_image_meta['sizes'])) {
540 $new_size = $this->new_image_meta['sizes'];
541 foreach ($this->old_image_meta['sizes'] as $key=>$image) {
542 $new_replace_url = $new_url;
543 if(isset($new_size[$key])) {
544 $new_replace_url = $base_url.$new_size[$key]['file'];
545 }
546 $replace = array(
547 'search' => $base_url.$image['file'],
548 'replace' => $new_replace_url,
549 );
550 $this->replace_items[] = $replace;
551 }
552 }
553 }
554
555 if(!empty($this->replace_items)) {
556 $replace_items = array();
557 foreach($this->replace_items as $args) {
558 if($args['search'] != $args['replace']) {
559 $replace_items[] = $args;
560 }
561 }
562 $this->replace_items = $replace_items;
563 $this->replaceURL();
564 }
565 }
566
567 function replaceURL() {
568 /* check in post content */
569 $this->checkInPostContent();
570
571 /* check in options */
572 $this->checkInOptions();
573
574 /* check in meta */
575 $this->checkInMetaData();
576
577 if(function_exists('folders_clear_all_caches')) {
578 folders_clear_all_caches();
579 }
580 }
581
582 function checkInPostContent() {
583 global $wpdb;
584 $post_table = $wpdb->prefix."posts";
585 if(!empty($this->replace_items)) {
586 $query = "SELECT ID, post_content FROM {$post_table} WHERE post_content LIKE %s";
587 $update_query = "UPDATE {$post_table} SET post_content = %s WHERE ID = %d";
588 foreach ($this->replace_items as $args) {
589 if($args['search'] != $args['replace']) {
590 $sql_query = $wpdb->prepare($query, "%".$args['search']."%");
591 $results = $wpdb->get_results($sql_query, ARRAY_A );
592 if(!empty($results)) {
593 foreach ($results AS $row) {
594 $content = $this->findAndReplaceContent($row['post_content'], $args['search'], $args['replace']);
595 $update_post_query = $wpdb->prepare($update_query, $content, $row['ID']);
596 $result = $wpdb->query($update_post_query);
597 }
598 }
599 }
600 }
601 }
602 }
603
604 function checkInOptions() {
605 global $wpdb;
606 $post_table = $wpdb->prefix."options";
607 if(!empty($this->replace_items)) {
608 $query = "SELECT option_id, option_value FROM {$post_table} WHERE option_value LIKE %s";
609 $update_query = "UPDATE {$post_table} SET option_value = %s WHERE option_id = %d";
610 foreach ($this->replace_items as $args) {
611 if($args['search'] != $args['replace']) {
612 $sql_query = $wpdb->prepare($query, "%".$args['search']."%");
613 $results = $wpdb->get_results($sql_query, ARRAY_A );
614 if(!empty($results)) {
615 foreach ($results AS $row) {
616 $content = $this->findAndReplaceContent($row['post_content'], $args['search'], $args['replace']);
617 $update_post_query = $wpdb->prepare($update_query, $content, $row['ID']);
618 $result = $wpdb->query($update_post_query);
619 }
620 }
621 }
622 }
623 }
624 }
625
626 function checkInMetaData() {
627 $tables = array(
628 array(
629 'table_name' => 'usermeta',
630 'primary_key' => 'umeta_id',
631 'search_key' => 'meta_value'
632 ),
633 array(
634 'table_name' => 'termmeta',
635 'primary_key' => 'meta_id',
636 'search_key' => 'meta_value'
637 ),
638 array(
639 'table_name' => 'postmeta',
640 'primary_key' => 'meta_id',
641 'search_key' => 'meta_value'
642 ),
643 array(
644 'table_name' => 'commentmeta',
645 'primary_key' => 'meta_id',
646 'search_key' => 'meta_value'
647 )
648 );
649 global $wpdb;
650 foreach ($tables as $table) {
651 $post_table = $wpdb->prefix . $table['table_name'];
652 if (!empty($this->replace_items)) {
653 $query = "SELECT {$table['primary_key']}, {$table['search_key']} FROM {$post_table} WHERE {$table['search_key']} LIKE %s";
654 $update_query = "UPDATE {$post_table} SET {$table['search_key']} = %s WHERE {$table['primary_key']} = %d";
655 foreach ($this->replace_items as $args) {
656 if ($args['search'] != $args['replace']) {
657 $sql_query = $wpdb->prepare($query, "%" . $args['search'] . "%");
658 $results = $wpdb->get_results($sql_query, ARRAY_A);
659 if (!empty($results)) {
660 foreach ($results as $row) {
661 $content = $this->findAndReplaceContent($row[$table['search_key']], $args['search'], $args['replace']);
662 $update_post_query = $wpdb->prepare($update_query, $content, $row[$table['primary_key']]);
663 $result = $wpdb->query($update_post_query);
664 }
665 }
666 }
667 }
668 }
669 }
670 }
671
672 function findAndReplaceContent($content, $search, $replace, $in_deep = false) {
673 $content = maybe_unserialize($content);
674 $isJson = $this->isJSON($content);
675
676 if ($isJson) {
677 $content = json_decode($content);
678 }
679
680 if (is_string($content)) {
681 $content = str_replace($search, $replace, $content);
682 }
683 else if(is_wp_error($content)) {
684
685 }
686 else if(is_array($content)) {
687 foreach($content as $index => $value) {
688 $content[$index] = $this->findAndReplaceContent($value, $search, $replace, true);
689 if (is_string($index)) {
690 $index_replaced = $this->findAndReplaceContent($index, $search, $replace, true);
691 if ($index_replaced !== $index)
692 $content = $this->changeArrayKey($content, array($index => $index_replaced));
693 }
694 }
695 }
696 else if(is_object($content)) {
697 foreach($content as $key => $value) {
698 $content->{$key} = $this->findAndReplaceContent($value, $search, $replace, true);
699 }
700 }
701
702 if ($isJson && $in_deep === false) {
703 $content = json_encode($content, JSON_UNESCAPED_SLASHES);
704 }
705 else if($in_deep === false && (is_array($content) || is_object($content))) {
706 $content = maybe_serialize($content);
707 }
708
709 return $content;
710 }
711
712 function changeArrayKey($array, $set) {
713 if (is_array($array) && is_array($set)) {
714 $newArr = array();
715 foreach ($array as $k => $v) {
716 $key = array_key_exists( $k, $set) ? $set[$k] : $k;
717 $newArr[$key] = is_array($v) ? $this->changeArrayKey($v, $set) : $v;
718 }
719 return $newArr;
720 }
721 return $array;
722 }
723
724 function isJSON($content)
725 {
726 if (is_array($content) || is_object($content))
727 return false;
728
729 $json = json_decode($content);
730 return $json && $json != $content;
731 }
732 }
733 $folders_replace_media = new folders_replace_media();