class.videopress-edit-attachment.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?php
  2. /**
  3. * VideoPress edit attachment screen
  4. *
  5. * @since 4.1
  6. */
  7. class VideoPress_Edit_Attachment {
  8. /**
  9. * Singleton method to initialize the object only once.
  10. *
  11. * @return VideoPress_Edit_Attachment
  12. */
  13. public static function init() {
  14. static $instance = null;
  15. if ( ! $instance ) {
  16. $instance = new VideoPress_Edit_Attachment();
  17. }
  18. return $instance;
  19. }
  20. /**
  21. * VideoPress_Edit_Attachment constructor.
  22. *
  23. * Adds in appropriate actions for attachment fields editor, meta boxes and saving.
  24. */
  25. public function __construct() {
  26. add_filter( 'attachment_fields_to_edit', array( $this, 'fields_to_edit' ), 10, 2 );
  27. add_filter( 'attachment_fields_to_save', array( $this, 'save_fields' ), 10, 2 );
  28. add_filter( 'wp_ajax_save-attachment', array( $this, 'save_fields' ), -1 );
  29. add_filter( 'wp_ajax_save-attachment-compat', array( $this, 'save_fields' ), -1 );
  30. add_action( 'add_meta_boxes', array( $this, 'configure_meta_boxes' ), 10, 2 );
  31. }
  32. /**
  33. * @param string $post_type
  34. * @param object $post
  35. */
  36. public function configure_meta_boxes( $post_type = 'unknown', $post = NULL ) {
  37. if ( NULL == $post ) {
  38. $post = (object) array ( 'ID' => 0 );
  39. }
  40. if ( 'attachment' != $post_type ) {
  41. return;
  42. }
  43. // If this has not been processed by videopress, we can skip the rest.
  44. if ( ! is_videopress_attachment( $post->ID ) ) {
  45. return;
  46. }
  47. add_meta_box( 'videopress-media-info', __( 'VideoPress Information', 'jetpack' ), array( $this, 'videopress_information_box' ), 'attachment', 'side', 'core' );
  48. }
  49. /**
  50. * @param array $post
  51. * @param array|null $attachment
  52. *
  53. * @return array
  54. */
  55. public function save_fields( $post, $attachment = null ) {
  56. if ( $attachment === null && isset( $_POST['attachment'] ) ) {
  57. $attachment = $_POST['attachment'];
  58. }
  59. if ( ! isset( $attachment['is_videopress_attachment'] ) || $attachment['is_videopress_attachment'] !== 'yes' ) {
  60. return $post;
  61. }
  62. $post_id = absint( $post['ID'] );
  63. $meta = wp_get_attachment_metadata( $post_id );
  64. // If this has not been processed by videopress, we can skip the rest.
  65. if ( ! is_videopress_attachment( $post['ID'] ) ) {
  66. return $post;
  67. }
  68. $values = array();
  69. // Add the video title & description in, so that we save it properly.
  70. if ( isset( $_POST['post_title'] ) ) {
  71. $values['title'] = trim( strip_tags( $_POST['post_title'] ) );
  72. }
  73. if ( isset( $_POST['post_excerpt'] ) ) {
  74. $values['description'] = trim( strip_tags( $_POST['post_excerpt'] ) );
  75. }
  76. if ( isset( $attachment['rating'] ) ) {
  77. $rating = $attachment['rating'];
  78. if ( ! empty( $rating ) && in_array( $rating, array( 'G', 'PG-13', 'R-17', 'X-18' ) ) ) {
  79. $values['rating'] = $rating;
  80. }
  81. }
  82. // We set a default here, as if it isn't selected, then we'll turn it off.
  83. $values['display_embed'] = 0;
  84. if ( isset( $attachment['display_embed'] ) ) {
  85. $display_embed = $attachment['display_embed'];
  86. $values['display_embed'] = 'on' === $display_embed ? 1 : 0;
  87. }
  88. $args = array(
  89. 'method' => 'POST',
  90. );
  91. $guid = get_post_meta( $post_id, 'videopress_guid', true );
  92. $endpoint = "videos/{$guid}";
  93. $result = Jetpack_Client::wpcom_json_api_request_as_blog( $endpoint, Jetpack_Client::WPCOM_JSON_API_VERSION, $args, $values );
  94. if ( is_wp_error( $result ) ) {
  95. $post['errors']['videopress']['errors'][] = __( 'There was an issue saving your updates to the VideoPress service. Please try again later.', 'jetpack' );
  96. return $post;
  97. }
  98. if ( isset( $values['display_embed'] ) ) {
  99. $meta['videopress']['display_embed'] = $values['display_embed'];
  100. }
  101. if ( isset( $values['rating'] ) ) {
  102. $meta['videopress']['rating'] = $values['rating'];
  103. }
  104. wp_update_attachment_metadata( $post_id, $meta );
  105. $response = json_decode( $result['body'], true );
  106. if ( 'true' !== $response ) {
  107. return $post;
  108. }
  109. return $post;
  110. }
  111. /**
  112. * Get the upload api path.
  113. *
  114. * @param string $guid
  115. * @return string
  116. */
  117. public function make_video_api_path( $guid ) {
  118. return sprintf(
  119. '%s://%s/rest/v%s/videos/%s',
  120. 'https',
  121. 'public-api.wordpress.com', //JETPACK__WPCOM_JSON_API_HOST,
  122. Jetpack_Client::WPCOM_JSON_API_VERSION,
  123. $guid
  124. );
  125. }
  126. /**
  127. * Creates an array of video fields to edit based on transcoded videos.
  128. *
  129. * @param array $fields video fields of interest
  130. * @param stdClass $post post object
  131. * @return array modified version of video fields for administrative interface display
  132. */
  133. public function fields_to_edit( $fields, $post ) {
  134. $post_id = absint( $post->ID );
  135. $meta = wp_get_attachment_metadata( $post_id );
  136. // If this has not been processed by videopress, we can skip the rest.
  137. if ( ! is_videopress_attachment( $post_id ) || ! isset( $meta['videopress'] ) ) {
  138. return $fields;
  139. }
  140. $info = (object) $meta['videopress'];
  141. $file_statuses = isset( $meta['file_statuses'] ) ? $meta['file_statuses'] : array();
  142. $guid = get_post_meta( $post_id, 'videopress_guid', true );
  143. unset( $fields['url'] );
  144. unset( $fields['post_content'] );
  145. if ( isset( $file_statuses['ogg'] ) && 'done' === $file_statuses['ogg'] ) {
  146. $v_name = preg_replace( '/\.\w+/', '', basename( $info->path ) );
  147. $video_name = $v_name . '_fmt1.ogv';
  148. $ogg_url = videopress_cdn_file_url( $guid, $video_name );
  149. $fields['video-ogg'] = array(
  150. 'label' => __( 'Ogg File URL', 'jetpack' ),
  151. 'input' => 'html',
  152. 'html' => "<input type='text' class='urlfield' readonly='readonly' name='attachments[$post_id][oggurl]' value='" . esc_url( $ogg_url, array( 'http', 'https' ) ) . "' />",
  153. 'helps' => __( 'Location of the Ogg video file.', 'jetpack' ),
  154. );
  155. }
  156. $fields['post_title']['helps'] = __( 'Title will appear on the first frame of your video', 'jetpack' );
  157. $fields['post_excerpt']['label'] = _x( 'Description', 'A header for the short description display', 'jetpack' );
  158. $fields['post_excerpt']['input'] = 'textarea';
  159. $fields['post_excerpt']['value'] = $info->description;
  160. $fields['is_videopress_attachment'] = array(
  161. 'input' => 'hidden',
  162. 'value' => 'yes',
  163. );
  164. $fields['videopress_shortcode'] = array(
  165. 'label' => _x( 'Shortcode', 'A header for the shortcode display', 'jetpack' ),
  166. 'input' => 'html',
  167. 'html' => "<input type=\"text\" name=\"videopress_shortcode\" value=\"[videopress {$guid}]\" readonly=\"readonly\"/>",
  168. 'show_in_modal' => true,
  169. 'show_in_edit' => false,
  170. );
  171. $fields['display_embed'] = array(
  172. 'label' => _x( 'Share', 'A header for the video sharing options area', 'jetpack' ),
  173. 'input' => 'html',
  174. 'html' => $this->display_embed_choice( $info )
  175. );
  176. $fields['video-rating'] = array(
  177. 'label' => _x( 'Rating', 'A header for the video rating area', 'jetpack' ),
  178. 'input' => 'html',
  179. 'html' => $this->display_rating( $info )
  180. );
  181. return $fields;
  182. }
  183. /**
  184. * @param stdClass $post
  185. */
  186. public function videopress_information_box( $post ) {
  187. $post_id = absint( $post->ID );
  188. $meta = wp_get_attachment_metadata( $post_id );
  189. $guid = get_post_meta( $post_id, 'videopress_guid', true );
  190. // If this has not been processed by videopress, we can skip the rest.
  191. if ( ! is_videopress_attachment( $post_id ) ) {
  192. return;
  193. }
  194. $info = (object) $meta['videopress'];
  195. $status = videopress_get_transcoding_status( $post_id );
  196. $formats = array(
  197. 'std_mp4' => 'Standard MP4',
  198. 'std_ogg' => 'OGG Vorbis',
  199. 'dvd_mp4' => 'DVD',
  200. 'hd_mp4' => 'High Definition',
  201. );
  202. $embed = "[videopress {$guid}]";
  203. $shortcode = '<input type="text" id="plugin-embed" readonly="readonly" style="width:180px;" value="' . esc_attr( $embed ) . '" onclick="this.focus();this.select();" />';
  204. $trans_status = '';
  205. $all_trans_done = true;
  206. foreach ( $formats as $status_key => $name ) {
  207. if ( 'DONE' !== $status[ $status_key ] ) {
  208. $all_trans_done = false;
  209. }
  210. $trans_status .= '- <strong>' . $name . ":</strong> <span id=\"status_$status_key\">" . ( 'DONE' === $status[ $status_key ] ? 'Done' : 'Processing' ) . '</span><br>';
  211. }
  212. $nonce = wp_create_nonce( 'videopress-update-transcoding-status' );
  213. $url = 'empty';
  214. if ( ! empty( $guid ) ) {
  215. $url = videopress_build_url( $guid );
  216. $url = "<a href=\"{$url}\">{$url}</a>";
  217. }
  218. $poster = '<em>Still Processing</em>';
  219. if ( ! empty( $info->poster ) ) {
  220. $poster = "<br><img src=\"{$info->poster}\" width=\"175px\">";
  221. }
  222. $status_update = '';
  223. if ( ! $all_trans_done ) {
  224. $status_update = ' (<a href="javascript:;" id="videopress-update-transcoding-status">update</a>)';
  225. }
  226. $html = <<< HTML
  227. <div class="misc-pub-section misc-pub-shortcode">
  228. <strong>Shortcode</strong><br>
  229. {$shortcode}
  230. </div>
  231. <div class="misc-pub-section misc-pub-url">
  232. <strong>Url</strong>
  233. {$url}
  234. </div>
  235. <div class="misc-pub-section misc-pub-poster">
  236. <strong>Poster</strong>
  237. {$poster}
  238. </div>
  239. <div class="misc-pub-section misc-pub-status">
  240. <strong>Transcoding Status$status_update:</strong>
  241. <div id="videopress-transcoding-status">{$trans_status}</div>
  242. </div>
  243. <script>
  244. jQuery( function($) {
  245. $( '#videopress-update-transcoding-status' ).on( "click", function() {
  246. jQuery.ajax( {
  247. type: 'post',
  248. url: 'admin-ajax.php',
  249. data: {
  250. action: 'videopress-update-transcoding-status',
  251. post_id: '{$post_id}',
  252. _ajax_nonce: '{$nonce}'
  253. },
  254. complete: function( response ) {
  255. if ( 200 === response.status ) {
  256. var statuses = response.responseJSON.data.status;
  257. for (var key in statuses) {
  258. $('#status_' + key).text( 'DONE' === statuses[key] ? 'Done' : 'Processing' );
  259. }
  260. }
  261. }
  262. });
  263. } );
  264. } );
  265. </script>
  266. HTML;
  267. echo $html;
  268. }
  269. /**
  270. * Build HTML to display a form checkbox for embedcode display preference
  271. *
  272. * @param object $info database row from the videos table
  273. * @return string input element of type checkbox set to checked state based on stored embed preference
  274. */
  275. protected function display_embed_choice( $info ) {
  276. $id = "attachments-{$info->post_id}-displayembed";
  277. $out = "<label for='$id'><input type='checkbox' name='attachments[{$info->post_id}][display_embed]' id='$id'";
  278. if ( $info->display_embed )
  279. $out .= ' checked="checked"';
  280. $out .= " />" . __( 'Display share menu and allow viewers to embed or download this video', 'jetpack' ) . '</label>';
  281. return $out;
  282. }
  283. /**
  284. * Build HTML to display a form input radio button for video ratings
  285. *
  286. * @param object $info database row from the videos table
  287. * @return string input elements of type radio with existing stored value selected
  288. */
  289. protected function display_rating( $info ) {
  290. $out = '';
  291. $ratings = array(
  292. 'G' => 'G',
  293. 'PG-13' => 'PG-13',
  294. 'R-17' => 'R',
  295. 'X-18' => 'X',
  296. );
  297. foreach( $ratings as $r => $label ) {
  298. $id = "attachments-{$info->post_id}-rating-$r";
  299. $out .= "<label for=\"$id\"><input type=\"radio\" name=\"attachments[{$info->post_id}][rating]\" id=\"$id\" value=\"$r\"";
  300. if ( $info->rating == $r ) {
  301. $out .= ' checked="checked"';
  302. }
  303. $out .= " />$label</label>";
  304. unset( $id );
  305. }
  306. return $out;
  307. }
  308. }
  309. // Let's start this thing up.
  310. VideoPress_Edit_Attachment::init();