From 4ef7e552da78dcfdbd4eda4743c47ea13a8b4b94 Mon Sep 17 00:00:00 2001 From: Robin Cornett Date: Thu, 18 May 2017 16:36:27 -0400 Subject: [PATCH] Refactor taxonomy widget Adds new widget form class, which all three widgets can share. --- .../class-displayfeaturedimagegenesis.php | 38 +-- includes/widgets/admin/checkbox.php | 8 + includes/widgets/admin/number.php | 8 + includes/widgets/admin/select.php | 15 ++ includes/widgets/admin/text.php | 8 + includes/widgets/admin/textarea.php | 7 + ...ss-displayfeaturedimagegenesis-widgets.php | 126 ++++++++++ ...ayfeaturedimagegenesis-taxonomy-widget.php | 234 ++++++++++-------- 8 files changed, 317 insertions(+), 127 deletions(-) create mode 100644 includes/widgets/admin/checkbox.php create mode 100644 includes/widgets/admin/number.php create mode 100644 includes/widgets/admin/select.php create mode 100644 includes/widgets/admin/text.php create mode 100644 includes/widgets/admin/textarea.php create mode 100644 includes/widgets/class-displayfeaturedimagegenesis-widgets.php diff --git a/includes/class-displayfeaturedimagegenesis.php b/includes/class-displayfeaturedimagegenesis.php index d2301a6..afadc01 100644 --- a/includes/class-displayfeaturedimagegenesis.php +++ b/includes/class-displayfeaturedimagegenesis.php @@ -212,7 +212,6 @@ class Display_Featured_Image_Genesis { /** * check existing settings array to see if a setting is in the array - * @return updated setting updates to default (0) * @since 1.5.0 */ public function check_settings() { @@ -253,6 +252,8 @@ class Display_Featured_Image_Genesis { * * @param string|array $new New settings. Can be a string, or an array. * @param string $setting Optional. Settings field name. Default is displayfeaturedimagegenesis. + * + * @return mixed updated option */ protected function update_settings( $new = '', $setting = 'displayfeaturedimagegenesis' ) { return update_option( $setting, wp_parse_args( $new, get_option( $setting ) ) ); @@ -264,12 +265,11 @@ class Display_Featured_Image_Genesis { * @since 1.1.0 */ public function load_textdomain() { - load_plugin_textdomain( 'display-featured-image-genesis', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); + load_plugin_textdomain( 'display-featured-image-genesis' ); } /** * enqueue admin scripts - * @return scripts to use image uploader * * @since 1.2.1 */ @@ -301,41 +301,41 @@ class Display_Featured_Image_Genesis { if ( in_array( $screen->id, array( 'widgets', 'customize' ), true ) ) { wp_enqueue_script( 'widget_selector' ); - wp_localize_script( 'widget_selector', 'displayfeaturedimagegenesis_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) ); + wp_localize_script( 'widget_selector', 'displayfeaturedimagegenesis_ajax_object', array( + 'ajax_url' => admin_url( 'admin-ajax.php' ), + ) ); } } /** * Register widgets for plugin - * @return widgets Taxonomy/term, CPT, and Author widgets * * @since 2.0.0 */ public function register_widgets() { - $files = array( - 'author', - 'cpt-archive', - 'taxonomy', - ); - - foreach ( $files as $file ) { - require_once plugin_dir_path( __FILE__ ) . 'widgets/displayfeaturedimagegenesis-' . $file . '-widget.php'; - } - if ( function_exists( 'is_customize_preview' ) && is_customize_preview() && ! function_exists( 'genesis' ) ) { return; } - register_widget( 'Display_Featured_Image_Genesis_Author_Widget' ); - register_widget( 'Display_Featured_Image_Genesis_Widget_Taxonomy' ); - register_widget( 'Display_Featured_Image_Genesis_Widget_CPT' ); + $widgets = array( + 'author' => 'Display_Featured_Image_Genesis_Author_Widget', + 'cpt-archive' => 'Display_Featured_Image_Genesis_Widget_CPT', + 'taxonomy' => 'Display_Featured_Image_Genesis_Widget_Taxonomy', + ); + + require_once plugin_dir_path( __FILE__ ) . 'widgets/class-displayfeaturedimagegenesis-widgets.php'; + foreach ( $widgets as $file => $widget ) { + require_once plugin_dir_path( __FILE__ ) . 'widgets/displayfeaturedimagegenesis-' . $file . '-widget.php'; + register_widget( $widget ); + } } /** * Add link to plugin settings page in plugin table - * @param $links link to settings page + * @param $links array link to settings page + * @return array * * @since 2.3.0 */ diff --git a/includes/widgets/admin/checkbox.php b/includes/widgets/admin/checkbox.php new file mode 100644 index 0000000..3b707b3 --- /dev/null +++ b/includes/widgets/admin/checkbox.php @@ -0,0 +1,8 @@ +parent->defaults[ $args['id'] ]; +echo '

'; +printf( '', esc_attr( $this->parent->get_field_name( $args['id'] ) ) ); +printf( '', esc_attr( $this->parent->get_field_id( $args['id'] ) ), esc_attr( $this->parent->get_field_name( $args['id'] ) ), checked( 1, esc_attr( $value ), false ) ); +printf( '', esc_attr( $this->parent->get_field_id( $args['id'] ) ), esc_attr( $args['label'] ) ); +echo '

'; diff --git a/includes/widgets/admin/number.php b/includes/widgets/admin/number.php new file mode 100644 index 0000000..f5aa788 --- /dev/null +++ b/includes/widgets/admin/number.php @@ -0,0 +1,8 @@ +', wp_kses( $flex, array( 'class' ) ) ); +printf( '', esc_attr( $this->parent->get_field_id( $args['id'] ) ), esc_attr( $args['label'] ) ); +printf( '%s', intval( $args['min'] ), intval( $args['max'] ), esc_attr( $this->parent->get_field_id( $args['id'] ) ), esc_attr( $this->parent->get_field_name( $args['id'] ) ), esc_attr( $instance[ $args['id'] ] ), esc_attr( $after ) ); +echo '

'; diff --git a/includes/widgets/admin/select.php b/includes/widgets/admin/select.php new file mode 100644 index 0000000..8113738 --- /dev/null +++ b/includes/widgets/admin/select.php @@ -0,0 +1,15 @@ +', wp_kses( $flex, array( 'class' ) ) ); +printf( '', esc_attr( $this->parent->get_field_id( $args['id'] ) ), esc_attr( $args['label'] ) ); +printf( ''; +echo '

'; diff --git a/includes/widgets/admin/text.php b/includes/widgets/admin/text.php new file mode 100644 index 0000000..f367361 --- /dev/null +++ b/includes/widgets/admin/text.php @@ -0,0 +1,8 @@ +'; +printf( '', esc_attr( $this->parent->get_field_id( $args['id'] ) ), $label_class, esc_attr( $args['label'] ) ); +printf( '', esc_attr( $this->parent->get_field_id( $args['id'] ) ), esc_attr( $this->parent->get_field_name( $args['id'] ) ), esc_attr( $instance[ $args['id'] ] ), $class ); +echo '

'; diff --git a/includes/widgets/admin/textarea.php b/includes/widgets/admin/textarea.php new file mode 100644 index 0000000..939c616 --- /dev/null +++ b/includes/widgets/admin/textarea.php @@ -0,0 +1,7 @@ +'; +$rows = isset( $args['rows'] ) ? $args['rows'] : 3; +printf( '', esc_attr( $this->parent->get_field_id( $args['id'] ) ), esc_attr( $args['label'] ) ); +printf( '', absint( $rows ), esc_attr( $this->parent->get_field_id( $args['id'] ) ), esc_attr( $this->parent->get_field_name( $args['id'] ) ), esc_attr( $instance[ $args['id'] ] ) ); +echo '

'; diff --git a/includes/widgets/class-displayfeaturedimagegenesis-widgets.php b/includes/widgets/class-displayfeaturedimagegenesis-widgets.php new file mode 100644 index 0000000..c235799 --- /dev/null +++ b/includes/widgets/class-displayfeaturedimagegenesis-widgets.php @@ -0,0 +1,126 @@ +parent = $parent; + $this->instance = $instance; + } + + /** + * @return array + */ + public function get_image_size() { + $sizes = genesis_get_image_sizes(); + $options = array(); + foreach ( (array) $sizes as $name => $size ) { + $options[ $name ] = sprintf( '%s ( %s x %s )', esc_html( $name ), (int) $size['width'], (int) $size['height'] ); + } + + return $options; + } + + /** + * Build boxes with fields. + * + * @param $boxes + * @param string $class + */ + public function do_boxes( $boxes, $class = '' ) { + foreach ( $boxes as $box => $value ) { + if ( ! $value ) { + continue; + } + $box_class = ! $class ? 'genesis-widget-column-box' : 'genesis-widget-column-box ' . $class; + printf( '
', esc_attr( $box_class ) ); + echo wp_kses_post( wpautop( $this->box_description( $box ) ) ); + $this->do_fields( $this->instance, $value ); + echo '
'; + } + } + + /** + * Add a description to a widget settings box. + * @param $box + * + * @return string + */ + public function box_description( $box ) { + $method = "describe_{$box}"; + return method_exists( $this, $method ) ? $this->$method() : ''; + } + + /** + * Cycle through the fields for a given box, pick the appropriate method, and go. + * @param $instance + * @param $fields + */ + public function do_fields( $instance, $fields ) { + foreach ( $fields as $field ) { + $method = "do_{$field['method']}"; + if ( method_exists( $this, $method ) ) { + $this->$method( $instance, $field['args'] ); + } + } + } + + /** + * Generic function to build a text input for the widget form. + * @param $instance + * @param $args + */ + public function do_text( $instance, $args ) { + include $this->path( 'text' ); + } + + /** + * Generic function to build a select input for the widget form. + * @param $instance + * @param $args + */ + public function do_select( $instance, $args ) { + include $this->path( 'select' ); + } + + /** + * Generic function to build a number input. + * @param $instance + * @param $args + */ + public function do_number( $instance, $args ) { + include $this->path( 'number' ); + } + + /** + * Generic function to build a checkbox input. + * @param $instance + * @param $args + */ + public function do_checkbox( $instance, $args ) { + include $this->path( 'checkbox' ); + } + + /** + * Generic function to build a textarea input + * @param $instance + * @param $args + */ + public function do_textarea( $instance, $args ) { + include $this->path( 'textarea' ); + } + + /** + * Get path for included files. + * + * @param string $file + * + * @return string + */ + public function path( $file ) { + return trailingslashit( plugin_dir_path( __FILE__ ) . 'admin' ) . $file . '.php'; + } +} diff --git a/includes/widgets/displayfeaturedimagegenesis-taxonomy-widget.php b/includes/widgets/displayfeaturedimagegenesis-taxonomy-widget.php index c66886c..aa8764e 100644 --- a/includes/widgets/displayfeaturedimagegenesis-taxonomy-widget.php +++ b/includes/widgets/displayfeaturedimagegenesis-taxonomy-widget.php @@ -70,9 +70,7 @@ class Display_Featured_Image_Genesis_Widget_Taxonomy extends WP_Widget { * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget. * @param array $instance The settings for the particular instance of the widget */ - function widget( $args, $instance ) { - - global $wp_query, $_genesis_displayed_ids; + public function widget( $args, $instance ) { // Merge with defaults $instance = wp_parse_args( (array) $instance, $this->defaults ); @@ -170,107 +168,143 @@ class Display_Featured_Image_Genesis_Widget_Taxonomy extends WP_Widget { * * @param array $instance Current settings */ - function form( $instance ) { + public function form( $instance ) { // Merge with defaults $instance = wp_parse_args( (array) $instance, $this->defaults ); + $form = new DisplayFeaturedImageGenesisWidgets( $this, $instance ); - ?> -

- - -

+ $form->do_text( $instance, array( + 'id' => 'title', + 'label' => __( 'Title:', 'display-featured-image-genesis' ), + 'class' => 'widefat', + ) ); -
+ echo '
'; -
+ $form->do_boxes( array( + 'taxonomy' => array( + array( + 'method' => 'select', + 'args' => array( + 'id' => 'taxonomy', + 'label' => __( 'Taxonomy:', 'display-featured-image-genesis' ), + 'flex' => true, + 'onchange' => sprintf( 'term_postback(\'%s\', this.value );', esc_attr( $this->get_field_id( 'term' ) ) ), + 'choices' => $this->get_taxonomies(), + ), + ), + array( + 'method' => 'select', + 'args' => array( + 'id' => 'term', + 'label' => __( 'Term:', 'display-featured-image-genesis' ), + 'flex' => true, + 'choices' => $this->get_term_lists( $instance, false ), + ), + ), + ), + ), 'genesis-widget-column-box-top' ); -

- - -

+ echo '
'; + echo '
'; -

- - -

+ $form->do_boxes( array( + 'image' => array( + array( + 'method' => 'checkbox', + 'args' => array( + 'id' => 'show_image', + 'label' => __( 'Show Featured Image', 'display-featured-image-genesis' ), + ), + ), + array( + 'method' => 'select', + 'args' => array( + 'id' => 'image_size', + 'label' => __( 'Image Size:', 'display-featured-image-genesis' ), + 'flex' => true, + 'choices' => $form->get_image_size(), + ), + ), + array( + 'method' => 'select', + 'args' => array( + 'id' => 'image_alignment', + 'label' => __( 'Image Alignment', 'display-featured-image-genesis' ), + 'flex' => true, + 'choices' => array( + 'alignnone' => __( 'None', 'display-featured-image-genesis' ), + 'alignleft' => __( 'Left', 'display-featured-image-genesis' ), + 'alignright' => __( 'Right', 'display-featured-image-genesis' ), + 'aligncenter' => __( 'Center', 'display-featured-image-genesis' ), + ), + ), + ), + ), + ), 'genesis-widget-column-box-top' ); -
+ echo '
'; + } -
+ /** + * @return array + */ + protected function get_taxonomies() { + $args = array( + 'public' => true, + 'show_ui' => true, + ); + $taxonomies = get_taxonomies( $args, 'objects' ); + $options = array(); + foreach ( $taxonomies as $taxonomy ) { + $options[ $taxonomy->name ] = $taxonomy->label; + } -

- /> - -

+ return $options; + } -

- /> - -

- -
- -
- -
- -
- -

- /> - -

- -

- - -

- -

- - -

- -
- -
- 'name', + 'order' => 'ASC', + 'hide_empty' => false, + ); + $taxonomy = $ajax ? sanitize_text_field( $_POST['taxonomy'] ) : $instance['taxonomy']; + $terms = get_terms( $taxonomy, $args ); + $options['none'] = '--'; + foreach ( $terms as $term ) { + if ( is_object( $term ) ) { + $options[ $term->term_id ] = $term->name; + } + } + return $options; } /** @@ -280,26 +314,10 @@ class Display_Featured_Image_Genesis_Widget_Taxonomy extends WP_Widget { */ function term_action_callback() { - $args = array( - 'orderby' => 'name', - 'order' => 'ASC', - 'hide_empty' => false, - ); - $terms = get_terms( $_POST['taxonomy'], $args ); - - // Build an appropriate JSON response containing this info - $list['none'] = '--'; - foreach ( $terms as $term ) { - $list[ $term->term_id ] = esc_attr( $term->name ); - } + $list = $this->get_term_lists( array(), true ); // And emit it - $emit = json_encode( $list ); - if ( function_exists( 'wp_json_encode' ) ) { - $emit = wp_json_encode( $list ); - } - echo $emit; + echo wp_json_encode( $list ); die(); } - }