Enable Media Replace - Version 1.0

Version Description

Download this release

Release Info

Developer MungoBBQ
Plugin Icon 128x128 Enable Media Replace
Version 1.0
Comparing to
See all releases

Version 1.0

enable-media-replace-sv_SE.mo ADDED
Binary file
enable-media-replace-sv_SE.po ADDED
@@ -0,0 +1,95 @@
1
+ msgid ""
2
+ msgstr ""
3
+ "Project-Id-Version: enable-media-replace\n"
4
+ "Report-Msgid-Bugs-To: \n"
5
+ "POT-Creation-Date: 2009-05-29 13:47+0100\n"
6
+ "PO-Revision-Date: \n"
7
+ "Last-Translator: Måns Jonasson <mans@mansjonasson.se>\n"
8
+ "Language-Team: \n"
9
+ "MIME-Version: 1.0\n"
10
+ "Content-Type: text/plain; charset=UTF-8\n"
11
+ "Content-Transfer-Encoding: 8bit\n"
12
+ "X-Poedit-KeywordsList: __;_e\n"
13
+ "X-Poedit-Basepath: .\n"
14
+ "X-Poedit-SearchPath-0: .\n"
15
+
16
+ #: enable-media-replace.php:27
17
+ #: enable-media-replace_sv_SE.php:27
18
+ msgid "Replace media"
19
+ msgstr "Ersätt media"
20
+
21
+ #: enable-media-replace.php:27
22
+ #: enable-media-replace_sv_SE.php:27
23
+ msgid "Upload a new file"
24
+ msgstr "Ladda upp en ny fil"
25
+
26
+ #: enable-media-replace.php:27
27
+ #: enable-media-replace_sv_SE.php:27
28
+ msgid "To replace the current file, click the link and upload a replacement."
29
+ msgstr "För att ersätta den nuvarande filen, klicka på länken och ladda upp en ny fil."
30
+
31
+ #: popup.php:9
32
+ #: upload.php:9
33
+ msgid "You do not have permission to upload files."
34
+ msgstr "Du har inte tillåtelse att ladda upp filer."
35
+
36
+ #: popup.php:26
37
+ msgid "Replace media upload"
38
+ msgstr "Ladda upp ny fil"
39
+
40
+ #: popup.php:36
41
+ msgid "Replace Media Upload"
42
+ msgstr "Ladda upp ny fil"
43
+
44
+ #: popup.php:40
45
+ msgid "NOTE: You are about to replace the media file"
46
+ msgstr "OBS: Du är på väg att ersätta filen"
47
+
48
+ #: popup.php:40
49
+ msgid "There is no undo. Think about it!"
50
+ msgstr "Det finns inget sätt att ångra. Tänk efter först!"
51
+
52
+ #: popup.php:42
53
+ msgid "Choose a file to upload from your computer"
54
+ msgstr "Välj en fil att ladda upp från din dator"
55
+
56
+ #: popup.php:46
57
+ msgid "Select media replacement type:"
58
+ msgstr "Välj ersättningsform:"
59
+
60
+ #: popup.php:48
61
+ msgid "Just replace the file"
62
+ msgstr "Ersätt bara filen"
63
+
64
+ #: popup.php:49
65
+ msgid "Note: This option requires you to upload a file of the same type ("
66
+ msgstr "Detta val kräver att du laddar upp en fil av exakt samma typ ("
67
+
68
+ #: popup.php:49
69
+ msgid ") as the one you are replacing. The name of the attachment will stay the same ("
70
+ msgstr ") som den du ersätter. Namnet på filen kommer att behållas ("
71
+
72
+ #: popup.php:49
73
+ msgid ") no matter what the file you upload is called."
74
+ msgstr ") oavsett vad filen du laddar upp heter på din dator."
75
+
76
+ #: popup.php:51
77
+ msgid "Replace the file, use new file name and update all links"
78
+ msgstr "Ersätt filen, använd det nya filnamnet och uppdatera alla länkar"
79
+
80
+ #: popup.php:52
81
+ msgid "Note: If you check this option, the name and type of the file you are about to upload will replace the old file. All links pointing to the current file ("
82
+ msgstr "Om du klickar i den här rutan kommer den även namnet och typen på den nya filen du laddar upp helt att ersätta den gamla. Alla länkar som pekar på den gamla filen ("
83
+
84
+ #: popup.php:52
85
+ msgid ") will be updated to point to the new file name."
86
+ msgstr ") kommer att uppdateras så att de pekar på det nya filnamnet."
87
+
88
+ #: popup.php:54
89
+ msgid "Upload"
90
+ msgstr "Ladda upp"
91
+
92
+ #: popup.php:54
93
+ msgid "Cancel"
94
+ msgstr "Avbryt"
95
+
enable-media-replace.mo ADDED
Binary file
enable-media-replace.php ADDED
@@ -0,0 +1,35 @@
1
+ <?php
2
+ /*
3
+ Plugin Name: Enable Media Replace
4
+ Plugin URI: http://www.mansjonasson.se/enable-media-replace
5
+ Description: Enable replacing media files by uploading a new file in the "Edit Media" section of the WordPress Media Library.
6
+ Version: 1.0
7
+ Author: Måns Jonasson
8
+ Author URI: http://www.mansjonasson.se
9
+
10
+ Dual licensed under the MIT and GPL licenses:
11
+ http://www.opensource.org/licenses/mit-license.php
12
+ http://www.gnu.org/licenses/gpl.html
13
+
14
+ Developed for .SE (Stiftelsen för Internetinfrastruktur) - http://www.iis.se
15
+ */
16
+
17
+ add_action( 'init', 'enable_media_replace_init' );
18
+ add_filter('attachment_fields_to_edit', 'enable_media_replace', 10, 2);
19
+
20
+ // Initialize this plugin. Called by 'init' hook.
21
+ function enable_media_replace_init() {
22
+ load_plugin_textdomain( 'enable-media-replace', '/wp-content/plugins/enable-media-replace' );
23
+ }
24
+
25
+ function enable_media_replace( $form_fields, $post ) {
26
+ if ($_GET["attachment_id"]) {
27
+ $link = "href=\"#\" onclick=\"window.open('" . get_bloginfo("wpurl") . "/wp-content/plugins/enable-media-replace/popup.php?attachment_id={$_GET["attachment_id"]}', 'enable_media_replace_popup', 'width=500,height=500');\"";
28
+ $form_fields["enable-media-replace"] = array("label" => __("Replace media", "enable-media-replace"), "input" => "html", "html" => "<p><a $link>" . __("Upload a new file", "enable-media-replace") . "</a></p>", "helps" => __("To replace the current file, click the link and upload a replacement.", "enable-media-replace"));
29
+ }
30
+ return $form_fields;
31
+ }
32
+
33
+
34
+
35
+ ?>
enable-media-replace.po ADDED
@@ -0,0 +1,95 @@
1
+ msgid ""
2
+ msgstr ""
3
+ "Project-Id-Version: enable-media-replace\n"
4
+ "Report-Msgid-Bugs-To: \n"
5
+ "POT-Creation-Date: 2009-05-29 13:49+0100\n"
6
+ "PO-Revision-Date: \n"
7
+ "Last-Translator: Måns Jonasson <mans@mansjonasson.se>\n"
8
+ "Language-Team: \n"
9
+ "MIME-Version: 1.0\n"
10
+ "Content-Type: text/plain; charset=UTF-8\n"
11
+ "Content-Transfer-Encoding: 8bit\n"
12
+ "X-Poedit-KeywordsList: __;_e\n"
13
+ "X-Poedit-Basepath: .\n"
14
+ "X-Poedit-SearchPath-0: .\n"
15
+
16
+ #: enable-media-replace.php:27
17
+ #: enable-media-replace_sv_SE.php:27
18
+ msgid "Replace media"
19
+ msgstr ""
20
+
21
+ #: enable-media-replace.php:27
22
+ #: enable-media-replace_sv_SE.php:27
23
+ msgid "Upload a new file"
24
+ msgstr ""
25
+
26
+ #: enable-media-replace.php:27
27
+ #: enable-media-replace_sv_SE.php:27
28
+ msgid "To replace the current file, click the link and upload a replacement."
29
+ msgstr ""
30
+
31
+ #: popup.php:9
32
+ #: upload.php:9
33
+ msgid "You do not have permission to upload files."
34
+ msgstr ""
35
+
36
+ #: popup.php:26
37
+ msgid "Replace media upload"
38
+ msgstr ""
39
+
40
+ #: popup.php:36
41
+ msgid "Replace Media Upload"
42
+ msgstr ""
43
+
44
+ #: popup.php:40
45
+ msgid "NOTE: You are about to replace the media file"
46
+ msgstr ""
47
+
48
+ #: popup.php:40
49
+ msgid "There is no undo. Think about it!"
50
+ msgstr ""
51
+
52
+ #: popup.php:42
53
+ msgid "Choose a file to upload from your computer"
54
+ msgstr ""
55
+
56
+ #: popup.php:46
57
+ msgid "Select media replacement type:"
58
+ msgstr ""
59
+
60
+ #: popup.php:48
61
+ msgid "Just replace the file"
62
+ msgstr ""
63
+
64
+ #: popup.php:49
65
+ msgid "Note: This option requires you to upload a file of the same type ("
66
+ msgstr ""
67
+
68
+ #: popup.php:49
69
+ msgid ") as the one you are replacing. The name of the attachment will stay the same ("
70
+ msgstr ""
71
+
72
+ #: popup.php:49
73
+ msgid ") no matter what the file you upload is called."
74
+ msgstr ""
75
+
76
+ #: popup.php:51
77
+ msgid "Replace the file, use new file name and update all links"
78
+ msgstr ""
79
+
80
+ #: popup.php:52
81
+ msgid "Note: If you check this option, the name and type of the file you are about to upload will replace the old file. All links pointing to the current file ("
82
+ msgstr ""
83
+
84
+ #: popup.php:52
85
+ msgid ") will be updated to point to the new file name."
86
+ msgstr ""
87
+
88
+ #: popup.php:54
89
+ msgid "Upload"
90
+ msgstr ""
91
+
92
+ #: popup.php:54
93
+ msgid "Cancel"
94
+ msgstr ""
95
+
popup.php ADDED
@@ -0,0 +1,59 @@
1
+ <?php
2
+
3
+ $wppath = str_replace("wp-content/plugins/enable-media-replace/popup.php", "", __FILE__);
4
+
5
+ require_once($wppath . "wp-load.php");
6
+ require_once($wppath . "wp-admin/admin.php");
7
+
8
+ if (!current_user_can('upload_files'))
9
+ wp_die(__('You do not have permission to upload files.', 'enable-media-replace'));
10
+
11
+ @header('Content-Type: ' . get_option('html_type') . '; charset=' . get_option('blog_charset'));
12
+
13
+ global $wpdb;
14
+
15
+ $table_name = $wpdb->prefix . "posts";
16
+
17
+ $sql = "SELECT guid, post_mime_type FROM $table_name WHERE ID = {$_GET["attachment_id"]}";
18
+
19
+ list($current_filename, $current_filetype) = mysql_fetch_array(mysql_query($sql));
20
+
21
+ $current_filename = substr($current_filename, (strrpos($current_filename, "/") + 1));
22
+
23
+
24
+ ?><html>
25
+ <head>
26
+ <title><?=__("Replace media upload")?></titl>
27
+
28
+ <link rel='stylesheet' href='<?=get_bloginfo("wpurl");?>/wp-admin/css/global.css?ver=20081210' type='text/css' media='all' />
29
+ <link rel='stylesheet' href='<?=get_bloginfo("wpurl");?>/wp-admin/wp-admin.css?ver=20081210' type='text/css' media='all' />
30
+ <link rel='stylesheet' href='<?=get_bloginfo("wpurl");?>/wp-admin/css/colors-fresh.css?ver=20081210' type='text/css' media='all' />
31
+ <link rel='stylesheet' href='<?=get_bloginfo("wpurl");?>/wp-admin/css/media.css?ver=20081210' type='text/css' media='all' />
32
+ </head>
33
+ <body id="media-upload">
34
+ <div class="wrap">
35
+ <div id="icon-upload" class="icon32"><br /></div>
36
+ <h2><?=__("Replace Media Upload", "enable-media-replace")?></h2>
37
+
38
+ <form enctype="multipart/form-data" method="post" action="<?=get_bloginfo("wpurl") . "/wp-content/plugins/enable-media-replace/upload.php"?>">
39
+ <input type="hidden" name="ID" value="<?=$_GET["attachment_id"]?>" />
40
+ <div id="message" class="updated fade"><p><?=__("NOTE: You are about to replace the media file", "enable-media-replace")?> "<?=$current_filename?>". <?=__("There is no undo. Think about it!", "enable-media-replace")?></p></div>
41
+
42
+ <p><?=__("Choose a file to upload from your computer", "enable-media-replace")?></p>
43
+
44
+ <input type="file" name="userfile" />
45
+
46
+ <p><?=__("Select media replacement type:", "enable-media-replace")?></p>
47
+
48
+ <label for="replace_type_1"><input CHECKED id="replace_type_1" type="radio" name="replace_type" value="replace"> <?=__("Just replace the file", "enable-media-replace")?></label>
49
+ <p class="howto"><?=__("Note: This option requires you to upload a file of the same type (", "enable-media-replace")?><?=$current_filetype?><?=__(") as the one you are replacing. The name of the attachment will stay the same (", "enable-media-replace")?><?=$current_filename?><?=__(") no matter what the file you upload is called.", "enable-media-replace")?></p>
50
+
51
+ <label for="replace_type_2"><input id="replace_type_2" type="radio" name="replace_type" value="replace_and_search"> <?=__("Replace the file, use new file name and update all links", "enable-media-replace")?></label>
52
+ <p class="howto"><?=__("Note: If you check this option, the name and type of the file you are about to upload will replace the old file. All links pointing to the current file (", "enable-media-replace")?><?=$current_filename?><?=__(") will be updated to point to the new file name.", "enable-media-replace")?></p>
53
+
54
+ <input type="submit" class="button" value="<?=__("Upload", "enable-media-replace")?>" /> <a href="#" onclick="window.close();"><?=__("Cancel", "enable-media-replace")?></a>
55
+
56
+ </form>
57
+ </div>
58
+ </body>
59
+ </html>
readme.txt ADDED
@@ -0,0 +1,53 @@
1
+ === Enable Media Replace ===
2
+ Contributors: mungobbq
3
+ Tags: admin, attachment, media, files
4
+ Requires at least: 2.7
5
+ Tested up to: 2.7.1
6
+ Stable tag: 1.0
7
+
8
+ Enables replacing attachment files by simply uploading a new file in the media library edit view.
9
+
10
+ == Description ==
11
+
12
+ Don't you find it tedious and complicated to have to first delete a file and then upload one with the exact same name every time you want to update an image or any uploaded file inside the WordPress media library?
13
+
14
+ Well, no longer!
15
+
16
+ Now you'll be able to replace any uploaded file from the media "edit" view, where it should be. Media replacement can be done in one of two ways:
17
+
18
+ 1. Just replace the file. This option requires you to upload a file of the same type as the one you are replacing. The name of the attachment will stay the same no matter what the file you upload is called.
19
+ 1. Replace the file, use new file name and update all links. If you check this option, the name and type of the file you are about to upload will replace the old file. All links pointing to the current file will be updated to point to the new file name.
20
+
21
+ This plugin is very powerful and a must-have for any larger sites built with WordPress.
22
+
23
+ Changes:
24
+
25
+ 1.0 First stable version of plugin.
26
+
27
+ == Installation ==
28
+
29
+ Quick and easy installation:
30
+
31
+ 1. Upload the folder `enable-media-replace` to the `/wp-content/plugins/` directory
32
+ 1. Activate the plugin through the 'Plugins' menu in WordPress
33
+ 1. Done!
34
+
35
+ == Frequently Asked Questions ==
36
+
37
+ = What does this plugin actually do? =
38
+
39
+ This plugin makes it easy to update/replace files that have been uploaded to the WordPress Media Library.
40
+
41
+ = How does it work? =
42
+
43
+ A new option will be available in the Edit Media view, called "Replace Media". Clicking it opens up a window where you can upload a new file to replace the old one.
44
+
45
+ == Screenshots ==
46
+
47
+ 1. The new link in the WordPress Edit Media view.
48
+ 2. The upload window.
49
+
50
+ == Wishlist / Coming attractons ==
51
+
52
+ Do you have suggestions? Feel free to contact me at mans@mansjonasson.se
53
+
screenshot-1.png ADDED
Binary file
screenshot-2.png ADDED
Binary file
upload.php ADDED
@@ -0,0 +1,109 @@
1
+ <?php
2
+
3
+ $wppath = str_replace("wp-content/plugins/enable-media-replace/upload.php", "", __FILE__);
4
+
5
+ require_once($wppath . "wp-load.php");
6
+ require_once($wppath . "wp-admin/admin.php");
7
+
8
+ if (!current_user_can('upload_files'))
9
+ wp_die(__('You do not have permission to upload files.'));
10
+
11
+ global $wpdb;
12
+
13
+ // Define DB table names
14
+ $table_name = $wpdb->prefix . "posts";
15
+ $postmeta_table_name = $wpdb->prefix . "postmeta";
16
+
17
+ // Get old guid and filetype from DB
18
+ $sql = "SELECT guid, post_mime_type FROM $table_name WHERE ID = {$_POST["ID"]}";
19
+ list($current_filename, $current_filetype) = mysql_fetch_array(mysql_query($sql));
20
+
21
+ // Massage a bunch of vars
22
+ $current_guid = $current_filename;
23
+ $current_filename = substr($current_filename, (strrpos($current_filename, "/") + 1));
24
+
25
+ $current_file = get_attached_file($_POST["ID"], true);
26
+ $current_path = substr($current_file, 0, (strrpos($current_file, "/")));
27
+ $current_file = str_replace("//", "/", $current_file);
28
+ $current_filename = basename($current_file);
29
+
30
+
31
+ $replace_type = $_POST["replace_type"];
32
+ // We have two types: replace / replace_and_search
33
+
34
+ if (is_uploaded_file($_FILES["userfile"]["tmp_name"])) {
35
+ $new_filename = $_FILES["userfile"]["name"];
36
+ $new_filetype = $_FILES["userfile"]["type"];
37
+ $new_filesize = $_FILES["userfile"]["size"];
38
+
39
+ if ($replace_type == "replace") {
40
+ // Drop-in replace and we don't even care if you uploaded something that is the wrong file-type.
41
+ // That's your own fault, because we warned you!
42
+
43
+ // Delete old file
44
+ unlink($current_file);
45
+
46
+ // Move new file to old location/name
47
+ move_uploaded_file($_FILES["userfile"]["tmp_name"], $current_file);
48
+
49
+ // Chmod new file to 644
50
+ chmod($current_file, 0644);
51
+
52
+ // Make thumb and/or update metadata
53
+ wp_update_attachment_metadata( $_POST["ID"], wp_generate_attachment_metadata( $_POST["ID"], $current_file ) );
54
+
55
+ }
56
+
57
+ else {
58
+ // Replace file, replace file name, update meta data, replace links pointing to old file name
59
+
60
+ // Delete old file
61
+ unlink($current_file);
62
+
63
+ // Massage new filename to adhere to WordPress standards
64
+ $new_filename= wp_unique_filename( $current_path, $new_filename );
65
+
66
+ // Move new file to old location, new name
67
+ $new_file = $current_path . "/" . $new_filename;
68
+ move_uploaded_file($_FILES["userfile"]["tmp_name"], $new_file);
69
+
70
+ // Chmod new file to 644
71
+ chmod($new_file, 0644);
72
+
73
+ $new_filetitle = preg_replace('/\.[^.]+#x2F;', '', basename($new_file));
74
+ $new_guid = str_replace($current_filename, $new_filename, $current_guid);
75
+
76
+ // Update database file name
77
+ mysql_query("UPDATE $table_name SET post_title = '$new_filetitle', post_name = '$new_filetitle', guid = '$new_guid', post_mime_type = '$new_filetype' WHERE ID = {$_POST["ID"]}");
78
+
79
+ // Update the postmeta file name
80
+
81
+ // Get old postmeta _wp_attached_file
82
+ $sql = "SELECT meta_value FROM $postmeta_table_name WHERE meta_key = '_wp_attached_file' AND post_id = '{$_POST["ID"]}'";
83
+ $old_meta_name = mysql_result(mysql_query($sql),0);
84
+
85
+ // Make new postmeta _wp_attached_file
86
+ $new_meta_name = str_replace($current_filename, $new_filename, $old_meta_name);
87
+ mysql_query("UPDATE $postmeta_table_name SET meta_value = '$new_meta_name' WHERE meta_key = '_wp_attached_file' AND post_id = '{$_POST["ID"]}'");
88
+
89
+ // Make thumb and/or update metadata
90
+ wp_update_attachment_metadata( $_POST["ID"], wp_generate_attachment_metadata( $_POST["ID"], $new_file) );
91
+
92
+ // Search-and-replace filename in post database
93
+ $sql = "SELECT ID, post_content FROM $table_name WHERE post_content LIKE '%$current_guid%'";
94
+ $rs = mysql_query($sql);
95
+
96
+ while($rows = mysql_fetch_assoc($rs)) {
97
+
98
+ // replace old guid with new guid
99
+ $post_content = $rows["post_content"];
100
+ $post_content = addslashes(str_replace($current_guid, $new_guid, $post_content));
101
+
102
+ mysql_query("UPDATE $table_name SET post_content = '$post_content' WHERE ID = {$rows["ID"]}");
103
+ }
104
+
105
+ }
106
+
107
+ }
108
+
109
+ ?><html><head></head><body><script type="text/javascript">window.close();window.opener.location.reload();</script></body></html>