From ff3fe360da6e4d2721317ea898c40a5329cb7c34 Mon Sep 17 00:00:00 2001 From: Robin Cornett Date: Tue, 25 Jun 2019 12:35:43 -0400 Subject: [PATCH] Move the ajax callback --- ...splayfeaturedimagegenesis-widgets-form.php | 38 +++++++++++++++++ ...ayfeaturedimagegenesis-taxonomy-widget.php | 41 +------------------ includes/widgets/fields/term-taxonomy.php | 2 +- 3 files changed, 41 insertions(+), 40 deletions(-) diff --git a/includes/widgets/class-displayfeaturedimagegenesis-widgets-form.php b/includes/widgets/class-displayfeaturedimagegenesis-widgets-form.php index 5eb6898..69eb132 100644 --- a/includes/widgets/class-displayfeaturedimagegenesis-widgets-form.php +++ b/includes/widgets/class-displayfeaturedimagegenesis-widgets-form.php @@ -73,6 +73,44 @@ class DisplayFeaturedImageGenesisWidgetsForm { ); } + /** + * @param $instance + * @param bool $ajax + * + * @return mixed + */ + public function get_term_lists( $instance, $ajax = false ) { + $args = array( + 'orderby' => 'name', + 'order' => 'ASC', + 'hide_empty' => false, + ); + $taxonomy = $ajax ? filter_input( INPUT_POST, 'taxonomy', FILTER_SANITIZE_STRING ) : $instance['taxonomy']; + $terms = get_terms( $taxonomy, $args ); + $options[''] = '--'; + foreach ( $terms as $term ) { + if ( is_object( $term ) ) { + $options[ $term->term_id ] = $term->name; + } + } + + return $options; + } + + /** + * Handles the callback to populate the custom term dropdown. The + * selected post type is provided in $_POST['taxonomy'], and the + * calling script expects a JSON array of term objects. + */ + public function term_action_callback() { + + $list = $this->get_term_lists( array(), true ); + + // And emit it + echo wp_json_encode( $list ); + die(); + } + /** * Build boxes with fields. * diff --git a/includes/widgets/displayfeaturedimagegenesis-taxonomy-widget.php b/includes/widgets/displayfeaturedimagegenesis-taxonomy-widget.php index c17ec38..9a7d7b9 100644 --- a/includes/widgets/displayfeaturedimagegenesis-taxonomy-widget.php +++ b/includes/widgets/displayfeaturedimagegenesis-taxonomy-widget.php @@ -39,7 +39,8 @@ class Display_Featured_Image_Genesis_Widget_Taxonomy extends WP_Widget { parent::__construct( 'featured-taxonomy', __( 'Display Featured Term Image', 'display-featured-image-genesis' ), $widget_ops, $control_ops ); - add_action( 'wp_ajax_widget_selector', array( $this, 'term_action_callback' ) ); + $form = new DisplayFeaturedImageGenesisWidgetsForm( $this, array() ); + add_action( 'wp_ajax_widget_selector', array( $form, 'term_action_callback' ) ); } @@ -153,42 +154,4 @@ class Display_Featured_Image_Genesis_Widget_Taxonomy extends WP_Widget { 'archive' => include 'fields/archive.php', ) ); } - - /** - * @param $instance - * @param bool $ajax - * - * @return mixed - */ - protected function get_term_lists( $instance, $ajax = false ) { - $args = array( - 'orderby' => 'name', - 'order' => 'ASC', - 'hide_empty' => false, - ); - $taxonomy = $ajax ? filter_input( INPUT_POST, 'taxonomy', FILTER_SANITIZE_STRING ) : $instance['taxonomy']; - $terms = get_terms( $taxonomy, $args ); - $options[''] = '--'; - foreach ( $terms as $term ) { - if ( is_object( $term ) ) { - $options[ $term->term_id ] = $term->name; - } - } - - return $options; - } - - /** - * Handles the callback to populate the custom term dropdown. The - * selected post type is provided in $_POST['taxonomy'], and the - * calling script expects a JSON array of term objects. - */ - public function term_action_callback() { - - $list = $this->get_term_lists( array(), true ); - - // And emit it - echo wp_json_encode( $list ); - die(); - } } diff --git a/includes/widgets/fields/term-taxonomy.php b/includes/widgets/fields/term-taxonomy.php index e6d8df8..e8b438d 100644 --- a/includes/widgets/fields/term-taxonomy.php +++ b/includes/widgets/fields/term-taxonomy.php @@ -33,7 +33,7 @@ return array( 'id' => 'term', 'label' => __( 'Term:', 'display-featured-image-genesis' ), 'flex' => true, - 'choices' => $this->get_term_lists( $instance, false ), + 'choices' => $form->get_term_lists( $instance, false ), ), ), );