Changeset 27486
- Timestamp:
- 03/09/2014 10:44:22 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/media.php
r27481 r27486 1000 1000 1001 1001 /** 1002 * The Playlist shortcode. 1003 * 1004 * This implements the functionality of the Playlist Shortcode for displaying 1005 * a collection of WordPress audio or video files in a post. 1002 * Output and enqueue default scripts and styles for playlists. 1006 1003 * 1007 1004 * @since 3.9.0 1008 1005 * 1009 * @param array $attr Attributes of the shortcode. 1010 * @param string $type Type of playlist. Accepts 'audio' and 'video'. 1011 * @return string Playlist output. Empty string if the passed type is unsupported. 1012 */ 1013 function wp_get_playlist( $attr, $type ) { 1014 global $content_width; 1015 $post = get_post(); 1016 1017 if ( ! in_array( $type, array( 'audio', 'video' ) ) ) { 1018 return ''; 1019 } 1020 1021 static $instance = 0; 1022 $instance++; 1023 1024 if ( ! empty( $attr['ids'] ) ) { 1025 // 'ids' is explicitly ordered, unless you specify otherwise. 1026 if ( empty( $attr['orderby'] ) ) { 1027 $attr['orderby'] = 'post__in'; 1028 } 1029 $attr['include'] = $attr['ids']; 1030 } 1031 1032 /** 1033 * Filter the playlist output. 1034 * 1035 * Passing a non-empty value to the filter will short-circuit generation 1036 * of the default playlist output, returning the passed value instead. 1037 * 1038 * @since 3.9.0 1039 * 1040 * @param string $output Playlist output. Default empty. 1041 * @param array $attr Array of shortcode attributes. 1042 * @param string $type Type of playlist to generate output for. 1043 */ 1044 $output = apply_filters( 'post_playlist', '', $attr, $type ); 1045 if ( $output != '' ) { 1046 return $output; 1047 } 1048 1049 /* 1050 * We're trusting author input, so let's at least make sure it looks 1051 * like a valid orderby statement. 1052 */ 1053 if ( isset( $attr['orderby'] ) ) { 1054 $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] ); 1055 if ( ! $attr['orderby'] ) 1056 unset( $attr['orderby'] ); 1057 } 1058 1059 extract( shortcode_atts( array( 1060 'order' => 'ASC', 1061 'orderby' => 'menu_order ID', 1062 'id' => $post ? $post->ID : 0, 1063 'include' => '', 1064 'exclude' => '', 1065 'style' => 'light', 1066 'tracklist' => 'audio' === $type, 1067 'tracknumbers' => 'audio' === $type, 1068 'images' => true, 1069 'artists' => true 1070 ), $attr, 'playlist' ) ); 1071 1072 $id = intval( $id ); 1073 if ( 'RAND' == $order ) { 1074 $orderby = 'none'; 1075 } 1076 1077 if ( ! in_array( $style, array( 'light', 'dark' ), true ) ) { 1078 $style = 'light'; 1079 } 1080 1081 $args = array( 1082 'post_status' => 'inherit', 1083 'post_type' => 'attachment', 1084 'post_mime_type' => $type, 1085 'order' => $order, 1086 'orderby' => $orderby 1087 ); 1088 1089 if ( ! empty( $include ) ) { 1090 $args['include'] = $include; 1091 $_attachments = get_posts( $args ); 1092 1093 $attachments = array(); 1094 foreach ( $_attachments as $key => $val ) { 1095 $attachments[$val->ID] = $_attachments[$key]; 1096 } 1097 } elseif ( ! empty( $exclude ) ) { 1098 $args['post_parent'] = $id; 1099 $args['exclude'] = $exclude; 1100 $attachments = get_children( $args ); 1101 } else { 1102 $args['post_parent'] = $id; 1103 $attachments = get_children( $args ); 1104 } 1105 1106 if ( empty( $attachments ) ) { 1107 return ''; 1108 } 1109 1110 if ( is_feed() ) { 1111 $output = "\n"; 1112 foreach ( $attachments as $att_id => $attachment ) { 1113 $output .= wp_get_attachment_link( $att_id ) . "\n"; 1114 } 1115 return $output; 1116 } 1117 1118 $supports_thumbs = ( current_theme_supports( 'post-thumbnails', "attachment:$type" ) && post_type_supports( "attachment:$type", 'thumbnail' ) ) 1119 || $images; 1120 1121 $outer = 22; // default padding and border of wrapper 1122 1123 $default_width = 640; 1124 $default_height = 360; 1125 1126 $theme_width = $content_width - $outer; 1127 $theme_height = round( ( $default_height * $theme_width ) / $default_width ); 1128 1129 $data = compact( 'type', 'style' ); 1130 1131 // don't pass strings to JSON, will be truthy in JS 1132 foreach ( array( 'tracklist', 'tracknumbers', 'images', 'artists' ) as $key ) { 1133 $data[$key] = filter_var( $$key, FILTER_VALIDATE_BOOLEAN ); 1134 } 1135 1136 $tracks = array(); 1137 foreach ( $attachments as $attachment ) { 1138 $url = wp_get_attachment_url( $attachment->ID ); 1139 $ftype = wp_check_filetype( $url, wp_get_mime_types() ); 1140 $track = array( 1141 'src' => $url, 1142 'type' => $ftype['type'], 1143 'title' => get_the_title( $attachment->ID ), 1144 'caption' => wptexturize( $attachment->post_excerpt ), 1145 'description' => wptexturize( $attachment->post_content ) 1146 ); 1147 1148 $track['meta'] = array(); 1149 $meta = wp_get_attachment_metadata( $attachment->ID ); 1150 if ( ! empty( $meta ) ) { 1151 1152 $keys = array( 'title', 'artist', 'band', 'album', 'genre', 'year', 'length', 'length_formatted' ); 1153 foreach ( $keys as $key ) { 1154 if ( ! empty( $meta[ $key ] ) ) { 1155 $track['meta'][ $key ] = $meta[ $key ]; 1156 } 1157 } 1158 1159 if ( 'video' === $type ) { 1160 if ( ! empty( $meta['width'] ) && ! empty( $meta['height'] ) ) { 1161 $width = $meta['width']; 1162 $height = $meta['height']; 1163 $theme_height = round( ( $height * $theme_width ) / $width ); 1164 } else { 1165 $width = $default_width; 1166 $height = $default_height; 1167 } 1168 1169 $track['dimensions'] = array( 1170 'original' => compact( 'width', 'height' ), 1171 'resized' => array( 1172 'width' => $theme_width, 1173 'height' => $theme_height 1174 ) 1175 ); 1176 } 1177 } 1178 1179 if ( $supports_thumbs ) { 1180 $id = get_post_thumbnail_id( $attachment->ID ); 1181 if ( ! empty( $id ) ) { 1182 list( $src, $width, $height ) = wp_get_attachment_image_src( $id, 'full' ); 1183 $track['image'] = compact( 'src', 'width', 'height' ); 1184 list( $src, $width, $height ) = wp_get_attachment_image_src( $id, 'thumbnail' ); 1185 $track['thumb'] = compact( 'src', 'width', 'height' ); 1186 } 1187 } 1188 1189 $tracks[] = $track; 1190 } 1191 $data['tracks'] = $tracks; 1192 1193 $safe_type = esc_attr( $type ); 1194 $safe_style = esc_attr( $style ); 1195 1196 ob_start(); 1197 1198 if ( 1 === $instance ): 1199 wp_enqueue_style( 'wp-mediaelement' ); 1200 wp_enqueue_script( 'wp-playlist' ); 1006 * @param string $type Type of playlist: "audio" or "video." 1007 */ 1008 function wp_playlist_scripts( $type ) { 1009 wp_enqueue_style( 'wp-mediaelement' ); 1010 wp_enqueue_script( 'wp-playlist' ); 1201 1011 ?> 1202 1012 <!--[if lt IE 9]><script>document.createElement('<?php echo esc_js( $type ) ?>');</script><![endif]--> … … 1234 1044 </div> 1235 1045 </script> 1236 <?php endif ?> 1046 <?php 1047 } 1048 add_action( 'wp_playlist_scripts', 'wp_playlist_scripts' ); 1049 1050 /** 1051 * The Playlist shortcode. 1052 * 1053 * This implements the functionality of the Playlist Shortcode for displaying 1054 * a collection of WordPress audio or video files in a post. 1055 * 1056 * @since 3.9.0 1057 * 1058 * @param array $attr Attributes of the shortcode. 1059 * @param string $type Type of playlist. Accepts 'audio' and 'video'. 1060 * @return string Playlist output. Empty string if the passed type is unsupported. 1061 */ 1062 function wp_get_playlist( $attr, $type ) { 1063 global $content_width; 1064 $post = get_post(); 1065 1066 if ( ! in_array( $type, array( 'audio', 'video' ) ) ) { 1067 return ''; 1068 } 1069 1070 static $instance = 0; 1071 $instance++; 1072 1073 if ( ! empty( $attr['ids'] ) ) { 1074 // 'ids' is explicitly ordered, unless you specify otherwise. 1075 if ( empty( $attr['orderby'] ) ) { 1076 $attr['orderby'] = 'post__in'; 1077 } 1078 $attr['include'] = $attr['ids']; 1079 } 1080 1081 /** 1082 * Filter the playlist output. 1083 * 1084 * Passing a non-empty value to the filter will short-circuit generation 1085 * of the default playlist output, returning the passed value instead. 1086 * 1087 * @since 3.9.0 1088 * 1089 * @param string $output Playlist output. Default empty. 1090 * @param array $attr Array of shortcode attributes. 1091 * @param string $type Type of playlist to generate output for. 1092 */ 1093 $output = apply_filters( 'post_playlist', '', $attr, $type ); 1094 if ( $output != '' ) { 1095 return $output; 1096 } 1097 1098 /* 1099 * We're trusting author input, so let's at least make sure it looks 1100 * like a valid orderby statement. 1101 */ 1102 if ( isset( $attr['orderby'] ) ) { 1103 $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] ); 1104 if ( ! $attr['orderby'] ) 1105 unset( $attr['orderby'] ); 1106 } 1107 1108 extract( shortcode_atts( array( 1109 'order' => 'ASC', 1110 'orderby' => 'menu_order ID', 1111 'id' => $post ? $post->ID : 0, 1112 'include' => '', 1113 'exclude' => '', 1114 'style' => 'light', 1115 'tracklist' => 'audio' === $type, 1116 'tracknumbers' => 'audio' === $type, 1117 'images' => true, 1118 'artists' => true 1119 ), $attr, 'playlist' ) ); 1120 1121 $id = intval( $id ); 1122 if ( 'RAND' == $order ) { 1123 $orderby = 'none'; 1124 } 1125 1126 if ( ! in_array( $style, array( 'light', 'dark' ), true ) ) { 1127 $style = 'light'; 1128 } 1129 1130 $args = array( 1131 'post_status' => 'inherit', 1132 'post_type' => 'attachment', 1133 'post_mime_type' => $type, 1134 'order' => $order, 1135 'orderby' => $orderby 1136 ); 1137 1138 if ( ! empty( $include ) ) { 1139 $args['include'] = $include; 1140 $_attachments = get_posts( $args ); 1141 1142 $attachments = array(); 1143 foreach ( $_attachments as $key => $val ) { 1144 $attachments[$val->ID] = $_attachments[$key]; 1145 } 1146 } elseif ( ! empty( $exclude ) ) { 1147 $args['post_parent'] = $id; 1148 $args['exclude'] = $exclude; 1149 $attachments = get_children( $args ); 1150 } else { 1151 $args['post_parent'] = $id; 1152 $attachments = get_children( $args ); 1153 } 1154 1155 if ( empty( $attachments ) ) { 1156 return ''; 1157 } 1158 1159 if ( is_feed() ) { 1160 $output = "\n"; 1161 foreach ( $attachments as $att_id => $attachment ) { 1162 $output .= wp_get_attachment_link( $att_id ) . "\n"; 1163 } 1164 return $output; 1165 } 1166 1167 $supports_thumbs = ( current_theme_supports( 'post-thumbnails', "attachment:$type" ) && post_type_supports( "attachment:$type", 'thumbnail' ) ) 1168 || $images; 1169 1170 $outer = 22; // default padding and border of wrapper 1171 1172 $default_width = 640; 1173 $default_height = 360; 1174 1175 $theme_width = $content_width - $outer; 1176 $theme_height = round( ( $default_height * $theme_width ) / $default_width ); 1177 1178 $data = compact( 'type', 'style' ); 1179 1180 // don't pass strings to JSON, will be truthy in JS 1181 foreach ( array( 'tracklist', 'tracknumbers', 'images', 'artists' ) as $key ) { 1182 $data[$key] = filter_var( $$key, FILTER_VALIDATE_BOOLEAN ); 1183 } 1184 1185 $tracks = array(); 1186 foreach ( $attachments as $attachment ) { 1187 $url = wp_get_attachment_url( $attachment->ID ); 1188 $ftype = wp_check_filetype( $url, wp_get_mime_types() ); 1189 $track = array( 1190 'src' => $url, 1191 'type' => $ftype['type'], 1192 'title' => get_the_title( $attachment->ID ), 1193 'caption' => wptexturize( $attachment->post_excerpt ), 1194 'description' => wptexturize( $attachment->post_content ) 1195 ); 1196 1197 $track['meta'] = array(); 1198 $meta = wp_get_attachment_metadata( $attachment->ID ); 1199 if ( ! empty( $meta ) ) { 1200 1201 $keys = array( 'title', 'artist', 'band', 'album', 'genre', 'year', 'length', 'length_formatted' ); 1202 foreach ( $keys as $key ) { 1203 if ( ! empty( $meta[ $key ] ) ) { 1204 $track['meta'][ $key ] = $meta[ $key ]; 1205 } 1206 } 1207 1208 if ( 'video' === $type ) { 1209 if ( ! empty( $meta['width'] ) && ! empty( $meta['height'] ) ) { 1210 $width = $meta['width']; 1211 $height = $meta['height']; 1212 $theme_height = round( ( $height * $theme_width ) / $width ); 1213 } else { 1214 $width = $default_width; 1215 $height = $default_height; 1216 } 1217 1218 $track['dimensions'] = array( 1219 'original' => compact( 'width', 'height' ), 1220 'resized' => array( 1221 'width' => $theme_width, 1222 'height' => $theme_height 1223 ) 1224 ); 1225 } 1226 } 1227 1228 if ( $supports_thumbs ) { 1229 $id = get_post_thumbnail_id( $attachment->ID ); 1230 if ( ! empty( $id ) ) { 1231 list( $src, $width, $height ) = wp_get_attachment_image_src( $id, 'full' ); 1232 $track['image'] = compact( 'src', 'width', 'height' ); 1233 list( $src, $width, $height ) = wp_get_attachment_image_src( $id, 'thumbnail' ); 1234 $track['thumb'] = compact( 'src', 'width', 'height' ); 1235 } 1236 } 1237 1238 $tracks[] = $track; 1239 } 1240 $data['tracks'] = $tracks; 1241 1242 $safe_type = esc_attr( $type ); 1243 $safe_style = esc_attr( $style ); 1244 1245 ob_start(); 1246 1247 if ( 1 === $instance ) { 1248 /** 1249 * Hook to print and enqueue playlist scripts, styles, and JavaScript templates. 1250 * 1251 * @since 3.9.0 1252 * 1253 * @param string $type Type of playlist: "audio" or "video." 1254 * @param string $style The "theme" for the playlist. Core provides "light" and "dark." 1255 */ 1256 do_action( 'wp_playlist_scripts', $type, $style ); 1257 } ?> 1237 1258 <div class="wp-playlist wp-<?php echo $safe_type ?>-playlist wp-playlist-<?php echo $safe_style ?>"> 1238 1259 <?php if ( 'audio' === $type ): ?> … … 1247 1268 <div class="wp-playlist-prev"></div> 1248 1269 <noscript> 1249 <?php 1250 $output = "\n"; 1270 <ol><?php 1251 1271 foreach ( $attachments as $att_id => $attachment ) { 1252 $output .= wp_get_attachment_link( $att_id ) . "\n"; 1253 } 1254 1255 echo $output; 1256 ?> 1272 printf( '<li>%s</li>', wp_get_attachment_link( $att_id ) ); 1273 } 1274 ?></ol> 1257 1275 </noscript> 1258 1276 <script type="application/json"><?php echo json_encode( $data ) ?></script>
Note: See TracChangeset
for help on using the changeset viewer.