PluginProbe
Flex Posts – Responsive Posts Block / 1.12.0
Flex Posts – Responsive Posts Block v1.12.0
2.1.0 trunk 1.12.0 2.0.0
flex-posts / includes / class-flex-posts-widget.php

class-flex-posts-widget.php in Flex Posts – Responsive Posts Block 1.12.0, at includes/class-flex-posts-widget.php

578 lines 14.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Flex Posts Widget
4 *
5 * @package Flex Posts
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 /**
13 * Flex Posts widget class
14 */
15 class Flex_Posts_Widget extends WP_Widget {
16
17 /**
18 * Enqueue scripts & styles
19 */
20 public function enqueue() {
21 if ( is_active_widget( false, false, $this->id_base ) ) {
22 add_action( 'wp_enqueue_scripts', array( $this, 'wp_enqueue' ), 9 );
23 }
24
25 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue' ) );
26 }
27
28 /**
29 * Register the stylesheets for widget.
30 */
31 public function wp_enqueue() {
32 $option = get_option( 'flex_posts' );
33 if ( empty( $option['disable_css'] ) ) {
34 wp_enqueue_style( 'flex-posts' );
35 }
36 }
37
38 /**
39 * Register the stylesheets & javascripts for widget admin.
40 */
41 public function admin_enqueue() {
42 wp_enqueue_style(
43 'flex-posts-admin',
44 FLEX_POSTS_URL . 'admin/css/widget-admin.css',
45 array(),
46 FLEX_POSTS_VERSION
47 );
48 wp_enqueue_script(
49 'flex-posts-admin',
50 FLEX_POSTS_URL . 'admin/js/widget-admin.js',
51 array( 'jquery' ),
52 FLEX_POSTS_VERSION,
53 true
54 );
55 wp_localize_script(
56 'flex-posts-admin',
57 'flex_posts_admin',
58 array(
59 'taxonomies' => flex_posts_get_taxonomies(),
60 )
61 );
62 }
63
64 /**
65 * Back-end widget form.
66 *
67 * @see WP_Widget::form()
68 *
69 * @param array $instance Previously saved values from database.
70 */
71 public function form( $instance ) {
72 $fields = $this->get_fields();
73 if ( empty( $fields ) ) {
74 return;
75 }
76
77 $sections = array();
78 foreach ( $fields as $key => $field ) {
79 if ( 'section' === $field['type'] ) {
80 $sections[ $key ] = $field['label'];
81 }
82 }
83 ?>
84
85 <?php if ( ! empty( $sections ) ) : ?>
86 <ul class="fp-tabs">
87 <?php foreach ( $sections as $name => $label ) : ?>
88 <li>
89 <a class="fp-tab-item" data-target="fp-tab-<?php echo esc_attr( $name ); ?>">
90 <?php echo esc_html( $label ); ?>
91 </a>
92 </li>
93 <?php endforeach; ?>
94 </ul>
95 <?php endif; ?>
96
97 <?php foreach ( $fields as $key => $field ) : ?>
98
99 <?php if ( 'section' === $field['type'] ) : ?>
100
101 <section class="fp-tab fp-tab-<?php echo esc_attr( $key ); ?> active">
102
103 <?php elseif ( 'section-end' === $field['type'] ) : ?>
104
105 </section>
106
107 <?php else : ?>
108
109 <?php
110 $name = $this->get_field_name( $key );
111 $id = $this->get_field_id( $key );
112 $default = isset( $field['default'] ) ? $field['default'] : '';
113 $value = isset( $instance[ $key ] ) ? $instance[ $key ] : $default;
114 $min = isset( $field['min'] ) ? $field['min'] : 0;
115 $max = isset( $field['max'] ) ? $field['max'] : null;
116 ?>
117 <p>
118 <?php if ( ! in_array( $field['type'], array( 'checkbox' ), true ) ) : ?>
119 <label for="<?php echo esc_attr( $id ); ?>">
120 <?php echo esc_html( $field['label'] ); ?>:
121 </label>
122 <?php endif; ?>
123
124 <?php if ( 'text' === $field['type'] ) : ?>
125
126 <?php
127 flex_posts_input(
128 array(
129 'type' => 'text',
130 'name' => $name,
131 'id' => $id,
132 'class' => isset( $field['class'] ) ? $field['class'] : 'widefat',
133 'value' => $value,
134 )
135 );
136 ?>
137
138 <?php elseif ( 'number' === $field['type'] ) : ?>
139
140 <?php
141 flex_posts_input(
142 array(
143 'type' => 'number',
144 'name' => $name,
145 'id' => $id,
146 'class' => isset( $field['class'] ) ? $field['class'] : 'small-text',
147 'value' => $value,
148 'min' => $min,
149 'max' => $max,
150 )
151 );
152 ?>
153
154 <?php elseif ( 'textarea' === $field['type'] ) : ?>
155
156 <?php
157 flex_posts_textarea(
158 array(
159 'name' => $name,
160 'id' => $id,
161 'class' => isset( $field['class'] ) ? $field['class'] : 'widefat',
162 'value' => $value,
163 )
164 );
165 ?>
166
167 <?php elseif ( 'select' === $field['type'] ) : ?>
168
169 <?php
170 flex_posts_select(
171 array(
172 'name' => $name,
173 'id' => $id,
174 'class' => isset( $field['class'] ) ? $field['class'] : 'widefat',
175 'options' => $field['options'],
176 'selected' => $value,
177 'first' => isset( $field['first'] ) ? $field['first'] : null,
178 'multiple' => isset( $field['multiple'] ) ? $field['multiple'] : false,
179 'array_name' => isset( $field['array_name'] ) ? $field['array_name'] : false,
180 'size' => isset( $field['size'] ) ? $field['size'] : false,
181 )
182 );
183 ?>
184
185 <?php elseif ( 'checkbox' === $field['type'] ) : ?>
186
187 <?php
188 flex_posts_input(
189 array(
190 'type' => 'checkbox',
191 'name' => $name,
192 'id' => $id,
193 'class' => isset( $field['class'] ) ? $field['class'] : 'checkbox',
194 'value' => 1,
195 'checked' => $value,
196 )
197 );
198 ?>
199 <label for="<?php echo esc_attr( $id ); ?>">
200 <?php echo esc_html( $field['label'] ); ?>
201 </label>
202
203 <?php elseif ( 'category' === $field['type'] ) : ?>
204
205 <?php
206 wp_dropdown_categories(
207 array(
208 'hide_empty' => 0,
209 'name' => $name,
210 'id' => $id,
211 'class' => isset( $field['class'] ) ? $field['class'] : 'widefat',
212 'hierarchical' => 1,
213 'show_option_all' => esc_html__( 'All Categories', 'flex_posts' ),
214 'selected' => $value,
215 )
216 );
217 ?>
218
219 <?php endif; ?>
220
221 <?php if ( ! empty( $field['desc'] ) ) : ?>
222 <br><small><?php echo esc_html( $field['desc'] ); ?></small>
223 <?php endif; ?>
224 </p>
225
226 <?php endif; ?>
227
228 <?php endforeach; ?>
229
230 <?php
231 }
232
233 /**
234 * Get layouts options
235 *
236 * @param int|null $layouts Number of layouts.
237 * @return array
238 */
239 protected function get_layouts_options( $layouts = null ) {
240 if ( null === $layouts ) {
241 $layouts = flex_posts_get_layouts();
242 }
243 $options = array();
244 for ( $i = 1; $i <= $layouts; $i++ ) {
245 $options[ $i ] = esc_html__( 'Layout', 'flex-posts' ) . ' ' . $i;
246 }
247 return $options;
248 }
249
250 /**
251 * Get form fields
252 *
253 * @return array Form fields
254 */
255 public function get_fields() {
256 /* ================ GENERAL ================= */
257
258 $fields['general'] = array(
259 'type' => 'section',
260 'label' => esc_html__( 'General', 'flex-posts' ),
261 );
262
263 $fields['title'] = array(
264 'type' => 'text',
265 'label' => esc_html__( 'Title', 'flex-posts' ),
266 );
267
268 $fields['title_url'] = array(
269 'type' => 'text',
270 'label' => esc_html__( 'Title URL', 'flex-posts' ),
271 );
272
273 $fields['layout'] = array(
274 'type' => 'select',
275 'label' => esc_html__( 'Layout', 'flex-posts' ),
276 'options' => $this->get_layouts_options(),
277 'class' => 'fp-layout widefat',
278 'default' => 1,
279 );
280
281 $fields['general_end'] = array(
282 'type' => 'section-end',
283 );
284
285 /* ================ QUERY ================= */
286
287 $fields['query'] = array(
288 'type' => 'section',
289 'label' => esc_html__( 'Query', 'flex-posts' ),
290 );
291
292 $fields['post_type'] = array(
293 'type' => 'select',
294 'label' => esc_html__( 'Post Type', 'flex-posts' ),
295 'options' => flex_posts_get_post_types( false ),
296 'default' => 'post',
297 'class' => 'fp-query-post-type widefat',
298 );
299
300 $fields['cat'] = array(
301 'type' => 'category',
302 'label' => esc_html__( 'Category', 'flex-posts' ),
303 'class' => 'fp-query-category widefat',
304 );
305
306 $fields['tag'] = array(
307 'type' => 'text',
308 'label' => esc_html__( 'Tag(s)', 'flex-posts' ),
309 'class' => 'fp-query-tag widefat',
310 );
311
312 $fields['order_by'] = array(
313 'type' => 'select',
314 'label' => esc_html__( 'Order by', 'flex-posts' ),
315 'options' => flex_posts_get_order_by( false ),
316 'default' => 'newest',
317 );
318
319 $fields['number'] = array(
320 'type' => 'number',
321 'label' => esc_html__( 'Number of posts to show', 'flex-posts' ),
322 'default' => 4,
323 'min' => 1,
324 );
325
326 $fields['skip'] = array(
327 'type' => 'number',
328 'label' => esc_html__( 'Number of posts to skip', 'flex-posts' ),
329 'default' => 0,
330 'min' => 0,
331 );
332
333 $fields['exclude_current'] = array(
334 'type' => 'checkbox',
335 'label' => esc_html__( 'Exclude current post', 'flex-posts' ),
336 'default' => 0,
337 );
338
339 $fields['query_end'] = array(
340 'type' => 'section-end',
341 );
342
343 /* ================ DISPLAY ================= */
344
345 $fields['display'] = array(
346 'type' => 'section',
347 'label' => esc_html__( 'Display', 'flex-posts' ),
348 );
349
350 $fields['show_image'] = array(
351 'type' => 'select',
352 'label' => esc_html__( 'Show image on', 'flex-posts' ),
353 'options' => array(
354 'all' => esc_html__( 'All posts', 'flex-posts' ),
355 'first' => esc_html__( 'First post only', 'flex-posts' ),
356 'none' => esc_html__( 'None', 'flex-posts' ),
357 ),
358 'class' => 'fp-show-image widefat',
359 'default' => 'all',
360 );
361
362 $fields['image_size'] = array(
363 'type' => 'select',
364 'label' => esc_html__( 'Thumbnail image size', 'flex-posts' ),
365 'options' => flex_posts_get_image_sizes( false ),
366 'class' => 'fp-image-size widefat',
367 'default' => '',
368 );
369
370 $fields['image_size2'] = array(
371 'type' => 'select',
372 'label' => esc_html__( 'Medium image size', 'flex-posts' ),
373 'options' => flex_posts_get_image_sizes( false ),
374 'class' => 'fp-image-size2 widefat',
375 'default' => '',
376 );
377
378 $fields['show_title'] = array(
379 'type' => 'checkbox',
380 'label' => esc_html__( 'Show post title', 'flex-posts' ),
381 'default' => 1,
382 );
383
384 $fields['show_categories'] = array(
385 'type' => 'checkbox',
386 'label' => esc_html__( 'Show categories', 'flex-posts' ),
387 'default' => 0,
388 );
389
390 $fields['show_author'] = array(
391 'type' => 'checkbox',
392 'label' => esc_html__( 'Show author', 'flex-posts' ),
393 'default' => 0,
394 );
395
396 $fields['show_avatar'] = array(
397 'type' => 'checkbox',
398 'label' => esc_html__( 'Show author image', 'flex-posts' ),
399 'default' => 0,
400 );
401
402 $fields['show_date'] = array(
403 'type' => 'checkbox',
404 'label' => esc_html__( 'Show date', 'flex-posts' ),
405 'default' => 1,
406 );
407
408 $fields['show_comments'] = array(
409 'type' => 'checkbox',
410 'label' => esc_html__( 'Show comments number', 'flex-posts' ),
411 'default' => 1,
412 );
413
414 $fields['show_excerpt'] = array(
415 'type' => 'checkbox',
416 'label' => esc_html__( 'Show excerpt', 'flex-posts' ),
417 'class' => 'fp-show-excerpt checkbox',
418 'default' => 0,
419 );
420
421 $fields['excerpt_length'] = array(
422 'type' => 'number',
423 'label' => esc_html__( 'Excerpt length', 'flex-posts' ),
424 'class' => 'fp-excerpt-length small-text',
425 'default' => 15,
426 );
427
428 $fields['show_readmore'] = array(
429 'type' => 'checkbox',
430 'label' => esc_html__( 'Show read more link', 'flex-posts' ),
431 'class' => 'fp-show-readmore checkbox',
432 'default' => 0,
433 );
434
435 $fields['readmore_text'] = array(
436 'type' => 'text',
437 'label' => esc_html__( 'Read more text', 'flex-posts' ),
438 'class' => 'fp-readmore-text widefat',
439 'default' => '',
440 );
441
442 $fields['pagination'] = array(
443 'type' => 'checkbox',
444 'label' => esc_html__( 'Show pagination', 'flex-posts' ),
445 'default' => 0,
446 );
447
448 $fields['display_end'] = array(
449 'type' => 'section-end',
450 );
451
452 /* ================ ADVANCED ================= */
453
454 $fields['advanced'] = array(
455 'type' => 'section',
456 'label' => esc_html__( 'Advanced', 'flex-posts' ),
457 );
458
459 $fields['block_title_el'] = array(
460 'type' => 'select',
461 'label' => esc_html__( 'Block Title HTML element', 'flex-posts' ),
462 'options' => flex_posts_get_title_elements_options( false ),
463 'default' => '',
464 );
465
466 $fields['post_title_el'] = array(
467 'type' => 'select',
468 'label' => esc_html__( 'Post Title HTML element', 'flex-posts' ),
469 'options' => flex_posts_get_title_elements_options( false ),
470 'default' => '',
471 );
472
473 $fields['className'] = array(
474 'type' => 'text',
475 'label' => esc_html__( 'Additional CSS class(es)', 'flex-posts' ),
476 'default' => '',
477 );
478
479 $fields['advanced_end'] = array(
480 'type' => 'section-end',
481 );
482
483 return $fields;
484 }
485
486 /**
487 * Sanitize widget form values as they are saved.
488 *
489 * @see WP_Widget::update()
490 *
491 * @param array $new_instance Values just sent to be saved.
492 * @param array $old_instance Previously saved values from database.
493 *
494 * @return array Updated safe values to be saved.
495 */
496 public function update( $new_instance, $old_instance ) {
497 $instance = $old_instance;
498
499 $instance['title'] = empty( $new_instance['title'] ) ? '' : sanitize_text_field( $new_instance['title'] );
500
501 $fields = $this->get_fields();
502 if ( ! empty( $fields ) ) {
503 foreach ( $fields as $key => $val ) {
504 if ( empty( $val['type'] ) ) {
505 continue;
506 }
507 switch ( $val['type'] ) {
508 case 'text':
509 $instance[ $key ] = empty( $new_instance[ $key ] ) ? '' : sanitize_text_field( $new_instance[ $key ] );
510 break;
511
512 case 'textarea':
513 $instance[ $key ] = empty( $new_instance[ $key ] ) ? '' : sanitize_textarea_field( $new_instance['title'] );
514 break;
515
516 case 'checkbox':
517 $instance[ $key ] = empty( $new_instance[ $key ] ) ? 0 : 1;
518 break;
519
520 case 'number':
521 $instance[ $key ] = intval( $new_instance[ $key ] );
522 break;
523
524 case 'select':
525 case 'category':
526 if ( empty( $new_instance[ $key ] ) ) {
527 $instance[ $key ] = '';
528 } else {
529 if ( is_array( $new_instance[ $key ] ) ) {
530 $instance[ $key ] = array_map( 'sanitize_key', $new_instance[ $key ] );
531 } else {
532 $instance[ $key ] = sanitize_key( $new_instance[ $key ] );
533 }
534 }
535 break;
536 }
537 }
538 }
539
540 return $instance;
541 }
542
543 /**
544 * Front-end display of widget.
545 *
546 * @see WP_Widget::widget()
547 *
548 * @param array $args Widget arguments.
549 * @param array $instance Saved values from database.
550 */
551 public function widget( $args, $instance ) {
552 $additional_classes = '';
553 if ( ! empty( $instance['className'] ) ) {
554 $additional_classes = ' ' . esc_attr( $instance['className'] );
555 }
556
557 if ( ! empty( $instance['block_title_el'] ) ) {
558 if ( in_array( $instance['block_title_el'], flex_posts_get_title_elements(), true ) ) {
559 $el = $instance['block_title_el'];
560
561 $args['before_title'] = '<' . sanitize_key( $el ) . ' class="widget-title">';
562 $args['after_title'] = '</' . sanitize_key( $el ) . '>';
563 }
564 }
565
566 $before_widget = $args['before_widget'];
567 if ( ! empty( $additional_classes ) ) {
568 $before_widget = str_replace( $this->option_name, $this->option_name . $additional_classes, $before_widget );
569 }
570 echo $before_widget; // @codingStandardsIgnoreLine.
571 $title = ! empty( $instance['title'] ) ? $instance['title'] : '';
572 $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
573 echo $args['before_title'] . $title . $args['after_title']; // @codingStandardsIgnoreLine.
574 $this->front( $instance );
575 echo $args['after_widget']; // @codingStandardsIgnoreLine.
576 }
577 }
578