extras.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * Custom functions that act independently of the theme templates.
  4. *
  5. * Eventually, some of the functionality here could be replaced by core features.
  6. *
  7. * @package AltoFocus
  8. */
  9. /**
  10. * Adds custom classes to the array of body classes.
  11. *
  12. * @param array $classes Classes for the body element.
  13. * @return array
  14. */
  15. function altofocus_body_classes( $classes ) {
  16. // Add class if no custom header or featured images
  17. $header_image = get_header_image();
  18. // Header text display class, determines size of logo
  19. $header_text_display = get_theme_mod( 'header_textcolor' );
  20. if ( $header_text_display == 'blank' ) {
  21. $classes[] = 'hide-site-title-description';
  22. }
  23. // Add class if footer image has been added
  24. $media_override = get_theme_mod( 'altofocus_media_override' );
  25. if ( $media_override == 'override' && is_singular() ) {
  26. $classes[] = 'media-override';
  27. }
  28. // Adds a class of group-blog to blogs with more than 1 published author.
  29. if ( is_multi_author() ) {
  30. $classes[] = 'group-blog';
  31. }
  32. // Adds a class of hfeed to non-singular pages.
  33. if ( ! is_singular() ) {
  34. $classes[] = 'hfeed';
  35. }
  36. // Add a class of no-sidebar when there is no sidebar present
  37. if ( ! is_active_sidebar( 'sidebar-1' ) ) {
  38. $classes[] = 'no-sidebar';
  39. }
  40. return $classes;
  41. }
  42. add_filter( 'body_class', 'altofocus_body_classes' );
  43. /**
  44. * Adds custom classes to the array of post classes.
  45. *
  46. * @param array $classes Classes for the article element.
  47. * @return array
  48. */
  49. function altofocus_post_classes( $classes ) {
  50. global $post, $content_width;
  51. // Use 1/2 of the $content_width variable as the threshold for images that are too small
  52. $image_size_threshhold = $content_width / 2;
  53. if ( ! is_singular() ) {
  54. $classes[] = 'grid-item';
  55. // Get image meta
  56. $image_meta = wp_get_attachment_metadata( get_post_thumbnail_id( $post->ID ) );
  57. // Set orientation and image constraint classes
  58. if ( isset( $image_meta['width'], $image_meta['height'] ) ) {
  59. // Use small classes when original image size is too small
  60. if ( $image_meta['width'] <= $image_size_threshhold || $image_meta['height'] <= $image_size_threshhold ) {
  61. $classes[] = 'grid-item-small';
  62. }
  63. // Use small class to make the grid-item images size conform to post-title size instead of image size
  64. if ( ( $image_meta['width'] > $image_meta['height'] ) &&
  65. ( ( $image_meta['width'] / $image_meta['height'] ) <= 1 ) ) {
  66. $classes[] = 'grid-item-small';
  67. }
  68. // Landscape images
  69. if ( $image_meta['width'] >= $image_meta['height'] ) {
  70. $classes[] = 'grid-item-landscape';
  71. // Portrait images
  72. } else {
  73. $classes[] = 'grid-item-portrait';
  74. }
  75. }
  76. // Get featured content settings and options
  77. $featured_options = get_option( 'featured-content' );
  78. $featured_tag_name = isset( $featured_options['tag-name'] ) ? $featured_options['tag-name'] : '';
  79. if ( ! empty( $featured_tag_name ) && has_tag( $featured_tag_name, $post->ID ) ) {
  80. $classes[] = 'grid-item-featured';
  81. }
  82. }
  83. return $classes;
  84. }
  85. add_filter( 'post_class', 'altofocus_post_classes' );
  86. /**
  87. * Add a pingback url auto-discovery header for singularly identifiable articles.
  88. */
  89. function altofocus_pingback_header() {
  90. if ( is_singular() && pings_open() ) {
  91. echo '<link rel="pingback" href="', esc_url( get_bloginfo( 'pingback_url' ) ), '">';
  92. }
  93. }
  94. add_action( 'wp_head', 'altofocus_pingback_header' );
  95. /**
  96. * Slideshow Gallery Filter
  97. *
  98. * Replaces the default gallery when type=slideshow is used
  99. * See: componenets/post/content-single.php
  100. * Source: https://wordpress.stackexchange.com/a/64022
  101. */
  102. function altofocus_slideshow_gallery_filter( $output, $attr ) {
  103. global $post;
  104. static $count = 0;
  105. if ( $count > 0 ) {
  106. return $output; // Ensure the gallery output is replaced once.
  107. }
  108. static $instance = 0;
  109. $instance++;
  110. // We're trusting author input, so let's at least make sure it looks like a valid orderby statement
  111. if ( isset( $attr['orderby'] ) ) {
  112. $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
  113. if ( !$attr['orderby'] ) {
  114. unset( $attr['orderby'] );
  115. }
  116. }
  117. extract(shortcode_atts(array(
  118. 'order' => 'ASC',
  119. 'orderby' => 'menu_order ID',
  120. 'id' => $post->ID,
  121. 'itemtag' => 'li',
  122. 'icontag' => null,
  123. 'captiontag' => 'p',
  124. 'columns' => 3,
  125. 'size' => 'large',
  126. 'include' => '',
  127. 'exclude' => ''
  128. ), $attr));
  129. $id = intval($id);
  130. if ( 'RAND' == $order ) {
  131. $orderby = 'none';
  132. }
  133. if ( ! empty( $include ) ) {
  134. $include = preg_replace( '/[^0-9,]+/', '', $include );
  135. $_attachments = get_posts( array( 'include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby ) );
  136. $attachments = array();
  137. foreach ( $_attachments as $key => $val ) {
  138. $attachments[$val->ID] = $_attachments[$key];
  139. }
  140. } elseif ( !empty($exclude) ) {
  141. $exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
  142. $attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  143. } else {
  144. $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  145. }
  146. if ( empty( $attachments ) ) {
  147. return '';
  148. }
  149. // The gallery output
  150. $selector = "gallery-{$instance}";
  151. $size_class = sanitize_html_class( $size );
  152. $output = "<section id=\"gallery-{$id}\" class=\"entry-gallery\"><div id=\"$selector\" class=\"flexslider slider slider-size-{$size_class}\"><ul class=\"slides\">";
  153. foreach ( $attachments as $att_id => $attachment ) {
  154. // Get image, meta & caption
  155. $image_link = wp_get_attachment_image( $att_id, $size );
  156. $image_meta = wp_get_attachment_metadata( $att_id );
  157. $image_caption = $attachment->post_excerpt;
  158. // Set orientation
  159. $orientation = '';
  160. if ( isset( $image_meta['height'], $image_meta['width'] ) )
  161. $orientation = ( $image_meta['height'] > $image_meta['width'] ) ? 'portrait' : 'landscape';
  162. // Image markup
  163. $output .= "<{$itemtag} class=\"slide {$orientation}\">";
  164. $output .= $image_link;
  165. // Add caption if it exists
  166. if ( ! empty( $image_caption ) )
  167. $output .= '<span class="image-caption">' . $image_caption . '</span>';
  168. $output .= "</{$itemtag}>";
  169. }
  170. $output .= '</section>';
  171. $count++; // Update counter after successful replacement.
  172. return $output;
  173. }