template-functions.php 9.3 KB

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