Sin descripción

class.videopress-edit-attachment.php 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. use Automattic\Jetpack\Connection\Client;
  3. /**
  4. * VideoPress edit attachment screen
  5. *
  6. * @since 4.1
  7. */
  8. class VideoPress_Edit_Attachment {
  9. /**
  10. * Singleton method to initialize the object only once.
  11. *
  12. * @return VideoPress_Edit_Attachment
  13. */
  14. public static function init() {
  15. static $instance = null;
  16. if ( ! $instance ) {
  17. $instance = new VideoPress_Edit_Attachment();
  18. }
  19. return $instance;
  20. }
  21. /**
  22. * VideoPress_Edit_Attachment constructor.
  23. *
  24. * Adds in appropriate actions for attachment fields editor, meta boxes and saving.
  25. */
  26. public function __construct() {
  27. add_filter( 'attachment_fields_to_edit', array( $this, 'fields_to_edit' ), 10, 2 );
  28. add_filter( 'attachment_fields_to_save', array( $this, 'save_fields' ), 10, 2 );
  29. add_filter( 'wp_ajax_save-attachment', array( $this, 'save_fields' ), -1 );
  30. add_filter( 'wp_ajax_save-attachment-compat', array( $this, 'save_fields' ), -1 );
  31. add_action( 'add_meta_boxes', array( $this, 'configure_meta_boxes' ), 10, 2 );
  32. }
  33. /**
  34. * @param string $post_type
  35. * @param object $post
  36. */
  37. public function configure_meta_boxes( $post_type = 'unknown', $post = null ) {
  38. if ( null == $post ) {
  39. $post = (object) array( 'ID' => 0 );
  40. }
  41. if ( 'attachment' != $post_type ) {
  42. return;
  43. }
  44. // If this has not been processed by videopress, we can skip the rest.
  45. if ( ! is_videopress_attachment( $post->ID ) ) {
  46. return;
  47. }
  48. add_meta_box( 'videopress-media-info', __( 'VideoPress Information', 'jetpack' ), array( $this, 'videopress_information_box' ), 'attachment', 'side', 'core' );
  49. }
  50. /**
  51. * @param array $post
  52. * @param array|null $attachment
  53. *
  54. * Disable phpcs rule for nonce verification since it's already done by Core.
  55. * @phpcs:disable WordPress.Security.NonceVerification
  56. *
  57. * @return array
  58. */
  59. public function save_fields( $post, $attachment = null ) {
  60. if ( null === $attachment && isset( $_POST['attachment'] ) ) {
  61. $attachment = $_POST['attachment'];
  62. }
  63. if ( ! isset( $attachment['is_videopress_attachment'] ) || 'yes' !== $attachment['is_videopress_attachment'] ) {
  64. return $post;
  65. }
  66. // If this has not been processed by videopress, we can skip the rest.
  67. if ( ! is_videopress_attachment( $post['ID'] ) ) {
  68. $post['errors']['videopress']['errors'][] = __( 'The media you are trying to update is not processed by VideoPress.', 'jetpack' );
  69. return $post;
  70. }
  71. $post_title = isset( $_POST['post_title'] ) ? $_POST['post_title'] : null;
  72. $post_excerpt = isset( $_POST['post_excerpt'] ) ? $_POST['post_excerpt'] : null;
  73. $rating = isset( $attachment['rating'] ) ? $attachment['rating'] : null;
  74. $display_embed = isset( $attachment['display_embed'] ) ? $attachment['display_embed'] : 0;
  75. $result = Videopress_Attachment_Metadata::persist_metadata(
  76. $post['ID'],
  77. get_post_meta( $post['ID'], 'videopress_guid', true ),
  78. $post_title,
  79. null, // @todo: Check why we haven't sent the caption in the first place.
  80. $post_excerpt,
  81. $rating,
  82. $this->normalize_display_embed_value( $display_embed )
  83. );
  84. if ( is_wp_error( $result ) ) {
  85. $post['errors']['videopress']['errors'][] = $result->get_error_message();
  86. return $post;
  87. }
  88. return $post;
  89. }
  90. /**
  91. * Convert the string values of display_embed option to the format that they will be stored in db.
  92. *
  93. * @param string $display_embed The denormalized version.
  94. *
  95. * @return int
  96. */
  97. private function normalize_display_embed_value( $display_embed ) {
  98. return 'on' === $display_embed ? 1 : 0;
  99. }
  100. /**
  101. * Get the upload api path.
  102. *
  103. * @param string $guid
  104. * @return string
  105. */
  106. public function make_video_api_path( $guid ) {
  107. return sprintf(
  108. '%s/rest/v%s/videos/%s',
  109. JETPACK__WPCOM_JSON_API_BASE,
  110. Client::WPCOM_JSON_API_VERSION,
  111. $guid
  112. );
  113. }
  114. /**
  115. * Creates an array of video fields to edit based on transcoded videos.
  116. *
  117. * @param array $fields video fields of interest
  118. * @param stdClass $post post object
  119. * @return array modified version of video fields for administrative interface display
  120. */
  121. public function fields_to_edit( $fields, $post ) {
  122. $post_id = absint( $post->ID );
  123. $meta = wp_get_attachment_metadata( $post_id );
  124. // If this has not been processed by videopress, we can skip the rest.
  125. if ( ! is_videopress_attachment( $post_id ) || ! isset( $meta['videopress'] ) ) {
  126. return $fields;
  127. }
  128. $info = (object) $meta['videopress'];
  129. $file_statuses = isset( $meta['file_statuses'] ) ? $meta['file_statuses'] : array();
  130. $guid = get_post_meta( $post_id, 'videopress_guid', true );
  131. unset( $fields['url'] );
  132. unset( $fields['post_content'] );
  133. if ( isset( $file_statuses['ogg'] ) && 'done' === $file_statuses['ogg'] ) {
  134. $v_name = preg_replace( '/\.\w+/', '', basename( $info->path ) );
  135. $video_name = $v_name . '_fmt1.ogv';
  136. $ogg_url = videopress_cdn_file_url( $guid, $video_name );
  137. $fields['video-ogg'] = array(
  138. 'label' => __( 'Ogg File URL', 'jetpack' ),
  139. 'input' => 'html',
  140. 'html' => "<input type='text' class='urlfield' readonly='readonly' name='attachments[$post_id][oggurl]' value='" . esc_url( $ogg_url, array( 'http', 'https' ) ) . "' />",
  141. 'helps' => __( 'Location of the Ogg video file.', 'jetpack' ),
  142. );
  143. }
  144. $fields['post_title']['helps'] = __( 'Title will appear on the first frame of your video', 'jetpack' );
  145. $fields['post_excerpt']['label'] = _x( 'Description', 'A header for the short description display', 'jetpack' );
  146. $fields['post_excerpt']['input'] = 'textarea';
  147. $fields['post_excerpt']['value'] = $info->description;
  148. $fields['is_videopress_attachment'] = array(
  149. 'input' => 'hidden',
  150. 'value' => 'yes',
  151. );
  152. $fields['videopress_shortcode'] = array(
  153. 'label' => _x( 'Shortcode', 'A header for the shortcode display', 'jetpack' ),
  154. 'input' => 'html',
  155. 'html' => "<input type=\"text\" name=\"videopress_shortcode\" value=\"[videopress {$guid}]\" readonly=\"readonly\"/>",
  156. 'show_in_modal' => true,
  157. 'show_in_edit' => false,
  158. );
  159. $fields['display_embed'] = array(
  160. 'label' => _x( 'Share', 'A header for the video sharing options area', 'jetpack' ),
  161. 'input' => 'html',
  162. 'html' => $this->display_embed_choice( $info ),
  163. );
  164. $fields['video-rating'] = array(
  165. 'label' => _x( 'Rating', 'A header for the video rating area', 'jetpack' ),
  166. 'input' => 'html',
  167. 'html' => $this->display_rating( $info ),
  168. );
  169. return $fields;
  170. }
  171. /**
  172. * @param stdClass $post
  173. */
  174. public function videopress_information_box( $post ) {
  175. $post_id = absint( $post->ID );
  176. $meta = wp_get_attachment_metadata( $post_id );
  177. $guid = get_post_meta( $post_id, 'videopress_guid', true );
  178. // If this has not been processed by videopress, we can skip the rest.
  179. if ( ! is_videopress_attachment( $post_id ) ) {
  180. return;
  181. }
  182. $info = (object) $meta['videopress'];
  183. $embed = "[videopress {$guid}]";
  184. $shortcode = '<input type="text" id="plugin-embed" readonly="readonly" style="width:180px;" value="' . esc_attr( $embed ) . '" onclick="this.focus();this.select();" />';
  185. $url = 'empty';
  186. if ( ! empty( $guid ) ) {
  187. $url = videopress_build_url( $guid );
  188. $url = "<a href=\"{$url}\">{$url}</a>";
  189. }
  190. $poster = '<em>Still Processing</em>';
  191. if ( ! empty( $info->poster ) ) {
  192. $poster = "<br><img src=\"{$info->poster}\" width=\"175px\">";
  193. }
  194. $html = <<< HTML
  195. <div class="misc-pub-section misc-pub-shortcode">
  196. <strong>Shortcode</strong><br>
  197. {$shortcode}
  198. </div>
  199. <div class="misc-pub-section misc-pub-url">
  200. <strong>Url</strong>
  201. {$url}
  202. </div>
  203. <div class="misc-pub-section misc-pub-poster">
  204. <strong>Poster</strong>
  205. {$poster}
  206. </div>
  207. HTML;
  208. echo $html;
  209. }
  210. /**
  211. * Build HTML to display a form checkbox for embedcode display preference
  212. *
  213. * @param object $info database row from the videos table
  214. * @return string input element of type checkbox set to checked state based on stored embed preference
  215. */
  216. protected function display_embed_choice( $info ) {
  217. $id = "attachments-{$info->post_id}-displayembed";
  218. $out = "<label for='$id'><input type='checkbox' name='attachments[{$info->post_id}][display_embed]' id='$id'";
  219. if ( $info->display_embed ) {
  220. $out .= ' checked="checked"';
  221. }
  222. $out .= ' />' . __( 'Display share menu and allow viewers to embed or download this video', 'jetpack' ) . '</label>';
  223. return $out;
  224. }
  225. /**
  226. * Build HTML to display a form input radio button for video ratings
  227. *
  228. * @param object $info database row from the videos table
  229. * @return string input elements of type radio with existing stored value selected
  230. */
  231. protected function display_rating( $info ) {
  232. $out = '';
  233. $ratings = array(
  234. 'G' => 'G',
  235. 'PG-13' => 'PG-13',
  236. 'R-17' => 'R',
  237. 'X-18' => 'X',
  238. );
  239. foreach ( $ratings as $r => $label ) {
  240. $id = "attachments-{$info->post_id}-rating-$r";
  241. $out .= "<label for=\"$id\"><input type=\"radio\" name=\"attachments[{$info->post_id}][rating]\" id=\"$id\" value=\"$r\"";
  242. if ( $info->rating == $r ) {
  243. $out .= ' checked="checked"';
  244. }
  245. $out .= " />$label</label>";
  246. unset( $id );
  247. }
  248. return $out;
  249. }
  250. }
  251. // Let's start this thing up.
  252. VideoPress_Edit_Attachment::init();