extras.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 Pique
  8. */
  9. /**
  10. * Checks to see if we're on the homepage or not.
  11. * We reuse this check in a few places, so this simplifies the process a bit.
  12. */
  13. function pique_is_frontpage() {
  14. if ( is_front_page() && ! is_home() ) :
  15. return true;
  16. else :
  17. return false;
  18. endif;
  19. }
  20. /*
  21. * Count our number of active panels
  22. * Primarily used to see if we have any panels active, duh.
  23. */
  24. function pique_panel_count() {
  25. $panels = array( '1', '2', '3', '4', '5', '6', '7', '8' );
  26. $panel_count = 0;
  27. foreach ( $panels as $panel ) :
  28. if ( get_theme_mod( 'pique_panel' . $panel ) ) :
  29. $panel_count++;
  30. endif;
  31. endforeach;
  32. return $panel_count;
  33. }
  34. /**
  35. * Adds custom classes to the array of body classes.
  36. *
  37. * @param array $classes Classes for the body element.
  38. * @return array
  39. */
  40. function pique_body_classes( $classes ) {
  41. // Adds a class of group-blog to blogs with more than 1 published author.
  42. if ( is_multi_author() ) :
  43. $classes[] = 'group-blog';
  44. endif;
  45. // Adds a body class if we're on the (static) front page
  46. if ( pique_is_frontpage() ) :
  47. $classes[] = 'pique-frontpage';
  48. endif;
  49. // Adds a class if we're on the static front page, and there's only a single panel
  50. if ( pique_is_frontpage() && 0 === pique_panel_count() ) :
  51. $classes[] = 'pique-no-panels';
  52. endif;
  53. // Adds a class if we're in the Customizer, since it doesn't like fixed backgrounds
  54. if ( is_customize_preview() ) :
  55. $classes[] = 'pique-customizer';
  56. endif;
  57. // Adds a class if we don't have a sidebar at all, so we can adjust the layout
  58. if ( is_active_sidebar( 'sidebar-1' ) ) :
  59. $classes[] = 'pique-sidebar';
  60. endif;
  61. // Adds a class on single CPTs.
  62. if ( is_singular() ) {
  63. $classes[] = 'pique-singular';
  64. }
  65. return $classes;
  66. }
  67. add_filter( 'body_class', 'pique_body_classes' );
  68. /**
  69. * Adds custom classes to the array of post classes.
  70. *
  71. * @param array $classes Classes for the post element.
  72. * @return array
  73. */
  74. function pique_post_classes( $classes ) {
  75. // Adds the page template slug, if we're using one, to the array of classes
  76. // We're stripping the prefixed folder name and the .php suffix for prettier CSS
  77. if ( get_page_template_slug() && '' !== get_page_template_slug() ) :
  78. $simple_slug = get_page_template_slug();
  79. // Make sure the template actually exists in this theme.
  80. if ( '' !== locate_template( $simple_slug ) ) :
  81. $simple_slug = explode( '.', $simple_slug );
  82. $simple_slug = $simple_slug[0];
  83. $simple_slug = explode( '/', $simple_slug );
  84. $simple_slug = $simple_slug[1];
  85. $classes[] = 'pique-' . $simple_slug;
  86. endif;
  87. endif;
  88. // Add a page template slug to the main posts page
  89. if ( get_the_ID() === (int) get_option( 'page_for_posts' ) ) :
  90. $classes[] = 'pique-template-recent-posts';
  91. endif;
  92. // Add the panel classes to all posts in archive views
  93. if ( is_home() || is_search() || is_archive() ) :
  94. $classes[] = 'pique-panel';
  95. endif;
  96. return $classes;
  97. }
  98. add_filter( 'post_class', 'pique_post_classes' );
  99. /*
  100. * Remove an element from an array.
  101. * Used in our post_class filtering machinations above.
  102. */
  103. function pique_remove_from_array( $item, $array ) {
  104. $index = array_search( $item, $array );
  105. if ( false !== $index ) {
  106. unset( $array[ $index ] );
  107. }
  108. return $array;
  109. }
  110. /**
  111. * We want to do some fancy-pants stuff with our navbar, so we'll need to
  112. * create a custom walker. This basically means that, if we're on the homepage,
  113. * and the links in the menu are to panels on that page, the link will direct to
  114. * that panel using a hash link, rather than directing to the new page itself.
  115. */
  116. class Pique_Menu extends Walker_Nav_Menu {
  117. var $current_menu = null;
  118. var $break_point = null;
  119. var $id_to_split_on = null;
  120. var $top_level_count = null;
  121. function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
  122. global $wp_query;
  123. global $post;
  124. // Get the locations of nav menus
  125. $theme_locations = get_nav_menu_locations();
  126. if ( is_object( $args ) ) :
  127. // Get the menu object of the current nav menu based on the returned theme location
  128. $menu_obj = get_term( $theme_locations[ $args->theme_location ], 'nav_menu' );
  129. endif;
  130. $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
  131. $class_names = $value = '';
  132. $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  133. $classes[] = 'menu-item-' . $item->ID;
  134. $class_names = implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
  135. $class_names = ' class="' . esc_attr( $class_names ) . '"';
  136. $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
  137. $id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
  138. $output .= $indent . '<li' . $id . $value . $class_names . '>';
  139. // Get the IDs for our target page (the page we're linking to), the parent of our target, and current page
  140. $target_page = $item->object_id;
  141. if ( get_post( $target_page ) ) :
  142. $target_parent = get_post( $target_page )->post_parent;
  143. else :
  144. $target_parent = 0;
  145. endif;
  146. if ( $post ) :
  147. $current_page = $post->ID;
  148. else :
  149. $current_page = null;
  150. endif;
  151. // Check to see if our target page's parent page is the front page. If so, we'll want to use a hash link.
  152. if ( 0 !== $target_parent && 'page-templates/template-front.php' === get_page_template_slug( $target_parent ) ) :
  153. if ( $current_page === $target_parent ) :
  154. $item->url = '#post-' . $target_page;
  155. else :
  156. $item->url = get_page_link( $target_parent ) . '#post-' . $target_page;
  157. endif;
  158. endif;
  159. $attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
  160. $attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
  161. $attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
  162. $attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
  163. if ( is_object( $args ) ) :
  164. $item_output = $args->before;
  165. $item_output .= '<a'. $attributes .'>';
  166. $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
  167. $item_output .= '</a>';
  168. $item_output .= $args->after;
  169. $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  170. endif;
  171. }
  172. }
  173. /*
  174. * Add an extra li to our nav for our priority+ navigation to use
  175. */
  176. function add_more_to_nav( $items, $args ) {
  177. if ( 'primary' === $args->theme_location ) :
  178. $items .= '<li id="more-menu" class="menu-item menu-item-has-children"><a href="#"><span class="screen-reader-text">More</span></a><ul class="sub-menu"></ul></li>';
  179. endif;
  180. return $items;
  181. }
  182. add_filter( 'wp_nav_menu_items', 'add_more_to_nav', 10, 2 );
  183. /*
  184. * Let's customize our excerpt a bit, so it looks better
  185. * First we decrease the default excerpt length, then
  186. * we give it a proper hellip for the more text.
  187. */
  188. function pique_custom_excerpt_length( $length ) {
  189. return 27;
  190. }
  191. add_filter( 'excerpt_length', 'pique_custom_excerpt_length', 999 );
  192. function pique_custom_excerpt_more($more) {
  193. $more_link = '<span class="read-more">';
  194. $more_link .= '<a class="more-link" href="' . esc_url( get_the_permalink() ) . '" rel="bookmark">';
  195. $more_link .= sprintf(
  196. wp_kses( __( 'Read more %s', 'pique' ), array( 'span' => array( 'class' => array() ) ) ),
  197. the_title( '<span class="screen-reader-text">"', '"</span>', false )
  198. );
  199. $more_link .= '</a></span>';
  200. return '&hellip; ' . $more_link;
  201. }
  202. add_filter( 'excerpt_more', 'pique_custom_excerpt_more' );
  203. /*
  204. * Filter the categories archive widget to add a span around post count
  205. */
  206. function pique_cat_count_span( $links ) {
  207. $links = str_replace( '</a> (', '</a><span class="post-count">(', $links );
  208. $links = str_replace( ')', ')</span>', $links );
  209. return $links;
  210. }
  211. add_filter( 'wp_list_categories', 'pique_cat_count_span' );
  212. /*
  213. * Filter the archives widget to add a span around post count
  214. */
  215. function pique_archive_count_span( $links ) {
  216. $links = str_replace( '</a>&nbsp;(', '</a><span class="post-count">(', $links );
  217. $links = str_replace( ')', ')</span>', $links );
  218. return $links;
  219. }
  220. add_filter( 'get_archives_link', 'pique_archive_count_span' );
  221. /*
  222. * Check if WooCommerce is activated and a single product is in view
  223. */
  224. if ( ! function_exists( 'pique_is_woocommerce_in_view' ) ) {
  225. function pique_is_woocommerce_in_view() {
  226. if ( ! class_exists( 'woocommerce' ) ) {
  227. return true;
  228. } else {
  229. if ( is_woocommerce() ) {
  230. return false;
  231. } else {
  232. return true;
  233. }
  234. }
  235. }
  236. }