Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_REST_API_V2_Verbum_Auth
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 3
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 register_routes
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 get_auth
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2/**
3 * Plugin Name: Verbum Comments Experience Auth.
4 * Description: This returns the user info based on their cookies/headers.
5 * Author: Vertex
6 * Text Domain: jetpack-mu-wpcom
7 *
8 * @package automattic/jetpack-mu-plugins
9 */
10
11declare( strict_types = 1 );
12
13/**
14 * Verbum Comments Experience Auth endpoint.
15 */
16class WPCOM_REST_API_V2_Verbum_Auth extends \WP_REST_Controller {
17    /**
18     * Constructor.
19     */
20    public function __construct() {
21        $this->namespace                       = 'wpcom/v2';
22        $this->rest_base                       = '/verbum/auth';
23        $this->wpcom_is_wpcom_only_endpoint    = false;
24        $this->wpcom_is_site_specific_endpoint = false;
25        add_action( 'rest_api_init', array( $this, 'register_routes' ) );
26    }
27
28    /**
29     * Register the routes for the objects of the controller.
30     */
31    public function register_routes() {
32        register_rest_route(
33            $this->namespace,
34            $this->rest_base,
35            array(
36                'show_in_index'       => false,
37                'methods'             => \WP_REST_Server::READABLE,
38                'callback'            => array( $this, 'get_auth' ),
39                'permission_callback' => '__return_true',
40            )
41        );
42    }
43
44    /**
45     * Authorize user based on their WordPress credentials or Facebook cookies.
46     *
47     * @return array|WP_Error
48     */
49    public function get_auth() {
50        $user = wp_get_current_user();
51        if ( $user->ID ) {
52            list( $wordpress_avatar_url ) = wpcom_get_avatar_url( $user->user_email, 60, '', true );
53            return array(
54                'account' => $user->user_login,
55                'avatar'  => $wordpress_avatar_url,
56                'email'   => $user->user_email,
57                'link'    => ( ! empty( $user->user_url ) ? esc_url_raw( $user->user_url ) : esc_url_raw( 'http://gravatar.com/' . $user->user_login ) ),
58                'name'    => ( ! empty( $user->display_name ) ? $user->display_name : $user->user_login ),
59                'uid'     => $user->ID,
60                'service' => 'wordpress',
61            );
62        } else {
63            $fb = \Automattic\Jetpack\Verbum_Comments::verify_facebook_identity();
64            if ( ! is_wp_error( $fb ) ) {
65                return array(
66                    'account' => $fb->name,
67                    'avatar'  => $fb->picture->data->url,
68                    'email'   => $fb->email,
69                    'link'    => esc_url_raw( 'http://gravatar.com/' . $fb->email ),
70                    'name'    => $fb->name,
71                    'uid'     => $user->id,
72                    'service' => 'facebook',
73                );
74            }
75        }
76        return new \WP_Error( '403', 'Not Authorized', array( 'status' => 403 ) );
77    }
78}
79
80wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Verbum_Auth' );