Completed
Push — update/paid-blocks-class ( b80f1f...3fb233 )
by
unknown
167:57 queued 158:58
created

VideoPress_XMLRPC::authenticate_user()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
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
	 * The current user object.
16
	 *
17
	 * @var WP_User
18
	 */
19
	private $current_user;
20
21
	/**
22
	 * Private VideoPress_XMLRPC constructor.
23
	 *
24
	 * Use the VideoPress_XMLRPC::init() method to get an instance.
25
	 */
26
	private function __construct() {
27
		add_filter( 'jetpack_xmlrpc_methods', array( $this, 'xmlrpc_methods' ), 10, 3 );
28
	}
29
30
	/**
31
	 * Initialize the VideoPress_XMLRPC and get back a singleton instance.
32
	 *
33
	 * @return VideoPress_XMLRPC
34
	 */
35
	public static function init() {
36
		if ( is_null( self::$instance ) ) {
37
			self::$instance = new VideoPress_XMLRPC();
38
		}
39
40
		return self::$instance;
41
	}
42
43
	/**
44
	 * Adds additional methods the WordPress xmlrpc API for handling VideoPress specific features
45
	 *
46
	 * @param array   $methods The Jetpack API methods.
47
	 * @param array   $core_methods The WordPress Core API methods (ignored).
48
	 * @param WP_User $user The user object the API request is signed by.
49
	 *
50
	 * @return array
51
	 */
52
	public function xmlrpc_methods( $methods, $core_methods, $user ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
53
		if ( $user && $user instanceof WP_User ) {
0 ignored issues
show
Bug introduced by
The class WP_User does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
54
			$this->current_user = $user;
55
		}
56
57
		$methods['jetpack.createMediaItem']             = array( $this, 'create_media_item' );
58
		$methods['jetpack.updateVideoPressMediaItem']   = array( $this, 'update_videopress_media_item' );
59
		$methods['jetpack.updateVideoPressPosterImage'] = array( $this, 'update_poster_image' );
60
61
		return $methods;
62
	}
63
64
	/**
65
	 * This is used by the WPCOM VideoPress uploader in order to create a media item with
66
	 * specific meta data about an uploaded file. After this, the transcoding session will
67
	 * update the meta information via the update_videopress_media_item() method.
68
	 *
69
	 * Note: This method technically handles the creation of multiple media objects, though
70
	 * in practice this is never done.
71
	 *
72
	 * @param array $media
73
	 * @return array
74
	 */
75
	public function create_media_item( $media ) {
76
		$this->authenticate_user();
77
78
		foreach ( $media as & $media_item ) {
79
			$title = sanitize_title( basename( $media_item['url'] ) );
80
			$guid  = isset( $media['guid'] ) ? $media['guid'] : null;
81
82
			$media_id = videopress_create_new_media_item( $title, $guid );
83
84
			wp_update_attachment_metadata(
85
				$media_id,
86
				array(
87
					'original' => array(
88
						'url' => $media_item['url'],
89
					),
90
				)
91
			);
92
93
			$media_item['post'] = get_post( $media_id );
94
		}
95
96
		return array( 'media' => $media );
97
	}
98
99
	/**
100
	 * @param array $request
101
	 *
102
	 * @return bool
103
	 */
104
	public function update_videopress_media_item( $request ) {
105
		$this->authenticate_user();
106
107
		$id     = $request['post_id'];
108
		$status = $request['status'];
109
		$format = $request['format'];
110
		$info   = $request['info'];
111
112
		if ( ! $attachment = get_post( $id ) ) {
113
			return false;
114
		}
115
116
		$attachment->guid = $info['original'];
117
118
		wp_update_post( $attachment );
119
120
		// Update the vp guid and set it to a direct meta property.
121
		update_post_meta( $id, 'videopress_guid', $info['guid'] );
122
123
		$meta = wp_get_attachment_metadata( $id );
124
125
		$meta['width']             = $info['width'];
126
		$meta['height']            = $info['height'];
127
		$meta['original']['url']   = $info['original'];
128
		$meta['videopress']        = $info;
129
		$meta['videopress']['url'] = 'https://videopress.com/v/' . $info['guid'];
130
131
		// Update file statuses
132
		$valid_formats = array( 'hd', 'ogg', 'mp4', 'dvd' );
133
		if ( in_array( $format, $valid_formats ) ) {
134
			$meta['file_statuses'][ $format ] = $status;
135
		}
136
137
		if ( ! get_post_meta( $id, '_thumbnail_id', true ) ) {
138
			// Update the poster in the VideoPress info.
139
			$thumbnail_id = videopress_download_poster_image( $info['poster'], $id );
140
141
			if ( is_int( $thumbnail_id ) ) {
142
				update_post_meta( $id, '_thumbnail_id', $thumbnail_id );
143
			}
144
		}
145
146
		wp_update_attachment_metadata( $id, $meta );
147
148
		videopress_update_meta_data( $id );
149
150
		// update the meta to tell us that we're processing or complete
151
		update_post_meta( $id, 'videopress_status', videopress_is_finished_processing( $id ) ? 'complete' : 'processing' );
152
153
		return true;
154
	}
155
156
	/**
157
	 * @param array $request
158
	 * @return bool
159
	 */
160
	public function update_poster_image( $request ) {
161
		$this->authenticate_user();
162
163
		$post_id = $request['post_id'];
164
		$poster  = $request['poster'];
165
166
		if ( ! $attachment = get_post( $post_id ) ) {
167
			return false;
168
		}
169
170
		// We add ssl => 1 to make sure that the videos.files.wordpress.com domain is parsed as photon.
171
		$poster = apply_filters( 'jetpack_photon_url', $poster, array( 'ssl' => 1 ), 'https' );
0 ignored issues
show
Unused Code introduced by
The call to apply_filters() has too many arguments starting with array('ssl' => 1).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
172
173
		$meta                         = wp_get_attachment_metadata( $post_id );
174
		$meta['videopress']['poster'] = $poster;
175
		wp_update_attachment_metadata( $post_id, $meta );
176
177
		// Update the poster in the VideoPress info.
178
		$thumbnail_id = videopress_download_poster_image( $poster, $post_id );
179
180
		if ( ! is_int( $thumbnail_id ) ) {
181
			return false;
182
		}
183
184
		update_post_meta( $post_id, '_thumbnail_id', $thumbnail_id );
185
186
		return true;
187
	}
188
189
	/**
190
	 * Check if the XML-RPC request is signed by a user token, and authenticate the user in WordPress.
191
	 *
192
	 * @return bool
193
	 */
194
	private function authenticate_user() {
195
		if ( $this->current_user ) {
196
			wp_set_current_user( $this->current_user->ID );
197
198
			return true;
199
		}
200
201
		return false;
202
	}
203
}
204