template-functions.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. /**
  3. * Functions which enhance the theme by hooking into WordPress
  4. *
  5. * @package WordPress
  6. * @subpackage Varia
  7. * @since 1.0.0
  8. */
  9. /**
  10. * Remove Gutenberg Block Styles
  11. * - We could end up keeping these, but for now these styles
  12. * should live in `_block-utilities.scss` where we can make
  13. * them simpler and more block-agnostic.
  14. */
  15. function wps_deregister_styles() {
  16. wp_dequeue_style( 'wp-block-library' );
  17. wp_dequeue_style( 'wp-block-library-theme' );
  18. }
  19. //add_action( 'wp_print_styles', 'wps_deregister_styles', 100 );
  20. /**
  21. * Adds custom classes to the array of body classes.
  22. *
  23. * @param array $classes Classes for the body element.
  24. * @return array
  25. */
  26. function varia_body_classes( $classes ) {
  27. if ( is_singular() ) {
  28. // Adds `singular` to singular pages.
  29. $classes[] = 'singular';
  30. } else {
  31. // Adds `hfeed` to non singular pages.
  32. $classes[] = 'hfeed';
  33. }
  34. // Adds a class if image filters are enabled.
  35. if ( varia_image_filters_enabled() ) {
  36. $classes[] = 'image-filters-enabled';
  37. }
  38. // Add a body class if the site header is hidden on the homepage.
  39. $hide_site_header = get_theme_mod( 'hide_site_header', false );
  40. if ( true === $hide_site_header && is_front_page() && is_page() ) {
  41. $classes[] = 'hide-homepage-header';
  42. }
  43. // Add a body class if the footer elements are hidden on the homepage.
  44. $hide_site_footer = get_theme_mod( 'hide_site_footer', false );
  45. if ( true === $hide_site_footer && is_front_page() && is_page() ) {
  46. $classes[] = 'hide-homepage-footer';
  47. }
  48. return $classes;
  49. }
  50. add_filter( 'body_class', 'varia_body_classes' );
  51. /**
  52. * Adds custom class to the array of posts classes.
  53. */
  54. function varia_post_classes( $classes, $class, $post_id ) {
  55. $classes[] = 'entry';
  56. return $classes;
  57. }
  58. add_filter( 'post_class', 'varia_post_classes', 10, 3 );
  59. /**
  60. * Add a pingback url auto-discovery header for single posts, pages, or attachments.
  61. */
  62. function varia_pingback_header() {
  63. if ( is_singular() && pings_open() ) {
  64. echo '<link rel="pingback" href="', esc_url( get_bloginfo( 'pingback_url' ) ), '">';
  65. }
  66. }
  67. add_action( 'wp_head', 'varia_pingback_header' );
  68. /**
  69. * Changes comment form default fields.
  70. */
  71. function varia_comment_form_defaults( $defaults ) {
  72. $comment_field = $defaults['comment_field'];
  73. // Adjust height of comment form.
  74. $defaults['comment_field'] = preg_replace( '/rows="\d+"/', 'rows="5"', $comment_field );
  75. return $defaults;
  76. }
  77. add_filter( 'comment_form_defaults', 'varia_comment_form_defaults' );
  78. /**
  79. * Filters the default archive titles.
  80. */
  81. function varia_get_the_archive_title() {
  82. if ( is_category() ) {
  83. $title = __( 'Category Archives: ', 'varia' ) . '<span class="page-description">' . single_term_title( '', false ) . '</span>';
  84. } elseif ( is_tag() ) {
  85. $title = __( 'Tag Archives: ', 'varia' ) . '<span class="page-description">' . single_term_title( '', false ) . '</span>';
  86. } elseif ( is_author() ) {
  87. $title = __( 'Author Archives: ', 'varia' ) . '<span class="page-description">' . get_the_author_meta( 'display_name' ) . '</span>';
  88. } elseif ( is_year() ) {
  89. $title = __( 'Yearly Archives: ', 'varia' ) . '<span class="page-description">' . get_the_date( _x( 'Y', 'yearly archives date format', 'varia' ) ) . '</span>';
  90. } elseif ( is_month() ) {
  91. $title = __( 'Monthly Archives: ', 'varia' ) . '<span class="page-description">' . get_the_date( _x( 'F Y', 'monthly archives date format', 'varia' ) ) . '</span>';
  92. } elseif ( is_day() ) {
  93. $title = __( 'Daily Archives: ', 'varia' ) . '<span class="page-description">' . get_the_date() . '</span>';
  94. } elseif ( is_post_type_archive() ) {
  95. $cpt = get_post_type_object( get_queried_object()->name );
  96. /* translators: %s: Post type singular name */
  97. $title = sprintf( esc_html__( '%s Archives', 'varia' ),
  98. $cpt->labels->singular_name
  99. );
  100. } elseif ( is_tax() ) {
  101. $tax = get_taxonomy( get_queried_object()->taxonomy );
  102. /* translators: %s: Taxonomy singular name */
  103. $title = sprintf( esc_html__( '%s Archives', 'varia' ),
  104. $tax->labels->singular_name
  105. );
  106. } else {
  107. $title = __( 'Archives:', 'varia' );
  108. }
  109. return $title;
  110. }
  111. add_filter( 'get_the_archive_title', 'varia_get_the_archive_title' );
  112. /**
  113. * Determines if post thumbnail can be displayed.
  114. */
  115. function varia_can_show_post_thumbnail() {
  116. return apply_filters( 'varia_can_show_post_thumbnail', ! post_password_required() && ! is_attachment() && has_post_thumbnail() );
  117. }
  118. /**
  119. * Returns true if image filters are enabled on the theme options.
  120. */
  121. function varia_image_filters_enabled() {
  122. return 0 !== get_theme_mod( 'image_filter', 1 );
  123. }
  124. /**
  125. * Returns the size for avatars used in the theme.
  126. */
  127. function varia_get_avatar_size() {
  128. return 60;
  129. }
  130. /**
  131. * Returns true if comment is by author of the post.
  132. *
  133. * @see get_comment_class()
  134. */
  135. function varia_is_comment_by_post_author( $comment = null ) {
  136. if ( is_object( $comment ) && $comment->user_id > 0 ) {
  137. $user = get_userdata( $comment->user_id );
  138. $post = get_post( $comment->comment_post_ID );
  139. if ( ! empty( $user ) && ! empty( $post ) ) {
  140. return $comment->user_id === $post->post_author;
  141. }
  142. }
  143. return false;
  144. }
  145. /**
  146. * WCAG 2.0 Attributes for Dropdown Menus
  147. *
  148. * Adjustments to menu attributes tot support WCAG 2.0 recommendations
  149. * for flyout and dropdown menus.
  150. *
  151. * @ref https://www.w3.org/WAI/tutorials/menus/flyout/
  152. */
  153. function varia_nav_menu_link_attributes( $atts, $item, $args, $depth ) {
  154. // Add [aria-haspopup] and [aria-expanded] to menu items that have children
  155. $item_has_children = in_array( 'menu-item-has-children', $item->classes );
  156. if ( $item_has_children ) {
  157. $atts['aria-haspopup'] = 'true';
  158. $atts['aria-expanded'] = 'false';
  159. }
  160. return $atts;
  161. }
  162. add_filter( 'nav_menu_link_attributes', 'varia_nav_menu_link_attributes', 10, 4 );
  163. /*
  164. * Create the continue reading link
  165. */
  166. function varia_continue_reading_link() {
  167. if ( ! is_admin() ) {
  168. $continue_reading = sprintf(
  169. /* translators: %s: Name of current post. */
  170. wp_kses( __( 'Continue reading %s', 'varia' ), array( 'span' => array( 'class' => array() ) ) ),
  171. the_title( '<span class="screen-reader-text">"', '"</span>', false )
  172. );
  173. return '<a class="more-link" href="' . esc_url( get_permalink() ) . '">' . $continue_reading . '</a>';
  174. }
  175. }
  176. // Filter the excerpt more link
  177. add_filter( 'excerpt_more', 'varia_continue_reading_link' );
  178. // Filter the content more link
  179. add_filter( 'the_content_more_link', 'varia_continue_reading_link' );
  180. /**
  181. * Add a dropdown icon to top-level menu items.
  182. *
  183. * @param string $output Nav menu item start element.
  184. * @param object $item Nav menu item.
  185. * @param int $depth Depth.
  186. * @param object $args Nav menu args.
  187. * @return string Nav menu item start element.
  188. * Add a dropdown icon to top-level menu items
  189. */
  190. function varia_add_dropdown_icons( $output, $item, $depth, $args ) {
  191. // Only add class to 'top level' items on the 'primary' menu.
  192. if ( ! isset( $args->theme_location ) || 'menu-1' !== $args->theme_location ) {
  193. return $output;
  194. }
  195. if ( in_array( 'mobile-parent-nav-menu-item', $item->classes, true ) && isset( $item->original_id ) ) {
  196. // Inject the keyboard_arrow_left SVG inside the parent nav menu item, and let the item link to the parent item.
  197. // @todo Only do this for nested submenus? If on a first-level submenu, then really the link could be "#" since the desire is to remove the target entirely.
  198. $link = sprintf(
  199. '<button class="menu-item-link-return" tabindex="-1">%s',
  200. varia_get_icon_svg( 'chevron_left', 24 )
  201. );
  202. // replace opening <a> with <button>
  203. $output = preg_replace(
  204. '/<a\s.*?>/',
  205. $link,
  206. $output,
  207. 1 // Limit.
  208. );
  209. // replace closing </a> with </button>
  210. $output = preg_replace(
  211. '#</a>#i',
  212. '</button>',
  213. $output,
  214. 1 // Limit.
  215. );
  216. } elseif ( in_array( 'menu-item-has-children', $item->classes, true ) ) {
  217. // Add SVG icon to parent items.
  218. $icon = varia_get_icon_svg( 'keyboard_arrow_down', 24 );
  219. $output .= sprintf(
  220. '<button class="submenu-expand" tabindex="-1">%s</button>',
  221. $icon
  222. );
  223. }
  224. return $output;
  225. }
  226. // add_filter( 'walker_nav_menu_start_el', 'varia_add_dropdown_icons', 10, 4 );
  227. /**
  228. * Create a nav menu item to be displayed on mobile to navigate from submenu back to the parent.
  229. *
  230. * This duplicates each parent nav menu item and makes it the first child of itself.
  231. *
  232. * @param array $sorted_menu_items Sorted nav menu items.
  233. * @param object $args Nav menu args.
  234. * @return array Amended nav menu items.
  235. */
  236. function varia_add_mobile_parent_nav_menu_items( $sorted_menu_items, $args ) {
  237. static $pseudo_id = 0;
  238. if ( ! isset( $args->theme_location ) || 'menu-1' !== $args->theme_location ) {
  239. return $sorted_menu_items;
  240. }
  241. $amended_menu_items = array();
  242. foreach ( $sorted_menu_items as $nav_menu_item ) {
  243. $amended_menu_items[] = $nav_menu_item;
  244. if ( in_array( 'menu-item-has-children', $nav_menu_item->classes, true ) ) {
  245. $parent_menu_item = clone $nav_menu_item;
  246. $parent_menu_item->original_id = $nav_menu_item->ID;
  247. $parent_menu_item->ID = --$pseudo_id;
  248. $parent_menu_item->db_id = $parent_menu_item->ID;
  249. $parent_menu_item->object_id = $parent_menu_item->ID;
  250. $parent_menu_item->classes = array( 'mobile-parent-nav-menu-item' );
  251. $parent_menu_item->menu_item_parent = $nav_menu_item->ID;
  252. $amended_menu_items[] = $parent_menu_item;
  253. }
  254. }
  255. return $amended_menu_items;
  256. }
  257. // add_filter( 'wp_nav_menu_objects', 'varia_add_mobile_parent_nav_menu_items', 10, 2 );