Completed
Push — fix/4.4.1-typos ( 3db521 )
by
unknown
25s
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

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
39
	/**
40
	 * Adds additional methods the WordPress xmlrpc API for handling VideoPress specific features
41
	 *
42
	 * @param array $methods
43
	 *
44
	 * @return array
45
	 */
46
	public function xmlrpc_methods( $methods ) {
47
48
		$methods['jetpack.createMediaItem']      = array( $this, 'create_media_item' );
49
		$methods['jetpack.updateVideoPressInfo'] = array( $this, 'update_videopress_info' );
50
51
		return $methods;
52
	}
53
54
	/**
55
	 * Endpoint to allow the transcoding session to send updated information about the VideoPress video when it completes a stage of transcoding.
56
	 *
57
	 * @param array $vp_info
58
	 *
59
	 * @return array|bool
60
	 */
61
	public function update_videopress_info( $vp_info ) {
62
		$errors = null;
63
		foreach ( $vp_info as $vp_item ) {
64
			$id   = $vp_item['post_id'];
65
			$guid = $vp_item['guid'];
66
67
			$attachment = get_post( $id );
68
69
			if ( ! $attachment ) {
70
				$errors[] = array(
71
					'id'    => $id,
72
					'error' => 'Post not found',
73
				);
74
75
				continue;
76
			}
77
78
			$attachment->guid = $vp_item['original'];
79
			$attachment->file = $vp_item['original'];
80
81
			wp_update_post( $attachment );
82
83
			// Update the vp guid and set it to a direct meta property.
84
			update_post_meta( $id, 'videopress_guid', $guid );
85
86
			$meta = wp_get_attachment_metadata( $attachment->ID );
87
88
			$current_poster = get_post_meta( $id, '_thumbnail_id' );
89
90
			$meta['width']             = $vp_item['width'];
91
			$meta['height']            = $vp_item['height'];
92
			$meta['original']['url']   = $vp_item['original'];
93
			$meta['videopress']        = $vp_item;
94
			$meta['videopress']['url'] = 'https://videopress.com/v/' . $guid;
95
96
			if ( ! $current_poster && isset( $vp_item['poster'] ) && ! empty( $vp_item['poster'] ) ) {
97
				$thumbnail_id = videopress_download_poster_image( $vp_item['poster'], $id );
98
				update_post_meta( $id, '_thumbnail_id', $thumbnail_id );
99
			}
100
101
			wp_update_attachment_metadata( $attachment->ID, $meta );
102
103
			// update the meta to tell us that we're processing or complete
104
			update_post_meta( $id, 'videopress_status', videopress_is_finished_processing( $attachment->ID ) ? 'complete' : 'processing' );
105
		}
106
107
		if ( count( $errors ) > 0 ) {
108
			return array( 'errors' => $errors );
109
110
		} else {
111
			return true;
112
		}
113
	}
114
115
	/**
116
	 * This is used by the WPCOM VideoPress uploader in order to create a media item with
117
	 * specific meta data about an uploaded file. After this, the transcoding session will
118
	 * update the meta information via the xmlrpc_update_videopress_info() method.
119
	 *
120
	 * Note: This method technically handles the creation of multiple media objects, though
121
	 * in practice this is never done.
122
	 *
123
	 * @param array $media
124
	 *
125
	 * @return array
126
	 */
127
	public function create_media_item( $media ) {
128
		$created_items = array();
129
130
		foreach ( $media as $media_item ) {
131
132
			$media_id = videopress_create_new_media_item( sanitize_title( basename( $media_item['url'] ) ) );
133
134
			wp_update_attachment_metadata( $media_id, array(
135
				'original' => array(
136
					'url' => $media_item['url'],
137
				),
138
			) );
139
140
			$created_items[] = array(
141
				'id'   => $media_id,
142
				'post' => get_post( $media_id ),
143
			);
144
		}
145
146
		return array( 'media' => $created_items );
147
	}
148
149
}
150