PluginProbe ʕ •ᴥ•ʔ
Folders – Unlimited Folders to Organize Media Library Folder, Pages, Posts, File Manager / 2.8.3
Folders – Unlimited Folders to Organize Media Library Folder, Pages, Posts, File Manager v2.8.3
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
985 lines
1 <?php
2 /**
3 * Class Folders Replace Media
4 *
5 * @author : Premio <contact@premio.io>
6 * @license : GPL2
7 * */
8
9 if ( ! defined( 'ABSPATH' ) ) exit;
10
11 class folders_replace_media {
12
13 public $button_color;
14
15 public $is_enabled = false;
16
17 public $upgradeLink;
18
19 function __construct() {
20
21 $customize_folders = get_option('customize_folders');
22
23 $this->button_color = isset($customize_folders['media_replace_button'])?$customize_folders['media_replace_button']:"#FA166B";
24
25 $this->is_enabled = isset($customize_folders['folders_enable_replace_media'])?$customize_folders['folders_enable_replace_media']:"yes";
26
27 $this->is_enabled = ($this->is_enabled == "yes")?true:false;
28
29 if($this->is_enabled) {
30
31 add_action('admin_menu', array($this, 'admin_menu'));
32
33 add_filter('media_row_actions', array($this, 'add_media_action'), 10, 2);
34
35 add_action('add_meta_boxes', function () {
36 add_meta_box('folders-replace-box', esc_html__('Replace Media', 'folders'), array($this, 'replace_meta_box'), 'attachment', 'side', 'low');
37 });
38 add_filter('attachment_fields_to_edit', array($this, 'attachment_editor'), 10, 2);
39
40 add_action('admin_enqueue_scripts', array($this, 'folders_admin_css_and_js'));
41
42 add_action('admin_init', array($this, 'handle_folders_file_upload'));
43 }
44
45 $customize_folders = get_option("customize_folders");
46 if(isset($customize_folders['show_folder_in_settings']) && $customize_folders['show_folder_in_settings'] == "yes") {
47 $this->upgradeLink = admin_url("options-general.php?page=wcp_folders_settings&setting_page=upgrade-to-pro");
48 } else {
49 $this->upgradeLink = admin_url("admin.php?page=folders-upgrade-to-pro");
50 }
51
52 /* to replace file name */
53 add_action('add_meta_boxes', function () {
54 add_meta_box('folders-replace-file-name', esc_html__('Change file name', 'folders'), array($this, 'change_file_name_box'), 'attachment', 'side', 'core');
55 });
56
57 add_filter('attachment_fields_to_edit', array($this, 'attachment_replace_name_with_title'), 10, 2);
58
59 add_action('admin_head', array($this, 'premio_replace_file_CSS'));
60
61 add_action('wp_enqueue_media', array($this, 'replace_media_file_script'));
62
63 add_action('admin_notices', array($this, 'admin_notices'));
64 }
65
66 /**
67 * Add admin notice
68 *
69 * @since 2.6.3
70 * @access public
71 *
72 */
73 public function admin_notices() {
74 if(isset($_REQUEST['premio_message']) && $_REQUEST['premio_message'] == "success") { ?>
75 <div class="notice notice-success is-dismissible">
76 <p><b><?php esc_html_e( 'File successfully replaced', 'folders' ); ?></b></p>
77 <p><?php esc_html_e( 'The file has been successfully replaced using the file replacement feature', 'folders' ); ?></p>
78 </div>
79
80 <style>
81 .folders-undo-notification {
82 position: fixed;
83 right: -500px;
84 bottom: 25px;
85 width: 280px;
86 background: #fff;
87 padding: 15px;
88 -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%);
89 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%);
90 transition: all .25s linear;
91 z-index: 250010;
92 }
93 .folders-undo-body {
94 position: relative;
95 font-size: 13px;
96 padding: 0 0 5px 0;
97 }
98 .close-undo-box {
99 position: absolute;
100 right: -10px;
101 top: 0;
102 width: 16px;
103 height: 16px;
104 transition: all .25s linear;
105 }
106 .close-undo-box span {
107 display: block;
108 position: relative;
109 width: 16px;
110 height: 16px;
111 transition: all .2s linear;
112 }
113 .close-undo-box span:after, .close-undo-box span:before {
114 content: "";
115 position: absolute;
116 width: 12px;
117 height: 2px;
118 background-color: #333;
119 display: block;
120 border-radius: 2px;
121 transform: rotate(45deg);
122 top: 7px;
123 left: 2px;
124 }
125 .close-undo-box span:after {
126 transform: rotate(-45deg);
127 }
128 .folders-undo-header {
129 font-weight: 500;
130 font-size: 14px;
131 padding: 0 0 3px 0;
132 color: #014737;
133 }
134 .folders-undo-notification.success {
135 border-left: solid 3px #70C6A3;
136 }
137 html[dir="rtl"] .folders-undo-notification {
138 right: auto;
139 left: -500px
140 }
141 html[dir="rtl"] .folders-undo-notification.active {
142 left: 25px;
143 }
144 html[dir="rtl"] .folders-undo-notification.success {
145 border-left: none;
146 border-right: solid 3px #70C6A3;
147 }
148 html[dir="rtl"] .close-undo-box {
149 right: auto;
150 left: -10px;
151 }
152 </style>
153 <div class="folders-undo-notification success" id="media-success">
154 <div class="folders-undo-body">
155 <a href="javascript:;" class="close-undo-box"><span></span></a>
156 <div class="folders-undo-header"><?php esc_html_e( 'File successfully replaced', 'folders' ); ?></div>
157 <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>
158 </div>
159 </div>
160 <script>
161 jQuery(document).ready(function(){
162 jQuery("#media-success").addClass("active");
163 setTimeout(function(){
164 jQuery("#media-success").removeClass("active");
165 }, 5000);
166
167 jQuery(document).on("click", ".close-undo-box", function(){
168 jQuery("#media-success").removeClass("active");
169 });
170 });
171 </script>
172 <?php }
173 }
174
175 /**
176 * Add Metabox to replace file name with title
177 *
178 * @since 2.6.3
179 * @access public
180 *
181 */
182 public function change_file_name_box($post) { ?>
183 <p class="upgrade-bottom">
184 <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>
185 </p>
186 <div class="upgrade-box">
187 <a href="<?php echo esc_url($this->upgradeLink) ?>" target="_blank"><?php esc_html_e("Upgrade to Pro", "folders"); ?></a>
188 </div>
189 <?php
190 }
191
192 /**
193 * Add Js and CSS files for replace file name with title
194 *
195 * @since 2.6.3
196 * @access public
197 *
198 */
199 public function replace_media_file_script() {
200 wp_enqueue_script('folders-replace-media', WCP_FOLDER_URL . 'assets/js/replace-file-name.js', array('jquery'), WCP_FOLDER_VERSION, true);
201 wp_localize_script('folders-replace-media', 'replace_media_options', array(
202 'ajax_url' => admin_url("admin-ajax.php"),
203 ));
204 }
205
206 public function premio_replace_file_CSS() {
207 echo '<style>
208 .compat-field-replace_file_name th.label {display: none;}
209 .compat-field-replace_file_name td.field {width: 100%; border-top: solid 1px #c0c0c0; padding:10px 0 0 0;margin: 0;float: none;}
210 .compat-field-replace_file_name td.field label {width: 100%; display: block;padding:0 0 10px 0;}
211 .compat-field-replace_file_name td.field label input[type="checkbox"] {margin: 0 4px 0 2px;}
212 .compat-field-replace_file_name td.field a.update-name-with-title {display: none;}
213 .compat-field-replace_file_name td.field a.update-name-with-title.show {display: inline-block;}
214
215 .compat-field-folders th.label {width: 100%; text-align: left; padding: 0 0 10px 0; margin: 0; border-top: solid 1px #c0c0c0;float: none;}
216 .compat-field-folders th.label .alignleft {float: none; text-align: left; font-weight: bold;}
217 .compat-field-folders th.label br {display: none;}
218 .compat-field-folders td.field {width: 100%; padding: 0; margin: 0;float: none;}
219 .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)}
220 .folders-undo-notification.no .folders-undo-header { color: #dd0000; }
221 .folders-undo-notification.yes .folders-undo-header { color: #014737; }
222 .update-name-with-title .spinner {display: none; visibility: visible; margin-right: 0;}
223 .update-name-with-title.in-progress .spinner {display: inline-block;}
224
225 #folders-replace-file-name .inside {position: relative;padding:0;margin:0}
226 #folders-replace-file-name .inside p {padding: 1em; margin: 0;}
227 #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;}
228 #folders-replace-file-name:hover .upgrade-box { display: block; }
229 #folders-replace-file-name:hover p {filter: blur(1.2px);}
230 #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;}
231 </style>';
232 }
233
234 /**
235 * Replace file name with title
236 *
237 * @since 2.6.3
238 * @access public
239 *
240 */
241 public function attachment_replace_name_with_title($formFields, $post)
242 {
243 $screen = null;
244 if (function_exists('get_current_screen'))
245 {
246 $screen = get_current_screen();
247
248 if(! is_null($screen) && $screen->id == 'attachment') // hide on edit attachment screen.
249 return $formFields;
250 }
251
252 $formFields["replace_file_name"] = array(
253 "label" => esc_html__("Replace media", "folders"),
254 "input" => "html",
255 "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>",
256 "helps" => ""
257 );
258
259 return $formFields;
260 }
261
262 /**
263 * Add Js and CSS files for replace file screen
264 *
265 * @since 2.6.3
266 * @access public
267 *
268 */
269 public function folders_admin_css_and_js($page) {
270 if($page == "media_page_folders-replace-media" || $page == "admin_page_folders-replace-media") {
271 wp_enqueue_style('folders-media', plugin_dir_url(dirname(__FILE__)) . 'assets/css/replace-media.css', array(), WCP_FOLDER_VERSION);
272 wp_enqueue_script('folders-media', plugin_dir_url(dirname(__FILE__)) . 'assets/js/replace-media.js', array(), WCP_FOLDER_VERSION);
273 }
274 }
275
276 /**
277 * Add file replace menu in admin
278 *
279 * @since 2.6.3
280 * @access public
281 * @return $string
282 *
283 */
284 public function admin_menu() {
285 add_submenu_page(null,
286 esc_html__("Replace media", "folders"),
287 esc_html__("Replace media", "folders"),
288 'upload_files',
289 'folders-replace-media',
290 array($this, 'folders_replace_media')
291 );
292 }
293
294 /**
295 * Add file replacement screen
296 *
297 * @since 2.6.3
298 * @access public
299 * @return $string
300 *
301 */
302 public function folders_replace_media() {
303 global $plugin_page;
304 $action = isset($_GET['action']) ? sanitize_text_field($_GET['action']) : '';
305 $attachment_id = isset($_GET['attachment_id']) ? sanitize_text_field($_GET['attachment_id']) : '';
306 $nonce = isset($_GET['nonce']) ? sanitize_text_field($_GET['nonce']) : '';
307 if (!wp_verify_nonce($nonce, "folders-replace-media-".$attachment_id)) {
308 echo 'Invalid Nonce';
309 exit;
310 }
311 $attachment = get_post($attachment_id);
312 if(empty($attachment) || !isset($attachment->guid)) {
313 echo 'Invalid URL';
314 exit;
315 }
316 $guid = $attachment->guid;
317 $guid = explode(".", $guid);
318 if($guid == $attachment->guid) {
319 echo 'Invalid URL';
320 exit;
321 }
322 $form_action = $this->getMediaReplaceURL($attachment_id);
323 include_once dirname(dirname(__FILE__)) . WCP_DS . "/templates" . WCP_DS . "admin" . WCP_DS . "media-replace.php";
324 }
325
326 /**
327 * Add action for file replacement
328 *
329 * @since 2.6.3
330 * @access public
331 * @return $actions
332 *
333 */
334 public function add_media_action($actions, $post) {
335 if(!$this->is_enabled) {
336 return array_merge($actions);
337 }
338 if (wp_attachment_is('image', $post->ID)) {
339 $link = $this->getMediaReplaceURL( $post->ID );
340
341 $newaction['replace_media'] = '<a style="color: ' . $this->button_color . '" href="' . $link . '" rel="permalink">' . esc_html__( "Replace media", "folders" ) . '</a>';
342
343 return array_merge( $actions, $newaction );
344 } else {
345 $link = $this->getMediaReplaceURL( $post->ID );
346
347 $newaction['replace_media'] = '<a style="color: ' . $this->button_color . '" target="_blank" href="' . $this->upgradeLink . '" rel="permalink">' . esc_html__( "Replace Media 🔑", "folders" ) . '</a>';
348
349 return array_merge( $actions, $newaction );
350 }
351 return $actions;
352 }
353
354 /**
355 * Get URL for file Replacement
356 *
357 * @since 2.6.3
358 * @access public
359 * @return $url
360 *
361 */
362 public function getMediaReplaceURL($attach_id) {
363 $url = admin_url( "upload.php");
364 $url = add_query_arg(array(
365 'page' => 'folders-replace-media',
366 'action' => 'folders_replace_media',
367 'attachment_id' => $attach_id,
368 'nonce' => wp_create_nonce("folders-replace-media-".$attach_id)
369 ), $url);
370
371 return $url;
372 }
373
374 /**
375 * Get file size
376 *
377 * @since 2.6.3
378 * @access public
379 * @return $size
380 *
381 */
382 public function replace_meta_box($post) {
383 if (wp_attachment_is('image', $post->ID)) {
384 $link = $this->getMediaReplaceURL($post->ID);
385 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>";
386 } else {
387 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>";
388 }
389 }
390
391 /**
392 * Get file size
393 *
394 * @since 2.6.3
395 * @access public
396 * @return $size
397 *
398 */
399 public function attachment_editor($formFields, $post)
400 {
401 $screen = null;
402 if (function_exists('get_current_screen'))
403 {
404 $screen = get_current_screen();
405
406 if(! is_null($screen) && $screen->id == 'attachment') // hide on edit attachment screen.
407 return $formFields;
408 }
409 if (wp_attachment_is('image', $post->ID)) {
410 $link = $this->getMediaReplaceURL( $post->ID );
411 $formFields["folders"] = array(
412 "label" => esc_html__( "Replace media", "folders" ),
413 "input" => "html",
414 "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>",
415 "helps" => esc_html__( "Click on the button to replace the file with another file", "folders" )
416 );
417 } else {
418 $formFields["folders"] = array(
419 "label" => esc_html__( "Replace media", "folders" ),
420 "input" => "html",
421 "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>",
422 "helps" => esc_html__( "Click on the button to replace the file with another file", "folders" )
423 );
424 }
425
426 return $formFields;
427 }
428
429 /**
430 * Get file size
431 *
432 * @since 2.6.3
433 * @access public
434 * @return $size
435 *
436 */
437 public function getFileSize($attachment_id) {
438 $size = filesize( get_attached_file( $attachment_id ));
439 if($size > 1000000) {
440 $size = ($size/1000000);
441 return number_format((float)$size, 2, ".", ",")." MB";
442 } else if($size > 1000) {
443 $size = ($size/1000);
444 return number_format((float)$size, 2, ".", ",")." KB";
445 }
446 return $size." B";
447 }
448
449 public $old_file_ext;
450 public $old_file_path;
451 public $old_file_url;
452 public $new_file_path;
453 public $new_file_url;
454
455 public $new_file_name;
456
457 public $current_upload_data;
458
459 public $mode = "rename-file";
460
461 public $old_image_meta;
462 public $new_image_meta;
463 public $upload_dir;
464
465 public $is_old_image = 0;
466 public $is_new_image = 0;
467 public $attachment_id;
468
469
470 /**
471 * Upload file and Replace it
472 *
473 * @since 2.6.3
474 * @access public
475 *
476 */
477 public function handle_folders_file_upload() {
478 global $wpdb;
479 if(isset($_FILES['new_media_file'])) {
480 if($_FILES['new_media_file']['error'] == 0) {
481 $attachment_id = isset($_GET['attachment_id']) ? sanitize_text_field($_GET['attachment_id']) : '';
482 $nonce = isset($_GET['nonce']) ? sanitize_text_field($_GET['nonce']) : '';
483 if (!wp_verify_nonce($nonce, "folders-replace-media-" . $attachment_id)) {
484 return;
485 }
486 $attachment = get_post($attachment_id);
487 if (empty($attachment) || !isset($attachment->guid)) {
488 return;
489 }
490 $attachment_url = $attachment->guid;
491 $url = wp_get_attachment_url($attachment_id);
492 if(!empty($url)) {
493 $attachment_url = $url;
494 }
495 $guid = explode(".", $attachment_url);
496 $guid = array_pop($guid);
497 if ($guid == $attachment->guid) {
498 return;
499 }
500
501 $replacement_option = isset($_REQUEST['replacement_option'])?$_REQUEST['replacement_option']:"replace_only_file";
502
503 $this->attachment_id = $attachment_id;
504
505 $file = $_FILES['new_media_file'];
506 $file_name = $file['name'];
507 $file_ext = explode(".", $file_name);
508 $file_ext = array_pop($file_ext);
509
510 if (wp_attachment_is('image', $attachment_id)) {
511 $this->is_old_image = 1;
512 }
513 $this->old_file_url = $attachment_url;
514 $this->old_image_meta = wp_get_attachment_metadata($attachment_id);
515
516 $new_file = $file['tmp_name'];
517
518 $file_parts = pathinfo($attachment_url);
519
520 $db_file_name = $file_parts['basename'];
521 $db_file_array = explode(".", $db_file_name);
522 array_pop($db_file_array);
523 $db_file_name = implode(".", $db_file_array).".";
524 $db_file_name .= $file_ext;
525
526 $wp_upload_path = wp_get_upload_dir();
527
528 $base_path = $old_path = $wp_upload_path['basedir'].DIRECTORY_SEPARATOR;
529 $baseurl = $old_url = $wp_upload_path['baseurl']."/";
530
531 $post_upload = "";
532
533 $wp_attached_file = get_post_meta($attachment_id, "_wp_attached_file", true);
534 if($wp_attached_file !== false) {
535 $old_file_name = explode("/", $wp_attached_file);
536 array_pop($old_file_name);
537
538 if(count($old_file_name) > 0) {
539 $old_path .= implode(DIRECTORY_SEPARATOR, $old_file_name);
540 $old_url .= implode("/", $old_file_name);
541 }
542
543 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'])) {
544 $baseurl .= $_REQUEST['new_folder_path'];
545 $base_path .= str_replace("/", DIRECTORY_SEPARATOR, $_REQUEST['new_folder_path']);
546
547 $post_upload = $_REQUEST['new_folder_path'];
548 } else if(count($old_file_name) > 0) {
549 $baseurl .= implode(DIRECTORY_SEPARATOR, $old_file_name);
550 $base_path .= implode("/", $old_file_name);
551
552 $post_upload = implode("/", $old_file_name);
553 }
554 }
555
556 $upload_dir = array();
557 $upload_dir['path'] = $base_path;
558 $upload_dir['old_path'] = $old_path;
559 $upload_dir['url'] = $baseurl;
560 $upload_dir['old_url'] = $old_url;
561
562 $this->upload_dir = $upload_dir;
563
564 $this->old_file_path = $old_path . "/" . $file_parts['basename'];
565
566 if (!is_dir($base_path)) {
567 mkdir($base_path, 755, true);
568 }
569
570 if (is_dir($base_path)) {
571
572 $file_array = explode(".", $file['name']);
573 $file_ext = array_pop($file_array);
574 $new_file_name = sanitize_title(implode(".", $file_array)).".".$file_ext;
575 if($replacement_option == "replace_only_file") {
576 $new_file_name = $db_file_name;
577 }
578
579 if(strtolower($new_file_name) != strtolower($file_parts['basename'])) {
580 $new_file_name = $this->checkForFileName($new_file_name, $base_path . DIRECTORY_SEPARATOR);
581 }
582
583 $this->new_file_path = $base_path . DIRECTORY_SEPARATOR . $new_file_name;
584
585 $status = move_uploaded_file($new_file, $this->new_file_path);
586
587 $this->new_file_url = trim($baseurl, "/")."/".$new_file_name;
588
589 if ($status) {
590 $old_file_path = str_replace(array("/",DIRECTORY_SEPARATOR), array("", ""), $this->old_file_path);
591 $new_file_path = str_replace(array("/",DIRECTORY_SEPARATOR), array("", ""), $this->new_file_path);
592 if($old_file_path != $new_file_path) {
593 if(file_exists($this->old_file_path)) {
594 @unlink($this->old_file_path);
595 }
596 }
597
598 update_attached_file($attachment->ID, $this->new_file_url);
599
600 $update_array = array();
601 $update_array['ID'] = $attachment->ID;
602 $update_array['guid'] = $this->new_file_url; //wp_get_attachment_url($this->post_id);
603 $update_array['post_mime_type'] = $file['type'];
604
605 $current_date = date("Y-m-d H:i:s");
606 $current_date_gmt = date_i18n("Y-m-d H:i:s", strtotime($current_date));
607 if(isset($_REQUEST['date_options']) && !empty($_REQUEST['date_options'])) {
608 if($_REQUEST['date_options'] == "replace_date") {
609 $update_array['post_date'] = $current_date;
610 $update_array['post_date_gmt'] = $current_date_gmt;
611 } else if($_REQUEST['date_options'] == "custom_date") {
612 $custom_date = $_POST['custom_date'];
613 $custom_hour = str_pad($_POST['custom_date_hour'],2,0, STR_PAD_LEFT);
614 $custom_minute = str_pad($_POST['custom_date_min'], 2, 0, STR_PAD_LEFT);
615 $custom_date = date("Y-m-d H:i:s", strtotime($custom_date." {$custom_hour}:{$custom_minute}"));
616 if($custom_date !== false) {
617 $datetime = date("Y-m-d H:i:s", strtotime($custom_date));
618 $datetime_gmt = date_i18n("Y-m-d H:i:s", strtotime($datetime));
619 $update_array['post_date'] = $datetime;
620 $update_array['post_date_gmt'] = $datetime_gmt;
621 }
622 }
623 }
624 $update_array['post_modified'] = $current_date;
625 $update_array['post_modified_gmt'] = $current_date_gmt;
626 $post_id = \wp_update_post($update_array, true);
627
628 update_post_meta( $attachment_id, '_wp_attached_file', trim(trim($post_upload, "/")."/".$new_file_name ), "/");
629
630 // update post doesn't update GUID on updates.
631 $wpdb->update($wpdb->posts, array('guid' => $this->new_file_url), array('ID' => $attachment->ID));
632
633 $this->removeThumbImages();
634
635 $metadata = wp_generate_attachment_metadata($attachment->ID, $this->new_file_path);
636 wp_update_attachment_metadata($attachment->ID, $metadata);
637
638 $this->new_image_meta = wp_get_attachment_metadata($attachment_id);
639
640 // update_post_meta( $attachment_id, '_wp_attached_file', trim(trim($post_upload, "/")."/".$new_file_name ), "/");
641
642 $this->searchAndReplace();
643 wp_redirect(admin_url("post.php?post=" . $attachment_id . "&action=edit&premio_message=success&image_update=1"));
644 exit;
645 } else {
646 wp_die("Error during uploading file");
647 }
648
649 } else {
650 wp_die("Permission issue, Unable to create directory");
651 }
652 }
653 }
654 }
655
656 /**
657 * Check for filename
658 *
659 * @since 2.6.3
660 * @access public
661 *
662 */
663 public function checkForFileName($fileName, $filePath, $postFix = 0) {
664 $new_file_name = $fileName;
665 if(!empty($postFix)) {
666 $file_array = explode(".", $fileName);
667 $file_ext = array_pop($file_array);
668 $new_file_name = implode(".", $file_array)."-".$postFix.".".$file_ext;
669 }
670 if(!file_exists($filePath.$new_file_name)) {
671 return $new_file_name;
672 }
673 return $this->checkForFileName($fileName, $filePath, ($postFix+1));
674 }
675
676 public $replace_items = array();
677
678
679 /**
680 * Check and Remove Thumb image in wp-content
681 *
682 * @since 2.6.3
683 * @access public
684 *
685 */
686 public function removeThumbImages() {
687 if(!empty($this->old_image_meta) && isset($this->old_image_meta['sizes']) && !empty($this->upload_dir) && isset($this->upload_dir['path'])) {
688 $path = $this->upload_dir['path'].DIRECTORY_SEPARATOR;
689 foreach ($this->old_image_meta['sizes'] as $image) {
690 if(file_exists($path.$image['file'])) {
691 @unlink($path . $image['file']);
692 }
693 }
694 }
695 }
696
697 /**
698 * Search and Replace files in Database
699 *
700 * @since 2.6.3
701 * @access public
702 * @return $string
703 *
704 */
705 public function searchAndReplace() {
706 if (wp_attachment_is('image', $this->attachment_id)) {
707 $this->is_new_image = 1;
708 }
709 if($this->old_file_url != $this->new_file_url) {
710 $replace = array(
711 'search' => $this->old_file_url,
712 'replace' => $this->new_file_url,
713 );
714 $this->replace_items[] = $replace;
715 }
716
717 $base_url = $this->upload_dir['url'];
718 $base_url = trim($base_url, "/")."/";
719 $new_url = $this->new_file_url;
720
721 if(isset($this->old_image_meta['sizes']) && !empty($this->old_image_meta['sizes'])) {
722 if(!isset($this->new_image_meta['sizes']) || empty($this->new_image_meta['sizes'])) {
723 foreach ($this->old_image_meta['sizes'] as $key=>$image) {
724 $replace = array(
725 'search' => $base_url.$image['file'],
726 'replace' => $new_url,
727 );
728 $this->replace_items[] = $replace;
729 }
730 } else if(isset($this->new_image_meta['sizes']) && !empty($this->new_image_meta['sizes'])) {
731 $new_size = $this->new_image_meta['sizes'];
732 foreach ($this->old_image_meta['sizes'] as $key=>$image) {
733 $new_replace_url = $new_url;
734 if(isset($new_size[$key])) {
735 $new_replace_url = $base_url.$new_size[$key]['file'];
736 }
737 $replace = array(
738 'search' => $base_url.$image['file'],
739 'replace' => $new_replace_url,
740 );
741 $this->replace_items[] = $replace;
742 }
743 }
744 }
745
746 if(!empty($this->replace_items)) {
747 $replace_items = array();
748 foreach($this->replace_items as $args) {
749 if($args['search'] != $args['replace']) {
750 $replace_items[] = $args;
751 }
752 }
753 $this->replace_items = $replace_items;
754 $this->replaceURL();
755 }
756 }
757
758 /**
759 * Replace URL in Database tables
760 *
761 * @since 2.6.3
762 * @access public
763 * @return $string
764 *
765 */
766 function replaceURL() {
767 /* check in post content */
768 $this->checkInPostContent();
769
770 /* check in options */
771 $this->checkInOptions();
772
773 /* check in meta */
774 $this->checkInMetaData();
775
776 if(function_exists('folders_clear_all_caches')) {
777 folders_clear_all_caches();
778 }
779 }
780
781 /**
782 * Checking image URLs in Post Content
783 *
784 * @since 2.6.3
785 * @access public
786 * @return $string
787 *
788 */
789 function checkInPostContent() {
790 global $wpdb;
791 $post_table = $wpdb->prefix."posts";
792 if(!empty($this->replace_items)) {
793 $query = "SELECT ID, post_content FROM {$post_table} WHERE post_content LIKE %s";
794 $update_query = "UPDATE {$post_table} SET post_content = %s WHERE ID = %d";
795 foreach ($this->replace_items as $args) {
796 if($args['search'] != $args['replace']) {
797 $sql_query = $wpdb->prepare($query, "%".$args['search']."%");
798 $results = $wpdb->get_results($sql_query, ARRAY_A );
799 if(!empty($results)) {
800 foreach ($results AS $row) {
801 $content = $this->findAndReplaceContent($row['post_content'], $args['search'], $args['replace']);
802 $update_post_query = $wpdb->prepare($update_query, $content, $row['ID']);
803 $result = $wpdb->query($update_post_query);
804 }
805 }
806 }
807 }
808 }
809 }
810
811 /**
812 * Checking image URLs in MetaData
813 *
814 * @since 2.6.3
815 * @access public
816 * @return $string
817 *
818 */
819 function checkInOptions() {
820 global $wpdb;
821 $post_table = $wpdb->prefix."options";
822 if(!empty($this->replace_items)) {
823 $query = "SELECT option_id, option_value FROM {$post_table} WHERE option_value LIKE %s";
824 $update_query = "UPDATE {$post_table} SET option_value = %s WHERE option_id = %d";
825 foreach ($this->replace_items as $args) {
826 if($args['search'] != $args['replace']) {
827 $sql_query = $wpdb->prepare($query, "%".$args['search']."%");
828 $results = $wpdb->get_results($sql_query, ARRAY_A );
829 if(!empty($results)) {
830 foreach ($results AS $row) {
831 $content = $this->findAndReplaceContent($row['post_content'], $args['search'], $args['replace']);
832 $update_post_query = $wpdb->prepare($update_query, $content, $row['ID']);
833 $result = $wpdb->query($update_post_query);
834 }
835 }
836 }
837 }
838 }
839 }
840
841 /**
842 * Checking image URLs in MetaData
843 *
844 * @since 2.6.3
845 * @access public
846 * @return $string
847 *
848 */
849 function checkInMetaData() {
850 $tables = array(
851 array(
852 'table_name' => 'usermeta',
853 'primary_key' => 'umeta_id',
854 'search_key' => 'meta_value'
855 ),
856 array(
857 'table_name' => 'termmeta',
858 'primary_key' => 'meta_id',
859 'search_key' => 'meta_value'
860 ),
861 array(
862 'table_name' => 'postmeta',
863 'primary_key' => 'meta_id',
864 'search_key' => 'meta_value'
865 ),
866 array(
867 'table_name' => 'commentmeta',
868 'primary_key' => 'meta_id',
869 'search_key' => 'meta_value'
870 )
871 );
872 global $wpdb;
873 foreach ($tables as $table) {
874 $post_table = $wpdb->prefix . $table['table_name'];
875 if (!empty($this->replace_items)) {
876 $query = "SELECT {$table['primary_key']}, {$table['search_key']} FROM {$post_table} WHERE {$table['search_key']} LIKE %s";
877 $update_query = "UPDATE {$post_table} SET {$table['search_key']} = %s WHERE {$table['primary_key']} = %d";
878 foreach ($this->replace_items as $args) {
879 if ($args['search'] != $args['replace']) {
880 $sql_query = $wpdb->prepare($query, "%" . $args['search'] . "%");
881 $results = $wpdb->get_results($sql_query, ARRAY_A);
882 if (!empty($results)) {
883 foreach ($results as $row) {
884 $content = $this->findAndReplaceContent($row[$table['search_key']], $args['search'], $args['replace']);
885 $update_post_query = $wpdb->prepare($update_query, $content, $row[$table['primary_key']]);
886 $result = $wpdb->query($update_post_query);
887 }
888 }
889 }
890 }
891 }
892 }
893 }
894
895 /**
896 * Checking for Array Key
897 *
898 * @since 2.6.3
899 * @access public
900 * @return $json
901 * Forked from Enable Media Replace
902 *
903 */
904 function findAndReplaceContent($content, $search, $replace, $depth = false) {
905 $content = maybe_unserialize($content);
906
907 // Checking for JSON Data
908 $isJson = $this->isJSON($content);
909 if ($isJson) {
910 $content = json_decode($content);
911 }
912
913 // Replace content if content is String
914 if (is_string($content)) {
915 $content = str_replace($search, $replace, $content);
916 }
917 else if(is_wp_error($content)) { // Return if error in data
918
919 }
920 else if(is_array($content)) { // Replace content if content is Array
921 foreach($content as $index => $value) {
922 $content[$index] = $this->findAndReplaceContent($value, $search, $replace, true);
923 if (is_string($index)) {
924 $index_replaced = $this->findAndReplaceContent($index, $search, $replace, true);
925 if ($index_replaced !== $index)
926 $content = $this->changeArrayKey($content, array($index => $index_replaced));
927 }
928 }
929 }
930 else if(is_object($content)) { // Replace content if content is Object
931 foreach($content as $key => $value) {
932 $content->{$key} = $this->findAndReplaceContent($value, $search, $replace, true);
933 }
934 }
935
936 if ($isJson && $depth === false) {
937 $content = json_encode($content, JSON_UNESCAPED_SLASHES);
938 }
939 else if($depth === false && (is_array($content) || is_object($content))) {
940 $content = maybe_serialize($content);
941 }
942
943 return $content;
944 }
945
946 /**
947 * Checking for Array Key
948 *
949 * @since 2.6.3
950 * @access public
951 * @return $json
952 *
953 */
954 function changeArrayKey($array, $set) {
955 if (is_array($array) && is_array($set)) {
956 $newArray = array();
957 foreach ($array as $k => $v) {
958 $key = array_key_exists( $k, $set) ? $set[$k] : $k;
959 $newArray[$key] = is_array($v) ? $this->changeArrayKey($v, $set) : $v;
960 }
961 return $newArray;
962 }
963 return $array;
964 }
965
966 /**
967 * Check if it is JSON or not
968 *
969 * @since 2.6.3
970 * @access public
971 * Forked from Enable Media Replace
972 * @return $json
973 *
974 */
975 function isJSON($content)
976 {
977 if (is_array($content) || is_object($content))
978 return false;
979
980 $json = json_decode($content);
981 return $json && $json != $content;
982 }
983 }
984 $folders_replace_media = new folders_replace_media();
985