PluginProbe
Flex Posts – Responsive Posts Block / 2.0.0
Flex Posts – Responsive Posts Block v2.0.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 2.0.0, at includes/class-flex-posts-widget.php

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