Plugin Directory

Changeset 2752469


Ignore:
Timestamp:
07/06/2022 09:50:52 AM (4 years ago)
Author:
peacefulqode
Message:

optmize code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • list-categories-widget/trunk/includes/widgets/woo-cat.php

    r2680879 r2752469  
    33class Custom_Post_Type_Widgets
    44{
    5 
    65    /**
    76     * Sets up a new widget instance
     
    1110        add_action('widgets_init', array($this, 'init'));
    1211    }
    13 
    14 
    1512    public function init()
    1613    {
    17 
    1814        register_widget('WP_Custom_Post_Type_Widgets_Categories');
    1915    }
    2016}
    21 
    2217class WP_Custom_Post_Type_Widgets_Categories extends WP_Widget
    2318{
     
    3429    {
    3530        $title = !empty($instance['title']) ? $instance['title'] : esc_html('Categories', 'category-widget');
    36 
    3731        /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
    3832        $title = apply_filters('widget_title', $title, $instance, $this->id_base);
    39 
    4033        $taxonomy = !empty($instance['taxonomy']) ? $instance['taxonomy'] : 'category';
    4134        $c        = !empty($instance['count']) ? (bool) $instance['count'] : false;
    4235        $h        = !empty($instance['hierarchical']) ? (bool) $instance['hierarchical'] : false;
    43 
    44 
    4536        echo wp_kses_post($args['before_widget']);
    4637        if ($title)
    4738        {
    4839            echo wp_kses_post($args['before_title']) . esc_html($title) . wp_kses_post($args['after_title']);
    49 
    5040        }
    51 
    52 
    5341        $cat_args = array(
    5442            'orderby'      => 'name',
     
    5846            'sanitize_links' => 1,
    5947            'walker'        => new List_Category_Images
    60 
    6148        );?>
    62 
    6349        <ul>
    64         <?php
    65         $cat_args['title_li'] = '';
    66 
    67         wp_list_categories(
    68             apply_filters(
    69                 'custom_post_type_widgets/categories/widget_categories_args',
    70                 $cat_args,
    71                 $instance,
    72                 $this->id,
    73                 $taxonomy
    74             )
    75         );
    76         ?>
     50            <?php
     51            $cat_args['title_li'] = '';
     52            wp_list_categories(
     53                apply_filters(
     54                    'custom_post_type_widgets/categories/widget_categories_args',
     55                    $cat_args,
     56                    $instance,
     57                    $this->id,
     58                    $taxonomy
     59                )
     60            );
     61            ?>
    7762        </ul>
    7863        <?php
    79 
    8064        echo wp_kses_post($args['after_widget']);
    8165    }
    82 
    8366    public function update($new_instance, $old_instance)
    8467    {
     
    8871        $instance['count']        = !empty($new_instance['count']) ? (bool) $new_instance['count'] : false;
    8972        $instance['hierarchical'] = !empty($new_instance['hierarchical']) ? (bool) $new_instance['hierarchical'] : false;
    90 
    91 
    9273        return $instance;
    9374    }
    94 
    9575    public function form($instance)
    9676    {
     
    9979        $count        = isset($instance['count']) ? (bool) $instance['count'] : false;
    10080        $hierarchical = isset($instance['hierarchical']) ? (bool) $instance['hierarchical'] : false;
    101 
    10281        ?>
    10382        <p><label for="<?php echo esc_attr($this->get_field_id('title'));   ?>"><?php esc_html_e('Title:', 'category-widget');?></label>
    10483            <input class="widefat" id="<?php echo esc_attr($this->get_field_id('title'));   ?>" name="<?php echo esc_attr($this->get_field_name('title'));   ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
    105 
    10684            <?php
    107         $taxonomies = get_taxonomies(array(), 'objects');
    108 
    109         if ($taxonomies) {
    110             printf(
    111                 '<p><label for="%1$s">%2$s</label>' .
    112                 '<select class="widefat" id="%1$s" name="%3$s">',
    113                 esc_attr($this->get_field_id('taxonomy')),
    114                 esc_html('Taxonomy:', 'category-widget'),
    115                 esc_html($this->get_field_name('taxonomy'))
    116             );
    117 
    118             foreach ($taxonomies as $taxobjects => $value) {
    119                 if (!$value->hierarchical) {
    120                     continue;
     85            $taxonomies = get_taxonomies(array(), 'objects');
     86            if ($taxonomies) {
     87                printf(
     88                    '<p><label for="%1$s">%2$s</label>' .
     89                    '<select class="widefat" id="%1$s" name="%3$s">',
     90                    esc_attr($this->get_field_id('taxonomy')),
     91                    esc_html('Taxonomy:', 'category-widget'),
     92                    esc_html($this->get_field_name('taxonomy'))
     93                );
     94                foreach ($taxonomies as $taxobjects => $value) {
     95                    if (!$value->hierarchical) {
     96                        continue;
     97                    }
     98                    if ('nav_menu' === $taxobjects || 'link_category' === $taxobjects || 'post_format' === $taxobjects) {
     99                        continue;
     100                    }
     101                    printf(
     102                        '<option value="%s"%s>%s</option>',
     103                        esc_attr($taxobjects),
     104                        selected($taxobjects, $taxonomy, false),
     105                        esc_html($value->label, 'category-widget') . ' ' . esc_html($taxobjects)
     106                    );
    121107                }
    122                 if ('nav_menu' === $taxobjects || 'link_category' === $taxobjects || 'post_format' === $taxobjects) {
    123                     continue;
    124                 }
    125 
    126                 printf(
    127                     '<option value="%s"%s>%s</option>',
    128                     esc_attr($taxobjects),
    129                     selected($taxobjects, $taxonomy, false),
    130                     esc_html($value->label, 'category-widget') . ' ' . esc_html($taxobjects)
    131                 );
     108                echo '</select></p>';
    132109            }
    133             echo '</select></p>';
    134         }
    135         ?>
    136 
     110            ?>
    137111            <p>
    138 
    139 
    140112                <input type="checkbox" class="checkbox" id="<?php echo esc_attr($this->get_field_id('count'));   ?>" name="<?php echo esc_attr($this->get_field_name('count'));   ?>"<?php checked($count);?> />
    141113                <label for="<?php echo esc_attr($this->get_field_id('count'));   ?>"><?php esc_html_e('Show post counts', 'category-widget');?></label><br />
    142 
    143114                <input type="checkbox" class="checkbox" id="<?php echo esc_attr($this->get_field_id('hierarchical'));   ?>" name="<?php echo esc_attr($this->get_field_name('hierarchical'));   ?>"<?php checked($hierarchical);?> />
    144115                <label for="<?php echo esc_attr($this->get_field_id('hierarchical'));   ?>"><?php esc_html_e('Show hierarchy', 'category-widget');?></label></p>
    145116                <?php
    146 }
    147 }
     117            }
     118        }
Note: See TracChangeset for help on using the changeset viewer.