class.videopress-xmlrpc.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * VideoPress playback module markup generator.
  4. *
  5. * @since 1.3
  6. */
  7. class VideoPress_XMLRPC {
  8. /**
  9. * @var VideoPress_XMLRPC
  10. **/
  11. private static $instance = null;
  12. /**
  13. * Private VideoPress_XMLRPC constructor.
  14. *
  15. * Use the VideoPress_XMLRPC::init() method to get an instance.
  16. */
  17. private function __construct() {
  18. add_filter( 'jetpack_xmlrpc_methods', array( $this, 'xmlrpc_methods' ) );
  19. }
  20. /**
  21. * Initialize the VideoPress_XMLRPC and get back a singleton instance.
  22. *
  23. * @return VideoPress_XMLRPC
  24. */
  25. public static function init() {
  26. if ( is_null( self::$instance ) ) {
  27. self::$instance = new VideoPress_XMLRPC;
  28. }
  29. return self::$instance;
  30. }
  31. /**
  32. * Adds additional methods the WordPress xmlrpc API for handling VideoPress specific features
  33. *
  34. * @param array $methods
  35. *
  36. * @return array
  37. */
  38. public function xmlrpc_methods( $methods ) {
  39. $methods['jetpack.createMediaItem'] = array( $this, 'create_media_item' );
  40. $methods['jetpack.updateVideoPressMediaItem'] = array( $this, 'update_videopress_media_item' );
  41. $methods['jetpack.updateVideoPressPosterImage'] = array( $this, 'update_poster_image' );
  42. return $methods;
  43. }
  44. /**
  45. * This is used by the WPCOM VideoPress uploader in order to create a media item with
  46. * specific meta data about an uploaded file. After this, the transcoding session will
  47. * update the meta information via the update_videopress_media_item() method.
  48. *
  49. * Note: This method technically handles the creation of multiple media objects, though
  50. * in practice this is never done.
  51. *
  52. * @param array $media
  53. * @return array
  54. */
  55. public function create_media_item( $media ) {
  56. foreach ( $media as & $media_item ) {
  57. $title = sanitize_title( basename( $media_item['url'] ) );
  58. $guid = isset( $media['guid'] ) ? $media['guid'] : null;
  59. $media_id = videopress_create_new_media_item( $title, $guid );
  60. wp_update_attachment_metadata( $media_id, array(
  61. 'original' => array(
  62. 'url' => $media_item['url'],
  63. ),
  64. ) );
  65. $media_item['post'] = get_post( $media_id );
  66. }
  67. return array( 'media' => $media );
  68. }
  69. /**
  70. * @param array $request
  71. *
  72. * @return bool
  73. */
  74. public function update_videopress_media_item( $request ) {
  75. $id = $request['post_id'];
  76. $status = $request['status'];
  77. $format = $request['format'];
  78. $info = $request['info'];
  79. if ( ! $attachment = get_post( $id ) ) {
  80. return false;
  81. }
  82. $attachment->guid = $info['original'];
  83. wp_update_post( $attachment );
  84. // Update the vp guid and set it to a direct meta property.
  85. update_post_meta( $id, 'videopress_guid', $info['guid'] );
  86. $meta = wp_get_attachment_metadata( $id );
  87. $meta['width'] = $info['width'];
  88. $meta['height'] = $info['height'];
  89. $meta['original']['url'] = $info['original'];
  90. $meta['videopress'] = $info;
  91. $meta['videopress']['url'] = 'https://videopress.com/v/' . $info['guid'];
  92. // Update file statuses
  93. $valid_formats = array( 'hd', 'ogg', 'mp4', 'dvd' );
  94. if ( in_array( $format, $valid_formats ) ) {
  95. $meta['file_statuses'][ $format ] = $status;
  96. }
  97. if ( ! get_post_meta( $id, '_thumbnail_id', true ) ) {
  98. // Update the poster in the VideoPress info.
  99. $thumbnail_id = videopress_download_poster_image( $info['poster'], $id );
  100. if ( is_int( $thumbnail_id ) ) {
  101. update_post_meta( $id, '_thumbnail_id', $thumbnail_id );
  102. }
  103. }
  104. wp_update_attachment_metadata( $id, $meta );
  105. videopress_update_meta_data( $id );
  106. // update the meta to tell us that we're processing or complete
  107. update_post_meta( $id, 'videopress_status', videopress_is_finished_processing( $id ) ? 'complete' : 'processing' );
  108. return true;
  109. }
  110. /**
  111. * @param array $request
  112. * @return bool
  113. */
  114. public function update_poster_image( $request ) {
  115. $post_id = $request['post_id'];
  116. $poster = $request['poster'];
  117. if ( ! $attachment = get_post( $post_id ) ) {
  118. return false;
  119. }
  120. // We add ssl => 1 to make sure that the videos.files.wordpress.com domain is parsed as photon.
  121. $poster = apply_filters( 'jetpack_photon_url', $poster, array( 'ssl' => 1 ), 'https' );
  122. $meta = wp_get_attachment_metadata( $post_id );
  123. $meta['videopress']['poster'] = $poster;
  124. wp_update_attachment_metadata( $post_id, $meta );
  125. // Update the poster in the VideoPress info.
  126. $thumbnail_id = videopress_download_poster_image( $poster, $post_id );
  127. if ( ! is_int( $thumbnail_id ) ) {
  128. return false;
  129. }
  130. update_post_meta( $post_id, '_thumbnail_id', $thumbnail_id );
  131. return true;
  132. }
  133. }