From 37cbcc7210778182a701a7b25762da515439a610 Mon Sep 17 00:00:00 2001 From: Robin Cornett Date: Mon, 5 Jan 2015 20:32:18 -0500 Subject: [PATCH] update tax widget to select tax, then term js needs work --- .../class-displayfeaturedimagegenesis.php | 7 ++ includes/js/widget-selector.js | 25 ++++ .../displayfeaturedimagegenesis-widget.php | 107 ++++++++++++------ 3 files changed, 104 insertions(+), 35 deletions(-) create mode 100644 includes/js/widget-selector.js diff --git a/includes/class-displayfeaturedimagegenesis.php b/includes/class-displayfeaturedimagegenesis.php index 9632a86..90a0640 100644 --- a/includes/class-displayfeaturedimagegenesis.php +++ b/includes/class-displayfeaturedimagegenesis.php @@ -181,6 +181,7 @@ class Display_Featured_Image_Genesis { $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( '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 ) ) { @@ -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() { diff --git a/includes/js/widget-selector.js b/includes/js/widget-selector.js new file mode 100644 index 0000000..d5685bf --- /dev/null +++ b/includes/js/widget-selector.js @@ -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('') + .attr('value', key).text(value); + if (value == old_term) { + new_option.attr('selected', true); + } + select_ctrl.append(new_option); + }); + }); +} diff --git a/includes/widgets/displayfeaturedimagegenesis-widget.php b/includes/widgets/displayfeaturedimagegenesis-widget.php index 31f8f14..df01347 100644 --- a/includes/widgets/displayfeaturedimagegenesis-widget.php +++ b/includes/widgets/displayfeaturedimagegenesis-widget.php @@ -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 ); + 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'] ); $title = $term->name; $slug = $term->slug; - $permalink = trailingslashit( get_home_url() ) . $instance['taxonomy'] . '/' . $slug; + $permalink = get_term_link( $term ); if ( $term_meta ) { $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 {

- - false + 'public' => true, + 'show_ui' => true ); - $args = array( - 'orderby' => 'name', - 'order' => 'ASC', - 'hide_empty' => true - ); - $output = 'objects'; $taxonomies = get_taxonomies( $tax_args ); - $taxonomies['category'] = 'category'; - $taxonomies['post_tag'] = 'post_tag'; - $terms = get_terms( $taxonomies, $args ); - foreach ( $terms as $term ) { - echo ''; - } - $instance['taxonomy'] = selected( $term->taxonomy ); ?> + + foreach ( $taxonomies as $taxonomy ) { + echo ''; + } ?> + +

+ +

+ +

+

+ /> + +

+ +

+ /> + +

+ +
+ + + +
+ +
+

/> @@ -238,27 +268,34 @@ class Display_Featured_Image_Genesis_Widget extends WP_Widget {

-
- -
- -
- -

- /> - -

- -

- /> - -

- -
-
'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(); + } + }