VideoPress_Edit_Attachment

VideoPress edit attachment screen.

Defined (1)

The class is defined in the following location(s).

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