WordPress.org

Make WordPress Core


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

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

See #6820.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.