1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* Bootstraps the Vendor system |
4
|
|
|
* |
5
|
|
|
* @package PrintCenter\Vendor\Widget |
6
|
|
|
* @since 1.0.0 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
// Exit if accessed directly |
11
|
|
|
if( ! defined( 'ABSPATH' ) ) { |
12
|
|
|
exit; |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Vendor info widget |
18
|
|
|
* |
19
|
|
|
* @since 1.0.0 |
20
|
|
|
*/ |
21
|
|
|
class WooCommerce_Product_Vendors_Widget extends WP_Widget { |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @access private |
26
|
|
|
* @var string $widget_cssclass CSS classes for the widget instance |
27
|
|
|
*/ |
28
|
|
|
private $widget_cssclass; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @access private |
33
|
|
|
* @var string $widget_description Description for the widget instance |
34
|
|
|
*/ |
35
|
|
|
private $widget_description; |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @access private |
40
|
|
|
* @var string $widget_idbase ID base for the widget instance |
41
|
|
|
*/ |
42
|
|
|
private $widget_idbase; |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @access private |
47
|
|
|
* @var string $widget_title Title for the widget instance |
48
|
|
|
*/ |
49
|
|
|
private $widget_title; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get things started |
53
|
|
|
* |
54
|
|
|
* @access public |
55
|
|
|
* @since 1.0.0 |
56
|
|
|
* @return void |
|
|
|
|
57
|
|
|
*/ |
58
|
|
|
public function __construct() { |
59
|
|
|
// Widget variable settings |
60
|
|
|
$this->widget_cssclass = 'widget_product_vendors'; |
61
|
|
|
$this->widget_description = __( 'Display selected or current product vendor info.', 'printcenter' ); |
62
|
|
|
$this->widget_idbase = 'product_vendors'; |
63
|
|
|
$this->widget_title = __( 'WooCommerce Product Vendors', 'printcenter' ); |
64
|
|
|
|
65
|
|
|
// Widget settings |
66
|
|
|
$widget_ops = array( 'classname' => $this->widget_cssclass, 'description' => $this->widget_description ); |
67
|
|
|
|
68
|
|
|
// Widget control settings |
69
|
|
|
$control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => $this->widget_idbase ); |
70
|
|
|
|
71
|
|
|
// Create the widget |
72
|
|
|
parent::__construct( $this->widget_idbase, $this->widget_title, $widget_ops, $control_ops ); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Display the widget on the frontend |
78
|
|
|
* |
79
|
|
|
* @access public |
80
|
|
|
* @since 1.0.0 |
81
|
|
|
* @param array $args Widget arguments |
82
|
|
|
* @param array $instance Widget settings for this instance |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
|
|
public function widget( $args, $instance ) { |
86
|
|
|
extract( $args, EXTR_SKIP ); |
87
|
|
|
|
88
|
|
|
$vendor_id = false; |
89
|
|
|
$vendors = false; |
90
|
|
|
|
91
|
|
|
// Only show current vendor widget when showing a vendor's product(s) |
92
|
|
|
$show_widget = true; |
93
|
|
|
|
94
|
|
|
if( $instance['vendor'] == 'current' ) { |
95
|
|
|
if( is_singular( 'product' ) ) { |
96
|
|
|
global $post; |
|
|
|
|
97
|
|
|
$vendors = printcenter_get_product_vendors( $post->ID ); |
98
|
|
|
if( ! $vendors ) { |
99
|
|
|
$show_widget = false; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if( is_archive() && ! is_tax( 'shop_vendor' ) ) { |
104
|
|
|
$show_widget = false; |
105
|
|
|
} |
106
|
|
|
} else { |
107
|
|
|
$vendors = array( |
108
|
|
|
printcenter_get_vendor( $instance['vendor'] ) |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if( $show_widget ) { |
113
|
|
|
if( is_tax( 'shop_vendor' ) ) { |
114
|
|
|
$vendor_id = get_queried_object()->term_id; |
115
|
|
|
if( $vendor_id ) { |
116
|
|
|
$vendors = array( |
117
|
|
|
printcenter_get_vendor( $vendor_id ) |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if( $vendors ) { |
123
|
|
|
// Set up widget title |
124
|
|
|
if( $instance['title'] ) { |
125
|
|
|
$title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ); |
126
|
|
|
} else { |
127
|
|
|
$title = false; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// Before widget (defined by themes) |
131
|
|
|
echo $before_widget; |
132
|
|
|
|
133
|
|
|
// Display the widget title if one was input (before and after defined by themes). |
134
|
|
|
if ( $title ) { echo $before_title . $title . $after_title; } |
135
|
|
|
|
136
|
|
|
// Widget content |
137
|
|
|
$html = ''; |
138
|
|
|
|
139
|
|
|
foreach( $vendors as $vendor ) { |
|
|
|
|
140
|
|
|
$html .= '<h4>' . $vendor->title . '</h4>'; |
141
|
|
|
$html .= '<p>' . $vendor->description . '</p>'; |
142
|
|
|
$html .= '<p><a href="' . esc_attr( $vendor->url ) . '" title"' . sprintf( __( 'More products from %1$s', 'printcenter' ), $vendor->title ) . '">' . sprintf( __( 'More products from %1$s', 'printcenter' ), $vendor->title ) . '</a></p>'; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// Action for plugins/themes to hook onto |
146
|
|
|
do_action( $this->widget_cssclass . '_top' ); |
147
|
|
|
|
148
|
|
|
echo $html; |
149
|
|
|
|
150
|
|
|
// Action for plugins/themes to hook onto |
151
|
|
|
do_action( $this->widget_cssclass . '_bottom' ); |
152
|
|
|
|
153
|
|
|
// After widget (defined by themes). |
154
|
|
|
echo $after_widget; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Method to update the settings from the form() method |
162
|
|
|
* |
163
|
|
|
* @access public |
164
|
|
|
* @since 1.0.0 |
165
|
|
|
* @param array $new_instance New settings |
166
|
|
|
* @param array $old_instance Previous settings |
167
|
|
|
* @return array Updated settings |
168
|
|
|
*/ |
169
|
|
|
public function update ( $new_instance, $old_instance ) { |
170
|
|
|
$instance = $old_instance; |
171
|
|
|
|
172
|
|
|
// Sanitise inputs |
173
|
|
|
$instance['title'] = strip_tags( $new_instance['title'] ); |
174
|
|
|
$instance['vendor'] = esc_attr( $new_instance['vendor'] ); |
175
|
|
|
|
176
|
|
|
return $instance; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* The form on the widget control in the widget administration area |
182
|
|
|
* |
183
|
|
|
* @access public |
184
|
|
|
* @since 1.0.0 |
185
|
|
|
* @param array $instance The settings for this instance. |
186
|
|
|
* @return void |
187
|
|
|
*/ |
188
|
|
|
public function form( $instance ) { |
189
|
|
|
|
190
|
|
|
// Set up the default widget settings |
191
|
|
|
$defaults = array( |
192
|
|
|
'title' => '', |
193
|
|
|
'vendor' => 'current' |
194
|
|
|
); |
195
|
|
|
|
196
|
|
|
$instance = wp_parse_args( (array) $instance, $defaults ); |
197
|
|
|
|
198
|
|
|
// Set up vendor options |
199
|
|
|
$vendors = printcenter_get_vendors(); |
200
|
|
|
$vendor_options = '<option value="current" ' . selected( $instance['vendor'], 'current', false ) . '>' . __( 'Current vendor(s)', 'printcenter' ) . '</option>'; |
201
|
|
|
|
202
|
|
|
foreach( $vendors as $vendor ) { |
|
|
|
|
203
|
|
|
$vendor_options .= '<option value="' . esc_attr( $vendor->ID ) . '" ' . selected( $instance['vendor'], $vendor->ID, false ) . '>' . esc_html( $vendor->title ) . '</option>'; |
204
|
|
|
} |
205
|
|
|
?> |
206
|
|
|
<p> |
207
|
|
|
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title (optional):', 'printcenter' ); ?></label> |
208
|
|
|
<input type="text" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" /> |
209
|
|
|
</p> |
210
|
|
|
<p> |
211
|
|
|
<label for="<?php echo $this->get_field_id( 'vendor' ); ?>"><?php _e( 'Vendor:', 'printcenter' ); ?></label> |
212
|
|
|
<select name="<?php echo $this->get_field_name( 'vendor' ); ?>" class="widefat" id="<?php echo $this->get_field_id( 'vendor' ); ?>"> |
213
|
|
|
<?php echo $vendor_options; ?> |
214
|
|
|
</select><br/><br/> |
215
|
|
|
<span class="description"><?php _e( '\'Current vendor(s)\' will display the details of the vendors whose product(s) are being viewed at the time. It will not show on other pages.', 'printcenter' ); ?></span> |
216
|
|
|
</p> |
217
|
|
|
<?php |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
add_action( 'widgets_init', create_function( '', 'return register_widget("WooCommerce_Product_Vendors_Widget");' ), 1 ); |
|
|
|
|
221
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.