update tax widget to select tax, then term

js needs work
This commit is contained in:
Robin Cornett
2015-01-05 20:32:18 -05:00
parent 1b2c90c872
commit 37cbcc7210
3 changed files with 104 additions and 35 deletions
@@ -181,6 +181,7 @@ class Display_Featured_Image_Genesis {
$check = strpos( get_current_screen()->id, 'displayfeaturedimagegenesis' ); $check = strpos( get_current_screen()->id, 'displayfeaturedimagegenesis' );
wp_register_script( 'displayfeaturedimage-upload', plugins_url( '/includes/js/settings-upload.js', dirname( __FILE__ ) ), array( 'jquery', 'media-upload', 'thickbox' ), $version ); wp_register_script( 'displayfeaturedimage-upload', plugins_url( '/includes/js/settings-upload.js', dirname( __FILE__ ) ), array( 'jquery', 'media-upload', 'thickbox' ), $version );
wp_register_script( 'widget-selector', plugins_url( '/includes/js/widget-selector.js', dirname( __FILE__ ) ), array( 'jquery' ), $version );
if ( 'appearance_page_displayfeaturedimagegenesis' === get_current_screen()->id || ! empty( get_current_screen()->taxonomy ) ) { if ( 'appearance_page_displayfeaturedimagegenesis' === get_current_screen()->id || ! empty( get_current_screen()->taxonomy ) ) {
@@ -192,6 +193,12 @@ class Display_Featured_Image_Genesis {
) ); ) );
} }
$screen = get_current_screen()->id;
if ( $screen === 'widgets' || $screen === 'customize' ) {
wp_enqueue_script( 'widget-selector' );
wp_localize_script( 'widget-selector', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}
} }
function register_widget() { function register_widget() {
+25
View File
@@ -0,0 +1,25 @@
function tax_term_postback( select_id, taxonomy ) {
var data = {
action: 'tax_term_action',
taxonomy: taxonomy
};
jQuery.post(ajax_object.ajax_url, data, function(response) {
// Decode the data received.
var terms = jQuery.parseJSON(response);
// Keep track of what was previously selected
var select_ctrl = jQuery('#' + select_id);
var old_term = select_ctrl.val();
// Clear out the old options, build up the new
select_ctrl.empty();
jQuery.each(terms, function(key, value) {
var new_option = jQuery('<option></option>')
.attr('value', key).text(value);
if (value == old_term) {
new_option.attr('selected', true);
}
select_ctrl.append(new_option);
});
});
}
@@ -59,6 +59,8 @@ class Display_Featured_Image_Genesis_Widget extends WP_Widget {
parent::__construct( 'featured-taxonomy', __( 'Genesis - Featured Taxonomy', 'display-featured-image-genesis' ), $widget_ops, $control_ops ); parent::__construct( 'featured-taxonomy', __( 'Genesis - Featured Taxonomy', 'display-featured-image-genesis' ), $widget_ops, $control_ops );
add_action( 'wp_ajax_tax_term_action', array( $this, 'tax_term_action_callback' ) );
} }
/** /**
@@ -93,7 +95,7 @@ class Display_Featured_Image_Genesis_Widget extends WP_Widget {
$term = get_term_by( 'id', $term_id, $instance['taxonomy'] ); $term = get_term_by( 'id', $term_id, $instance['taxonomy'] );
$title = $term->name; $title = $term->name;
$slug = $term->slug; $slug = $term->slug;
$permalink = trailingslashit( get_home_url() ) . $instance['taxonomy'] . '/' . $slug; $permalink = get_term_link( $term );
if ( $term_meta ) { if ( $term_meta ) {
$image_id = Display_Featured_Image_Genesis_Common::get_image_id( $term_meta['dfig_image'] ); $image_id = Display_Featured_Image_Genesis_Common::get_image_id( $term_meta['dfig_image'] );
@@ -184,32 +186,60 @@ class Display_Featured_Image_Genesis_Widget extends WP_Widget {
<div class="genesis-widget-column-box genesis-widget-column-box-top"> <div class="genesis-widget-column-box genesis-widget-column-box-top">
<p> <p>
<label for="<?php echo $this->get_field_id( 'term' ); ?>"><?php _e( 'Taxonomy', 'display-featured-image-genesis' ); ?>:</label> <label for="<?php echo $this->get_field_id( 'taxonomy' ); ?>"><?php _e( 'Taxonomy', 'display-featured-image-genesis' ); ?>:</label>
<select id="<?php echo $this->get_field_id( 'term' ); ?>" name="<?php echo $this->get_field_name( 'term' ); ?>" > <select id="<?php echo $this->get_field_id( 'taxonomy' ); ?>" name="<?php echo $this->get_field_name( 'taxonomy' ); ?>" onchange="tax_term_postback('<?php echo $this->get_field_id( 'term' ); ?>', this.value);" >
<?php <?php
$tax_args = array( $tax_args = array(
'_builtin' => false 'public' => true,
'show_ui' => true
); );
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => true
);
$output = 'objects';
$taxonomies = get_taxonomies( $tax_args ); $taxonomies = get_taxonomies( $tax_args );
$taxonomies['category'] = 'category';
$taxonomies['post_tag'] = 'post_tag'; foreach ( $taxonomies as $taxonomy ) {
$terms = get_terms( $taxonomies, $args ); echo '<option value="'. esc_attr( $taxonomy ) .'" '. selected( esc_attr( $taxonomy ), $instance['taxonomy'], false ) .'>'. esc_attr( $taxonomy ) .'</option>';
foreach ( $terms as $term ) { } ?>
echo '<option value="'. esc_attr( $term->term_id ) .'" '. selected( esc_attr( $term->term_id ), $instance['term'], false ) .'>'. esc_attr( $term->name . ' (' . $term->taxonomy . ')' ) .'</option>'; </select>
} </p>
$instance['taxonomy'] = selected( $term->taxonomy ); ?>
<p>
<label for="<?php echo $this->get_field_id( 'term' ); ?>"><?php _e( 'Term', 'display-featured-image-genesis' ); ?>:</label>
<select id="<?php echo $this->get_field_id( 'term' ); ?>" name="<?php echo $this->get_field_name( 'term' ); ?>" >
<?php
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false
);
$output = 'objects';
$terms = get_terms( $instance['taxonomy'], $args );
foreach ( $terms as $term ) {
echo '<option value="'. esc_attr( $term->term_id ) .'" '. selected( esc_attr( $term->term_id ), $instance['term'], false ) .'>'. esc_attr( $term->name ) .'</option>';
} ?>
</select>
</p> </p>
</div> </div>
<div class="genesis-widget-column-box"> <div class="genesis-widget-column-box">
<p>
<input id="<?php echo $this->get_field_id( 'show_title' ); ?>" type="checkbox" name="<?php echo $this->get_field_name( 'show_title' ); ?>" value="1" <?php checked( $instance['show_title'] ); ?>/>
<label for="<?php echo $this->get_field_id( 'show_title' ); ?>"><?php _e( 'Show Taxonomy Title', 'display-featured-image-genesis' ); ?></label>
</p>
<p>
<input id="<?php echo $this->get_field_id( 'show_content' ); ?>" type="checkbox" name="<?php echo $this->get_field_name( 'show_content' ); ?>" value="1" <?php checked( $instance['show_content'] ); ?>/>
<label for="<?php echo $this->get_field_id( 'show_content' ); ?>"><?php _e( 'Show Taxonomy Intro Text', 'display-featured-image-genesis' ); ?></label>
</p>
</div>
</div>
<div class="genesis-widget-column genesis-widget-column-right">
<div class="genesis-widget-column-box genesis-widget-column-box-top">
<p> <p>
<input id="<?php echo $this->get_field_id( 'show_image' ); ?>" type="checkbox" name="<?php echo $this->get_field_name( 'show_image' ); ?>" value="1" <?php checked( $instance['show_image'] ); ?>/> <input id="<?php echo $this->get_field_id( 'show_image' ); ?>" type="checkbox" name="<?php echo $this->get_field_name( 'show_image' ); ?>" value="1" <?php checked( $instance['show_image'] ); ?>/>
<label for="<?php echo $this->get_field_id( 'show_image' ); ?>"><?php _e( 'Show Featured Image', 'display-featured-image-genesis' ); ?></label> <label for="<?php echo $this->get_field_id( 'show_image' ); ?>"><?php _e( 'Show Featured Image', 'display-featured-image-genesis' ); ?></label>
@@ -238,27 +268,34 @@ class Display_Featured_Image_Genesis_Widget extends WP_Widget {
</div> </div>
</div>
<div class="genesis-widget-column genesis-widget-column-right">
<div class="genesis-widget-column-box genesis-widget-column-box-top">
<p>
<input id="<?php echo $this->get_field_id( 'show_title' ); ?>" type="checkbox" name="<?php echo $this->get_field_name( 'show_title' ); ?>" value="1" <?php checked( $instance['show_title'] ); ?>/>
<label for="<?php echo $this->get_field_id( 'show_title' ); ?>"><?php _e( 'Show Taxonomy Title', 'display-featured-image-genesis' ); ?></label>
</p>
<p>
<input id="<?php echo $this->get_field_id( 'show_content' ); ?>" type="checkbox" name="<?php echo $this->get_field_name( 'show_content' ); ?>" value="1" <?php checked( $instance['show_content'] ); ?>/>
<label for="<?php echo $this->get_field_id( 'show_content' ); ?>"><?php _e( 'Show Taxonomy Intro Text', 'display-featured-image-genesis' ); ?></label>
</p>
</div>
</div> </div>
<?php <?php
} }
/**
* Handles the callback to populate the custom term dropdown. The
* selected post type is provided in $_POST['post_type'], and the
* calling script expects a JSON array of term objects.
*/
function tax_term_action_callback() {
// And from there, a list of available terms in that tax
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => true
);
$terms = get_terms( $_POST['taxonomy'], $args );
// Build an appropriate JSON response containing this info
foreach ( $terms as $term ) {
$list[$term->slug] = $term->name;
}
// And emit it
echo json_encode( $list );
die();
}
} }