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

WP_Widget_Shopping_Cart::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 10
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
/**
4
 * Shopping Cart widget class
5
 *
6
 * @since 3.8
7
 *
8
 * @todo  Check if widget_wp_shopping_cart_init function is still required?
9
 */
10
class WP_Widget_Shopping_Cart extends WP_Widget {
11
12
	/**
13
	 * Widget Constuctor
14
	 */
15
	public function __construct() {
16
17
		$widget_ops = array(
18
			'classname'   => 'widget_wpsc_shopping_cart',
19
			'description' => __( 'Shopping Cart Widget', 'wp-e-commerce' )
20
		);
21
22
		parent::__construct( 'wpsc_shopping_cart', __( '(WPEC) Shopping Cart', '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
	 */
33
	public function widget( $args, $instance ) {
34
35
		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...
36
37
		// Create fancy collapser
38
		$fancy_collapser = '';
39
		if ( $instance['show_sliding_cart'] == 1 ) {
40
			if ( isset($_SESSION['slider_state']) && is_numeric( $_SESSION['slider_state'] ) ) {
0 ignored issues
show
introduced by
Usage of $_SESSION variable is prohibited.
Loading history...
41
				if ( $_SESSION['slider_state'] == 0 ) {
0 ignored issues
show
introduced by
Usage of $_SESSION variable is prohibited.
Loading history...
42
					$collapser_image = 'plus.png';
43
				} else {
44
					$collapser_image = 'minus.png';
45
				}
46
				$fancy_collapser = ' <a href="#" onclick="return shopping_cart_collapser()" id="fancy_collapser_link"><img src="' . WPSC_CORE_IMAGES_URL . '/' . $collapser_image . '" title="" alt="" id="fancy_collapser" /></a>';
47
			} else {
48
				if ( ! wpsc_get_customer_meta( 'nzshpcart' ) ) {
49
					$collapser_image = 'plus.png';
50
				} else {
51
					$collapser_image = 'minus.png';
52
				}
53
				$fancy_collapser = ' <a href="#" onclick="return shopping_cart_collapser()" id="fancy_collapser_link"><img src="' . WPSC_CORE_IMAGES_URL . '/' . $collapser_image . '" title="" alt="" id="fancy_collapser" /></a>';
54
			}
55
		}
56
57
		// Start widget output
58
		$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Shopping Cart', 'wp-e-commerce' ) : $instance['title'] );
59
		echo $before_widget;
60
61
		if ( $title )
62
			echo $before_title . $title . $fancy_collapser . $after_title;
63
64
		// Set display state
65
		$display_state = '';
66
		if ( ( ( isset( $_SESSION['slider_state'] ) && ( $_SESSION['slider_state'] == 0 ) ) || ( wpsc_cart_item_count() < 1 ) ) && ( get_option( 'show_sliding_cart' ) == 1 ) )
0 ignored issues
show
introduced by
Usage of $_SESSION variable is prohibited.
Loading history...
67
			$display_state = 'style="display: none;"';
68
69
		// Output start, if we are not allowed to save results ( WPSC_DONT_CACHE ) load the cart using ajax
70
		$use_object_frame = false;
71
		if ( WPSC_PAGE_CACHE_IN_USE  ) {
72
			echo '<div id="sliding_cart" class="shopping-cart-wrapper">';
73
			if ( ( strstr( $_SERVER['HTTP_USER_AGENT'], 'MSIE' ) == false ) && ( $use_object_frame == true ) ) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE') of type string to the boolean false. If you are specifically checking for an empty string, consider using the more explicit === '' instead.
Loading history...
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
introduced by
Due to using Batcache, server side based client related logic will not work, use JS instead.
Loading history...
74
				?>
75
				<object codetype="text/html" type="text/html" data="index.php?wpsc_action=cart_html_page" border="0">
76
					<p><?php _e( 'Loading...', 'wp-e-commerce' ); ?></p>
77
				</object>
78
				<?php
79
			} else {
80
				?>
81
				<div class="wpsc_cart_loading"><p><?php _e( 'Loading...', 'wp-e-commerce' ); ?></p></div>
82
				<?php
83
			}
84
			echo '</div>';
85
		} else {
86
			echo '<div id="sliding_cart" class="shopping-cart-wrapper" ' . $display_state . '>';
87
			include( wpsc_get_template_file_path( 'wpsc-cart_widget.php' ) );
88
			echo '</div>';
89
		}
90
91
		// End widget output
92
		echo $after_widget;
93
94
	}
95
96
	/**
97
	 * Update Widget
98
	 *
99
	 * @param $new_instance (array) New widget values.
100
	 * @param $old_instance (array) Old widget values.
101
	 *
102
	 * @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>.

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...
103
	 */
104
	public function update( $new_instance, $old_instance ) {
105
106
		$instance = $old_instance;
107
		$instance['title']  = strip_tags( $new_instance['title'] );
108
		$instance['show_sliding_cart']  = strip_tags( $new_instance['show_sliding_cart'] );
109
		return $instance;
110
	}
111
112
	/**
113
	 * Widget Options Form
114
	 *
115
	 * @param $instance (array) Widget values.
116
	 */
117
	public function form( $instance ) {
118
		global $wpdb;
119
120
		// Defaults
121
		$instance = wp_parse_args( (array)$instance, array(
122
			'title' => __( 'Shopping Cart', 'wp-e-commerce' ),
123
			'show_sliding_cart' => 0
124
		) );
125
126
		// Values
127
		$title = esc_attr( $instance['title'] );
128
		$show_sliding_cart = esc_attr( $instance['show_sliding_cart'] );
129
		if( 1 == $show_sliding_cart)
130
			$show_sliding_cart = 'checked="checked"';
131
		else
132
			$show_sliding_cart = '';
133
		?>
134
		<p>
135
			<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:', 'wp-e-commerce' ); ?></label>
136
			<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" />
137
		</p>
138
		<input type='hidden' name="<?php echo $this->get_field_name( 'show_sliding_cart' ); ?>" value='0' />
139
		<p>
140
141
			<label for="<?php echo $this->get_field_id('show_sliding_cart'); ?>"><?php _e( 'Use Sliding Cart:', 'wp-e-commerce' ); ?></label>
142
			<input id="<?php echo $this->get_field_id( 'show_sliding_cart' ); ?>" name="<?php echo $this->get_field_name( 'show_sliding_cart' ); ?>" type="checkbox" value="1" <?php echo $show_sliding_cart; ?> />
143
		</p>
144
145
		<?php
146
	}
147
}
148
add_action( 'widgets_init', create_function( '', 'return register_widget("WP_Widget_Shopping_Cart");' ) );
0 ignored issues
show
introduced by
create_function is discouraged, please use Anonymous functions instead.
Loading history...
149
150
?>
151