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

See #6820.


git-svn-id: https://develop.svn.wordpress.org/trunk@31619 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor
2015-03-05 05:34:40 +00:00
parent 26875b75a3
commit 6c8322e8fa
8 changed files with 124 additions and 42 deletions

View File

@@ -2267,6 +2267,9 @@ function wp_ajax_save_attachment() {
if ( 'attachment' != $post['post_type'] )
wp_send_json_error();
if ( isset( $changes['parent'] ) )
$post['post_parent'] = $changes['parent'];
if ( isset( $changes['title'] ) )
$post['post_title'] = $changes['title'];

View File

@@ -139,6 +139,9 @@ class WP_Media_List_Table extends WP_List_Table {
if ( isset( $_REQUEST['found_post_id'] ) && isset( $_REQUEST['media'] ) )
return 'attach';
if ( isset( $_REQUEST['parent_post_id'] ) && isset( $_REQUEST['media'] ) )
return 'detach';
if ( isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) )
return 'delete_all';
@@ -406,7 +409,16 @@ foreach ( $columns as $column_name => $column_display_name ) {
} else {
echo $title;
} ?></strong>,
<?php echo get_the_time( __( 'Y/m/d' ) ); ?>
<?php echo get_the_time( __( 'Y/m/d' ) ); ?><br />
<?php
if ( $user_can_edit ):
$detach_url = add_query_arg( array(
'parent_post_id' => $post->post_parent,
'media[]' => $post->ID,
'_wpnonce' => wp_create_nonce( 'bulk-' . $this->_args['plural'] )
), 'upload.php' ); ?>
<a class="hide-if-no-js detach-from-parent" href="<?php echo $detach_url ?>"><?php _e( 'Detach' ); ?></a>
<?php endif; ?>
</td>
<?php
} else {

View File

@@ -3010,3 +3010,61 @@ function wp_read_audio_metadata( $file ) {
return $metadata;
}
/**
* Encapsulate logic for Attach/Detach actions
*
* @since 4.2.0
*
* @global wpdb $wpdb
* @param int $parent_id
* @param string $action
*/
function wp_media_attach_action( $parent_id, $action = 'attach' ) {
global $wpdb;
if ( ! $parent_id ) {
return;
}
if ( ! current_user_can( 'edit_post', $parent_id ) ) {
wp_die( __( 'You are not allowed to edit this post.' ) );
}
$ids = array();
foreach ( (array) $_REQUEST['media'] as $att_id ) {
$att_id = (int) $att_id;
if ( ! current_user_can( 'edit_post', $att_id ) ) {
continue;
}
$ids[] = $att_id;
}
if ( ! empty( $ids ) ) {
$ids_string = implode( ',', $ids );
if ( 'attach' === $action ) {
$result = $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_parent = %d WHERE post_type = 'attachment' AND ID IN ( $ids_string )", $parent_id ) );
} else {
$result = $wpdb->query( "UPDATE $wpdb->posts SET post_parent = 0 WHERE post_type = 'attachment' AND ID IN ( $ids_string )" );
}
foreach ( $ids as $att_id ) {
clean_attachment_cache( $att_id );
}
}
if ( isset( $result ) ) {
$location = 'upload.php';
if ( $referer = wp_get_referer() ) {
if ( false !== strpos( $referer, 'upload.php' ) ) {
$location = remove_query_arg( array( 'attached', 'detached' ), $referer );
}
}
$key = 'attach' === $action ? 'attached' : 'detached';
$location = add_query_arg( array( $key => $result ), $location );
wp_redirect( $location );
exit;
}
}