Skip to content

Instantly share code, notes, and snippets.

@benhuson
Created September 24, 2015 06:51
Show Gist options
  • Save benhuson/22ee35c59793ed6d5f39 to your computer and use it in GitHub Desktop.
Save benhuson/22ee35c59793ed6d5f39 to your computer and use it in GitHub Desktop.
Index: trunk/taxonomy-images.php
===================================================================
--- trunk/taxonomy-images.php (revision 752123)
+++ trunk/taxonomy-images.php (working copy)
@@ -798,10 +798,13 @@
$term = get_term( $term_id, $taxonomy->name );
+ $nonce = wp_create_nonce( 'taxonomy-image-plugin-create-association' );
+ $nonce_remove = wp_create_nonce( 'taxonomy-image-plugin-remove-association' );
+
$o = "\n" . '<div id="' . esc_attr( 'taxonomy-image-control-' . $tt_id ) . '" class="taxonomy-image-control hide-if-no-js">';
- $o.= "\n" . '<a class="thickbox taxonomy-image-thumbnail" href="' . esc_url( admin_url( 'media-upload.php' ) . '?type=image&tab=library&post_id=0&TB_iframe=true' ) . '" title="' . esc_attr( sprintf( __( 'Associate an image with the %1$s named &#8220;%2$s&#8221;.', 'taxonomy-images' ), $name, $term->name ) ) . '"><img id="' . esc_attr( 'taxonomy_image_plugin_' . $tt_id ) . '" src="' . esc_url( $img ) . '" alt="" /></a>';
- $o.= "\n" . '<a class="control upload thickbox" href="' . esc_url( admin_url( 'media-upload.php' ) . '?type=image&tab=type&post_id=0&TB_iframe=true' ) . '" title="' . esc_attr( sprintf( __( 'Upload a new image for this %s.', 'taxonomy-images' ), $name ) ) . '">' . esc_html__( 'Upload.', 'taxonomy-images' ) . '</a>';
- $o.= "\n" . '<a class="control remove' . $hide . '" href="#" id="' . esc_attr( 'remove-' . $tt_id ) . '" rel="' . esc_attr( $tt_id ) . '" title="' . esc_attr( sprintf( __( 'Remove image from this %s.', 'taxonomy-images' ), $name ) ) . '">' . esc_html__( 'Delete', 'taxonomy-images' ) . '</a>';
+ $o.= "\n" . '<a class="thickbox taxonomy-image-thumbnail" data-tt-id="' . $tt_id . '" data-attachment-id="' . $attachment_id . '" data-nonce="' . $nonce . '" href="' . esc_url( admin_url( 'media-upload.php' ) . '?type=image&tab=library&post_id=0&TB_iframe=true' ) . '" title="' . esc_attr( sprintf( __( 'Associate an image with the %1$s named &#8220;%2$s&#8221;.', 'taxonomy-images' ), $name, $term->name ) ) . '"><img id="' . esc_attr( 'taxonomy_image_plugin_' . $tt_id ) . '" src="' . esc_url( $img ) . '" alt="" /></a>';
+ $o.= "\n" . '<a class="control upload thickbox" data-tt-id="' . $tt_id . '" data-attachment-id="' . $attachment_id . '" data-nonce="' . $nonce . '" href="' . esc_url( admin_url( 'media-upload.php' ) . '?type=image&tab=type&post_id=0&TB_iframe=true' ) . '" title="' . esc_attr( sprintf( __( 'Upload a new image for this %s.', 'taxonomy-images' ), $name ) ) . '">' . esc_html__( 'Upload.', 'taxonomy-images' ) . '</a>';
+ $o.= "\n" . '<a class="control remove' . $hide . '" data-tt-id="' . $tt_id . '" data-nonce="' . $nonce_remove . '" href="#" id="' . esc_attr( 'remove-' . $tt_id ) . '" rel="' . esc_attr( $tt_id ) . '" title="' . esc_attr( sprintf( __( 'Remove image from this %s.', 'taxonomy-images' ), $name ) ) . '">' . esc_html__( 'Delete', 'taxonomy-images' ) . '</a>';
$o.= "\n" . '<input type="hidden" class="tt_id" name="' . esc_attr( 'tt_id-' . $tt_id ) . '" value="' . esc_attr( $tt_id ) . '" />';
$o.= "\n" . '<input type="hidden" class="image_id" name="' . esc_attr( 'image_id-' . $tt_id ) . '" value="' . esc_attr( $attachment_id ) . '" />';
@@ -822,6 +825,11 @@
* @access private
*/
function taxonomy_image_plugin_media_upload_popup_js() {
+
+ if ( version_compare( get_bloginfo( 'version' ), 3.5 ) >= 0 ) {
+ return;
+ }
+
wp_enqueue_script(
'taxonomy-images-media-upload-popup',
taxonomy_image_plugin_url( 'media-upload-popup.js' ),
@@ -849,6 +857,10 @@
if ( false == taxonomy_image_plugin_is_screen_active() )
return;
+ if ( version_compare( get_bloginfo( 'version' ), 3.5 ) >= 0 ) {
+ return;
+ }
+
wp_enqueue_script(
'taxonomy-image-plugin-edit-tags',
taxonomy_image_plugin_url( 'edit-tags.js' ),
@@ -1182,3 +1194,34 @@
return $link;
}
+
+
+function taxonomy_images_admin_enqueue_scripts() {
+
+ if ( false == taxonomy_image_plugin_is_screen_active() ) {
+ return;
+ }
+
+ if ( version_compare( get_bloginfo( 'version' ), 3.5 ) < 0 ) {
+ return;
+ }
+
+ wp_enqueue_media();
+
+ wp_enqueue_script(
+ 'taxonomy-images-media-modal',
+ taxonomy_image_plugin_url( 'js/media-modal.js' ),
+ array( 'jquery' ),
+ taxonomy_image_plugin_version()
+ );
+
+ wp_localize_script( 'taxonomy-images-media-modal', 'TaxonomyImagesMediaModal', array(
+ 'wp_media_post_id' => 0,
+ 'attachment_id' => 0,
+ 'uploader_title' => __( 'Set Featured Image', 'taxonomy-images' ),
+ 'uploader_button_text' => __( 'Set featured image', 'taxonomy-images' ),
+ 'default_img_src' => taxonomy_image_plugin_url( 'default.png' )
+ ) );
+
+}
+add_action( 'admin_enqueue_scripts', 'taxonomy_images_admin_enqueue_scripts' );
Index: trunk/js/media-modal.js
===================================================================
--- trunk/js/media-modal.js (revision 0)
+++ trunk/js/media-modal.js (revision 0)
@@ -0,0 +1,162 @@
+
+// Media Modal Frame
+var taxonomy_images_file_frame;
+
+( function( $ ) {
+
+ $( document ).ready( function() {
+
+ // Store the old id (not sure if this is application when editing a term)
+ TaxonomyImagesMediaModal.wp_media_post_id = wp.media.model.settings.post.id;
+
+ // When the remove icon is clicked...
+ $( '.taxonomy-image-control a.remove' ).on( 'click', function( event ) {
+
+ event.preventDefault();
+
+ var tt_id = $( this ).data( 'tt-id' );
+
+ $.ajax( {
+ url : ajaxurl,
+ type : 'POST',
+ dataType : 'json',
+ data : {
+ 'action' : 'taxonomy_image_plugin_remove_association',
+ 'wp_nonce' : $( this ).data( 'nonce' ),
+ 'tt_id' : $( this ).data( 'tt-id' )
+ },
+ cache : false,
+ success : function ( response ) {
+ if ( 'good' === response.status ) {
+
+ selector = $( '#taxonomy-image-control-' + tt_id );
+
+ /* Update the image on the screen below */
+ selector.find( '.taxonomy-image-thumbnail img' ).attr( 'src', TaxonomyImagesMediaModal.default_img_src );
+
+ selector.find( 'a.taxonomy-image-thumbnail' ).data( 'attachment-id', 0 );
+ selector.find( 'a.upload' ).data( 'attachment-id', 0 );
+
+ /* Show delete control on the screen below */
+ selector.find( '.remove' ).hide();
+
+ }
+ else if ( 'bad' === response.status ) {
+ alert( response.why );
+ }
+ }
+ } );
+
+ } );
+
+ // When image or upload icon clicked...
+ $( '.taxonomy-image-control a.upload, .taxonomy-image-control a.taxonomy-image-thumbnail' ).on( 'click', function( event ) {
+
+ event.preventDefault();
+
+ button = $( this );
+
+ TaxonomyImagesMediaModal.attachment_id = $( this ).data( 'attachment-id' );
+ TaxonomyImagesMediaModal.nonce = $( this ).data( 'nonce' );
+
+ // If the media frame already exists, reopen it.
+ if ( taxonomy_images_file_frame ) {
+
+ // Set the post ID to the term being edited and open
+ wp.media.model.settings.post.id = $( this ).data( 'tt-id' );
+ taxonomy_images_file_frame.uploader.uploader.param( 'post_id', $( this ).data( 'tt-id' ) );
+ taxonomy_images_file_frame.open();
+ return;
+
+ } else {
+
+ // Set the wp.media post id so the uploader grabs the term ID being edited
+ wp.media.model.settings.post.id = $( this ).data( 'tt-id' );
+
+ }
+
+ // Create the media frame.
+ taxonomy_images_file_frame = wp.media.frames.taxonomy_images_file_frame = wp.media( {
+ title : TaxonomyImagesMediaModal.uploader_title,
+ button : {
+ text : TaxonomyImagesMediaModal.uploader_button_text,
+ },
+ multiple : false
+ } );
+
+ // Pre-select selected attachment
+ wp.media.frames.taxonomy_images_file_frame.on( 'open', function() {
+ var selection = wp.media.frames.taxonomy_images_file_frame.state().get( 'selection' );
+ var selected_id = TaxonomyImagesMediaModal.attachment_id;
+ if ( selected_id > 0 ) {
+ attachment = wp.media.attachment( selected_id );
+ attachment.fetch();
+ selection.add( attachment ? [ attachment ] : [] );
+ }
+ } );
+
+ // When an image is selected, run a callback.
+ taxonomy_images_file_frame.on( 'select', function() {
+
+ // We set multiple to false so only get one image from the uploader
+ attachment = taxonomy_images_file_frame.state().get( 'selection' ).first().toJSON();
+
+ var tt_id = wp.media.model.settings.post.id;
+ var attachment_id = attachment.id;
+
+ // Do something with attachment.id and/or attachment.url here
+ $.ajax( {
+ url : ajaxurl,
+ type : 'POST',
+ dataType : 'json',
+ data : {
+ 'action' : 'taxonomy_image_create_association',
+ 'wp_nonce' : TaxonomyImagesMediaModal.nonce,
+ 'attachment_id' : attachment.id,
+ 'tt_id' : parseInt( wp.media.model.settings.post.id )
+ },
+ success : function ( response ) {
+ if ( 'good' === response.status ) {
+ var parent_id = button.parent().attr( 'id' );
+
+ /* Set state of all other buttons. */
+ $( '.taxonomy-image-modal-control' ).each( function( i, e ) {
+ if ( parent_id == $( e ).attr( 'id' ) ) {
+ return true;
+ }
+ $( e ).find( '.create-association' ).show();
+ $( e ).find( '.remove-association' ).hide();
+ } );
+
+ selector = $( '#taxonomy-image-control-' + tt_id );
+
+ /* Update the image on the screen below */
+ selector.find( '.taxonomy-image-thumbnail img' ).attr( 'src', response.attachment_thumb_src );
+
+ selector.find( 'a.taxonomy-image-thumbnail' ).data( 'attachment-id', attachment_id );
+ selector.find( 'a.upload' ).data( 'attachment-id', attachment_id );
+
+ /* Show delete control on the screen below */
+ $( selector ).find( '.remove' ).each( function ( i, e ) {
+ $( e ).removeClass( 'hide' );
+ } );
+
+ }
+ else if ( 'bad' === response.status ) {
+ alert( response.why );
+ }
+ }
+ } );
+
+ wp.media.model.settings.post.id = TaxonomyImagesMediaModal.wp_media_post_id;
+
+ } );
+
+ // Finally, open the modal
+ taxonomy_images_file_frame.open();
+
+ } );
+
+ } );
+
+} )( jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment