template-functions.php 8.8 KB

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