This repository has been archived by the owner on Apr 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
pis-functions.php
1020 lines (904 loc) · 31.7 KB
/
pis-functions.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
/**
* Prevent direct access to this file.
*
* @since 2.0
*/
if ( ! defined( 'WPINC' ) ) {
exit( 'No script kiddies please!' );
}
/**
* Return the class for the HTML element.
*
* @since 1.9
*
* @param string $default One or more classes, defined by plugin's developer, to add to the class list.
* @param string|array $class One or more classes, defined by the user, to add to the class list.
* @param boolean $echo If the function should echo or not the output. Default true.
* @return string $output List of classes.
*/
function pis_class( $default = '', $class = '', $echo = true ) {
// Define $classes as array
$classes = array();
// If $default is not empy, add the value ad an element of the array
if( ! empty( $default ) )
$classes[] = $default;
// If $class is not empty, transform it into an array and add the elements to the array
if ( ! empty( $class ) ) {
if ( ! is_array( $class ) ) $class = preg_split( '#\s+#', $class );
$classes = array_merge( $classes, $class );
}
// Escape evil chars in $classes
$classes = array_map( 'esc_attr', $classes );
// Remove null or empty or space-only-filled elements from the array
foreach ( $classes as $key => $value ) {
if ( is_null( $value ) || $value == '' || $value == ' ' ) {
unset( $classes[ $key ] );
}
}
// Convert the array into string
$classes = implode( ' ', $classes );
// Complete the final output
$classes = 'class="' . $classes . '"';
if ( true === $echo ) {
echo apply_filters( 'pis_classes', $classes );
} else {
return apply_filters( 'pis_classes', $classes );
}
}
/**
* Return the paragraph class with inline style.
*
* @since 1.12
*
* @param string $margin The margin of the paragraph.
* @param string $unit The unit measure to be used.
* @param string $class The default class defined by the plugin's developer.
* @param string $class_filter The name of the class filter.
* @return string $output The class and the inline style.
* @uses pis_class()
*/
function pis_paragraph( $margin, $unit, $class, $class_filter ) {
( ! is_null( $margin ) ) ? $style = ' style="margin-bottom: ' . $margin . $unit . ';"' : $style = '';
$output = pis_class( $class, apply_filters( $class_filter, '' ), false ) . $style;
return $output;
}
/**
* Return the given text with paragraph breaks (HTML <br />).
*
* @since 1.12
* @param string $text The text to be checked.
* @return string $text The checked text with paragraph breaks.
*/
function pis_break_text( $text ) {
// Convert cross-platform newlines into HTML '<br />'
$text = str_replace( array( "\r\n", "\n", "\r" ), "<br />", $text );
return $text;
}
/**
* Return the array containing the custom fields of the post.
*
* @since 1.12
* @return array The custom fields of the post.
*/
function pis_meta() {
global $wpdb;
$limit = (int) apply_filters( 'pis_postmeta_limit', 30 );
$keys = $wpdb->get_col( "
SELECT meta_key
FROM $wpdb->postmeta
GROUP BY meta_key
HAVING meta_key NOT LIKE '\_%'
ORDER BY meta_key
LIMIT $limit" );
if ( $keys )
natcasesort($keys);
return $keys;
}
/**
* Generate an HTML arrow.
*
* @since 1.15
* @return string $output The HTML arrow.
* @uses pis_class()
*/
function pis_arrow( $pre_space = true ) {
$the_arrow = '→';
if ( is_rtl() ) $the_arrow = '←';
if ( $pre_space ) {
$space = ' ';
} else {
$space = '';
}
$output = $space . '<span ' . pis_class( 'pis-arrow', apply_filters( 'pis_arrow_class', '' ), false ) . '>' . $the_arrow . '</span>';
return $output;
}
/**
* Generate the output for the more and/or the HTML arrow.
*
* @since 1.15
* @uses pis_arrow()
* @param string $the_more The text to be displayed for "Continue reading". Default empty.
* @param boolean $no_the_more If the text for "Continue reading" must be hidden. Default false.
* @param boolean $exc_arrow If the arrow must be displayed or not. Default false.
* @param boolean $echo If echo the output or return.
* @return string The HTML arrow linked to the post.
*/
function pis_more_arrow( $the_more = '', $no_the_more = false, $exc_arrow = false, $echo = true ) {
if ( $the_more || $exc_arrow ) {
if ( $the_more && $exc_arrow ) {
$the_arrow = pis_arrow();
} else if ( ( ! $the_more || $no_the_more ) && $exc_arrow ) {
$the_arrow = pis_arrow( false );
} else {
$the_arrow = '';
}
$output = '<span ' . pis_class( 'pis-more', apply_filters( 'pis_more_class', '' ), false ) . '>';
$output .= '<a href="' . get_permalink() . '" title="' . esc_attr__( 'Read the full post', 'posts-in-sidebar' ) . '" rel="bookmark">';
if ( $no_the_more ) {
$output .= pis_arrow( false );
} else {
$output .= $the_more . $the_arrow;
}
$output .= '</a>';
$output .= '</span>';
}
if ( ! isset( $output ) ) {
return '';
}
if ( $echo ) {
echo $output;
} else {
return $output;
}
}
/**
* Add the custom styles to wp_head hook.
*
* @since 1.13
* @return The HTML for custom styles in the HEAD section.
*/
function pis_add_styles_to_head() {
// Get the options from the database.
$custom_styles = (array) get_option( 'widget_pis_posts_in_sidebar' );
// Define $styles as an array.
$styles = array();
// Get all the values of "custom_styles" key into $styles.
foreach ( $custom_styles as $key => $value ) {
if ( isset( $value['custom_styles'] ) ) {
$styles[] = $value['custom_styles'];
}
}
// Remove any empty elements from the array
$styles = array_filter( $styles );
// Make the array as string.
$styles = implode( "\n", $styles );
// Print the output if it's not empty.
if ( $styles ) {
$output = "\n\n" . '<!-- Styles generated by Posts in Sidebar plugin -->' . "\n";
$output .= '<style type="text/css">' . "\n" . $styles . "\n" . '</style>';
$output .= "\n" . '<!-- / Styles generated by Posts in Sidebar plugin -->' . "\n\n";
echo $output;
}
}
add_action( 'wp_head', 'pis_add_styles_to_head' );
/**
* Add the utilities section: author, date of the post and comments.
*
* @since 1.18
* @param array The array of parameters.
* @return The HTML for the section.
* @uses pis_paragraph()
* @uses pis_class()
* @uses pis_get_comments_number()
*/
function pis_utility_section( $args ) {
$defaults = array(
'display_author' => false,
'display_date' => false,
'display_mod_date' => false,
'comments' => false,
'utility_margin' => NULL,
'margin_unit' => 'px',
'author_text' => __( 'By', 'posts-in-sidebar' ),
'linkify_author' => false,
'utility_sep' => '|',
'date_text' => __( 'Published on', 'posts-in-sidebar' ),
'linkify_date' => false,
'mod_date_text' => __( 'Modified on', 'posts-in-sidebar' ),
'linkify_mod_date' => false,
'comments_text' => __( 'Comments:', 'posts-in-sidebar' ),
'pis_post_id' => '',
'link_to_comments' => true,
'gravatar_display' => false,
'gravatar_position' => '',
'gravatar_author' => '',
'gravatar_size' => 32,
'gravatar_default' => '',
);
$args = wp_parse_args( $args, $defaults );
extract( $args, EXTR_SKIP );
$output = '';
if ( $display_author || $display_date || $display_mod_date || $comments ) {
$output .= '<p ' . pis_paragraph( $utility_margin, $margin_unit, 'pis-utility', 'pis_utility_class' ) . '>';
}
/* The Gravatar */
if ( $gravatar_display && 'next_author' == $gravatar_position ) {
$output .= pis_get_gravatar( array(
'author' => $gravatar_author,
'size' => $gravatar_size,
'default' => $gravatar_default,
) );
}
/* The author */
if ( $display_author ) {
$output .= '<span ' . pis_class( 'pis-author', apply_filters( 'pis_author_class', '' ), false ) . '>';
if ( $author_text ) $output .= $author_text . ' ';
if ( $linkify_author ) {
$author_title = sprintf( __( 'View all posts by %s', 'posts-in-sidebar' ), get_the_author() );
$author_link = get_author_posts_url( get_the_author_meta( 'ID' ) );
$output .= '<a ' . pis_class( 'pis-author-link', apply_filters( 'pis_author_link_class', '' ), false ) . ' href="' . $author_link . '" title="' . esc_attr( $author_title ) . '" rel="author">';
$output .= get_the_author();
$output .= '</a>';
} else {
$output .= get_the_author();
}
$output .= '</span>';
}
/* The date */
if ( $display_date ) {
if ( $display_author ) {
$output .= '<span ' . pis_class( 'pis-separator', apply_filters( 'pis_separator_class', '' ), false ) . '> ' . $utility_sep . ' </span>';
}
$output .= '<span ' . pis_class( 'pis-date', apply_filters( 'pis_date_class', '' ), false ) . '>';
if ( $date_text ) $output .= $date_text . ' ';
if ( $linkify_date ) {
$date_title = sprintf( __( 'Permalink to %s', 'posts-in-sidebar' ), the_title_attribute( 'echo=0' ) );
$output .= '<a ' . pis_class( 'pis-date-link', apply_filters( 'pis_date_link_class', '' ), false ) . ' href="' . get_permalink() . '" title="' . esc_attr( $date_title ) . '" rel="bookmark">';
$output .= get_the_date();
$output .= '</a>';
} else {
$output .= get_the_date();
}
$output .= '</span>';
}
/* The modification date */
if ( $display_mod_date ) {
/**
* The modification date is displayed under these two conditions:
* 1. if the creation date is not displayed OR
* 2. if the creation date is displayed AND the modification date is different from the creation date.
*/
if ( ( ! $display_date ) || ( $display_date && get_the_modified_date() != get_the_date() ) ) {
if ( $display_author || $display_date ) {
$output .= '<span ' . pis_class( 'pis-separator', apply_filters( 'pis_separator_class', '' ), false ) . '> ' . $utility_sep . ' </span>';
}
$output .= '<span ' . pis_class( 'pis-mod-date', apply_filters( 'pis_mod_date_class', '' ), false ) . '>';
if ( $mod_date_text ) $output .= $mod_date_text . ' ';
if ( $linkify_mod_date ) {
$mod_date_title = sprintf( __( 'Permalink to %s', 'posts-in-sidebar' ), the_title_attribute( 'echo=0' ) );
$output .= '<a ' . pis_class( 'pis-mod-date-link', apply_filters( 'pis_mod_date_link_class', '' ), false ) . ' href="' . get_permalink() . '" title="' . esc_attr( $mod_date_title ) . '" rel="bookmark">';
$output .= get_the_modified_date();
$output .= '</a>';
} else {
$output .= get_the_modified_date();
}
$output .= '</span>';
}
}
/* The comments */
if ( ! post_password_required() ) {
if ( $comments ) {
if ( $display_author || $display_date || $display_mod_date ) {
$output .= '<span ' . pis_class( 'pis-separator', apply_filters( 'pis_separator_class', '' ), false ) . '> ' . $utility_sep . ' </span>';
}
$output .= '<span ' . pis_class( 'pis-comments', apply_filters( 'pis_comments_class', '' ), false ) . '>';
if ( $comments_text ) $output .= $comments_text . ' ';
$output .= pis_get_comments_number( $pis_post_id, $link_to_comments );
$output .= '</span>';
}
}
if ( $display_author || $display_date || $display_mod_date || $comments ) {
$output .= '</p>';
}
return $output;
}
/**
* Add the thumbnail of the post.
*
* @since 1.18
* @param array The array of parameters.
* @return The HTML for the thumbnail.
*/
function pis_the_thumbnail( $args ) {
$defaults = array(
'display_image' => false,
'image_align' => 'no_change',
'side_image_margin' => NULL,
'bottom_image_margin' => NULL,
'margin_unit' => 'px',
'post_link' => '',
'pis_query' => '',
'image_size' => 'thumbnail',
'thumb_wrap' => false,
'custom_image_url' => '',
'custom_img_no_thumb' => true,
'post_type' => 'post',
'image_link' => '',
'image_link_to_post' => true,
);
$args = wp_parse_args( $args, $defaults );
extract( $args, EXTR_SKIP );
if ( $thumb_wrap ) {
$open_wrap = '<p class="pis-thumbnail">';
$close_wrap = '</p>';
} else {
$open_wrap = '';
$close_wrap = '';
}
switch ( $image_align ) {
case 'left' :
$image_class = ' alignleft';
$image_style = '';
if ( ! is_null( $side_image_margin ) || ! is_null( $bottom_image_margin ) ) {
$image_style = ' style="display: inline; float: left; margin-right: ' . $side_image_margin . $margin_unit . '; margin-bottom: ' . $bottom_image_margin . $margin_unit . ';"';
$image_style = str_replace( ' margin-right: px;', '', $image_style);
$image_style = str_replace( ' margin-bottom: px;', '', $image_style);
}
break;
case 'right':
$image_class = ' alignright';
$image_style = '';
if ( ! is_null( $side_image_margin ) || ! is_null( $bottom_image_margin ) ) {
$image_style = ' style="display: inline; float: right; margin-left: ' . $side_image_margin . $margin_unit . '; margin-bottom: ' . $bottom_image_margin . $margin_unit . ';"';
$image_style = str_replace( ' margin-left: px;', '', $image_style);
$image_style = str_replace( ' margin-bottom: px;', '', $image_style);
}
break;
case 'center':
$image_class = ' aligncenter';
$image_style = '';
if ( ! is_null( $bottom_image_margin ) )
$image_style = ' style="margin-bottom: ' . $bottom_image_margin . $margin_unit . ';"';
break;
default:
$image_class = '';
$image_style = '';
break;
}
$output = $open_wrap;
if ( $image_link_to_post ) {
// Figure out if a custom link for the featured image has been set.
if ( $image_link ) {
$the_image_link = $image_link;
} else {
$the_image_link = get_permalink();
}
$output .= '<a ' . pis_class( 'pis-thumbnail-link', apply_filters( 'pis_thumbnail_link_class', '' ), false ) . 'href="' . esc_url( strip_tags( $the_image_link ) ) . '" title="' . esc_attr( $post_link ) . '" rel="bookmark">';
}
/**
* If the post type is an attachment (an image, or any other attachment),
* the construct is different.
*
* @since 1.28
*/
if ( 'attachment' == $post_type ) {
$image_html = wp_get_attachment_image(
$pis_query->post->ID,
$image_size,
false,
array(
'class' => "attachment-$image_size pis-thumbnail-img" . ' ' . apply_filters( 'pis_thumbnail_class', '' ) . $image_class,
)
);
} else {
/**
* If the post has not a post-thumbnail AND a custom image URL is defined (in this case the custom image will be used only if the post has not a featured image)
* OR
* if custom image URL is defined AND the custom image should be used in every case (in this case the custom image will be used for all posts, even those who already have a featured image).
*/
if ( ( ! has_post_thumbnail() && $custom_image_url ) || ( $custom_image_url && ! $custom_img_no_thumb ) ) {
$image_html = '<img src="' . esc_url( $custom_image_url ) . '" alt="" class="pis-thumbnail-img' . ' ' . apply_filters( 'pis_thumbnail_class', '' ) . $image_class . '">';
} else {
$image_html = get_the_post_thumbnail(
$pis_query->post->ID,
$image_size,
array(
'class' => 'pis-thumbnail-img' . ' ' . apply_filters( 'pis_thumbnail_class', '' ) . $image_class,
)
);
}
}
$output .= str_replace( '<img', '<img' . $image_style, $image_html );
if ( $image_link_to_post ) {
$output .= '</a>';
}
$output .= $close_wrap;
return $output;
}
/**
* Add the text of the post in form of excerpt, full post, and so on.
*
* @since 1.18
* @param array $args The array containing the custom parameters.
* @return The HTML for the text of the post.
* @uses pis_break_text()
* @uses pis_more_arrow()
*/
function pis_the_text( $args ) {
$defaults = array(
'excerpt' => 'excerpt',
'pis_query' => '',
'exc_length' => 20,
'the_more' => __( 'Read more…', 'posts-in-sidebar' ),
'exc_arrow' => false,
);
$args = wp_parse_args( $args, $defaults );
extract( $args, EXTR_SKIP );
$output = '';
/*
"Full content" = the content of the post as displayed in the page.
"Rich content" = the content with inline images, titles and more (shortcodes will be executed).
"Content" = the full text of the content, whitout any ornament (shortcodes will be stripped).
"More excerpt" = the excerpt up to the point of the "more" tag (inserted by the user, shortcodes will be stripped).
"Excerpt" = the excerpt as defined by the user or generated by WordPress (shortcodes will be stripped).
"Only Read more" = no excerpt, only the Read more link
*/
switch ( $excerpt ) :
case 'full_content':
/* Filter the post content. If not filtered, shortcodes (and other things) will not be executed.
* See https://codex.wordpress.org/Function_Reference/get_the_content
*/
$output .= apply_filters( 'the_content', get_the_content() );
break;
case 'rich_content':
$content = $pis_query->post->post_content;
// Honor any paragraph break
$content = pis_break_text( $content );
$content = do_shortcode( $content );
$output .= apply_filters( 'pis_rich_content', $content );
break;
case 'content':
// Remove shortcodes
$content = strip_shortcodes( $pis_query->post->post_content );
// remove any HTML tag
$content = wp_kses( $content, array() );
// Honor any paragraph break
$content = pis_break_text( $content );
$output .= apply_filters( 'pis_content', $content );
break;
case 'more_excerpt':
$excerpt_text = strip_shortcodes( $pis_query->post->post_content );
$testformore = strpos( $excerpt_text, '<!--more-->' );
if ( $testformore ) {
$excerpt_text = substr( $excerpt_text, 0, $testformore );
} else {
$excerpt_text = wp_trim_words( $excerpt_text, $exc_length, '…' );
}
$output .= apply_filters( 'pis_more_excerpt_text', $excerpt_text ) . ' ' . pis_more_arrow( $the_more, false, $exc_arrow, false );
break;
case 'excerpt':
/**
* Check if the Relevanssi plugin is active and restore the user-defined excerpt in place of the Relevanssi-generated excerpt.
* @see https://wordpress.org/support/topic/issue-with-excerpts-when-using-relevanssi-search
*
* @since 1.26
*/
if ( function_exists( 'relevanssi_do_excerpt' ) && isset( $pis_query->post->original_excerpt ) ) {
$pis_query->post->post_excerpt = $pis_query->post->original_excerpt;
}
// If we have a user-defined excerpt...
if ( $pis_query->post->post_excerpt ) {
// Honor any paragraph break
$user_excerpt = pis_break_text( $pis_query->post->post_excerpt );
$output .= apply_filters( 'pis_user_excerpt', $user_excerpt ) . ' ' . pis_more_arrow( $the_more, false, $exc_arrow, false );
} else {
// ... else generate an excerpt
$excerpt_text = strip_shortcodes( $pis_query->post->post_content );
$no_the_more = false;
if ( count( explode( ' ', wp_strip_all_tags( $excerpt_text ) ) ) <= $exc_length ) $no_the_more = true;
$excerpt_text = wp_trim_words( $excerpt_text, $exc_length, '…' );
$output .= apply_filters( 'pis_excerpt_text', $excerpt_text ) . ' ' . pis_more_arrow( $the_more, $no_the_more, $exc_arrow, false );
}
break;
case 'only_read_more':
$excerpt_text = '';
$output .= apply_filters( 'pis_only_read_more', $excerpt_text ) . ' ' . pis_more_arrow( $the_more, false, $exc_arrow, false );
break;
endswitch;
// Close The text
return $output;
}
/**
* Return the custom taxonomies of the current post.
*
* @since 1.29
* @param array $args The array containing the custom parameters.
* @see https://codex.wordpress.org/Function_Reference/get_the_terms#Get_terms_for_all_custom_taxonomies
*/
function pis_custom_taxonomies_terms_links( $args ) {
$defaults = array(
'postID' => '',
'term_hashtag' => '',
'term_sep' => ',',
'terms_margin' => NULL,
'margin_unit' => 'px',
);
$args = wp_parse_args( $args, $defaults );
extract( $args, EXTR_SKIP );
// get post by post id
$post = get_post( $postID );
// get post type by post
$post_type = $post->post_type;
// get post type taxonomies
$taxonomies = get_object_taxonomies( $post_type, 'objects' );
$output = '';
foreach ( $taxonomies as $taxonomy_slug => $taxonomy ) {
// Exclude the standard WordPress 'category' and 'post_tag' taxonomies otherwise we'll have a duplicate in the front-end.
if ( 'category' != $taxonomy_slug && 'post_tag' != $taxonomy_slug ) {
// get the terms related to post
$list_of_terms = get_the_term_list( $postID, $taxonomy_slug, $term_hashtag, $term_sep . ' ' . $term_hashtag, '' );
if ( $list_of_terms ) {
$output .= '<p ' . pis_paragraph( $terms_margin, $margin_unit, 'pis-terms-links pis-' . $taxonomy_slug, 'pis_terms_class' ) . '>';
$output .= '<span class="pis-tax-name">' . $taxonomy->label . '</span>: ' . apply_filters( 'pis_terms_list', $list_of_terms );
$output .= '</p>';
}
}
}
return $output;
}
/**
* Build the query based on taxonomies.
*
* @param array $args The array containing the custom parameters.
* @return array An array of array of parameters.
* @since 1.29
*/
function pis_tax_query( $args ) {
$defaults = array (
'relation' => '',
'taxonomy_aa' => '',
'field_aa' => 'slug',
'terms_aa' => '',
'operator_aa' => 'IN',
'relation_a' => '',
'taxonomy_ab' => '',
'field_ab' => 'slug',
'terms_ab' => '',
'operator_ab' => 'IN',
'taxonomy_ba' => '',
'field_ba' => 'slug',
'terms_ba' => '',
'operator_ba' => 'IN',
'relation_b' => '',
'taxonomy_bb' => '',
'field_bb' => 'slug',
'terms_bb' => '',
'operator_bb' => 'IN',
);
$args = wp_parse_args( $args, $defaults );
extract( $args, EXTR_SKIP );
if ( '' == $taxonomy_aa && '' == $terms_aa ) {
$tax_query = '';
} else {
// Convert terms into arrays
$terms_aa = explode( ',', $terms_aa );
if ( $terms_ab ) $terms_ab = explode( ',', $terms_ab );
if ( $terms_ba ) $terms_ba = explode( ',', $terms_ba );
if ( $terms_bb ) $terms_bb = explode( ',', $terms_bb );
// Let's figure out the tax_query to build
if ( $taxonomy_aa && ! $taxonomy_ab && ! $taxonomy_ba && ! $taxonomy_bb ) {
$tax_query = array(
array(
'taxonomy' => $taxonomy_aa,
'field' => $field_aa,
'terms' => $terms_aa, // This must be an array
'operator' => $operator_aa,
)
);
} else if ( $taxonomy_aa && ! $taxonomy_ab && $taxonomy_ba && ! $taxonomy_bb && ! empty( $relation ) ) {
$tax_query = array(
'relation' => $relation,
array(
'taxonomy' => $taxonomy_aa,
'field' => $field_aa,
'terms' => $terms_aa, // This must be an array
'operator' => $operator_aa,
),
array(
'taxonomy' => $taxonomy_ba,
'field' => $field_ba,
'terms' => $terms_ba, // This must be an array
'operator' => $operator_ba,
)
);
} else if ( $taxonomy_aa && $taxonomy_ab && $taxonomy_ba && ! $taxonomy_bb && ! empty( $relation ) ) {
$tax_query = array(
'relation' => $relation,
array(
'relation_a' => $relation_a,
array (
'taxonomy' => $taxonomy_aa,
'field' => $field_aa,
'terms' => $terms_aa, // This must be an array
'operator' => $operator_aa,
),
array (
'taxonomy' => $taxonomy_ab,
'field' => $field_ab,
'terms' => $terms_ab, // This must be an array
'operator' => $operator_ab,
)
),
array(
'taxonomy' => $taxonomy_ba,
'field' => $field_ba,
'terms' => $terms_ba, // This must be an array
'operator' => $operator_ba,
)
);
} else if ( $taxonomy_aa && ! $taxonomy_ab && $taxonomy_ba && $taxonomy_bb && ! empty( $relation ) ) {
$tax_query = array(
'relation' => $relation,
array(
'taxonomy' => $taxonomy_aa,
'field' => $field_aa,
'terms' => $terms_aa, // This must be an array
'operator' => $operator_aa,
),
array(
'relation_b' => $relation_b,
array (
'taxonomy' => $taxonomy_ba,
'field' => $field_ba,
'terms' => $terms_ba, // This must be an array
'operator' => $operator_ba,
),
array (
'taxonomy' => $taxonomy_bb,
'field' => $field_bb,
'terms' => $terms_bb, // This must be an array
'operator' => $operator_bb,
)
)
);
} else if ( $taxonomy_aa && $taxonomy_ab && $taxonomy_ba && $taxonomy_bb && ! empty( $relation ) ) {
$tax_query = array(
'relation' => $relation,
array(
'relation_a' => $relation_a,
array (
'taxonomy' => $taxonomy_aa,
'field' => $field_aa,
'terms' => $terms_aa, // This must be an array
'operator' => $operator_aa,
),
array (
'taxonomy' => $taxonomy_ab,
'field' => $field_ab,
'terms' => $terms_ab, // This must be an array
'operator' => $operator_ab,
)
),
array(
'relation_b' => $relation_b,
array (
'taxonomy' => $taxonomy_ba,
'field' => $field_ba,
'terms' => $terms_ba, // This must be an array
'operator' => $operator_ba,
),
array (
'taxonomy' => $taxonomy_bb,
'field' => $field_bb,
'terms' => $terms_bb, // This must be an array
'operator' => $operator_bb,
)
)
);
}
}
if ( isset( $tax_query ) ) {
return $tax_query;
} else {
return '';
}
}
/**
* Remove empty keys from an array recursively.
*
* @param array $array The array to be checked.
* @param boolean $make_empty If the output is to return as an empty string.
* @since 1.29
* @see http://stackoverflow.com/questions/7696548/php-how-to-remove-empty-entries-of-an-array-recursively
*/
function pis_array_remove_empty_keys( $array, $make_empty = false ) {
foreach ( $array as $key => $value ) {
if ( is_array( $value ) ) {
$array[$key] = pis_array_remove_empty_keys( $array[$key] );
}
if ( empty( $array[$key] ) ) {
unset( $array[$key] );
}
}
if ( empty( $array ) && $make_empty ) {
$array = '';
}
return $array;
}
/**
* Return the debugging informations.
*
* @param array $parameters The array containing the custom parameters.
* @since 2.0.3
*/
function pis_debug( $parameters ) {
$defaults = array (
'debug_query' => false,
'debug_params' => false,
'debug_query_number' => false,
'params' => '',
'args' => '',
'cached' => false,
);
$parameters = wp_parse_args( $parameters, $defaults );
extract( $parameters, EXTR_SKIP );
$output = '';
if ( $debug_query || $debug_params || $debug_query_number ) {
$output .= '<h3>' . sprintf( __( '%s Debug', 'posts-in-sidebar' ), 'Posts in Sidebar' ) . '</h3>';
$output .= '<p>'; global $wp_version;
$output .= sprintf( __( 'Site URL: %s', 'posts-in-sidebar' ), site_url() . '<br>' );
$output .= sprintf( __( 'WP version: %s', 'posts-in-sidebar' ), $wp_version . '<br>' );
$output .= sprintf( __( 'PiS version: %s', 'posts-in-sidebar' ), PIS_VERSION . '<br>' );
if ( $cached ) $output .= __( 'Cache: active', 'posts-in-sidebar' ); else $output .= __( 'Cache: not active' );
$output .= '</p>';
}
if ( $debug_query ) {
$output .= '<p><strong>' . __( 'The parameters for the query:', 'posts-in-sidebar' ) . '</strong></p>';
$output .= '<pre>' . print_r( $params, true ) . '</pre>';
}
if ( $debug_params ) {
$output .= '<p><strong>' . __( 'The complete set of parameters of the widget:', 'posts-in-sidebar' ) . '</strong></p>';
$output .= '<pre>' . print_r( $args, true ) . '</pre>';
}
if ( $debug_query_number ) {
$output .= '<p><strong>' . __( 'The total number of queries so far:', 'posts-in-sidebar' ) . '</strong></p>';
$output .= '<pre>' . sprintf( __( '%1$s queries in %2$s seconds', 'posts-in-sidebar' ), get_num_queries(), timer_stop() ) . '</pre>';
}
return $output;
}
/**
* Return the version of Posts in Sidebar and if the cache is active.
*
* @param boolean $cached If the cache is active or not.
* @since 2.0.3
*/
function pis_generated( $cached ) {
/* Whether the cache is active */
if ( $cached ) {
$pis_cache_active = ' - Cache is active';
} else {
$pis_cache_active = '';
}
/* Output the credits and cache */
return '<!-- Generated by Posts in Sidebar v' . PIS_VERSION . $pis_cache_active . ' -->';
}
/**
* Returns the HTML for the comments link.
*
* @param integer $pis_post_id The ID of the post.
* @param boolean $link If the output is to be wrapped into a link to comments.
* @since 3.0
*/
function pis_get_comments_number( $pis_post_id, $link ) {
$num_comments = get_comments_number( $pis_post_id ); // get_comments_number returns only a numeric value
if ( 0 == $num_comments && ! comments_open( $pis_post_id ) ) {
$output = __( 'Comments are closed.', 'posts-in-sidebar' );
} else {
// Construct the comments string.
if ( 1 == $num_comments ) {
$comments = __( '1 Comment', 'posts-in-sidebar' );
} else if ( 1 < $num_comments ) {
$comments = sprintf( __( '%d Comments', 'posts-in-sidebar' ), $num_comments );
} else {
$comments = __( 'Leave a comment', 'posts-in-sidebar' );
}
// Contruct the HTML string for the comments.
if ( $link ) {
$output = '<a ' . pis_class( 'pis-comments-link', apply_filters( 'pis_comments_link_class', '' ), false ) . ' href="' . get_comments_link( $pis_post_id ) . '">' . $comments . '</a>';
} else {
$output = $comments;
}
}
return $output;
}
/**
* Returns the HTML string for the archive link.
*
* @param array $args The array containing the custom args.
* @since 3.0
*/
function pis_archive_link( $args ) {
$defaults = array(
'link_to' => 'category',
'tax_name' => '',
'tax_term_name' => '',
'archive_text' => __( 'Display all posts', 'posts-in-sidebar' ),
'archive_margin' => NULL,
'margin_unit' => 'px'
);
$args = wp_parse_args( $args, $defaults );
extract( $args, EXTR_SKIP );
switch ( $link_to ) {
case 'author':
$term_identity = get_user_by( 'slug', $tax_term_name );
if ( $term_identity ) {
$term_link = get_author_posts_url( $term_identity->ID, $tax_term_name );
$term_name = $term_identity->display_name;
}
break;
case 'category':
$term_identity = get_term_by( 'slug', $tax_term_name, 'category' );
if ( $term_identity ) {
$term_link = get_term_link( $term_identity->term_id, 'category' );
$term_name = $term_identity->name;
}
break;
case 'tag':
$term_identity = get_term_by( 'slug', $tax_term_name, 'post_tag' );
if ( $term_identity ) {
$term_link = get_term_link( $term_identity->term_id, 'post_tag' );
$term_name = $term_identity->name;
}
break;
case 'custom_post_type':
if ( post_type_exists( $tax_term_name ) ) {
$term_link = get_post_type_archive_link( $tax_term_name );
$post_type_object = get_post_type_object( $tax_term_name );
$term_name = $post_type_object->labels->name;
}
break;
case 'custom_taxonomy':
$term_identity = get_term_by( 'slug', $tax_term_name, $tax_name );
if ( $term_identity ) {
$term_link = get_term_link( $term_identity->term_id, $tax_name );
$term_name = $term_identity->name;
}
break;
default : // This is the case of post formats
$term_identity = get_term_by( 'slug', $link_to, 'post_format' );
if ( $term_identity ) {
$term_link = get_post_format_link( substr( $link_to, 12 ) );
$term_name = $term_identity->name;
}
break;
}
if ( isset( $term_link ) ) {
if ( strpos( $archive_text, '%s' ) ) {
$archive_text = str_replace( '%s', $term_name, $archive_text );
}
$output = '<p ' . pis_paragraph( $archive_margin, $margin_unit, 'pis-archive-link', 'pis_archive_class' ) . '>';
$output .= '<a ' . pis_class( 'pis-archive-link-class', apply_filters( 'pis_archive_link_class', '' ), false ) . ' href="' . esc_url( $term_link ) . '" title="' . esc_attr( $archive_text ) . '" rel="bookmark">';
$output .= $archive_text;
$output .= '</a>';
$output .= '</p>';
}
if ( isset( $output ) ) return $output; else return '';
}