diff --git a/includes/abstracts/abstract-wc-widget.php b/includes/abstracts/abstract-wc-widget.php index 50f15b82cdf..038d57efd46 100644 --- a/includes/abstracts/abstract-wc-widget.php +++ b/includes/abstracts/abstract-wc-widget.php @@ -36,7 +36,7 @@ abstract class WC_Widget extends WP_Widget { /** * get_cached_widget function. */ - function get_cached_widget( $args ) { + public function get_cached_widget( $args ) { $cache = wp_cache_get( apply_filters( 'woocommerce_cached_widget_id', $this->widget_id ), 'widget' ); @@ -54,12 +54,14 @@ abstract class WC_Widget extends WP_Widget { /** * Cache the widget - * @param string $content + * @param array $args + * @param string $content + * @return string the content that was cached */ public function cache_widget( $args, $content ) { - $cache[ $args['widget_id'] ] = $content; + wp_cache_set( apply_filters( 'woocommerce_cached_widget_id', $this->widget_id ), array( $args['widget_id'] => $content ), 'widget' ); - wp_cache_set( apply_filters( 'woocommerce_cached_widget_id', $this->widget_id ), $cache, 'widget' ); + return $content; } /** @@ -71,6 +73,30 @@ abstract class WC_Widget extends WP_Widget { wp_cache_delete( apply_filters( 'woocommerce_cached_widget_id', $this->widget_id ), 'widget' ); } + /** + * Output the html at the start of a widget + * @param array $args + */ + public function widget_start( $args, $instance ) { + extract( $args ); + + if ( $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) ) { + echo $before_widget . $before_title . $title . $after_title; + } else { + echo $before_widget; + } + } + + /** + * Output the html at the end of a widget + * @param array $args + */ + public function widget_end( $args ) { + extract( $args ); + + echo $after_widget; + } + /** * update function. * diff --git a/includes/widgets/class-wc-widget-products.php b/includes/widgets/class-wc-widget-products.php index ca4daed56c7..bd33abdfbf2 100644 --- a/includes/widgets/class-wc-widget-products.php +++ b/includes/widgets/class-wc-widget-products.php @@ -82,39 +82,21 @@ class WC_Widget_Products extends WC_Widget { } /** - * widget function. - * - * @see WP_Widget - * @access public - * @param array $args - * @param array $instance - * @return void + * Query the products and return them + * @param array $args + * @param array $instance + * @return WP_Query */ - public function widget( $args, $instance ) { - - if ( $this->get_cached_widget( $args ) ) - return; - - ob_start(); - extract( $args ); - - $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ); - $number = absint( $instance['number'] ); - $show = sanitize_title( $instance['show'] ); - $orderby = sanitize_title( $instance['orderby'] ); - $order = sanitize_title( $instance['order'] ); - $show_rating = false; - + public function get_products( $args, $instance ) { $query_args = array( - 'posts_per_page' => $number, - 'post_status' => 'publish', - 'post_type' => 'product', - 'no_found_rows' => 1, - 'order' => $order == 'asc' ? 'asc' : 'desc' + 'posts_per_page' => absint( $instance['number'] ), + 'post_status' => 'publish', + 'post_type' => 'product', + 'no_found_rows' => 1, + 'order' => $instance['order'] === 'asc' ? 'asc' : 'desc', + 'meta_query' => array() ); - $query_args['meta_query'] = array(); - if ( empty( $instance['show_hidden'] ) ) { $query_args['meta_query'][] = WC()->query->visibility_meta_query(); $query_args['post_parent'] = 0; @@ -132,7 +114,7 @@ class WC_Widget_Products extends WC_Widget { $query_args['meta_query'][] = WC()->query->stock_status_meta_query(); $query_args['meta_query'] = array_filter( $query_args['meta_query'] ); - switch ( $show ) { + switch ( $instance['show'] ) { case 'featured' : $query_args['meta_query'][] = array( 'key' => '_featured', @@ -140,13 +122,13 @@ class WC_Widget_Products extends WC_Widget { ); break; case 'onsale' : - $product_ids_on_sale = wc_get_product_ids_on_sale(); - $product_ids_on_sale[] = 0; + $product_ids_on_sale = wc_get_product_ids_on_sale(); + $product_ids_on_sale[] = 0; $query_args['post__in'] = $product_ids_on_sale; break; } - switch ( $orderby ) { + switch ( $instance['orderby'] ) { case 'price' : $query_args['meta_key'] = '_price'; $query_args['orderby'] = 'meta_value_num'; @@ -162,33 +144,42 @@ class WC_Widget_Products extends WC_Widget { $query_args['orderby'] = 'date'; } - $r = new WP_Query( $query_args ); + return new WP_Query( $query_args ); + } - if ( $r->have_posts() ) { + /** + * widget function. + * + * @see WP_Widget + * @access public + * @param array $args + * @param array $instance + * @return void + */ + public function widget( $args, $instance ) { + if ( $this->get_cached_widget( $args ) ) { + return; + } - echo $before_widget; + ob_start(); - if ( $title ) - echo $before_title . $title . $after_title; + if ( ( $products = $this->get_products( $args, $instance ) ) && $products->have_posts() ) { + $this->widget_start( $args, $instance ); echo ''; - echo $after_widget; + $this->widget_end( $args, $instance ); } wp_reset_postdata(); - $content = ob_get_clean(); - - echo $content; - - $this->cache_widget( $args, $content ); + echo $this->cache_widget( $args, ob_get_clean() ); } }