|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Handle the VideoPress metadata properties. |
|
4
|
|
|
* |
|
5
|
|
|
* @package Jetpack |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
use Automattic\Jetpack\Connection\Client; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class Videopress_Attachment_Metadata |
|
12
|
|
|
*/ |
|
13
|
|
|
class Videopress_Attachment_Metadata { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Persist the VideoPress metadata information, including rating and display_embed. |
|
17
|
|
|
* |
|
18
|
|
|
* @param string|int $post_id The post id. |
|
19
|
|
|
* @param string $guid VideoPress Guid. |
|
20
|
|
|
* @param string $post_title The post title. |
|
21
|
|
|
* @param string $caption Video caption. |
|
22
|
|
|
* @param string $post_excerpt The post excerpt. |
|
23
|
|
|
* @param string $rating The rating. |
|
24
|
|
|
* @param int $display_embed The display_embed. |
|
25
|
|
|
* |
|
26
|
|
|
* @return bool|\WP_Error |
|
27
|
|
|
*/ |
|
28
|
|
|
public static function persist_metadata( $post_id, $guid, $post_title, $caption, $post_excerpt, $rating, $display_embed ) { |
|
29
|
|
|
$post_id = absint( $post_id ); |
|
30
|
|
|
|
|
31
|
|
|
$args = array( |
|
32
|
|
|
'method' => 'POST', |
|
33
|
|
|
'headers' => array( 'content-type' => 'application/json' ), |
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
|
|
$display_embed = (int) $display_embed; |
|
37
|
|
|
|
|
38
|
|
|
$values = self::build_wpcom_api_request_values( $post_title, $caption, $post_excerpt, $rating, $display_embed ); |
|
39
|
|
|
$endpoint = 'videos'; |
|
40
|
|
|
$values['guid'] = $guid; |
|
41
|
|
|
|
|
42
|
|
|
$result = Client::wpcom_json_api_request_as_blog( $endpoint, '2', $args, wp_json_encode( $values ), 'wpcom' ); |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
$validated_result = self::validate_result( $result ); |
|
45
|
|
|
if ( true !== $validated_result ) { |
|
46
|
|
|
return $validated_result; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
// If we are in WPCOM, then we don't need to make anything else since we've already updated the video information. |
|
50
|
|
|
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
|
51
|
|
|
return true; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$meta = wp_get_attachment_metadata( $post_id ); |
|
55
|
|
|
|
|
56
|
|
|
if ( self::is_display_embed_valid( $display_embed ) ) { |
|
57
|
|
|
$meta['videopress']['display_embed'] = (bool) $values['display_embed']; // convert it to bool since that's how we store it on wp-admin side. |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if ( isset( $values['rating'] ) ) { |
|
61
|
|
|
$meta['videopress']['rating'] = $values['rating']; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
wp_update_attachment_metadata( $post_id, $meta ); |
|
65
|
|
|
|
|
66
|
|
|
return true; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Check if the given media item is a VideoPress file. |
|
71
|
|
|
* |
|
72
|
|
|
* @param stdClass $item The media item. |
|
73
|
|
|
* |
|
74
|
|
|
* @return bool |
|
75
|
|
|
*/ |
|
76
|
|
|
public static function is_videopress_media( $item ) { |
|
77
|
|
|
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
|
78
|
|
|
return 0 === strpos( $item->mime_type, 'video/' ); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// Else, we are in Jetpack and we need to check if the video is video/videopress. |
|
82
|
|
|
return 'video/videopress' === $item->mime_type; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Check if display_embed has valid values. |
|
87
|
|
|
* |
|
88
|
|
|
* @param mixed $display_embed The input display embed. |
|
89
|
|
|
* |
|
90
|
|
|
* @return bool |
|
91
|
|
|
*/ |
|
92
|
|
|
private static function is_display_embed_valid( $display_embed ) { |
|
93
|
|
|
return ( 0 === $display_embed || 1 === $display_embed ); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Validate the response received from WPCOM. |
|
98
|
|
|
* |
|
99
|
|
|
* @param array|\WP_Error $result The result returned by the client. |
|
100
|
|
|
*/ |
|
101
|
|
|
private static function validate_result( $result ) { |
|
102
|
|
|
$response_code = isset( $result['response']['code'] ) ? $result['response']['code'] : 500; |
|
103
|
|
|
|
|
104
|
|
|
// When Client::wpcom_json_api_request_as_blog is called in WPCOM, bad response codes are not converted to WP_Error. |
|
105
|
|
|
// Because of this, we need to manually check the response code to check if the direct API call is 200 (OK). |
|
106
|
|
|
if ( 200 === $response_code && ! is_wp_error( $result ) ) { |
|
107
|
|
|
return true; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$error_message = __( |
|
111
|
|
|
'There was an issue saving your updates to the VideoPress service. Please try again later.', |
|
112
|
|
|
'jetpack' |
|
113
|
|
|
); |
|
114
|
|
|
|
|
115
|
|
|
$error_code = $response_code; |
|
116
|
|
|
|
|
117
|
|
|
if ( is_wp_error( $result ) ) { |
|
118
|
|
|
$error_code = $result->get_error_code(); |
|
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return new \WP_Error( $error_code, $error_message ); |
|
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Build the request values that will be passed to the WPCOM API. |
|
126
|
|
|
* |
|
127
|
|
|
* @param string $post_title The video title. |
|
128
|
|
|
* @param string $caption The video caption. |
|
129
|
|
|
* @param string $post_excerpt The except. |
|
130
|
|
|
* @param string $rating The video rating. |
|
131
|
|
|
* @param string $display_embed The video display_embed. |
|
132
|
|
|
* |
|
133
|
|
|
* @return array |
|
134
|
|
|
*/ |
|
135
|
|
|
private static function build_wpcom_api_request_values( $post_title, $caption, $post_excerpt, $rating, $display_embed ) { |
|
136
|
|
|
$values = array(); |
|
137
|
|
|
|
|
138
|
|
|
// Add the video title & description in, so that we save it properly. |
|
139
|
|
|
if ( isset( $post_title ) ) { |
|
140
|
|
|
$values['title'] = trim( wp_strip_all_tags( $post_title ) ); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
if ( isset( $caption ) ) { |
|
144
|
|
|
$values['caption'] = trim( wp_strip_all_tags( $caption ) ); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
if ( isset( $post_excerpt ) ) { |
|
148
|
|
|
$values['description'] = trim( wp_strip_all_tags( $post_excerpt ) ); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
if ( isset( $rating ) ) { |
|
152
|
|
|
$values['rating'] = $rating; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
if ( self::is_display_embed_valid( $display_embed ) ) { |
|
156
|
|
|
$values['display_embed'] = $display_embed; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return $values; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|