Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
78.16% covered (warning)
78.16%
68 / 87
71.43% covered (warning)
71.43%
5 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
XMLRPC
78.16% covered (warning)
78.16%
68 / 87
71.43% covered (warning)
71.43%
5 / 7
39.37
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 init
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 xmlrpc_methods
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 create_media_item
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
1 / 1
10
 update_videopress_media_item
87.10% covered (warning)
87.10%
27 / 31
0.00% covered (danger)
0.00%
0 / 1
9.17
 update_poster_image
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
12
 authenticate_user
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * The VIdeoPress XMLRPC class
4 *
5 * @package automattic/jetpack-videopress
6 */
7
8namespace Automattic\Jetpack\VideoPress;
9
10use WP_User;
11
12/**
13 * VideoPress playback module markup generator.
14 *
15 * @since 0.1.1
16 */
17class XMLRPC {
18
19    /**
20     * Singleton XMLRPC instance.
21     *
22     * @var XMLRPC
23     **/
24    private static $instance = null;
25
26    /**
27     * The current user object.
28     *
29     * @var WP_User
30     */
31    private $current_user;
32
33    /**
34     * Private VideoPress_XMLRPC constructor.
35     *
36     * Use the VideoPress_XMLRPC::init() method to get an instance.
37     */
38    private function __construct() {
39        add_filter( 'jetpack_xmlrpc_methods', array( $this, 'xmlrpc_methods' ), 10, 3 );
40    }
41
42    /**
43     * Initialize the VideoPress_XMLRPC and get back a singleton instance.
44     *
45     * @return XMLRPC
46     */
47    public static function init() {
48        if ( self::$instance === null ) {
49            self::$instance = new XMLRPC();
50        }
51
52        return self::$instance;
53    }
54
55    /**
56     * Adds additional methods the WordPress xmlrpc API for handling VideoPress specific features
57     *
58     * @param array   $methods The Jetpack API methods.
59     * @param array   $core_methods The WordPress Core API methods (ignored).
60     * @param WP_User $user The user object the API request is signed by.
61     *
62     * @return array
63     */
64    public function xmlrpc_methods( $methods, $core_methods, $user ) {
65        if ( $user && $user instanceof WP_User ) {
66            $this->current_user = $user;
67        }
68
69        $methods['jetpack.createMediaItem']                = array( $this, 'create_media_item' );
70        $methods['jetpack.updateVideoPressMediaItem']      = array( $this, 'update_videopress_media_item' );
71        $methods['jetpack.updateVideoPressMediaItemError'] = array( $this, 'update_videopress_media_item' );
72        $methods['jetpack.updateVideoPressPosterImage']    = array( $this, 'update_poster_image' );
73
74        return $methods;
75    }
76
77    /**
78     * This is used by the WPCOM VideoPress uploader in order to create a media item with
79     * specific meta data about an uploaded file. After this, the transcoding session will
80     * update the meta information via the update_videopress_media_item() method.
81     *
82     * Note: This method technically handles the creation of multiple media objects, though
83     * in practice this is never done.
84     *
85     * @param array $media Media items being uploaded.
86     * @return array
87     */
88    public function create_media_item( $media ) {
89        $this->authenticate_user();
90
91        foreach ( $media as & $media_item ) {
92            $url   = is_string( $media_item['url'] ?? null ) ? $media_item['url'] : '';
93            $title = isset( $media_item['title'] ) && '' !== $media_item['title']
94                ? sanitize_text_field( $media_item['title'] )
95                : sanitize_title( basename( $url ) );
96            $guid  = $media['guid'] ?? null;
97
98            $media_id = videopress_create_new_media_item( $title, $guid );
99
100            $post_update = array();
101            if ( isset( $media_item['description'] ) && '' !== $media_item['description'] ) {
102                $post_update['post_content'] = sanitize_textarea_field( $media_item['description'] );
103            }
104            if ( isset( $media_item['caption'] ) && '' !== $media_item['caption'] ) {
105                $post_update['post_excerpt'] = sanitize_textarea_field( $media_item['caption'] );
106            }
107            if ( $post_update ) {
108                $post_update['ID'] = $media_id;
109                wp_update_post( $post_update );
110            }
111
112            wp_update_attachment_metadata(
113                $media_id,
114                array(
115                    'original' => array(
116                        'url' => $media_item['url'],
117                    ),
118                )
119            );
120
121            $media_item['post'] = get_post( $media_id );
122        }
123
124        return array( 'media' => $media );
125    }
126
127    /**
128     * Update VideoPress metadata for a media item.
129     *
130     * @param array $request Media item to update.
131     *
132     * @return bool
133     */
134    public function update_videopress_media_item( $request ) {
135        $this->authenticate_user();
136
137        $id     = $request['post_id'];
138        $status = $request['status'];
139        $format = $request['format'];
140        $info   = $request['info'];
141
142        $attachment = get_post( $id );
143        if ( ! $attachment ) {
144            return false;
145        }
146
147        $attachment->guid           = $info['original'];
148        $attachment->post_mime_type = 'video/videopress';
149
150        wp_update_post( $attachment );
151
152        // Update the vp guid and set it to a direct meta property.
153        update_post_meta( $id, 'videopress_guid', $info['guid'] );
154
155        $meta = wp_get_attachment_metadata( $id );
156        if ( ! is_array( $meta ) ) {
157            $meta = array();
158        }
159
160        $meta['width']             = $info['width'];
161        $meta['height']            = $info['height'];
162        $meta['original']['url']   = $info['original'];
163        $meta['videopress']        = $info;
164        $meta['videopress']['url'] = 'https://videopress.com/v/' . $info['guid'];
165
166        // Update file statuses
167        if ( ! empty( $format ) ) {
168            $meta['file_statuses'][ $format ] = $status;
169        }
170
171        if ( ! empty( $info['poster'] ) && ! get_post_meta( $id, '_thumbnail_id', true ) ) {
172            // Update the poster in the VideoPress info.
173            $thumbnail_id = videopress_download_poster_image( $info['poster'], $id );
174
175            if ( is_int( $thumbnail_id ) ) {
176                update_post_meta( $id, '_thumbnail_id', $thumbnail_id );
177            }
178        }
179
180        wp_update_attachment_metadata( $id, $meta );
181
182        videopress_update_meta_data( $id );
183
184        // A later auxiliary-format callback must not erase an MP4 failure.
185        $failed = 'ERROR' === ( $meta['file_statuses']['mp4'] ?? '' );
186        update_post_meta( $id, 'videopress_status', $failed ? 'failed' : ( videopress_is_finished_processing( $id ) ? 'complete' : 'processing' ) );
187
188        return true;
189    }
190
191    /**
192     * Update poster image for a VideoPress media item.
193     *
194     * @param array $request The media item to update.
195     * @return bool
196     */
197    public function update_poster_image( $request ) {
198        $this->authenticate_user();
199
200        $post_id = $request['post_id'];
201        $poster  = $request['poster'];
202
203        $attachment = get_post( $post_id );
204        if ( ! $attachment ) {
205            return false;
206        }
207
208        $poster = apply_filters( 'jetpack_photon_url', $poster );
209
210        $meta                         = wp_get_attachment_metadata( $post_id );
211        $meta['videopress']['poster'] = $poster;
212        wp_update_attachment_metadata( $post_id, $meta );
213
214        // Update the poster in the VideoPress info.
215        $thumbnail_id = videopress_download_poster_image( $poster, $post_id );
216
217        if ( ! is_int( $thumbnail_id ) ) {
218            return false;
219        }
220
221        update_post_meta( $post_id, '_thumbnail_id', $thumbnail_id );
222
223        return true;
224    }
225
226    /**
227     * Check if the XML-RPC request is signed by a user token, and authenticate the user in WordPress.
228     *
229     * @return bool
230     */
231    private function authenticate_user() {
232        if ( $this->current_user ) {
233            wp_set_current_user( $this->current_user->ID );
234
235            return true;
236        }
237
238        return false;
239    }
240}