Completed
Push — add/react-videopress-settings ( 1a2dcb )
by
unknown
18:36 queued 08:26
created

Jetpack_VideoPress::videopress_shortcode_options()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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