PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/inc/template-tags.php

https://github.com/GergelyToth/_s
PHP | 228 lines | 148 code | 37 blank | 43 comment | 35 complexity | 2f594b7c928595b5e0b956c15cfe964f 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 _s
  8. */
  9. if ( ! function_exists( '_s_content_nav' ) ) :
  10. /**
  11. * Display navigation to next/previous pages when applicable
  12. */
  13. function _s_content_nav( $nav_id ) {
  14. global $wp_query, $post;
  15. // Don't print empty markup on single pages if there's nowhere to navigate.
  16. if ( is_single() ) {
  17. $previous = ( is_attachment() ) ? get_post( $post->post_parent ) : get_adjacent_post( false, '', true );
  18. $next = get_adjacent_post( false, '', false );
  19. if ( ! $next && ! $previous )
  20. return;
  21. }
  22. // Don't print empty markup in archives if there's only one page.
  23. if ( $wp_query->max_num_pages < 2 && ( is_home() || is_archive() || is_search() ) )
  24. return;
  25. $nav_class = ( is_single() ) ? 'post-navigation' : 'paging-navigation';
  26. ?>
  27. <nav role="navigation" id="<?php echo esc_attr( $nav_id ); ?>" class="<?php echo $nav_class; ?>">
  28. <h1 class="screen-reader-text"><?php _e( 'Post navigation', '_s' ); ?></h1>
  29. <?php if ( is_single() ) : // navigation links for single posts ?>
  30. <?php previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', '_s' ) . '</span> %title' ); ?>
  31. <?php next_post_link( '<div class="nav-next">%link</div>', '%title <span class="meta-nav">' . _x( '&rarr;', 'Next post link', '_s' ) . '</span>' ); ?>
  32. <?php elseif ( $wp_query->max_num_pages > 1 && ( is_home() || is_archive() || is_search() ) ) : // navigation links for home, archive, and search pages ?>
  33. <?php if ( get_next_posts_link() ) : ?>
  34. <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', '_s' ) ); ?></div>
  35. <?php endif; ?>
  36. <?php if ( get_previous_posts_link() ) : ?>
  37. <div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', '_s' ) ); ?></div>
  38. <?php endif; ?>
  39. <?php endif; ?>
  40. </nav><!-- #<?php echo esc_html( $nav_id ); ?> -->
  41. <?php
  42. }
  43. endif; // _s_content_nav
  44. if ( ! function_exists( '_s_comment' ) ) :
  45. /**
  46. * Template for comments and pingbacks.
  47. *
  48. * Used as a callback by wp_list_comments() for displaying the comments.
  49. */
  50. function _s_comment( $comment, $args, $depth ) {
  51. $GLOBALS['comment'] = $comment;
  52. if ( 'pingback' == $comment->comment_type || 'trackback' == $comment->comment_type ) : ?>
  53. <li id="comment-<?php comment_ID(); ?>" <?php comment_class(); ?>>
  54. <div class="comment-body">
  55. <?php _e( 'Pingback:', '_s' ); ?> <?php comment_author_link(); ?> <?php edit_comment_link( __( 'Edit', '_s' ), '<span class="edit-link">', '</span>' ); ?>
  56. </div>
  57. <?php else : ?>
  58. <li id="comment-<?php comment_ID(); ?>" <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?>>
  59. <article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
  60. <footer class="comment-meta">
  61. <div class="comment-author vcard">
  62. <?php if ( 0 != $args['avatar_size'] ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
  63. <?php printf( __( '%s <span class="says">says:</span>', '_s' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?>
  64. </div><!-- .comment-author -->
  65. <div class="comment-metadata">
  66. <a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>">
  67. <time datetime="<?php comment_time( 'c' ); ?>">
  68. <?php printf( _x( '%1$s at %2$s', '1: date, 2: time', '_s' ), get_comment_date(), get_comment_time() ); ?>
  69. </time>
  70. </a>
  71. <?php edit_comment_link( __( 'Edit', '_s' ), '<span class="edit-link">', '</span>' ); ?>
  72. </div><!-- .comment-metadata -->
  73. <?php if ( '0' == $comment->comment_approved ) : ?>
  74. <p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', '_s' ); ?></p>
  75. <?php endif; ?>
  76. </footer><!-- .comment-meta -->
  77. <div class="comment-content">
  78. <?php comment_text(); ?>
  79. </div><!-- .comment-content -->
  80. <div class="reply">
  81. <?php comment_reply_link( array_merge( $args, array( 'add_below' => 'div-comment', 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
  82. </div><!-- .reply -->
  83. </article><!-- .comment-body -->
  84. <?php
  85. endif;
  86. }
  87. endif; // ends check for _s_comment()
  88. if ( ! function_exists( '_s_the_attached_image' ) ) :
  89. /**
  90. * Prints the attached image with a link to the next attached image.
  91. */
  92. function _s_the_attached_image() {
  93. $post = get_post();
  94. $attachment_size = apply_filters( '_s_attachment_size', array( 1200, 1200 ) );
  95. $next_attachment_url = wp_get_attachment_url();
  96. /**
  97. * Grab the IDs of all the image attachments in a gallery so we can get the
  98. * URL of the next adjacent image in a gallery, or the first image (if
  99. * we're looking at the last image in a gallery), or, in a gallery of one,
  100. * just the link to that image file.
  101. */
  102. $attachment_ids = get_posts( array(
  103. 'post_parent' => $post->post_parent,
  104. 'fields' => 'ids',
  105. 'numberposts' => -1,
  106. 'post_status' => 'inherit',
  107. 'post_type' => 'attachment',
  108. 'post_mime_type' => 'image',
  109. 'order' => 'ASC',
  110. 'orderby' => 'menu_order ID'
  111. ) );
  112. // If there is more than 1 attachment in a gallery...
  113. if ( count( $attachment_ids ) > 1 ) {
  114. foreach ( $attachment_ids as $attachment_id ) {
  115. if ( $attachment_id == $post->ID ) {
  116. $next_id = current( $attachment_ids );
  117. break;
  118. }
  119. }
  120. // get the URL of the next image attachment...
  121. if ( $next_id )
  122. $next_attachment_url = get_attachment_link( $next_id );
  123. // or get the URL of the first image attachment.
  124. else
  125. $next_attachment_url = get_attachment_link( array_shift( $attachment_ids ) );
  126. }
  127. printf( '<a href="%1$s" title="%2$s" rel="attachment">%3$s</a>',
  128. esc_url( $next_attachment_url ),
  129. the_title_attribute( array( 'echo' => false ) ),
  130. wp_get_attachment_image( $post->ID, $attachment_size )
  131. );
  132. }
  133. endif;
  134. if ( ! function_exists( '_s_posted_on' ) ) :
  135. /**
  136. * Prints HTML with meta information for the current post-date/time and author.
  137. */
  138. function _s_posted_on() {
  139. $time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time>';
  140. if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) )
  141. $time_string .= '<time class="updated" datetime="%3$s">%4$s</time>';
  142. $time_string = sprintf( $time_string,
  143. esc_attr( get_the_date( 'c' ) ),
  144. esc_html( get_the_date() ),
  145. esc_attr( get_the_modified_date( 'c' ) ),
  146. esc_html( get_the_modified_date() )
  147. );
  148. printf( __( '<span class="posted-on">Posted on %1$s</span><span class="byline"> by %2$s</span>', '_s' ),
  149. sprintf( '<a href="%1$s" title="%2$s" rel="bookmark">%3$s</a>',
  150. esc_url( get_permalink() ),
  151. esc_attr( get_the_time() ),
  152. $time_string
  153. ),
  154. sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s">%3$s</a></span>',
  155. esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
  156. esc_attr( sprintf( __( 'View all posts by %s', '_s' ), get_the_author() ) ),
  157. esc_html( get_the_author() )
  158. )
  159. );
  160. }
  161. endif;
  162. /**
  163. * Returns true if a blog has more than 1 category
  164. */
  165. function _s_categorized_blog() {
  166. if ( false === ( $all_the_cool_cats = get_transient( 'all_the_cool_cats' ) ) ) {
  167. // Create an array of all the categories that are attached to posts
  168. $all_the_cool_cats = get_categories( array(
  169. 'hide_empty' => 1,
  170. ) );
  171. // Count the number of categories that are attached to the posts
  172. $all_the_cool_cats = count( $all_the_cool_cats );
  173. set_transient( 'all_the_cool_cats', $all_the_cool_cats );
  174. }
  175. if ( '1' != $all_the_cool_cats ) {
  176. // This blog has more than 1 category so _s_categorized_blog should return true
  177. return true;
  178. } else {
  179. // This blog has only 1 category so _s_categorized_blog should return false
  180. return false;
  181. }
  182. }
  183. /**
  184. * Flush out the transients used in _s_categorized_blog
  185. */
  186. function _s_category_transient_flusher() {
  187. // Like, beat it. Dig?
  188. delete_transient( 'all_the_cool_cats' );
  189. }
  190. add_action( 'edit_category', '_s_category_transient_flusher' );
  191. add_action( 'save_post', '_s_category_transient_flusher' );