Make WordPress Core

Ticket #6820: detach_media2.diff

File detach_media2.diff, 6.6 KB (added by dphiffer, 14 years ago)

Updated after getting some feedback from nacin

  • wp-admin/admin-ajax.php

     
    14671467case 'time_format' :
    14681468        die( date_i18n( sanitize_option( 'time_format', $_POST['date'] ) ) );
    14691469        break;
     1470case '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;
    14701480default :
    14711481        do_action( 'wp_ajax_' . $_POST['action'] );
    14721482        die('0');
  • wp-admin/includes/class-wp-media-list-table.php

     
    7979                $actions['delete'] = __( 'Delete Permanently' );
    8080                if ( $this->detached )
    8181                        $actions['attach'] = __( 'Attach to a post' );
    82 
     82                else
     83                        $actions['detach'] = __( 'Detach media' );
    8384                return $actions;
    8485        }
    8586
     
    289290?>
    290291                        <td <?php echo $attributes ?>>
    291292                                <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>
    293295                        </td>
    294296<?php
    295297                } else {
    296298?>
    297299                        <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>
    299301<?php
    300302                }
    301303                break;
  • wp-admin/js/media.dev.js

     
    11
    2 var findPosts;
     2var findPosts, detachMedia;
    33(function($){
    44        findPosts = {
    55                open : function(af_name, af_val) {
     
    7171                        }
    7272                }
    7373        };
     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                },
    7490
     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
    75120        $(document).ready(function() {
    76121                $('#find-posts-submit').click(function(e) {
    77122                        if ( '' == $('#find-posts-response').html() )
     
    93138                                }
    94139                        });
    95140                });
     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                });
    96156        });
    97157})(jQuery);
  • wp-admin/upload.php

     
    8989                                exit;
    9090                        }
    9191                        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;
    92127                case 'trash':
    93128                        foreach ( (array) $post_ids as $post_id ) {
    94129                                if ( !current_user_can( 'delete_post', $post_id ) )
     
    178213        $_SERVER['REQUEST_URI'] = remove_query_arg(array('attached'), $_SERVER['REQUEST_URI']);
    179214}
    180215
     216if ( 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
    181222if ( isset($_GET['deleted']) && (int) $_GET['deleted'] ) {
    182223        $message = sprintf( _n( 'Media attachment permanently deleted.', '%d media attachments permanently deleted.', $_GET['deleted'] ), number_format_i18n( $_GET['deleted'] ) );
    183224        $_SERVER['REQUEST_URI'] = remove_query_arg(array('deleted'), $_SERVER['REQUEST_URI']);