Ticket #6820: detach_media.diff
File detach_media.diff, 5.8 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 $detached = $wpdb->update( $wpdb->posts, array( 'post_parent' => 0 ), array( 'post_type' => 'attachment', 'ID' => $post_id ) ); 1475 $html = _( '(Unattached)' ) . "<br />\n" . 1476 "<a class=\"hide-if-no-js\" onclick=\"findPosts.open( 'media[]','$post_id' );return false;\" href=\"#the-list\">" . _( 'Attach' ) . "</a>"; 1477 $x = new WP_Ajax_Response(); 1478 $x->add( array( 1479 'id' => $post_id, 1480 'data' => $html 1481 )); 1482 $x->send(); 1483 break; 1470 1484 default : 1471 1485 do_action( 'wp_ajax_' . $_POST['action'] ); 1472 1486 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" onclick="detachMedia.detach(<?php echo $post->ID; ?>);return false;" href="#the-list"><?php _e( 'Detach' ); ?></a> 293 295 </td> 294 296 <?php 295 297 } else { -
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) { … … 72 72 } 73 73 }; 74 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); }, 87 error : function(r) { detachMedia.error(r); } 88 }); 89 }, 90 91 show : function(x) { 92 93 if ( typeof(x) == 'string' ) { 94 this.error({'responseText': x}); 95 return; 96 } 97 98 var r = wpAjax.parseAjaxResponse(x); 99 if ( r.errors ) { 100 this.error({'responseText': wpAjax.broken}); 101 } 102 r = r.responses[0]; 103 $('#post-' + r.id + ' td.parent').html(r.data); 104 105 var count = $('.subsubsub .detached .count').html(); 106 count = parseInt(count.substr(1, count.length - 2), 10); 107 $('.subsubsub .detached .count').html('(' + (count + 1) + ')'); 108 109 if ($('#message').length > 0) { 110 $('#message').html('<p>Detached 1 media file.</p>'); 111 } else { 112 $('h2').after('<div id="message" class="updated below-h2"><p>Detached 1 media file.</p></div>'); 113 } 114 }, 115 116 error : function(r) { 117 var er = r.statusText; 118 119 if ( r.responseText ) { 120 er = r.responseText.replace( /<.[^<>]*?>/g, '' ); 121 } 122 if ( er ) { 123 $('#find-posts-response').html(er); 124 } 125 } 126 }; 127 75 128 $(document).ready(function() { 76 129 $('#find-posts-submit').click(function(e) { 77 130 if ( '' == $('#find-posts-response').html() ) -
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']);