Make WordPress Core

Ticket #45984: 45984-helper-functions-file.patch

File 45984-helper-functions-file.patch, 4.2 KB (added by kjellr, 5 years ago)
  • src/wp-content/themes/twentynineteen/inc/helper-functions.php

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