← All changes
|
jetpack_vendor/automattic/jetpack-videopress/src/class-data.php
+144
-0
16.2
→
16.3-beta
View file →
| @@ -72,8 +72,30 @@ | ||
| 72 | 72 | return boolval( get_option( 'videopress_player_preload_disabled', false ) ); |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | /** |
| 76 | + * Gets whether embeds render an inline player instead of one iframe per video. | |
| 77 | + * | |
| 78 | + * Iframes are the default, so this opt-in option defaults to false. | |
| 79 | + * | |
| 80 | + * @return boolean If embeds should mount players in the page from one shared player script. | |
| 81 | + */ | |
| 82 | + public static function get_videopress_inline_player_enabled() { | |
| 83 | + return boolval( get_option( 'videopress_inline_player_enabled', false ) ); | |
| 84 | + } | |
| 85 | + | |
| 86 | + /** | |
| 87 | + * Gets whether the share menu is turned off for every video on the site. | |
| 88 | + * | |
| 89 | + * Sharing follows each video's own setting by default, so this opt-out option defaults to false. | |
| 90 | + * | |
| 91 | + * @return boolean If no video may display the share menu, whatever its own setting. | |
| 92 | + */ | |
| 93 | + public static function get_videopress_share_menu_disabled() { | |
| 94 | + return boolval( get_option( 'videopress_share_menu_disabled', false ) ); | |
| 95 | + } | |
| 96 | + | |
| 97 | + /** | |
| 76 | 98 | * Gets the VideoPress Settings. |
| 77 | 99 | * |
| 78 | 100 | * @return array The settings as an associative array. |
| 79 | 101 | */ |
| @@ -92,8 +114,10 @@ | ||
| 92 | 114 | return array( |
| 93 | 115 | 'videopress_videos_private_for_site' => self::get_videopress_videos_private_for_site(), |
| 94 | 116 | 'videopress_auto_subtitles_disabled' => self::get_videopress_auto_subtitles_disabled(), |
| 95 | 117 | 'videopress_player_preload_disabled' => self::get_videopress_player_preload_disabled(), |
| 118 | + 'videopress_inline_player_enabled' => self::get_videopress_inline_player_enabled(), | |
| 119 | + 'videopress_share_menu_disabled' => self::get_videopress_share_menu_disabled(), | |
| 96 | 120 | 'site_is_private' => $site_is_private, |
| 97 | 121 | 'site_type' => $site_type, |
| 98 | 122 | ); |
| 99 | 123 | } |
| @@ -153,8 +177,128 @@ | ||
| 153 | 177 | $video_data['totalPages'] = $headers['X-WP-TotalPages']; |
| 154 | 178 | } |
| 155 | 179 | |
| 156 | 180 | return $video_data; |
| 181 | + } | |
| 182 | + | |
| 183 | + /** | |
| 184 | + * Gets the site's newest VideoPress videos as playlist entries, newest first. | |
| 185 | + * | |
| 186 | + * Goes through the media REST endpoint rather than a direct WP_Query so the | |
| 187 | + * package's rest_attachment_query filter resolves VideoPress membership on | |
| 188 | + * every host: by mime off WordPress.com Simple, from wpcom's videos table on it. | |
| 189 | + * The editor's block preview sends the same request. | |
| 190 | + * | |
| 191 | + * @param int $count How many videos to fetch. | |
| 192 | + * | |
| 193 | + * @return array Entries with guid, durationMs and height keys. | |
| 194 | + */ | |
| 195 | + public static function get_latest_videopress_playlist_entries( $count ) { | |
| 196 | + $count = max( 1, (int) $count ); | |
| 197 | + $args = array( | |
| 198 | + 'per_page' => $count, | |
| 199 | + 'orderby' => 'date', | |
| 200 | + 'order' => 'desc', | |
| 201 | + 'videopress_has_guid' => 1, | |
| 202 | + ); | |
| 203 | + | |
| 204 | + // Attachments keep their original mime there, so narrow to videos before the ID-set constraint. | |
| 205 | + if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { | |
| 206 | + $args['videopress_only_videos'] = 1; | |
| 207 | + } | |
| 208 | + | |
| 209 | + $request = new WP_REST_Request( 'GET', '/wp/v2/media' ); | |
| 210 | + $request->set_query_params( $args ); | |
| 211 | + $response = rest_do_request( $request ); | |
| 212 | + | |
| 213 | + // WordPress.com Simple registers the wp/v2 routes only for REST requests, so on a | |
| 214 | + // page render the media endpoint does not exist: query the attachments directly. | |
| 215 | + if ( $response->is_error() ) { | |
| 216 | + return self::query_latest_videopress_playlist_entries( $count ); | |
| 217 | + } | |
| 218 | + | |
| 219 | + $entries = array(); | |
| 220 | + foreach ( (array) $response->get_data() as $item ) { | |
| 221 | + $item = (array) $item; | |
| 222 | + $guid = $item['jetpack_videopress_guid'] ?? ''; | |
| 223 | + | |
| 224 | + // The REST field is empty on a site without a connection; the attachment meta still knows. | |
| 225 | + if ( ( ! is_string( $guid ) || '' === $guid ) && ! empty( $item['id'] ) ) { | |
| 226 | + $guid = get_post_meta( (int) $item['id'], 'videopress_guid', true ); | |
| 227 | + } | |
| 228 | + | |
| 229 | + $details = isset( $item['media_details'] ) ? (array) $item['media_details'] : array(); | |
| 230 | + $videopress = isset( $details['videopress'] ) ? (array) $details['videopress'] : array(); | |
| 231 | + $entry = self::playlist_entry( $guid, $videopress['duration'] ?? 0, $details['height'] ?? $videopress['height'] ?? 0 ); | |
| 232 | + if ( $entry ) { | |
| 233 | + $entries[] = $entry; | |
| 234 | + } | |
| 235 | + } | |
| 236 | + | |
| 237 | + return $entries; | |
| 238 | + } | |
| 239 | + | |
| 240 | + /** | |
| 241 | + * The newest VideoPress videos straight from the posts table, for hosts where the | |
| 242 | + * media endpoint is unavailable. Mirrors what the endpoint does with the package's | |
| 243 | + * query filter: by mime off WordPress.com Simple, from wpcom's videos table on it. | |
| 244 | + * | |
| 245 | + * @param int $count How many videos to fetch. | |
| 246 | + * | |
| 247 | + * @return array Entries with guid, durationMs and height keys. | |
| 248 | + */ | |
| 249 | + private static function query_latest_videopress_playlist_entries( $count ) { | |
| 250 | + $is_wpcom = defined( 'IS_WPCOM' ) && IS_WPCOM; | |
| 251 | + $posts = get_posts( | |
| 252 | + array( | |
| 253 | + 'post_type' => 'attachment', | |
| 254 | + 'post_status' => 'inherit', | |
| 255 | + 'post_mime_type' => $is_wpcom ? 'video' : 'video/videopress', | |
| 256 | + 'orderby' => 'date', | |
| 257 | + 'order' => 'DESC', | |
| 258 | + 'posts_per_page' => $count, | |
| 259 | + ) | |
| 260 | + ); | |
| 261 | + | |
| 262 | + $entries = array(); | |
| 263 | + foreach ( $posts as $post ) { | |
| 264 | + $guid = get_post_meta( $post->ID, 'videopress_guid', true ); | |
| 265 | + if ( ( ! is_string( $guid ) || '' === $guid ) && $is_wpcom && function_exists( 'video_get_info_by_blogpostid' ) ) { | |
| 266 | + $info = video_get_info_by_blogpostid( get_current_blog_id(), $post->ID ); | |
| 267 | + $guid = is_object( $info ) && ! empty( $info->guid ) ? (string) $info->guid : ''; | |
| 268 | + } | |
| 269 | + | |
| 270 | + $meta = wp_get_attachment_metadata( $post->ID ); | |
| 271 | + $meta = is_array( $meta ) ? $meta : array(); | |
| 272 | + $videopress = isset( $meta['videopress'] ) ? (array) $meta['videopress'] : array(); | |
| 273 | + $duration = $videopress['duration'] ?? ( isset( $meta['length'] ) && is_numeric( $meta['length'] ) ? (int) $meta['length'] * 1000 : 0 ); | |
| 274 | + $entry = self::playlist_entry( $guid, $duration, $meta['height'] ?? $videopress['height'] ?? 0 ); | |
| 275 | + if ( $entry ) { | |
| 276 | + $entries[] = $entry; | |
| 277 | + } | |
| 278 | + } | |
| 279 | + | |
| 280 | + return $entries; | |
| 281 | + } | |
| 282 | + | |
| 283 | + /** | |
| 284 | + * One playlist entry, or null when the GUID is unusable. | |
| 285 | + * | |
| 286 | + * @param mixed $guid VideoPress GUID. | |
| 287 | + * @param mixed $duration Duration in ms. | |
| 288 | + * @param mixed $height Height in px. | |
| 289 | + * | |
| 290 | + * @return array|null | |
| 291 | + */ | |
| 292 | + private static function playlist_entry( $guid, $duration, $height ) { | |
| 293 | + if ( ! is_string( $guid ) || ! preg_match( '/^[a-zA-Z0-9]{8}$/', $guid ) ) { | |
| 294 | + return null; | |
| 295 | + } | |
| 296 | + return array( | |
| 297 | + 'guid' => $guid, | |
| 298 | + 'durationMs' => is_numeric( $duration ) ? max( 0, (int) $duration ) : 0, | |
| 299 | + 'height' => is_numeric( $height ) ? max( 0, (int) $height ) : 0, | |
| 300 | + ); | |
| 157 | 301 | } |
| 158 | 302 | |
| 159 | 303 | /** |
| 160 | 304 | * Gets the user data |