woothemes /
woocommerce
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
||
| 4 | exit; |
||
| 5 | } |
||
| 6 | |||
| 7 | /** |
||
| 8 | * List products. One widget to rule them all. |
||
| 9 | * |
||
| 10 | * @author WooThemes |
||
| 11 | * @category Widgets |
||
| 12 | * @package WooCommerce/Widgets |
||
| 13 | * @version 2.3.0 |
||
| 14 | * @extends WC_Widget |
||
| 15 | */ |
||
| 16 | class WC_Widget_Products extends WC_Widget { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Constructor. |
||
| 20 | */ |
||
| 21 | public function __construct() { |
||
| 22 | $this->widget_cssclass = 'woocommerce widget_products'; |
||
| 23 | $this->widget_description = __( 'Display a list of your products on your site.', 'woocommerce' ); |
||
| 24 | $this->widget_id = 'woocommerce_products'; |
||
| 25 | $this->widget_name = __( 'WooCommerce Products', 'woocommerce' ); |
||
| 26 | $this->settings = array( |
||
| 27 | 'title' => array( |
||
| 28 | 'type' => 'text', |
||
| 29 | 'std' => __( 'Products', 'woocommerce' ), |
||
| 30 | 'label' => __( 'Title', 'woocommerce' ) |
||
| 31 | ), |
||
| 32 | 'number' => array( |
||
| 33 | 'type' => 'number', |
||
| 34 | 'step' => 1, |
||
| 35 | 'min' => 1, |
||
| 36 | 'max' => '', |
||
| 37 | 'std' => 5, |
||
| 38 | 'label' => __( 'Number of products to show', 'woocommerce' ) |
||
| 39 | ), |
||
| 40 | 'show' => array( |
||
| 41 | 'type' => 'select', |
||
| 42 | 'std' => '', |
||
| 43 | 'label' => __( 'Show', 'woocommerce' ), |
||
| 44 | 'options' => array( |
||
| 45 | '' => __( 'All Products', 'woocommerce' ), |
||
| 46 | 'featured' => __( 'Featured Products', 'woocommerce' ), |
||
| 47 | 'onsale' => __( 'On-sale Products', 'woocommerce' ), |
||
| 48 | ) |
||
| 49 | ), |
||
| 50 | 'orderby' => array( |
||
| 51 | 'type' => 'select', |
||
| 52 | 'std' => 'date', |
||
| 53 | 'label' => __( 'Order by', 'woocommerce' ), |
||
| 54 | 'options' => array( |
||
| 55 | 'date' => __( 'Date', 'woocommerce' ), |
||
| 56 | 'price' => __( 'Price', 'woocommerce' ), |
||
| 57 | 'rand' => __( 'Random', 'woocommerce' ), |
||
| 58 | 'sales' => __( 'Sales', 'woocommerce' ), |
||
| 59 | ) |
||
| 60 | ), |
||
| 61 | 'order' => array( |
||
| 62 | 'type' => 'select', |
||
| 63 | 'std' => 'desc', |
||
| 64 | 'label' => _x( 'Order', 'Sorting order', 'woocommerce' ), |
||
| 65 | 'options' => array( |
||
| 66 | 'asc' => __( 'ASC', 'woocommerce' ), |
||
| 67 | 'desc' => __( 'DESC', 'woocommerce' ), |
||
| 68 | ) |
||
| 69 | ), |
||
| 70 | 'hide_free' => array( |
||
| 71 | 'type' => 'checkbox', |
||
| 72 | 'std' => 0, |
||
| 73 | 'label' => __( 'Hide free products', 'woocommerce' ) |
||
| 74 | ), |
||
| 75 | 'show_hidden' => array( |
||
| 76 | 'type' => 'checkbox', |
||
| 77 | 'std' => 0, |
||
| 78 | 'label' => __( 'Show hidden products', 'woocommerce' ) |
||
| 79 | ) |
||
| 80 | ); |
||
| 81 | |||
| 82 | parent::__construct(); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Query the products and return them. |
||
| 87 | * @param array $args |
||
| 88 | * @param array $instance |
||
| 89 | * @return WP_Query |
||
| 90 | */ |
||
| 91 | public function get_products( $args, $instance ) { |
||
| 92 | $number = ! empty( $instance['number'] ) ? absint( $instance['number'] ) : $this->settings['number']['std']; |
||
| 93 | $show = ! empty( $instance['show'] ) ? sanitize_title( $instance['show'] ) : $this->settings['show']['std']; |
||
| 94 | $orderby = ! empty( $instance['orderby'] ) ? sanitize_title( $instance['orderby'] ) : $this->settings['orderby']['std']; |
||
| 95 | $order = ! empty( $instance['order'] ) ? sanitize_title( $instance['order'] ) : $this->settings['order']['std']; |
||
| 96 | |||
| 97 | $query_args = array( |
||
| 98 | 'posts_per_page' => $number, |
||
| 99 | 'post_status' => 'publish', |
||
| 100 | 'post_type' => 'product', |
||
| 101 | 'no_found_rows' => 1, |
||
| 102 | 'order' => $order, |
||
| 103 | 'meta_query' => array() |
||
| 104 | ); |
||
| 105 | |||
| 106 | if ( empty( $instance['show_hidden'] ) ) { |
||
| 107 | $query_args['meta_query'][] = WC()->query->visibility_meta_query(); |
||
| 108 | $query_args['post_parent'] = 0; |
||
| 109 | } |
||
| 110 | |||
| 111 | View Code Duplication | if ( ! empty( $instance['hide_free'] ) ) { |
|
|
0 ignored issues
–
show
|
|||
| 112 | $query_args['meta_query'][] = array( |
||
| 113 | 'key' => '_price', |
||
| 114 | 'value' => 0, |
||
| 115 | 'compare' => '>', |
||
| 116 | 'type' => 'DECIMAL', |
||
| 117 | ); |
||
| 118 | } |
||
| 119 | |||
| 120 | $query_args['meta_query'][] = WC()->query->stock_status_meta_query(); |
||
| 121 | $query_args['meta_query'] = array_filter( $query_args['meta_query'] ); |
||
| 122 | |||
| 123 | switch ( $show ) { |
||
| 124 | case 'featured' : |
||
| 125 | $query_args['meta_query'][] = array( |
||
| 126 | 'key' => '_featured', |
||
| 127 | 'value' => 'yes' |
||
| 128 | ); |
||
| 129 | break; |
||
| 130 | case 'onsale' : |
||
| 131 | $product_ids_on_sale = wc_get_product_ids_on_sale(); |
||
| 132 | $product_ids_on_sale[] = 0; |
||
| 133 | $query_args['post__in'] = $product_ids_on_sale; |
||
| 134 | break; |
||
| 135 | } |
||
| 136 | |||
| 137 | switch ( $orderby ) { |
||
| 138 | case 'price' : |
||
| 139 | $query_args['meta_key'] = '_price'; |
||
| 140 | $query_args['orderby'] = 'meta_value_num'; |
||
| 141 | break; |
||
| 142 | case 'rand' : |
||
| 143 | $query_args['orderby'] = 'rand'; |
||
| 144 | break; |
||
| 145 | case 'sales' : |
||
| 146 | $query_args['meta_key'] = 'total_sales'; |
||
| 147 | $query_args['orderby'] = 'meta_value_num'; |
||
| 148 | break; |
||
| 149 | default : |
||
| 150 | $query_args['orderby'] = 'date'; |
||
| 151 | } |
||
| 152 | |||
| 153 | return new WP_Query( apply_filters( 'woocommerce_products_widget_query_args', $query_args ) ); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Output widget. |
||
| 158 | * |
||
| 159 | * @see WP_Widget |
||
| 160 | * |
||
| 161 | * @param array $args |
||
| 162 | * @param array $instance |
||
| 163 | */ |
||
| 164 | public function widget( $args, $instance ) { |
||
| 165 | if ( $this->get_cached_widget( $args ) ) { |
||
| 166 | return; |
||
| 167 | } |
||
| 168 | |||
| 169 | ob_start(); |
||
| 170 | |||
| 171 | if ( ( $products = $this->get_products( $args, $instance ) ) && $products->have_posts() ) { |
||
| 172 | $this->widget_start( $args, $instance ); |
||
| 173 | |||
| 174 | echo apply_filters( 'woocommerce_before_widget_product_list', '<ul class="product_list_widget">' ); |
||
| 175 | |||
| 176 | while ( $products->have_posts() ) { |
||
| 177 | $products->the_post(); |
||
| 178 | wc_get_template( 'content-widget-product.php', array( 'show_rating' => false ) ); |
||
| 179 | } |
||
| 180 | |||
| 181 | echo apply_filters( 'woocommerce_after_widget_product_list', '</ul>' ); |
||
| 182 | |||
| 183 | $this->widget_end( $args ); |
||
| 184 | } |
||
| 185 | |||
| 186 | wp_reset_postdata(); |
||
| 187 | |||
| 188 | echo $this->cache_widget( $args, ob_get_clean() ); |
||
| 189 | } |
||
| 190 | } |
||
| 191 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.