|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Validate user-supplied guid values against expected inputs |
|
5
|
|
|
* |
|
6
|
|
|
* @since 1.1 |
|
7
|
|
|
* @param string $guid video identifier |
|
8
|
|
|
* @return bool true if passes validation test |
|
9
|
|
|
*/ |
|
10
|
|
|
function videopress_is_valid_guid( $guid ) { |
|
11
|
|
|
if ( ! empty( $guid ) && strlen( $guid ) === 8 && ctype_alnum( $guid ) ) { |
|
12
|
|
|
return true; |
|
13
|
|
|
} |
|
14
|
|
|
return false; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Get details about a specific video by GUID: |
|
19
|
|
|
* |
|
20
|
|
|
* @param $guid string |
|
21
|
|
|
* @return object |
|
22
|
|
|
*/ |
|
23
|
|
|
function videopress_get_video_details( $guid ) { |
|
24
|
|
|
if ( ! videopress_is_valid_guid( $guid ) ) { |
|
25
|
|
|
return new WP_Error( 'bad-guid-format', __( 'Invalid Video GUID!', 'jetpack' ) ); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
$version = '1.1'; |
|
29
|
|
|
$endpoint = sprintf( '/videos/%1$s', $guid ); |
|
30
|
|
|
$response = wp_remote_get( sprintf( 'https://public-api.wordpress.com/rest/v%1$s%2$s', $version, $endpoint ) ); |
|
31
|
|
|
$data = json_decode( wp_remote_retrieve_body( $response ) ); |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Allow functions to modify fetched video details. |
|
35
|
|
|
* |
|
36
|
|
|
* This filter allows third-party code to modify the return data |
|
37
|
|
|
* about a given video. It may involve swapping some data out or |
|
38
|
|
|
* adding new parameters. |
|
39
|
|
|
* |
|
40
|
|
|
* @since 4.0.0 |
|
41
|
|
|
* |
|
42
|
|
|
* @param object $data The data returned by the WPCOM API. See: https://developer.wordpress.com/docs/api/1.1/get/videos/%24guid/ |
|
43
|
|
|
* @param string $guid The GUID of the VideoPress video in question. |
|
44
|
|
|
*/ |
|
45
|
|
|
return apply_filters( 'videopress_get_video_details', $data, $guid ); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get an attachment ID given a URL. |
|
51
|
|
|
* |
|
52
|
|
|
* Modified from http://wpscholar.com/blog/get-attachment-id-from-wp-image-url/ |
|
53
|
|
|
* |
|
54
|
|
|
* @todo: Add some caching in here. |
|
|
|
|
|
|
55
|
|
|
* |
|
56
|
|
|
* @param string $url |
|
57
|
|
|
* |
|
58
|
|
|
* @return int|bool Attachment ID on success, false on failure |
|
59
|
|
|
*/ |
|
60
|
|
|
function videopress_get_attachment_id_by_url( $url ) { |
|
61
|
|
|
$wp_upload_dir = wp_upload_dir(); |
|
62
|
|
|
// Strip out protocols, so it doesn't fail because searching for http: in https: dir. |
|
63
|
|
|
$dir = set_url_scheme( trailingslashit( $wp_upload_dir['baseurl'] ), 'relative' ); |
|
64
|
|
|
|
|
65
|
|
|
// Is URL in uploads directory? |
|
66
|
|
|
if ( false !== strpos( $url, $dir ) ) { |
|
67
|
|
|
|
|
68
|
|
|
$file = basename( $url ); |
|
69
|
|
|
|
|
70
|
|
|
$query_args = array( |
|
71
|
|
|
'post_type' => 'attachment', |
|
72
|
|
|
'post_status' => 'inherit', |
|
73
|
|
|
'fields' => 'ids', |
|
74
|
|
|
'meta_query' => array( |
|
75
|
|
|
array( |
|
76
|
|
|
'key' => '_wp_attachment_metadata', |
|
77
|
|
|
'compare' => 'LIKE', |
|
78
|
|
|
'value' => $file, |
|
79
|
|
|
), |
|
80
|
|
|
) |
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
|
|
$query = new WP_Query( $query_args ); |
|
84
|
|
|
|
|
85
|
|
|
if ( $query->have_posts() ) { |
|
86
|
|
|
foreach ( $query->posts as $attachment_id ) { |
|
87
|
|
|
$meta = wp_get_attachment_metadata( $attachment_id ); |
|
88
|
|
|
$original_file = basename( $meta['file'] ); |
|
89
|
|
|
$cropped_files = wp_list_pluck( $meta['sizes'], 'file' ); |
|
90
|
|
|
|
|
91
|
|
|
if ( $original_file === $file || in_array( $file, $cropped_files ) ) { |
|
92
|
|
|
return (int) $attachment_id; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
return false; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Return an absolute URI for a given filename and guid on the CDN. |
|
103
|
|
|
* No check is performed to ensure the guid exists or the file is present. Simple centralized string builder. |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $guid VideoPress identifier |
|
106
|
|
|
* @param string $filename name of file associated with the guid (video file name or thumbnail file name) |
|
107
|
|
|
* @return string Absolute URL of VideoPress file for the given guid. |
|
108
|
|
|
*/ |
|
109
|
|
|
function video_cdn_file_url( $guid, $filename ) { |
|
110
|
|
|
if ( is_ssl() ) { |
|
111
|
|
|
return "https://videos.files.wordpress.com/{$guid}/{$filename}"; |
|
112
|
|
|
|
|
113
|
|
|
} else { |
|
114
|
|
|
return "http://videos.videopress.com/{$guid}/{$filename}"; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Wrapper function to extract the status of a particular clip from the database row. |
|
121
|
|
|
* |
|
122
|
|
|
* @param array $info single row from the videos table |
|
123
|
|
|
* @param string $format named video format |
|
124
|
|
|
* @return string format status string, or empty string if no match |
|
125
|
|
|
*/ |
|
126
|
|
|
function videopress_format_status( $info, $format ){ |
|
127
|
|
|
|
|
128
|
|
|
if ( empty( $info ) || empty( $format ) || ! videopress_is_valid_format($format) ) { |
|
129
|
|
|
return ''; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
if ( $format == 'fmt_std' || $format == 'fmt_dvd' || $format == 'fmt_hd' ) { |
|
133
|
|
|
return $info->$format; |
|
134
|
|
|
|
|
135
|
|
|
} elseif ( $format == 'fmt1_ogg' ) { |
|
136
|
|
|
if ( empty( $info->fmts_ogg ) ) { |
|
137
|
|
|
return ''; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$r = preg_match( '/fmt1_ogg:([\w-]+);/', $info->fmts_ogg, $m ); |
|
141
|
|
|
if ( $r === 0 || $r === false ) { |
|
142
|
|
|
return ''; |
|
143
|
|
|
} else { |
|
144
|
|
|
return $m[1]; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Defines valid named format types |
|
152
|
|
|
* |
|
153
|
|
|
* @param string $format named video format checked against master list |
|
154
|
|
|
* @return bool true if given named format is valid, else false |
|
155
|
|
|
**/ |
|
156
|
|
|
function videopress_is_valid_format($format) { |
|
157
|
|
|
static $valid_formats = array( 'fmt_std', 'fmt_dvd', 'fmt_hd', 'fmt1_ogg' ); |
|
158
|
|
|
if ( !empty( $format ) && in_array( $format, $valid_formats ) ) { |
|
159
|
|
|
return true; |
|
160
|
|
|
|
|
161
|
|
|
} else { |
|
162
|
|
|
return false; |
|
163
|
|
|
} |
|
164
|
|
|
} |
This check looks
TODOcomments that have been left in the code.``TODO``s show that something is left unfinished and should be attended to.