template-functions.php 8.3 KB

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