← All changes
|
jetpack_vendor/automattic/jetpack-videopress/src/class-attachment-handler.php
+102
-5
12.3.2
→
16.3
View file →
| @@ -8,8 +8,10 @@ | ||
| 8 | 8 | namespace Automattic\Jetpack\VideoPress; |
| 9 | 9 | |
| 10 | 10 | use Automattic\Jetpack\Connection\Client; |
| 11 | 11 | use Automattic\Jetpack\Current_Plan; |
| 12 | +use WP_Error; | |
| 13 | +use WP_Post; | |
| 12 | 14 | |
| 13 | 15 | /** |
| 14 | 16 | * VideoPress Attachment_Handler class. |
| 15 | 17 | */ |
| @@ -41,8 +43,12 @@ | ||
| 41 | 43 | add_filter( 'ajax_query_attachments_args', array( __CLASS__, 'ajax_query_attachments_args' ) ); |
| 42 | 44 | add_action( 'pre_get_posts', array( __CLASS__, 'media_list_table_query' ) ); |
| 43 | 45 | |
| 44 | 46 | add_filter( 'user_has_cap', array( __CLASS__, 'disable_delete_if_disconnected' ), 10, 3 ); |
| 47 | + | |
| 48 | + add_action( 'admin_print_scripts-upload.php', array( __CLASS__, 'enqueue_media_library_poll' ) ); | |
| 49 | + add_action( 'admin_print_styles-upload.php', array( __CLASS__, 'enqueue_media_library_styles' ) ); | |
| 50 | + add_filter( 'heartbeat_received', array( __CLASS__, 'heartbeat_received' ), 10, 2 ); | |
| 45 | 51 | } |
| 46 | 52 | |
| 47 | 53 | /** |
| 48 | 54 | * Returns the VideoPress URL for the give post id, otherwise returns the provided default. |
| @@ -115,9 +121,10 @@ | ||
| 115 | 121 | // Phone home and have wp.com delete the VideoPress entry and files. |
| 116 | 122 | $wpcom_response = Client::wpcom_json_api_request_as_blog( |
| 117 | 123 | sprintf( '/videos/%s/delete', $guid ), |
| 118 | 124 | '1.1', |
| 119 | - array( 'method' => 'POST' ) | |
| 125 | + array( 'method' => 'POST' ), | |
| 126 | + array( 'user_id' => get_current_user_id() ) | |
| 120 | 127 | ); |
| 121 | 128 | |
| 122 | 129 | if ( is_wp_error( $wpcom_response ) ) { |
| 123 | 130 | return $wpcom_response; |
| @@ -187,16 +194,21 @@ | ||
| 187 | 194 | |
| 188 | 195 | /** |
| 189 | 196 | * Make sure that any Video that has a VideoPress GUID passes that data back. |
| 190 | 197 | * |
| 191 | - * @param WP_Post $post Attachment object. | |
| 198 | + * @param array $post Attachment data array. | |
| 199 | + * @return array | |
| 192 | 200 | */ |
| 193 | 201 | public static function prepare_attachment_for_js( $post ) { |
| 194 | 202 | if ( 'video' === $post['type'] ) { |
| 195 | - $guid = get_post_meta( $post['id'], 'videopress_guid' ); | |
| 203 | + $guid = get_post_meta( $post['id'], 'videopress_guid', true ); | |
| 196 | 204 | if ( $guid ) { |
| 197 | 205 | $post['videopress_guid'] = $guid; |
| 198 | 206 | } |
| 207 | + $status = get_post_meta( $post['id'], 'videopress_status', true ); | |
| 208 | + if ( $status ) { | |
| 209 | + $post['videopress_status'] = $status; | |
| 210 | + } | |
| 199 | 211 | } |
| 200 | 212 | return $post; |
| 201 | 213 | } |
| 202 | 214 | |
| @@ -272,14 +284,14 @@ | ||
| 272 | 284 | */ |
| 273 | 285 | public static function disable_delete_if_disconnected( $allcaps, $cap, $args ) { |
| 274 | 286 | |
| 275 | 287 | // Only apply this filter to `delete_post` checks |
| 276 | - if ( 'delete_post' !== $args[0] ) { | |
| 288 | + if ( ! isset( $args[0] ) || 'delete_post' !== $args[0] ) { | |
| 277 | 289 | return $allcaps; |
| 278 | 290 | } |
| 279 | 291 | |
| 280 | 292 | // Only apply this filter to VideoPress attachments |
| 281 | - if ( ! is_videopress_attachment( $args[2] ) ) { | |
| 293 | + if ( ! isset( $args[2] ) || ! is_videopress_attachment( $args[2] ) ) { | |
| 282 | 294 | return $allcaps; |
| 283 | 295 | } |
| 284 | 296 | |
| 285 | 297 | // Set the capability to false if the user can't perform the actions |
| @@ -287,6 +299,91 @@ | ||
| 287 | 299 | $allcaps[ $cap[0] ] = false; |
| 288 | 300 | } |
| 289 | 301 | |
| 290 | 302 | return $allcaps; |
| 303 | + } | |
| 304 | + | |
| 305 | + /** | |
| 306 | + * Enqueues the script that hooks into WP Heartbeat to refresh | |
| 307 | + * processing VideoPress video data in the media library grid. | |
| 308 | + * | |
| 309 | + * @since 0.35.4 | |
| 310 | + * | |
| 311 | + * @return void | |
| 312 | + */ | |
| 313 | + public static function enqueue_media_library_poll() { | |
| 314 | + wp_enqueue_script( | |
| 315 | + 'videopress-media-library-poll', | |
| 316 | + plugins_url( 'js/media-library-poll.js', __FILE__ ), | |
| 317 | + array( 'jquery', 'heartbeat', 'media-grid' ), | |
| 318 | + Package_Version::PACKAGE_VERSION, | |
| 319 | + true | |
| 320 | + ); | |
| 321 | + | |
| 322 | + add_filter( 'heartbeat_settings', array( __CLASS__, 'heartbeat_settings' ) ); | |
| 323 | + } | |
| 324 | + | |
| 325 | + /** | |
| 326 | + * Adds inline styles for the media library grid on the upload page. | |
| 327 | + * | |
| 328 | + * @since 0.35.4 | |
| 329 | + * | |
| 330 | + * @return void | |
| 331 | + */ | |
| 332 | + public static function enqueue_media_library_styles() { | |
| 333 | + // Constrain poster images so they fit within the media library grid cell. | |
| 334 | + wp_add_inline_style( | |
| 335 | + 'media-views', | |
| 336 | + '.attachment-preview.type-video .thumbnail .centered img.thumbnail { max-width: 100%; max-height: 100%; }' | |
| 337 | + ); | |
| 338 | + } | |
| 339 | + | |
| 340 | + /** | |
| 341 | + * Lowers the Heartbeat minimum interval on the media library page | |
| 342 | + * so the polling script can request a faster tick rate. | |
| 343 | + * | |
| 344 | + * @since 0.35.4 | |
| 345 | + * | |
| 346 | + * @param array $settings Heartbeat settings. | |
| 347 | + * @return array | |
| 348 | + */ | |
| 349 | + public static function heartbeat_settings( $settings ) { | |
| 350 | + $settings['minimalInterval'] = 5; | |
| 351 | + | |
| 352 | + return $settings; | |
| 353 | + } | |
| 354 | + | |
| 355 | + /** | |
| 356 | + * Heartbeat API handler that checks the processing status of VideoPress videos. | |
| 357 | + * | |
| 358 | + * @since 0.35.4 | |
| 359 | + * | |
| 360 | + * @param array $response The Heartbeat response. | |
| 361 | + * @param array $data The data sent with the Heartbeat request. | |
| 362 | + * @return array | |
| 363 | + */ | |
| 364 | + public static function heartbeat_received( $response, $data ) { | |
| 365 | + if ( empty( $data['videopress_processing_ids'] ) || ! is_array( $data['videopress_processing_ids'] ) ) { | |
| 366 | + return $response; | |
| 367 | + } | |
| 368 | + | |
| 369 | + $statuses = array(); | |
| 370 | + foreach ( $data['videopress_processing_ids'] as $id ) { | |
| 371 | + $id = (int) $id; | |
| 372 | + | |
| 373 | + if ( ! current_user_can( 'edit_post', $id ) ) { | |
| 374 | + continue; | |
| 375 | + } | |
| 376 | + | |
| 377 | + $status = get_post_meta( $id, 'videopress_status', true ); | |
| 378 | + if ( $status ) { | |
| 379 | + $statuses[ $id ] = $status; | |
| 380 | + } | |
| 381 | + } | |
| 382 | + | |
| 383 | + if ( ! empty( $statuses ) ) { | |
| 384 | + $response['videopress_processing_status'] = $statuses; | |
| 385 | + } | |
| 386 | + | |
| 387 | + return $response; | |
| 291 | 388 | } |
| 292 | 389 | } |