PluginProbe ʕ •ᴥ•ʔ
Folders – Unlimited Folders to Organize Media Library Folder, Pages, Posts, File Manager / 2.8.2
Folders – Unlimited Folders to Organize Media Library Folder, Pages, Posts, File Manager v2.8.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 3 years ago class-polylang.php 3 years ago class-review-box.php 3 years ago class-upgrade-box.php 3 years ago class-wpml.php 3 years ago folders.class.php 3 years ago form.class.php 3 years ago media.replace.php 3 years ago plugin.updates.php 3 years ago plugins.class.php 3 years ago tree.class.php 3 years ago
media.replace.php
795 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 $replacement_option = isset($_REQUEST['replacement_option'])?$_REQUEST['replacement_option']:"replace_only_file";
396
397 $this->attachment_id = $attachment_id;
398
399 $file = $_FILES['new_media_file'];
400 $file_name = $file['name'];
401 $file_ext = explode(".", $file_name);
402 $file_ext = array_pop($file_ext);
403
404 if (wp_attachment_is('image', $attachment_id)) {
405 $this->is_old_image = 1;
406 }
407 $this->old_file_url = $attachment_url;
408 $this->old_image_meta = wp_get_attachment_metadata($attachment_id);
409
410 $new_file = $file['tmp_name'];
411
412 $file_parts = pathinfo($attachment_url);
413
414 $db_file_name = $file_parts['basename'];
415 $db_file_array = explode(".", $db_file_name);
416 array_pop($db_file_array);
417 $db_file_name = implode(".", $db_file_array).".";
418 $db_file_name .= $file_ext;
419
420 $wp_upload_path = wp_get_upload_dir();
421
422 $base_path = $old_path = $wp_upload_path['basedir'].DIRECTORY_SEPARATOR;
423 $baseurl = $old_url = $wp_upload_path['baseurl']."/";
424
425 $post_upload = "";
426
427 $wp_attached_file = get_post_meta($attachment_id, "_wp_attached_file", true);
428 if($wp_attached_file !== false) {
429 $old_file_name = explode("/", $wp_attached_file);
430 array_pop($old_file_name);
431
432 if(count($old_file_name) > 0) {
433 $old_path .= implode(DIRECTORY_SEPARATOR, $old_file_name);
434 $old_url .= implode("/", $old_file_name);
435 }
436
437 if($replacement_option == "replace_file_with_name" && isset($_REQUEST['new_folder_option']) && $_REQUEST['new_folder_option'] && isset($_REQUEST['new_folder_path']) && !empty($_REQUEST['new_folder_path'])) {
438 $baseurl .= $_REQUEST['new_folder_path'];
439 $base_path .= str_replace("/", DIRECTORY_SEPARATOR, $_REQUEST['new_folder_path']);
440
441 $post_upload = $_REQUEST['new_folder_path'];
442 } else if(count($old_file_name) > 0) {
443 $baseurl .= implode(DIRECTORY_SEPARATOR, $old_file_name);
444 $base_path .= implode("/", $old_file_name);
445
446 $post_upload = implode("/", $old_file_name);
447 }
448 }
449
450 $upload_dir = array();
451 $upload_dir['path'] = $base_path;
452 $upload_dir['old_path'] = $old_path;
453 $upload_dir['url'] = $baseurl;
454 $upload_dir['old_url'] = $old_url;
455
456 $this->upload_dir = $upload_dir;
457
458 $this->old_file_path = $old_path . "/" . $file_parts['basename'];
459
460 if (!is_dir($base_path)) {
461 mkdir($base_path, 755, true);
462 }
463
464 if (is_dir($base_path)) {
465
466 $file_array = explode(".", $file['name']);
467 $file_ext = array_pop($file_array);
468 $new_file_name = sanitize_title(implode(".", $file_array)).".".$file_ext;
469 if($replacement_option == "replace_only_file") {
470 $new_file_name = $db_file_name;
471 }
472
473 if(strtolower($new_file_name) != strtolower($file_parts['basename'])) {
474 $new_file_name = $this->checkForFileName($new_file_name, $base_path . DIRECTORY_SEPARATOR);
475 }
476
477 $this->new_file_path = $base_path . DIRECTORY_SEPARATOR . $new_file_name;
478
479 $status = move_uploaded_file($new_file, $this->new_file_path);
480
481 $this->new_file_url = trim($baseurl, "/")."/".$new_file_name;
482
483 if ($status) {
484 $old_file_path = str_replace(array("/",DIRECTORY_SEPARATOR), array("", ""), $this->old_file_path);
485 $new_file_path = str_replace(array("/",DIRECTORY_SEPARATOR), array("", ""), $this->new_file_path);
486 if($old_file_path != $new_file_path) {
487 if(file_exists($this->old_file_path)) {
488 @unlink($this->old_file_path);
489 }
490 }
491
492 update_attached_file($attachment->ID, $this->new_file_url);
493
494 $update_array = array();
495 $update_array['ID'] = $attachment->ID;
496 $update_array['guid'] = $this->new_file_url; //wp_get_attachment_url($this->post_id);
497 $update_array['post_mime_type'] = $file['type'];
498
499 $current_date = date("Y-m-d H:i:s");
500 $current_date_gmt = date_i18n("Y-m-d H:i:s", strtotime($current_date));
501 if(isset($_REQUEST['date_options']) && !empty($_REQUEST['date_options'])) {
502 if($_REQUEST['date_options'] == "replace_date") {
503 $update_array['post_date'] = $current_date;
504 $update_array['post_date_gmt'] = $current_date_gmt;
505 } else if($_REQUEST['date_options'] == "custom_date") {
506 $custom_date = $_POST['custom_date'];
507 $custom_hour = str_pad($_POST['custom_date_hour'],2,0, STR_PAD_LEFT);
508 $custom_minute = str_pad($_POST['custom_date_min'], 2, 0, STR_PAD_LEFT);
509 $custom_date = date("Y-m-d H:i:s", strtotime($custom_date." {$custom_hour}:{$custom_minute}"));
510 if($custom_date !== false) {
511 $datetime = date("Y-m-d H:i:s", strtotime($custom_date));
512 $datetime_gmt = date_i18n("Y-m-d H:i:s", strtotime($datetime));
513 $update_array['post_date'] = $datetime;
514 $update_array['post_date_gmt'] = $datetime_gmt;
515 }
516 }
517 }
518 $update_array['post_modified'] = $current_date;
519 $update_array['post_modified_gmt'] = $current_date_gmt;
520 $post_id = \wp_update_post($update_array, true);
521
522 update_post_meta( $attachment_id, '_wp_attached_file', trim(trim($post_upload, "/")."/".$new_file_name ), "/");
523
524 // update post doesn't update GUID on updates.
525 $wpdb->update($wpdb->posts, array('guid' => $this->new_file_url), array('ID' => $attachment->ID));
526
527 $this->removeThumbImages();
528
529 $metadata = wp_generate_attachment_metadata($attachment->ID, $this->new_file_path);
530 wp_update_attachment_metadata($attachment->ID, $metadata);
531
532 $this->new_image_meta = wp_get_attachment_metadata($attachment_id);
533
534 // update_post_meta( $attachment_id, '_wp_attached_file', trim(trim($post_upload, "/")."/".$new_file_name ), "/");
535
536 $this->searchAndReplace();
537 wp_redirect(admin_url("post.php?post=" . $attachment_id . "&action=edit&premio_message=success&image_update=1"));
538 exit;
539 } else {
540 wp_die("Error during uploading file");
541 }
542
543 } else {
544 wp_die("Permission issue, Unable to create directory");
545 }
546 }
547 }
548 }
549
550 public function checkForFileName($fileName, $filePath, $postFix = 0) {
551 $new_file_name = $fileName;
552 if(!empty($postFix)) {
553 $file_array = explode(".", $fileName);
554 $file_ext = array_pop($file_array);
555 $new_file_name = implode(".", $file_array)."-".$postFix.".".$file_ext;
556 }
557 if(!file_exists($filePath.$new_file_name)) {
558 return $new_file_name;
559 }
560 return $this->checkForFileName($fileName, $filePath, ($postFix+1));
561 }
562
563 public $replace_items = array();
564
565 public function removeThumbImages() {
566 if(!empty($this->old_image_meta) && isset($this->old_image_meta['sizes']) && !empty($this->upload_dir) && isset($this->upload_dir['path'])) {
567 $path = $this->upload_dir['path'].DIRECTORY_SEPARATOR;
568 foreach ($this->old_image_meta['sizes'] as $image) {
569 if(file_exists($path.$image['file'])) {
570 @unlink($path . $image['file']);
571 }
572 }
573 }
574 }
575
576 public function searchAndReplace() {
577 if (wp_attachment_is('image', $this->attachment_id)) {
578 $this->is_new_image = 1;
579 }
580 if($this->old_file_url != $this->new_file_url) {
581 $replace = array(
582 'search' => $this->old_file_url,
583 'replace' => $this->new_file_url,
584 );
585 $this->replace_items[] = $replace;
586 }
587
588 $base_url = $this->upload_dir['url'];
589 $base_url = trim($base_url, "/")."/";
590 $new_url = $this->new_file_url;
591
592 if(isset($this->old_image_meta['sizes']) && !empty($this->old_image_meta['sizes'])) {
593 if(!isset($this->new_image_meta['sizes']) || empty($this->new_image_meta['sizes'])) {
594 foreach ($this->old_image_meta['sizes'] as $key=>$image) {
595 $replace = array(
596 'search' => $base_url.$image['file'],
597 'replace' => $new_url,
598 );
599 $this->replace_items[] = $replace;
600 }
601 } else if(isset($this->new_image_meta['sizes']) && !empty($this->new_image_meta['sizes'])) {
602 $new_size = $this->new_image_meta['sizes'];
603 foreach ($this->old_image_meta['sizes'] as $key=>$image) {
604 $new_replace_url = $new_url;
605 if(isset($new_size[$key])) {
606 $new_replace_url = $base_url.$new_size[$key]['file'];
607 }
608 $replace = array(
609 'search' => $base_url.$image['file'],
610 'replace' => $new_replace_url,
611 );
612 $this->replace_items[] = $replace;
613 }
614 }
615 }
616
617 if(!empty($this->replace_items)) {
618 $replace_items = array();
619 foreach($this->replace_items as $args) {
620 if($args['search'] != $args['replace']) {
621 $replace_items[] = $args;
622 }
623 }
624 $this->replace_items = $replace_items;
625 $this->replaceURL();
626 }
627 }
628
629 function replaceURL() {
630 /* check in post content */
631 $this->checkInPostContent();
632
633 /* check in options */
634 $this->checkInOptions();
635
636 /* check in meta */
637 $this->checkInMetaData();
638
639 if(function_exists('folders_clear_all_caches')) {
640 folders_clear_all_caches();
641 }
642 }
643
644 function checkInPostContent() {
645 global $wpdb;
646 $post_table = $wpdb->prefix."posts";
647 if(!empty($this->replace_items)) {
648 $query = "SELECT ID, post_content FROM {$post_table} WHERE post_content LIKE %s";
649 $update_query = "UPDATE {$post_table} SET post_content = %s WHERE ID = %d";
650 foreach ($this->replace_items as $args) {
651 if($args['search'] != $args['replace']) {
652 $sql_query = $wpdb->prepare($query, "%".$args['search']."%");
653 $results = $wpdb->get_results($sql_query, ARRAY_A );
654 if(!empty($results)) {
655 foreach ($results AS $row) {
656 $content = $this->findAndReplaceContent($row['post_content'], $args['search'], $args['replace']);
657 $update_post_query = $wpdb->prepare($update_query, $content, $row['ID']);
658 $result = $wpdb->query($update_post_query);
659 }
660 }
661 }
662 }
663 }
664 }
665
666 function checkInOptions() {
667 global $wpdb;
668 $post_table = $wpdb->prefix."options";
669 if(!empty($this->replace_items)) {
670 $query = "SELECT option_id, option_value FROM {$post_table} WHERE option_value LIKE %s";
671 $update_query = "UPDATE {$post_table} SET option_value = %s WHERE option_id = %d";
672 foreach ($this->replace_items as $args) {
673 if($args['search'] != $args['replace']) {
674 $sql_query = $wpdb->prepare($query, "%".$args['search']."%");
675 $results = $wpdb->get_results($sql_query, ARRAY_A );
676 if(!empty($results)) {
677 foreach ($results AS $row) {
678 $content = $this->findAndReplaceContent($row['post_content'], $args['search'], $args['replace']);
679 $update_post_query = $wpdb->prepare($update_query, $content, $row['ID']);
680 $result = $wpdb->query($update_post_query);
681 }
682 }
683 }
684 }
685 }
686 }
687
688 function checkInMetaData() {
689 $tables = array(
690 array(
691 'table_name' => 'usermeta',
692 'primary_key' => 'umeta_id',
693 'search_key' => 'meta_value'
694 ),
695 array(
696 'table_name' => 'termmeta',
697 'primary_key' => 'meta_id',
698 'search_key' => 'meta_value'
699 ),
700 array(
701 'table_name' => 'postmeta',
702 'primary_key' => 'meta_id',
703 'search_key' => 'meta_value'
704 ),
705 array(
706 'table_name' => 'commentmeta',
707 'primary_key' => 'meta_id',
708 'search_key' => 'meta_value'
709 )
710 );
711 global $wpdb;
712 foreach ($tables as $table) {
713 $post_table = $wpdb->prefix . $table['table_name'];
714 if (!empty($this->replace_items)) {
715 $query = "SELECT {$table['primary_key']}, {$table['search_key']} FROM {$post_table} WHERE {$table['search_key']} LIKE %s";
716 $update_query = "UPDATE {$post_table} SET {$table['search_key']} = %s WHERE {$table['primary_key']} = %d";
717 foreach ($this->replace_items as $args) {
718 if ($args['search'] != $args['replace']) {
719 $sql_query = $wpdb->prepare($query, "%" . $args['search'] . "%");
720 $results = $wpdb->get_results($sql_query, ARRAY_A);
721 if (!empty($results)) {
722 foreach ($results as $row) {
723 $content = $this->findAndReplaceContent($row[$table['search_key']], $args['search'], $args['replace']);
724 $update_post_query = $wpdb->prepare($update_query, $content, $row[$table['primary_key']]);
725 $result = $wpdb->query($update_post_query);
726 }
727 }
728 }
729 }
730 }
731 }
732 }
733
734 function findAndReplaceContent($content, $search, $replace, $in_deep = false) {
735 $content = maybe_unserialize($content);
736 $isJson = $this->isJSON($content);
737
738 if ($isJson) {
739 $content = json_decode($content);
740 }
741
742 if (is_string($content)) {
743 $content = str_replace($search, $replace, $content);
744 }
745 else if(is_wp_error($content)) {
746
747 }
748 else if(is_array($content)) {
749 foreach($content as $index => $value) {
750 $content[$index] = $this->findAndReplaceContent($value, $search, $replace, true);
751 if (is_string($index)) {
752 $index_replaced = $this->findAndReplaceContent($index, $search, $replace, true);
753 if ($index_replaced !== $index)
754 $content = $this->changeArrayKey($content, array($index => $index_replaced));
755 }
756 }
757 }
758 else if(is_object($content)) {
759 foreach($content as $key => $value) {
760 $content->{$key} = $this->findAndReplaceContent($value, $search, $replace, true);
761 }
762 }
763
764 if ($isJson && $in_deep === false) {
765 $content = json_encode($content, JSON_UNESCAPED_SLASHES);
766 }
767 else if($in_deep === false && (is_array($content) || is_object($content))) {
768 $content = maybe_serialize($content);
769 }
770
771 return $content;
772 }
773
774 function changeArrayKey($array, $set) {
775 if (is_array($array) && is_array($set)) {
776 $newArr = array();
777 foreach ($array as $k => $v) {
778 $key = array_key_exists( $k, $set) ? $set[$k] : $k;
779 $newArr[$key] = is_array($v) ? $this->changeArrayKey($v, $set) : $v;
780 }
781 return $newArr;
782 }
783 return $array;
784 }
785
786 function isJSON($content)
787 {
788 if (is_array($content) || is_object($content))
789 return false;
790
791 $json = json_decode($content);
792 return $json && $json != $content;
793 }
794 }
795 $folders_replace_media = new folders_replace_media();