PluginProbe ʕ •ᴥ•ʔ
Folders – Unlimited Folders to Organize Media Library Folder, Pages, Posts, File Manager / 2.6.4
Folders – Unlimited Folders to Organize Media Library Folder, Pages, Posts, File Manager v2.6.4
3.1.9 3.1.8 3.1.7 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 2.9.8 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 trunk 1.3.7 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6 2.6.1 2.6.2 2.6.3 2.6.4 2.6.5 2.6.6 2.6.7 2.6.8 2.6.9 2.7 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9 2.9.1 2.9.2
folders / includes / media.replace.php
folders / includes Last commit date
class-affiliate.php 5 years ago class-polylang.php 5 years ago class-review-box.php 5 years ago class-wpml.php 5 years ago folders.class.php 5 years ago form.class.php 5 years ago media.replace.php 5 years ago plugin.updates.php 5 years ago plugins.class.php 5 years ago tree.class.php 5 years ago
media.replace.php
486 lines
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) exit;
3
4 class folders_replace_media {
5
6 public $button_color;
7
8 function __construct() {
9
10 $customize_folders = get_option('customize_folders');
11
12 $this->button_color = isset($customize_folders['media_replace_button'])?$customize_folders['media_replace_button']:"#FA166B";
13
14 add_action('admin_menu', array($this, 'admin_menu'));
15
16 add_filter('media_row_actions', array($this, 'add_media_action'), 10, 2);
17
18 add_action('add_meta_boxes', function () {
19 add_meta_box('folders-replace-box', esc_html__('Replace Media', 'folders'), array($this, 'replace_meta_box'), 'attachment', 'side', 'low');
20 });
21 add_filter('attachment_fields_to_edit', array($this, 'attachment_editor'), 10, 2);
22
23 add_action('admin_enqueue_scripts', array($this, 'folders_admin_css_and_js'));
24
25 add_action('admin_init', array($this, 'handle_folders_file_upload'));
26
27 }
28
29 public function folders_admin_css_and_js($page) {
30 if($page == "media_page_folders-replace-media") {
31 wp_enqueue_style('folders-media', plugin_dir_url(dirname(__FILE__)) . 'assets/css/replace-media.css', array(), WCP_FOLDER_VERSION);
32 wp_enqueue_script('folders-media', plugin_dir_url(dirname(__FILE__)) . 'assets/js/replace-media.js', array(), WCP_FOLDER_VERSION);
33 }
34 }
35
36 public function admin_menu()
37 {
38 add_submenu_page(null,
39 esc_html__("Replace media", "folders"),
40 esc_html__("Replace media", "folders"),
41 'upload_files',
42 'folders-replace-media',
43 array($this, 'folders_replace_media')
44 );
45 }
46
47 public function folders_replace_media() {
48 global $plugin_page;
49 $action = isset($_GET['action']) ? sanitize_text_field($_GET['action']) : '';
50 $attachment_id = isset($_GET['attachment_id']) ? sanitize_text_field($_GET['attachment_id']) : '';
51 $nonce = isset($_GET['nonce']) ? sanitize_text_field($_GET['nonce']) : '';
52 if (!wp_verify_nonce($nonce, "folders-replace-media-".$attachment_id)) {
53 echo 'Invalid Nonce';
54 exit;
55 }
56 $attachment = get_post($attachment_id);
57 if(empty($attachment) || !isset($attachment->guid)) {
58 echo 'Invalid URL';
59 exit;
60 }
61 $guid = $attachment->guid;
62 $guid = explode(".", $guid);
63 if($guid == $attachment->guid) {
64 echo 'Invalid URL';
65 exit;
66 }
67 $form_action = $this->getMediaReplaceURL($attachment_id);
68 include_once dirname(dirname(__FILE__)) . WCP_DS . "/templates" . WCP_DS . "admin" . WCP_DS . "media-replace.php";
69 }
70
71 public function add_media_action($actions, $post) {
72 $link = $this->getMediaReplaceURL($post->ID);
73
74 $newaction['replace_media'] = '<a style="color: '.$this->button_color.'" href="' . $link . '" rel="permalink">' . esc_html__("Replace media", "folders") . '</a>';
75 return array_merge($actions, $newaction);
76 }
77
78 public function getMediaReplaceURL($attach_id) {
79 $url = admin_url( "upload.php");
80 $url = add_query_arg(array(
81 'page' => 'folders-replace-media',
82 'action' => 'folders_replace_media',
83 'attachment_id' => $attach_id,
84 'nonce' => wp_create_nonce("folders-replace-media-".$attach_id)
85 ), $url);
86
87 return $url;
88 }
89
90 public function replace_meta_box($post) {
91 $link = $this->getMediaReplaceURL($post->ID);
92 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>";
93 }
94
95 public function attachment_editor($form_fields, $post)
96 {
97 $screen = null;
98 if (function_exists('get_current_screen'))
99 {
100 $screen = get_current_screen();
101
102 if(! is_null($screen) && $screen->id == 'attachment') // hide on edit attachment screen.
103 return $form_fields;
104 }
105
106 $link = $this->getMediaReplaceURL($post->ID);
107 $form_fields["folders"] = array(
108 "label" => esc_html__("Replace media", "folders"),
109 "input" => "html",
110 "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>", "helps" => esc_html__("Click on the button to replace the file with another file", "folders")
111 );
112
113 return $form_fields;
114 }
115
116 public function getFileSize($attachment_id) {
117 $size = filesize( get_attached_file( $attachment_id ));
118 if($size > 1000000) {
119 $size = ($size/1000000);
120 return number_format((float)$size, 2, ".", ",")." MB";
121 } else if($size > 1000) {
122 $size = ($size/1000);
123 return number_format((float)$size, 2, ".", ",")." KB";
124 }
125 return $size." B";
126 }
127
128 public $old_file_ext;
129 public $old_file_path;
130 public $old_file_url;
131 public $new_file_path;
132 public $new_file_url;
133
134 public $new_file_name;
135
136 public $current_upload_data;
137
138 public $mode = "rename-file";
139
140 public $old_image_meta;
141 public $new_image_meta;
142 public $upload_dir;
143
144 public $is_old_image = 0;
145 public $is_new_image = 0;
146 public $attachment_id;
147
148 public function handle_folders_file_upload() {
149 global $wpdb;
150 if(isset($_FILES['new_media_file'])) {
151 if($_FILES['new_media_file']['error'] == 0) {
152 $attachment_id = isset($_GET['attachment_id']) ? sanitize_text_field($_GET['attachment_id']) : '';
153 $nonce = isset($_GET['nonce']) ? sanitize_text_field($_GET['nonce']) : '';
154 if (!wp_verify_nonce($nonce, "folders-replace-media-" . $attachment_id)) {
155 return;
156 }
157 $attachment = get_post($attachment_id);
158 if (empty($attachment) || !isset($attachment->guid)) {
159 return;
160 }
161 $attachment_url = $attachment->guid;
162 $url = wp_get_attachment_url($attachment_id);
163 if(!empty($url)) {
164 $attachment_url = $url;
165 }
166 $guid = explode(".", $attachment_url);
167 $guid = array_pop($guid);
168 if ($guid == $attachment->guid) {
169 return;
170 }
171
172 $this->attachment_id = $attachment_id;
173
174 $file = $_FILES['new_media_file'];
175 $file_name = $file['name'];
176 $file_ext = explode(".", $file_name);
177 $file_ext = array_pop($file_ext);
178
179 if ($guid == $file_ext) {
180 $this->mode = "replace-file";
181 }
182
183 if (wp_attachment_is('image', $attachment_id)) {
184 $this->is_old_image = 1;
185 }
186 $this->old_file_url = $attachment_url;
187
188 $new_file = $file['tmp_name'];
189
190 $file_parts = pathinfo($attachment_url);
191
192 $upload_dir = wp_upload_dir($attachment->post_date_gmt);
193 $this->current_upload_data = $upload_dir;
194 $upload_path = $upload_dir['path'];
195
196 $this->upload_dir = $upload_dir;
197
198 $this->old_file_path = $upload_dir['path'] . "/" . $file_parts['basename'];
199
200 $this->old_image_meta = wp_get_attachment_metadata($attachment_id);
201 if (!is_dir($upload_path)) {
202 mkdir($upload_path, 755, true);
203 }
204 if (is_dir($upload_path)) {
205 $file_name = uniqid()."-".$file['name'];
206 $upload_file = $upload_path . DIRECTORY_SEPARATOR . $file_name;
207 $status = move_uploaded_file($new_file, $upload_file);
208
209 $this->new_file_path = $upload_dir['path'] . "/" . $file_name;
210
211 $this->new_file_url = $upload_dir['url']."/".$file_name;
212
213 if ($status) {
214
215 if(file_exists($this->upload_dir['path'].DIRECTORY_SEPARATOR.$file_parts['basename'])) {
216 @unlink($this->upload_dir['path'].DIRECTORY_SEPARATOR.$file_parts['basename']);
217 }
218
219 update_attached_file($attachment->ID, $this->new_file_path);
220
221 $update_array = array();
222 $update_array['ID'] = $attachment->ID;
223 $update_array['post_title'] = $file_parts['filename'];
224 $update_array['post_name'] = sanitize_title($file_parts['filename']);
225 $update_array['guid'] = $this->new_file_path; //wp_get_attachment_url($this->post_id);
226 $update_array['post_mime_type'] = $file['type'];
227 $post_id = \wp_update_post($update_array, true);
228
229 // update post doesn't update GUID on updates.
230 $wpdb->update($wpdb->posts, array('guid' => $this->new_file_path), array('ID' => $attachment->ID));
231
232 $this->removeThumbImages();
233
234 $metadata = wp_generate_attachment_metadata($attachment->ID, $this->new_file_path);
235 wp_update_attachment_metadata($attachment->ID, $metadata);
236
237 $this->new_image_meta = wp_get_attachment_metadata($attachment_id);
238
239 $this->searchAndReplace();
240
241 wp_redirect(admin_url("post.php?post=" . $attachment_id . "&action=edit"));
242 exit;
243 } else {
244 wp_die("Error during uploading file");
245 }
246
247 } else {
248 wp_die("Permission issue, Unable to create directory");
249 }
250 }
251 }
252 }
253
254 public $replace_items = array();
255
256 public function removeThumbImages() {
257 if(!empty($this->old_image_meta) && isset($this->old_image_meta['sizes']) && !empty($this->upload_dir) && isset($this->upload_dir['path'])) {
258 $path = $this->upload_dir['path'].DIRECTORY_SEPARATOR;
259 foreach ($this->old_image_meta['sizes'] as $image) {
260 if(file_exists($path.$image['file'])) {
261 @unlink($path . $image['file']);
262 }
263 }
264 }
265 }
266
267 public function searchAndReplace() {
268 if (wp_attachment_is('image', $this->attachment_id)) {
269 $this->is_new_image = 1;
270 }
271 if($this->old_file_url != $this->new_file_url) {
272 $replace = array(
273 'search' => $this->old_file_url,
274 'replace' => $this->new_file_url,
275 );
276 $this->replace_items[] = $replace;
277 }
278
279 $base_url = $this->upload_dir['url'];
280 $base_url = trim($base_url, "/")."/";
281 $new_url = $this->new_file_url;
282
283 if(isset($this->old_image_meta['sizes']) && !empty($this->old_image_meta['sizes'])) {
284 if(!isset($this->new_image_meta['sizes']) || empty($this->new_image_meta['sizes'])) {
285 foreach ($this->old_image_meta['sizes'] as $key=>$image) {
286 $replace = array(
287 'search' => $base_url.$image['file'],
288 'replace' => $new_url,
289 );
290 $this->replace_items[] = $replace;
291 }
292 } else if(isset($this->new_image_meta['sizes']) && !empty($this->new_image_meta['sizes'])) {
293 $new_size = $this->new_image_meta['sizes'];
294 foreach ($this->old_image_meta['sizes'] as $key=>$image) {
295 $new_replace_url = $new_url;
296 if(isset($new_size[$key])) {
297 $new_replace_url = $base_url.$new_size[$key]['file'];
298 }
299 $replace = array(
300 'search' => $base_url.$image['file'],
301 'replace' => $new_replace_url,
302 );
303 $this->replace_items[] = $replace;
304 }
305 }
306 }
307
308 if(!empty($this->replace_items)) {
309 $replace_items = array();
310 foreach($this->replace_items as $args) {
311 if($args['search'] != $args['replace']) {
312 $replace_items[] = $args;
313 }
314 }
315 $this->replace_items = $replace_items;
316 $this->replaceURL();
317 }
318 }
319
320 function replaceURL() {
321 /* check in post content */
322 $this->checkInPostContent();
323
324 /* check in options */
325 $this->checkInOptions();
326
327 /* check in meta */
328 $this->checkInMetaData();
329
330 if(function_exists('folders_clear_all_caches')) {
331 folders_clear_all_caches();
332 }
333 }
334
335 function checkInPostContent() {
336 global $wpdb;
337 $post_table = $wpdb->prefix."posts";
338 if(!empty($this->replace_items)) {
339 $query = "SELECT ID, post_content FROM {$post_table} WHERE post_content LIKE %s";
340 $update_query = "UPDATE {$post_table} SET post_content = %s WHERE ID = %d";
341 foreach ($this->replace_items as $args) {
342 if($args['search'] != $args['replace']) {
343 $sql_query = $wpdb->prepare($query, "%".$args['search']."%");
344 $results = $wpdb->get_results($sql_query, ARRAY_A );
345 if(!empty($results)) {
346 foreach ($results AS $row) {
347 $content = $this->findAndReplaceContent($row['post_content'], $args['search'], $args['replace']);
348 $update_post_query = $wpdb->prepare($update_query, $content, $row['ID']);
349 $result = $wpdb->query($update_post_query);
350 }
351 }
352 }
353 }
354 }
355 }
356
357 function checkInOptions() {
358 global $wpdb;
359 $post_table = $wpdb->prefix."options";
360 if(!empty($this->replace_items)) {
361 $query = "SELECT option_id, option_value FROM {$post_table} WHERE option_value LIKE %s";
362 $update_query = "UPDATE {$post_table} SET option_value = %s WHERE option_id = %d";
363 foreach ($this->replace_items as $args) {
364 if($args['search'] != $args['replace']) {
365 $sql_query = $wpdb->prepare($query, "%".$args['search']."%");
366 $results = $wpdb->get_results($sql_query, ARRAY_A );
367 if(!empty($results)) {
368 foreach ($results AS $row) {
369 $content = $this->findAndReplaceContent($row['post_content'], $args['search'], $args['replace']);
370 $update_post_query = $wpdb->prepare($update_query, $content, $row['ID']);
371 $result = $wpdb->query($update_post_query);
372 }
373 }
374 }
375 }
376 }
377 }
378
379 function checkInMetaData() {
380 $tables = array(
381 array(
382 'table_name' => 'usermeta',
383 'primary_key' => 'umeta_id',
384 'search_key' => 'meta_value'
385 ),
386 array(
387 'table_name' => 'termmeta',
388 'primary_key' => 'meta_id',
389 'search_key' => 'meta_value'
390 ),
391 array(
392 'table_name' => 'postmeta',
393 'primary_key' => 'meta_id',
394 'search_key' => 'meta_value'
395 ),
396 array(
397 'table_name' => 'commentmeta',
398 'primary_key' => 'meta_id',
399 'search_key' => 'meta_value'
400 )
401 );
402 global $wpdb;
403 foreach ($tables as $table) {
404 $post_table = $wpdb->prefix . $table['table_name'];
405 if (!empty($this->replace_items)) {
406 $query = "SELECT {$table['primary_key']}, {$table['search_key']} FROM {$post_table} WHERE {$table['search_key']} LIKE %s";
407 $update_query = "UPDATE {$post_table} SET {$table['search_key']} = %s WHERE {$table['primary_key']} = %d";
408 foreach ($this->replace_items as $args) {
409 if ($args['search'] != $args['replace']) {
410 $sql_query = $wpdb->prepare($query, "%" . $args['search'] . "%");
411 $results = $wpdb->get_results($sql_query, ARRAY_A);
412 if (!empty($results)) {
413 foreach ($results as $row) {
414 $content = $this->findAndReplaceContent($row[$table['search_key']], $args['search'], $args['replace']);
415 $update_post_query = $wpdb->prepare($update_query, $content, $row[$table['primary_key']]);
416 $result = $wpdb->query($update_post_query);
417 }
418 }
419 }
420 }
421 }
422 }
423 }
424
425 function findAndReplaceContent($content, $search, $replace, $in_deep = false) {
426 $content = maybe_unserialize($content);
427 $isJson = $this->isJSON($content);
428
429 if ($isJson) {
430 $content = json_decode($content);
431 }
432
433 if (is_string($content)) {
434 $content = str_replace($search, $replace, $content);
435 }
436 else if(is_wp_error($content)) {
437
438 }
439 else if(is_array($content)) {
440 foreach($content as $index => $value) {
441 $content[$index] = $this->findAndReplaceContent($value, $search, $replace, true);
442 if (is_string($index)) {
443 $index_replaced = $this->findAndReplaceContent($index, $search, $replace, true);
444 if ($index_replaced !== $index)
445 $content = $this->changeArrayKey($content, array($index => $index_replaced));
446 }
447 }
448 }
449 else if(is_object($content)) {
450 foreach($content as $key => $value) {
451 $content->{$key} = $this->findAndReplaceContent($value, $search, $replace, true);
452 }
453 }
454
455 if ($isJson && $in_deep === false) {
456 $content = json_encode($content, JSON_UNESCAPED_SLASHES);
457 }
458 else if($in_deep === false && (is_array($content) || is_object($content))) {
459 $content = maybe_serialize($content);
460 }
461
462 return $content;
463 }
464
465 function changeArrayKey($array, $set) {
466 if (is_array($array) && is_array($set)) {
467 $newArr = array();
468 foreach ($array as $k => $v) {
469 $key = array_key_exists( $k, $set) ? $set[$k] : $k;
470 $newArr[$key] = is_array($v) ? $this->changeArrayKey($v, $set) : $v;
471 }
472 return $newArr;
473 }
474 return $array;
475 }
476
477 function isJSON($content)
478 {
479 if (is_array($content) || is_object($content))
480 return false;
481
482 $json = json_decode($content);
483 return $json && $json != $content;
484 }
485 }
486 $folders_replace_media = new folders_replace_media();