Completed
Push — feature/videopress-uploader ( 31b66d...022324 )
by
unknown
09:37
created

Jetpack_VideoPress::make_video_get_path()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 1
eloc 7
c 1
b 1
f 1
nc 1
nop 1
dl 0
loc 9
rs 9.6666
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 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' ) && $options['blog_id'] ) {
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
			add_action( 'wp_ajax_videopress-update-transcoding-status', array( $this, 'wp_ajax_update_transcoding_status' ), -1 );
54
55
			add_action( 'admin_menu', array( $this, 'admin_menu' ) );
56
		}
57
58
		if ( $this->can( 'upload_videos' ) && $options['blog_id'] ) {
59
			add_action( 'wp_ajax_videopress-get-upload-token', array( $this, 'wp_ajax_videopress_get_upload_token' ) );
60
			add_filter( 'plupload_default_settings', array( $this, 'videopress_pluploder_config' ) );
61
		}
62
63
		add_filter( 'videopress_shortcode_options', array( $this, 'videopress_shortcode_options' ) );
64
		add_filter( 'jetpack_xmlrpc_methods', array( $this, 'xmlrpc_methods' ) );
65
		add_filter( 'wp_get_attachment_url', array( $this, 'update_attachment_url_for_videopress' ), 10, 2 );
66
	}
67
68
	/**
69
	 * @param string $url
70
	 * @param int $post_id
71
	 *
72
	 * @return mixed
73
	 */
74
	public function update_attachment_url_for_videopress( $url, $post_id ) {
75
76
		$post = get_post( $post_id );
77
78
		if ( $post->post_mime_type === 'video/videopress' ) {
79
			$meta = wp_get_attachment_metadata( $post->ID );
80
81
			if ( isset( $meta['original']['url'] ) ) {
82
				$url = $meta['original']['url'];
83
			}
84
		}
85
86
		return $url;
87
	}
88
89
	/**
90
	 * Adds additional methods the WordPress xmlrpc API for handling VideoPress specific features
91
	 *
92
	 * @param array $methods
93
	 * @return array
94
	 **/
95
	public function xmlrpc_methods( $methods ) {
96
		$methods['jetpack.createMediaItem'] = array( $this, 'xmlrpc_create_media_item' );
97
		$methods['jetpack.updateVideoPressInfo'] = array( $this, 'xmlrpc_update_videopress_info' );
98
99
		return $methods;
100
	}
101
102
	/**
103
	 * @param array $vp_info
104
	 *
105
	 * @return array|bool
106
	 */
107
	public function xmlrpc_update_videopress_info( $vp_info ) {
108
109
		$errors = null;
110
		foreach ( $vp_info as $vp_item ) {
111
			$id = $vp_item['post_id'];
112
			$guid = $vp_item['guid'];
113
114
			$post = get_post( $id );
115
116
			if ( ! $post ) {
117
				$errors[] = array(
118
					'id' => $id,
119
					'error' => 'Post not found',
120
				);
121
122
				continue;
123
			}
124
125
			$post->guid = $vp_item['original'];
126
			$post->file = $vp_item['original'];
127
128
			wp_update_post( $post );
129
130
			$meta = wp_get_attachment_metadata( $post->ID );
131
			$meta['original']['url'] = $vp_item['original'];
132
			$meta['videopress'] = $vp_item;
133
			$meta['videopress']['url'] = 'https://videopress.com/v/' . $guid;
134
135
			wp_update_attachment_metadata( $post->ID, $meta );
136
		}
137
138
		if ( count( $errors ) > 0 ) {
139
			return array( 'errors' => $errors );
140
141
		} else {
142
			return true;
143
		}
144
	}
145
146
	/**
147
	 * @param array $media
148
	 *
149
	 * @return array
150
	 */
151
	public function xmlrpc_create_media_item( $media ) {
152
		$created_items = array();
153
154
		foreach ( $media as $media_item ) {
155
			$post = array(
156
				'post_type'   => 'attachment',
157
				'post_mime_type' => 'video/videopress',
158
				'post_title' => sanitize_title( basename( $media_item['url'] ) ),
159
				'post_content' => '',
160
			);
161
162
			$media_id = wp_insert_post( $post );
163
164
			wp_update_attachment_metadata( $media_id, array(
165
				'original' => array(
166
					'url' => $media_item['url'],
167
					'file' => $media_item['file'],
168
					'mime_type' => $media_item['type'],
169
				),
170
			) );
171
172
			$created_items[] = array(
173
				'id' => $media_id,
174
				'post' => get_post( $media_id ),
175
			);
176
		}
177
178
		return array( 'media' => $created_items );
179
	}
180
181
	/**
182
	 * @return null
183
	 */
184
	function wp_ajax_videopress_get_upload_token() {
185
		if ( ! $this->can( 'upload_videos' ) ) {
186
			return wp_send_json_error();
187
		}
188
189
		$options = $this->get_options();
190
191
		$args = array(
192
			'method'  => 'POST',
193
		);
194
195
		$endpoint = "sites/{$options['id']}/media/token";
196
		$result = Jetpack_Client::wpcom_json_api_request_as_blog( $endpoint, Jetpack_Client::WPCOM_JSON_API_VERSION, $args );
197
198 View Code Duplication
		if ( is_wp_error( $result ) ) {
199
			return wp_send_json_error( array( 'message' => __( 'Could not obtain a VideoPress upload token. Please try again later.', 'jetpack' ), 'result' => $result ) );
200
		}
201
202
		$response = json_decode( $result['body'], true );
203
204
		if ( empty( $response['upload_blog_id'] ) || empty( $response['upload_token'] ) ) {
205
			return wp_send_json_error( array( 'message' => __( 'Could not obtain a VideoPress upload token. Please try again later.', 'jetpack' ), 'result' => $result ) );
206
		}
207
208
		$response['upload_action_url'] = self::make_media_upload_path( $response['upload_blog_id'] );
209
210
		return wp_send_json_success( $response );
211
	}
212
213
	/**
214
	 * Get the upload api path.
215
	 *
216
	 * @param $blog_id
217
	 * @return string
218
	 */
219
	function make_media_upload_path( $blog_id ) {
220
		return sprintf(
221
			'%s://%s/rest/v%s/videos/%s/new',
222
			'https',
223
			'public-api.wordpress.com', //JETPACK__WPCOM_JSON_API_HOST,
224
			Jetpack_Client::WPCOM_JSON_API_VERSION,
225
			$blog_id
226
		);
227
	}
228
229
	/**
230
	 * Get the video update path
231
	 *
232
	 * @param string $guid
233
	 * @return string
234
	 */
235
	function make_video_get_path( $guid ) {
236
		return sprintf(
237
			'%s://%s/rest/v%s/videos/%s',
238
			'https',
239
			'public-api.wordpress.com', //JETPACK__WPCOM_JSON_API_HOST,
240
			Jetpack_Client::WPCOM_JSON_API_VERSION,
241
			$guid
242
		);
243
	}
244
245
	/**
246
	 * Modify the default plupload config to turn on videopress specific filters.
247
	 */
248
	function videopress_pluploder_config( $config ) {
249
		$config['filters']['videopress_check_uploads'] = 1;
250
		return $config;
251
	}
252
253
	public function wp_ajax_update_transcoding_status() {
254
		return $this->update_transcoding_status();
255
	}
256
257
	public function update_transcoding_status() {
258 View Code Duplication
		if ( ! isset( $_POST['post_id'] ) ) {
259
			return wp_send_json_error( array( 'message' => __( 'A valid post_id is required.', 'jetpack' ) ) );
260
		}
261
262
		$post_id = $_POST['post_id'];
263
		$meta = wp_get_attachment_metadata( $post_id );
264
265
		// If this has not been processed by videopress, we can skip the rest.
266 View Code Duplication
		if ( ! isset( $meta['videopress'] ) ) {
267
			return wp_send_json_error( array( 'message' => __( 'That post does not have a VideoPress video associated to it..', 'jetpack' ) ) );
268
		}
269
270
		$info = (object) $meta['videopress'];
271
272
		$result = wp_remote_get( $this->make_video_get_path( $info->guid ) );
273
274 View Code Duplication
		if ( is_wp_error( $result ) ) {
275
			var_dump($result);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($result); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
276
			return wp_send_json_error( array( 'message' => __( 'Could not update the VideoPress video status. Please try again later.', 'jetpack' ) ) );
277
		}
278
279
		$response = json_decode( $result['body'], true );
280
		var_dump($response);
281
	}
282
283
	/**
284
	 * Get VideoPress options
285
	 */
286
	function get_options() {
287
		$defaults = array(
288
			'blogs' => array(),
289
			'blog_id' => 0,
290
			'access' => '',
291
			'allow-upload' => false,
292
			'freedom' => false,
293
			'hd' => false,
294
			'meta' => array(
295
				'max_upload_size' => 0,
296
			),
297
		);
298
299
		$options = Jetpack_Options::get_option( $this->option_name, array() );
300
301
		// If options have not been saved yet, check for older VideoPress plugin options.
302
		if ( empty( $options ) ) {
303
			$options['freedom'] = (bool) get_option( 'video_player_freedom', false );
304
			$options['hd'] = (bool) get_option( 'video_player_high_quality', false );
305
		}
306
307
		$options = array_merge( $defaults, $options );
308
		$options['id'] = Jetpack_Options::get_option( 'id' );
309
310
		return $options;
311
	}
312
313
	/**
314
	 * Update VideoPress options
315
	 */
316
	function update_options( $options ) {
317
		Jetpack_Options::update_option( $this->option_name, $options );
318
	}
319
320
	/**
321
	 * Runs when the VideoPress module is activated.
322
	 */
323
	function jetpack_module_activated() {
324
		if ( ! $this->is_connection_owner() )
325
			return;
326
327
		$options = $this->get_options();
328
329
		// Ask WordPress.com for a list of VideoPress blogs
330
		$result = $this->query( 'jetpack.vpGetBlogs' );
331
		if ( ! is_wp_error( $result ) )
332
			$options['blogs'] = $result;
333
334
		// If there's at least one available blog, let's use it.
335
		if ( is_array( $options['blogs'] ) && count( $options['blogs'] ) > 0 )
336
			$options['blog_id'] = $options['blogs'][0]['blog_id'];
337
338
		$this->update_options( $options );
339
	}
340
341
	/**
342
	 * Runs when the VideoPress module is deactivated.
343
	 */
344
	function jetpack_module_deactivated() {
345
		Jetpack_Options::delete_option( $this->option_name );
346
	}
347
348
	/**
349
	 * Remote Query
350
	 *
351
	 * Performs a remote XML-RPC query using Jetpack's IXR Client. And also
352
	 * appends some useful stuff about this setup to the query.
353
	 *
354
	 * @return the Jetpack_IXR_Client object after querying.
355
	 */
356
	function query( $method, $args = null ) {
357
		$options = $this->get_options();
358
		Jetpack::load_xml_rpc_client();
359
		$xml = new Jetpack_IXR_Client( array(
360
			'user_id' => JETPACK_MASTER_USER, // All requests are on behalf of the connection owner.
361
		) );
362
363
		$params = array(
364
			'args' => $args,
365
			'video_blog_id' => $options['blog_id'],
366
			'caps' => array(),
367
		);
368
369
		// Let Jetpack know about our local caps.
370 View Code Duplication
		foreach ( array( 'read_videos', 'edit_videos', 'delete_videos', 'upload_videos' ) as $cap )
371
			if ( $this->can( $cap ) )
372
				$params['caps'][] = $cap;
373
374
		$xml->query( $method, $params );
375
376
		if ( $xml->isError() )
377
			return new WP_Error( 'xml_rpc_error', 'An XML-RPC error has occurred.' );
378
379
		$response = $xml->getResponse();
380
381
		// If there's any metadata with the response, save it for future use.
382
		if ( is_array( $response ) && isset( $response['meta'] ) ) {
383
			$options = $this->get_options();
384
			if ( $response['meta'] !== $options['meta'] ) {
385
				$options['meta'] = array_merge( $options['meta'], $response['meta'] );
386
				$this->update_options( $options );
387
			}
388
		}
389
390
		if ( is_array( $response ) && isset( $response['result'] ) )
391
			return $response['result'];
392
393
		return $response;
394
	}
395
396
	/**
397
	 * Runs before the VideoPress Configuration screen loads, useful
398
	 * to update options and yield errors.
399
	 */
400
	function jetpack_configuration_load() {
401
		$this->enqueue_admin_scripts();
402
403
		/**
404
		 * Save configuration
405
		 */
406
		if ( ! empty( $_POST['action'] ) && $_POST['action'] == 'videopress-save' ) {
407
			check_admin_referer( 'videopress-settings' );
408
			$options = $this->get_options();
409
410
			if ( isset( $_POST['blog_id'] ) && in_array( $_POST['blog_id'], wp_list_pluck( $options['blogs'], 'blog_id' ) ) )
411
				$options['blog_id'] = $_POST['blog_id'];
412
413
			// Allow the None setting too.
414
			if ( isset( $_POST['blog_id'] ) && $_POST['blog_id'] == 0 )
415
				$options['blog_id'] = 0;
416
417
			/**
418
			 * @see $this->can()
419
			 */
420
			if ( isset( $_POST['videopress-access'] ) && in_array( $_POST['videopress-access'], array( '', 'read', 'edit', 'delete' ) ) )
421
				$options['access'] = $_POST['videopress-access'];
422
423
			$options['freedom'] = isset( $_POST['videopress-freedom'] );
424
			$options['hd'] = isset( $_POST['videopress-hd'] );
425
426
			// Allow upload only if some level of access has been granted, and uploads were allowed.
427
			$options['allow-upload'] = false;
428
			if ( ! empty( $options['access'] ) && isset( $_POST['videopress-upload'] ) )
429
				$options['allow-upload'] = true;
430
431
			$this->update_options( $options );
432
			Jetpack::state( 'message', 'module_configured' );
433
			wp_safe_redirect( Jetpack::module_configuration_url( $this->module ) );
434
		}
435
436
		/**
437
		 * Refresh the list of available WordPress.com blogs
438
		 */
439
		if ( ! empty( $_GET['videopress'] ) && $_GET['videopress'] == 'refresh-blogs' ) {
440
			check_admin_referer( 'videopress-settings' );
441
			$options = $this->get_options();
442
443
			$result = $this->query( 'jetpack.vpGetBlogs' );
444
			if ( ! is_wp_error( $result ) ) {
445
				$options['blogs'] = $result;
446
				$this->update_options( $options );
447
			}
448
449
			wp_safe_redirect( Jetpack::module_configuration_url( $this->module ) );
450
		}
451
	}
452
453
	/**
454
	 * Renders the VideoPress Configuration screen in Jetpack.
455
	 */
456
	function jetpack_configuration_screen() {
457
		$options = $this->get_options();
458
		$refresh_url = wp_nonce_url( add_query_arg( 'videopress', 'refresh-blogs' ), 'videopress-settings' );
459
		?>
460
		<div class="narrow">
461
			<form method="post" id="videopress-settings">
462
				<input type="hidden" name="action" value="videopress-save" />
463
				<?php wp_nonce_field( 'videopress-settings' ); ?>
464
465
				<table id="menu" class="form-table">
466
					<tr>
467
						<th scope="row" colspan="2">
468
							<p><?php _e( 'Please note that the VideoPress module requires a WordPress.com account with an active <a href="http://store.wordpress.com/premium-upgrades/videopress/" target="_blank">VideoPress subscription</a>.', 'jetpack' ); ?></p>
469
						</th>
470
					</tr>
471
					<tr>
472
						<th scope="row">
473
							<label><?php _e( 'Connected WordPress.com Blog', 'jetpack' ); ?></label>
474
						</th>
475
						<td>
476
							<select name="blog_id">
477
								<option value="0" <?php selected( $options['blog_id'], 0 ); ?>> <?php esc_html_e( 'None', 'jetpack' ); ?></option>
478
								<?php foreach ( $options['blogs'] as $blog ) : ?>
479
								<option value="<?php echo absint( $blog['blog_id'] ); ?>" <?php selected( $options['blog_id'], $blog['blog_id'] ); ?>><?php echo esc_html( $blog['name'] ); ?> (<?php echo esc_html( $blog['domain'] ); ?>)</option>
480
								<?php endforeach; ?>
481
							</select>
482
							<p class="description"><?php _e( 'Only videos from the selected blog will be available in your media library.', 'jetpack' ); ?>
483
								<?php printf( __( '<a href="%s">Click here</a> to refresh this list.', 'jetpack' ), esc_url( $refresh_url ) ); ?>
484
							</p>
485
						</td>
486
					</tr>
487
					<tr>
488
						<th scope="row">
489
							<label><?php _e( 'Video Library Access', 'jetpack' ); ?></label>
490
						</th>
491
						<td>
492
							<label><input type="radio" name="videopress-access" value="" <?php checked( '', $options['access'] ); ?> />
493
								<?php _e( 'Do not allow other users to access my VideoPress library', 'jetpack' ); ?></label><br/>
494
							<label><input type="radio" name="videopress-access" value="read" <?php checked( 'read', $options['access'] ); ?> />
495
								<?php _e( 'Allow users to access my videos', 'jetpack' ); ?></label><br/>
496
							<label><input type="radio" name="videopress-access" value="edit" <?php checked( 'edit', $options['access'] ); ?> />
497
								<?php _e( 'Allow users to access and edit my videos', 'jetpack' ); ?></label><br/>
498
							<label><input type="radio" name="videopress-access" value="delete" <?php checked( 'delete', $options['access'] ); ?> />
499
								<?php _e( 'Allow users to access, edit, and delete my videos', 'jetpack' ); ?></label><br/><br />
500
501
							<label><input type="checkbox" name="videopress-upload" value="1" <?php checked( $options['allow-upload'] ); ?> />
502
								<?php _e( 'Allow users to upload videos', 'jetpack' ); ?></label><br />
503
						</td>
504
					</tr>
505
					<tr>
506
						<th scope="row">
507
							<label for="videopress-freedom"><?php _e( 'Free formats', 'jetpack' ); ?></label>
508
						</th>
509
						<td>
510
							<label><input type="checkbox" name="videopress-freedom" id="videopress-freedom" <?php checked( $options['freedom'] ); ?> />
511
								<?php _e( 'Only display videos in free software formats', 'jetpack' ); ?></label>
512
							<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>
513
						</td>
514
					</tr>
515
					<tr>
516
						<th scope="row">
517
							<label for="videopress-hd"><?php _e( 'Default quality', 'jetpack' ); ?></label>
518
						</th>
519
						<td>
520
							<label><input type="checkbox" name="videopress-hd" id="videopress-hd" <?php checked( $options['hd'] ); ?> />
521
								<?php _e( 'Display higher quality video by default.', 'jetpack' ); ?></label>
522
							<p class="description"><?php _e( 'This setting may be overridden for individual videos.', 'jetpack' ); ?></p>
523
						</td>
524
					</tr>
525
				</table>
526
527
				<?php submit_button(); ?>
528
			</form>
529
		</div>
530
		<?php
531
	}
532
533
	function admin_menu() {
534
		add_media_page( __( 'VideoPress Library', 'jetpack' ), __( 'VideoPress', 'jetpack' ), 'upload_files', 'videopress-library', array( $this, 'admin_menu_library' ) );
535
	}
536
537
	function admin_menu_library() {
538
		wp_enqueue_media();
539
		$this->enqueue_admin_scripts();
540
		?>
541
		<div class="wrap" style="max-width: 600px;">
542
			<?php screen_icon(); ?>
543
	        <h2><?php _e( 'VideoPress Library', 'jetpack' ); ?></h2>
544
	        <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>
545
	        <p class="hide-if-no-js"><a href="#" id="videopress-browse" class="button"><?php _e( 'Browse Your VideoPress Library', 'jetpack' ); ?></a></p>
546
	        <p class="hide-if-js description"><?php _e( 'Please enable JavaScript support in your browser to use VideoPress.', 'jetpack' ); ?></p>
547
	    </div>
548
		<?php
549
	}
550
551
	/**
552
	 * A can of coke
553
	 *
554
	 * Similar to current_user_can, but internal to VideoPress. Returns
555
	 * true if the given VideoPress capability is allowed by the given user.
556
	 */
557
	function can( $cap, $user_id = false ) {
558
		if ( ! $user_id )
559
			$user_id = get_current_user_id();
560
561
		// Connection owners are allowed to do all the things.
562
		if ( $this->is_connection_owner( $user_id ) )
563
			return true;
564
565
		/**
566
		 * The access setting can be set by the connection owner, to allow sets
567
		 * of operations to other site users. Each access value corresponds to
568
		 * an array of things they can do.
569
		 */
570
571
		$options = $this->get_options();
572
		$map = array(
573
			'read'   => array( 'read_videos' ),
574
			'edit'   => array( 'read_videos', 'edit_videos' ),
575
			'delete' => array( 'read_videos', 'edit_videos', 'delete_videos' ),
576
		);
577
578
		if ( ! array_key_exists( $options['access'], $map ) )
579
			return false;
580
581
		if ( ! in_array( $cap, $map[ $options['access'] ] ) && 'upload_videos' != $cap )
582
			return false;
583
584
		// Additional and intrenal caps checks
585
586
		if ( ! user_can( $user_id, 'upload_files' ) )
587
			return false;
588
589
		if ( 'edit_videos' == $cap && ! user_can( $user_id, 'edit_others_posts' ) )
590
			return false;
591
592
		if ( 'delete_videos' == $cap && ! user_can( $user_id, 'delete_others_posts' ) )
593
			return false;
594
595
		if ( 'upload_videos' == $cap && ! $options['allow-upload'] )
596
			return false;
597
598
		return true;
599
	}
600
601
	/**
602
	 * Returns true if the provided user is the Jetpack connection owner.
603
	 */
604
	function is_connection_owner( $user_id = false ) {
605
		if ( ! $user_id )
606
			$user_id = get_current_user_id();
607
608
		$user_token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER );
609
		return $user_token && is_object( $user_token ) && isset( $user_token->external_user_id ) && $user_id === $user_token->external_user_id;
610
	}
611
612
	/**
613
	 * Our custom AJAX callback for the query-attachments action
614
	 * used in the media modal. By-passed if not for VideoPress.
615
	 */
616
	function wp_ajax_query_attachments() {
617
618
		// Watch for VideoPress calls
619
		if ( ! isset( $_POST['query']['videopress'] ) )
620
			return;
621
622
		if ( ! $this->can( 'read_videos' ) )
623
			return wp_send_json_error( 'permission denied' );
624
625
		// Get and sanitize query arguments.
626
		$query_args = $this->sanitize_wp_query_args( $_POST['query'] );
627
628
		// Fire a remote WP_Query
629
		$result = $this->query( 'jetpack.vpQuery', $query_args );
630
631
		if ( is_wp_error( $result ) )
632
			return wp_send_json_error( 'xml rpc request error' );
633
634
		$items = $result;
635
636
		foreach ( $items as $key => $item ) {
637
638
			// Check local permissions
639
			if ( ! $this->can( 'edit_videos' ) )
640
				unset( $item['vp_nonces']['update'] );
641
642
			if ( ! $this->can( 'delete_videos' ) )
643
				unset( $item['vp_nonces']['delete'] );
644
645
			// Add a second pair of nonces for the .org blog.
646
			$item['nonces'] = array();
647 View Code Duplication
			if ( ! empty( $item['vp_nonces']['update'] ) )
648
				$item['nonces']['update'] = wp_create_nonce( 'update-videopress-post_' . $item['id'] );
649
650 View Code Duplication
			if ( ! empty( $item['vp_nonces']['delete'] ) )
651
				$item['nonces']['delete'] = wp_create_nonce( 'delete-videopress-post_' . $item['id'] );
652
653
			$item['vp_embed'] = videopress_shortcode_callback( array(
654
				$item['vp_guid'],
655
				'autoplay' => true,
656
				'flashonly' => true,
657
				'w' => 440,
658
			) );
659
660
			$items[ $key ] = $item;
661
		}
662
663
		wp_send_json_success( $items );
664
	}
665
666
	/**
667
	 * Sanitize user-provided WP_Query arguments
668
	 *
669
	 * These might be sent to the VideoPress server, for a remote WP_Query
670
	 * call so let's make sure they're sanitized and safe to send.
671
	 */
672
	function sanitize_wp_query_args( $args ) {
673
		$args = shortcode_atts( array(
674
			'posts_per_page' => 40,
675
			'orderby' => 'date',
676
			'order' => 'desc',
677
			'paged' => 1,
678
			's' => '',
679
		), (array) $args, 'wpvideo' );
680
681
		$args['posts_per_page'] = absint( $args['posts_per_page'] );
682
683
		$args['orderby'] = strtolower( $args['orderby'] );
684
		$args['orderby'] = ( in_array( $args['orderby'], array( 'date' ) ) ) ? $args['orderby'] : 'date';
685
686
		$args['order'] = strtolower( $args['order'] );
687
		$args['order'] = ( in_array( $args['order'], array( 'asc', 'desc' ) ) ) ? $args['order'] : 'desc';
688
689
		$args['paged'] = absint( $args['paged'] );
690
		$args['s'] = sanitize_text_field( $args['s'] );
691
		return $args;
692
	}
693
694
	/**
695
	 * Custom AJAX callback for the save-attachment action. If the request was
696
	 * not for a VideoPress object, core's fallback action will kick in.
697
	 */
698
	function wp_ajax_save_attachment() {
699
		if ( ! isset( $_POST['is_videopress'] ) )
700
			return;
701
702
		if ( ! $this->can( 'edit_videos' ) )
703
			return wp_send_json_error( 'permission denied' );
704
705
		$post_id = 0;
706 View Code Duplication
		if ( ! isset( $_POST['id'] ) || ! $post_id = absint( $_POST['id'] ) )
707
			wp_send_json_error();
708
709
		if ( ! isset( $_POST['vp_nonces']['update'] ) )
710
			wp_send_json_error();
711
712
		check_ajax_referer( 'update-videopress-post_' . $post_id, 'nonce' );
713
714
		$changes = ( ! empty( $_POST['changes'] ) ) ? (array) $_POST['changes'] : array();
715
		$changes = shortcode_atts( array(
716
			'title' => null,
717
			'caption' => null,
718
			'description' => null,
719
720
			'vp_share' => null,
721
			'vp_rating' => null,
722
		), $changes, 'wpvideo' );
723
724
		if ( ! is_null( $changes['vp_share'] ) )
725
			$changes['vp_share'] = (bool) $changes['vp_share'];
726
727
		if ( ! is_null( $changes['vp_rating'] ) )
728
			$changes['vp_rating'] = ( array_key_exists( $changes['vp_rating'], $this->get_available_ratings() ) ) ? $changes['vp_rating'] : null;
729
730
		// Remove null-values
731
		foreach ( $changes as $key => $value )
732
			if ( is_null( $value ) )
733
				unset( $changes[ $key ] );
734
735
		$result = $this->query( 'jetpack.vpSaveAttachment', array(
736
			'post_id' => $post_id,
737
			'changes' => $changes,
738
			'nonce' => $_POST['vp_nonces']['update'],
739
		) );
740
741
		if ( is_wp_error( $result ) )
742
			return wp_send_json_error( 'xml rpc request error' );
743
744
		wp_send_json_success();
745
	}
746
747
	/**
748
	 * Custom AJAX callback for the delete-post action, only for VideoPress objects.
749
	 */
750
	function wp_ajax_delete_post() {
751
		if ( ! isset( $_POST['is_videopress'] ) )
752
			return;
753
754
		if ( ! $this->can( 'delete_videos' ) )
755
			return wp_send_json_error( 'permission denied' );
756
757
		$post_id = 0;
758 View Code Duplication
		if ( ! isset( $_POST['id'] ) || ! $post_id = absint( $_POST['id'] ) )
759
			wp_send_json_error();
760
761
		if ( ! isset( $_POST['vp_nonces']['delete'] ) )
762
			wp_send_json_error();
763
764
		check_ajax_referer( 'delete-videopress-post_' . $post_id );
765
766
		$result = $this->query( 'jetpack.vpDeleteAttachment', array(
767
			'post_id' => $post_id,
768
			'nonce' => $_POST['vp_nonces']['delete'],
769
		) );
770
771
		if ( is_wp_error( $result ) )
772
			return wp_send_json_error( 'xml rpc request error' );
773
774
		wp_send_json_success();
775
	}
776
777
	/**
778
	 * Register VideoPress admin scripts.
779
	 */
780
	function enqueue_admin_scripts() {
781
		if ( did_action( 'videopress_enqueue_admin_scripts' ) )
782
			return;
783
784
		wp_enqueue_script( 'videopress-uploader', plugins_url( 'js/videopress-uploader.js', __FILE__) , array( 'jquery', 'wp-plupload' ), $this->version );
785
		wp_enqueue_script( 'videopress-admin', plugins_url( 'js/videopress-admin.js', __FILE__ ), array( 'jquery', 'media-views', 'media-models' ), $this->version );
786
		wp_enqueue_style( 'videopress-admin', plugins_url( 'videopress-admin.css', __FILE__ ), array(), $this->version );
787
788
		$caps = array();
789 View Code Duplication
		foreach( array( 'read_videos', 'edit_videos', 'delete_videos', 'upload_videos' ) as $cap )
790
			$caps[ $cap ] = $this->can( $cap );
791
792
		$l10n = array(
793
			'selectVideoFile' => __( 'Please select a video file to upload.', 'jetpack' ),
794
			'videoUploading' => __( 'Your video is uploading... Please do not close this window.', 'jetpack' ),
795
			'unknownError' => __( 'An unknown error has occurred. Please try again later.', 'jetpack' ),
796
			'videoUploaded' => __( 'Your video has successfully been uploaded. It will appear in your VideoPress Library shortly.', 'jetpack' ),
797
			'VideoPressLibraryRouter' => __( 'VideoPress Library', 'jetpack' ),
798
			'uploadVideoRouter' => __( 'Upload a Video', 'jetpack' ),
799
			'insertVideoButton' => __( 'Insert Video', 'jetpack' ),
800
801
		);
802
803
		wp_localize_script( 'videopress-admin', 'VideoPressAdminSettings', array(
804
			'caps' => $caps,
805
			'l10n' => $l10n,
806
		) );
807
		
808
		/**
809
		 * Fires after VideoPress scripts are enqueued in the dashboard.
810
		 *
811
		 * @since 2.5.0
812
		 */
813
		do_action( 'videopress_enqueue_admin_scripts' );
814
	}
815
816
	/**
817
	 * Get an array of available ratings. Keys are options, values are labels.
818
	 */
819
	function get_available_ratings() {
820
		return array(
821
			'G' => 'G',
822
			'PG-13' => 'PG-13',
823
			'R-17' => 'R',
824
			'X-18' => 'X',
825
		);
826
	}
827
828
	/**
829
	 * Additional VideoPress media templates.
830
	 */
831
	function print_media_templates() {
832
		$options = $this->get_options();
833
		?>
834
		<script type="text/html" id="tmpl-videopress-attachment">
835
			<# if ( data.vp_ogg_url ) { #>
836
			<label class="setting vp-setting">
837
				<span><?php _e( 'Ogg File URL', 'jetpack' ); ?></span>
838
				<input type="text" value="{{ data.vp_ogg_url }}" onclick="this.focus();this.select();" readonly />
839
				<p class="help"><?php _e( 'Location of the Ogg video file.', 'jetpack' ); ?></p>
840
			</label>
841
			<# } #>
842
843
			<label class="setting vp-setting">
844
				<span><?php _e( 'Share', 'jetpack' ); ?></span>
845
				<input class="vp-checkbox" type="checkbox" <# if ( '1' === data.vp_share ) { #>checked<# } #> <# if ( ! data.can.save ) { #>disabled<# } #> />
846
				<label>
847
					<?php _e( 'Display share menu and allow viewers to embed or download this video', 'jetpack' ); ?>
848
				</label>
849
				<input class="vp-checkbox-text" type="text" value="{{ data.vp_share }}" data-setting="vp_share" style="display:none;" />
850
			</label>
851
852
			<label class="setting vp-setting">
853
				<span><?php _e( 'Rating', 'jetpack' ); ?></span>
854
855
				<?php foreach ( $this->get_available_ratings() as $value => $label ) : ?>
856
				<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 ); ?>"
857
					<# if ( '<?php echo esc_attr( $value ); ?>' === data.vp_rating ) { #>checked<# } #>
858
					<# if ( ! data.can.save ) { #>disabled<# } #> />
859
				<label for="vp-rating-<?php echo sanitize_html_class( $value ); ?>"><?php echo esc_html( $label ); ?></label>
860
				<?php endforeach; ?>
861
862
				<input class="vp-radio-text" type="text" value="{{ data.vp_rating }}" data-setting="vp_rating" style="display:none;" />
863
			</label>
864
865
			<label class="setting vp-setting">
866
				<span><?php _e( 'Shortcode', 'jetpack' ); ?></span>
867
				<input type="text" value="[wpvideo {{ data.vp_guid }}]" onclick="this.focus();this.select();" readonly />
868
			</label>
869
870
			<label class="setting vp-setting vp-preview">
871
				<span><?php _e( 'Preview', 'jetpack' ); ?></span>
872
				<# if ( ! data.vp_thumbnail_url ) { #>
873
					<span class="videopress-preview-unavailable"><?php esc_html_e( 'The preview is unavailable while this video is being processed.', 'jetpack' ); ?></span>
874
				<# } else { #>
875
				<a href="#" class="videopress-preview" id="videopress-thumbnail-{{ data.vp_guid }}" data-videopress-guid="{{ data.vp_guid }}"><img src="{{ data.vp_thumbnail_url }}" /></a>
876
				<# } #>
877
			</label>
878
		</script>
879
880
		<script type="text/html" id="tmpl-videopress-media-modal">
881
			<div class="videopress-modal">
882
				<p><?php _e( 'Video Preview:', 'jetpack' ); ?></p>
883
				<div class="videopress-video-container">{{{ data.video }}}</div>
884
				<p class="submit">
885
					<a class="videopress-modal-close button" href="#"><?php _e( 'Close', 'jetpack' ); ?></a>
886
				</p>
887
			</div>
888
			<div class="videopress-modal-backdrop"></div>
889
		</script>
890
891
		<script type="text/html" id="tmpl-videopress-uploader">
892
			<div class="videopress-errors"></div>
893
			<form class="videopress-upload-form" action="" method="post" target="videopress_upload_frame" enctype="multipart/form-data">
894
				<input type="hidden" name="action" value="videopress_upload" />
895
				<input type="hidden" name="videopress_blog_id" value="0" />
896
				<input type="hidden" name="videopress_token" value="0" />
897
				<?php $formats = 'ogv, mp4, m4v, mov, wmv, avi, mpg, 3gp, 3g2'; ?>
898
				<?php
899
					$max_upload_size = 0;
900
					if ( ! empty( $options['meta']['max_upload_size'] ) )
901
						$max_upload_size = absint( $options['meta']['max_upload_size'] );
902
903
					$upload_size_unit = $max_upload_size;
904
					$byte_sizes = array( 'KB', 'MB', 'GB' );
905
906
					for ( $u = -1; $upload_size_unit > 1024 && $u < count( $byte_sizes ) - 1; $u++ )
907
						$upload_size_unit /= 1024;
908
909
					if ( $u < 0 ) {
910
						$upload_size_unit = 0;
911
						$u = 0;
912
					} else {
913
						$upload_size_unit = (int) $upload_size_unit;
914
					}
915
				?>
916
				<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>
917
918
				<input type="file" name="videopress_file" />
919
				<?php submit_button( __( 'Upload Video', 'jetpack' ) ); ?>
920
			</form>
921
			<iframe width="0" height="0" name="videopress_upload_frame"></iframe>
922
		</script>
923
		<?php
924
	}
925
926
	/**
927
	 * Filters the VideoPress shortcode options, makes sure that
928
	 * the settings set in Jetpack's VideoPress module are applied.
929
	 */
930
	function videopress_shortcode_options( $options ) {
931
		$videopress_options = $this->get_options();
932
933
		if ( false === $options['freedom'] )
934
			$options['freedom'] = $videopress_options['freedom'];
935
936
		$options['hd'] = $videopress_options['hd'];
937
938
		return $options;
939
	}
940
941
}
942
943
// Initialize the module.
944
Jetpack_VideoPress::init();
945