class-wc-widget-brand-description.php
1 year ago
class-wc-widget-brand-nav.php
7 months ago
class-wc-widget-brand-thumbnails.php
1 year ago
class-wc-widget-cart.php
3 years ago
class-wc-widget-layered-nav-filters.php
2 years ago
class-wc-widget-layered-nav.php
3 months ago
class-wc-widget-price-filter.php
8 months ago
class-wc-widget-product-categories.php
7 months ago
class-wc-widget-product-search.php
5 years ago
class-wc-widget-product-tag-cloud.php
5 years ago
class-wc-widget-products.php
1 year ago
class-wc-widget-rating-filter.php
5 years ago
class-wc-widget-recent-reviews.php
2 months ago
class-wc-widget-recently-viewed.php
1 year ago
class-wc-widget-top-rated-products.php
5 years ago
class-wc-widget-brand-thumbnails.php
236 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types = 1); |
| 4 | |
| 5 | /** |
| 6 | * Brand Thumbnails Widget |
| 7 | * |
| 8 | * Show brand images as thumbnails |
| 9 | * |
| 10 | * Important: For internal use only by the Automattic\WooCommerce\Internal\Brands package. |
| 11 | * |
| 12 | * @package WooCommerce\Widgets |
| 13 | * @version 9.4.0 |
| 14 | */ |
| 15 | class WC_Widget_Brand_Thumbnails extends WP_Widget { |
| 16 | |
| 17 | /** |
| 18 | * Widget CSS class. |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | public $woo_widget_cssclass; |
| 23 | |
| 24 | /** |
| 25 | * Widget description. |
| 26 | * |
| 27 | * @var string |
| 28 | */ |
| 29 | public $woo_widget_description; |
| 30 | |
| 31 | /** |
| 32 | * Widget id base. |
| 33 | * |
| 34 | * @var string |
| 35 | */ |
| 36 | public $woo_widget_idbase; |
| 37 | |
| 38 | /** |
| 39 | * Widget name. |
| 40 | * |
| 41 | * @var string |
| 42 | */ |
| 43 | public $woo_widget_name; |
| 44 | |
| 45 | /** Constructor */ |
| 46 | public function __construct() { |
| 47 | |
| 48 | /* Widget variable settings. */ |
| 49 | $this->woo_widget_name = __( 'WooCommerce Brand Thumbnails', 'woocommerce' ); |
| 50 | $this->woo_widget_description = __( 'Show a grid of brand thumbnails.', 'woocommerce' ); |
| 51 | $this->woo_widget_idbase = 'wc_brands_brand_thumbnails'; |
| 52 | $this->woo_widget_cssclass = 'widget_brand_thumbnails'; |
| 53 | |
| 54 | /* Widget settings. */ |
| 55 | $widget_ops = array( |
| 56 | 'classname' => $this->woo_widget_cssclass, |
| 57 | 'description' => $this->woo_widget_description, |
| 58 | ); |
| 59 | |
| 60 | /* Create the widget. */ |
| 61 | parent::__construct( $this->woo_widget_idbase, $this->woo_widget_name, $widget_ops ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Echoes the widget content. |
| 66 | * |
| 67 | * @see WP_Widget |
| 68 | * |
| 69 | * @param array $args Display arguments including 'before_title', 'after_title', |
| 70 | * 'before_widget', and 'after_widget'. |
| 71 | * @param array $instance The settings for the particular instance of the widget. |
| 72 | */ |
| 73 | public function widget( $args, $instance ) { |
| 74 | $instance = wp_parse_args( |
| 75 | $instance, |
| 76 | array( |
| 77 | 'title' => '', |
| 78 | 'columns' => 1, |
| 79 | 'exclude' => '', |
| 80 | 'orderby' => 'name', |
| 81 | 'hide_empty' => 0, |
| 82 | 'number' => '', |
| 83 | ) |
| 84 | ); |
| 85 | |
| 86 | $exclude = array_map( 'intval', explode( ',', $instance['exclude'] ) ); |
| 87 | $order = 'name' === $instance['orderby'] ? 'asc' : 'desc'; |
| 88 | |
| 89 | $brands = get_terms( |
| 90 | array( |
| 91 | 'taxonomy' => 'product_brand', |
| 92 | 'hide_empty' => $instance['hide_empty'], |
| 93 | 'orderby' => $instance['orderby'], |
| 94 | 'exclude' => $exclude, |
| 95 | 'number' => $instance['number'], |
| 96 | 'order' => $order, |
| 97 | ) |
| 98 | ); |
| 99 | |
| 100 | if ( ! $brands ) { |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Filter the widget's title. |
| 106 | * |
| 107 | * @since 9.4.0 |
| 108 | * |
| 109 | * @param string $title Widget title |
| 110 | * @param array $instance The settings for the particular instance of the widget. |
| 111 | * @param string $woo_widget_idbase The widget's id base. |
| 112 | */ |
| 113 | $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->woo_widget_idbase ); |
| 114 | |
| 115 | echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput |
| 116 | if ( '' !== $title ) { |
| 117 | echo $args['before_title'] . $title . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput |
| 118 | } |
| 119 | |
| 120 | wc_get_template( |
| 121 | 'widgets/brand-thumbnails.php', |
| 122 | array( |
| 123 | 'brands' => $brands, |
| 124 | 'columns' => (int) $instance['columns'], |
| 125 | 'fluid_columns' => ! empty( $instance['fluid_columns'] ) ? true : false, |
| 126 | ), |
| 127 | 'woocommerce', |
| 128 | WC()->plugin_path() . '/templates/brands/' |
| 129 | ); |
| 130 | |
| 131 | echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Update widget instance. |
| 136 | * |
| 137 | * @param array $new_instance The new settings for the particular instance of the widget. |
| 138 | * @param array $old_instance The old settings for the particular instance of the widget. |
| 139 | * |
| 140 | * @see WP_Widget->update |
| 141 | */ |
| 142 | public function update( $new_instance, $old_instance ) { |
| 143 | $instance['title'] = wp_strip_all_tags( stripslashes( $new_instance['title'] ) ); |
| 144 | $instance['columns'] = wp_strip_all_tags( stripslashes( $new_instance['columns'] ) ); |
| 145 | $instance['fluid_columns'] = ! empty( $new_instance['fluid_columns'] ) ? true : false; |
| 146 | $instance['orderby'] = wp_strip_all_tags( stripslashes( $new_instance['orderby'] ) ); |
| 147 | $instance['exclude'] = wp_strip_all_tags( stripslashes( $new_instance['exclude'] ) ); |
| 148 | $instance['hide_empty'] = wp_strip_all_tags( stripslashes( (string) $new_instance['hide_empty'] ) ); |
| 149 | $instance['number'] = wp_strip_all_tags( stripslashes( $new_instance['number'] ) ); |
| 150 | |
| 151 | if ( ! $instance['columns'] ) { |
| 152 | $instance['columns'] = 1; |
| 153 | } |
| 154 | |
| 155 | if ( ! $instance['orderby'] ) { |
| 156 | $instance['orderby'] = 'name'; |
| 157 | } |
| 158 | |
| 159 | if ( ! $instance['exclude'] ) { |
| 160 | $instance['exclude'] = ''; |
| 161 | } |
| 162 | |
| 163 | if ( ! $instance['hide_empty'] ) { |
| 164 | $instance['hide_empty'] = 0; |
| 165 | } |
| 166 | |
| 167 | if ( ! $instance['number'] ) { |
| 168 | $instance['number'] = ''; |
| 169 | } |
| 170 | |
| 171 | return $instance; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Outputs the settings update form. |
| 176 | * |
| 177 | * @param array $instance Current settings. |
| 178 | */ |
| 179 | public function form( $instance ) { |
| 180 | if ( ! isset( $instance['hide_empty'] ) ) { |
| 181 | $instance['hide_empty'] = 0; |
| 182 | } |
| 183 | |
| 184 | if ( ! isset( $instance['orderby'] ) ) { |
| 185 | $instance['orderby'] = 'name'; |
| 186 | } |
| 187 | |
| 188 | if ( empty( $instance['fluid_columns'] ) ) { |
| 189 | $instance['fluid_columns'] = false; |
| 190 | } |
| 191 | |
| 192 | ?> |
| 193 | <p> |
| 194 | <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'woocommerce' ); ?></label> |
| 195 | <input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; ?>" /> |
| 196 | </p> |
| 197 | |
| 198 | <p> |
| 199 | <label for="<?php echo esc_attr( $this->get_field_id( 'columns' ) ); ?>"><?php esc_html_e( 'Columns:', 'woocommerce' ); ?></label> |
| 200 | <input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'columns' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'columns' ) ); ?>" value="<?php echo isset( $instance['columns'] ) ? esc_attr( $instance['columns'] ) : '1'; ?>" /> |
| 201 | </p> |
| 202 | |
| 203 | <p> |
| 204 | <label for="<?php echo esc_attr( $this->get_field_id( 'fluid_columns' ) ); ?>"><?php esc_html_e( 'Fluid columns:', 'woocommerce' ); ?></label> |
| 205 | <input type="checkbox" <?php checked( $instance['fluid_columns'] ); ?> id="<?php echo esc_attr( $this->get_field_id( 'fluid_columns' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'fluid_columns' ) ); ?>" /> |
| 206 | </p> |
| 207 | |
| 208 | <p> |
| 209 | <label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php esc_html_e( 'Number:', 'woocommerce' ); ?></label> |
| 210 | <input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'number' ) ); ?>" value="<?php if ( isset ( $instance['number'] ) ) { echo esc_attr( $instance['number'] ); } // phpcs:ignore ?>" placeholder="<?php esc_attr_e( 'All', 'woocommerce' ); ?>" /> |
| 211 | </p> |
| 212 | |
| 213 | <p> |
| 214 | <label for="<?php echo esc_attr( $this->get_field_id( 'exclude' ) ); ?>"><?php esc_html_e( 'Exclude:', 'woocommerce' ); ?></label> |
| 215 | <input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'exclude' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'exclude' ) ); ?>" value="<?php if ( isset ( $instance['exclude'] ) ) { echo esc_attr( $instance['exclude'] ); } // phpcs:ignore ?>" placeholder="<?php esc_attr_e( 'None', 'woocommerce' ); ?>" /> |
| 216 | </p> |
| 217 | |
| 218 | <p> |
| 219 | <label for="<?php echo esc_attr( $this->get_field_id( 'hide_empty' ) ); ?>"><?php esc_html_e( 'Hide empty brands:', 'woocommerce' ); ?></label> |
| 220 | <select id="<?php echo esc_attr( $this->get_field_id( 'hide_empty' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'hide_empty' ) ); ?>"> |
| 221 | <option value="1" <?php selected( $instance['hide_empty'], 1 ); ?>><?php esc_html_e( 'Yes', 'woocommerce' ); ?></option> |
| 222 | <option value="0" <?php selected( $instance['hide_empty'], 0 ); ?>><?php esc_html_e( 'No', 'woocommerce' ); ?></option> |
| 223 | </select> |
| 224 | </p> |
| 225 | |
| 226 | <p> |
| 227 | <label for="<?php echo esc_attr( $this->get_field_id( 'orderby' ) ); ?>"><?php esc_html_e( 'Order by:', 'woocommerce' ); ?></label> |
| 228 | <select id="<?php echo esc_attr( $this->get_field_id( 'orderby' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'orderby' ) ); ?>"> |
| 229 | <option value="name" <?php selected( $instance['orderby'], 'name' ); ?>><?php esc_html_e( 'Name', 'woocommerce' ); ?></option> |
| 230 | <option value="count" <?php selected( $instance['orderby'], 'count' ); ?>><?php esc_html_e( 'Count', 'woocommerce' ); ?></option> |
| 231 | </select> |
| 232 | </p> |
| 233 | <?php |
| 234 | } |
| 235 | } |
| 236 |