Açıklama Yok

helper-functions.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Common theme functions
  4. *
  5. * @package WordPress
  6. * @subpackage Twenty_Nineteen
  7. * @since Twenty Nineteen 1.5
  8. */
  9. /**
  10. * Determines if post thumbnail can be displayed.
  11. */
  12. function twentynineteen_can_show_post_thumbnail() {
  13. return apply_filters( 'twentynineteen_can_show_post_thumbnail', ! post_password_required() && ! is_attachment() && has_post_thumbnail() );
  14. }
  15. /**
  16. * Returns true if image filters are enabled on the theme options.
  17. */
  18. function twentynineteen_image_filters_enabled() {
  19. return 0 !== get_theme_mod( 'image_filter', 1 );
  20. }
  21. /**
  22. * Returns the size for avatars used in the theme.
  23. */
  24. function twentynineteen_get_avatar_size() {
  25. return 60;
  26. }
  27. /**
  28. * Returns true if comment is by author of the post.
  29. *
  30. * @see get_comment_class()
  31. */
  32. function twentynineteen_is_comment_by_post_author( $comment = null ) {
  33. if ( is_object( $comment ) && $comment->user_id > 0 ) {
  34. $user = get_userdata( $comment->user_id );
  35. $post = get_post( $comment->comment_post_ID );
  36. if ( ! empty( $user ) && ! empty( $post ) ) {
  37. return $comment->user_id === $post->post_author;
  38. }
  39. }
  40. return false;
  41. }
  42. /**
  43. * Returns information about the current post's discussion, with cache support.
  44. */
  45. function twentynineteen_get_discussion_data() {
  46. static $discussion, $post_id;
  47. $current_post_id = get_the_ID();
  48. if ( $current_post_id === $post_id ) {
  49. return $discussion; /* If we have discussion information for post ID, return cached object */
  50. } else {
  51. $post_id = $current_post_id;
  52. }
  53. $comments = get_comments(
  54. array(
  55. 'post_id' => $current_post_id,
  56. 'orderby' => 'comment_date_gmt',
  57. 'order' => get_option( 'comment_order', 'asc' ), /* Respect comment order from Settings » Discussion. */
  58. 'status' => 'approve',
  59. 'number' => 20, /* Only retrieve the last 20 comments, as the end goal is just 6 unique authors */
  60. )
  61. );
  62. $authors = array();
  63. foreach ( $comments as $comment ) {
  64. $authors[] = ( (int) $comment->user_id > 0 ) ? (int) $comment->user_id : $comment->comment_author_email;
  65. }
  66. $authors = array_unique( $authors );
  67. $discussion = (object) array(
  68. 'authors' => array_slice( $authors, 0, 6 ), /* Six unique authors commenting on the post. */
  69. 'responses' => get_comments_number( $current_post_id ), /* Number of responses. */
  70. );
  71. return $discussion;
  72. }
  73. /**
  74. * Converts HSL to HEX colors.
  75. */
  76. function twentynineteen_hsl_hex( $h, $s, $l, $to_hex = true ) {
  77. $h /= 360;
  78. $s /= 100;
  79. $l /= 100;
  80. $r = $l;
  81. $g = $l;
  82. $b = $l;
  83. $v = ( $l <= 0.5 ) ? ( $l * ( 1.0 + $s ) ) : ( $l + $s - $l * $s );
  84. if ( $v > 0 ) {
  85. $m = $l + $l - $v;
  86. $sv = ( $v - $m ) / $v;
  87. $h *= 6.0;
  88. $sextant = floor( $h );
  89. $fract = $h - $sextant;
  90. $vsf = $v * $sv * $fract;
  91. $mid1 = $m + $vsf;
  92. $mid2 = $v - $vsf;
  93. switch ( $sextant ) {
  94. case 0:
  95. $r = $v;
  96. $g = $mid1;
  97. $b = $m;
  98. break;
  99. case 1:
  100. $r = $mid2;
  101. $g = $v;
  102. $b = $m;
  103. break;
  104. case 2:
  105. $r = $m;
  106. $g = $v;
  107. $b = $mid1;
  108. break;
  109. case 3:
  110. $r = $m;
  111. $g = $mid2;
  112. $b = $v;
  113. break;
  114. case 4:
  115. $r = $mid1;
  116. $g = $m;
  117. $b = $v;
  118. break;
  119. case 5:
  120. $r = $v;
  121. $g = $m;
  122. $b = $mid2;
  123. break;
  124. }
  125. }
  126. $r = round( $r * 255, 0 );
  127. $g = round( $g * 255, 0 );
  128. $b = round( $b * 255, 0 );
  129. if ( $to_hex ) {
  130. $r = ( $r < 15 ) ? '0' . dechex( $r ) : dechex( $r );
  131. $g = ( $g < 15 ) ? '0' . dechex( $g ) : dechex( $g );
  132. $b = ( $b < 15 ) ? '0' . dechex( $b ) : dechex( $b );
  133. return "#$r$g$b";
  134. }
  135. return "rgb($r, $g, $b)";
  136. }