template-tags.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Custom template tags for this theme.
  4. *
  5. * Eventually, some of the functionality here could be replaced by core features.
  6. *
  7. * @package Akina
  8. */
  9. if ( ! function_exists( 'akina_posted_on' ) ) :
  10. /**
  11. * Prints HTML with meta information for the current post-date/time and author.
  12. */
  13. function akina_posted_on() {
  14. $time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
  15. if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
  16. $time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
  17. }
  18. $time_string = sprintf( $time_string,
  19. esc_attr( get_the_date( 'c' ) ),
  20. esc_html( get_the_date() ),
  21. esc_attr( get_the_modified_date( 'c' ) ),
  22. esc_html( get_the_modified_date() )
  23. );
  24. $posted_on = sprintf(
  25. _x( 'Posted on %s', 'post date', 'sakurairo' ),
  26. '<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a>'
  27. );
  28. $byline = sprintf(
  29. _x( 'by %s', 'post author', 'sakurairo' ),
  30. '<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>'
  31. );
  32. echo '<span class="posted-on">' . $posted_on . '</span><span class="byline"> ' . $byline . '</span>'; // WPCS: XSS OK.
  33. }
  34. endif;
  35. if ( ! function_exists( 'akina_entry_footer' ) ) :
  36. /**
  37. * Prints HTML with meta information for the categories, tags and comments.
  38. */
  39. function akina_entry_footer() {
  40. // Hide category and tag text for pages.
  41. if ( 'post' === get_post_type() ) {
  42. /* translators: used between list items, there is a space after the comma */
  43. $categories_list = get_the_category_list( __( ', ', 'sakurairo' ) );
  44. if ( $categories_list && akina_categorized_blog() ) {
  45. printf( '<span class="cat-links">' . __( 'Posted in %1$s', 'sakurairo' ) . '</span>', $categories_list ); // WPCS: XSS OK.
  46. }
  47. /* translators: used between list items, there is a space after the comma */
  48. $tags_list = get_the_tag_list( '', __( ', ', 'sakurairo' ) );
  49. if ( $tags_list ) {
  50. printf( '<span class="tags-links">' . __( 'Tagged %1$s', 'sakurairo' ) . '</span>', $tags_list ); // WPCS: XSS OK.
  51. }
  52. }
  53. if ( ! is_single() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
  54. echo '<span class="comments-link">';
  55. /* translators: %s: post title */
  56. comments_popup_link( sprintf( wp_kses( __( 'Leave a Comment<span class="screen-reader-text"> on %s</span>', 'sakurairo' ), array( 'span' => array( 'class' => array() ) ) ), get_the_title() ) );
  57. echo '</span>';
  58. }
  59. edit_post_link(
  60. sprintf(
  61. /* translators: %s: Name of current post */
  62. __( 'Edit %s', 'sakurairo' ),
  63. the_title( '<span class="screen-reader-text">"', '"</span>', false )
  64. ),
  65. '<span class="edit-link">',
  66. '</span>'
  67. );
  68. }
  69. endif;
  70. /**
  71. * Returns true if a blog has more than 1 category.
  72. *
  73. * @return bool
  74. */
  75. function akina_categorized_blog() {
  76. if ( false === ( $all_the_cool_cats = get_transient( 'akina_categories' ) ) ) {
  77. // Create an array of all the categories that are attached to posts.
  78. $all_the_cool_cats = get_categories( array(
  79. 'fields' => 'ids',
  80. 'hide_empty' => 1,
  81. // We only need to know if there is more than one category.
  82. 'number' => 2,
  83. ) );
  84. // Count the number of categories that are attached to the posts.
  85. $all_the_cool_cats = count( $all_the_cool_cats );
  86. set_transient( 'akina_categories', $all_the_cool_cats );
  87. }
  88. if ( $all_the_cool_cats > 1 ) {
  89. // This blog has more than 1 category so akina_categorized_blog should return true.
  90. return true;
  91. } else {
  92. // This blog has only 1 category so akina_categorized_blog should return false.
  93. return false;
  94. }
  95. }
  96. /**
  97. * Flush out the transients used in akina_categorized_blog.
  98. */
  99. function akina_category_transient_flusher() {
  100. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
  101. return;
  102. }
  103. // Like, beat it. Dig?
  104. delete_transient( 'akina_categories' );
  105. }
  106. add_action( 'edit_category', 'akina_category_transient_flusher' );
  107. add_action( 'save_post', 'akina_category_transient_flusher' );