PluginProbe
Knowledge Base documentation & wiki plugin – BasePress Docs / trunk
Knowledge Base documentation & wiki plugin – BasePress Docs vtrunk
basepress / includes / class-basepress-sections-widget.php

class-basepress-sections-widget.php in Knowledge Base documentation & wiki plugin – BasePress Docs trunk, at includes/class-basepress-sections-widget.php

353 lines 11.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // Exit if called directly
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 class Basepress_Sections_Widget extends WP_Widget {
8
9
10 /**
11 * Basepress_Sections_Widget constructor.
12 *
13 * @since 1.0.0
14 */
15 public function __construct() {
16 parent::__construct(
17 'basepress_sections_widget', // Base ID
18 esc_html__( 'Knowledge Base - Sections', 'basepress' ), // Name
19 array( 'description' => esc_html__( 'Lists all sections for the current Knowledge Base', 'basepress' ) ) // Args
20 );
21 }
22
23
24
25
26 /**
27 * Front-end display of widget.
28 *
29 * @since 1.0.0
30 * @updated 1.4.0
31 *
32 * @param array $args
33 * @param array $instance
34 */
35 public function widget( $args, $instance ) {
36 global $basepress_utils;
37
38 if( defined( 'REST_REQUEST' ) ){
39 return '';
40 }
41
42 $options = $basepress_utils->get_options();
43 $product = $basepress_utils->get_product();
44
45 if ( ! $product->id ) {
46 return;
47 }
48
49 //If the widget is called outside of BasePress pages or it is a search result page return
50 if ( ! $basepress_utils->is_knowledgebase || ( $basepress_utils->is_global_search && ! isset( $options['single_product_mode'] ) ) ) {
51 return;
52 }
53
54 echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
55
56 if ( ! empty( $instance['title'] ) ) {
57 echo $args['before_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
58 echo esc_html( apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance ) );
59 echo $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
60 }
61
62 $this->get_sections_list( $instance );
63
64 echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
65 }
66
67
68
69
70 /**
71 * Back-end widget form.
72 *
73 * @since 1.0.0
74 *
75 * @param array $instance
76 * @return string|void
77 */
78 public function form( $instance ) {
79 $title = ! empty( $instance['title'] ) ? sanitize_text_field( $instance['title'] ) : '';
80 $count = ! empty( $instance['count'] ) ? sanitize_text_field( $instance['count'] ) : '';
81 $limit_to_level = ! empty( $instance['limit-to-level'] ) ? sanitize_text_field( $instance['limit-to-level'] ) : '';
82 $max_level = ! empty( $instance['max-level'] ) ? sanitize_text_field( $instance['max-level'] ) : 1;
83 $order_by = ! empty( $instance['order-by'] ) ? sanitize_text_field( $instance['order-by'] ) : 'custom';
84 $post_count = ! empty( $instance['post-count'] ) ? sanitize_text_field( $instance['post-count'] ) : 0;
85 ?>
86 <p>
87 <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', 'basepress' ); ?></label>
88 <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
89 </p>
90
91 <p>
92 <label for="<?php echo esc_attr( $this->get_field_id( 'count' ) ); ?>"><?php esc_attr_e( 'Number of sections to show:', 'basepress' ); ?></label>
93 <input class="tiny-text" id="<?php echo esc_attr( $this->get_field_id( 'count' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'count' ) ); ?>" type="number" value="<?php echo esc_attr( $count ); ?>" size="3">
94 </p>
95
96 <p>
97 <label for="<?php echo esc_attr( $this->get_field_id( 'order-by' ) ); ?>"><?php esc_attr_e( 'Order by:', 'basepress' ); ?></label>
98 <select class="tiny-text" id="<?php echo esc_attr( $this->get_field_id( 'order-by' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'order-by' ) ); ?>" type="number" value="<?php echo esc_attr( $order_by ); ?>">
99 <option value="custom"<?php echo ( 'custom' == $order_by ? ' selected' : '' ); ?>><?php echo esc_attr_e( 'Custom', 'basepress' ); ?></option>
100 <option value="date-asc"<?php echo ( 'date-asc' == $order_by ? ' selected' : '' ); ?>><?php echo esc_attr_e( 'Date Ascending', 'basepress' ); ?></option>
101 <option value="date-desc"<?php echo ( 'date-desc' == $order_by ? ' selected' : '' ); ?>><?php echo esc_attr_e( 'Date Descending', 'basepress' ); ?></option>
102 </select>
103 </p>
104
105 <p>
106 <input class="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'post-count' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'post-count' ) ); ?>" type="checkbox" value="1" <?php checked( $post_count, 1 ); ?>>
107 <label for="<?php echo esc_attr( $this->get_field_id( 'post-count' ) ); ?>"><?php esc_attr_e( 'Display articles count', 'basepress' ); ?></label>
108 </p>
109
110 <p>
111 <input class="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'limit-to-level' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'limit-to-level' ) ); ?>" type="checkbox" value="1" <?php checked( $limit_to_level, 1 ); ?>">
112 <label for="<?php echo esc_attr( $this->get_field_id( 'max-level' ) ); ?>"><?php esc_attr_e( 'Show up to level:', 'basepress' ); ?></label>
113 <input class="tiny-text" id="<?php echo esc_attr( $this->get_field_id( 'max-level' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'max-level' ) ); ?>" type="number" value="<?php echo esc_attr( $max_level ); ?>" size="3" min="1">
114 </p>
115
116 <?php
117 }
118
119
120
121
122 /**
123 * Sanitizes widget form values as they are saved.
124 *
125 * @since 1.0.0
126 *
127 * @param array $new_instance
128 * @param array $old_instance
129 * @return array
130 */
131 public function update( $new_instance, $old_instance ) {
132 $instance = array();
133 $instance['title'] = ! empty( $new_instance['title'] ) ? sanitize_text_field( wp_strip_all_tags( $new_instance['title'] ) ) : '';
134 $instance['count'] = ! empty( $new_instance['count'] ) ? sanitize_text_field( $new_instance['count'] ) : '';
135 $instance['limit-to-level'] = ! empty( $new_instance['limit-to-level'] ) ? sanitize_text_field( $new_instance['limit-to-level'] ) : '';
136 $instance['max-level'] = ! empty( $new_instance['max-level'] ) ? sanitize_text_field( $new_instance['max-level'] ) : '';
137 $instance['order-by'] = ! empty( $new_instance['order-by'] ) ? sanitize_text_field( $new_instance['order-by'] ) : 'custom';
138 $instance['post-count'] = ! empty( $new_instance['post-count'] ) ? sanitize_text_field( $new_instance['post-count'] ) : 0;
139
140 return $instance;
141 }
142
143
144
145 /**
146 * Generates the sections list
147 *
148 * @since 1.0.0
149 *
150 * @param $instance
151 */
152 private function get_sections_list( $instance ) {
153
154 global $basepress_utils;
155 $product = $basepress_utils->get_product();
156 $queried_object = get_queried_object();
157 $terms_count = isset( $instance['count'] ) ? $instance['count'] : 0;
158 $limit_to_level = isset( $instance['limit-to-level'] ) && $instance['limit-to-level'] ? true : false;
159 $max_level = isset( $instance['max-level'] ) && (int)$instance['max-level'] ? $instance['max-level'] - 1 : 999;
160
161 if ( is_a( $queried_object, 'WP_Post' ) ) {
162 $terms = get_the_terms( $queried_object->ID, 'knowledgebase_cat' );
163 foreach ( $terms as $term ) {
164 if ( 0 == $term->parent ) {
165 continue;
166 }
167 $current_term_id = $term->term_id;
168 }
169 } elseif ( is_a( $queried_object, 'WP_Term' ) ) {
170 $current_term_id = $queried_object->term_id;
171 } else {
172 $current_term_id = '';
173 }
174
175 switch ( $instance['order-by'] ) {
176
177 case 'date-asc':
178 $meta_query = '';
179 $order_by = 'term_id';
180 $order = 'ASC';
181 break;
182
183 case 'date-desc':
184 $meta_query = '';
185 $order_by = 'term_id';
186 $order = 'DESC';
187 break;
188
189 case 'custom':
190 default:
191 $meta_query = array(
192 'relation' => 'OR',
193 array(
194 'key' => 'basepress_position',
195 ),
196 array(
197 'key' => 'basepress_position',
198 'compare' => 'NOT EXISTS'
199 )
200 );
201 $order_by = 'meta_value_num';
202 $order = 'ASC';
203 break;
204 }
205
206 $terms_args = array(
207 'taxonomy' => 'knowledgebase_cat',
208 'hide_empty' => true,
209 'child_of' => $product->id,
210 'meta_query' => $meta_query,
211 'orderby' => $order_by,
212 'order' => $order,
213 'number' => $terms_count,
214 );
215
216 $terms_args = apply_filters( 'basepress_section_widget_terms_args', $terms_args );
217
218 $sections = get_terms( $terms_args );
219
220 //Store the section levels
221 $levels = array();
222
223 foreach ( $sections as $key => $section ) {
224
225 if ( $section->parent == $product->id ) {
226 $levels[ $section->term_id ] = 0;
227 } else {
228 $level = $levels[ $section->parent ] + 1;
229 $levels[ $section->term_id ] = $level;
230 }
231 //Limit sub-sections to max level
232 if( $limit_to_level && $levels[ $section->term_id ] > $max_level ){
233 unset( $sections[$key] );
234 }
235 }
236
237 /**
238 * Filter to modify the list of sections
239 */
240 $sections = apply_filters( 'basepress_section_widget_items', $sections );
241
242 echo '<ul class="bpress-widget-list">';
243
244 foreach ( $sections as $section ) {
245
246 $level = $levels[ $section->term_id ];
247 $level_name = 0 == $level ? ' section' : ' subsection';
248
249 $permalink = get_term_link( $section->term_id, 'knowledgebase_cat' );
250 $show_icons = basepress_show_section_icon();
251 $classes = $current_term_id == $section->term_id ? ' active' : '';
252
253 if ( $show_icons ) {
254 $icon = get_term_meta( $section->term_id, 'icon', true );
255 $icon = $icon ? $icon : $basepress_utils->icons->sections->default;
256 $classes .= ' show-icon';
257 }
258
259 echo '<li class="bpress-widget-item' . esc_attr( $level_name ) . esc_attr( $classes ) . ' level-' . esc_attr( $level ) . '">';
260 if ( $show_icons ) {
261 echo '<span class="' . esc_attr( $icon ) . ' bpress-widget-icon"></span>';
262 }
263 echo '<a href="' . esc_url( $permalink ) . '">';
264
265 echo esc_html( $section->name );
266 if ( $instance['post-count'] && $section->count > 0 ) {
267 echo ' (' . esc_html( $section->count ) . ')';
268 }
269
270 echo '</a>';
271
272 echo '</li>';
273
274 }
275
276 echo '</ul>';
277
278 }
279
280 /**
281 * Walker function to create nested lists for the widget
282 * NOT USED YET
283 *
284 * @since 1.3.0
285 *
286 * @param $sections
287 * @param $product_id
288 * @param $current_term_id
289 * @param $post_count
290 */
291 private function walk( $sections, $product_id, $current_term_id, $post_count ) {
292 global $basepress_utils;
293 $show_icons = basepress_show_section_icon();
294
295 //Store the section levels
296 $levels = array();
297
298 foreach ( $sections as $section ) {
299 if ( $section->parent == $product_id ) {
300 $levels[ $section->term_id ] = 0;
301 } else {
302 $level = $levels[ $section->parent ] + 1;
303 $levels[ $section->term_id ] = $level;
304 }
305 }
306
307 $last_level = -1;
308
309 foreach ( $sections as $section ) {
310 $permalink = get_term_link( $section->term_id, 'knowledgebase_cat' );
311 $classes = $current_term_id == $section->term_id ? ' active' : '';
312
313 if ( $show_icons ) {
314 $icon = get_term_meta( $section->term_id, 'icon', true );
315 $icon = $icon ? $icon : $basepress_utils->icons->sections->default;
316 $classes .= ' show-icon';
317 }
318
319 $level = $levels[ $section->term_id ];
320 $section_level = $section->parent == $product_id ? ' section' : ' subsection';
321
322 //Add an item on the table of content
323 if ( $level > $last_level ) {
324 echo '<ul class="bpress-widget-list">';
325
326 } else {
327 echo str_repeat( '</li></ul>', $last_level - $level ); // phpcs:ignore
328 echo '</li>';
329 }
330 echo '<li class="bpress-widget-item' . esc_attr( $section_level ) . esc_attr( $classes ) . '">';
331
332 if ( $show_icons ) {
333 echo '<span class="' . esc_attr( $icon ) . ' bpress-widget-icon"></span>';
334 }
335
336 echo '<a href="' . esc_url( $permalink ) . '">';
337 echo esc_html( $section->name );
338 if ( $post_count && $section->count > 0 ) {
339 echo ' (' . esc_attr( $section->count ) . ')';
340 }
341 echo '</a>';
342
343 $last_level = $level;
344 }
345
346 if ( count( $sections ) != 0 ) {
347 echo '</li></ul>';
348 }
349
350 }
351 }
352 ?>
353