Completed
Push — fix/4.4.1-typos ( 3db521 )
by
unknown
25s
created

VideoPress_AJAX   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 94
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A init() 0 7 2
B wp_ajax_videopress_get_upload_token() 0 30 3
A wp_ajax_update_transcoding_status() 0 18 3
1
<?php
2
3
class VideoPress_AJAX {
4
5
	/**
6
	 * @var VideoPress_AJAX
7
	 **/
8
	private static $instance = null;
9
10
	/**
11
	 * Private VideoPress_AJAX constructor.
12
	 *
13
	 * Use the VideoPress_AJAX::init() method to get an instance.
14
	 */
15
	private function __construct() {
16
		add_action( 'wp_ajax_videopress-get-upload-token', array( $this, 'wp_ajax_videopress_get_upload_token' ) );
17
18
		add_action( 'wp_ajax_videopress-update-transcoding-status', array(
19
			$this,
20
			'wp_ajax_update_transcoding_status'
21
		), -1 );
22
	}
23
24
	/**
25
	 * Initialize the VideoPress_AJAX and get back a singleton instance.
26
	 *
27
	 * @return VideoPress_AJAX
28
	 */
29
	public static function init() {
30
		if ( is_null( self::$instance ) ) {
31
			self::$instance = new VideoPress_AJAX;
32
		}
33
34
		return self::$instance;
35
	}
36
37
	/**
38
	 * Ajax method that is used by the VideoPress uploader to get a token to upload a file to the wpcom api.
39
	 *
40
	 * @return void
41
	 */
42
	public function wp_ajax_videopress_get_upload_token() {
43
44
		$options = VideoPress_Options::get_options();
45
46
		$args = array(
47
			'method' => 'POST',
48
		);
49
50
		$endpoint = "sites/{$options['shadow_blog_id']}/media/token";
51
		$result   = Jetpack_Client::wpcom_json_api_request_as_blog( $endpoint, Jetpack_Client::WPCOM_JSON_API_VERSION, $args );
52
53
		if ( is_wp_error( $result ) ) {
54
			wp_send_json_error( array( 'message' => __( 'Could not obtain a VideoPress upload token. Please try again later.', 'jetpack' ) ) );
55
			return;
56
		}
57
58
		$response = json_decode( $result['body'], true );
59
60
		if ( empty( $response['upload_token'] ) ) {
61
			wp_send_json_error( array( 'message' => __( 'Could not obtain a VideoPress upload token. Please try again later.', 'jetpack' ) ) );
62
			return;
63
		}
64
65
		$title = sanitize_title( basename( $_POST['filename'] ) );
66
67
		$response['upload_action_url'] = videopress_make_media_upload_path( $options['shadow_blog_id'] );
68
		$response['upload_media_id']   = videopress_create_new_media_item( $title );
69
70
		wp_send_json_success( $response );
71
	}
72
73
	/**
74
	 * Ajax action to update the video transcoding status from the WPCOM API.
75
	 *
76
	 * @return void
77
	 */
78
	public function wp_ajax_update_transcoding_status() {
79
		if ( ! isset( $_POST['post_id'] ) ) {
80
			wp_send_json_error( array( 'message' => __( 'A valid post_id is required.', 'jetpack' ) ) );
81
			return;
82
		}
83
84
		$post_id = (int) $_POST['post_id'];
85
86
		if ( ! videopress_update_meta_data( $post_id ) ) {
87
			wp_send_json_error( array( 'message' => __( 'That post does not have a VideoPress video associated to it..', 'jetpack' ) ) );
88
			return;
89
		}
90
91
		wp_send_json_success( array(
92
			'message' => __( 'Status updated', 'jetpack' ),
93
			'status'  => videopress_get_transcoding_status( $post_id )
94
		) );
95
	}
96
}
97
98
// Let's start this thing up.
99
VideoPress_AJAX::init();