Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
41.67% covered (danger)
41.67%
20 / 48
20.00% covered (danger)
20.00%
1 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
VideoPressToken
41.67% covered (danger)
41.67%
20 / 48
20.00% covered (danger)
20.00%
1 / 5
82.31
0.00% covered (danger)
0.00%
0 / 1
 check_connection
60.00% covered (warning)
60.00%
3 / 5
0.00% covered (danger)
0.00%
0 / 1
5.02
 blog_id
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
3.14
 videopress_playback_jwt
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
12
 videopress_onetime_upload_token
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
20
 videopress_upload_jwt
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2/**
3 * VideoPress Token
4 *
5 * @package automattic/jetpack-videopress
6 */
7
8namespace Automattic\Jetpack\VideoPress;
9
10use Automattic\Jetpack\Connection\Client;
11use Automattic\Jetpack\Connection\Manager as Connection_Manager;
12use Jetpack_Options;
13/**
14 * VideoPress token utility class
15 */
16class VideoPressToken {
17    /**
18     * Check if user is connected.
19     *
20     * @return bool
21     * @throws Upload_Exception - If user is not connected.
22     */
23    private static function check_connection() {
24        if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
25            return true;
26        }
27        if ( ! ( new Connection_Manager() )->has_connected_owner() ) {
28            throw new Upload_Exception( __( 'You need to connect Jetpack before being able to upload a video to VideoPress.', 'jetpack-videopress-pkg' ) );
29        }
30        return true;
31    }
32
33    /**
34     * Get current blog id.
35     *
36     * @return string - Blog id.
37     */
38    public static function blog_id() {
39        if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
40            $blog_id = get_current_blog_id();
41        } else {
42            $blog_id = Jetpack_Options::get_option( 'id' );
43        }
44
45        return $blog_id;
46    }
47
48    /**
49     * Retrieve a Playback JWT via WPCOM api.
50     *
51     * @param string $guid The VideoPress GUID.
52     * @return string
53     */
54    public static function videopress_playback_jwt( $guid ) {
55        $blog_id = static::blog_id();
56
57        $args = array(
58            'method' => 'POST',
59        );
60
61        $endpoint = "sites/{$blog_id}/media/videopress-playback-jwt/{$guid}";
62
63        $result = Client::wpcom_json_api_request_as_blog( $endpoint, 'v2', $args, null, 'wpcom' );
64
65        if ( is_wp_error( $result ) ) {
66            return $result;
67        }
68
69        $response = json_decode( $result['body'], true );
70
71        if ( empty( $response['metadata_token'] ) ) {
72            return false;
73        }
74
75        return $response['metadata_token'];
76    }
77
78    /**
79     * Retrieve a One Time Upload Token via WPCOM api.
80     *
81     * @return string
82     * @throws Upload_Exception If token is empty or is had an error.
83     */
84    public static function videopress_onetime_upload_token() {
85        if ( static::check_connection() ) {
86            $blog_id = static::blog_id();
87
88            $args = array(
89                'method' => 'POST',
90            );
91
92            $endpoint = "sites/{$blog_id}/media/token";
93            $result   = Client::wpcom_json_api_request_as_blog( $endpoint, Client::WPCOM_JSON_API_VERSION, $args );
94
95            if ( is_wp_error( $result ) ) {
96                throw new Upload_Exception( __( 'Could not obtain a VideoPress upload token. Please try again later.', 'jetpack-videopress-pkg' ) );
97            }
98
99            $response = json_decode( $result['body'], true );
100
101            if ( empty( $response['upload_token'] ) ) {
102                throw new Upload_Exception( __( 'Could not obtain a VideoPress upload token. Please try again later.', 'jetpack-videopress-pkg' ) );
103            }
104
105            return $response['upload_token'];
106        }
107    }
108
109    /**
110     * Gets the VideoPress Upload JWT
111     *
112     * @return string
113     * @throws Upload_Exception - If user is not connected, if token is empty or failed to obtain.
114     */
115    public static function videopress_upload_jwt() {
116        if ( static::check_connection() ) {
117            $blog_id  = static::blog_id();
118            $endpoint = "sites/{$blog_id}/media/videopress-upload-jwt";
119            $args     = array( 'method' => 'POST' );
120            $result   = Client::wpcom_json_api_request_as_blog( $endpoint, 'v2', $args, null, 'wpcom' );
121
122            if ( is_wp_error( $result ) ) {
123                throw new Upload_Exception(
124                    __( 'Could not obtain a VideoPress upload JWT. Please try again later.', 'jetpack-videopress-pkg' ) .
125                    '(' . $result->get_error_message() . ')'
126                );
127            }
128
129            $response = json_decode( $result['body'], true );
130
131            if ( empty( $response['upload_token'] ) ) {
132                throw new Upload_Exception( __( 'Could not obtain a VideoPress upload JWT. Please try again later. (empty upload token)', 'jetpack-videopress-pkg' ) );
133            }
134
135            return $response['upload_token'];
136        }
137    }
138}