Completed
Push — master ( a85a3b...a9e055 )
by Justin
11:13
created

WP_Widget_Product_Specials::widget()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 15
rs 9.4285
cc 3
eloc 9
nc 2
nop 2
1
<?php
2
/**
3
 * Admin Menu widget class
4
 *
5
 * @since 3.8
6
 *
7
 * @todo Special count does not work when figuring out wether to show widget.
8
 * @todo Add option to set how many products show?
9
 */
10
class WP_Widget_Product_Specials extends WP_Widget {
11
12
	/**
13
	 * Widget Constuctor
14
	 */
15
	public function __construct() {
16
17
		$widget_ops = array(
18
			'classname'   => 'widget_wpsc_product_specials',
19
			'description' => __( 'Product Specials Widget', 'wp-e-commerce' )
20
		);
21
22
		parent::__construct( 'wpsc_product_specials', __( '(WPEC) Product Specials', 'wp-e-commerce' ), $widget_ops );
23
24
	}
25
26
	/**
27
	 * Widget Output
28
	 *
29
	 * @param $args (array)
30
	 * @param $instance (array) Widget values.
31
	 *
32
	 * @todo Add individual capability checks for each menu item rather than just manage_options.
33
	 */
34
	public function widget( $args, $instance ) {
35
36
		global $wpdb, $table_prefix;
37
38
		extract( $args );
0 ignored issues
show
introduced by
extract() usage is highly discouraged, due to the complexity and unintended issues it might cause.
Loading history...
39
40
		echo $before_widget;
41
		$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Product Specials', 'wp-e-commerce' ) : $instance['title'] );
42
		if ( $title )
43
			echo $before_title . $title . $after_title;
44
45
		wpsc_specials( $args, $instance );
46
		echo $after_widget;
47
48
	}
49
50
	/**
51
	 * Update Widget
52
	 *
53
	 * @param $new_instance (array) New widget values.
54
	 * @param $old_instance (array) Old widget values.
55
	 *
56
	 * @return (array) New values.
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string|integer|boolean>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
57
	 */
58
	public function update( $new_instance, $old_instance ) {
59
60
		$instance = $old_instance;
61
		$instance['title']            = strip_tags( $new_instance['title'] );
62
		$instance['number']           = (int) $new_instance['number'];
63
		$instance['show_thumbnails']  = (bool) $new_instance['show_thumbnails'];
64
		$instance['show_description'] = (bool) $new_instance['show_description'];
65
		$instance['show_old_price']   = (bool) $new_instance['show_old_price'];
66
		$instance['show_discount']    = (bool) $new_instance['show_discount'];
67
68
		return $instance;
69
70
	}
71
72
	/**
73
	 * Widget Options Form
74
	 *
75
	 * @param $instance (array) Widget values.
76
	 */
77
	public function form( $instance ) {
78
79
		global $wpdb;
80
81
		// Defaults
82
		$instance = wp_parse_args( (array) $instance, array(
83
			'title'            => '',
84
			'show_description' => false,
85
			'show_thumbnails'  => false,
86
			'number'           => 5,
87
			'show_old_price'   => false,
88
			'show_discount'    => false,
89
		) );
90
91
		// Values
92
		$title = esc_attr( $instance['title'] );
93
		$number = (int) $instance['number'];
94
		$show_thumbnails  = (bool) $instance['show_thumbnails'];
95
		$show_description = (bool) $instance['show_description'];
96
		$show_discount    = (bool) $instance['show_discount'];
97
		$show_old_price   = (bool) $instance['show_old_price'];
98
99
		?>
100
		<p>
101
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'wp-e-commerce' ); ?></label>
102
			<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
103
		</p>
104
		<p>
105
			<label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of products to show:', 'wp-e-commerce' ); ?></label>
106
			<input type="text" id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" value="<?php echo $number; ?>" size="3" />
107
		</p>
108
		<p>
109
			<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'show_description' ); ?>" name="<?php echo $this->get_field_name( 'show_description' ); ?>" <?php checked( $show_description ); ?>>
110
			<label for="<?php echo $this->get_field_id( 'show_description' ); ?>"><?php _e( 'Show Description', 'wp-e-commerce' ); ?></label><br />
111
			<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'show_thumbnails' ); ?>" name="<?php echo $this->get_field_name( 'show_thumbnails' ); ?>" <?php checked( $show_thumbnails ); ?>>
112
			<label for="<?php echo $this->get_field_id( 'show_thumbnails' ); ?>"><?php _e( 'Show Thumbnails', 'wp-e-commerce' ); ?></label><br />
113
			<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'show_old_price' ); ?>" name="<?php echo $this->get_field_name( 'show_old_price' ); ?>" <?php checked( $show_old_price, '1' ); ?>>
114
			<label for="<?php echo $this->get_field_id( 'show_old_price' ); ?>"><?php _e( 'Show Old Price', 'wp-e-commerce' ); ?></label><br />
115
			<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'show_discount' ); ?>" name="<?php echo $this->get_field_name( 'show_discount' ); ?>" <?php checked( $show_discount, '1' ); ?>>
116
			<label for="<?php echo $this->get_field_id( 'show_discount' ); ?>"><?php _e( 'Show Discount', 'wp-e-commerce' ); ?></label>
117
		</p>
118
<?php
119
	}
120
121
}
122
123
add_action( 'widgets_init', '_wpsc_action_register_specials_widget' );
124
function _wpsc_action_register_specials_widget() {
125
	register_widget( 'WP_Widget_Product_Specials' );
126
}
127
128
129
function _wpsc_filter_special_widget_where( $where ) {
130
	global $wpdb;
131
132
	// find variations that have sales price, then get a list of parent IDs
133
	$sql = "
134
		SELECT DISTINCT(p.post_parent)
135
		FROM {$wpdb->posts} AS p
136
		INNER JOIN {$wpdb->postmeta} AS pm
137
			ON p.ID = pm.post_id AND pm.meta_key = '_wpsc_special_price' AND pm.meta_value > 0
138
		WHERE p.post_parent != 0 AND p.post_status IN ('publish', 'inherit')
139
	";
140
141
	$parent_ids = $wpdb->get_col( $sql );
142
143
	if ( $parent_ids ) {
144
		$parent_ids = array_map( 'absint', $parent_ids );
145
		$where .= " AND ({$wpdb->posts}.ID IN (" . implode( ', ', $parent_ids ) . ") OR pm.meta_value > 0) ";
146
	} else {
147
		$where .= " AND pm.meta_value > 0 ";
148
	}
149
150
	return $where;
151
}
152
153
function _wpsc_filter_special_widget_join( $join ) {
154
	global $wpdb;
155
	$join .= " INNER JOIN {$wpdb->postmeta} AS pm ON {$wpdb->posts}.ID = pm.post_id AND pm.meta_key = '_wpsc_special_price' ";
156
	return $join;
157
}
158
159
/**
160
 * Product Specials Widget content function
161
 *
162
 * Displays the latest products.
163
 *
164
 * Changes made in 3.8 that may affect users:
165
 *
166
 * 1. The product title link text does now not have a bold tag, it should be styled via css.
167
 * 2. <br /> tags have been ommitted. Padding and margins should be applied via css.
168
 * 3. Each product is enclosed in a <div> with a 'wpec-special-product' class.
169
 * 4. The product list is enclosed in a <div> with a 'wpec-special-products' class.
170
 * 5. Function now expect a single paramter with an array of options (used to be a string which prepended the output).
171
 */
172
173
function wpsc_specials( $args = null, $instance ) {
174
175
	global $wpdb;
176
177
	$args = wp_parse_args( (array) $args, array( 'number' => 5 ) );
178
179
	if ( ! $number = (int) $instance['number'] )
180
		$number = 5;
181
182
	$show_thumbnails  = isset( $instance['show_thumbnails']  ) ? (bool) $instance['show_thumbnails']  : false;
183
	$show_description = isset( $instance['show_description'] ) ? (bool) $instance['show_description'] : false;
184
	$show_discount    = isset( $instance['show_discount']    ) ? (bool) $instance['show_discount']    : false;
185
	$show_old_price   = isset( $instance['show_old_price']   ) ? (bool) $instance['show_old_price']   : false;
186
187
	$args = array(
188
		'post_type'           => 'wpsc-product',
189
		'ignore_sticky_posts' => 1,
190
		'post_status'         => 'publish',
191
		'post_parent'         => 0,
192
		'posts_per_page'      => $number,
193
		'no_found_rows'       => true,
194
	);
195
196
	add_filter( 'posts_join', '_wpsc_filter_special_widget_join' );
197
	add_filter( 'posts_where', '_wpsc_filter_special_widget_where' );
198
	$special_products = new WP_Query( $args );
199
	remove_filter( 'posts_join', '_wpsc_filter_special_widget_join' );
200
	remove_filter( 'posts_where', '_wpsc_filter_special_widget_where' );
201
202
	if ( ! $special_products->post_count ) {
203
		echo apply_filters( 'wpsc_specials_widget_no_items_message', __( 'We currently have no items on special.', 'wp-e-commerce' ) );
204
		return;
205
	}
206
207
	$product_ids = array();
208
209
	while ( $special_products->have_posts() ) :
210
		$special_products->the_post();
211
		?>
212
		<h4><strong><a class="wpsc_product_title" href="<?php echo esc_url( wpsc_product_url( wpsc_the_product_id(), false ) ); ?>"><?php echo esc_html( wpsc_the_product_title() ); ?></a></h4></strong>
213
214
		<?php if ( $show_description ): ?>
215
			<div class="wpsc-special-description">
216
				<?php echo wpsc_the_product_description(); ?>
217
			</div>
218
		<?php endif; // close show description
219
220
		if ( ! in_array( wpsc_the_product_id(), $product_ids ) ) :
221
			$product_ids[] = wpsc_the_product_id();
222
			$has_children  = wpsc_product_has_children( get_the_ID() );
223
			$width         = get_option( 'product_image_width' );
224
			$height        = get_option( 'product_image_height' );
225
			if ( $show_thumbnails ) :
226
				if ( wpsc_the_product_thumbnail() ) : ?>
227
					<a rel="<?php echo str_replace(array(" ", '"',"'", '&quot;','&#039;'), array("_", "", "", "",''), wpsc_the_product_title()); ?>" href="<?php echo esc_url( wpsc_the_product_permalink() ); ?>"><img class="product_image" id="product_image_<?php echo esc_attr( wpsc_the_product_id() ); ?>" alt="<?php echo esc_attr( wpsc_the_product_title() ); ?>" title="<?php echo esc_attr( wpsc_the_product_title() ); ?>" src="<?php echo esc_url( wpsc_the_product_thumbnail( $width, $height ) ); ?>"/></a>
228
				<?php else : ?>
229
					<a href="<?php esc_url( wpsc_the_product_permalink() ); ?>"><img class="no-image" id="product_image_<?php echo esc_attr( wpsc_the_product_id() ); ?>" alt="<?php echo esc_attr( wpsc_the_product_title() ); ?>" title="<?php echo esc_attr( wpsc_the_product_title() ); ?>" src="<?php echo esc_url( WPSC_CORE_THEME_PATH . '/wpsc-images/noimage.png' ); ?>" width="<?php echo esc_attr( $width ); ?>" height="<?php echo esc_attr( $height ); ?>" /></a>
230
				<?php endif; ?>
231
				<br />
232
			<?php endif; // close show thumbnails ?>
233
			<div id="special_product_price_<?php echo esc_attr( wpsc_the_product_id() ); ?>">
234
				<?php
235
					wpsc_the_product_price_display(
236
						array(
237
							'output_old_price' => $show_old_price,
238
							'output_you_save'  => $show_discount,
239
						)
240
					);
241
				?>
242
			</div><br />
243
			<?php
244
		endif;
245
	endwhile;
246
	wp_reset_postdata();
247
}
248