-
Notifications
You must be signed in to change notification settings - Fork 21
/
class-widget.php
2033 lines (1820 loc) · 78.1 KB
/
class-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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Implementation of the widget class.
*
* @package categoryposts.
*
* @since 4.7
*/
namespace categoryPosts;
// Don't call the file directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Category Posts Widget Class
*
* Shows the single category posts with some configurable options
*/
class Widget extends \WP_Widget {
/**
* Widget constructor.
*/
public function __construct() {
$widget_ops = array(
'show_instance_in_rest' => true,
'classname' => 'cat-post-widget',
'description' => __( 'List single category posts', 'category-posts' ),
);
parent::__construct( WIDGET_BASE_ID, __( 'Category Posts', 'category-posts' ), $widget_ops );
}
/**
* Calculate the HTML for showing the thumb of a post item.
*
* Used as a filter for the thumb wordpress API to add css based stretching and cropping
* when the image is not at the requested dimensions
*
* @param int $post_id the ID of the post of which the thumb is a featured image.
* @param int $post_thumbnail_id The id of the featured image attachment.
* @param string|array $size The requested size identified by name or (width, height) array.
* @param mixed $attr ignored in this context.
* @return string The HTML for the thumb related to the post
*
* @since 4.1
*/
public function post_thumbnail_html( $post_id, $post_thumbnail_id, $size, $attr ) {
// HTML generated by the core APIs.
$html = wp_get_attachment_image
(
$post_thumbnail_id,
null,
false,
array
(
"data-cat-posts-width" => $this->instance['thumb_w'],
"data-cat-posts-height" => $this->instance['thumb_h']
)
);
if ( ! $html ) {
return '';
}
// normalize style
$html = preg_replace( '/style="([^"]*)"/i', '', $html );
// replace size.
$array = array();
preg_match( '/width="([^"]*)"/i', $html, $array );
$pattern = '/\s' . $array[1] . 'px/';
$html = preg_replace( $pattern, ' ' . $size[0] . 'px', $html );
// replace width.
$pattern = '/width="' . $array[1] . '"/';
if ( 0 != $this->instance['thumb_w'] ) {
$html = preg_replace( $pattern, 'width="' . $size[0] . '"', $html );
} else {
$html = preg_replace( $pattern, '', $html );
}
// replace height
$array = array();
preg_match( '/height="([^"]*)"/i', $html, $array );
$pattern = '/height="' . $array[1] . '"/';
if ( 0 != $this->instance['thumb_h'] ) {
$html = preg_replace( $pattern, 'height="' . $size[1] . '"', $html );
} else {
$html = preg_replace( $pattern, '', $html );
}
$show_post_format = isset( $this->instance['show_post_format'] ) && ( 'none' !== $this->instance['show_post_format'] );
if ( $show_post_format || $this->instance['thumb_hover'] ) {
$format = get_post_format() ? : 'standard';
$post_format_class = 'cat-post-format cat-post-format-' . $format;
}
$html = '<span class="cat-post-crop ' . $post_format_class . '">' . $html . '</span>';
return $html;
}
/*
* wrapper to execute the the_post_thumbnail with filters.
*/
/**
* Calculate the HTML for showing the thumb of a post item.
*
* It is a wrapper to execute the the_post_thumbnail with filters
*
* @param string|array $size The requested size identified by name or (width, height) array.
*
* @return string The HTML for the thumb related to the post and empty string if it can not be calculated
*
* @since 4.1
*/
public function the_post_thumbnail( $size = 'post-thumbnail' ) {
if ( empty( $size ) ) { // if junk value, make it a normal thumb.
$size = 'post-thumbnail';
} elseif ( is_array( $size ) && ( 2 === count( $size ) ) ) { // good format at least.
// normalize to ints first.
list( $width, $height ) = array_map('intval', $size);
if ( ( 0 === $width ) && ( 0 === $height ) ) { // Both values zero then revert to ratio from the orig with large.
$size = array( get_option( 'large_size_w', 150 ), get_option( 'large_size_h', 150 ) );
} elseif ( ( 0 === $width ) && ( 0 !== $height ) ) {
// if thumb width 0 set to max/full widths for wp rendering.
$post_thumb = get_the_post_thumbnail( get_the_ID(), 'full' );
preg_match( '/(?<=width=")[\d]*/', $post_thumb, $thumb_full_w );
$size[0] = $thumb_full_w[0];
} elseif ( ( 0 !== $width ) && ( 0 === $height ) ) {
// if thumb height 0 get full thumb for ratio and calc height with ratio.
$post_thumb = get_the_post_thumbnail( get_the_ID(), 'full' );
preg_match( '/(?<=width=")[\d]*/', $post_thumb, $thumb_full_w );
preg_match( '/(?<=height=")[\d]*/', $post_thumb, $thumb_full_h );
$ratio = $thumb_full_w[0] / $thumb_full_h[0];
$size[1] = intval( $width / $ratio );
}
} else {
$size = array( get_option( 'thumbnail_size_w', 150 ), get_option( 'thumbnail_size_h', 150 ) ); // yet another form of junk.
}
$post_thumbnail_id = get_post_thumbnail_id( get_the_ID() );
if ( ! $post_thumbnail_id && $this->instance['default_thunmbnail'] ) {
$post_thumbnail_id = $this->instance['default_thunmbnail'];
}
do_action( 'begin_fetch_post_thumbnail_html', get_the_ID(), $post_thumbnail_id, $size );
$ret = $this->post_thumbnail_html( get_the_ID(), $post_thumbnail_id, $size, '' );
do_action( 'end_fetch_post_thumbnail_html', get_the_ID(), $post_thumbnail_id, $size );
return $ret;
}
/**
* Excerpt more link filter
*
* @param string $more The "more" text passed by the filter.
*
* @return string The link to the post with the "more" text configured in the widget.
*/
public function excerpt_more_filter( $more ) {
return ' <a class="cat-post-excerpt-more more-link" href="' . get_permalink() . '">' . esc_html( $this->instance['excerpt_more_text'] ) . '</a>';
}
/**
* Apply the_content filter for excerpt
* This should show sharing buttons which comes with other widgets in the widget output in the same way as on the main content
*
* @param string $text The HTML with other applied excerpt filters.
*
* @return string If option hide_social_buttons is unchecked applay the_content filter.
*
* @since 4.6
*/
public function apply_the_excerpt( $text ) {
$ret = apply_filters( 'the_content', $text );
return $ret;
}
/**
* Calculate the wp-query arguments matching the filter settings of the widget
*
* @param array $instance Array which contains the various settings.
* @return array The array that can be fed to wp_Query to get the relevant posts
*
* @since 4.6
*/
public function queryArgs( $instance ) {
$valid_sort_orders = array(
'date', 'title', 'comment_count', 'rand'
);
if ( isset( $instance['sort_by'] ) && in_array( $instance['sort_by'], $valid_sort_orders, true ) ) {
$sort_by = $instance['sort_by'];
} else {
$sort_by = 'date';
}
$sort_order = ( isset( $instance['asc_sort_order'] ) && $instance['asc_sort_order'] ) ? 'ASC' : 'DESC';
// start sticky posts
$ignore_sticky = isset( $instance['sticky'] ) && $instance['sticky'] ? false : true;
// Get array of post info.
$args = array(
'orderby' => $sort_by,
'order' => $sort_order,
'ignore_sticky_posts' => $ignore_sticky, // Make sure we do not get stickies out of order.
'no_found_rows' => true, // Do not count the total numbers of rows by default.
);
$non_default_valid_status = array(
'publish',
'future',
'publish,future',
'private',
'private,publish',
'private,publish,future',
);
if ( isset( $instance['status'] ) && in_array( $instance['status'], $non_default_valid_status, true ) ) {
$args['post_status'] = $instance['status'];
}
if ( isset( $instance['num'] ) ) {
$args['showposts'] = (int) $instance['num'];
}
if ( isset( $instance['offset'] ) && ( (int) $instance['offset'] > 1 ) ) {
$args['offset'] = (int) $instance['offset'] - 1;
}
if ( isset( $instance['cat'] ) ) {
if ( isset( $instance['no_cat_childs'] ) && $instance['no_cat_childs'] ) {
$args['category__in'] = (int) $instance['cat'];
} else {
$args['cat'] = (int) $instance['cat'];
}
}
if ( is_singular() && isset( $instance['exclude_current_post'] ) && $instance['exclude_current_post'] ) {
$args['post__not_in'] = array( get_the_ID() );
}
if ( isset( $instance['hideNoThumb'] ) && $instance['hideNoThumb'] ) {
$args = array_merge(
$args,
array(
'meta_query' => array(
array(
'key' => '_thumbnail_id',
'compare' => 'EXISTS',
),
),
)
);
}
switch ( $instance['date_range'] ) {
case 'days_ago':
$ago = (int) $instance['days_ago'];
// If there is no valid integer value given, bail.
if ( 0 === $ago ) {
break;
}
$date = date( 'Y-m-d', strtotime( '-' . $ago . ' days' ) );
$args['date_query'] = array(
'after' => $date,
'inclusive' => true,
);
break;
case 'between_dates':
// Validation note - not doing any, assuming the query will
// fail gracefully enough for now as it is not clear what Should
// the validation be right now.
$start_date = $instance['start_date'];
$end_date = $instance['end_date'];
$args['date_query'] = array(
'after' => $start_date,
'before' => $end_date,
'inclusive' => true,
);
break;
}
return $args;
}
/**
* Calculate the HTML of the title based on the widget settings
*
* @param string $before_title The sidebar configured HTML that should come
* before the title itself.
* @param string $after_title The sidebar configured HTML that should come
* after the title itself.
* @param array $instance Array which contains the various settings.
* @return string The HTML for the title area
*
* @since 4.6
*/
public function titleHTML( $before_title, $after_title, $instance ) {
$ret = '';
if( in_array( $instance['title_level'], array( 'H1','H2', 'H3', 'H6', 'H5', 'H6') ) ) {
$before_title = '';
$after_title = '';
}
// If no title, use the name of the category.
if ( ! isset( $instance['title'] ) || ! $instance['title'] ) {
$instance['title'] = '';
if ( 0 !== (int) $instance['cat'] ) {
$category_info = get_category( $instance['cat'] );
if ( $category_info && ! is_wp_error( $category_info ) ) {
$instance['title'] = $category_info->name;
} else {
$instance['cat'] = 0; // For further processing treat it like "all categories".
$instance['title'] = __( 'Category Posts', 'category-posts' );
}
} else {
$instance['title'] = __( 'Category Posts', 'category-posts' );
}
}
if ( ! ( isset( $instance['hide_title'] ) && $instance['hide_title'] ) ) {
$ret = $before_title;
if ( isset( $instance['is_shortcode'] ) ) {
$title = esc_html( $instance['title'] );
} else {
$title = apply_filters( 'widget_title', $instance['title'], $instance, WIDGET_BASE_ID );
}
if ( isset( $instance['title_link'] ) && $instance['title_link'] ) {
if ( 0 !== (int) $instance['cat'] ) {
$ret .= '<a href="' . get_category_link( $instance['cat'] ) . '">' . $title . '</a>';
} elseif ( isset( $instance['title_link_url'] ) && $instance['title_link_url'] ) {
$ret .= '<a href="' . esc_url( $instance['title_link_url'] ) . '">' . $title . '</a>';
} else {
$ret .= '<a href="' . esc_url( $this->blog_page_url() ) . '">' . $title . '</a>';
}
} else {
$ret .= $title;
}
$ret .= $after_title;
}
$ret = $this->add_heading_level( $instance, $ret, 'title_level' );
return $ret;
}
/**
* Add a heading level
*
* @return string The title string
*
* @since 5.0
*/
public function add_heading_level( $instance, $ret, $key ) {
$class = ( isset( $instance[ 'disable_theme_styles' ] ) && $instance[ 'disable_theme_styles' ] ) ? '' : ' class="widget-title"';
switch( $instance[ $key ] ) {
case 'H1':
$ret = '<h1' . $class . '>' . $ret . '</h1>';
break;
case 'H2':
$ret = '<h2' . $class . '>' . $ret . '</h2>';
break;
case 'H3':
$ret = '<h3' . $class . '>' . $ret . '</h3>';
break;
case 'H4':
$ret = '<h4' . $class . '>' . $ret . '</h4>';
break;
case 'H5':
$ret = '<h5' . $class . '>' . $ret . '</h5>';
break;
case 'H6':
$ret = '<h6' . $class . '>' . $ret . '</h6>';
break;
}
return $ret;
}
/**
* Get the URL of the blog page or home page if no explicit blog page is defined.
*
* @return string The URL of the blog page
*
* @since 4.8
*/
private function blog_page_url() {
$blog_page = get_option( 'page_for_posts' );
if ( $blog_page ) {
$url = get_permalink( $blog_page );
} else {
$url = home_url();
}
return $url;
}
/**
* Calculate the HTML of the load more button based on the widget settings
*
* @param array $instance Array which contains the various settings.
*
* @return string The HTML for the load more area
*
* @since 4.9
*/
public function loadMoreHTML( $instance ) {
global $post_count;
if ( ! $instance['enable_loadmore'] || $post_count <= $instance['num'] ) {
return '';
}
$ret = '<div class="' . __NAMESPACE__ . '-loadmore">';
$context = 0;
if ( is_singular() ) {
$context = get_the_ID();
}
wp_enqueue_script( 'jquery' );
add_action( 'wp_footer', __NAMESPACE__ . '\embed_loadmore_scripts', 100 );
// We rely on the widget number to be properly set.
// but need a slight different handling for proper widgets.
if ( is_int( $this->number ) ) {
// it is a proper widget, add the prefix.
$id = 'widget-' . $this->number;
} else {
$id = str_replace( WIDGET_BASE_ID . '-', '', $this->number );
}
// Placeholder
$placeholder_text = $instance['loadmore_text'] !== '' ? $instance['loadmore_text'] : sprintf( esc_attr__( 'Load More (%s/%s)', 'category-posts' ), '%step%', '%all%');
$pattern = '/%step%/';
$loadmore_text = preg_replace( $pattern, $instance['num'], $placeholder_text );
$pattern = '/%all%/';
$loadmore_text = preg_replace( $pattern, $post_count, $loadmore_text );
// Load more
$number = $instance['num'];
$start = $instance['offset'] + $number;
$loading = $instance['loading_text'] !== '' ? $instance['loading_text'] : esc_attr__( 'Loading...', 'category-posts' );
$scrollTo = isset( $instance['loadmore_scrollTo'] ) && $instance['loadmore_scrollTo'];
$ret .= '<button type="button" data-loading="' . esc_attr( $loading ) . '" data-id="' . esc_attr( $id ) .
'" data-start="' . esc_attr( $start ) . '" data-context="' . esc_attr( $context ) . '" data-number="' . esc_attr( $number ) .
'" data-post-count="' . esc_attr( $post_count ) . '" data-placeholder="' . esc_attr( $placeholder_text ) . '" data-scrollto="' . esc_html( $scrollTo ) . '">' .
esc_html( $loadmore_text ) .
'</button>';
$ret .= '</div>';
return $ret;
}
/**
* Calculate the HTML of the footer based on the widget settings
*
* @param array $instance Array which contains the various settings.
* @return string The HTML for the footer area
*
* @since 4.6
*/
public function footerHTML( $instance ) {
$ret = '';
$url = '';
$text = '';
if ( isset( $instance['footer_link'] ) ) {
$url = $instance['footer_link'];
}
if ( isset( $instance['footer_link_text'] ) ) {
$text = $instance['footer_link_text'];
}
// if url is set, but no text, just use the url as text.
if ( empty( $text ) && ! empty( $url ) ) {
$text = $url;
}
// if no url is set but just text, assume the url should be to the relevant archive page
// category archive for categories filter and home page or blog page when "all categories"
// is used.
if ( ! empty( $text ) && empty( $url ) ) {
if ( isset( $instance['cat'] ) && ( 0 !== (int) $instance['cat'] ) && ( null !== get_category( $instance['cat'] ) ) ) {
$url = get_category_link( $instance['cat'] );
} else {
$url = $this->blog_page_url();
}
}
if ( ! empty( $url ) ) {
$ret .= '<a class="cat-post-footer-link" href="' . esc_url( $url ) . '">' . esc_html( $text ) . '</a>';
}
return $ret;
}
/**
* Current post item date string based on the format requested in the settings
*
* @param array $instance Array which contains the various settings.
* @param bool $everything_is_link Indicates whether the return string should avoid links.
*
* @since 4.8
*/
public function itemDate( $instance, $everything_is_link ) {
global $post;
$ret = '';
if ( ! isset( $instance['preset_date_format'] ) ) {
$preset_date_format = 'other';
} else {
$preset_date_format = $instance['preset_date_format'];
}
$attr = '';
switch ( $preset_date_format ) {
case 'sitedateandtime':
$date = get_the_time( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) );
break;
case 'localsitedateandtime':
$date = get_the_time( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) ) . ' GMT';
$time = get_post_time( 'U', true );
$attr = ' data-publishtime="' . $time . '" data-format="time"';
add_action( 'wp_footer', __NAMESPACE__ . '\embed_date_scripts' );
break;
case 'sitedate':
$date = get_the_time( get_option( 'date_format' ) );
break;
case 'localsitedate':
$date = get_the_time( get_option( 'date_format' ) ) . ' GMT';
$time = get_post_time( 'U', true );
$attr = ' data-publishtime="' . $time . '" data-format="date"';
add_action( 'wp_footer', __NAMESPACE__ . '\embed_date_scripts' );
break;
default:
if ( isset( $instance['date_format'] ) && strlen( trim( $instance['date_format'] ) ) > 0 ) {
$date_format = $instance['date_format'];
} else {
$date_format = 'j M Y';
}
$date = get_the_time( $date_format );
break;
}
if ( isset( $instance['date_past_time'] ) && 0 < $instance['date_past_time'] && $post ) {
$post_date = get_the_time( "Y-m-d H:i:s", $post->ID );
$current_date = current_time( "Y-m-d H:i:s" );
$past_days = date_diff(
date_create( $post_date ),
date_create( $current_date )
)->days;
if ( $past_days <= $instance['date_past_time'] ) {
$date = human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) );
$attr = ' data-publishtime="" data-format="sincepublished"';
}
}
$post_date_class = ( isset( $instance[ 'disable_theme_styles' ] ) && $instance[ 'disable_theme_styles' ] ) ? "" : " post-date";
$ret .= '<span class="cat-post-date' . $post_date_class . '"' . $attr . '>' . $date . '</span>';
return $ret;
}
/**
* Calculate the HTML for showing the thumb of a post item.
* Expected to be called from a loop with globals properly set.
*
* @param array $instance Array which contains the various settings.
* @param bool $no_link indicates whether the thumb should be wrapped in a link or a span.
* @return string The HTML for the thumb related to the post
*
* @since 4.6
*/
public function itemThumb( $instance, $no_link ) {
$ret = '';
if ( ( isset( $instance['default_thunmbnail'] ) && ( $instance['default_thunmbnail'] ) ) || has_post_thumbnail() ) {
$class = '';
$disable_css = isset( $instance['disable_css'] ) && $instance['disable_css'];
if ( isset( $this->instance['thumb_hover'] ) && ! $disable_css ) {
$class = 'class="cat-post-thumbnail cat-post-' . $instance['thumb_hover'] . '"';
} else {
$class = 'class="cat-post-thumbnail"';
}
$title_args = array( 'echo' => false );
if ( $no_link ) {
$ret .= '<span ' . $class . '>';
} else {
$ret .= '<a ' . $class . ' href="' . get_the_permalink() . '" title="' . the_title_attribute( $title_args ) . '">';
}
$ret .= $this->the_post_thumbnail( array( $this->instance['thumb_w'], $this->instance['thumb_h'] ) );
if ( $no_link ) {
$ret .= '</span>';
} else {
$ret .= '</a>';
}
}
return $ret;
}
/**
* Current post item categories string
*
* @param array $instance Array which contains the various settings.
* @param bool $everything_is_link Indicates whether the return string should avoid links.
*
* @since 4.8
*/
public function itemCategories( $instance, $everything_is_link ) {
$post_category_class = ( isset( $instance[ 'disable_theme_styles' ] ) && $instance[ 'disable_theme_styles' ] ) ? "" : " entry-categories post-categories";
$ret = '<span class="cat-post-tax-category' . $post_category_class . '">';
$cat_ids = wp_get_post_categories( get_the_ID(), array( 'number' => 0 ) );
foreach ( $cat_ids as $cat_id ) {
if ( $everything_is_link ) {
$ret .= ' ' . get_cat_name( $cat_id );
} else {
$ret .= " <a href='" . get_category_link( $cat_id ) . "'>" . get_cat_name( $cat_id ) . '</a>';
}
}
$ret .= '</span>';
return $ret;
}
/**
* Current post item tags string
*
* @param array $instance Array which contains the various settings.
* @param bool $everything_is_link Indicates whether the return string should avoid links.
*
* @since 4.8
*/
public function itemTags( $instance, $everything_is_link ) {
$post_tag_class = ( isset( $instance[ 'disable_theme_styles' ] ) && $instance[ 'disable_theme_styles' ] ) ? "" : " widget_tag_cloud tagcloud post-tags";
$ret = '<span class="cat-post-tax-tag' . $post_tag_class . '">';
$tag_ids = wp_get_post_tags( get_the_ID(), array( 'number' => 0 ) );
foreach ( $tag_ids as $tag_id ) {
if ( $everything_is_link ) {
$ret .= ' ' . $tag_id->name;
} else {
$ret .= " <a href='" . get_tag_link( $tag_id->term_id ) . "'>" . $tag_id->name . '</a>';
}
}
$ret .= '</span>';
return $ret;
}
/**
* Current post item comment number string
*
* @param array $instance Array which contains the various settings.
* @param bool $everything_is_link Indicates whether the return string should avoid links.
*
* @since 4.8
*/
public function itemCommentNum( $instance, $everything_is_link ) {
global $post;
$ret = '<span class="cat-post-comment-num comment-meta">';
if ( $everything_is_link ) {
$ret .= '(' . \get_comments_number() . ')';
} else {
$link = sprintf(
'<a href="%1$s" title="%2$s">(%3$d)</a>',
esc_url( get_comments_link( $post->ID ) ),
esc_attr( sprintf( __( '(%d) comments to this post' ), get_comments_number() ) ),
get_comments_number()
);
$ret .= $link;
}
$ret .= '</span>';
return $ret;
}
/**
* Current post item author string
*
* @param array $instance Array which contains the various settings.
* @param bool $everything_is_link Indicates whether the return string should avoid links.
*
* @since 4.8
*/
public function itemAuthor( $instance, $everything_is_link ) {
$post_author_class = ( isset( $instance[ 'disable_theme_styles' ] ) && $instance[ 'disable_theme_styles' ] ) ? "" : " post-author";
$ret = '<span class="cat-post-author' . $post_author_class . '">';
if ( $everything_is_link ) {
$ret .= get_the_author();
} else {
$link = get_the_author_posts_link();
$ret .= $link;
}
$ret .= '</span>';
return $ret;
}
/**
* Current post item excerpt string
*
* @param array $instance Array which contains the various settings.
* @param bool $everything_is_link Indicates whether the return string should avoid links.
*
* @since 4.8
*/
public function itemExcerpt( $instance, $everything_is_link ) {
global $post;
// use the_excerpt filter to get the "normal" excerpt of the post
// then apply our filter to let users customize excerpts in their own way.
if ( isset( $instance['excerpt_length'] ) && ( $instance['excerpt_length'] > 0 ) ) {
$length = (int) $instance['excerpt_length'];
} else {
$length = 999; // Use the wordpress default.
}
if ( ! isset( $instance['excerpt_filters'] ) || $instance['excerpt_filters'] ) { // pre 4.7 widgets has filters on.
$excerpt = apply_filters( 'the_excerpt', \get_the_excerpt() );
} else { // if filters off replicate functionality of core generating excerpt.
$more_text = '[…]';
if ( isset( $instance['excerpt_more_text'] ) && $instance['excerpt_more_text'] ) {
$more_text = ltrim( $instance['excerpt_more_text'] );
}
if ( $everything_is_link ) {
$excerpt_more_text = ' <span class="cat-post-excerpt-more">' . $more_text . '</span>';
} else {
$excerpt_more_text = ' <a class="cat-post-excerpt-more" href="' . get_permalink() . '" title="' . sprintf( __( 'Continue reading %s' ), get_the_title() ) . '">' . $more_text . '</a>';
}
if ( '' === $post->post_excerpt ) {
$text = get_the_content( '' );
$text = strip_shortcodes( $text );
$excerpt = \wp_trim_words( $text, $length, $excerpt_more_text );
// adjust html output same way as for the normal excerpt,
// just force all functions depending on the_excerpt hook.
$excerpt = shortcode_unautop( wpautop( convert_chars( convert_smilies( wptexturize( $excerpt ) ) ) ) );
} else {
$text = $post->post_excerpt;
$excerpt = \wp_trim_words( $text, $length, $excerpt_more_text );
$excerpt = shortcode_unautop( wpautop( convert_chars( convert_smilies( wptexturize( $excerpt ) ) ) ) );
}
}
$excerpt = str_replace('<p>', '<p class="cpwp-excerpt-text">', $excerpt);
$ret = apply_filters( 'cpw_excerpt', $excerpt, $this );
return $ret;
}
/**
* Current post item More Link string
*
* @param array $instance Array which contains the various settings.
* @param bool $everything_is_link Indicates whether the return string should avoid links.
*
* @since 5.0
*/
public function itemMoreLink( $instance, $everything_is_link ) {
global $post;
$more_text = '[…]';
if ( isset( $instance['excerpt_more_text'] ) && '' !== $instance['excerpt_more_text'] ) {
$more_text = sanitize_text_field( $instance['excerpt_more_text'] );
}
$more_link_class = ( isset( $instance[ 'disable_theme_styles' ] ) && $instance[ 'disable_theme_styles' ] ) ? "" : " more-link";
if ( $everything_is_link ) {
$ret = ' <span class="cat-post-excerpt-more' . $more_link_class . '">' . $more_text . '</span>';
} else {
$ret = ' <a class="cat-post-excerpt-more' . $more_link_class . '" href="' . get_permalink() . '">' . $more_text . '</a>';
}
if ( isset( $instance['excerpt_more_text'] ) && '' === $instance['excerpt_more_text'] ) {
$ret = '' !== apply_filters( 'excerpt_more', '' ) ? apply_filters( 'excerpt_more', '' ) : $ret;
}
return $ret;
}
/**
* Current post item title string
*
* @param array $instance Array which contains the various settings.
* @param bool $everything_is_link Indicates whether the return string should avoid links.
*
* @since 4.8
*/
public function itemTitle( $instance, $everything_is_link ) {
$ret = '';
if ( $everything_is_link ) {
$ret .= '<span class="cat-post-title">' . get_the_title() . '</span>';
} else {
$ret .= '<a class="cat-post-title"';
$ret .= ' href="' . get_the_permalink() . '" rel="bookmark">' . get_the_title();
$ret .= '</a>';
}
$ret = $this->add_heading_level( $instance, $ret, 'item_title_level' );
return $ret;
}
/**
* Calculate the HTML for a post item based on the widget settings and post.
* Expected to be called in an active loop with all the globals set.
*
* @param array $instance Array which contains the various settings.
* @param null|integer $current_post_id If on singular page specifies the id of
* the post, otherwise null.
* @return string The HTML for item related to the post
*
* @since 4.6
*/
public function itemHTML( $instance, $current_post_id ) {
global $post;
$everything_is_link = isset( $instance['everything_is_link'] ) && $instance['everything_is_link'];
$no_wrap = isset( $instance['text_do_not_wrap_thumb'] ) && $instance['text_do_not_wrap_thumb'];
$template = '';
if ( isset( $instance['template'] ) ) {
$template = $instance['template'];
} else {
$template = convert_settings_to_template( $instance );
}
$ret = '<li ';
// Current post.
if ( $current_post_id === $post->ID ) {
$ret .= "class='cat-post-item cat-post-current'";
} else {
$ret .= "class='cat-post-item'";
}
$ret .= '>'; // close the li opening tag.
if ( $everything_is_link ) {
$ret .= '<a class="cat-post-everything-is-link" href="' . get_the_permalink() . '" title="">';
}
// Post details (Template).
$widget = $this;
$template_res = preg_replace_callback(
get_template_regex(),
function ( $matches ) use ( $widget, $instance, $everything_is_link ) {
switch ( $matches[0] ) {
case '%title%':
return $widget->itemTitle( $instance, $everything_is_link );
case '%author%':
return $widget->itemAuthor( $instance, $everything_is_link );
case '%commentnum%':
return $widget->itemCommentNum( $instance, $everything_is_link );
case '%date%':
return $widget->itemDate( $instance, $everything_is_link );
case '%thumb%':
return $widget->itemThumb( $instance, $everything_is_link );
case '%post_tag%':
return $widget->itemTags( $instance, $everything_is_link );
case '%category%':
return $widget->itemCategories( $instance, $everything_is_link );
case '%excerpt%':
return $widget->itemExcerpt( $instance, $everything_is_link );
case '%more-link%':
return $widget->itemMoreLink( $instance, $everything_is_link );
default:
return $matches[0];
}
},
$template
);
// Replace empty line with closing and opening DIV.
$template_res = trim( $template_res );
$template_res = str_replace( "\n\r", '</div><div>', $template_res ); // in widget areas.
$template_res = str_replace( "\n\n", '</div><div>', $template_res ); // as shortcode.
$template_res = '<div>' . $template_res . '</div>';
// replace new lines with spaces.
$template_res = str_replace( "\n\r", ' ', $template_res ); // in widget areas.
$template_res = str_replace( "\n\n", ' ', $template_res ); // as shortcode.
$ret .= $template_res;
if ( $everything_is_link ) {
$ret .= '</a>';
}
$ret .= '</li>';
return $this->xss_strip_js( $ret );
}
/**
* Strip output from Javascript tags
*
* @param string $out Render output
* @return string Striped output
*
* @since 5.0
*/
public function xss_strip_js( $out ) {
return preg_replace('/<script\b[^>]*>(.*?)<\/script>/is', "", $out);
}
/**
* Filter to set the number of words in an excerpt
*
* @param int $length The number of words as configured by wordpress core or set by previous filters.
* @return int The number of words configured for the widget,
* or the $length parameter if it is not configured or garbage value.
*
* @since 4.6
*/
public function excerpt_length_filter( $length ) {
if ( isset( $this->instance['excerpt_length'] ) && $this->instance['excerpt_length'] > 0 ) {
$length = $this->instance['excerpt_length'];
}
return $length;
}
/**
* Set the proper excerpt filters based on the settings
*
* @param array $instance widget settings.
* @return void
*
* @since 4.6
*/
public function setExcerpFilters( $instance ) {
if ( isset( $instance['excerpt'] ) && $instance['excerpt'] ) {
// Excerpt length filter.
if ( isset( $instance['excerpt_length'] ) && ( (int) $instance['excerpt_length'] ) > 0 ) {
add_filter( 'excerpt_length', array( $this, 'excerpt_length_filter' ) );
}
if ( isset( $instance['excerpt_more_text'] ) && ( '' !== ltrim( $instance['excerpt_more_text'] ) ) ) {
add_filter( 'excerpt_more', array( $this, 'excerpt_more_filter' ) );
}
add_filter( 'the_excerpt', array( $this, 'apply_the_excerpt' ) );
}
}
/**
* Remove the excerpt filter
*
* @param array $instance widget settings.
* @return void
*
* @since 4.6
*/
public function removeExcerpFilters( $instance ) {
remove_filter( 'excerpt_length', array( $this, 'excerpt_length_filter' ) );
remove_filter( 'excerpt_more', array( $this, 'excerpt_more_filter' ) );
add_filter( 'get_the_excerpt', 'wp_trim_excerpt' );
remove_filter( 'the_excerpt', array( $this, 'apply_the_excerpt' ) );
}
/**
* The main widget display controller
*
* Called by the sidebar processing core logic to display the widget.
*
* @param array $args An array containing the "environment" setting for the widget,
* namely, the enclosing tags for the widget and its title.
* @param array $instance The settings associate with the widget.
*
* @since 4.1
*/
public function widget( $args, $instance ) {
global $before_title, $after_title;