Completed
Push — add/feature-rollout ( 16c199...2484fa )
by
unknown
10:53
created

VideoPress_XMLRPC::update_videopress_info()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 53
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 31
nc 8
nop 1
dl 0
loc 53
rs 7.1199
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A VideoPress_XMLRPC::create_media_item() 0 18 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * VideoPress playback module markup generator.
4
 *
5
 * @since 1.3
6
 */
7
class VideoPress_XMLRPC {
8
9
	/**
10
	 * @var VideoPress_XMLRPC
11
	 **/
12
	private static $instance = null;
13
14
15
	/**
16
	 * Private VideoPress_XMLRPC constructor.
17
	 *
18
	 * Use the VideoPress_XMLRPC::init() method to get an instance.
19
	 */
20
	private function __construct() {
21
		add_filter( 'jetpack_xmlrpc_methods', array( $this, 'xmlrpc_methods' ) );
22
	}
23
24
	/**
25
	 * Initialize the VideoPress_XMLRPC and get back a singleton instance.
26
	 *
27
	 * @return VideoPress_XMLRPC
28
	 */
29
	public static function init() {
30
		if ( is_null( self::$instance ) ) {
31
			self::$instance = new VideoPress_XMLRPC;
32
		}
33
34
		return self::$instance;
35
	}
36
37
	/**
38
	 * Adds additional methods the WordPress xmlrpc API for handling VideoPress specific features
39
	 *
40
	 * @param array $methods
41
	 *
42
	 * @return array
43
	 */
44
	public function xmlrpc_methods( $methods ) {
45
46
		$methods['jetpack.createMediaItem']           = array( $this, 'create_media_item' );
47
		$methods['jetpack.updateVideoPressMediaItem'] = array( $this, 'update_videopress_media_item' );
48
		$methods['jetpack.updateVideoPressPosterImage'] = array( $this, 'update_poster_image' );
49
50
		return $methods;
51
	}
52
53
	/**
54
	 * This is used by the WPCOM VideoPress uploader in order to create a media item with
55
	 * specific meta data about an uploaded file. After this, the transcoding session will
56
	 * update the meta information via the update_videopress_media_item() method.
57
	 *
58
	 * Note: This method technically handles the creation of multiple media objects, though
59
	 * in practice this is never done.
60
	 *
61
	 * @param array $media
62
	 * @return array
63
	 */
64
	public function create_media_item( $media ) {
65
		foreach ( $media as & $media_item ) {
66
			$title = sanitize_title( basename( $media_item['url'] ) );
67
			$guid  = isset( $media['guid'] ) ? $media['guid'] : null;
68
69
			$media_id = videopress_create_new_media_item( $title, $guid );
70
71
			wp_update_attachment_metadata( $media_id, array(
72
				'original' => array(
73
					'url' => $media_item['url'],
74
				),
75
			) );
76
77
			$media_item['post'] = get_post( $media_id );
78
		}
79
80
		return array( 'media' => $media );
81
	}
82
83
	/**
84
	 * @param array $request
85
	 *
86
	 * @return bool
87
	 */
88
	public function update_videopress_media_item( $request ) {
89
90
		$id     = $request['post_id'];
91
		$status = $request['status'];
92
		$format = $request['format'];
93
        $info   = $request['info'];
94
95
        if ( ! $attachment = get_post( $id ) )  {
96
            return false;
97
        }
98
99
		$attachment->guid = $info['original'];
100
101
		wp_update_post( $attachment );
102
103
		// Update the vp guid and set it to a direct meta property.
104
		update_post_meta( $id, 'videopress_guid', $info['guid'] );
105
106
        $meta = wp_get_attachment_metadata( $id );
107
108
        $meta['width']             = $info['width'];
109
        $meta['height']            = $info['height'];
110
        $meta['original']['url']   = $info['original'];
111
		$meta['videopress']        = $info;
112
		$meta['videopress']['url'] = 'https://videopress.com/v/' . $info['guid'];
113
114
        // Update file statuses
115
		$valid_formats = array( 'hd', 'ogg', 'mp4', 'dvd' );
116
		if ( in_array( $format, $valid_formats ) ) {
117
            $meta['file_statuses'][ $format ] = $status;
118
		}
119
120
		if ( ! get_post_meta( $id, '_thumbnail_id', true ) ) {
121
			// Update the poster in the VideoPress info.
122
			$thumbnail_id = videopress_download_poster_image( $info['poster'], $id );
123
124
			if ( is_int( $thumbnail_id ) ) {
125
				update_post_meta( $id, '_thumbnail_id', $thumbnail_id );
126
			}
127
		}
128
129
		wp_update_attachment_metadata( $id, $meta );
130
131
		videopress_update_meta_data( $id );
132
133
		// update the meta to tell us that we're processing or complete
134
		update_post_meta( $id, 'videopress_status', videopress_is_finished_processing( $id ) ? 'complete' : 'processing' );
135
136
		// Get the attached file and if there isn't one, then let's update it with the one from the server.
137
		$file = get_attached_file( $id );
138
		if ( ! $file && is_string( $info['original'] ) ) {
139
			videopress_download_video( $info['original'], $id );
140
		}
141
142
		return true;
143
	}
144
145
	/**
146
	 * @param array $request
147
	 * @return bool
148
	 */
149
	public function update_poster_image( $request ) {
150
151
		$post_id = $request['post_id'];
152
		$poster  = $request['poster'];
153
154
		if ( ! $attachment = get_post( $post_id ) )  {
155
			return false;
156
		}
157
158
		// Update the poster in the VideoPress info.
159
		$thumbnail_id = videopress_download_poster_image( $poster, $post_id );
160
161
		if ( !is_int( $thumbnail_id ) ) {
162
			return false;
163
		}
164
165
		update_post_meta( $post_id, '_thumbnail_id', $thumbnail_id );
166
		$meta = wp_get_attachment_metadata( $post_id );
167
168
		$meta['videopress']['poster'] = $poster;
169
		wp_update_attachment_metadata( $post_id, $meta );
170
171
		return true;
172
	}
173
}
174