template-functions.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <?php
  2. /**
  3. * Functions which enhance the theme by hooking into WordPress
  4. *
  5. * @package WordPress
  6. * @subpackage Twenty_Nineteen
  7. * @since 1.0.0
  8. */
  9. /**
  10. * Adds custom classes to the array of body classes.
  11. *
  12. * @param array $classes Classes for the body element.
  13. * @return array
  14. */
  15. function twentynineteen_body_classes( $classes ) {
  16. if ( is_singular() ) {
  17. // Adds `singular` to singular pages.
  18. $classes[] = 'singular';
  19. } else {
  20. // Adds `hfeed` to non singular pages.
  21. $classes[] = 'hfeed';
  22. }
  23. // Adds a class if image filters are enabled.
  24. if ( twentynineteen_image_filters_enabled() ) {
  25. $classes[] = 'image-filters-enabled';
  26. }
  27. return $classes;
  28. }
  29. add_filter( 'body_class', 'twentynineteen_body_classes' );
  30. /**
  31. * Adds custom class to the array of posts classes.
  32. */
  33. function twentynineteen_post_classes( $classes, $class, $post_id ) {
  34. $classes[] = 'entry';
  35. return $classes;
  36. }
  37. add_filter( 'post_class', 'twentynineteen_post_classes', 10, 3 );
  38. /**
  39. * Add a pingback url auto-discovery header for single posts, pages, or attachments.
  40. */
  41. function twentynineteen_pingback_header() {
  42. if ( is_singular() && pings_open() ) {
  43. echo '<link rel="pingback" href="', esc_url( get_bloginfo( 'pingback_url' ) ), '">';
  44. }
  45. }
  46. add_action( 'wp_head', 'twentynineteen_pingback_header' );
  47. /**
  48. * Changes comment form default fields.
  49. */
  50. function twentynineteen_comment_form_defaults( $defaults ) {
  51. $comment_field = $defaults['comment_field'];
  52. // Adjust height of comment form.
  53. $defaults['comment_field'] = preg_replace( '/rows="\d+"/', 'rows="5"', $comment_field );
  54. return $defaults;
  55. }
  56. add_filter( 'comment_form_defaults', 'twentynineteen_comment_form_defaults' );
  57. /**
  58. * Filters the default archive titles.
  59. */
  60. function twentynineteen_get_the_archive_title() {
  61. if ( is_category() ) {
  62. $title = __( 'Category Archives: ', 'twentynineteen' ) . '<span class="page-description">' . single_term_title( '', false ) . '</span>';
  63. } elseif ( is_tag() ) {
  64. $title = __( 'Tag Archives: ', 'twentynineteen' ) . '<span class="page-description">' . single_term_title( '', false ) . '</span>';
  65. } elseif ( is_author() ) {
  66. $title = __( 'Author Archives: ', 'twentynineteen' ) . '<span class="page-description">' . get_the_author_meta( 'display_name' ) . '</span>';
  67. } elseif ( is_year() ) {
  68. $title = __( 'Yearly Archives: ', 'twentynineteen' ) . '<span class="page-description">' . get_the_date( _x( 'Y', 'yearly archives date format', 'twentynineteen' ) ) . '</span>';
  69. } elseif ( is_month() ) {
  70. $title = __( 'Monthly Archives: ', 'twentynineteen' ) . '<span class="page-description">' . get_the_date( _x( 'F Y', 'monthly archives date format', 'twentynineteen' ) ) . '</span>';
  71. } elseif ( is_day() ) {
  72. $title = __( 'Daily Archives: ', 'twentynineteen' ) . '<span class="page-description">' . get_the_date() . '</span>';
  73. } elseif ( is_post_type_archive() ) {
  74. $title = __( 'Post Type Archives: ', 'twentynineteen' ) . '<span class="page-description">' . post_type_archive_title( '', false ) . '</span>';
  75. } elseif ( is_tax() ) {
  76. $tax = get_taxonomy( get_queried_object()->taxonomy );
  77. /* translators: %s: Taxonomy singular name. */
  78. $title = sprintf( esc_html__( '%s Archives:', 'twentynineteen' ), $tax->labels->singular_name );
  79. } else {
  80. $title = __( 'Archives:', 'twentynineteen' );
  81. }
  82. return $title;
  83. }
  84. add_filter( 'get_the_archive_title', 'twentynineteen_get_the_archive_title' );
  85. /**
  86. * Determines if post thumbnail can be displayed.
  87. */
  88. function twentynineteen_can_show_post_thumbnail() {
  89. return apply_filters( 'twentynineteen_can_show_post_thumbnail', ! post_password_required() && ! is_attachment() && has_post_thumbnail() );
  90. }
  91. /**
  92. * Returns true if image filters are enabled on the theme options.
  93. */
  94. function twentynineteen_image_filters_enabled() {
  95. return 0 !== get_theme_mod( 'image_filter', 1 );
  96. }
  97. /**
  98. * Add custom sizes attribute to responsive image functionality for post thumbnails.
  99. *
  100. * @origin Twenty Nineteen 1.0
  101. *
  102. * @param array $attr Attributes for the image markup.
  103. * @return string Value for use in post thumbnail 'sizes' attribute.
  104. */
  105. function twentynineteen_post_thumbnail_sizes_attr( $attr ) {
  106. if ( is_admin() ) {
  107. return $attr;
  108. }
  109. if ( ! is_singular() ) {
  110. $attr['sizes'] = '(max-width: 34.9rem) calc(100vw - 2rem), (max-width: 53rem) calc(8 * (100vw / 12)), (min-width: 53rem) calc(6 * (100vw / 12)), 100vw';
  111. }
  112. return $attr;
  113. }
  114. add_filter( 'wp_get_attachment_image_attributes', 'twentynineteen_post_thumbnail_sizes_attr', 10, 1 );
  115. /**
  116. * Returns the size for avatars used in the theme.
  117. */
  118. function twentynineteen_get_avatar_size() {
  119. return 60;
  120. }
  121. /**
  122. * Returns true if comment is by author of the post.
  123. *
  124. * @see get_comment_class()
  125. */
  126. function twentynineteen_is_comment_by_post_author( $comment = null ) {
  127. if ( is_object( $comment ) && $comment->user_id > 0 ) {
  128. $user = get_userdata( $comment->user_id );
  129. $post = get_post( $comment->comment_post_ID );
  130. if ( ! empty( $user ) && ! empty( $post ) ) {
  131. return $comment->user_id === $post->post_author;
  132. }
  133. }
  134. return false;
  135. }
  136. /**
  137. * Returns information about the current post's discussion, with cache support.
  138. */
  139. function twentynineteen_get_discussion_data() {
  140. static $discussion, $post_id;
  141. $current_post_id = get_the_ID();
  142. if ( $current_post_id === $post_id ) {
  143. return $discussion; /* If we have discussion information for post ID, return cached object */
  144. } else {
  145. $post_id = $current_post_id;
  146. }
  147. $comments = get_comments(
  148. array(
  149. 'post_id' => $current_post_id,
  150. 'orderby' => 'comment_date_gmt',
  151. 'order' => get_option( 'comment_order', 'asc' ), /* Respect comment order from Settings » Discussion. */
  152. 'status' => 'approve',
  153. 'number' => 20, /* Only retrieve the last 20 comments, as the end goal is just 6 unique authors */
  154. )
  155. );
  156. $authors = array();
  157. foreach ( $comments as $comment ) {
  158. $authors[] = ( (int) $comment->user_id > 0 ) ? (int) $comment->user_id : $comment->comment_author_email;
  159. }
  160. $authors = array_unique( $authors );
  161. $discussion = (object) array(
  162. 'authors' => array_slice( $authors, 0, 6 ), /* Six unique authors commenting on the post. */
  163. 'responses' => get_comments_number( $current_post_id ), /* Number of responses. */
  164. );
  165. return $discussion;
  166. }
  167. /**
  168. * Add an extra menu to our nav for our priority+ navigation to use
  169. *
  170. * @param object $nav_menu Nav menu.
  171. * @param object $args Nav menu args.
  172. * @return string More link for hidden menu items.
  173. */
  174. function twentynineteen_add_ellipses_to_nav( $nav_menu, $args ) {
  175. if ( 'menu-1' === $args->theme_location ) :
  176. $nav_menu .= '
  177. <div class="main-menu-more">
  178. <ul class="main-menu">
  179. <li class="menu-item menu-item-has-children">
  180. <button class="submenu-expand main-menu-more-toggle is-empty" tabindex="-1"
  181. aria-label="' . esc_attr__( 'More', 'twentynineteen' ) . '" aria-haspopup="true" aria-expanded="false">' .
  182. twentynineteen_get_icon_svg( 'arrow_drop_down_ellipsis' ) . '
  183. </button>
  184. <ul class="sub-menu hidden-links">
  185. <li class="mobile-parent-nav-menu-item">
  186. <button class="menu-item-link-return">' .
  187. twentynineteen_get_icon_svg( 'chevron_left' ) .
  188. esc_html__( 'Back', 'twentynineteen' ) . '
  189. </button>
  190. </li>
  191. </ul>
  192. </li>
  193. </ul>
  194. </div>';
  195. endif;
  196. return $nav_menu;
  197. }
  198. add_filter( 'wp_nav_menu', 'twentynineteen_add_ellipses_to_nav', 10, 2 );
  199. /**
  200. * WCAG 2.0 Attributes for Dropdown Menus
  201. *
  202. * Adjustments to menu attributes tot support WCAG 2.0 recommendations
  203. * for flyout and dropdown menus.
  204. *
  205. * @ref https://www.w3.org/WAI/tutorials/menus/flyout/
  206. */
  207. function twentynineteen_nav_menu_link_attributes( $atts, $item, $args, $depth ) {
  208. // Add [aria-haspopup] and [aria-expanded] to menu items that have children
  209. $item_has_children = in_array( 'menu-item-has-children', $item->classes );
  210. if ( $item_has_children ) {
  211. $atts['aria-haspopup'] = 'true';
  212. $atts['aria-expanded'] = 'false';
  213. }
  214. return $atts;
  215. }
  216. add_filter( 'nav_menu_link_attributes', 'twentynineteen_nav_menu_link_attributes', 10, 4 );
  217. /**
  218. * Add a dropdown icon to top-level menu items.
  219. *
  220. * @param string $output Nav menu item start element.
  221. * @param object $item Nav menu item.
  222. * @param int $depth Depth.
  223. * @param object $args Nav menu args.
  224. * @return string Nav menu item start element.
  225. * Add a dropdown icon to top-level menu items
  226. */
  227. function twentynineteen_add_dropdown_icons( $output, $item, $depth, $args ) {
  228. // Only add class to 'top level' items on the 'primary' menu.
  229. if ( ! isset( $args->theme_location ) || 'menu-1' !== $args->theme_location ) {
  230. return $output;
  231. }
  232. if ( in_array( 'mobile-parent-nav-menu-item', $item->classes, true ) && isset( $item->original_id ) ) {
  233. // Inject the keyboard_arrow_left SVG inside the parent nav menu item, and let the item link to the parent item.
  234. // @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.
  235. $link = sprintf(
  236. '<button class="menu-item-link-return" tabindex="-1">%s',
  237. twentynineteen_get_icon_svg( 'chevron_left', 24 )
  238. );
  239. // replace opening <a> with <button>
  240. $output = preg_replace(
  241. '/<a\s.*?>/',
  242. $link,
  243. $output,
  244. 1 // Limit.
  245. );
  246. // replace closing </a> with </button>
  247. $output = preg_replace(
  248. '#</a>#i',
  249. '</button>',
  250. $output,
  251. 1 // Limit.
  252. );
  253. } elseif ( in_array( 'menu-item-has-children', $item->classes, true ) ) {
  254. // Add SVG icon to parent items.
  255. $icon = twentynineteen_get_icon_svg( 'keyboard_arrow_down', 24 );
  256. $output .= sprintf(
  257. '<button class="submenu-expand" tabindex="-1">%s</button>',
  258. $icon
  259. );
  260. }
  261. return $output;
  262. }
  263. add_filter( 'walker_nav_menu_start_el', 'twentynineteen_add_dropdown_icons', 10, 4 );
  264. /**
  265. * Create a nav menu item to be displayed on mobile to navigate from submenu back to the parent.
  266. *
  267. * This duplicates each parent nav menu item and makes it the first child of itself.
  268. *
  269. * @param array $sorted_menu_items Sorted nav menu items.
  270. * @param object $args Nav menu args.
  271. * @return array Amended nav menu items.
  272. */
  273. function twentynineteen_add_mobile_parent_nav_menu_items( $sorted_menu_items, $args ) {
  274. static $pseudo_id = 0;
  275. if ( ! isset( $args->theme_location ) || 'menu-1' !== $args->theme_location ) {
  276. return $sorted_menu_items;
  277. }
  278. $amended_menu_items = array();
  279. foreach ( $sorted_menu_items as $nav_menu_item ) {
  280. $amended_menu_items[] = $nav_menu_item;
  281. if ( in_array( 'menu-item-has-children', $nav_menu_item->classes, true ) ) {
  282. $parent_menu_item = clone $nav_menu_item;
  283. $parent_menu_item->original_id = $nav_menu_item->ID;
  284. $parent_menu_item->ID = --$pseudo_id;
  285. $parent_menu_item->db_id = $parent_menu_item->ID;
  286. $parent_menu_item->object_id = $parent_menu_item->ID;
  287. $parent_menu_item->classes = array( 'mobile-parent-nav-menu-item' );
  288. $parent_menu_item->menu_item_parent = $nav_menu_item->ID;
  289. $amended_menu_items[] = $parent_menu_item;
  290. }
  291. }
  292. return $amended_menu_items;
  293. }
  294. add_filter( 'wp_nav_menu_objects', 'twentynineteen_add_mobile_parent_nav_menu_items', 10, 2 );
  295. /**
  296. * Convert HSL to HEX colors
  297. */
  298. function twentynineteen_hsl_hex( $h, $s, $l, $to_hex = true ) {
  299. $h /= 360;
  300. $s /= 100;
  301. $l /= 100;
  302. $r = $l;
  303. $g = $l;
  304. $b = $l;
  305. $v = ( $l <= 0.5 ) ? ( $l * ( 1.0 + $s ) ) : ( $l + $s - $l * $s );
  306. if ( $v > 0 ) {
  307. $m;
  308. $sv;
  309. $sextant;
  310. $fract;
  311. $vsf;
  312. $mid1;
  313. $mid2;
  314. $m = $l + $l - $v;
  315. $sv = ( $v - $m ) / $v;
  316. $h *= 6.0;
  317. $sextant = floor( $h );
  318. $fract = $h - $sextant;
  319. $vsf = $v * $sv * $fract;
  320. $mid1 = $m + $vsf;
  321. $mid2 = $v - $vsf;
  322. switch ( $sextant ) {
  323. case 0:
  324. $r = $v;
  325. $g = $mid1;
  326. $b = $m;
  327. break;
  328. case 1:
  329. $r = $mid2;
  330. $g = $v;
  331. $b = $m;
  332. break;
  333. case 2:
  334. $r = $m;
  335. $g = $v;
  336. $b = $mid1;
  337. break;
  338. case 3:
  339. $r = $m;
  340. $g = $mid2;
  341. $b = $v;
  342. break;
  343. case 4:
  344. $r = $mid1;
  345. $g = $m;
  346. $b = $v;
  347. break;
  348. case 5:
  349. $r = $v;
  350. $g = $m;
  351. $b = $mid2;
  352. break;
  353. }
  354. }
  355. $r = round( $r * 255, 0 );
  356. $g = round( $g * 255, 0 );
  357. $b = round( $b * 255, 0 );
  358. if ( $to_hex ) {
  359. $r = ( $r < 15 ) ? '0' . dechex( $r ) : dechex( $r );
  360. $g = ( $g < 15 ) ? '0' . dechex( $g ) : dechex( $g );
  361. $b = ( $b < 15 ) ? '0' . dechex( $b ) : dechex( $b );
  362. return "#$r$g$b";
  363. }
  364. return "rgb($r, $g, $b)";
  365. }