* @subpackage Media
*/
+/**
+ * Retrieve additional image sizes.
+ *
+ * @since 4.7.0
+ *
+ * @global array $_wp_additional_image_sizes
+ *
+ * @return array Additional images size data.
+ */
+function wp_get_additional_image_sizes() {
+ global $_wp_additional_image_sizes;
+ if ( ! $_wp_additional_image_sizes ) {
+ $_wp_additional_image_sizes = array();
+ }
+ return $_wp_additional_image_sizes;
+}
+
/**
* Scale down the default size of an image.
*
* @since 2.5.0
*
* @global int $content_width
- * @global array $_wp_additional_image_sizes
*
* @param int $width Width of the image in pixels.
* @param int $height Height of the image in pixels.
* @return array Width and height of what the result image should resize to.
*/
function image_constrain_size_for_editor( $width, $height, $size = 'medium', $context = null ) {
- global $content_width, $_wp_additional_image_sizes;
+ global $content_width;
+
+ $_wp_additional_image_sizes = wp_get_additional_image_sizes();
if ( ! $context )
$context = is_admin() ? 'edit' : 'display';
if ( intval($content_width) > 0 ) {
$max_width = min( intval($content_width), $max_width );
}
- } elseif ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) && in_array( $size, array_keys( $_wp_additional_image_sizes ) ) ) {
+ } elseif ( ! empty( $_wp_additional_image_sizes ) && in_array( $size, array_keys( $_wp_additional_image_sizes ) ) ) {
$max_width = intval( $_wp_additional_image_sizes[$size]['width'] );
$max_height = intval( $_wp_additional_image_sizes[$size]['height'] );
- if ( intval($content_width) > 0 && 'edit' == $context ) // Only in admin. Assume that theme authors know what they're doing.
- $max_width = min( intval($content_width), $max_width );
+ // Only in admin. Assume that theme authors know what they're doing.
+ if ( intval( $content_width ) > 0 && 'edit' === $context ) {
+ $max_width = min( intval( $content_width ), $max_width );
+ }
}
// $size == 'full' has no constraint
else {
* the image is an intermediate size. False on failure.
*/
function image_downsize( $id, $size = 'medium' ) {
-
- if ( !wp_attachment_is_image($id) )
- return false;
+ $is_image = wp_attachment_is_image( $id );
/**
* Filters whether to preempt the output of image_downsize().
$is_intermediate = false;
$img_url_basename = wp_basename($img_url);
+ // If the file isn't an image, attempt to replace its URL with a rendered image from its meta.
+ // Otherwise, a non-image type could be returned.
+ if ( ! $is_image ) {
+ if ( ! empty( $meta['sizes'] ) ) {
+ $img_url = str_replace( $img_url_basename, $meta['sizes']['full']['file'], $img_url );
+ $img_url_basename = $meta['sizes']['full']['file'];
+ $width = $meta['sizes']['full']['width'];
+ $height = $meta['sizes']['full']['height'];
+ } else {
+ return false;
+ }
+ }
+
// try for a new style intermediate size
if ( $intermediate = image_get_intermediate_size($id, $size) ) {
$img_url = str_replace($img_url_basename, $intermediate['file'], $img_url);
*
* @since 3.9.0
*
- * @global array $_wp_additional_image_sizes
- *
* @param string $name The image size to check.
* @return bool True if the image size exists, false if not.
*/
function has_image_size( $name ) {
- global $_wp_additional_image_sizes;
-
- return isset( $_wp_additional_image_sizes[ $name ] );
+ $sizes = wp_get_additional_image_sizes();
+ return isset( $sizes[ $name ] );
}
/**
if ( is_array( $size ) ) {
$candidates = array();
+ if ( ! isset( $imagedata['file'] ) && isset( $imagedata['sizes']['full'] ) ) {
+ $imagedata['height'] = $imagedata['sizes']['full']['height'];
+ $imagedata['width'] = $imagedata['sizes']['full']['width'];
+ }
+
foreach ( $imagedata['sizes'] as $_size => $data ) {
// If there's an exact match to an existing image size, short circuit.
if ( $data['width'] == $size[0] && $data['height'] == $size[1] ) {
}
// include the full filesystem path of the intermediate file
- if ( empty($data['path']) && !empty($data['file']) ) {
+ if ( empty( $data['path'] ) && ! empty( $data['file'] ) && ! empty( $imagedata['file'] ) ) {
$file_url = wp_get_attachment_url($post_id);
$data['path'] = path_join( dirname($imagedata['file']), $data['file'] );
$data['url'] = path_join( dirname($file_url), $data['file'] );
*
* @since 3.0.0
*
- * @global array $_wp_additional_image_sizes
- *
* @return array Returns a filtered array of image size strings.
*/
function get_intermediate_image_sizes() {
- global $_wp_additional_image_sizes;
+ $_wp_additional_image_sizes = wp_get_additional_image_sizes();
$image_sizes = array('thumbnail', 'medium', 'medium_large', 'large'); // Standard sizes
- if ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) )
+ if ( ! empty( $_wp_additional_image_sizes ) ) {
$image_sizes = array_merge( $image_sizes, array_keys( $_wp_additional_image_sizes ) );
+ }
/**
* Filters the list of intermediate image sizes.
$default_attr = array(
'src' => $src,
'class' => "attachment-$size_class size-$size_class",
- 'alt' => trim(strip_tags( get_post_meta($attachment_id, '_wp_attachment_image_alt', true) )), // Use Alt field first
+ 'alt' => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ),
);
- if ( empty($default_attr['alt']) )
- $default_attr['alt'] = trim(strip_tags( $attachment->post_excerpt )); // If not, Use the Caption
- if ( empty($default_attr['alt']) )
- $default_attr['alt'] = trim(strip_tags( $attachment->post_title )); // Finally, use the title
$attr = wp_parse_args( $attr, $default_attr );
// Generate 'srcset' and 'sizes' if not already present.
if ( empty( $attr['srcset'] ) ) {
- $image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true );
+ $image_meta = wp_get_attachment_metadata( $attachment_id );
if ( is_array( $image_meta ) ) {
$size_array = array( absint( $width ), absint( $height ) );
}
if ( ! is_array( $image_meta ) ) {
- $image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true );
+ $image_meta = wp_get_attachment_metadata( $attachment_id );
}
$image_src = $image[0];
}
if ( ! is_array( $image_meta ) ) {
- $image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true );
+ $image_meta = wp_get_attachment_metadata( $attachment_id );
}
$image_src = $image[0];
$width = absint( $size[0] );
} elseif ( is_string( $size ) ) {
if ( ! $image_meta && $attachment_id ) {
- $image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true );
+ $image_meta = wp_get_attachment_metadata( $attachment_id );
}
if ( is_array( $image_meta ) ) {
}
foreach ( $selected_images as $image => $attachment_id ) {
- $image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true );
+ $image_meta = wp_get_attachment_metadata( $attachment_id );
$content = str_replace( $image, wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ), $content );
}
$caption_width = apply_filters( 'img_caption_shortcode_width', $width, $atts, $content );
$style = '';
- if ( $caption_width )
+ if ( $caption_width ) {
$style = 'style="width: ' . (int) $caption_width . 'px" ';
+ }
- $html = '';
if ( $html5 ) {
$html = '<figure ' . $atts['id'] . $style . 'class="' . esc_attr( $class ) . '">'
. do_shortcode( $content ) . '<figcaption class="wp-caption-text">' . $atts['caption'] . '</figcaption></figure>';
$width_rule = '';
if ( ! empty( $atts['width'] ) ) {
- $width_rule = sprintf( 'width: %dpx; ', $atts['width'] );
+ $width_rule = sprintf( 'width: %dpx;', $atts['width'] );
}
$output = sprintf( '<div style="%s" class="wp-video">%s</div>', $width_rule, $html );
* Retrieves taxonomies attached to given the attachment.
*
* @since 2.5.0
+ * @since 4.7.0 Introduced the `$output` parameter.
*
* @param int|array|object $attachment Attachment ID, data array, or data object.
+ * @param string $output Output type. 'names' to return an array of taxonomy names,
+ * or 'objects' to return an array of taxonomy objects.
+ * Default is 'names'.
* @return array Empty array on failure. List of taxonomies on success.
*/
-function get_attachment_taxonomies( $attachment ) {
+function get_attachment_taxonomies( $attachment, $output = 'names' ) {
if ( is_int( $attachment ) ) {
$attachment = get_post( $attachment );
} elseif ( is_array( $attachment ) ) {
}
$taxonomies = array();
- foreach ( $objects as $object )
- if ( $taxes = get_object_taxonomies($object) )
- $taxonomies = array_merge($taxonomies, $taxes);
+ foreach ( $objects as $object ) {
+ if ( $taxes = get_object_taxonomies( $object, $output ) ) {
+ $taxonomies = array_merge( $taxonomies, $taxes );
+ }
+ }
- return array_unique($taxonomies);
+ if ( 'names' === $output ) {
+ $taxonomies = array_unique( $taxonomies );
+ }
+
+ return $taxonomies;
}
/**
require_once ABSPATH . WPINC . '/class-wp-image-editor.php';
require_once ABSPATH . WPINC . '/class-wp-image-editor-gd.php';
require_once ABSPATH . WPINC . '/class-wp-image-editor-imagick.php';
-
/**
* Filters the list of image editing library classes.
*
list( $type, $subtype ) = array( $attachment->post_mime_type, '' );
$attachment_url = wp_get_attachment_url( $attachment->ID );
+ $base_url = str_replace( wp_basename( $attachment_url ), '', $attachment_url );
$response = array(
'id' => $attachment->ID,
if ( current_user_can( 'delete_post', $attachment->ID ) )
$response['nonces']['delete'] = wp_create_nonce( 'delete-post_' . $attachment->ID );
- if ( $meta && 'image' === $type ) {
+ if ( $meta && ( 'image' === $type || ! empty( $meta['sizes'] ) ) ) {
$sizes = array();
/** This filter is documented in wp-admin/includes/media.php */
/** This filter is documented in wp-includes/media.php */
if ( $downsize = apply_filters( 'image_downsize', false, $attachment->ID, $size ) ) {
- if ( ! $downsize[3] )
+ if ( empty( $downsize[3] ) ) {
continue;
+ }
+
$sizes[ $size ] = array(
'height' => $downsize[2],
'width' => $downsize[1],
'orientation' => $downsize[2] > $downsize[1] ? 'portrait' : 'landscape',
);
} elseif ( isset( $meta['sizes'][ $size ] ) ) {
- if ( ! isset( $base_url ) )
- $base_url = str_replace( wp_basename( $attachment_url ), '', $attachment_url );
-
// Nothing from the filter, so consult image metadata if we have it.
$size_meta = $meta['sizes'][ $size ];
}
}
- $sizes['full'] = array( 'url' => $attachment_url );
+ if ( 'image' === $type ) {
+ $sizes['full'] = array( 'url' => $attachment_url );
- if ( isset( $meta['height'], $meta['width'] ) ) {
- $sizes['full']['height'] = $meta['height'];
- $sizes['full']['width'] = $meta['width'];
- $sizes['full']['orientation'] = $meta['height'] > $meta['width'] ? 'portrait' : 'landscape';
+ if ( isset( $meta['height'], $meta['width'] ) ) {
+ $sizes['full']['height'] = $meta['height'];
+ $sizes['full']['width'] = $meta['width'];
+ $sizes['full']['orientation'] = $meta['height'] > $meta['width'] ? 'portrait' : 'landscape';
+ }
+
+ $response = array_merge( $response, $sizes['full'] );
+ } elseif ( $meta['sizes']['full']['file'] ) {
+ $sizes['full'] = array(
+ 'url' => $base_url . $meta['sizes']['full']['file'],
+ 'height' => $meta['sizes']['full']['height'],
+ 'width' => $meta['sizes']['full']['width'],
+ 'orientation' => $meta['sizes']['full']['height'] > $meta['sizes']['full']['width'] ? 'portrait' : 'landscape'
+ );
}
- $response = array_merge( $response, array( 'sizes' => $sizes ), $sizes['full'] );
- } elseif ( $meta && 'video' === $type ) {
+ $response = array_merge( $response, array( 'sizes' => $sizes ) );
+ }
+
+ if ( $meta && 'video' === $type ) {
if ( isset( $meta['width'] ) )
$response['width'] = (int) $meta['width'];
if ( isset( $meta['height'] ) )
'filterByDate' => __( 'Filter by date' ),
'filterByType' => __( 'Filter by type' ),
'searchMediaLabel' => __( 'Search Media' ),
+ 'searchMediaPlaceholder' => __( 'Search media items...' ), // placeholder (no ellipsis)
'noMedia' => __( 'No media files found.' ),
// Library Details
*
* @since 4.0.0
*
- * @global string $wp_version
- *
* @return array The relevant CSS file URLs.
*/
function wpview_media_sandbox_styles() {
- $version = 'ver=' . $GLOBALS['wp_version'];
+ $version = 'ver=' . get_bloginfo( 'version' );
$mediaelement = includes_url( "js/mediaelement/mediaelementplayer.min.css?$version" );
$wpmediaelement = includes_url( "js/mediaelement/wp-mediaelement.css?$version" );