PageRenderTime 37ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/template-tags.php

https://gitlab.com/aristath/mdl
PHP | 266 lines | 164 code | 27 blank | 75 comment | 31 complexity | a835dce016da7dcefe009525052f79cf MD5 | raw file
  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 MDL
  8. */
  9. if ( ! function_exists( 'the_posts_navigation' ) ) :
  10. /**
  11. * Display navigation to next/previous set of posts when applicable.
  12. *
  13. * @todo Remove this function when WordPress 4.3 is released.
  14. */
  15. function the_posts_navigation() {
  16. // Don't print empty markup if there's only one page.
  17. if ( $GLOBALS['wp_query']->max_num_pages < 2 ) {
  18. return;
  19. }
  20. ?>
  21. <nav class="navigation posts-navigation" role="navigation">
  22. <h2 class="screen-reader-text"><?php esc_html_e( 'Posts navigation', 'mdl' ); ?></h2>
  23. <div class="nav-links">
  24. <?php if ( get_next_posts_link() ) : ?>
  25. <div class="nav-previous"><?php next_posts_link( esc_html__( 'Older posts', 'mdl' ) ); ?></div>
  26. <?php endif; ?>
  27. <?php if ( get_previous_posts_link() ) : ?>
  28. <div class="nav-next"><?php previous_posts_link( esc_html__( 'Newer posts', 'mdl' ) ); ?></div>
  29. <?php endif; ?>
  30. </div><!-- .nav-links -->
  31. </nav><!-- .navigation -->
  32. <?php
  33. }
  34. endif;
  35. if ( ! function_exists( 'the_post_navigation' ) ) :
  36. /**
  37. * Display navigation to next/previous post when applicable.
  38. *
  39. * @todo Remove this function when WordPress 4.3 is released.
  40. */
  41. function the_post_navigation() {
  42. // Don't print empty markup if there's nowhere to navigate.
  43. $previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, '', true );
  44. $next = get_adjacent_post( false, '', false );
  45. if ( ! $next && ! $previous ) {
  46. return;
  47. }
  48. ?>
  49. <nav class="navigation post-navigation" role="navigation">
  50. <h2 class="screen-reader-text"><?php esc_html_e( 'Post navigation', 'mdl' ); ?></h2>
  51. <div class="nav-links">
  52. <?php
  53. previous_post_link( '<div class="nav-previous">%link</div>', '%title' );
  54. next_post_link( '<div class="nav-next">%link</div>', '%title' );
  55. ?>
  56. </div><!-- .nav-links -->
  57. </nav><!-- .navigation -->
  58. <?php
  59. }
  60. endif;
  61. if ( ! function_exists( 'mdl_posted_on' ) ) :
  62. /**
  63. * Prints HTML with meta information for the current post-date/time and author.
  64. */
  65. function mdl_posted_on() {
  66. $time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
  67. if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
  68. $time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
  69. }
  70. $time_string = sprintf( $time_string,
  71. esc_attr( get_the_date( 'c' ) ),
  72. esc_html( get_the_date() ),
  73. esc_attr( get_the_modified_date( 'c' ) ),
  74. esc_html( get_the_modified_date() )
  75. );
  76. $posted_on = sprintf(
  77. esc_html_x( 'Posted on %s', 'post date', 'mdl' ),
  78. '<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a>'
  79. );
  80. $byline = sprintf(
  81. esc_html_x( 'by %s', 'post author', 'mdl' ),
  82. '<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>'
  83. );
  84. echo '<span class="posted-on">' . $posted_on . '</span><span class="byline"> ' . $byline . '</span>'; // WPCS: XSS OK.
  85. }
  86. endif;
  87. if ( ! function_exists( 'mdl_entry_footer' ) ) :
  88. /**
  89. * Prints HTML with meta information for the categories, tags and comments.
  90. */
  91. function mdl_entry_footer() {
  92. // Hide category and tag text for pages.
  93. if ( 'post' == get_post_type() ) {
  94. /* translators: used between list items, there is a space after the comma */
  95. $categories_list = get_the_category_list( esc_html__( ', ', 'mdl' ) );
  96. if ( $categories_list && mdl_categorized_blog() ) {
  97. printf( '<span class="cat-links">' . esc_html__( 'Posted in %1$s', 'mdl' ) . '</span>', $categories_list ); // WPCS: XSS OK.
  98. }
  99. /* translators: used between list items, there is a space after the comma */
  100. $tags_list = get_the_tag_list( '', esc_html__( ', ', 'mdl' ) );
  101. if ( $tags_list ) {
  102. printf( '<span class="tags-links">' . esc_html__( 'Tagged %1$s', 'mdl' ) . '</span>', $tags_list ); // WPCS: XSS OK.
  103. }
  104. }
  105. if ( ! is_single() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
  106. echo '<span class="comments-link">';
  107. comments_popup_link( esc_html__( 'Leave a comment', 'mdl' ), esc_html__( '1 Comment', 'mdl' ), esc_html__( '% Comments', 'mdl' ) );
  108. echo '</span>';
  109. }
  110. edit_post_link( esc_html__( 'Edit', 'mdl' ), '<span class="edit-link">', '</span>' );
  111. }
  112. endif;
  113. if ( ! function_exists( 'the_archive_title' ) ) :
  114. /**
  115. * Shim for `the_archive_title()`.
  116. *
  117. * Display the archive title based on the queried object.
  118. *
  119. * @todo Remove this function when WordPress 4.3 is released.
  120. *
  121. * @param string $before Optional. Content to prepend to the title. Default empty.
  122. * @param string $after Optional. Content to append to the title. Default empty.
  123. */
  124. function the_archive_title( $before = '', $after = '' ) {
  125. if ( is_category() ) {
  126. $title = sprintf( esc_html__( 'Category: %s', 'mdl' ), single_cat_title( '', false ) );
  127. } elseif ( is_tag() ) {
  128. $title = sprintf( esc_html__( 'Tag: %s', 'mdl' ), single_tag_title( '', false ) );
  129. } elseif ( is_author() ) {
  130. $title = sprintf( esc_html__( 'Author: %s', 'mdl' ), '<span class="vcard">' . get_the_author() . '</span>' );
  131. } elseif ( is_year() ) {
  132. $title = sprintf( esc_html__( 'Year: %s', 'mdl' ), get_the_date( esc_html_x( 'Y', 'yearly archives date format', 'mdl' ) ) );
  133. } elseif ( is_month() ) {
  134. $title = sprintf( esc_html__( 'Month: %s', 'mdl' ), get_the_date( esc_html_x( 'F Y', 'monthly archives date format', 'mdl' ) ) );
  135. } elseif ( is_day() ) {
  136. $title = sprintf( esc_html__( 'Day: %s', 'mdl' ), get_the_date( esc_html_x( 'F j, Y', 'daily archives date format', 'mdl' ) ) );
  137. } elseif ( is_tax( 'post_format' ) ) {
  138. if ( is_tax( 'post_format', 'post-format-aside' ) ) {
  139. $title = esc_html_x( 'Asides', 'post format archive title', 'mdl' );
  140. } elseif ( is_tax( 'post_format', 'post-format-gallery' ) ) {
  141. $title = esc_html_x( 'Galleries', 'post format archive title', 'mdl' );
  142. } elseif ( is_tax( 'post_format', 'post-format-image' ) ) {
  143. $title = esc_html_x( 'Images', 'post format archive title', 'mdl' );
  144. } elseif ( is_tax( 'post_format', 'post-format-video' ) ) {
  145. $title = esc_html_x( 'Videos', 'post format archive title', 'mdl' );
  146. } elseif ( is_tax( 'post_format', 'post-format-quote' ) ) {
  147. $title = esc_html_x( 'Quotes', 'post format archive title', 'mdl' );
  148. } elseif ( is_tax( 'post_format', 'post-format-link' ) ) {
  149. $title = esc_html_x( 'Links', 'post format archive title', 'mdl' );
  150. } elseif ( is_tax( 'post_format', 'post-format-status' ) ) {
  151. $title = esc_html_x( 'Statuses', 'post format archive title', 'mdl' );
  152. } elseif ( is_tax( 'post_format', 'post-format-audio' ) ) {
  153. $title = esc_html_x( 'Audio', 'post format archive title', 'mdl' );
  154. } elseif ( is_tax( 'post_format', 'post-format-chat' ) ) {
  155. $title = esc_html_x( 'Chats', 'post format archive title', 'mdl' );
  156. }
  157. } elseif ( is_post_type_archive() ) {
  158. $title = sprintf( esc_html__( 'Archives: %s', 'mdl' ), post_type_archive_title( '', false ) );
  159. } elseif ( is_tax() ) {
  160. $tax = get_taxonomy( get_queried_object()->taxonomy );
  161. /* translators: 1: Taxonomy singular name, 2: Current taxonomy term */
  162. $title = sprintf( esc_html__( '%1$s: %2$s', 'mdl' ), $tax->labels->singular_name, single_term_title( '', false ) );
  163. } else {
  164. $title = esc_html__( 'Archives', 'mdl' );
  165. }
  166. /**
  167. * Filter the archive title.
  168. *
  169. * @param string $title Archive title to be displayed.
  170. */
  171. $title = apply_filters( 'get_the_archive_title', $title );
  172. if ( ! empty( $title ) ) {
  173. echo $before . $title . $after; // WPCS: XSS OK.
  174. }
  175. }
  176. endif;
  177. if ( ! function_exists( 'the_archive_description' ) ) :
  178. /**
  179. * Shim for `the_archive_description()`.
  180. *
  181. * Display category, tag, or term description.
  182. *
  183. * @todo Remove this function when WordPress 4.3 is released.
  184. *
  185. * @param string $before Optional. Content to prepend to the description. Default empty.
  186. * @param string $after Optional. Content to append to the description. Default empty.
  187. */
  188. function the_archive_description( $before = '', $after = '' ) {
  189. $description = apply_filters( 'get_the_archive_description', term_description() );
  190. if ( ! empty( $description ) ) {
  191. /**
  192. * Filter the archive description.
  193. *
  194. * @see term_description()
  195. *
  196. * @param string $description Archive description to be displayed.
  197. */
  198. echo $before . $description . $after; // WPCS: XSS OK.
  199. }
  200. }
  201. endif;
  202. /**
  203. * Returns true if a blog has more than 1 category.
  204. *
  205. * @return bool
  206. */
  207. function mdl_categorized_blog() {
  208. if ( false === ( $all_the_cool_cats = get_transient( 'mdl_categories' ) ) ) {
  209. // Create an array of all the categories that are attached to posts.
  210. $all_the_cool_cats = get_categories( array(
  211. 'fields' => 'ids',
  212. 'hide_empty' => 1,
  213. // We only need to know if there is more than one category.
  214. 'number' => 2,
  215. ) );
  216. // Count the number of categories that are attached to the posts.
  217. $all_the_cool_cats = count( $all_the_cool_cats );
  218. set_transient( 'mdl_categories', $all_the_cool_cats );
  219. }
  220. if ( $all_the_cool_cats > 1 ) {
  221. // This blog has more than 1 category so mdl_categorized_blog should return true.
  222. return true;
  223. } else {
  224. // This blog has only 1 category so mdl_categorized_blog should return false.
  225. return false;
  226. }
  227. }
  228. /**
  229. * Flush out the transients used in mdl_categorized_blog.
  230. */
  231. function mdl_category_transient_flusher() {
  232. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
  233. return;
  234. }
  235. // Like, beat it. Dig?
  236. delete_transient( 'mdl_categories' );
  237. }
  238. add_action( 'edit_category', 'mdl_category_transient_flusher' );
  239. add_action( 'save_post', 'mdl_category_transient_flusher' );