summaryrefslogtreecommitdiff
blob: 72e88c8f4bbbd39207005ef8b655018e7a13dd39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/**
 * VideoPress playback module markup generator.
 *
 * @since 1.3
 */
class VideoPress_XMLRPC {

	/**
	 * @var VideoPress_XMLRPC
	 **/
	private static $instance = null;


	/**
	 * Private VideoPress_XMLRPC constructor.
	 *
	 * Use the VideoPress_XMLRPC::init() method to get an instance.
	 */
	private function __construct() {
		add_filter( 'jetpack_xmlrpc_methods', array( $this, 'xmlrpc_methods' ) );
	}

	/**
	 * Initialize the VideoPress_XMLRPC and get back a singleton instance.
	 *
	 * @return VideoPress_XMLRPC
	 */
	public static function init() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new VideoPress_XMLRPC;
		}

		return self::$instance;
	}

	/**
	 * Adds additional methods the WordPress xmlrpc API for handling VideoPress specific features
	 *
	 * @param array $methods
	 *
	 * @return array
	 */
	public function xmlrpc_methods( $methods ) {

		$methods['jetpack.createMediaItem']           = array( $this, 'create_media_item' );
		$methods['jetpack.updateVideoPressMediaItem'] = array( $this, 'update_videopress_media_item' );
		$methods['jetpack.updateVideoPressPosterImage'] = array( $this, 'update_poster_image' );

		return $methods;
	}

	/**
	 * This is used by the WPCOM VideoPress uploader in order to create a media item with
	 * specific meta data about an uploaded file. After this, the transcoding session will
	 * update the meta information via the update_videopress_media_item() method.
	 *
	 * Note: This method technically handles the creation of multiple media objects, though
	 * in practice this is never done.
	 *
	 * @param array $media
	 * @return array
	 */
	public function create_media_item( $media ) {
		foreach ( $media as & $media_item ) {
			$title = sanitize_title( basename( $media_item['url'] ) );
			$guid  = isset( $media['guid'] ) ? $media['guid'] : null;

			$media_id = videopress_create_new_media_item( $title, $guid );

			wp_update_attachment_metadata( $media_id, array(
				'original' => array(
					'url' => $media_item['url'],
				),
			) );

			$media_item['post'] = get_post( $media_id );
		}

		return array( 'media' => $media );
	}

	/**
	 * @param array $request
	 *
	 * @return bool
	 */
	public function update_videopress_media_item( $request ) {

		$id     = $request['post_id'];
		$status = $request['status'];
		$format = $request['format'];
        $info   = $request['info'];

        if ( ! $attachment = get_post( $id ) )  {
            return false;
        }

		$attachment->guid = $info['original'];

		wp_update_post( $attachment );

		// Update the vp guid and set it to a direct meta property.
		update_post_meta( $id, 'videopress_guid', $info['guid'] );

        $meta = wp_get_attachment_metadata( $id );

        $meta['width']             = $info['width'];
        $meta['height']            = $info['height'];
        $meta['original']['url']   = $info['original'];
		$meta['videopress']        = $info;
		$meta['videopress']['url'] = 'https://videopress.com/v/' . $info['guid'];

        // Update file statuses
		$valid_formats = array( 'hd', 'ogg', 'mp4', 'dvd' );
		if ( in_array( $format, $valid_formats ) ) {
            $meta['file_statuses'][ $format ] = $status;
		}

		if ( ! get_post_meta( $id, '_thumbnail_id', true ) ) {
			// Update the poster in the VideoPress info.
			$thumbnail_id = videopress_download_poster_image( $info['poster'], $id );

			if ( is_int( $thumbnail_id ) ) {
				update_post_meta( $id, '_thumbnail_id', $thumbnail_id );
			}
		}

		wp_update_attachment_metadata( $id, $meta );

		videopress_update_meta_data( $id );

		// update the meta to tell us that we're processing or complete
		update_post_meta( $id, 'videopress_status', videopress_is_finished_processing( $id ) ? 'complete' : 'processing' );

		// Get the attached file and if there isn't one, then let's update it with the one from the server.
		$file = get_attached_file( $id );
		if ( ! $file && is_string( $info['original'] ) ) {
			videopress_download_video( $info['original'], $id );
		}

		return true;
	}

	/**
	 * @param array $request
	 * @return bool
	 */
	public function update_poster_image( $request ) {

		$post_id = $request['post_id'];
		$poster  = $request['poster'];

		if ( ! $attachment = get_post( $post_id ) )  {
			return false;
		}

		// Update the poster in the VideoPress info.
		$thumbnail_id = videopress_download_poster_image( $poster, $post_id );

		if ( !is_int( $thumbnail_id ) ) {
			return false;
		}

		update_post_meta( $post_id, '_thumbnail_id', $thumbnail_id );
		$meta = wp_get_attachment_metadata( $post_id );

		$meta['videopress']['poster'] = $poster;
		wp_update_attachment_metadata( $post_id, $meta );

		return true;
	}
}