Completed
Push — add/react-videopress-settings ( a1eeb3 )
by
unknown
280:22 queued 269:01
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
	 * A can of coke
204
	 *
205
	 * Similar to current_user_can, but internal to VideoPress. Returns
206
	 * true if the given VideoPress capability is allowed by the given user.
207
	 */
208
	function can( $cap, $user_id = false ) {
209
		if ( ! $user_id )
210
			$user_id = get_current_user_id();
211
212
		// Connection owners are allowed to do all the things.
213
		if ( $this->is_connection_owner( $user_id ) )
214
			return true;
215
216
		/**
217
		 * The access setting can be set by the connection owner, to allow sets
218
		 * of operations to other site users. Each access value corresponds to
219
		 * an array of things they can do.
220
		 */
221
222
		$options = $this->get_options();
223
		$map = array(
224
			'read'   => array( 'read_videos' ),
225
			'edit'   => array( 'read_videos', 'edit_videos' ),
226
			'delete' => array( 'read_videos', 'edit_videos', 'delete_videos' ),
227
		);
228
229
		if ( ! array_key_exists( $options['access'], $map ) )
230
			return false;
231
232
		if ( ! in_array( $cap, $map[ $options['access'] ] ) && 'upload_videos' != $cap )
233
			return false;
234
235
		// Additional and intrenal caps checks
236
237
		if ( ! user_can( $user_id, 'upload_files' ) )
238
			return false;
239
240
		if ( 'edit_videos' == $cap && ! user_can( $user_id, 'edit_others_posts' ) )
241
			return false;
242
243
		if ( 'delete_videos' == $cap && ! user_can( $user_id, 'delete_others_posts' ) )
244
			return false;
245
246
		if ( 'upload_videos' == $cap && ! $options['allow-upload'] )
247
			return false;
248
249
		return true;
250
	}
251
252
	/**
253
	 * Returns true if the provided user is the Jetpack connection owner.
254
	 */
255
	function is_connection_owner( $user_id = false ) {
256
		if ( ! $user_id )
257
			$user_id = get_current_user_id();
258
259
		$user_token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER );
260
		return $user_token && is_object( $user_token ) && isset( $user_token->external_user_id ) && $user_id === $user_token->external_user_id;
261
	}
262
263
	/**
264
	 * Our custom AJAX callback for the query-attachments action
265
	 * used in the media modal. By-passed if not for VideoPress.
266
	 */
267
	function wp_ajax_query_attachments() {
268
269
		// Watch for VideoPress calls
270
		if ( ! isset( $_POST['query']['videopress'] ) )
271
			return;
272
273
		if ( ! $this->can( 'read_videos' ) )
274
			return wp_send_json_error( 'permission denied' );
275
276
		// Get and sanitize query arguments.
277
		$query_args = $this->sanitize_wp_query_args( $_POST['query'] );
278
279
		// Fire a remote WP_Query
280
		$result = $this->query( 'jetpack.vpQuery', $query_args );
281
282
		if ( is_wp_error( $result ) )
283
			return wp_send_json_error( 'xml rpc request error' );
284
285
		$items = $result;
286
287
		foreach ( $items as $key => $item ) {
288
289
			// Check local permissions
290
			if ( ! $this->can( 'edit_videos' ) )
291
				unset( $item['vp_nonces']['update'] );
292
293
			if ( ! $this->can( 'delete_videos' ) )
294
				unset( $item['vp_nonces']['delete'] );
295
296
			// Add a second pair of nonces for the .org blog.
297
			$item['nonces'] = array();
298 View Code Duplication
			if ( ! empty( $item['vp_nonces']['update'] ) )
299
				$item['nonces']['update'] = wp_create_nonce( 'update-videopress-post_' . $item['id'] );
300
301 View Code Duplication
			if ( ! empty( $item['vp_nonces']['delete'] ) )
302
				$item['nonces']['delete'] = wp_create_nonce( 'delete-videopress-post_' . $item['id'] );
303
304
			$item['vp_embed'] = videopress_shortcode_callback( array(
305
				$item['vp_guid'],
306
				'autoplay' => true,
307
				'flashonly' => true,
308
				'w' => 440,
309
			) );
310
311
			$items[ $key ] = $item;
312
		}
313
314
		wp_send_json_success( $items );
315
	}
316
317
	/**
318
	 * Sanitize user-provided WP_Query arguments
319
	 *
320
	 * These might be sent to the VideoPress server, for a remote WP_Query
321
	 * call so let's make sure they're sanitized and safe to send.
322
	 */
323
	function sanitize_wp_query_args( $args ) {
324
		$args = shortcode_atts( array(
325
			'posts_per_page' => 40,
326
			'orderby' => 'date',
327
			'order' => 'desc',
328
			'paged' => 1,
329
			's' => '',
330
		), (array) $args, 'wpvideo' );
331
332
		$args['posts_per_page'] = absint( $args['posts_per_page'] );
333
334
		$args['orderby'] = strtolower( $args['orderby'] );
335
		$args['orderby'] = ( in_array( $args['orderby'], array( 'date' ) ) ) ? $args['orderby'] : 'date';
336
337
		$args['order'] = strtolower( $args['order'] );
338
		$args['order'] = ( in_array( $args['order'], array( 'asc', 'desc' ) ) ) ? $args['order'] : 'desc';
339
340
		$args['paged'] = absint( $args['paged'] );
341
		$args['s'] = sanitize_text_field( $args['s'] );
342
		return $args;
343
	}
344
345
	/**
346
	 * Custom AJAX callback for the save-attachment action. If the request was
347
	 * not for a VideoPress object, core's fallback action will kick in.
348
	 */
349
	function wp_ajax_save_attachment() {
350
		if ( ! isset( $_POST['is_videopress'] ) )
351
			return;
352
353
		if ( ! $this->can( 'edit_videos' ) )
354
			return wp_send_json_error( 'permission denied' );
355
356
		$post_id = 0;
357 View Code Duplication
		if ( ! isset( $_POST['id'] ) || ! $post_id = absint( $_POST['id'] ) )
358
			wp_send_json_error();
359
360
		if ( ! isset( $_POST['vp_nonces']['update'] ) )
361
			wp_send_json_error();
362
363
		check_ajax_referer( 'update-videopress-post_' . $post_id, 'nonce' );
364
365
		$changes = ( ! empty( $_POST['changes'] ) ) ? (array) $_POST['changes'] : array();
366
		$changes = shortcode_atts( array(
367
			'title' => null,
368
			'caption' => null,
369
			'description' => null,
370
371
			'vp_share' => null,
372
			'vp_rating' => null,
373
		), $changes, 'wpvideo' );
374
375
		if ( ! is_null( $changes['vp_share'] ) )
376
			$changes['vp_share'] = (bool) $changes['vp_share'];
377
378
		if ( ! is_null( $changes['vp_rating'] ) )
379
			$changes['vp_rating'] = ( array_key_exists( $changes['vp_rating'], $this->get_available_ratings() ) ) ? $changes['vp_rating'] : null;
380
381
		// Remove null-values
382
		foreach ( $changes as $key => $value )
383
			if ( is_null( $value ) )
384
				unset( $changes[ $key ] );
385
386
		$result = $this->query( 'jetpack.vpSaveAttachment', array(
387
			'post_id' => $post_id,
388
			'changes' => $changes,
389
			'nonce' => $_POST['vp_nonces']['update'],
390
		) );
391
392
		if ( is_wp_error( $result ) )
393
			return wp_send_json_error( 'xml rpc request error' );
394
395
		wp_send_json_success();
396
	}
397
398
	/**
399
	 * Custom AJAX callback for the delete-post action, only for VideoPress objects.
400
	 */
401
	function wp_ajax_delete_post() {
402
		if ( ! isset( $_POST['is_videopress'] ) )
403
			return;
404
405
		if ( ! $this->can( 'delete_videos' ) )
406
			return wp_send_json_error( 'permission denied' );
407
408
		$post_id = 0;
409 View Code Duplication
		if ( ! isset( $_POST['id'] ) || ! $post_id = absint( $_POST['id'] ) )
410
			wp_send_json_error();
411
412
		if ( ! isset( $_POST['vp_nonces']['delete'] ) )
413
			wp_send_json_error();
414
415
		check_ajax_referer( 'delete-videopress-post_' . $post_id );
416
417
		$result = $this->query( 'jetpack.vpDeleteAttachment', array(
418
			'post_id' => $post_id,
419
			'nonce' => $_POST['vp_nonces']['delete'],
420
		) );
421
422
		if ( is_wp_error( $result ) )
423
			return wp_send_json_error( 'xml rpc request error' );
424
425
		wp_send_json_success();
426
	}
427
428
	/**
429
	 * Register VideoPress admin scripts.
430
	 */
431
	function enqueue_admin_scripts() {
432
		if ( did_action( 'videopress_enqueue_admin_scripts' ) )
433
			return;
434
435
		wp_enqueue_script( 'videopress-admin', plugins_url( 'js/videopress-admin.js', __FILE__ ), array( 'jquery', 'media-views', 'media-models' ), $this->version );
436
		wp_enqueue_style( 'videopress-admin', plugins_url( 'videopress-admin.css', __FILE__ ), array(), $this->version );
437
438
		$caps = array();
439 View Code Duplication
		foreach( array( 'read_videos', 'edit_videos', 'delete_videos', 'upload_videos' ) as $cap )
440
			$caps[ $cap ] = $this->can( $cap );
441
442
		$l10n = array(
443
			'selectVideoFile' => __( 'Please select a video file to upload.', 'jetpack' ),
444
			'videoUploading' => __( 'Your video is uploading... Please do not close this window.', 'jetpack' ),
445
			'unknownError' => __( 'An unknown error has occurred. Please try again later.', 'jetpack' ),
446
			'videoUploaded' => __( 'Your video has successfully been uploaded. It will appear in your VideoPress Library shortly.', 'jetpack' ),
447
			'VideoPressLibraryRouter' => __( 'VideoPress Library', 'jetpack' ),
448
			'uploadVideoRouter' => __( 'Upload a Video', 'jetpack' ),
449
			'insertVideoButton' => __( 'Insert Video', 'jetpack' ),
450
		);
451
452
		wp_localize_script( 'videopress-admin', 'VideoPressAdminSettings', array(
453
			'caps' => $caps,
454
			'l10n' => $l10n,
455
		) );
456
		
457
		/**
458
		 * Fires after VideoPress scripts are enqueued in the dashboard.
459
		 *
460
		 * @since 2.5.0
461
		 */
462
		do_action( 'videopress_enqueue_admin_scripts' );
463
	}
464
465
	/**
466
	 * Get an array of available ratings. Keys are options, values are labels.
467
	 */
468
	function get_available_ratings() {
469
		return array(
470
			'G'     => 'G',
471
			'PG-13' => 'PG-13',
472
			'R-17'  => 'R',
473
			'X-18'  => 'X',
474
		);
475
	}
476
477
	/**
478
	 * Additional VideoPress media templates.
479
	 */
480
	function print_media_templates() {
481
		$options = $this->get_options();
482
		?>
483
		<script type="text/html" id="tmpl-videopress-attachment">
484
			<# if ( data.vp_ogg_url ) { #>
485
			<label class="setting vp-setting">
486
				<span><?php _e( 'Ogg File URL', 'jetpack' ); ?></span>
487
				<input type="text" value="{{ data.vp_ogg_url }}" onclick="this.focus();this.select();" readonly />
488
				<p class="help"><?php _e( 'Location of the Ogg video file.', 'jetpack' ); ?></p>
489
			</label>
490
			<# } #>
491
492
			<label class="setting vp-setting">
493
				<span><?php _e( 'Share', 'jetpack' ); ?></span>
494
				<input class="vp-checkbox" type="checkbox" <# if ( '1' === data.vp_share ) { #>checked<# } #> <# if ( ! data.can.save ) { #>disabled<# } #> />
495
				<label>
496
					<?php _e( 'Display share menu and allow viewers to embed or download this video', 'jetpack' ); ?>
497
				</label>
498
				<input class="vp-checkbox-text" type="text" value="{{ data.vp_share }}" data-setting="vp_share" style="display:none;" />
499
			</label>
500
501
			<label class="setting vp-setting">
502
				<span><?php _e( 'Rating', 'jetpack' ); ?></span>
503
504
				<?php foreach ( $this->get_available_ratings() as $value => $label ) : ?>
505
				<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 ); ?>"
506
					<# if ( '<?php echo esc_attr( $value ); ?>' === data.vp_rating ) { #>checked<# } #>
507
					<# if ( ! data.can.save ) { #>disabled<# } #> />
508
				<label for="vp-rating-<?php echo sanitize_html_class( $value ); ?>"><?php echo esc_html( $label ); ?></label>
509
				<?php endforeach; ?>
510
511
				<input class="vp-radio-text" type="text" value="{{ data.vp_rating }}" data-setting="vp_rating" style="display:none;" />
512
			</label>
513
514
			<label class="setting vp-setting">
515
				<span><?php _e( 'Shortcode', 'jetpack' ); ?></span>
516
				<input type="text" value="[wpvideo {{ data.vp_guid }}]" onclick="this.focus();this.select();" readonly />
517
			</label>
518
519
			<label class="setting vp-setting vp-preview">
520
				<span><?php _e( 'Preview', 'jetpack' ); ?></span>
521
				<# if ( ! data.vp_thumbnail_url ) { #>
522
					<span class="videopress-preview-unavailable"><?php esc_html_e( 'The preview is unavailable while this video is being processed.', 'jetpack' ); ?></span>
523
				<# } else { #>
524
				<a href="#" class="videopress-preview" id="videopress-thumbnail-{{ data.vp_guid }}" data-videopress-guid="{{ data.vp_guid }}"><img src="{{ data.vp_thumbnail_url }}" /></a>
525
				<# } #>
526
			</label>
527
		</script>
528
529
		<script type="text/html" id="tmpl-videopress-media-modal">
530
			<div class="videopress-modal">
531
				<p><?php _e( 'Video Preview:', 'jetpack' ); ?></p>
532
				<div class="videopress-video-container">{{{ data.video }}}</div>
533
				<p class="submit">
534
					<a class="videopress-modal-close button" href="#"><?php _e( 'Close', 'jetpack' ); ?></a>
535
				</p>
536
			</div>
537
			<div class="videopress-modal-backdrop"></div>
538
		</script>
539
540
		<script type="text/html" id="tmpl-videopress-uploader">
541
			<div class="videopress-errors"></div>
542
			<form class="videopress-upload-form" action="" method="post" target="videopress_upload_frame" enctype="multipart/form-data">
543
				<input type="hidden" name="action" value="videopress_upload" />
544
				<input type="hidden" name="videopress_blog_id" value="0" />
545
				<input type="hidden" name="videopress_token" value="0" />
546
				<?php $formats = 'ogv, mp4, m4v, mov, wmv, avi, mpg, 3gp, 3g2'; ?>
547
				<?php
548
					$max_upload_size = 0;
549
					if ( ! empty( $options['meta']['max_upload_size'] ) )
550
						$max_upload_size = absint( $options['meta']['max_upload_size'] );
551
552
					$upload_size_unit = $max_upload_size;
553
					$byte_sizes = array( 'KB', 'MB', 'GB' );
554
555
					for ( $u = -1; $upload_size_unit > 1024 && $u < count( $byte_sizes ) - 1; $u++ )
556
						$upload_size_unit /= 1024;
557
558
					if ( $u < 0 ) {
559
						$upload_size_unit = 0;
560
						$u = 0;
561
					} else {
562
						$upload_size_unit = (int) $upload_size_unit;
563
					}
564
				?>
565
				<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>
566
567
				<input type="file" name="videopress_file" />
568
				<?php submit_button( __( 'Upload Video', 'jetpack' ) ); ?>
569
			</form>
570
			<iframe width="0" height="0" name="videopress_upload_frame"></iframe>
571
		</script>
572
		<?php
573
	}
574
575
	/**
576
	 * Filters the VideoPress shortcode options, makes sure that
577
	 * the settings set in Jetpack's VideoPress module are applied.
578
	 */
579
	function videopress_shortcode_options( $options ) {
580
		$videopress_options = $this->get_options();
581
582
		if ( false === $options['freedom'] )
583
			$options['freedom'] = $videopress_options['freedom'];
584
585
		$options['hd'] = $videopress_options['hd'];
586
587
		return $options;
588
	}
589
590
	/**
591
	 * Adds additional methods the WordPress xmlrpc API for handling VideoPress specific features
592
	 *
593
	 * @param array $methods
594
	 * @return array
595
	 */
596
	public function xmlrpc_methods( $methods ) {
597
598
		$methods['jetpack.createMediaItem'] = array( $this, 'xmlrpc_create_media_item' );
599
		$methods['jetpack.updateVideoPressInfo'] = array( $this, 'xmlrpc_update_videopress_info' );
600
601
		return $methods;
602
	}
603
604
	/**
605
	 * Endpoint to allow the transcoding session to send updated information about the VideoPress video when it completes a stage of transcoding.
606
	 *
607
	 * @param array $vp_info
608
	 *
609
	 * @return array|bool
610
	 */
611
	public function xmlrpc_update_videopress_info( $vp_info ) {
612
613
		$errors = null;
614
		foreach ( $vp_info as $vp_item ) {
615
			$id = $vp_item['post_id'];
616
			$guid = $vp_item['guid'];
617
618
			$post = get_post( $id );
619
620
			if ( ! $post ) {
621
				$errors[] = array(
622
					'id' => $id,
623
					'error' => 'Post not found',
624
				);
625
626
				continue;
627
			}
628
629
			$post->guid = $vp_item['original'];
630
			$post->file = $vp_item['original'];
631
632
			wp_update_post( $post );
633
634
			// Update the vp guid and set it to a dirrect meta property.
635
			update_post_meta( $id, 'videopress_guid', $guid );
636
637
			$meta = wp_get_attachment_metadata( $post->ID );
638
			$meta['width'] = $vp_item['width'];
639
			$meta['height'] = $vp_item['height'];
640
			$meta['original']['url'] = $vp_item['original'];
641
			$meta['videopress'] = $vp_item;
642
			$meta['videopress']['url'] = 'https://videopress.com/v/' . $guid;
643
644
			// 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...
645
646
			wp_update_attachment_metadata( $post->ID, $meta );
647
		}
648
649
		if ( count( $errors ) > 0 ) {
650
			return array( 'errors' => $errors );
651
652
		} else {
653
			return true;
654
		}
655
	}
656
657
	/**
658
	 * This is used by the WPCOM VideoPress uploader in order to create a media item with
659
	 * specific meta data about an uploaded file. After this, the transcoding session will
660
	 * update the meta information via the xmlrpc_update_videopress_info() method.
661
	 *
662
	 * Note: This method technically handles the creation of multiple media objects, though
663
	 * in practice this is never done.
664
	 *
665
	 * @param array $media
666
	 *
667
	 * @return array
668
	 */
669
	public function xmlrpc_create_media_item( $media ) {
670
		$created_items = array();
671
672
		foreach ( $media as $media_item ) {
673
			$post = array(
674
				'post_type'   => 'attachment',
675
				'post_mime_type' => 'video/videopress',
676
				'post_title' => sanitize_title( basename( $media_item['url'] ) ),
677
				'post_content' => '',
678
			);
679
680
			$media_id = wp_insert_post( $post );
681
682
			wp_update_attachment_metadata( $media_id, array(
683
				'original' => array(
684
					'url' => $media_item['url'],
685
					'file' => $media_item['file'],
686
					'mime_type' => $media_item['type'],
687
				),
688
			) );
689
690
			$created_items[] = array(
691
				'id' => $media_id,
692
				'post' => get_post( $media_id ),
693
			);
694
		}
695
696
		return array( 'media' => $created_items );
697
	}
698
699
}
700
701
// Initialize the module.
702
Jetpack_VideoPress::init();
703