184
* Registers a new image size
206
* Register a new image size.
208
* Cropping behavior for the image size is dependent on the value of $crop:
209
* 1. If false (default), images will be scaled, not cropped.
210
* 2. If an array in the form of array( x_crop_position, y_crop_position ):
211
* - x_crop_position accepts 'left' 'center', or 'right'.
212
* - y_crop_position accepts 'top', 'center', or 'bottom'.
213
* Images will be cropped to the specified dimensions within the defined crop area.
214
* 3. If true, images will be cropped to the specified dimensions using center positions.
218
* @param string $name Image size identifier.
219
* @param int $width Image width in pixels.
220
* @param int $height Image height in pixels.
221
* @param bool|array $crop Optional. Whether to crop images to specified height and width or resize.
222
* An array can specify positioning of the crop area. Default false.
223
* @return bool|array False, if no image was created. Metadata array on success.
188
225
function add_image_size( $name, $width = 0, $height = 0, $crop = false ) {
189
226
global $_wp_additional_image_sizes;
190
$_wp_additional_image_sizes[$name] = array( 'width' => absint( $width ), 'height' => absint( $height ), 'crop' => (bool) $crop );
194
* Registers an image size for the post thumbnail
228
$_wp_additional_image_sizes[ $name ] = array(
229
'width' => absint( $width ),
230
'height' => absint( $height ),
236
* Check if an image size exists.
240
* @param string $name The image size to check.
241
* @return bool True if the image size exists, false if not.
243
function has_image_size( $name ) {
244
global $_wp_additional_image_sizes;
246
return isset( $_wp_additional_image_sizes[ $name ] );
250
* Remove a new image size.
254
* @param string $name The image size to remove.
255
* @return bool True if the image size was successfully removed, false on failure.
257
function remove_image_size( $name ) {
258
global $_wp_additional_image_sizes;
260
if ( isset( $_wp_additional_image_sizes[ $name ] ) ) {
261
unset( $_wp_additional_image_sizes[ $name ] );
269
* Registers an image size for the post thumbnail.
272
* @see add_image_size() for details on cropping behavior.
274
* @param int $width Image width in pixels.
275
* @param int $height Image height in pixels.
276
* @param bool|array $crop Optional. Whether to crop images to specified height and width or resize.
277
* An array can specify positioning of the crop area. Default false.
278
* @return bool|array False, if no image was created. Metadata array on success.
198
280
function set_post_thumbnail_size( $width = 0, $height = 0, $crop = false ) {
199
281
add_image_size( 'post-thumbnail', $width, $height, $crop );
303
* Retrieve calculated resized dimensions for use in WP_Image_Editor.
305
* Calculate dimensions and coordinates for a resized image that fits within a
306
* specified width and height. If $crop is true, the largest matching central
307
* portion of the image will be cropped out and resized to the required size.
403
* Retrieve calculated resize dimensions for use in WP_Image_Editor.
405
* Calculates dimensions and coordinates for a resized image that fits
406
* within a specified width and height.
408
* Cropping behavior is dependent on the value of $crop:
409
* 1. If false (default), images will not be cropped.
410
* 2. If an array in the form of array( x_crop_position, y_crop_position ):
411
* - x_crop_position accepts 'left' 'center', or 'right'.
412
* - y_crop_position accepts 'top', 'center', or 'bottom'.
413
* Images will be cropped to the specified dimensions within the defined crop area.
414
* 3. If true, images will be cropped to the specified dimensions using center positions.
310
* @uses apply_filters() Calls 'image_resize_dimensions' on $orig_w, $orig_h, $dest_w, $dest_h and
311
* $crop to provide custom resize dimensions.
313
* @param int $orig_w Original width.
314
* @param int $orig_h Original height.
315
* @param int $dest_w New width.
316
* @param int $dest_h New height.
317
* @param bool $crop Optional, default is false. Whether to crop image or resize.
318
* @return bool|array False on failure. Returned array matches parameters for imagecopyresampled() PHP function.
418
* @param int $orig_w Original width in pixels.
419
* @param int $orig_h Original height in pixels.
420
* @param int $dest_w New width in pixels.
421
* @param int $dest_h New height in pixels.
422
* @param bool|array $crop Optional. Whether to crop image to specified height and width or resize.
423
* An array can specify positioning of the crop area. Default false.
424
* @return bool|array False on failure. Returned array matches parameters for `imagecopyresampled()`.
320
426
function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = false) {
834
1072
</{$captiontag}>";
836
1074
$output .= "</{$itemtag}>";
837
if ( $columns > 0 && ++$i % $columns == 0 )
1075
if ( ! $html5 && $columns > 0 && ++$i % $columns == 0 ) {
838
1076
$output .= '<br style="clear: both" />';
1080
if ( ! $html5 && $columns > 0 && $i % $columns !== 0 ) {
1082
<br style='clear: both' />";
842
<br style='clear: both;' />
1092
* Output the templates used by playlists.
1096
function wp_underscore_playlist_templates() {
1098
<script type="text/html" id="tmpl-wp-playlist-current-item">
1099
<# if ( data.image ) { #>
1100
<img src="{{ data.thumb.src }}"/>
1102
<div class="wp-playlist-caption">
1103
<span class="wp-playlist-item-meta wp-playlist-item-title">“{{ data.title }}”</span>
1104
<# if ( data.meta.album ) { #><span class="wp-playlist-item-meta wp-playlist-item-album">{{ data.meta.album }}</span><# } #>
1105
<# if ( data.meta.artist ) { #><span class="wp-playlist-item-meta wp-playlist-item-artist">{{ data.meta.artist }}</span><# } #>
1108
<script type="text/html" id="tmpl-wp-playlist-item">
1109
<div class="wp-playlist-item">
1110
<a class="wp-playlist-caption" href="{{ data.src }}">
1111
{{ data.index ? ( data.index + '. ' ) : '' }}
1112
<# if ( data.caption ) { #>
1115
<span class="wp-playlist-item-title">“{{{ data.title }}}”</span>
1116
<# if ( data.artists && data.meta.artist ) { #>
1117
<span class="wp-playlist-item-artist"> — {{ data.meta.artist }}</span>
1121
<# if ( data.meta.length_formatted ) { #>
1122
<div class="wp-playlist-item-length">{{ data.meta.length_formatted }}</div>
1130
* Output and enqueue default scripts and styles for playlists.
1134
* @param string $type Type of playlist. Accepts 'audio' or 'video'.
1136
function wp_playlist_scripts( $type ) {
1137
wp_enqueue_style( 'wp-mediaelement' );
1138
wp_enqueue_script( 'wp-playlist' );
1140
<!--[if lt IE 9]><script>document.createElement('<?php echo esc_js( $type ) ?>');</script><![endif]-->
1142
add_action( 'wp_footer', 'wp_underscore_playlist_templates', 0 );
1143
add_action( 'admin_footer', 'wp_underscore_playlist_templates', 0 );
1145
add_action( 'wp_playlist_scripts', 'wp_playlist_scripts' );
1148
* The playlist shortcode.
1150
* This implements the functionality of the playlist shortcode for displaying
1151
* a collection of WordPress audio or video files in a post.
1155
* @param array $attr Playlist shortcode attributes.
1156
* @return string Playlist output. Empty string if the passed type is unsupported.
1158
function wp_playlist_shortcode( $attr ) {
1159
global $content_width;
1162
static $instance = 0;
1165
if ( ! empty( $attr['ids'] ) ) {
1166
// 'ids' is explicitly ordered, unless you specify otherwise.
1167
if ( empty( $attr['orderby'] ) ) {
1168
$attr['orderby'] = 'post__in';
1170
$attr['include'] = $attr['ids'];
1174
* Filter the playlist output.
1176
* Passing a non-empty value to the filter will short-circuit generation
1177
* of the default playlist output, returning the passed value instead.
1181
* @param string $output Playlist output. Default empty.
1182
* @param array $attr An array of shortcode attributes.
1184
$output = apply_filters( 'post_playlist', '', $attr );
1185
if ( $output != '' ) {
1190
* We're trusting author input, so let's at least make sure it looks
1191
* like a valid orderby statement.
1193
if ( isset( $attr['orderby'] ) ) {
1194
$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
1195
if ( ! $attr['orderby'] )
1196
unset( $attr['orderby'] );
1199
extract( shortcode_atts( array(
1202
'orderby' => 'menu_order ID',
1203
'id' => $post ? $post->ID : 0,
1207
'tracklist' => true,
1208
'tracknumbers' => true,
1211
), $attr, 'playlist' ) );
1213
$id = intval( $id );
1214
if ( 'RAND' == $order ) {
1219
'post_status' => 'inherit',
1220
'post_type' => 'attachment',
1221
'post_mime_type' => $type,
1223
'orderby' => $orderby
1226
if ( ! empty( $include ) ) {
1227
$args['include'] = $include;
1228
$_attachments = get_posts( $args );
1230
$attachments = array();
1231
foreach ( $_attachments as $key => $val ) {
1232
$attachments[$val->ID] = $_attachments[$key];
1234
} elseif ( ! empty( $exclude ) ) {
1235
$args['post_parent'] = $id;
1236
$args['exclude'] = $exclude;
1237
$attachments = get_children( $args );
1239
$args['post_parent'] = $id;
1240
$attachments = get_children( $args );
1243
if ( empty( $attachments ) ) {
1249
foreach ( $attachments as $att_id => $attachment ) {
1250
$output .= wp_get_attachment_link( $att_id ) . "\n";
1255
$outer = 22; // default padding and border of wrapper
1257
$default_width = 640;
1258
$default_height = 360;
1260
$theme_width = empty( $content_width ) ? $default_width : ( $content_width - $outer );
1261
$theme_height = empty( $content_width ) ? $default_height : round( ( $default_height * $theme_width ) / $default_width );
1263
$data = compact( 'type' );
1265
// don't pass strings to JSON, will be truthy in JS
1266
foreach ( array( 'tracklist', 'tracknumbers', 'images', 'artists' ) as $key ) {
1267
$data[$key] = filter_var( $$key, FILTER_VALIDATE_BOOLEAN );
1271
foreach ( $attachments as $attachment ) {
1272
$url = wp_get_attachment_url( $attachment->ID );
1273
$ftype = wp_check_filetype( $url, wp_get_mime_types() );
1276
'type' => $ftype['type'],
1277
'title' => $attachment->post_title,
1278
'caption' => $attachment->post_excerpt,
1279
'description' => $attachment->post_content
1282
$track['meta'] = array();
1283
$meta = wp_get_attachment_metadata( $attachment->ID );
1284
if ( ! empty( $meta ) ) {
1286
foreach ( wp_get_attachment_id3_keys( $attachment ) as $key => $label ) {
1287
if ( ! empty( $meta[ $key ] ) ) {
1288
$track['meta'][ $key ] = $meta[ $key ];
1292
if ( 'video' === $type ) {
1293
if ( ! empty( $meta['width'] ) && ! empty( $meta['height'] ) ) {
1294
$width = $meta['width'];
1295
$height = $meta['height'];
1296
$theme_height = round( ( $height * $theme_width ) / $width );
1298
$width = $default_width;
1299
$height = $default_height;
1302
$track['dimensions'] = array(
1303
'original' => compact( 'width', 'height' ),
1305
'width' => $theme_width,
1306
'height' => $theme_height
1313
$id = get_post_thumbnail_id( $attachment->ID );
1314
if ( ! empty( $id ) ) {
1315
list( $src, $width, $height ) = wp_get_attachment_image_src( $id, 'full' );
1316
$track['image'] = compact( 'src', 'width', 'height' );
1317
list( $src, $width, $height ) = wp_get_attachment_image_src( $id, 'thumbnail' );
1318
$track['thumb'] = compact( 'src', 'width', 'height' );
1320
$src = wp_mime_type_icon( $attachment->ID );
1323
$track['image'] = compact( 'src', 'width', 'height' );
1324
$track['thumb'] = compact( 'src', 'width', 'height' );
1330
$data['tracks'] = $tracks;
1332
$safe_type = esc_attr( $type );
1333
$safe_style = esc_attr( $style );
1337
if ( 1 === $instance ) {
1339
* Print and enqueue playlist scripts, styles, and JavaScript templates.
1343
* @param string $type Type of playlist. Possible values are 'audio' or 'video'.
1344
* @param string $style The 'theme' for the playlist. Core provides 'light' and 'dark'.
1346
do_action( 'wp_playlist_scripts', $type, $style );
1348
<div class="wp-playlist wp-<?php echo $safe_type ?>-playlist wp-playlist-<?php echo $safe_style ?>">
1349
<?php if ( 'audio' === $type ): ?>
1350
<div class="wp-playlist-current-item"></div>
1352
<<?php echo $safe_type ?> controls="controls" preload="none" width="<?php
1353
echo (int) $theme_width;
1354
?>"<?php if ( 'video' === $safe_type ):
1355
echo ' height="', (int) $theme_height, '"';
1356
endif; ?>></<?php echo $safe_type ?>>
1357
<div class="wp-playlist-next"></div>
1358
<div class="wp-playlist-prev"></div>
1361
foreach ( $attachments as $att_id => $attachment ) {
1362
printf( '<li>%s</li>', wp_get_attachment_link( $att_id ) );
1366
<script type="application/json"><?php echo json_encode( $data ) ?></script>
1369
return ob_get_clean();
1371
add_shortcode( 'playlist', 'wp_playlist_shortcode' );
849
1374
* Provide a No-JS Flash fallback as a last resort for audio / video
854
1379
* @return string Fallback HTML
856
1381
function wp_mediaelement_fallback( $url ) {
1383
* Filter the Mediaelement fallback output for no-JS.
1387
* @param string $output Fallback output for no-JS.
1388
* @param string $url Media file URL.
857
1390
return apply_filters( 'wp_mediaelement_fallback', sprintf( '<a href="%1$s">%1$s</a>', esc_url( $url ) ), $url );
861
* Return a filtered list of WP-supported audio formats
1394
* Return a filtered list of WP-supported audio formats.
866
1399
function wp_get_audio_extensions() {
1401
* Filter the list of supported audio formats.
1405
* @param array $extensions An array of support audio formats. Defaults are
1406
* 'mp3', 'ogg', 'wma', 'm4a', 'wav'.
867
1408
return apply_filters( 'wp_audio_extensions', array( 'mp3', 'ogg', 'wma', 'm4a', 'wav' ) );
1412
* Return useful keys to use to lookup data from an attachment's stored metadata.
1416
* @param WP_Post $attachment The current attachment, provided for context.
1417
* @param string $context The context. Accepts 'edit', 'display'. Default 'display'.
1418
* @return array Key/value pairs of field keys to labels.
1420
function wp_get_attachment_id3_keys( $attachment, $context = 'display' ) {
1422
'artist' => __( 'Artist' ),
1423
'album' => __( 'Album' ),
1426
if ( 'display' === $context ) {
1427
$fields['genre'] = __( 'Genre' );
1428
$fields['year'] = __( 'Year' );
1429
$fields['length_formatted'] = _x( 'Length', 'video or audio' );
1433
* Filter the editable list of keys to look up data from an attachment's metadata.
1437
* @param array $fields Key/value pairs of field keys to labels.
1438
* @param WP_Post $attachment Attachment object.
1439
* @param string $context The context. Accepts 'edit', 'display'. Default 'display'.
1441
return apply_filters( 'wp_get_attachment_id3_keys', $fields, $attachment, $context );
871
1444
* The Audio shortcode.
873
1446
* This implements the functionality of the Audio Shortcode for displaying
1137
1797
if ( ! empty( $$fallback ) ) {
1138
1798
if ( empty( $fileurl ) )
1139
1799
$fileurl = $$fallback;
1140
$type = wp_check_filetype( $$fallback, wp_get_mime_types() );
1141
// m4v sometimes shows up as video/mpeg which collides with mp4
1142
if ( 'm4v' === $type['ext'] )
1143
$type['type'] = 'video/m4v';
1144
$html .= sprintf( $source, $type['type'], esc_url( $$fallback ) );
1801
if ( 'src' === $fallback && preg_match( $yt_pattern, $src ) ) {
1802
$type = array( 'type' => 'video/youtube' );
1804
$type = wp_check_filetype( $$fallback, wp_get_mime_types() );
1806
$url = add_query_arg( '_', $instances, $$fallback );
1807
$html .= sprintf( $source, $type['type'], esc_url( $url ) );
1811
if ( ! empty( $content ) ) {
1812
if ( false !== strpos( $content, "\n" ) )
1813
$content = str_replace( array( "\r\n", "\n", "\t" ), '', $content );
1815
$html .= trim( $content );
1147
1818
if ( 'mediaelement' === $library )
1148
1819
$html .= wp_mediaelement_fallback( $fileurl );
1149
1820
$html .= '</video>';
1151
1822
$html = sprintf( '<div style="width: %dpx; max-width: 100%%;" class="wp-video">%s</div>', $width, $html );
1825
* Filter the output of the video shortcode.
1829
* @param string $html Video shortcode HTML output.
1830
* @param array $atts Array of video shortcode attributes.
1831
* @param string $video Video file.
1832
* @param int $post_id Post ID.
1833
* @param string $library Media library used for the video shortcode.
1152
1835
return apply_filters( 'wp_video_shortcode', $html, $atts, $video, $post_id, $library );
1154
1837
add_shortcode( 'video', 'wp_video_shortcode' );
1806
2604
if ( $meta && ( 'audio' === $type || 'video' === $type ) ) {
1807
2605
if ( isset( $meta['length_formatted'] ) )
1808
2606
$response['fileLength'] = $meta['length_formatted'];
2608
$response['meta'] = array();
2609
foreach ( wp_get_attachment_id3_keys( $attachment ) as $key => $label ) {
2610
if ( ! empty( $meta[ $key ] ) ) {
2611
$response['meta'][ $key ] = $meta[ $key ];
2615
$id = get_post_thumbnail_id( $attachment->ID );
2616
if ( ! empty( $id ) ) {
2617
list( $src, $width, $height ) = wp_get_attachment_image_src( $id, 'full' );
2618
$response['image'] = compact( 'src', 'width', 'height' );
2619
list( $src, $width, $height ) = wp_get_attachment_image_src( $id, 'thumbnail' );
2620
$response['thumb'] = compact( 'src', 'width', 'height' );
2622
$src = wp_mime_type_icon( $attachment->ID );
2625
$response['image'] = compact( 'src', 'width', 'height' );
2626
$response['thumb'] = compact( 'src', 'width', 'height' );
1811
2630
if ( function_exists('get_compat_media_markup') )
1812
2631
$response['compat'] = get_compat_media_markup( $attachment->ID, array( 'in_modal' => true ) );
2634
* Filter the attachment data prepared for JavaScript.
2638
* @param array $response Array of prepared attachment data.
2639
* @param int|object $attachment Attachment ID or object.
2640
* @param array $meta Array of attachment meta data.
1814
2642
return apply_filters( 'wp_prepare_attachment_for_js', $response, $attachment, $meta );
1926
2801
'addToGallery' => __( 'Add to gallery' ),
1927
2802
'addToGalleryTitle' => __( 'Add to Gallery' ),
1928
2803
'reverseOrder' => __( 'Reverse order' ),
2806
'imageDetailsTitle' => __( 'Image Details' ),
2807
'imageReplaceTitle' => __( 'Replace Image' ),
2808
'imageDetailsCancel' => __( 'Cancel Edit' ),
2809
'editImage' => __( 'Edit Image' ),
2812
'chooseImage' => __( 'Choose Image' ),
2813
'selectAndCrop' => __( 'Select and Crop' ),
2814
'skipCropping' => __( 'Skip Cropping' ),
2815
'cropImage' => __( 'Crop Image' ),
2816
'cropYourImage' => __( 'Crop your image' ),
2817
'cropping' => __( 'Cropping…' ),
2818
'suggestedDimensions' => __( 'Suggested image dimensions:' ),
2819
'cropError' => __( 'There has been an error cropping your image.' ),
2822
'audioDetailsTitle' => __( 'Audio Details' ),
2823
'audioReplaceTitle' => __( 'Replace Audio' ),
2824
'audioAddSourceTitle' => __( 'Add Audio Source' ),
2825
'audioDetailsCancel' => __( 'Cancel Edit' ),
2828
'videoDetailsTitle' => __( 'Video Details' ),
2829
'videoReplaceTitle' => __( 'Replace Video' ),
2830
'videoAddSourceTitle' => __( 'Add Video Source' ),
2831
'videoDetailsCancel' => __( 'Cancel Edit' ),
2832
'videoSelectPosterImageTitle' => _( 'Select Poster Image' ),
2833
'videoAddTrackTitle' => __( 'Add Subtitles' ),
2836
'playlistDragInfo' => __( 'Drag and drop to reorder tracks.' ),
2837
'createPlaylistTitle' => __( 'Create Audio Playlist' ),
2838
'editPlaylistTitle' => __( 'Edit Audio Playlist' ),
2839
'cancelPlaylistTitle' => __( '← Cancel Audio Playlist' ),
2840
'insertPlaylist' => __( 'Insert audio playlist' ),
2841
'updatePlaylist' => __( 'Update audio playlist' ),
2842
'addToPlaylist' => __( 'Add to audio playlist' ),
2843
'addToPlaylistTitle' => __( 'Add to Audio Playlist' ),
2846
'videoPlaylistDragInfo' => __( 'Drag and drop to reorder videos.' ),
2847
'createVideoPlaylistTitle' => __( 'Create Video Playlist' ),
2848
'editVideoPlaylistTitle' => __( 'Edit Video Playlist' ),
2849
'cancelVideoPlaylistTitle' => __( '← Cancel Video Playlist' ),
2850
'insertVideoPlaylist' => __( 'Insert video playlist' ),
2851
'updateVideoPlaylist' => __( 'Update video playlist' ),
2852
'addToVideoPlaylist' => __( 'Add to video playlist' ),
2853
'addToVideoPlaylistTitle' => __( 'Add to Video Playlist' ),
2857
* Filter the media view settings.
2861
* @param array $settings List of media view settings.
2862
* @param WP_Post $post Post object.
1931
2864
$settings = apply_filters( 'media_view_settings', $settings, $post );
1932
$strings = apply_filters( 'media_view_strings', $strings, $post );
2867
* Filter the media view strings.
2871
* @param array $strings List of media view strings.
2872
* @param WP_Post $post Post object.
2874
$strings = apply_filters( 'media_view_strings', $strings, $post );
1934
2876
$strings['settings'] = $settings;
1936
2878
wp_localize_script( 'media-views', '_wpMediaViewsL10n', $strings );
1938
2880
wp_enqueue_script( 'media-editor' );
2881
wp_enqueue_script( 'media-audiovideo' );
1939
2882
wp_enqueue_style( 'media-views' );
2884
wp_enqueue_script( 'mce-view' );
2885
wp_enqueue_script( 'image-edit' );
2887
wp_enqueue_style( 'imgareaselect' );
1940
2888
wp_plupload_default_settings();
1942
2890
require_once ABSPATH . WPINC . '/media-template.php';