Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
38.55% covered (danger)
38.55%
32 / 83
42.86% covered (danger)
42.86%
3 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
XMLRPC
38.55% covered (danger)
38.55%
32 / 83
42.86% covered (danger)
42.86%
3 / 7
196.12
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
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 create_media_item
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
1 / 1
10
 update_videopress_media_item
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
42
 update_poster_image
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
12
 authenticate_user
50.00% covered (danger)
50.00%
2 / 4
0.00% covered (danger)
0.00%
0 / 1
2.50
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.updateVideoPressPosterImage'] = array( $this, 'update_poster_image' );
72
73        return $methods;
74    }
75
76    /**
77     * This is used by the WPCOM VideoPress uploader in order to create a media item with
78     * specific meta data about an uploaded file. After this, the transcoding session will
79     * update the meta information via the update_videopress_media_item() method.
80     *
81     * Note: This method technically handles the creation of multiple media objects, though
82     * in practice this is never done.
83     *
84     * @param array $media Media items being uploaded.
85     * @return array
86     */
87    public function create_media_item( $media ) {
88        $this->authenticate_user();
89
90        foreach ( $media as & $media_item ) {
91            $url   = is_string( $media_item['url'] ?? null ) ? $media_item['url'] : '';
92            $title = isset( $media_item['title'] ) && '' !== $media_item['title']
93                ? sanitize_text_field( $media_item['title'] )
94                : sanitize_title( basename( $url ) );
95            $guid  = $media['guid'] ?? null;
96
97            $media_id = videopress_create_new_media_item( $title, $guid );
98
99            $post_update = array();
100            if ( isset( $media_item['description'] ) && '' !== $media_item['description'] ) {
101                $post_update['post_content'] = sanitize_textarea_field( $media_item['description'] );
102            }
103            if ( isset( $media_item['caption'] ) && '' !== $media_item['caption'] ) {
104                $post_update['post_excerpt'] = sanitize_textarea_field( $media_item['caption'] );
105            }
106            if ( $post_update ) {
107                $post_update['ID'] = $media_id;
108                wp_update_post( $post_update );
109            }
110
111            wp_update_attachment_metadata(
112                $media_id,
113                array(
114                    'original' => array(
115                        'url' => $media_item['url'],
116                    ),
117                )
118            );
119
120            $media_item['post'] = get_post( $media_id );
121        }
122
123        return array( 'media' => $media );
124    }
125
126    /**
127     * Update VideoPress metadata for a media item.
128     *
129     * @param array $request Media item to update.
130     *
131     * @return bool
132     */
133    public function update_videopress_media_item( $request ) {
134        $this->authenticate_user();
135
136        $id     = $request['post_id'];
137        $status = $request['status'];
138        $format = $request['format'];
139        $info   = $request['info'];
140
141        $attachment = get_post( $id );
142        if ( ! $attachment ) {
143            return false;
144        }
145
146        $attachment->guid           = $info['original'];
147        $attachment->post_mime_type = 'video/videopress';
148
149        wp_update_post( $attachment );
150
151        // Update the vp guid and set it to a direct meta property.
152        update_post_meta( $id, 'videopress_guid', $info['guid'] );
153
154        $meta = wp_get_attachment_metadata( $id );
155
156        $meta['width']             = $info['width'];
157        $meta['height']            = $info['height'];
158        $meta['original']['url']   = $info['original'];
159        $meta['videopress']        = $info;
160        $meta['videopress']['url'] = 'https://videopress.com/v/' . $info['guid'];
161
162        // Update file statuses
163        if ( ! empty( $format ) ) {
164            $meta['file_statuses'][ $format ] = $status;
165        }
166
167        if ( ! get_post_meta( $id, '_thumbnail_id', true ) ) {
168            // Update the poster in the VideoPress info.
169            $thumbnail_id = videopress_download_poster_image( $info['poster'], $id );
170
171            if ( is_int( $thumbnail_id ) ) {
172                update_post_meta( $id, '_thumbnail_id', $thumbnail_id );
173            }
174        }
175
176        wp_update_attachment_metadata( $id, $meta );
177
178        videopress_update_meta_data( $id );
179
180        // update the meta to tell us that we're processing or complete
181        update_post_meta( $id, 'videopress_status', videopress_is_finished_processing( $id ) ? 'complete' : 'processing' );
182
183        return true;
184    }
185
186    /**
187     * Update poster image for a VideoPress media item.
188     *
189     * @param array $request The media item to update.
190     * @return bool
191     */
192    public function update_poster_image( $request ) {
193        $this->authenticate_user();
194
195        $post_id = $request['post_id'];
196        $poster  = $request['poster'];
197
198        $attachment = get_post( $post_id );
199        if ( ! $attachment ) {
200            return false;
201        }
202
203        $poster = apply_filters( 'jetpack_photon_url', $poster );
204
205        $meta                         = wp_get_attachment_metadata( $post_id );
206        $meta['videopress']['poster'] = $poster;
207        wp_update_attachment_metadata( $post_id, $meta );
208
209        // Update the poster in the VideoPress info.
210        $thumbnail_id = videopress_download_poster_image( $poster, $post_id );
211
212        if ( ! is_int( $thumbnail_id ) ) {
213            return false;
214        }
215
216        update_post_meta( $post_id, '_thumbnail_id', $thumbnail_id );
217
218        return true;
219    }
220
221    /**
222     * Check if the XML-RPC request is signed by a user token, and authenticate the user in WordPress.
223     *
224     * @return bool
225     */
226    private function authenticate_user() {
227        if ( $this->current_user ) {
228            wp_set_current_user( $this->current_user->ID );
229
230            return true;
231        }
232
233        return false;
234    }
235}