summaryrefslogtreecommitdiff
blob: c745350ba883158b2ffdd42a86d7bda1d91224f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
 * Handle the VideoPress metadata properties.
 *
 * @package Jetpack
 */

use Automattic\Jetpack\Connection\Client;

/**
 * Class Videopress_Attachment_Metadata
 */
class Videopress_Attachment_Metadata {

	/**
	 * Persist the VideoPress metadata information, including rating and display_embed.
	 *
	 * @param string|int $post_id        The post id.
	 * @param string     $guid           VideoPress Guid.
	 * @param string     $post_title     The post title.
	 * @param string     $caption        Video caption.
	 * @param string     $post_excerpt   The post excerpt.
	 * @param string     $rating         The rating.
	 * @param int        $display_embed  The display_embed.
	 * @param int        $allow_download Allow video downloads.
	 *
	 * @return bool|\WP_Error
	 */
	public static function persist_metadata( $post_id, $guid, $post_title, $caption, $post_excerpt, $rating, $display_embed, $allow_download ) {
		$post_id = absint( $post_id );

		$args = array(
			'method'  => 'POST',
			'headers' => array( 'content-type' => 'application/json' ),
		);

		// Keep null values to avoid accidental unset.
		$display_embed  = null === $display_embed ? null : (int) $display_embed;
		$allow_download = null === $allow_download ? null : (int) $allow_download;

		$values         = self::build_wpcom_api_request_values( $post_title, $caption, $post_excerpt, $rating, $display_embed, $allow_download );
		$endpoint       = 'videos';
		$values['guid'] = $guid;

		$result = Client::wpcom_json_api_request_as_blog( $endpoint, '2', $args, wp_json_encode( $values ), 'wpcom' );

		$validated_result = self::validate_result( $result );
		if ( true !== $validated_result ) {
			return $validated_result;
		}

		// If we are in WPCOM, then we don't need to make anything else since we've already updated the video information.
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
			return true;
		}

		$meta = wp_get_attachment_metadata( $post_id );

		if ( isset( $values['display_embed'] ) ) {
			$meta['videopress']['display_embed'] = (bool) $values['display_embed']; // convert it to bool since that's how we store it on wp-admin side.
		}

		if ( isset( $values['allow_download'] ) ) {
			$meta['videopress']['allow_download'] = (bool) $values['allow_download'];
		}

		if ( isset( $values['rating'] ) ) {
			$meta['videopress']['rating'] = $values['rating'];
		}

		wp_update_attachment_metadata( $post_id, $meta );

		return true;
	}

	/**
	 * Check if the given media item is a VideoPress file.
	 *
	 * @param stdClass $item The media item.
	 *
	 * @return bool
	 */
	public static function is_videopress_media( $item ) {
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
			return 0 === strpos( $item->mime_type, 'video/' );
		}

		// Else, we are in Jetpack and we need to check if the video is video/videopress.
		return 'video/videopress' === $item->mime_type;
	}

	/**
	 * Check if display_embed has valid values.
	 *
	 * @param mixed $display_embed The input display embed.
	 *
	 * @return bool
	 */
	private static function is_display_embed_valid( $display_embed ) {
		return in_array( $display_embed, array( 0, 1 ), true );
	}

	/**
	 * Check if allow_download has valid values
	 *
	 * @param mixed $allow_download The value to test.
	 * @return bool
	 */
	private static function is_allow_download_valid( $allow_download ) {
		return in_array( $allow_download, array( 0, 1 ), true );
	}

	/**
	 * Validate the response received from WPCOM.
	 *
	 * @param array|\WP_Error $result The result returned by the client.
	 */
	private static function validate_result( $result ) {
		$response_code = isset( $result['response']['code'] ) ? $result['response']['code'] : 500;

		// When Client::wpcom_json_api_request_as_blog is called in WPCOM, bad response codes are not converted to WP_Error.
		// Because of this, we need to manually check the response code to check if the direct API call is 200 (OK).
		if ( 200 === $response_code && ! is_wp_error( $result ) ) {
			return true;
		}

		$error_message = __(
			'There was an issue saving your updates to the VideoPress service. Please try again later.',
			'jetpack'
		);

		$error_code = $response_code;

		if ( is_wp_error( $result ) ) {
			$error_code = $result->get_error_code();
		}

		return new \WP_Error( $error_code, $error_message );
	}

	/**
	 * Build the request values that will be passed to the WPCOM API.
	 *
	 * @param string $post_title The video title.
	 * @param string $caption The video caption.
	 * @param string $post_excerpt The except.
	 * @param string $rating The video rating.
	 * @param string $display_embed The video display_embed.
	 * @param int    $allow_download The video allow_download.
	 *
	 * @return array
	 */
	private static function build_wpcom_api_request_values( $post_title, $caption, $post_excerpt, $rating, $display_embed, $allow_download ) {
		$values = array();

		// Add the video title & description in, so that we save it properly.
		if ( isset( $post_title ) ) {
			$values['title'] = trim( wp_strip_all_tags( $post_title ) );
		}

		if ( isset( $caption ) ) {
			$values['caption'] = trim( wp_strip_all_tags( $caption ) );
		}

		if ( isset( $post_excerpt ) ) {
			$values['description'] = trim( wp_strip_all_tags( $post_excerpt ) );
		}

		if ( isset( $rating ) ) {
			$values['rating'] = $rating;
		}

		if ( self::is_display_embed_valid( $display_embed ) ) {
			$values['display_embed'] = $display_embed;
		}

		if ( self::is_allow_download_valid( $allow_download ) ) {
			$values['allow_download'] = $allow_download;
		}

		return $values;
	}
}