abstract-wc-data.php
3 years ago
abstract-wc-deprecated-hooks.php
6 years ago
abstract-wc-integration.php
5 years ago
abstract-wc-log-handler.php
5 years ago
abstract-wc-object-query.php
5 years ago
abstract-wc-order.php
3 years ago
abstract-wc-payment-gateway.php
3 years ago
abstract-wc-payment-token.php
5 years ago
abstract-wc-privacy.php
5 years ago
abstract-wc-product.php
4 years ago
abstract-wc-session.php
5 years ago
abstract-wc-settings-api.php
3 years ago
abstract-wc-shipping-method.php
5 years ago
abstract-wc-widget.php
4 years ago
class-wc-background-process.php
5 years ago
abstract-wc-widget.php
409 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Abstract widget class |
| 4 | * |
| 5 | * @class WC_Widget |
| 6 | * @package WooCommerce\Abstracts |
| 7 | */ |
| 8 | |
| 9 | use Automattic\Jetpack\Constants; |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * WC_Widget |
| 17 | * |
| 18 | * @package WooCommerce\Abstracts |
| 19 | * @version 2.5.0 |
| 20 | * @extends WP_Widget |
| 21 | */ |
| 22 | abstract class WC_Widget extends WP_Widget { |
| 23 | |
| 24 | /** |
| 25 | * CSS class. |
| 26 | * |
| 27 | * @var string |
| 28 | */ |
| 29 | public $widget_cssclass; |
| 30 | |
| 31 | /** |
| 32 | * Widget description. |
| 33 | * |
| 34 | * @var string |
| 35 | */ |
| 36 | public $widget_description; |
| 37 | |
| 38 | /** |
| 39 | * Widget ID. |
| 40 | * |
| 41 | * @var string |
| 42 | */ |
| 43 | public $widget_id; |
| 44 | |
| 45 | /** |
| 46 | * Widget name. |
| 47 | * |
| 48 | * @var string |
| 49 | */ |
| 50 | public $widget_name; |
| 51 | |
| 52 | /** |
| 53 | * Settings. |
| 54 | * |
| 55 | * @var array |
| 56 | */ |
| 57 | public $settings; |
| 58 | |
| 59 | /** |
| 60 | * Constructor. |
| 61 | */ |
| 62 | public function __construct() { |
| 63 | $widget_ops = array( |
| 64 | 'classname' => $this->widget_cssclass, |
| 65 | 'description' => $this->widget_description, |
| 66 | 'customize_selective_refresh' => true, |
| 67 | 'show_instance_in_rest' => true, |
| 68 | ); |
| 69 | |
| 70 | parent::__construct( $this->widget_id, $this->widget_name, $widget_ops ); |
| 71 | |
| 72 | add_action( 'save_post', array( $this, 'flush_widget_cache' ) ); |
| 73 | add_action( 'deleted_post', array( $this, 'flush_widget_cache' ) ); |
| 74 | add_action( 'switch_theme', array( $this, 'flush_widget_cache' ) ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get cached widget. |
| 79 | * |
| 80 | * @param array $args Arguments. |
| 81 | * @return bool true if the widget is cached otherwise false |
| 82 | */ |
| 83 | public function get_cached_widget( $args ) { |
| 84 | // Don't get cache if widget_id doesn't exists. |
| 85 | if ( empty( $args['widget_id'] ) ) { |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | $cache = wp_cache_get( $this->get_widget_id_for_cache( $this->widget_id ), 'widget' ); |
| 90 | |
| 91 | if ( ! is_array( $cache ) ) { |
| 92 | $cache = array(); |
| 93 | } |
| 94 | |
| 95 | if ( isset( $cache[ $this->get_widget_id_for_cache( $args['widget_id'] ) ] ) ) { |
| 96 | echo $cache[ $this->get_widget_id_for_cache( $args['widget_id'] ) ]; // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Cache the widget. |
| 105 | * |
| 106 | * @param array $args Arguments. |
| 107 | * @param string $content Content. |
| 108 | * @return string the content that was cached |
| 109 | */ |
| 110 | public function cache_widget( $args, $content ) { |
| 111 | // Don't set any cache if widget_id doesn't exist. |
| 112 | if ( empty( $args['widget_id'] ) ) { |
| 113 | return $content; |
| 114 | } |
| 115 | |
| 116 | $cache = wp_cache_get( $this->get_widget_id_for_cache( $this->widget_id ), 'widget' ); |
| 117 | |
| 118 | if ( ! is_array( $cache ) ) { |
| 119 | $cache = array(); |
| 120 | } |
| 121 | |
| 122 | $cache[ $this->get_widget_id_for_cache( $args['widget_id'] ) ] = $content; |
| 123 | |
| 124 | wp_cache_set( $this->get_widget_id_for_cache( $this->widget_id ), $cache, 'widget' ); |
| 125 | |
| 126 | return $content; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Flush the cache. |
| 131 | */ |
| 132 | public function flush_widget_cache() { |
| 133 | foreach ( array( 'https', 'http' ) as $scheme ) { |
| 134 | wp_cache_delete( $this->get_widget_id_for_cache( $this->widget_id, $scheme ), 'widget' ); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Get this widgets title. |
| 140 | * |
| 141 | * @param array $instance Array of instance options. |
| 142 | * @return string |
| 143 | */ |
| 144 | protected function get_instance_title( $instance ) { |
| 145 | if ( isset( $instance['title'] ) ) { |
| 146 | return $instance['title']; |
| 147 | } |
| 148 | |
| 149 | if ( isset( $this->settings, $this->settings['title'], $this->settings['title']['std'] ) ) { |
| 150 | return $this->settings['title']['std']; |
| 151 | } |
| 152 | |
| 153 | return ''; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Output the html at the start of a widget. |
| 158 | * |
| 159 | * @param array $args Arguments. |
| 160 | * @param array $instance Instance. |
| 161 | */ |
| 162 | public function widget_start( $args, $instance ) { |
| 163 | echo $args['before_widget']; // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped |
| 164 | |
| 165 | $title = apply_filters( 'widget_title', $this->get_instance_title( $instance ), $instance, $this->id_base ); |
| 166 | |
| 167 | if ( $title ) { |
| 168 | echo $args['before_title'] . $title . $args['after_title']; // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Output the html at the end of a widget. |
| 174 | * |
| 175 | * @param array $args Arguments. |
| 176 | */ |
| 177 | public function widget_end( $args ) { |
| 178 | echo $args['after_widget']; // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Updates a particular instance of a widget. |
| 183 | * |
| 184 | * @see WP_Widget->update |
| 185 | * @param array $new_instance New instance. |
| 186 | * @param array $old_instance Old instance. |
| 187 | * @return array |
| 188 | */ |
| 189 | public function update( $new_instance, $old_instance ) { |
| 190 | |
| 191 | $instance = $old_instance; |
| 192 | |
| 193 | if ( empty( $this->settings ) ) { |
| 194 | return $instance; |
| 195 | } |
| 196 | |
| 197 | // Loop settings and get values to save. |
| 198 | foreach ( $this->settings as $key => $setting ) { |
| 199 | if ( ! isset( $setting['type'] ) ) { |
| 200 | continue; |
| 201 | } |
| 202 | |
| 203 | // Format the value based on settings type. |
| 204 | switch ( $setting['type'] ) { |
| 205 | case 'number': |
| 206 | $instance[ $key ] = absint( $new_instance[ $key ] ); |
| 207 | |
| 208 | if ( isset( $setting['min'] ) && '' !== $setting['min'] ) { |
| 209 | $instance[ $key ] = max( $instance[ $key ], $setting['min'] ); |
| 210 | } |
| 211 | |
| 212 | if ( isset( $setting['max'] ) && '' !== $setting['max'] ) { |
| 213 | $instance[ $key ] = min( $instance[ $key ], $setting['max'] ); |
| 214 | } |
| 215 | break; |
| 216 | case 'textarea': |
| 217 | $instance[ $key ] = wp_kses( trim( wp_unslash( $new_instance[ $key ] ) ), wp_kses_allowed_html( 'post' ) ); |
| 218 | break; |
| 219 | case 'checkbox': |
| 220 | $instance[ $key ] = empty( $new_instance[ $key ] ) ? 0 : 1; |
| 221 | break; |
| 222 | default: |
| 223 | $instance[ $key ] = isset( $new_instance[ $key ] ) ? sanitize_text_field( $new_instance[ $key ] ) : $setting['std']; |
| 224 | break; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Sanitize the value of a setting. |
| 229 | */ |
| 230 | $instance[ $key ] = apply_filters( 'woocommerce_widget_settings_sanitize_option', $instance[ $key ], $new_instance, $key, $setting ); |
| 231 | } |
| 232 | |
| 233 | $this->flush_widget_cache(); |
| 234 | |
| 235 | return $instance; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Outputs the settings update form. |
| 240 | * |
| 241 | * @see WP_Widget->form |
| 242 | * |
| 243 | * @param array $instance Instance. |
| 244 | */ |
| 245 | public function form( $instance ) { |
| 246 | |
| 247 | if ( empty( $this->settings ) ) { |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | foreach ( $this->settings as $key => $setting ) { |
| 252 | |
| 253 | $class = isset( $setting['class'] ) ? $setting['class'] : ''; |
| 254 | $value = isset( $instance[ $key ] ) ? $instance[ $key ] : $setting['std']; |
| 255 | |
| 256 | switch ( $setting['type'] ) { |
| 257 | |
| 258 | case 'text': |
| 259 | ?> |
| 260 | <p> |
| 261 | <label for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo wp_kses_post( $setting['label'] ); ?></label><?php // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped ?> |
| 262 | <input class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>" type="text" value="<?php echo esc_attr( $value ); ?>" /> |
| 263 | </p> |
| 264 | <?php |
| 265 | break; |
| 266 | |
| 267 | case 'number': |
| 268 | ?> |
| 269 | <p> |
| 270 | <label for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo $setting['label']; /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></label> |
| 271 | <input class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>" type="number" step="<?php echo esc_attr( $setting['step'] ); ?>" min="<?php echo esc_attr( $setting['min'] ); ?>" max="<?php echo esc_attr( $setting['max'] ); ?>" value="<?php echo esc_attr( $value ); ?>" /> |
| 272 | </p> |
| 273 | <?php |
| 274 | break; |
| 275 | |
| 276 | case 'select': |
| 277 | ?> |
| 278 | <p> |
| 279 | <label for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo $setting['label']; /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></label> |
| 280 | <select class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>"> |
| 281 | <?php foreach ( $setting['options'] as $option_key => $option_value ) : ?> |
| 282 | <option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $option_key, $value ); ?>><?php echo esc_html( $option_value ); ?></option> |
| 283 | <?php endforeach; ?> |
| 284 | </select> |
| 285 | </p> |
| 286 | <?php |
| 287 | break; |
| 288 | |
| 289 | case 'textarea': |
| 290 | ?> |
| 291 | <p> |
| 292 | <label for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo $setting['label']; /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></label> |
| 293 | <textarea class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>" cols="20" rows="3"><?php echo esc_textarea( $value ); ?></textarea> |
| 294 | <?php if ( isset( $setting['desc'] ) ) : ?> |
| 295 | <small><?php echo esc_html( $setting['desc'] ); ?></small> |
| 296 | <?php endif; ?> |
| 297 | </p> |
| 298 | <?php |
| 299 | break; |
| 300 | |
| 301 | case 'checkbox': |
| 302 | ?> |
| 303 | <p> |
| 304 | <input class="checkbox <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>" type="checkbox" value="1" <?php checked( $value, 1 ); ?> /> |
| 305 | <label for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo $setting['label']; /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></label> |
| 306 | </p> |
| 307 | <?php |
| 308 | break; |
| 309 | |
| 310 | // Default: run an action. |
| 311 | default: |
| 312 | do_action( 'woocommerce_widget_field_' . $setting['type'], $key, $value, $setting, $instance ); |
| 313 | break; |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Get current page URL with various filtering props supported by WC. |
| 320 | * |
| 321 | * @return string |
| 322 | * @since 3.3.0 |
| 323 | */ |
| 324 | protected function get_current_page_url() { |
| 325 | if ( Constants::is_defined( 'SHOP_IS_ON_FRONT' ) ) { |
| 326 | $link = home_url(); |
| 327 | } elseif ( is_shop() ) { |
| 328 | $link = get_permalink( wc_get_page_id( 'shop' ) ); |
| 329 | } elseif ( is_product_category() ) { |
| 330 | $link = get_term_link( get_query_var( 'product_cat' ), 'product_cat' ); |
| 331 | } elseif ( is_product_tag() ) { |
| 332 | $link = get_term_link( get_query_var( 'product_tag' ), 'product_tag' ); |
| 333 | } else { |
| 334 | $queried_object = get_queried_object(); |
| 335 | $link = get_term_link( $queried_object->slug, $queried_object->taxonomy ); |
| 336 | } |
| 337 | |
| 338 | // Min/Max. |
| 339 | if ( isset( $_GET['min_price'] ) ) { |
| 340 | $link = add_query_arg( 'min_price', wc_clean( wp_unslash( $_GET['min_price'] ) ), $link ); |
| 341 | } |
| 342 | |
| 343 | if ( isset( $_GET['max_price'] ) ) { |
| 344 | $link = add_query_arg( 'max_price', wc_clean( wp_unslash( $_GET['max_price'] ) ), $link ); |
| 345 | } |
| 346 | |
| 347 | // Order by. |
| 348 | if ( isset( $_GET['orderby'] ) ) { |
| 349 | $link = add_query_arg( 'orderby', wc_clean( wp_unslash( $_GET['orderby'] ) ), $link ); |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Search Arg. |
| 354 | * To support quote characters, first they are decoded from " entities, then URL encoded. |
| 355 | */ |
| 356 | if ( get_search_query() ) { |
| 357 | $link = add_query_arg( 's', rawurlencode( htmlspecialchars_decode( get_search_query() ) ), $link ); |
| 358 | } |
| 359 | |
| 360 | // Post Type Arg. |
| 361 | if ( isset( $_GET['post_type'] ) ) { |
| 362 | $link = add_query_arg( 'post_type', wc_clean( wp_unslash( $_GET['post_type'] ) ), $link ); |
| 363 | |
| 364 | // Prevent post type and page id when pretty permalinks are disabled. |
| 365 | if ( is_shop() ) { |
| 366 | $link = remove_query_arg( 'page_id', $link ); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | // Min Rating Arg. |
| 371 | if ( isset( $_GET['rating_filter'] ) ) { |
| 372 | $link = add_query_arg( 'rating_filter', wc_clean( wp_unslash( $_GET['rating_filter'] ) ), $link ); |
| 373 | } |
| 374 | |
| 375 | // All current filters. |
| 376 | if ( $_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes() ) { // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.FoundInControlStructure, WordPress.CodeAnalysis.AssignmentInCondition.Found |
| 377 | foreach ( $_chosen_attributes as $name => $data ) { |
| 378 | $filter_name = wc_attribute_taxonomy_slug( $name ); |
| 379 | if ( ! empty( $data['terms'] ) ) { |
| 380 | $link = add_query_arg( 'filter_' . $filter_name, implode( ',', $data['terms'] ), $link ); |
| 381 | } |
| 382 | if ( 'or' === $data['query_type'] ) { |
| 383 | $link = add_query_arg( 'query_type_' . $filter_name, 'or', $link ); |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | return apply_filters( 'woocommerce_widget_get_current_page_url', $link, $this ); |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * Get widget id plus scheme/protocol to prevent serving mixed content from (persistently) cached widgets. |
| 393 | * |
| 394 | * @since 3.4.0 |
| 395 | * @param string $widget_id Id of the cached widget. |
| 396 | * @param string $scheme Scheme for the widget id. |
| 397 | * @return string Widget id including scheme/protocol. |
| 398 | */ |
| 399 | protected function get_widget_id_for_cache( $widget_id, $scheme = '' ) { |
| 400 | if ( $scheme ) { |
| 401 | $widget_id_for_cache = $widget_id . '-' . $scheme; |
| 402 | } else { |
| 403 | $widget_id_for_cache = $widget_id . '-' . ( is_ssl() ? 'https' : 'http' ); |
| 404 | } |
| 405 | |
| 406 | return apply_filters( 'woocommerce_cached_widget_id', $widget_id_for_cache ); |
| 407 | } |
| 408 | } |
| 409 |