Skip to content

Commit

Permalink
Add more JS to convert bulk-edit inputs, and ensure bulk-editing only…
Browse files Browse the repository at this point in the history
… saves one term. Another that fixes #3
  • Loading branch information
jtsternberg committed May 13, 2014
1 parent 155125f commit 5d06e88
Showing 1 changed file with 101 additions and 9 deletions.
110 changes: 101 additions & 9 deletions WDS_Taxonomy_Radio.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* $custom_tax_mb->indented = false;
*
* @link http://codex.wordpress.org/Function_Reference/add_meta_box#Parameters
* @version 0.1.3
* @version 0.1.4
*/
class WDS_Taxonomy_Radio {

Expand Down Expand Up @@ -79,6 +79,18 @@ class WDS_Taxonomy_Radio {
*/
public $indented = true;

/**
* Checks if there is a bulk-edit term to set
* @var boolean|term object
*/
public $to_set = false;

/**
* Array of post ids whose terms have been reset from bulk-edit. (prevents recursion)
* @var array
*/
public $single_term_set = array();

/**
* Initiates our metabox action
* @param string $tax_slug Taxonomy slug
Expand All @@ -92,6 +104,31 @@ public function __construct( $tax_slug, $post_types = array() ) {

add_action( 'add_meta_boxes', array( $this, 'add_radio_box' ) );
add_action( 'admin_footer', array( $this, 'js_checkbox_transform' ) );

// Handle bulk-editing
if ( isset( $_REQUEST['bulk_edit'] ) && 'Update' == $_REQUEST['bulk_edit'] ) {

// Get wp tax name designation
$name = $this->slug;
if ( 'category' == $name )
$name = 'post_category';
if ( 'tag' == $name )
$name = 'post_tag';

// If this tax name exists in the query arg
if ( isset( $_REQUEST[ $name ] ) && is_array( $_REQUEST[ $name ] ) ) {
$this->to_set = end( $_REQUEST[ $name ] );
} elseif ( isset( $_REQUEST['tax_input'][ $name ] ) && is_array( $_REQUEST['tax_input'][ $name ] ) ) {
$this->to_set = end( $_REQUEST['tax_input'][ $name ] );
}

// Then get it's term object
if ( $this->to_set ) {
$this->to_set = get_term( $this->to_set, $this->slug );
// And hook in our re-save action
add_action( 'set_object_terms', array( $this, 'maybe_resave_terms' ), 10, 5 );
}
}
}

/**
Expand Down Expand Up @@ -159,25 +196,80 @@ public function js_checkbox_transform() {

?>
<script type="text/javascript">
// Handles changing input types to radios for WDS_Taxonomy_Radio
jQuery(document).ready(function($){
$('.editinline').on( 'click', function( evt ) {
var $postsFilter = $('#posts-filter');
var $theList = $postsFilter.find('#the-list');

// Handles changing the input type attributes
var changeToRadio = function( $context ) {
$context = $context ? $context : $theList;
var $taxListInputs = $context.find( '.<?php echo $this->slug; ?>-checklist li input' );
if ( $taxListInputs.length ) {
// loop and switch input types
$taxListInputs.each( function() {
$(this).attr( 'type', 'radio' ).addClass('transformed-to-radio');
});
}
};

$postsFilter
// Handle converting radios in bulk-edit row
.on( 'click', '#doaction, #doaction2', function(){
var name = $(this).attr('id').substr(2);
if ( 'edit' === $( 'select[name="' + name + '"]' ).val() ) {
setTimeout( function() {
changeToRadio( $theList.find('#bulk-edit') );
}, 50 );
}
})
// when clicking new radio inputs, be sure to uncheck all but the one clicked
.on( 'change', '.transformed-to-radio', function() {
var $this = $(this);
$siblings = $this.parents( '.<?php echo $this->slug; ?>-checklist' ).find( 'li .transformed-to-radio' ).prop( 'checked', false );
$this.prop( 'checked', true );
});

// Handle converting radios in inline-edit rows
$theList.find('.editinline').on( 'click', function() {
var $this = $(this);
setTimeout( function() {
var $editRow = $this.parents( 'tr' ).next();
var $taxListInputs = $editRow.find( '.<?php echo $this->slug; ?>-checklist li input' );
if ( $taxListInputs.length ) {
// loop and switch input types
$taxListInputs.each( function() {
$(this).attr( 'type', 'radio' );
});
}
changeToRadio( $editRow );
}, 50 );
});
});
</script>
<?php
}

/**
* Handles resaving terms to post when bulk-editing so that only one term will be applied
* @since 0.1.4
* @param int $object_id Object ID.
* @param array $terms An array of object terms.
* @param array $tt_ids An array of term taxonomy IDs.
* @param string $taxonomy Taxonomy slug.
* @param bool $append Whether to append new terms to the old terms.
* @param array $old_tt_ids Old array of term taxonomy IDs.
*/
public function maybe_resave_terms( $object_id, $terms, $tt_ids, $taxonomy, $append ) {
if (
// if the terms being edited are not this taxonomy
$taxonomy != $this->slug
// or we already did our magic
|| in_array( $object_id, $this->single_term_set, true )
) {
// Then bail
return;
}

// Prevent recursion
$this->single_term_set[] = $object_id;
// Replace terms with the one term
wp_set_object_terms( $object_id, $this->to_set->slug, $taxonomy, $append );
}

/**
* Gets the taxonomy object from the slug
* @return object Taxonomy object
Expand Down

0 comments on commit 5d06e88

Please sign in to comment.