Нет описания

class.videopress-xmlrpc.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. * The current user object.
  14. *
  15. * @var WP_User
  16. */
  17. private $current_user;
  18. /**
  19. * Private VideoPress_XMLRPC constructor.
  20. *
  21. * Use the VideoPress_XMLRPC::init() method to get an instance.
  22. */
  23. private function __construct() {
  24. add_filter( 'jetpack_xmlrpc_methods', array( $this, 'xmlrpc_methods' ), 10, 3 );
  25. }
  26. /**
  27. * Initialize the VideoPress_XMLRPC and get back a singleton instance.
  28. *
  29. * @return VideoPress_XMLRPC
  30. */
  31. public static function init() {
  32. if ( is_null( self::$instance ) ) {
  33. self::$instance = new VideoPress_XMLRPC();
  34. }
  35. return self::$instance;
  36. }
  37. /**
  38. * Adds additional methods the WordPress xmlrpc API for handling VideoPress specific features
  39. *
  40. * @param array $methods The Jetpack API methods.
  41. * @param array $core_methods The WordPress Core API methods (ignored).
  42. * @param WP_User $user The user object the API request is signed by.
  43. *
  44. * @return array
  45. */
  46. public function xmlrpc_methods( $methods, $core_methods, $user ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
  47. if ( $user && $user instanceof WP_User ) {
  48. $this->current_user = $user;
  49. }
  50. $methods['jetpack.createMediaItem'] = array( $this, 'create_media_item' );
  51. $methods['jetpack.updateVideoPressMediaItem'] = array( $this, 'update_videopress_media_item' );
  52. $methods['jetpack.updateVideoPressPosterImage'] = array( $this, 'update_poster_image' );
  53. return $methods;
  54. }
  55. /**
  56. * This is used by the WPCOM VideoPress uploader in order to create a media item with
  57. * specific meta data about an uploaded file. After this, the transcoding session will
  58. * update the meta information via the update_videopress_media_item() method.
  59. *
  60. * Note: This method technically handles the creation of multiple media objects, though
  61. * in practice this is never done.
  62. *
  63. * @param array $media
  64. * @return array
  65. */
  66. public function create_media_item( $media ) {
  67. $this->authenticate_user();
  68. foreach ( $media as & $media_item ) {
  69. $title = sanitize_title( basename( $media_item['url'] ) );
  70. $guid = isset( $media['guid'] ) ? $media['guid'] : null;
  71. $media_id = videopress_create_new_media_item( $title, $guid );
  72. wp_update_attachment_metadata(
  73. $media_id,
  74. array(
  75. 'original' => array(
  76. 'url' => $media_item['url'],
  77. ),
  78. )
  79. );
  80. $media_item['post'] = get_post( $media_id );
  81. }
  82. return array( 'media' => $media );
  83. }
  84. /**
  85. * @param array $request
  86. *
  87. * @return bool
  88. */
  89. public function update_videopress_media_item( $request ) {
  90. $this->authenticate_user();
  91. $id = $request['post_id'];
  92. $status = $request['status'];
  93. $format = $request['format'];
  94. $info = $request['info'];
  95. if ( ! $attachment = get_post( $id ) ) {
  96. return false;
  97. }
  98. $attachment->guid = $info['original'];
  99. $attachment->post_mime_type = 'video/videopress';
  100. wp_update_post( $attachment );
  101. // Update the vp guid and set it to a direct meta property.
  102. update_post_meta( $id, 'videopress_guid', $info['guid'] );
  103. $meta = wp_get_attachment_metadata( $id );
  104. $meta['width'] = $info['width'];
  105. $meta['height'] = $info['height'];
  106. $meta['original']['url'] = $info['original'];
  107. $meta['videopress'] = $info;
  108. $meta['videopress']['url'] = 'https://videopress.com/v/' . $info['guid'];
  109. // Update file statuses
  110. if ( ! empty( $format ) ) {
  111. $meta['file_statuses'][ $format ] = $status;
  112. }
  113. if ( ! get_post_meta( $id, '_thumbnail_id', true ) ) {
  114. // Update the poster in the VideoPress info.
  115. $thumbnail_id = videopress_download_poster_image( $info['poster'], $id );
  116. if ( is_int( $thumbnail_id ) ) {
  117. update_post_meta( $id, '_thumbnail_id', $thumbnail_id );
  118. }
  119. }
  120. wp_update_attachment_metadata( $id, $meta );
  121. videopress_update_meta_data( $id );
  122. // update the meta to tell us that we're processing or complete
  123. update_post_meta( $id, 'videopress_status', videopress_is_finished_processing( $id ) ? 'complete' : 'processing' );
  124. return true;
  125. }
  126. /**
  127. * @param array $request
  128. * @return bool
  129. */
  130. public function update_poster_image( $request ) {
  131. $this->authenticate_user();
  132. $post_id = $request['post_id'];
  133. $poster = $request['poster'];
  134. if ( ! $attachment = get_post( $post_id ) ) {
  135. return false;
  136. }
  137. $poster = apply_filters( 'jetpack_photon_url', $poster );
  138. $meta = wp_get_attachment_metadata( $post_id );
  139. $meta['videopress']['poster'] = $poster;
  140. wp_update_attachment_metadata( $post_id, $meta );
  141. // Update the poster in the VideoPress info.
  142. $thumbnail_id = videopress_download_poster_image( $poster, $post_id );
  143. if ( ! is_int( $thumbnail_id ) ) {
  144. return false;
  145. }
  146. update_post_meta( $post_id, '_thumbnail_id', $thumbnail_id );
  147. return true;
  148. }
  149. /**
  150. * Check if the XML-RPC request is signed by a user token, and authenticate the user in WordPress.
  151. *
  152. * @return bool
  153. */
  154. private function authenticate_user() {
  155. if ( $this->current_user ) {
  156. wp_set_current_user( $this->current_user->ID );
  157. return true;
  158. }
  159. return false;
  160. }
  161. }