Completed
Push — 4.1.0/videopress-remove-legacy... ( 38c1a4 )
by
unknown
386:25 queued 376:13
created

Jetpack_VideoPress::admin_menu()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * VideoPress in Jetpack
4
 *
5
 */
6
class Jetpack_VideoPress {
7
	public $module = 'videopress';
8
	public $option_name = 'videopress';
9
	public $version = 4;
10
11
	/**
12
	 * Singleton
13
	 */
14
	public static function init() {
15
		static $instance = false;
16
17
		if ( ! $instance )
18
			$instance = new Jetpack_VideoPress;
19
20
		return $instance;
21
	}
22
23
	function __construct() {
24
		$this->version = time(); // <s>ghost</s> cache busters!
25
		add_action( 'init', array( $this, 'on_init' ) );
26
		add_action( 'jetpack_activate_module_videopress', array( $this, 'jetpack_module_activated' ) );
27
		add_action( 'jetpack_deactivate_module_videopress', array( $this, 'jetpack_module_deactivated' ) );
28
	}
29
30
	/**
31
	 * Fires on init since is_connection_owner should wait until the user is initialized by $wp->init();
32
	 */
33
	function on_init() {
34
		$options = $this->get_options();
0 ignored issues
show
Unused Code introduced by
$options is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
35
36
		// Only the connection owner can configure this module.
37
		if ( $this->is_connection_owner() ) {
38
			Jetpack::enable_module_configurable( $this->module );
39
			Jetpack::module_configuration_load( $this->module, array( $this, 'jetpack_configuration_load' ) );
40
			Jetpack::module_configuration_screen( $this->module, array( $this, 'jetpack_configuration_screen' ) );
41
		}
42
43
		// Only if the current user can manage the VideoPress library and one has been connected.
44
		if ( $this->can( 'read_videos' ) ) {
45
			add_action( 'wp_enqueue_media', array( $this, 'enqueue_admin_scripts' ) );
46
			add_action( 'print_media_templates', array( $this, 'print_media_templates' ) );
47
48
			// Load these at priority -1 so they're fired before Core's are.
49
			add_action( 'wp_ajax_query-attachments', array( $this, 'wp_ajax_query_attachments' ), -1 );
50
			add_action( 'wp_ajax_save-attachment', array( $this, 'wp_ajax_save_attachment' ), -1 );
51
			add_action( 'wp_ajax_save-attachment-compat', array( $this, 'wp_ajax_save_attachment' ), -1 );
52
			add_action( 'wp_ajax_delete-post', array( $this, 'wp_ajax_delete_post' ), -1 );
53
		}
54
55
		if ( $this->can( 'upload_videos' ) ) {
56
			add_action( 'wp_ajax_videopress-get-upload-token', array( $this, 'wp_ajax_videopress_get_upload_token' ) );
57
		}
58
59
		add_filter( 'videopress_shortcode_options', array( $this, 'videopress_shortcode_options' ) );
60
		add_filter( 'jetpack_xmlrpc_methods', array( $this, 'xmlrpc_methods' ) );
61
	}
62
63
	function wp_ajax_videopress_get_upload_token() {
64
		if ( ! $this->can( 'upload_videos' ) )
65
			return wp_send_json_error();
66
67
		$result = $this->query( 'jetpack.vpGetUploadToken' );
68
		if ( is_wp_error( $result ) )
69
			return wp_send_json_error( array( 'message' => __( 'Could not obtain a VideoPress upload token. Please try again later.', 'jetpack' ) ) );
70
71
		$response = $result;
72
		if ( empty( $response['videopress_blog_id'] ) || empty( $response['videopress_token'] ) || empty( $response[ 'videopress_action_url' ] ) )
73
			return wp_send_json_error( array( 'message' => __( 'Could not obtain a VideoPress upload token. Please try again later.', 'jetpack' ) ) );
74
75
		return wp_send_json_success( $response );
76
	}
77
78
	/**
79
	 * Get VideoPress options
80
	 */
81
	function get_options() {
82
		$defaults = array(
83
			'freedom' => false,
84
			'hd' => false,
85
			'meta' => array(
86
				'max_upload_size' => 0,
87
			),
88
		);
89
90
		$options = Jetpack_Options::get_option( $this->option_name, array() );
91
92
		// If options have not been saved yet, check for older VideoPress plugin options.
93
		if ( empty( $options ) ) {
94
			$options['freedom'] = (bool) get_option( 'video_player_freedom', false );
95
			$options['hd'] = (bool) get_option( 'video_player_high_quality', false );
96
		}
97
98
		$options = array_merge( $defaults, $options );
99
		return $options;
100
	}
101
102
	/**
103
	 * Update VideoPress options
104
	 */
105
	function update_options( $options ) {
106
		Jetpack_Options::update_option( $this->option_name, $options );
107
	}
108
109
	/**
110
	 * Runs when the VideoPress module is activated.
111
	 */
112
	function jetpack_module_activated() {
113
		if ( ! $this->is_connection_owner() )
114
			return;
115
116
		$options = $this->get_options();
117
118
		$this->update_options( $options );
119
	}
120
121
	/**
122
	 * Runs when the VideoPress module is deactivated.
123
	 */
124
	function jetpack_module_deactivated() {
125
		Jetpack_Options::delete_option( $this->option_name );
126
	}
127
128
	/**
129
	 * Remote Query
130
	 *
131
	 * Performs a remote XML-RPC query using Jetpack's IXR Client. And also
132
	 * appends some useful stuff about this setup to the query.
133
	 *
134
	 * @return the Jetpack_IXR_Client object after querying.
135
	 */
136
	function query( $method, $args = null ) {
137
		$options = $this->get_options();
138
		Jetpack::load_xml_rpc_client();
139
		$xml = new Jetpack_IXR_Client( array(
140
			'user_id' => JETPACK_MASTER_USER, // All requests are on behalf of the connection owner.
141
		) );
142
143
		$params = array(
144
			'args' => $args,
145
			'video_blog_id' => $options['blog_id'],
146
			'caps' => array(),
147
		);
148
149
		// Let Jetpack know about our local caps.
150 View Code Duplication
		foreach ( array( 'read_videos', 'edit_videos', 'delete_videos', 'upload_videos' ) as $cap )
151
			if ( $this->can( $cap ) )
152
				$params['caps'][] = $cap;
153
154
		$xml->query( $method, $params );
155
156
		if ( $xml->isError() )
157
			return new WP_Error( 'xml_rpc_error', 'An XML-RPC error has occurred.' );
158
159
		$response = $xml->getResponse();
160
161
		// If there's any metadata with the response, save it for future use.
162
		if ( is_array( $response ) && isset( $response['meta'] ) ) {
163
			$options = $this->get_options();
164
			if ( $response['meta'] !== $options['meta'] ) {
165
				$options['meta'] = array_merge( $options['meta'], $response['meta'] );
166
				$this->update_options( $options );
167
			}
168
		}
169
170
		if ( is_array( $response ) && isset( $response['result'] ) )
171
			return $response['result'];
172
173
		return $response;
174
	}
175
176
	/**
177
	 * Runs before the VideoPress Configuration screen loads, useful
178
	 * to update options and yield errors.
179
	 */
180
	function jetpack_configuration_load() {
181
		$this->enqueue_admin_scripts();
182
183
		/**
184
		 * Save configuration
185
		 */
186
		if ( ! empty( $_POST['action'] ) && $_POST['action'] == 'videopress-save' ) {
187
			check_admin_referer( 'videopress-settings' );
188
			$options = $this->get_options();
189
190
			$options['freedom'] = isset( $_POST['videopress-freedom'] );
191
			$options['hd'] = isset( $_POST['videopress-hd'] );
192
193
			$this->update_options( $options );
194
			Jetpack::state( 'message', 'module_configured' );
195
			wp_safe_redirect( Jetpack::module_configuration_url( $this->module ) );
196
		}
197
	}
198
199
	/**
200
	 * Renders the VideoPress Configuration screen in Jetpack.
201
	 */
202
	function jetpack_configuration_screen() {
203
		$options = $this->get_options();
204
		?>
205
		<div class="narrow">
206
			<form method="post" id="videopress-settings">
207
				<input type="hidden" name="action" value="videopress-save" />
208
				<?php wp_nonce_field( 'videopress-settings' ); ?>
209
210
				<table id="menu" class="form-table">
211
					<tr>
212
						<th scope="row">
213
							<label for="videopress-freedom"><?php _e( 'Free formats', 'jetpack' ); ?></label>
214
						</th>
215
						<td>
216
							<label><input type="checkbox" name="videopress-freedom" id="videopress-freedom" <?php checked( $options['freedom'] ); ?> />
217
								<?php _e( 'Only display videos in free software formats', 'jetpack' ); ?></label>
218
							<p class="description"><?php _e( 'Ogg file container with Theora video and Vorbis audio. Note that some browsers are unable to play free software video formats, including Internet Explorer and Safari.', 'jetpack' ); ?></p>
219
						</td>
220
					</tr>
221
					<tr>
222
						<th scope="row">
223
							<label for="videopress-hd"><?php _e( 'Default quality', 'jetpack' ); ?></label>
224
						</th>
225
						<td>
226
							<label><input type="checkbox" name="videopress-hd" id="videopress-hd" <?php checked( $options['hd'] ); ?> />
227
								<?php _e( 'Display higher quality video by default.', 'jetpack' ); ?></label>
228
							<p class="description"><?php _e( 'This setting may be overridden for individual videos.', 'jetpack' ); ?></p>
229
						</td>
230
					</tr>
231
				</table>
232
233
				<?php submit_button(); ?>
234
			</form>
235
		</div>
236
		<?php
237
	}
238
239
	/**
240
	 * A can of coke
241
	 *
242
	 * Similar to current_user_can, but internal to VideoPress. Returns
243
	 * true if the given VideoPress capability is allowed by the given user.
244
	 */
245
	function can( $cap, $user_id = false ) {
246
		if ( ! $user_id )
247
			$user_id = get_current_user_id();
248
249
		// Connection owners are allowed to do all the things.
250
		if ( $this->is_connection_owner( $user_id ) )
251
			return true;
252
253
		// Additional and internal caps checks
254
255
		if ( ! user_can( $user_id, 'upload_files' ) )
256
			return false;
257
258
		if ( 'edit_videos' == $cap && ! user_can( $user_id, 'edit_others_posts' ) )
259
			return false;
260
261
		if ( 'delete_videos' == $cap && ! user_can( $user_id, 'delete_others_posts' ) )
262
			return false;
263
264
		return true;
265
	}
266
267
	/**
268
	 * Returns true if the provided user is the Jetpack connection owner.
269
	 */
270
	function is_connection_owner( $user_id = false ) {
271
		if ( ! $user_id )
272
			$user_id = get_current_user_id();
273
274
		$user_token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER );
275
		return $user_token && is_object( $user_token ) && isset( $user_token->external_user_id ) && $user_id === $user_token->external_user_id;
276
	}
277
278
	/**
279
	 * Our custom AJAX callback for the query-attachments action
280
	 * used in the media modal. By-passed if not for VideoPress.
281
	 */
282
	function wp_ajax_query_attachments() {
283
284
		// Watch for VideoPress calls
285
		if ( ! isset( $_POST['query']['videopress'] ) )
286
			return;
287
288
		if ( ! $this->can( 'read_videos' ) )
289
			return wp_send_json_error( 'permission denied' );
290
291
		// Get and sanitize query arguments.
292
		$query_args = $this->sanitize_wp_query_args( $_POST['query'] );
293
294
		// Fire a remote WP_Query
295
		$result = $this->query( 'jetpack.vpQuery', $query_args );
296
297
		if ( is_wp_error( $result ) )
298
			return wp_send_json_error( 'xml rpc request error' );
299
300
		$items = $result;
301
302
		foreach ( $items as $key => $item ) {
303
304
			// Check local permissions
305
			if ( ! $this->can( 'edit_videos' ) )
306
				unset( $item['vp_nonces']['update'] );
307
308
			if ( ! $this->can( 'delete_videos' ) )
309
				unset( $item['vp_nonces']['delete'] );
310
311
			// Add a second pair of nonces for the .org blog.
312
			$item['nonces'] = array();
313 View Code Duplication
			if ( ! empty( $item['vp_nonces']['update'] ) )
314
				$item['nonces']['update'] = wp_create_nonce( 'update-videopress-post_' . $item['id'] );
315
316 View Code Duplication
			if ( ! empty( $item['vp_nonces']['delete'] ) )
317
				$item['nonces']['delete'] = wp_create_nonce( 'delete-videopress-post_' . $item['id'] );
318
319
			$item['vp_embed'] = videopress_shortcode_callback( array(
320
				$item['vp_guid'],
321
				'autoplay' => true,
322
				'flashonly' => true,
323
				'w' => 440,
324
			) );
325
326
			$items[ $key ] = $item;
327
		}
328
329
		wp_send_json_success( $items );
330
	}
331
332
	/**
333
	 * Sanitize user-provided WP_Query arguments
334
	 *
335
	 * These might be sent to the VideoPress server, for a remote WP_Query
336
	 * call so let's make sure they're sanitized and safe to send.
337
	 */
338
	function sanitize_wp_query_args( $args ) {
339
		$args = shortcode_atts( array(
340
			'posts_per_page' => 40,
341
			'orderby' => 'date',
342
			'order' => 'desc',
343
			'paged' => 1,
344
			's' => '',
345
		), (array) $args, 'wpvideo' );
346
347
		$args['posts_per_page'] = absint( $args['posts_per_page'] );
348
349
		$args['orderby'] = strtolower( $args['orderby'] );
350
		$args['orderby'] = ( in_array( $args['orderby'], array( 'date' ) ) ) ? $args['orderby'] : 'date';
351
352
		$args['order'] = strtolower( $args['order'] );
353
		$args['order'] = ( in_array( $args['order'], array( 'asc', 'desc' ) ) ) ? $args['order'] : 'desc';
354
355
		$args['paged'] = absint( $args['paged'] );
356
		$args['s'] = sanitize_text_field( $args['s'] );
357
		return $args;
358
	}
359
360
	/**
361
	 * Custom AJAX callback for the save-attachment action. If the request was
362
	 * not for a VideoPress object, core's fallback action will kick in.
363
	 */
364
	function wp_ajax_save_attachment() {
365
		if ( ! isset( $_POST['is_videopress'] ) )
366
			return;
367
368
		if ( ! $this->can( 'edit_videos' ) )
369
			return wp_send_json_error( 'permission denied' );
370
371
		$post_id = 0;
372 View Code Duplication
		if ( ! isset( $_POST['id'] ) || ! $post_id = absint( $_POST['id'] ) )
373
			wp_send_json_error();
374
375
		if ( ! isset( $_POST['vp_nonces']['update'] ) )
376
			wp_send_json_error();
377
378
		check_ajax_referer( 'update-videopress-post_' . $post_id, 'nonce' );
379
380
		$changes = ( ! empty( $_POST['changes'] ) ) ? (array) $_POST['changes'] : array();
381
		$changes = shortcode_atts( array(
382
			'title' => null,
383
			'caption' => null,
384
			'description' => null,
385
386
			'vp_share' => null,
387
			'vp_rating' => null,
388
		), $changes, 'wpvideo' );
389
390
		if ( ! is_null( $changes['vp_share'] ) )
391
			$changes['vp_share'] = (bool) $changes['vp_share'];
392
393
		if ( ! is_null( $changes['vp_rating'] ) )
394
			$changes['vp_rating'] = ( array_key_exists( $changes['vp_rating'], $this->get_available_ratings() ) ) ? $changes['vp_rating'] : null;
395
396
		// Remove null-values
397
		foreach ( $changes as $key => $value )
398
			if ( is_null( $value ) )
399
				unset( $changes[ $key ] );
400
401
		$result = $this->query( 'jetpack.vpSaveAttachment', array(
402
			'post_id' => $post_id,
403
			'changes' => $changes,
404
			'nonce' => $_POST['vp_nonces']['update'],
405
		) );
406
407
		if ( is_wp_error( $result ) )
408
			return wp_send_json_error( 'xml rpc request error' );
409
410
		wp_send_json_success();
411
	}
412
413
	/**
414
	 * Custom AJAX callback for the delete-post action, only for VideoPress objects.
415
	 */
416
	function wp_ajax_delete_post() {
417
		if ( ! isset( $_POST['is_videopress'] ) )
418
			return;
419
420
		if ( ! $this->can( 'delete_videos' ) )
421
			return wp_send_json_error( 'permission denied' );
422
423
		$post_id = 0;
424 View Code Duplication
		if ( ! isset( $_POST['id'] ) || ! $post_id = absint( $_POST['id'] ) )
425
			wp_send_json_error();
426
427
		if ( ! isset( $_POST['vp_nonces']['delete'] ) )
428
			wp_send_json_error();
429
430
		check_ajax_referer( 'delete-videopress-post_' . $post_id );
431
432
		$result = $this->query( 'jetpack.vpDeleteAttachment', array(
433
			'post_id' => $post_id,
434
			'nonce' => $_POST['vp_nonces']['delete'],
435
		) );
436
437
		if ( is_wp_error( $result ) )
438
			return wp_send_json_error( 'xml rpc request error' );
439
440
		wp_send_json_success();
441
	}
442
443
	/**
444
	 * Register VideoPress admin scripts.
445
	 */
446
	function enqueue_admin_scripts() {
447
		if ( did_action( 'videopress_enqueue_admin_scripts' ) )
448
			return;
449
450
		wp_enqueue_script( 'videopress-admin', plugins_url( 'js/videopress-admin.js', __FILE__ ), array( 'jquery', 'media-views', 'media-models' ), $this->version );
451
		wp_enqueue_style( 'videopress-admin', plugins_url( 'videopress-admin.css', __FILE__ ), array(), $this->version );
452
453
		$caps = array();
454 View Code Duplication
		foreach( array( 'read_videos', 'edit_videos', 'delete_videos', 'upload_videos' ) as $cap )
455
			$caps[ $cap ] = $this->can( $cap );
456
457
		$l10n = array(
458
			'selectVideoFile' => __( 'Please select a video file to upload.', 'jetpack' ),
459
			'videoUploading' => __( 'Your video is uploading... Please do not close this window.', 'jetpack' ),
460
			'unknownError' => __( 'An unknown error has occurred. Please try again later.', 'jetpack' ),
461
			'videoUploaded' => __( 'Your video has successfully been uploaded. It will appear in your VideoPress Library shortly.', 'jetpack' ),
462
			'VideoPressLibraryRouter' => __( 'VideoPress Library', 'jetpack' ),
463
			'uploadVideoRouter' => __( 'Upload a Video', 'jetpack' ),
464
			'insertVideoButton' => __( 'Insert Video', 'jetpack' ),
465
466
		);
467
468
		wp_localize_script( 'videopress-admin', 'VideoPressAdminSettings', array(
469
			'caps' => $caps,
470
			'l10n' => $l10n,
471
		) );
472
		
473
		/**
474
		 * Fires after VideoPress scripts are enqueued in the dashboard.
475
		 *
476
		 * @since 2.5.0
477
		 */
478
		do_action( 'videopress_enqueue_admin_scripts' );
479
	}
480
481
	/**
482
	 * Get an array of available ratings. Keys are options, values are labels.
483
	 */
484
	function get_available_ratings() {
485
		return array(
486
			'G' => 'G',
487
			'PG-13' => 'PG-13',
488
			'R-17' => 'R',
489
			'X-18' => 'X',
490
		);
491
	}
492
493
	/**
494
	 * Additional VideoPress media templates.
495
	 */
496
	function print_media_templates() {
497
		$options = $this->get_options();
498
		?>
499
		<script type="text/html" id="tmpl-videopress-attachment">
500
			<# if ( data.vp_ogg_url ) { #>
501
			<label class="setting vp-setting">
502
				<span><?php _e( 'Ogg File URL', 'jetpack' ); ?></span>
503
				<input type="text" value="{{ data.vp_ogg_url }}" onclick="this.focus();this.select();" readonly />
504
				<p class="help"><?php _e( 'Location of the Ogg video file.', 'jetpack' ); ?></p>
505
			</label>
506
			<# } #>
507
508
			<label class="setting vp-setting">
509
				<span><?php _e( 'Share', 'jetpack' ); ?></span>
510
				<input class="vp-checkbox" type="checkbox" <# if ( '1' === data.vp_share ) { #>checked<# } #> <# if ( ! data.can.save ) { #>disabled<# } #> />
511
				<label>
512
					<?php _e( 'Display share menu and allow viewers to embed or download this video', 'jetpack' ); ?>
513
				</label>
514
				<input class="vp-checkbox-text" type="text" value="{{ data.vp_share }}" data-setting="vp_share" style="display:none;" />
515
			</label>
516
517
			<label class="setting vp-setting">
518
				<span><?php _e( 'Rating', 'jetpack' ); ?></span>
519
520
				<?php foreach ( $this->get_available_ratings() as $value => $label ) : ?>
521
				<input class="vp-radio" type="radio" name="vp-radio-group" id="vp-rating-<?php echo sanitize_html_class( $value ); ?>" value="<?php echo esc_attr( $value ); ?>"
522
					<# if ( '<?php echo esc_attr( $value ); ?>' === data.vp_rating ) { #>checked<# } #>
523
					<# if ( ! data.can.save ) { #>disabled<# } #> />
524
				<label for="vp-rating-<?php echo sanitize_html_class( $value ); ?>"><?php echo esc_html( $label ); ?></label>
525
				<?php endforeach; ?>
526
527
				<input class="vp-radio-text" type="text" value="{{ data.vp_rating }}" data-setting="vp_rating" style="display:none;" />
528
			</label>
529
530
			<label class="setting vp-setting">
531
				<span><?php _e( 'Shortcode', 'jetpack' ); ?></span>
532
				<input type="text" value="[wpvideo {{ data.vp_guid }}]" onclick="this.focus();this.select();" readonly />
533
			</label>
534
535
			<label class="setting vp-setting vp-preview">
536
				<span><?php _e( 'Preview', 'jetpack' ); ?></span>
537
				<# if ( ! data.vp_thumbnail_url ) { #>
538
					<span class="videopress-preview-unavailable"><?php esc_html_e( 'The preview is unavailable while this video is being processed.', 'jetpack' ); ?></span>
539
				<# } else { #>
540
				<a href="#" class="videopress-preview" id="videopress-thumbnail-{{ data.vp_guid }}" data-videopress-guid="{{ data.vp_guid }}"><img src="{{ data.vp_thumbnail_url }}" /></a>
541
				<# } #>
542
			</label>
543
		</script>
544
545
		<script type="text/html" id="tmpl-videopress-media-modal">
546
			<div class="videopress-modal">
547
				<p><?php _e( 'Video Preview:', 'jetpack' ); ?></p>
548
				<div class="videopress-video-container">{{{ data.video }}}</div>
549
				<p class="submit">
550
					<a class="videopress-modal-close button" href="#"><?php _e( 'Close', 'jetpack' ); ?></a>
551
				</p>
552
			</div>
553
			<div class="videopress-modal-backdrop"></div>
554
		</script>
555
556
		<script type="text/html" id="tmpl-videopress-uploader">
557
			<div class="videopress-errors"></div>
558
			<form class="videopress-upload-form" action="" method="post" target="videopress_upload_frame" enctype="multipart/form-data">
559
				<input type="hidden" name="action" value="videopress_upload" />
560
				<input type="hidden" name="videopress_blog_id" value="0" />
561
				<input type="hidden" name="videopress_token" value="0" />
562
				<?php $formats = 'ogv, mp4, m4v, mov, wmv, avi, mpg, 3gp, 3g2'; ?>
563
				<?php
564
					$max_upload_size = 0;
565
					if ( ! empty( $options['meta']['max_upload_size'] ) )
566
						$max_upload_size = absint( $options['meta']['max_upload_size'] );
567
568
					$upload_size_unit = $max_upload_size;
569
					$byte_sizes = array( 'KB', 'MB', 'GB' );
570
571
					for ( $u = -1; $upload_size_unit > 1024 && $u < count( $byte_sizes ) - 1; $u++ )
572
						$upload_size_unit /= 1024;
573
574
					if ( $u < 0 ) {
575
						$upload_size_unit = 0;
576
						$u = 0;
577
					} else {
578
						$upload_size_unit = (int) $upload_size_unit;
579
					}
580
				?>
581
				<p><?php printf( __( 'Use the form below to upload a video to your VideoPress Library. The following video formats are supported: %s. Maximum upload file size is %d%s.', 'jetpack' ), esc_html( $formats ), esc_html( $upload_size_unit ), esc_html( $byte_sizes[ $u ] ) ); ?></p>
582
583
				<input type="file" name="videopress_file" />
584
				<?php submit_button( __( 'Upload Video', 'jetpack' ) ); ?>
585
			</form>
586
			<iframe width="0" height="0" name="videopress_upload_frame"></iframe>
587
		</script>
588
		<?php
589
	}
590
591
	/**
592
	 * Filters the VideoPress shortcode options, makes sure that
593
	 * the settings set in Jetpack's VideoPress module are applied.
594
	 */
595
	function videopress_shortcode_options( $options ) {
596
		$videopress_options = $this->get_options();
597
598
		if ( false === $options['freedom'] )
599
			$options['freedom'] = $videopress_options['freedom'];
600
601
		$options['hd'] = $videopress_options['hd'];
602
603
		return $options;
604
	}
605
606
	/**
607
	 * Adds additional methods the WordPress xmlrpc API for handling VideoPress specific features
608
	 *
609
	 * @param array $methods
610
	 * @return array
611
	 */
612
	public function xmlrpc_methods( $methods ) {
613
614
		$methods['jetpack.createMediaItem'] = array( $this, 'xmlrpc_create_media_item' );
615
		$methods['jetpack.updateVideoPressInfo'] = array( $this, 'xmlrpc_update_videopress_info' );
616
617
		return $methods;
618
	}
619
620
	/**
621
	 * Endpoint to allow the transcoding session to send updated information about the VideoPress video when it completes a stage of transcoding.
622
	 *
623
	 * @param array $vp_info
624
	 *
625
	 * @return array|bool
626
	 */
627
	public function xmlrpc_update_videopress_info( $vp_info ) {
628
629
		$errors = null;
630
		foreach ( $vp_info as $vp_item ) {
631
			$id = $vp_item['post_id'];
632
			$guid = $vp_item['guid'];
633
634
			$post = get_post( $id );
635
636
			if ( ! $post ) {
637
				$errors[] = array(
638
					'id' => $id,
639
					'error' => 'Post not found',
640
				);
641
642
				continue;
643
			}
644
645
			$post->guid = $vp_item['original'];
646
			$post->file = $vp_item['original'];
647
648
			wp_update_post( $post );
649
650
			// Update the vp guid and set it to a dirrect meta property.
651
			update_post_meta( $id, 'videopress_guid', $guid );
652
653
			$meta = wp_get_attachment_metadata( $post->ID );
654
			$meta['width'] = $vp_item['width'];
655
			$meta['height'] = $vp_item['height'];
656
			$meta['original']['url'] = $vp_item['original'];
657
			$meta['videopress'] = $vp_item;
658
			$meta['videopress']['url'] = 'https://videopress.com/v/' . $guid;
659
660
			// TODO: Add poster updating.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
661
662
			wp_update_attachment_metadata( $post->ID, $meta );
663
		}
664
665
		if ( count( $errors ) > 0 ) {
666
			return array( 'errors' => $errors );
667
668
		} else {
669
			return true;
670
		}
671
	}
672
673
	/**
674
	 * This is used by the WPCOM VideoPress uploader in order to create a media item with
675
	 * specific meta data about an uploaded file. After this, the transcoding session will
676
	 * update the meta information via the xmlrpc_update_videopress_info() method.
677
	 *
678
	 * Note: This method technically handles the creation of multiple media objects, though
679
	 * in practice this is never done.
680
	 *
681
	 * @param array $media
682
	 *
683
	 * @return array
684
	 */
685
	public function xmlrpc_create_media_item( $media ) {
686
		$created_items = array();
687
688
		foreach ( $media as $media_item ) {
689
			$post = array(
690
				'post_type'   => 'attachment',
691
				'post_mime_type' => 'video/videopress',
692
				'post_title' => sanitize_title( basename( $media_item['url'] ) ),
693
				'post_content' => '',
694
			);
695
696
			$media_id = wp_insert_post( $post );
697
698
			wp_update_attachment_metadata( $media_id, array(
699
				'original' => array(
700
					'url' => $media_item['url'],
701
					'file' => $media_item['file'],
702
					'mime_type' => $media_item['type'],
703
				),
704
			) );
705
706
			$created_items[] = array(
707
				'id' => $media_id,
708
				'post' => get_post( $media_id ),
709
			);
710
		}
711
712
		return array( 'media' => $created_items );
713
	}
714
}
715
716
// Initialize the module.
717
Jetpack_VideoPress::init();
718