Permalink
Browse files

Add bulk media actions for detach and regenerate.

This is a bit of a kludge since–as of this commit–there's no proper way
to add bulk actions to the Media page. We can fake it with a bit of
javascript, however.
  • Loading branch information...
mgsisk committed Feb 4, 2013
1 parent 5a5af56 commit 1479c847e37288f060efbdc1f3b2c454d87ef135
Showing with 109 additions and 4 deletions.
  1. +28 −0 -/js/admin-media.js
  2. +81 −4 -/php/media.php
View
@@ -42,4 +42,32 @@ jQuery( function( $ ) {
} );
}
} );
+
+ /** Add regenerate and detach bulk actions. */
+ $( 'body.wp-admin.upload-php [name="action"],body.wp-admin.upload-php [name="action2"]' ).append( '<option value="webcomic_regenerate">'
+ + $( '[data-webcomic-regenerate]' ).data( 'webcomic-regenerate' )
+ + '</option><option value="webcomic_detach">'
+ + $( '[data-webcomic-detach]' ).data( 'webcomic-detach' )
+ + '</option>'
+ );
+
+ $( 'body.wp-admin.upload-php #doaction' ).on( 'click', function( e ) {
+ if ( 'webcomic_regenerate' === $( '[name="action"]' ).val() || 'webcomic_detach' === $( '[name="action"]' ).val() ) {
+ webcomic_bulk_action( $( '[name="action"]' ) );
+ }
+ } );
+
+ $( 'body.wp-admin.upload-php #doaction2' ).on( 'click', function( e ) {
+ if ( 'webcomic_regenerate' === $( '[name="action2"]' ).val() || 'webcomic_detach' === $( '[name="action2"]' ).val() ) {
+ webcomic_bulk_action( $( '[name="action2"]' ) );
+ }
+ } );
+
+ function webcomic_bulk_action( $el ) {
+ var $form = $el.parents( 'form' );
+
+ $el.attr( 'name', 'webcomic_action' );
+ $form.attr( 'method', 'post' );
+ $form.attr( 'action', $( '[data-webcomic-admin-url]' ).data( 'webcomic-admin-url' ) );
+ }
} );
View
@@ -15,6 +15,7 @@ class WebcomicMedia extends Webcomic {
* @uses Webcomic::$notice
* @uses WebcomicMedia::admin_init()
* @uses WebcomicMedia::admin_menu()
+ * @uses WebcomicMedia::admin_foot()
* @uses WebcomicMedia::delete_attachment()
* @uses WebcomicMedia::admin_enqueue_scripts()
* @uses WebcomicMedia::media_upload_webcomic_media()
@@ -27,6 +28,7 @@ class WebcomicMedia extends Webcomic {
public function __construct() {
add_action( 'admin_init', array( $this, 'admin_init' ) );
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
+ add_action( 'admin_footer', array( $this, 'admin_footer' ) );
add_action( 'delete_attachment', array( $this, 'delete_attachment' ), 10, 1 );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
add_action( 'media_upload_webcomic_media', array( $this, 'media_upload_webcomic_media' ) );
@@ -46,6 +48,8 @@ public function __construct() {
* @hook admin_init
*/
public function admin_init() {
+ global $wpdb;
+
if ( isset( $_GET[ 'webcomic_action' ], $_GET[ 'post' ] ) and 'regenerate' === $_GET[ 'webcomic_action' ] and $meta = wp_get_attachment_metadata( $_GET[ 'post' ] ) ) {
check_admin_referer( 'webcomic_regenerate' );
@@ -62,20 +66,77 @@ public function admin_init() {
} else {
wp_update_attachment_metadata( $_GET[ 'post' ], $meta );
- self::$notice[] = __( 'Regenerated images for media.', 'webcomic' );
+ self::$notice[] = __( 'Regenerated images for media attachments.', 'webcomic' );
}
}
if ( isset( $_GET[ 'webcomic_action' ], $_GET[ 'post' ] ) and 'detach' === $_GET[ 'webcomic_action' ] and intval( $_GET[ 'post' ] ) ) {
check_admin_referer( 'webcomic_detach' );
- if ( wp_update_post( array( 'ID' => ( integer ) $_GET[ 'post' ], 'post_type' => 'attachment', 'post_parent' => 0, 'menu_order' => 0 ) ) ) {
+ if ( wp_update_post( array( 'ID' => ( integer ) $_GET[ 'post' ], 'post_parent' => 0 ) ) ) {
self::$notice[] = __( 'Detached media.', 'webcomic' );
} else {
self::$error[] = __( 'Media could not be detached.', 'webcomic' );
}
}
+ if ( isset( $_POST[ 'webcomic_action' ] ) and 'webcomic_regenerate' === $_POST[ 'webcomic_action' ] and !empty( $_POST[ 'media' ] ) ) {
+ check_admin_referer( 'bulk-media' );
+
+ $dir = wp_upload_dir();
+ $good = $bad = 0;
+ $sizes = get_intermediate_image_sizes();
+
+ foreach ( $_POST[ 'media' ] as $id ) {
+ if ( false !== strpos( get_post_mime_type( $id ), 'image' ) ) {
+ foreach ( $sizes as $size ) {
+ if ( $file = image_get_intermediate_size( $id, $size ) ) {
+ @unlink( path_join( $dir[ 'basedir' ], $file[ 'path' ] ) );
+ }
+ }
+
+ if ( is_wp_error( $meta = wp_generate_attachment_metadata( $id, get_attached_file( $id ) ) ) ) {
+ $bad++;
+ } else {
+ wp_update_attachment_metadata( $id, $meta );
+
+ $good++;
+ }
+ }
+ }
+
+ if ( $bad ) {
+ self::$error[] = sprintf( _n( 'Images could not be regenerated for %s media attachment.', 'Images could not be regenerated for %s media attachments.', $bad, 'webcomic' ), $bad );
+ }
+
+ if ( $good ) {
+ self::$notice[] = sprintf( _n( 'Images regenerated for %s media attachment.', 'Images regenerated for %s media attachments.', $good, 'webcomic' ), $good );
+ }
+ }
+
+ if ( isset( $_POST[ 'webcomic_action' ] ) and 'webcomic_detach' === $_POST[ 'webcomic_action' ] and !empty( $_POST[ 'media' ] ) ) {
+ check_admin_referer( 'bulk-media' );
+ $good = $bad = 0;
+
+ foreach ( $_POST[ 'media' ] as $id ) {
+ if ( $wpdb->get_var( $wpdb->prepare( "SELECT post_parent FROM {$wpdb->posts} WHERE ID = %s", $id ) ) ) {
+ if ( wp_update_post( array( 'ID' => ( integer ) $id, 'post_parent' => 0 ) ) ) {
+ $good++;
+ } else {
+ $bad++;
+ }
+ }
+ }
+
+ if ( $bad ) {
+ self::$error[] = sprintf( _n( 'Could not detach %s attachment.', 'Could not detach %s attachments.', $bad, 'webcomic' ), $bad );
+ }
+
+ if ( $good ) {
+ self::$notice[] = sprintf( _n( 'Detached %s attachment.', 'Detached %s attachments.', $good, 'webcomic' ), $good );
+ }
+ }
+
if ( isset( $_POST[ 'webcomic_action' ] ) and 'attach' === $_POST[ 'webcomic_action' ] and wp_verify_nonce( $_POST[ 'webcomic_attach' ], 'webcomic_attach' ) ) {
if ( empty( $_POST[ 'matches' ] ) ) {
self::$error[] = __( 'No matches selected.', 'webcomic' );
@@ -181,6 +242,22 @@ public function admin_menu() {
add_submenu_page( 'upload.php', __( 'Webcomic Generator', 'webcomic' ), __( 'Webcomic Generator', 'webcomic' ), 'upload_files', 'webcomic-generator', array( $this, 'page_generator' ) );
}
+ /** Render HTML data for bulk media actions.
+ *
+ * @hook admin_footer
+ */
+ public function admin_footer() {
+ $screen = get_current_screen();
+
+ if ( 'upload' === $screen->id ) {
+ printf( '<span data-webcomic-admin-url="%s" data-webcomic-regenerate="%s" data-webcomic-detach="%s"></span>',
+ admin_url( 'upload.php' ),
+ esc_attr( __( 'Regenerate Images', 'webcomic' ) ),
+ esc_attr( __( 'Detach', 'webcomic' ) )
+ );
+ }
+ }
+
/** Manage webcomic media from the modal media manager.
*
* @hook media_upload_webcomic_media
@@ -245,7 +322,7 @@ public function delete_attachment( $id ) {
public function admin_enqueue_scripts() {
$screen = get_current_screen();
- if ( 'media_page_webcomic-generator' === $screen->id or ( isset( $_GET[ 'tab' ] ) and 'webcomic_media' === $_GET[ 'tab' ] ) ) {
+ if ( 'upload' === $screen->id or 'media_page_webcomic-generator' === $screen->id or ( isset( $_GET[ 'tab' ] ) and 'webcomic_media' === $_GET[ 'tab' ] ) ) {
wp_register_script( 'webcomic-admin-media', self::$url . '-/js/admin-media.js', array( 'jquery-ui-sortable' ) );
wp_enqueue_script( 'webcomic-admin-media' );
@@ -494,7 +571,7 @@ public function page_attacher() {
<?php
if ( !$attachments or is_wp_error( $attachments ) ) {
_e( 'No unattached media could be found.', 'webcomic' );
- } else if ( !$posts ) {
+ } elseif ( !$posts ) {
_e( 'No orphaned webcomic posts could be found.', 'webcomic' );
} else {
_e( 'No matches could be found.', 'webcomic' );

0 comments on commit 1479c84

Please sign in to comment.