PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce/includes/widgets/class-wc-widget-products.php

https://gitlab.com/webkod3r/tripolis
PHP | 190 lines | 143 code | 21 blank | 26 comment | 9 complexity | cc676dfcfc89cf994649d5126869685b MD5 | raw file
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit;
  4. }
  5. /**
  6. * List products. One widget to rule them all.
  7. *
  8. * @author WooThemes
  9. * @category Widgets
  10. * @package WooCommerce/Widgets
  11. * @version 2.3.0
  12. * @extends WC_Widget
  13. */
  14. class WC_Widget_Products extends WC_Widget {
  15. /**
  16. * Constructor.
  17. */
  18. public function __construct() {
  19. $this->widget_cssclass = 'woocommerce widget_products';
  20. $this->widget_description = __( 'Display a list of your products on your site.', 'woocommerce' );
  21. $this->widget_id = 'woocommerce_products';
  22. $this->widget_name = __( 'WooCommerce Products', 'woocommerce' );
  23. $this->settings = array(
  24. 'title' => array(
  25. 'type' => 'text',
  26. 'std' => __( 'Products', 'woocommerce' ),
  27. 'label' => __( 'Title', 'woocommerce' )
  28. ),
  29. 'number' => array(
  30. 'type' => 'number',
  31. 'step' => 1,
  32. 'min' => 1,
  33. 'max' => '',
  34. 'std' => 5,
  35. 'label' => __( 'Number of products to show', 'woocommerce' )
  36. ),
  37. 'show' => array(
  38. 'type' => 'select',
  39. 'std' => '',
  40. 'label' => __( 'Show', 'woocommerce' ),
  41. 'options' => array(
  42. '' => __( 'All Products', 'woocommerce' ),
  43. 'featured' => __( 'Featured Products', 'woocommerce' ),
  44. 'onsale' => __( 'On-sale Products', 'woocommerce' ),
  45. )
  46. ),
  47. 'orderby' => array(
  48. 'type' => 'select',
  49. 'std' => 'date',
  50. 'label' => __( 'Order by', 'woocommerce' ),
  51. 'options' => array(
  52. 'date' => __( 'Date', 'woocommerce' ),
  53. 'price' => __( 'Price', 'woocommerce' ),
  54. 'rand' => __( 'Random', 'woocommerce' ),
  55. 'sales' => __( 'Sales', 'woocommerce' ),
  56. )
  57. ),
  58. 'order' => array(
  59. 'type' => 'select',
  60. 'std' => 'desc',
  61. 'label' => _x( 'Order', 'Sorting order', 'woocommerce' ),
  62. 'options' => array(
  63. 'asc' => __( 'ASC', 'woocommerce' ),
  64. 'desc' => __( 'DESC', 'woocommerce' ),
  65. )
  66. ),
  67. 'hide_free' => array(
  68. 'type' => 'checkbox',
  69. 'std' => 0,
  70. 'label' => __( 'Hide free products', 'woocommerce' )
  71. ),
  72. 'show_hidden' => array(
  73. 'type' => 'checkbox',
  74. 'std' => 0,
  75. 'label' => __( 'Show hidden products', 'woocommerce' )
  76. )
  77. );
  78. parent::__construct();
  79. }
  80. /**
  81. * Query the products and return them.
  82. * @param array $args
  83. * @param array $instance
  84. * @return WP_Query
  85. */
  86. public function get_products( $args, $instance ) {
  87. $number = ! empty( $instance['number'] ) ? absint( $instance['number'] ) : $this->settings['number']['std'];
  88. $show = ! empty( $instance['show'] ) ? sanitize_title( $instance['show'] ) : $this->settings['show']['std'];
  89. $orderby = ! empty( $instance['orderby'] ) ? sanitize_title( $instance['orderby'] ) : $this->settings['orderby']['std'];
  90. $order = ! empty( $instance['order'] ) ? sanitize_title( $instance['order'] ) : $this->settings['order']['std'];
  91. $query_args = array(
  92. 'posts_per_page' => $number,
  93. 'post_status' => 'publish',
  94. 'post_type' => 'product',
  95. 'no_found_rows' => 1,
  96. 'order' => $order,
  97. 'meta_query' => array()
  98. );
  99. if ( empty( $instance['show_hidden'] ) ) {
  100. $query_args['meta_query'][] = WC()->query->visibility_meta_query();
  101. $query_args['post_parent'] = 0;
  102. }
  103. if ( ! empty( $instance['hide_free'] ) ) {
  104. $query_args['meta_query'][] = array(
  105. 'key' => '_price',
  106. 'value' => 0,
  107. 'compare' => '>',
  108. 'type' => 'DECIMAL',
  109. );
  110. }
  111. $query_args['meta_query'][] = WC()->query->stock_status_meta_query();
  112. $query_args['meta_query'] = array_filter( $query_args['meta_query'] );
  113. switch ( $show ) {
  114. case 'featured' :
  115. $query_args['meta_query'][] = array(
  116. 'key' => '_featured',
  117. 'value' => 'yes'
  118. );
  119. break;
  120. case 'onsale' :
  121. $product_ids_on_sale = wc_get_product_ids_on_sale();
  122. $product_ids_on_sale[] = 0;
  123. $query_args['post__in'] = $product_ids_on_sale;
  124. break;
  125. }
  126. switch ( $orderby ) {
  127. case 'price' :
  128. $query_args['meta_key'] = '_price';
  129. $query_args['orderby'] = 'meta_value_num';
  130. break;
  131. case 'rand' :
  132. $query_args['orderby'] = 'rand';
  133. break;
  134. case 'sales' :
  135. $query_args['meta_key'] = 'total_sales';
  136. $query_args['orderby'] = 'meta_value_num';
  137. break;
  138. default :
  139. $query_args['orderby'] = 'date';
  140. }
  141. return new WP_Query( apply_filters( 'woocommerce_products_widget_query_args', $query_args ) );
  142. }
  143. /**
  144. * Output widget.
  145. *
  146. * @see WP_Widget
  147. *
  148. * @param array $args
  149. * @param array $instance
  150. */
  151. public function widget( $args, $instance ) {
  152. if ( $this->get_cached_widget( $args ) ) {
  153. return;
  154. }
  155. ob_start();
  156. if ( ( $products = $this->get_products( $args, $instance ) ) && $products->have_posts() ) {
  157. $this->widget_start( $args, $instance );
  158. echo apply_filters( 'woocommerce_before_widget_product_list', '<ul class="product_list_widget">' );
  159. while ( $products->have_posts() ) {
  160. $products->the_post();
  161. wc_get_template( 'content-widget-product.php', array( 'show_rating' => false ) );
  162. }
  163. echo apply_filters( 'woocommerce_after_widget_product_list', '</ul>' );
  164. $this->widget_end( $args );
  165. }
  166. wp_reset_postdata();
  167. echo $this->cache_widget( $args, ob_get_clean() );
  168. }
  169. }