template.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. if ( ! function_exists( 'cc_posted_on' ) ) :
  3. function cc_posted_on() {
  4. $time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
  5. if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
  6. $time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
  7. }
  8. $time_string = sprintf( $time_string,
  9. esc_attr( get_the_date( 'c' ) ),
  10. esc_html( get_the_date() ),
  11. esc_attr( get_the_modified_date( 'c' ) ),
  12. esc_html( get_the_modified_date() )
  13. );
  14. $posted_on = sprintf(
  15. esc_html_x( 'Posted on %s', 'post date', 'cc' ),
  16. '<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a>'
  17. );
  18. $byline = sprintf(
  19. esc_html_x( 'by %s', 'post author', 'cc' ),
  20. '<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author() ) . '</a></span>'
  21. );
  22. echo '<span class="posted-on">' . $posted_on . '</span><span class="byline"> ' . $byline . '</span>'; // WPCS: XSS OK.
  23. }
  24. endif;
  25. if ( ! function_exists( 'cc_entry_footer' ) ) :
  26. function cc_entry_footer() {
  27. // Hide category and tag text for pages.
  28. if ( 'post' === get_post_type() ) {
  29. /* translators: used between list items, there is a space after the comma */
  30. $categories_list = get_the_category_list( esc_html__( ', ', 'cc' ) );
  31. if ( $categories_list && cc_categorized_blog() ) {
  32. printf( '<span class="cat-links">' . esc_html__( 'Posted in %1$s', 'cc' ) . '</span>', $categories_list ); // WPCS: XSS OK.
  33. }
  34. /* translators: used between list items, there is a space after the comma */
  35. $tags_list = get_the_tag_list( '', esc_html__( ', ', 'cc' ) );
  36. if ( $tags_list ) {
  37. printf( '<span class="tags-links">' . esc_html__( 'Tagged %1$s', 'cc' ) . '</span>', $tags_list ); // WPCS: XSS OK.
  38. }
  39. }
  40. if ( ! is_single() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
  41. echo '<span class="comments-link">';
  42. comments_popup_link( esc_html__( 'Leave a comment', 'cc' ), esc_html__( '1 Comment', 'cc' ), esc_html__( '% Comments', 'cc' ) );
  43. echo '</span>';
  44. }
  45. edit_post_link(
  46. sprintf(
  47. /* translators: %s: Name of current post */
  48. esc_html__( 'Edit %s', 'cc' ),
  49. the_title( '<span class="screen-reader-text">"', '"</span>', false )
  50. ),
  51. '<span class="edit-link">',
  52. '</span>'
  53. );
  54. }
  55. endif;
  56. /**
  57. * Returns true if a blog has more than 1 category.
  58. *
  59. * @return bool
  60. */
  61. function cc_categorized_blog() {
  62. if ( false === ( $all_the_cool_cats = get_transient( 'cc_categories' ) ) ) {
  63. // Create an array of all the categories that are attached to posts.
  64. $all_the_cool_cats = get_categories( array(
  65. 'fields' => 'ids',
  66. 'hide_empty' => 1,
  67. // We only need to know if there is more than one category.
  68. 'number' => 2,
  69. ) );
  70. // Count the number of categories that are attached to the posts.
  71. $all_the_cool_cats = count( $all_the_cool_cats );
  72. set_transient( 'cc_categories', $all_the_cool_cats );
  73. }
  74. if ( $all_the_cool_cats > 1 ) {
  75. // This blog has more than 1 category so cc_categorized_blog should return true.
  76. return true;
  77. } else {
  78. // This blog has only 1 category so cc_categorized_blog should return false.
  79. return false;
  80. }
  81. }
  82. /**
  83. * Flush out the transients used in cc_categorized_blog.
  84. */
  85. function cc_category_transient_flusher() {
  86. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
  87. return;
  88. }
  89. // Like, beat it. Dig?
  90. delete_transient( 'cc_categories' );
  91. }
  92. add_action( 'edit_category', 'cc_category_transient_flusher' );
  93. add_action( 'save_post', 'cc_category_transient_flusher' );