if ( ! defined( 'ABSPATH' ) ) { exit; } /** * List products. One widget to rule them all. * * @author WooThemes * @category Widgets * @package WooCommerce/Widgets * @version 2.3.0 * @extends WC_Widget */ class WC_Widget_Products extends WC_Widget { /** * Constructor. */ public function __construct() { $this->widget_cssclass = 'woocommerce widget_products'; $this->widget_description = __( 'Display a list of your products on your site.', 'woocommerce' ); $this->widget_id = 'woocommerce_products'; $this->widget_name = __( 'WooCommerce products', 'woocommerce' ); $this->settings = array( 'title' => array( 'type' => 'text', 'std' => __( 'Products', 'woocommerce' ), 'label' => __( 'Title', 'woocommerce' ), ), 'number' => array( 'type' => 'number', 'step' => 1, 'min' => 1, 'max' => '', 'std' => 5, 'label' => __( 'Number of products to show', 'woocommerce' ), ), 'show' => array( 'type' => 'select', 'std' => '', 'label' => __( 'Show', 'woocommerce' ), 'options' => array( '' => __( 'All products', 'woocommerce' ), 'featured' => __( 'Featured products', 'woocommerce' ), 'onsale' => __( 'On-sale products', 'woocommerce' ), ), ), 'orderby' => array( 'type' => 'select', 'std' => 'date', 'label' => __( 'Order by', 'woocommerce' ), 'options' => array( 'date' => __( 'Date', 'woocommerce' ), 'price' => __( 'Price', 'woocommerce' ), 'rand' => __( 'Random', 'woocommerce' ), 'sales' => __( 'Sales', 'woocommerce' ), ), ), 'order' => array( 'type' => 'select', 'std' => 'desc', 'label' => _x( 'Order', 'Sorting order', 'woocommerce' ), 'options' => array( 'asc' => __( 'ASC', 'woocommerce' ), 'desc' => __( 'DESC', 'woocommerce' ), ), ), 'hide_free' => array( 'type' => 'checkbox', 'std' => 0, 'label' => __( 'Hide free products', 'woocommerce' ), ), 'show_hidden' => array( 'type' => 'checkbox', 'std' => 0, 'label' => __( 'Show hidden products', 'woocommerce' ), ), ); parent::__construct(); } /** * Query the products and return them. * @param array $args * @param array $instance * @return WP_Query */ public function get_products( $args, $instance ) { $number = ! empty( $instance['number'] ) ? absint( $instance['number'] ) : $this->settings['number']['std']; $show = ! empty( $instance['show'] ) ? sanitize_title( $instance['show'] ) : $this->settings['show']['std']; $orderby = ! empty( $instance['orderby'] ) ? sanitize_title( $instance['orderby'] ) : $this->settings['orderby']['std']; $order = ! empty( $instance['order'] ) ? sanitize_title( $instance['order'] ) : $this->settings['order']['std']; $product_visibility_term_ids = wc_get_product_visibility_term_ids(); $query_args = array( 'posts_per_page' => $number, 'post_status' => 'publish', 'post_type' => 'product', 'no_found_rows' => 1, 'order' => $order, 'meta_query' => array(), 'tax_query' => array( 'relation' => 'AND', ), ); if ( empty( $instance['show_hidden'] ) ) { $query_args['tax_query'][] = array( 'taxonomy' => 'product_visibility', 'field' => 'term_taxonomy_id', 'terms' => is_search() ? $product_visibility_term_ids['exclude-from-search'] : $product_visibility_term_ids['exclude-from-catalog'], 'operator' => 'NOT IN', ); $query_args['post_parent'] = 0; } if ( ! empty( $instance['hide_free'] ) ) { $query_args['meta_query'][] = array( 'key' => '_price', 'value' => 0, 'compare' => '>', 'type' => 'DECIMAL', ); } if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) { $query_args['tax_query'] = array( array( 'taxonomy' => 'product_visibility', 'field' => 'term_taxonomy_id', 'terms' => $product_visibility_term_ids['outofstock'], 'operator' => 'NOT IN', ), ); } switch ( $show ) { case 'featured' : $query_args['tax_query'][] = array( 'taxonomy' => 'product_visibility', 'field' => 'term_taxonomy_id', 'terms' => $product_visibility_term_ids['featured'], ); break; case 'onsale' : $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 ) { case 'price' : $query_args['meta_key'] = '_price'; $query_args['orderby'] = 'meta_value_num'; break; case 'rand' : $query_args['orderby'] = 'rand'; break; case 'sales' : $query_args['meta_key'] = 'total_sales'; $query_args['orderby'] = 'meta_value_num'; break; default : $query_args['orderby'] = 'date'; } return new WP_Query( apply_filters( 'woocommerce_products_widget_query_args', $query_args ) ); } /** * Output widget. * * @see WP_Widget * * @param array $args * @param array $instance */ public function widget( $args, $instance ) { if ( $this->get_cached_widget( $args ) ) { return; } ob_start(); if ( ( $products = $this->get_products( $args, $instance ) ) && $products->have_posts() ) { $this->widget_start( $args, $instance ); echo apply_filters( 'woocommerce_before_widget_product_list', '' ); $this->widget_end( $args ); } wp_reset_postdata(); echo $this->cache_widget( $args, ob_get_clean() ); } } if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Rating Filter Widget and related functions. * * * @author WooThemes * @category Widgets * @package WooCommerce/Widgets * @version 2.6.0 * @extends WC_Widget */ class WC_Widget_Rating_Filter extends WC_Widget { /** * Constructor. */ public function __construct() { $this->widget_cssclass = 'woocommerce widget_rating_filter'; $this->widget_description = __( 'Filter products by rating when viewing product archives and categories.', 'woocommerce' ); $this->widget_id = 'woocommerce_rating_filter'; $this->widget_name = __( 'WooCommerce average rating filter', 'woocommerce' ); $this->settings = array( 'title' => array( 'type' => 'text', 'std' => __( 'Average rating', 'woocommerce' ), 'label' => __( 'Title', 'woocommerce' ), ), ); parent::__construct(); } /** * Get current page URL for layered nav items. * @return string */ protected function get_page_base_url() { if ( defined( 'SHOP_IS_ON_FRONT' ) ) { $link = home_url(); } elseif ( is_post_type_archive( 'product' ) || is_page( wc_get_page_id( 'shop' ) ) ) { $link = get_post_type_archive_link( 'product' ); } else { $link = get_term_link( get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); } // Min/Max if ( isset( $_GET['min_price'] ) ) { $link = add_query_arg( 'min_price', wc_clean( $_GET['min_price'] ), $link ); } if ( isset( $_GET['max_price'] ) ) { $link = add_query_arg( 'max_price', wc_clean( $_GET['max_price'] ), $link ); } // Orderby if ( isset( $_GET['orderby'] ) ) { $link = add_query_arg( 'orderby', wc_clean( $_GET['orderby'] ), $link ); } /** * Search Arg. * To support quote characters, first they are decoded from " entities, then URL encoded. */ if ( get_search_query() ) { $link = add_query_arg( 's', rawurlencode( htmlspecialchars_decode( get_search_query() ) ), $link ); } // Post Type Arg if ( isset( $_GET['post_type'] ) ) { $link = add_query_arg( 'post_type', wc_clean( $_GET['post_type'] ), $link ); } // All current filters if ( $_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes() ) { foreach ( $_chosen_attributes as $name => $data ) { $filter_name = sanitize_title( str_replace( 'pa_', '', $name ) ); if ( ! empty( $data['terms'] ) ) { $link = add_query_arg( 'filter_' . $filter_name, implode( ',', $data['terms'] ), $link ); } if ( 'or' == $data['query_type'] ) { $link = add_query_arg( 'query_type_' . $filter_name, 'or', $link ); } } } return $link; } /** * Count products after other filters have occurred by adjusting the main query. * @param int $rating * @return int */ protected function get_filtered_product_count( $rating ) { global $wpdb; $tax_query = WC_Query::get_main_tax_query(); $meta_query = WC_Query::get_main_meta_query(); // Unset current rating filter. foreach ( $tax_query as $key => $query ) { if ( ! empty( $query['rating_filter'] ) ) { unset( $tax_query[ $key ] ); break; } } // Set new rating filter. $product_visibility_terms = wc_get_product_visibility_term_ids(); $tax_query[] = array( 'taxonomy' => 'product_visibility', 'field' => 'term_taxonomy_id', 'terms' => $product_visibility_terms[ 'rated-' . $rating ], 'operator' => 'IN', 'rating_filter' => true, ); $meta_query = new WP_Meta_Query( $meta_query ); $tax_query = new WP_Tax_Query( $tax_query ); $meta_query_sql = $meta_query->get_sql( 'post', $wpdb->posts, 'ID' ); $tax_query_sql = $tax_query->get_sql( $wpdb->posts, 'ID' ); $sql = "SELECT COUNT( DISTINCT {$wpdb->posts}.ID ) FROM {$wpdb->posts} "; $sql .= $tax_query_sql['join'] . $meta_query_sql['join']; $sql .= " WHERE {$wpdb->posts}.post_type = 'product' AND {$wpdb->posts}.post_status = 'publish' "; $sql .= $tax_query_sql['where'] . $meta_query_sql['where']; if ( $search = WC_Query::get_main_search_query_sql() ) { $sql .= ' AND ' . $search; } return absint( $wpdb->get_var( $sql ) ); } /** * widget function. * * @see WP_Widget * * @param array $args * @param array $instance */ public function widget( $args, $instance ) { global $wp_the_query; if ( ! is_post_type_archive( 'product' ) && ! is_tax( get_object_taxonomies( 'product' ) ) ) { return; } if ( ! $wp_the_query->post_count ) { return; } ob_start(); $found = false; $rating_filter = isset( $_GET['rating_filter'] ) ? array_filter( array_map( 'absint', explode( ',', $_GET['rating_filter'] ) ) ) : array(); $this->widget_start( $args, $instance ); echo ''; $this->widget_end( $args ); if ( ! $found ) { ob_end_clean(); } else { echo ob_get_clean(); } } } if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Recent Reviews Widget. * * @author WooThemes * @category Widgets * @package WooCommerce/Widgets * @version 2.3.0 * @extends WC_Widget */ class WC_Widget_Recent_Reviews extends WC_Widget { /** * Constructor. */ public function __construct() { $this->widget_cssclass = 'woocommerce widget_recent_reviews'; $this->widget_description = __( 'Display a list of your most recent reviews on your site.', 'woocommerce' ); $this->widget_id = 'woocommerce_recent_reviews'; $this->widget_name = __( 'WooCommerce recent reviews', 'woocommerce' ); $this->settings = array( 'title' => array( 'type' => 'text', 'std' => __( 'Recent reviews', 'woocommerce' ), 'label' => __( 'Title', 'woocommerce' ), ), 'number' => array( 'type' => 'number', 'step' => 1, 'min' => 1, 'max' => '', 'std' => 10, 'label' => __( 'Number of reviews to show', 'woocommerce' ), ), ); parent::__construct(); } /** * Output widget. * * @see WP_Widget * * @param array $args * @param array $instance */ public function widget( $args, $instance ) { global $comments, $comment; if ( $this->get_cached_widget( $args ) ) { return; } ob_start(); $number = ! empty( $instance['number'] ) ? absint( $instance['number'] ) : $this->settings['number']['std']; $comments = get_comments( array( 'number' => $number, 'status' => 'approve', 'post_status' => 'publish', 'post_type' => 'product', 'parent' => 0 ) ); if ( $comments ) { $this->widget_start( $args, $instance ); echo ''; $this->widget_end( $args ); } $content = ob_get_clean(); echo $content; $this->cache_widget( $args, $content ); } } if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Recent Products Widget. * * @author WooThemes * @category Widgets * @package WooCommerce/Widgets * @version 2.3.0 * @extends WC_Widget */ class WC_Widget_Recently_Viewed extends WC_Widget { /** * Constructor. */ public function __construct() { $this->widget_cssclass = 'woocommerce widget_recently_viewed_products'; $this->widget_description = __( 'Display a list of recently viewed products.', 'woocommerce' ); $this->widget_id = 'woocommerce_recently_viewed_products'; $this->widget_name = __( 'WooCommerce recently viewed', 'woocommerce' ); $this->settings = array( 'title' => array( 'type' => 'text', 'std' => __( 'Recently viewed products', 'woocommerce' ), 'label' => __( 'Title', 'woocommerce' ), ), 'number' => array( 'type' => 'number', 'step' => 1, 'min' => 1, 'max' => '', 'std' => 10, 'label' => __( 'Number of products to show', 'woocommerce' ), ), ); parent::__construct(); } /** * Output widget. * * @see WP_Widget * * @param array $args * @param array $instance */ public function widget( $args, $instance ) { $viewed_products = ! empty( $_COOKIE['woocommerce_recently_viewed'] ) ? (array) explode( '|', $_COOKIE['woocommerce_recently_viewed'] ) : array(); $viewed_products = array_reverse( array_filter( array_map( 'absint', $viewed_products ) ) ); if ( empty( $viewed_products ) ) { return; } ob_start(); $number = ! empty( $instance['number'] ) ? absint( $instance['number'] ) : $this->settings['number']['std']; $query_args = array( 'posts_per_page' => $number, 'no_found_rows' => 1, 'post_status' => 'publish', 'post_type' => 'product', 'post__in' => $viewed_products, 'orderby' => 'post__in', ); if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) { $query_args['tax_query'] = array( array( 'taxonomy' => 'product_visibility', 'field' => 'name', 'terms' => 'outofstock', 'operator' => 'NOT IN', ), ); } $r = new WP_Query( $query_args ); if ( $r->have_posts() ) { $this->widget_start( $args, $instance ); echo apply_filters( 'woocommerce_before_widget_product_list', '' ); $this->widget_end( $args ); } wp_reset_postdata(); $content = ob_get_clean(); echo $content; } }