説明なし

class-videopress-attachment-metadata.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * Handle the VideoPress metadata properties.
  4. *
  5. * @package Jetpack
  6. */
  7. use Automattic\Jetpack\Connection\Client;
  8. /**
  9. * Class Videopress_Attachment_Metadata
  10. */
  11. class Videopress_Attachment_Metadata {
  12. /**
  13. * Persist the VideoPress metadata information, including rating and display_embed.
  14. *
  15. * @param string|int $post_id The post id.
  16. * @param string $guid VideoPress Guid.
  17. * @param string $post_title The post title.
  18. * @param string $caption Video caption.
  19. * @param string $post_excerpt The post excerpt.
  20. * @param string $rating The rating.
  21. * @param int $display_embed The display_embed.
  22. *
  23. * @return bool|\WP_Error
  24. */
  25. public static function persist_metadata( $post_id, $guid, $post_title, $caption, $post_excerpt, $rating, $display_embed ) {
  26. $post_id = absint( $post_id );
  27. $args = array(
  28. 'method' => 'POST',
  29. 'headers' => array( 'content-type' => 'application/json' ),
  30. );
  31. $display_embed = (int) $display_embed;
  32. $values = self::build_wpcom_api_request_values( $post_title, $caption, $post_excerpt, $rating, $display_embed );
  33. $endpoint = 'videos';
  34. $values['guid'] = $guid;
  35. $result = Client::wpcom_json_api_request_as_blog( $endpoint, '2', $args, wp_json_encode( $values ), 'wpcom' );
  36. $validated_result = self::validate_result( $result );
  37. if ( true !== $validated_result ) {
  38. return $validated_result;
  39. }
  40. // If we are in WPCOM, then we don't need to make anything else since we've already updated the video information.
  41. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  42. return true;
  43. }
  44. $meta = wp_get_attachment_metadata( $post_id );
  45. if ( self::is_display_embed_valid( $display_embed ) ) {
  46. $meta['videopress']['display_embed'] = (bool) $values['display_embed']; // convert it to bool since that's how we store it on wp-admin side.
  47. }
  48. if ( isset( $values['rating'] ) ) {
  49. $meta['videopress']['rating'] = $values['rating'];
  50. }
  51. wp_update_attachment_metadata( $post_id, $meta );
  52. return true;
  53. }
  54. /**
  55. * Check if the given media item is a VideoPress file.
  56. *
  57. * @param stdClass $item The media item.
  58. *
  59. * @return bool
  60. */
  61. public static function is_videopress_media( $item ) {
  62. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  63. return 0 === strpos( $item->mime_type, 'video/' );
  64. }
  65. // Else, we are in Jetpack and we need to check if the video is video/videopress.
  66. return 'video/videopress' === $item->mime_type;
  67. }
  68. /**
  69. * Check if display_embed has valid values.
  70. *
  71. * @param mixed $display_embed The input display embed.
  72. *
  73. * @return bool
  74. */
  75. private static function is_display_embed_valid( $display_embed ) {
  76. return ( 0 === $display_embed || 1 === $display_embed );
  77. }
  78. /**
  79. * Validate the response received from WPCOM.
  80. *
  81. * @param array|\WP_Error $result The result returned by the client.
  82. */
  83. private static function validate_result( $result ) {
  84. $response_code = isset( $result['response']['code'] ) ? $result['response']['code'] : 500;
  85. // When Client::wpcom_json_api_request_as_blog is called in WPCOM, bad response codes are not converted to WP_Error.
  86. // Because of this, we need to manually check the response code to check if the direct API call is 200 (OK).
  87. if ( 200 === $response_code && ! is_wp_error( $result ) ) {
  88. return true;
  89. }
  90. $error_message = __(
  91. 'There was an issue saving your updates to the VideoPress service. Please try again later.',
  92. 'jetpack'
  93. );
  94. $error_code = $response_code;
  95. if ( is_wp_error( $result ) ) {
  96. $error_code = $result->get_error_code();
  97. }
  98. return new \WP_Error( $error_code, $error_message );
  99. }
  100. /**
  101. * Build the request values that will be passed to the WPCOM API.
  102. *
  103. * @param string $post_title The video title.
  104. * @param string $caption The video caption.
  105. * @param string $post_excerpt The except.
  106. * @param string $rating The video rating.
  107. * @param string $display_embed The video display_embed.
  108. *
  109. * @return array
  110. */
  111. private static function build_wpcom_api_request_values( $post_title, $caption, $post_excerpt, $rating, $display_embed ) {
  112. $values = array();
  113. // Add the video title & description in, so that we save it properly.
  114. if ( isset( $post_title ) ) {
  115. $values['title'] = trim( wp_strip_all_tags( $post_title ) );
  116. }
  117. if ( isset( $caption ) ) {
  118. $values['caption'] = trim( wp_strip_all_tags( $caption ) );
  119. }
  120. if ( isset( $post_excerpt ) ) {
  121. $values['description'] = trim( wp_strip_all_tags( $post_excerpt ) );
  122. }
  123. if ( isset( $rating ) ) {
  124. $values['rating'] = $rating;
  125. }
  126. if ( self::is_display_embed_valid( $display_embed ) ) {
  127. $values['display_embed'] = $display_embed;
  128. }
  129. return $values;
  130. }
  131. }