Ticket #6820: detach_media2.diff
File detach_media2.diff, 6.6 KB (added by , 14 years ago) |
---|
-
wp-admin/admin-ajax.php
1467 1467 case 'time_format' : 1468 1468 die( date_i18n( sanitize_option( 'time_format', $_POST['date'] ) ) ); 1469 1469 break; 1470 case 'detach_media' : 1471 $post_id = intval( $_POST['post_id'] ); 1472 if ( !current_user_can( 'edit_post', $post_id ) ) 1473 die( '-1' ); 1474 $parent_id = intval( $wpdb->get_var( $wpdb->prepare( "SELECT post_parent FROM $wpdb->posts WHERE post_type = 'attachment' AND ID = %d", $post_id ) ) ); 1475 if ( !current_user_can( 'edit_post', $parent_id ) ) 1476 die( '-1' ); 1477 $detached = $wpdb->update( $wpdb->posts, array( 'post_parent' => 0 ), array( 'post_type' => 'attachment', 'ID' => $post_id ) ); 1478 die($detached); 1479 break; 1470 1480 default : 1471 1481 do_action( 'wp_ajax_' . $_POST['action'] ); 1472 1482 die('0'); -
wp-admin/includes/class-wp-media-list-table.php
79 79 $actions['delete'] = __( 'Delete Permanently' ); 80 80 if ( $this->detached ) 81 81 $actions['attach'] = __( 'Attach to a post' ); 82 82 else 83 $actions['detach'] = __( 'Detach media' ); 83 84 return $actions; 84 85 } 85 86 … … 289 290 ?> 290 291 <td <?php echo $attributes ?>> 291 292 <strong><a href="<?php echo get_edit_post_link( $post->post_parent ); ?>"><?php echo $title ?></a></strong>, 292 <?php echo get_the_time( __( 'Y/m/d' ) ); ?> 293 <?php echo get_the_time( __( 'Y/m/d' ) ); ?><br /> 294 <a class="hide-if-no-js detach-media" href="#the-list"><?php _e( 'Detach' ); ?></a> 293 295 </td> 294 296 <?php 295 297 } else { 296 298 ?> 297 299 <td <?php echo $attributes ?>><?php _e( '(Unattached)' ); ?><br /> 298 <a class="hide-if-no-js " onclick="findPosts.open( 'media[]','<?php echo $post->ID ?>' );return false;" href="#the-list"><?php _e( 'Attach' ); ?></a></td>300 <a class="hide-if-no-js attach-media" href="#the-list"><?php _e( 'Attach' ); ?></a></td> 299 301 <?php 300 302 } 301 303 break; -
wp-admin/js/media.dev.js
1 1 2 var findPosts ;2 var findPosts, detachMedia; 3 3 (function($){ 4 4 findPosts = { 5 5 open : function(af_name, af_val) { … … 71 71 } 72 72 } 73 73 }; 74 75 detachMedia = { 76 detach : function(id) { 77 var post = { 78 post_id: id, 79 action: 'detach_media', 80 _ajax_nonce: $('#_ajax_nonce').val() 81 }; 82 $.ajax({ 83 type : 'POST', 84 url : ajaxurl, 85 data : post, 86 success : function(x) { detachMedia.show(x, id); }, 87 error : function(r) { detachMedia.error(r); } 88 }); 89 }, 74 90 91 show : function(x, id) { 92 93 if ( x == '-1' ) { 94 this.showMessage("Sorry you don't have permission to detach that media file.", true); 95 return; 96 } 97 98 $('#post-' + id + ' td.parent').html('(Unattached)<br /><a href="#the-list" class="hide-if-no-js attach-media">Attach</a>'); 99 100 var count = $('.subsubsub .detached .count').html(); 101 count = parseInt(count.substr(1, count.length - 2), 10); 102 $('.subsubsub .detached .count').html('(' + (count + 1) + ')'); 103 104 this.showMessage('Detached 1 media file.'); 105 }, 106 107 showMessage: function(message, isError) { 108 message = message.replace( /<.[^<>]*?>/g, '' ); 109 var className = 'below-h2 ' + ((isError) ? 'error' : 'updated'); 110 if ($('#message').length > 0) { 111 $('message').attr('class', className); 112 $('#message').html('<p>' + message + '</p>'); 113 } else { 114 $('h2').after('<div id="message" class="' + className + '"><p>' + message + '</p></div>'); 115 } 116 } 117 118 }; 119 75 120 $(document).ready(function() { 76 121 $('#find-posts-submit').click(function(e) { 77 122 if ( '' == $('#find-posts-response').html() ) … … 93 138 } 94 139 }); 95 140 }); 141 $('#the-list').delegate('.attach-media', 'click', function(e) { 142 var idMatch = $(this).parents('tr').attr('id').match(/post-(\d)/); 143 if ( idMatch ) { 144 findPosts.open('media[]', idMatch[1]); 145 e.preventDefault(); 146 } 147 }); 148 149 $('#the-list').delegate('.detach-media', 'click', function(e) { 150 var idMatch = $(this).parents('tr').attr('id').match(/post-(\d)/); 151 if ( idMatch ) { 152 detachMedia.detach(idMatch[1]); 153 e.preventDefault(); 154 } 155 }); 96 156 }); 97 157 })(jQuery); -
wp-admin/upload.php
89 89 exit; 90 90 } 91 91 break; 92 case 'detach': 93 $detach = array(); 94 foreach ( (array) $_REQUEST['media'] as $att_id ) { 95 $att_id = (int) $att_id; 96 97 if ( !current_user_can( 'edit_post', $att_id ) ) 98 continue; 99 100 $detach[] = $att_id; 101 clean_attachment_cache( $att_id ); 102 } 103 104 if ( ! empty( $detach ) ) { 105 $parent_ids = $wpdb->get_col( $wpdb->prepare( "SELECT post_parent FROM $wpdb->posts WHERE post_type = 'attachment' AND ID IN ( $detach )" ) ); 106 foreach ( $parent_ids as $parent_id ) { 107 if ( !current_user_can( 'edit_post', $parent_id ) ) 108 wp_die( __( 'You are not allowed to edit this post.' ) ); 109 } 110 111 $detach = implode( ',', $detach ); 112 $detached = $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_parent = 0 WHERE post_type = 'attachment' AND ID IN ( $detach )" ) ); 113 } 114 115 if ( isset( $detached ) ) { 116 $location = 'upload.php'; 117 if ( $referer = wp_get_referer() ) { 118 if ( false !== strpos( $referer, 'upload.php' ) ) 119 $location = $referer; 120 } 121 122 $location = add_query_arg( array( 'detached_media' => $detached ) , $location ); 123 wp_redirect( $location ); 124 exit; 125 } 126 break; 92 127 case 'trash': 93 128 foreach ( (array) $post_ids as $post_id ) { 94 129 if ( !current_user_can( 'delete_post', $post_id ) ) … … 178 213 $_SERVER['REQUEST_URI'] = remove_query_arg(array('attached'), $_SERVER['REQUEST_URI']); 179 214 } 180 215 216 if ( isset($_GET['detached_media']) && (int) $_GET['detached_media'] ) { 217 $detached = (int) $_GET['detached_media']; 218 $message = sprintf( _n('Detached %d media file.', 'Detached %d media files.', $detached), $detached ); 219 $_SERVER['REQUEST_URI'] = remove_query_arg(array('detached_media'), $_SERVER['REQUEST_URI']); 220 } 221 181 222 if ( isset($_GET['deleted']) && (int) $_GET['deleted'] ) { 182 223 $message = sprintf( _n( 'Media attachment permanently deleted.', '%d media attachments permanently deleted.', $_GET['deleted'] ), number_format_i18n( $_GET['deleted'] ) ); 183 224 $_SERVER['REQUEST_URI'] = remove_query_arg(array('deleted'), $_SERVER['REQUEST_URI']);