Split up the product widget methods

This commit is contained in:
Mike Jolley 2014-10-29 11:07:02 +00:00
parent 9be06dd9e5
commit 69ca69ce52
2 changed files with 68 additions and 51 deletions

View File

@ -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 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.
*

View File

@ -82,39 +82,21 @@ class WC_Widget_Products extends WC_Widget {
}
/**
* widget function.
*
* @see WP_Widget
* @access public
* Query the products and return them
* @param array $args
* @param array $instance
* @return void
* @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,
'posts_per_page' => absint( $instance['number'] ),
'post_status' => 'publish',
'post_type' => 'product',
'no_found_rows' => 1,
'order' => $order == 'asc' ? 'asc' : 'desc'
'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',
@ -146,7 +128,7 @@ class WC_Widget_Products extends WC_Widget {
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 '<ul class="product_list_widget">';
while ( $r->have_posts()) {
$r->the_post();
wc_get_template( 'content-widget-product.php', array( 'show_rating' => $show_rating ) );
while ( $products->have_posts() ) {
$products->the_post();
wc_get_template( 'content-widget-product.php', array( 'show_rating' => false ) );
}
echo '</ul>';
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() );
}
}