template-functions.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. return $classes;
  35. }
  36. add_filter( 'body_class', 'varia_body_classes' );
  37. /**
  38. * Adds custom class to the array of posts classes.
  39. */
  40. function varia_post_classes( $classes, $class, $post_id ) {
  41. $classes[] = 'entry';
  42. return $classes;
  43. }
  44. add_filter( 'post_class', 'varia_post_classes', 10, 3 );
  45. /**
  46. * Add a pingback url auto-discovery header for single posts, pages, or attachments.
  47. */
  48. function varia_pingback_header() {
  49. if ( is_singular() && pings_open() ) {
  50. echo '<link rel="pingback" href="', esc_url( get_bloginfo( 'pingback_url' ) ), '">';
  51. }
  52. }
  53. add_action( 'wp_head', 'varia_pingback_header' );
  54. /**
  55. * Changes comment form default fields.
  56. */
  57. function varia_comment_form_defaults( $defaults ) {
  58. $comment_field = $defaults['comment_field'];
  59. // Adjust height of comment form.
  60. $defaults['comment_field'] = preg_replace( '/rows="\d+"/', 'rows="5"', $comment_field );
  61. return $defaults;
  62. }
  63. add_filter( 'comment_form_defaults', 'varia_comment_form_defaults' );
  64. /**
  65. * Filters the default archive titles.
  66. */
  67. function varia_get_the_archive_title() {
  68. if ( is_category() ) {
  69. $title = __( 'Category Archives: ', 'varia' ) . '<span class="page-description">' . single_term_title( '', false ) . '</span>';
  70. } elseif ( is_tag() ) {
  71. $title = __( 'Tag Archives: ', 'varia' ) . '<span class="page-description">' . single_term_title( '', false ) . '</span>';
  72. } elseif ( is_author() ) {
  73. $title = __( 'Author Archives: ', 'varia' ) . '<span class="page-description">' . get_the_author_meta( 'display_name' ) . '</span>';
  74. } elseif ( is_year() ) {
  75. $title = __( 'Yearly Archives: ', 'varia' ) . '<span class="page-description">' . get_the_date( _x( 'Y', 'yearly archives date format', 'varia' ) ) . '</span>';
  76. } elseif ( is_month() ) {
  77. $title = __( 'Monthly Archives: ', 'varia' ) . '<span class="page-description">' . get_the_date( _x( 'F Y', 'monthly archives date format', 'varia' ) ) . '</span>';
  78. } elseif ( is_day() ) {
  79. $title = __( 'Daily Archives: ', 'varia' ) . '<span class="page-description">' . get_the_date() . '</span>';
  80. } elseif ( is_post_type_archive() ) {
  81. $title = __( 'Post Type Archives: ', 'varia' ) . '<span class="page-description">' . post_type_archive_title( '', false ) . '</span>';
  82. } elseif ( is_tax() ) {
  83. $tax = get_taxonomy( get_queried_object()->taxonomy );
  84. /* translators: %s: Taxonomy singular name */
  85. $title = sprintf( esc_html__( '%s Archives:', 'varia' ), $tax->labels->singular_name );
  86. } else {
  87. $title = __( 'Archives:', 'varia' );
  88. }
  89. return $title;
  90. }
  91. add_filter( 'get_the_archive_title', 'varia_get_the_archive_title' );
  92. /**
  93. * Determines if post thumbnail can be displayed.
  94. */
  95. function varia_can_show_post_thumbnail() {
  96. return apply_filters( 'varia_can_show_post_thumbnail', ! post_password_required() && ! is_attachment() && has_post_thumbnail() );
  97. }
  98. /**
  99. * Returns the size for avatars used in the theme.
  100. */
  101. function varia_get_avatar_size() {
  102. return 60;
  103. }
  104. /**
  105. * Returns true if comment is by author of the post.
  106. *
  107. * @see get_comment_class()
  108. */
  109. function varia_is_comment_by_post_author( $comment = null ) {
  110. if ( is_object( $comment ) && $comment->user_id > 0 ) {
  111. $user = get_userdata( $comment->user_id );
  112. $post = get_post( $comment->comment_post_ID );
  113. if ( ! empty( $user ) && ! empty( $post ) ) {
  114. return $comment->user_id === $post->post_author;
  115. }
  116. }
  117. return false;
  118. }
  119. /**
  120. * WCAG 2.0 Attributes for Dropdown Menus
  121. *
  122. * Adjustments to menu attributes tot support WCAG 2.0 recommendations
  123. * for flyout and dropdown menus.
  124. *
  125. * @ref https://www.w3.org/WAI/tutorials/menus/flyout/
  126. */
  127. function varia_nav_menu_link_attributes( $atts, $item, $args, $depth ) {
  128. // Add [aria-haspopup] and [aria-expanded] to menu items that have children
  129. $item_has_children = in_array( 'menu-item-has-children', $item->classes );
  130. if ( $item_has_children ) {
  131. $atts['aria-haspopup'] = 'true';
  132. $atts['aria-expanded'] = 'false';
  133. }
  134. return $atts;
  135. }
  136. add_filter( 'nav_menu_link_attributes', 'varia_nav_menu_link_attributes', 10, 4 );
  137. /**
  138. * Add a dropdown icon to top-level menu items.
  139. *
  140. * @param string $output Nav menu item start element.
  141. * @param object $item Nav menu item.
  142. * @param int $depth Depth.
  143. * @param object $args Nav menu args.
  144. * @return string Nav menu item start element.
  145. * Add a dropdown icon to top-level menu items
  146. */
  147. function varia_add_dropdown_icons( $output, $item, $depth, $args ) {
  148. // Only add class to 'top level' items on the 'primary' menu.
  149. if ( ! isset( $args->theme_location ) || 'menu-1' !== $args->theme_location ) {
  150. return $output;
  151. }
  152. if ( in_array( 'mobile-parent-nav-menu-item', $item->classes, true ) && isset( $item->original_id ) ) {
  153. // Inject the keyboard_arrow_left SVG inside the parent nav menu item, and let the item link to the parent item.
  154. // @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.
  155. $link = sprintf(
  156. '<button class="menu-item-link-return" tabindex="-1">%s',
  157. varia_get_icon_svg( 'chevron_left', 24 )
  158. );
  159. // replace opening <a> with <button>
  160. $output = preg_replace(
  161. '/<a\s.*?>/',
  162. $link,
  163. $output,
  164. 1 // Limit.
  165. );
  166. // replace closing </a> with </button>
  167. $output = preg_replace(
  168. '#</a>#i',
  169. '</button>',
  170. $output,
  171. 1 // Limit.
  172. );
  173. } elseif ( in_array( 'menu-item-has-children', $item->classes, true ) ) {
  174. // Add SVG icon to parent items.
  175. $icon = varia_get_icon_svg( 'keyboard_arrow_down', 24 );
  176. $output .= sprintf(
  177. '<button class="submenu-expand" tabindex="-1">%s</button>',
  178. $icon
  179. );
  180. }
  181. return $output;
  182. }
  183. // add_filter( 'walker_nav_menu_start_el', 'varia_add_dropdown_icons', 10, 4 );
  184. /**
  185. * Create a nav menu item to be displayed on mobile to navigate from submenu back to the parent.
  186. *
  187. * This duplicates each parent nav menu item and makes it the first child of itself.
  188. *
  189. * @param array $sorted_menu_items Sorted nav menu items.
  190. * @param object $args Nav menu args.
  191. * @return array Amended nav menu items.
  192. */
  193. function varia_add_mobile_parent_nav_menu_items( $sorted_menu_items, $args ) {
  194. static $pseudo_id = 0;
  195. if ( ! isset( $args->theme_location ) || 'menu-1' !== $args->theme_location ) {
  196. return $sorted_menu_items;
  197. }
  198. $amended_menu_items = array();
  199. foreach ( $sorted_menu_items as $nav_menu_item ) {
  200. $amended_menu_items[] = $nav_menu_item;
  201. if ( in_array( 'menu-item-has-children', $nav_menu_item->classes, true ) ) {
  202. $parent_menu_item = clone $nav_menu_item;
  203. $parent_menu_item->original_id = $nav_menu_item->ID;
  204. $parent_menu_item->ID = --$pseudo_id;
  205. $parent_menu_item->db_id = $parent_menu_item->ID;
  206. $parent_menu_item->object_id = $parent_menu_item->ID;
  207. $parent_menu_item->classes = array( 'mobile-parent-nav-menu-item' );
  208. $parent_menu_item->menu_item_parent = $nav_menu_item->ID;
  209. $amended_menu_items[] = $parent_menu_item;
  210. }
  211. }
  212. return $amended_menu_items;
  213. }
  214. // add_filter( 'wp_nav_menu_objects', 'varia_add_mobile_parent_nav_menu_items', 10, 2 );