Make WordPress Core

Changeset 31619


Ignore:
Timestamp:
03/05/2015 05:34:40 AM (10 years ago)
Author:
wonderboymusic
Message:

Allow attachments to be Detached from their parent in media grid and list modes.

See #6820.

Location:
trunk/src
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/ajax-actions.php

    r31534 r31619  
    22682268        wp_send_json_error();
    22692269
     2270    if ( isset( $changes['parent'] ) )
     2271        $post['post_parent'] = $changes['parent'];
     2272
    22702273    if ( isset( $changes['title'] ) )
    22712274        $post['post_title'] = $changes['title'];
  • trunk/src/wp-admin/includes/class-wp-media-list-table.php

    r31220 r31619  
    139139        if ( isset( $_REQUEST['found_post_id'] ) && isset( $_REQUEST['media'] ) )
    140140            return 'attach';
     141
     142        if ( isset( $_REQUEST['parent_post_id'] ) && isset( $_REQUEST['media'] ) )
     143            return 'detach';
    141144
    142145        if ( isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) )
     
    407410                    echo $title;
    408411                } ?></strong>,
    409                 <?php echo get_the_time( __( 'Y/m/d' ) ); ?>
     412                <?php echo get_the_time( __( 'Y/m/d' ) ); ?><br />
     413                <?php
     414                if ( $user_can_edit ):
     415                    $detach_url = add_query_arg( array(
     416                        'parent_post_id' => $post->post_parent,
     417                        'media[]' => $post->ID,
     418                        '_wpnonce' => wp_create_nonce( 'bulk-' . $this->_args['plural'] )
     419                    ), 'upload.php' ); ?>
     420                <a class="hide-if-no-js detach-from-parent" href="<?php echo $detach_url ?>"><?php _e( 'Detach' ); ?></a>
     421                <?php endif; ?>
    410422            </td>
    411423<?php
  • trunk/src/wp-admin/includes/media.php

    r31440 r31619  
    30113011    return $metadata;
    30123012}
     3013
     3014/**
     3015 * Encapsulate logic for Attach/Detach actions
     3016 *
     3017 * @since 4.2.0
     3018 *
     3019 * @global wpdb $wpdb
     3020 * @param int    $parent_id
     3021 * @param string $action
     3022 */
     3023function wp_media_attach_action( $parent_id, $action = 'attach' ) {
     3024    global $wpdb;
     3025
     3026    if ( ! $parent_id ) {
     3027        return;
     3028    }
     3029
     3030    if ( ! current_user_can( 'edit_post', $parent_id ) ) {
     3031        wp_die( __( 'You are not allowed to edit this post.' ) );
     3032    }
     3033    $ids = array();
     3034    foreach ( (array) $_REQUEST['media'] as $att_id ) {
     3035        $att_id = (int) $att_id;
     3036
     3037        if ( ! current_user_can( 'edit_post', $att_id ) ) {
     3038            continue;
     3039        }
     3040
     3041        $ids[] = $att_id;
     3042    }
     3043
     3044    if ( ! empty( $ids ) ) {
     3045        $ids_string = implode( ',', $ids );
     3046        if ( 'attach' === $action ) {
     3047            $result = $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_parent = %d WHERE post_type = 'attachment' AND ID IN ( $ids_string )", $parent_id ) );
     3048        } else {
     3049            $result = $wpdb->query( "UPDATE $wpdb->posts SET post_parent = 0 WHERE post_type = 'attachment' AND ID IN ( $ids_string )" );
     3050        }
     3051
     3052        foreach ( $ids as $att_id ) {
     3053            clean_attachment_cache( $att_id );
     3054        }
     3055    }
     3056
     3057    if ( isset( $result ) ) {
     3058        $location = 'upload.php';
     3059        if ( $referer = wp_get_referer() ) {
     3060            if ( false !== strpos( $referer, 'upload.php' ) ) {
     3061                $location = remove_query_arg( array( 'attached', 'detached' ), $referer );
     3062            }
     3063        }
     3064
     3065        $key = 'attach' === $action ? 'attached' : 'detached';
     3066        $location = add_query_arg( array( $key => $result ), $location );
     3067        wp_redirect( $location );
     3068        exit;
     3069    }
     3070}
  • trunk/src/wp-admin/upload.php

    r31562 r31619  
    114114
    115115    switch ( $doaction ) {
     116        case 'detach':
     117            wp_media_attach_action( $_REQUEST['parent_post_id'], 'detach' );
     118            break;
     119
    116120        case 'attach':
    117             $parent_id = (int) $_REQUEST['found_post_id'];
    118             if ( !$parent_id )
    119                 return;
    120 
    121             $parent = get_post( $parent_id );
    122             if ( !current_user_can( 'edit_post', $parent_id ) )
    123                 wp_die( __( 'You are not allowed to edit this post.' ) );
    124 
    125             $attach = array();
    126             foreach ( (array) $_REQUEST['media'] as $att_id ) {
    127                 $att_id = (int) $att_id;
    128 
    129                 if ( !current_user_can( 'edit_post', $att_id ) )
    130                     continue;
    131 
    132                 $attach[] = $att_id;
    133             }
    134 
    135             if ( ! empty( $attach ) ) {
    136                 $attach_string = implode( ',', $attach );
    137                 $attached = $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_parent = %d WHERE post_type = 'attachment' AND ID IN ( $attach_string )", $parent_id ) );
    138                 foreach ( $attach as $att_id ) {
    139                     clean_attachment_cache( $att_id );
    140                 }
    141             }
    142 
    143             if ( isset( $attached ) ) {
    144                 $location = 'upload.php';
    145                 if ( $referer = wp_get_referer() ) {
    146                     if ( false !== strpos( $referer, 'upload.php' ) )
    147                         $location = $referer;
    148                 }
    149 
    150                 $location = add_query_arg( array( 'attached' => $attached ) , $location );
    151                 wp_redirect( $location );
    152                 exit;
    153             }
    154             break;
     121            wp_media_attach_action( $_REQUEST['found_post_id'] );
     122            break;
     123
    155124        case 'trash':
    156125            if ( !isset( $post_ids ) )
     
    257226if ( ! empty( $_GET['attached'] ) && $attached = absint( $_GET['attached'] ) ) {
    258227    $message = sprintf( _n('Reattached %d attachment.', 'Reattached %d attachments.', $attached), $attached );
    259     $_SERVER['REQUEST_URI'] = remove_query_arg(array('attached'), $_SERVER['REQUEST_URI']);
     228    $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'detached', 'attached' ), $_SERVER['REQUEST_URI'] );
     229}
     230
     231if ( ! empty( $_GET['detached'] ) && $detached = absint( $_GET['detached'] ) ) {
     232    $message = sprintf( _n( 'Detached %d attachment.', 'Detached %d attachments.', $detached ), $detached );
     233    $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'detached', 'attached' ), $_SERVER['REQUEST_URI'] );
    260234}
    261235
  • trunk/src/wp-includes/js/media/views.js

    r31618 r31619  
    26572657        } else {
    26582658            this.listenTo( this.model, 'change:percent', this.progress );
     2659            this.listenTo( this.model, 'change:parent', this.render );
    26592660        }
    26602661        this.listenTo( this.model, 'change:title', this._syncTitle );
     
    31963197        'click .edit-attachment':         'editAttachment',
    31973198        'click .refresh-attachment':      'refreshAttachment',
    3198         'keydown':                        'toggleSelectionHandler'
     3199        'keydown':                        'toggleSelectionHandler',
     3200        'click .detach-from-parent':      'detachFromParent'
    31993201    },
    32003202
     
    32953297            return;
    32963298        }
     3299    },
     3300
     3301    /**
     3302     * @param {Object} event
     3303     */
     3304    detachFromParent: function( event ) {
     3305        event.preventDefault();
     3306
     3307        this.model.save({
     3308            'parent' : 0,
     3309            'uploadedTo' : 0,
     3310            'uploadedToLink' : '',
     3311            'uploadedToTitle' : ''
     3312        });
    32973313    }
    32983314});
  • trunk/src/wp-includes/js/media/views/attachment.js

    r31491 r31619  
    5353        } else {
    5454            this.listenTo( this.model, 'change:percent', this.progress );
     55            this.listenTo( this.model, 'change:parent', this.render );
    5556        }
    5657        this.listenTo( this.model, 'change:title', this._syncTitle );
  • trunk/src/wp-includes/js/media/views/attachment/details.js

    r31491 r31619  
    3636        'click .edit-attachment':         'editAttachment',
    3737        'click .refresh-attachment':      'refreshAttachment',
    38         'keydown':                        'toggleSelectionHandler'
     38        'keydown':                        'toggleSelectionHandler',
     39        'click .detach-from-parent':      'detachFromParent'
    3940    },
    4041
     
    135136            return;
    136137        }
     138    },
     139
     140    /**
     141     * @param {Object} event
     142     */
     143    detachFromParent: function( event ) {
     144        event.preventDefault();
     145
     146        this.model.save({
     147            'parent' : 0,
     148            'uploadedTo' : 0,
     149            'uploadedToLink' : '',
     150            'uploadedToTitle' : ''
     151        });
    137152    }
    138153});
  • trunk/src/wp-includes/media-template.php

    r31546 r31619  
    418418                        <# } else { #>
    419419                            <span class="value">{{ data.uploadedToTitle }}</span>
     420                        <# } #>
     421                        <# if ( data.nonces.edit ) { #>
     422                        <a class="detach-from-parent" data-id="{{ data.id }}" href="#">(<?php _e( 'Detach' ); ?>)</a>
    420423                        <# } #>
    421424                    </label>
Note: See TracChangeset for help on using the changeset viewer.