-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclass-demo-quotes-plugin-people-widget.php
308 lines (252 loc) · 9.83 KB
/
class-demo-quotes-plugin-people-widget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
/**
* People Widget.
*
* @package WordPress\Plugins\Demo_Quotes_Plugin
* @subpackage People_Widget
*/
// Avoid direct calls to this file.
if ( ! function_exists( 'add_action' ) ) {
header( 'Status: 403 Forbidden' );
header( 'HTTP/1.1 403 Forbidden' );
exit();
}
if ( class_exists( 'Demo_Quotes_Plugin' ) && ( class_exists( 'WP_Widget' ) && ! class_exists( 'Demo_Quotes_Plugin_People_Widget' ) ) ) {
/**
* Demo Quotes People Widget.
* Based on WP Native Categories widget.
*/
class Demo_Quotes_Plugin_People_Widget extends WP_Widget {
/**
* Widget name.
*
* @const string
*/
const DQPW_NAME = 'demo_quotes_people_widget';
/**
* Widget default settings.
*
* @var array
*/
public $dqpw_defaults = array(
'title' => null, // Will be set to localized string via dqpw_set_properties().
'count' => true,
'hierarchical' => true,
'dropdown' => false,
);
/**
* Register widget with WordPress.
*/
public function __construct() {
$widget_ops = array(
'classname' => 'widget_categories, ' . self::DQPW_NAME,
'description' => __( 'A list or drop-down of people of whom quotes are available.', 'demo-quotes-plugin' ),
);
parent::__construct(
self::DQPW_NAME, // Base ID.
__( 'Demo Quotes People Widget', 'demo-quotes-plugin' ), // Name.
$widget_ops // Option arguments.
);
add_action( 'wp_enqueue_scripts', array( $this, 'dqpw_wp_enqueue_scripts' ), 12 );
$this->dqpw_set_properties();
}
/**
* Fill some property arrays with translated strings.
*
* @return void
*/
private function dqpw_set_properties() {
$this->dqpw_defaults['title'] = __( 'Quotes by:', 'demo-quotes-plugin' );
}
/**
* Conditionally add front-end scripts and styles.
*
* @return void
*/
public function dqpw_wp_enqueue_scripts() {
if ( is_active_widget( false, false, $this->id_base, true ) ) {
wp_enqueue_style( Demo_Quotes_Plugin::$name . '-css' );
}
}
/**
* Generate the widget output.
*
* @param array $args Widget arguments.
* @param array $instance Current Widget instance.
*
* @return void
*/
public function widget( $args, $instance ) {
/* Merge incoming $instance with widget settings defaults. */
$instance = wp_parse_args( (array) $instance, $this->dqpw_defaults );
$title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
$tax_args = array(
'taxonomy' => Demo_Quotes_Plugin_Cpt::$taxonomy_name,
'orderby' => 'name',
'show_count' => $instance['count'],
'hierarchical' => $instance['hierarchical'],
'hide_empty' => true,
);
/* Generate output. */
echo '
<!-- BEGIN Demo Quotes Plugin People Widget -->
' . wp_kses_post( $args['before_widget'] );
if ( ! empty( $title ) && is_string( $title ) ) {
echo '
' . wp_kses_post( $args['before_title'] . $title . $args['after_title'] );
}
// People drop-down.
if ( true === $instance['dropdown'] ) {
$tax_args['show_option_none'] = __( 'Select Person', 'demo-quotes-plugin' );
$tax_args['id'] = self::DQPW_NAME . '-dropdown';
$this->dropdown_custom_taxonomy( apply_filters( 'demo_quotes_people_widget_dropdown_args', $tax_args ) );
?>
<script type='text/javascript'>
/* <![CDATA[ */
// People Widget drop-down.
var dqppwDropdown = document.getElementById('<?php echo esc_js( self::DQPW_NAME . '-dropdown' ); ?>');
function dqppwOnPersonChange() {
if ( dqppwDropdown.options[dqppwDropdown.selectedIndex].value != 0 && dqppwDropdown.options[dqppwDropdown.selectedIndex].value != -1 ) {
location.href = "<?php echo esc_js( home_url() ); ?>/?<?php echo esc_js( Demo_Quotes_Plugin_Cpt::$taxonomy_name ); ?>="+dqppwDropdown.options[dqppwDropdown.selectedIndex].value;
}
}
dqppwDropdown.onchange = dqppwOnPersonChange;
/* ]]> */
</script>
<?php
} else {
// People list.
echo '
<ul>';
$tax_args['title_li'] = '';
wp_list_categories( apply_filters( 'demo_quotes_people_widget_args', $tax_args ) );
echo '
</ul>';
}
echo '
', wp_kses_post( $args['after_widget'] ), '
<!-- END Demo Quotes Plugin People Widget -->';
}
/**
* Show drop-down for custom taxonomy with working slugs.
* Based on wp_dropdown_categories().
*
* @param array $args Settings to create the drop-down.
*
* @return string
*/
private function dropdown_custom_taxonomy( $args ) {
$defaults = array(
'show_option_all' => '',
'show_option_none' => '',
'orderby' => 'id',
'order' => 'ASC',
'show_count' => 0,
'hide_empty' => 1,
'child_of' => 0,
'exclude' => '',
'echo' => 1,
'selected' => 0,
'hierarchical' => 0,
'name' => 'cat',
'id' => '',
'class' => 'postform',
'depth' => 0,
'tab_index' => 0,
'taxonomy' => 'category',
'hide_if_empty' => false,
);
$defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0;
$args = wp_parse_args( $args, $defaults );
if ( ! isset( $args['pad_counts'] ) && $args['show_count'] && $args['hierarchical'] ) {
$args['pad_counts'] = true;
}
$tab_index_attribute = '';
if ( (int) $args['tab_index'] > 0 ) {
$tab_index_attribute = ' tabindex="' . intval( $args['tab_index'] ) . '"';
}
$terms = get_terms( $args['taxonomy'], $args );
$name = esc_attr( $args['name'] );
$class = esc_attr( $args['class'] );
$id = ( $args['id'] ) ? esc_attr( $args['id'] ) : $name;
$output = '';
if ( ! $args['hide_if_empty'] || ! empty( $terms ) ) {
$output = '<select name="' . $name . '" id="' . $id . '" class="' . $class . '" ' . $tab_index_attribute . ">\n";
}
if ( empty( $terms ) && ! $args['hide_if_empty'] && ! empty( $show_option_none ) ) {
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WP core hook.
$show_option_none = apply_filters( 'list_cats', $show_option_none );
$output .= "\t" . '<option value="-1" selected="selected">' . esc_html( $show_option_none ) . "</option>\n";
}
if ( ! empty( $terms ) ) {
if ( $args['show_option_all'] ) {
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WP core hook.
$show_option_all = apply_filters( 'list_cats', $args['show_option_all'] );
$selected = ( '0' === strval( $args['selected'] ) ) ? ' selected="selected"' : '';
$output .= "\t<option value=\"0\"$selected>" . esc_html( $show_option_all ) . "</option>\n";
}
if ( $args['show_option_none'] ) {
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WP core hook.
$show_option_none = apply_filters( 'list_cats', $args['show_option_none'] );
$selected = ( '-1' === strval( $args['selected'] ) ) ? ' selected="selected"' : '';
$output .= "\t<option value=\"-1\"$selected>" . esc_html( $show_option_none ) . "</option>\n";
}
// Disregard depth.
foreach ( $terms as $term ) {
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WP core hook.
$term_name = apply_filters( 'list_cats', $term->name, $term );
$output .= "\t" . '<option class="level-0" value="' . esc_attr( $term->slug ) . '"';
$output .= selected( $args['selected'], $term->term_id, false );
$output .= '>' . esc_html( $term_name );
if ( $args['show_count'] ) {
$output .= ' (' . esc_html( $term->count ) . ')';
}
$output .= "</option>\n";
}
}
if ( ! $args['hide_if_empty'] || ! empty( $terms ) ) {
$output .= "</select>\n";
}
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WP core hook.
$output = apply_filters( 'wp_dropdown_cats', $output );
if ( $args['echo'] ) {
echo $output; // WPCS: XSS ok.
}
return $output;
}
/**
* Update widget settings.
*
* @param array $new_instance Incoming widget settings.
* @param array $old_instance Previously saved widget settings.
*
* @return array
*/
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = wp_strip_all_tags( $new_instance['title'] );
$instance['count'] = ( ! empty( $new_instance['count'] ) ? true : false );
$instance['dropdown'] = ( ! empty( $new_instance['dropdown'] ) ? true : false );
return $instance;
}
/**
* Show widget settings form.
*
* @param array $instance The Widget settings.
*
* @return void
*/
public function form( $instance ) {
$instance = wp_parse_args( (array) $instance, $this->dqpw_defaults );
echo '
<p><label for="', esc_attr( $this->get_field_id( 'title' ) ), '">',
esc_html__( 'Title:', 'default' ), '</label>
<input class="widefat" id="', esc_attr( $this->get_field_id( 'title' ) ), '" name="', esc_attr( $this->get_field_name( 'title' ) ), '" type="text" value="', esc_attr( $instance['title'] ), '" /></p>
<p><input type="checkbox" class="checkbox" id="', esc_attr( $this->get_field_id( 'dropdown' ) ), '" name="', esc_attr( $this->get_field_name( 'dropdown' ) ), '"', checked( $instance['dropdown'], true, false ), ' />
<label for="', esc_attr( $this->get_field_id( 'dropdown' ) ), '">',
esc_html__( 'Display as dropdown', 'default' ), '</label><br />
<input type="checkbox" class="checkbox" id="', esc_attr( $this->get_field_id( 'count' ) ), '" name="', esc_attr( $this->get_field_name( 'count' ) ), '"', checked( $instance['count'], true, false ), ' />
<label for="', esc_attr( $this->get_field_id( 'count' ) ), '">', esc_html__( 'Show quote counts', 'demo-quotes-plugin' ), '</label><p>';
}
} /* End of class. */
} /* End of class exists wrapper. */