Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
Identity
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 1
72
0.00% covered (danger)
0.00%
0 / 1
 settings
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 1
72
1<?php
2/**
3 * The commenter's identity.
4 *
5 * @package automattic/jetpack-comments
6 */
7
8namespace Automattic\Jetpack\Comments;
9
10/**
11 * Who is leaving the comment being written now.
12 */
13class Identity {
14
15    /**
16     * Who is leaving the comment, as far as this site knows.
17     *
18     * @return array
19     */
20    public static function settings() {
21        $commenter = wp_get_current_commenter();
22
23        // Nothing under `identity` is about the visitor: the HTML is page-cached
24        // and served to everyone, so who holds a passport comes from a cookie.
25        $settings = array(
26            'isLoggedIn' => is_user_logged_in(),
27            'avatarUrl'  => '',
28            'commenter'  => array(
29                'author' => $commenter['comment_author'],
30                'email'  => $commenter['comment_author_email'],
31                'url'    => $commenter['comment_author_url'],
32            ),
33            'user'       => null,
34            'identity'   => array(
35                'blogId'        => Checkpoint::blog_id(),
36                'canSignIn'     => false,
37                'connect'       => null,
38                'connectUrl'    => Checkpoint_Endpoint::route_url( Checkpoint_Endpoint::CONNECT_ROUTE ),
39                // Empty off Simple, where Checkpoint_Endpoint::email() can only answer no.
40                'emailUrl'      => function_exists( 'is_email_wp_emails' ) ? Checkpoint_Endpoint::route_url( Checkpoint_Endpoint::EMAIL_ROUTE ) : '',
41                'origin'        => 'https://public-api.wordpress.com',
42                'codeField'     => Checkpoint::CODE_FIELD,
43                'passportField' => Checkpoint::PASSPORT_FIELD,
44                'displayCookie' => Passport::DISPLAY_COOKIE,
45                'cookieHash'    => COOKIEHASH,
46                'cookiePath'    => COOKIEPATH,
47                'cookieDomain'  => COOKIE_DOMAIN ? COOKIE_DOMAIN : '',
48                'defaultAvatar' => Avatars::default_url( 80 ),
49                // A path: Simple's admin_url() is the .wordpress.com host, which cannot clear a custom domain's cookies.
50                'logoutUrl'     => wp_make_link_relative( admin_url( 'admin-ajax.php' ) ),
51                'logoutAction'  => Checkpoint_Endpoint::LOGOUT_ACTION,
52            ),
53        );
54
55        if ( is_user_logged_in() ) {
56            $user                  = wp_get_current_user();
57            $settings['avatarUrl'] = html_entity_decode( (string) get_avatar_url( $user->ID, array( 'size' => 80 ) ), ENT_QUOTES );
58            $settings['user']      = array(
59                'name'           => $user->display_name,
60                // Empty for a user who may not edit their own profile, as core's link would be.
61                'editProfileUrl' => get_edit_user_link(),
62            );
63
64            return $settings;
65        }
66
67        if ( get_option( 'show_avatars' ) ) {
68            $settings['avatarUrl'] = $commenter['comment_author_email']
69                ? html_entity_decode( (string) get_avatar_url( $commenter['comment_author_email'], array( 'size' => 80 ) ), ENT_QUOTES )
70                : Avatars::default_url( 80 );
71        }
72
73        if ( ! Checkpoint::is_available() ) {
74            return $settings;
75        }
76
77        // Visitors share this challenge until it expires: it only filters messages
78        // to the window that opened the popup, and the connect route issues fresh ones.
79        $challenge = rtrim( strtr( base64_encode( random_bytes( 32 ) ), '+/', '-_' ), '=' ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- base64url is the wire format.
80
81        $connect = Checkpoint::connect_url( $challenge );
82
83        $settings['identity']['canSignIn'] = true;
84        $settings['identity']['connect']   = is_wp_error( $connect ) ? null : $connect;
85
86        return $settings;
87    }
88}