Media: add a function, wp_get_additional_image_sizes(), that wraps the retrieval of the global $_wp_additional_image_sizes. Removes 6 global imports.

See #37699.

Built from https://develop.svn.wordpress.org/trunk@38303


git-svn-id: http://core.svn.wordpress.org/trunk@38244 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Scott Taylor 2016-08-22 04:37:28 +00:00
parent b34dea908f
commit d31456b615
5 changed files with 57 additions and 36 deletions

View File

@ -700,13 +700,11 @@ function wp_restore_image($post_id) {
* Saves image to post along with enqueued changes
* in $_REQUEST['history']
*
* @global array $_wp_additional_image_sizes
*
* @param int $post_id
* @return \stdClass
*/
function wp_save_image( $post_id ) {
global $_wp_additional_image_sizes;
$_wp_additional_image_sizes = wp_get_additional_image_sizes();
$return = new stdClass;
$success = $delete = $scaled = $nocrop = false;

View File

@ -67,8 +67,6 @@ function wp_crop_image( $src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $s
*
* @since 2.1.0
*
* @global array $_wp_additional_image_sizes
*
* @param int $attachment_id Attachment Id to process.
* @param string $file Filepath of the Attached image.
* @return mixed Metadata for attachment.
@ -87,23 +85,34 @@ function wp_generate_attachment_metadata( $attachment_id, $file ) {
$metadata['file'] = _wp_relative_upload_path($file);
// Make thumbnails and other intermediate sizes.
global $_wp_additional_image_sizes;
$_wp_additional_image_sizes = wp_get_additional_image_sizes();
$sizes = array();
foreach ( get_intermediate_image_sizes() as $s ) {
$sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => false );
if ( isset( $_wp_additional_image_sizes[$s]['width'] ) )
$sizes[$s]['width'] = intval( $_wp_additional_image_sizes[$s]['width'] ); // For theme-added sizes
else
$sizes[$s]['width'] = get_option( "{$s}_size_w" ); // For default sizes set in options
if ( isset( $_wp_additional_image_sizes[$s]['height'] ) )
$sizes[$s]['height'] = intval( $_wp_additional_image_sizes[$s]['height'] ); // For theme-added sizes
else
$sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options
if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) )
$sizes[$s]['crop'] = $_wp_additional_image_sizes[$s]['crop']; // For theme-added sizes
else
$sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options
if ( isset( $_wp_additional_image_sizes[$s]['width'] ) ) {
// For theme-added sizes
$sizes[$s]['width'] = intval( $_wp_additional_image_sizes[$s]['width'] );
} else {
// For default sizes set in options
$sizes[$s]['width'] = get_option( "{$s}_size_w" );
}
if ( isset( $_wp_additional_image_sizes[$s]['height'] ) ) {
// For theme-added sizes
$sizes[$s]['height'] = intval( $_wp_additional_image_sizes[$s]['height'] );
} else {
// For default sizes set in options
$sizes[$s]['height'] = get_option( "{$s}_size_h" );
}
if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) ) {
// For theme-added sizes
$sizes[$s]['crop'] = $_wp_additional_image_sizes[$s]['crop'];
} else {
// For default sizes set in options
$sizes[$s]['crop'] = get_option( "{$s}_crop" );
}
}
/**

View File

@ -1367,14 +1367,12 @@ function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) {
*
* @since 2.9.0
*
* @global array $_wp_additional_image_sizes
*
* @param int $thumbnail_id ID of the attachment used for thumbnail
* @param mixed $post The post ID or object associated with the thumbnail, defaults to global $post.
* @return string html
*/
function _wp_post_thumbnail_html( $thumbnail_id = null, $post = null ) {
global $_wp_additional_image_sizes;
$_wp_additional_image_sizes = wp_get_additional_image_sizes();
$post = get_post( $post );
$post_type_object = get_post_type_object( $post->post_type );

View File

@ -6,6 +6,23 @@
* @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.
*
@ -27,7 +44,6 @@
* @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.
@ -39,7 +55,9 @@
* @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';
@ -82,11 +100,13 @@ function image_constrain_size_for_editor( $width, $height, $size = 'medium', $co
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 {
@ -258,15 +278,12 @@ function add_image_size( $name, $width = 0, $height = 0, $crop = false ) {
*
* @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 ] );
}
/**
@ -748,15 +765,14 @@ function image_get_intermediate_size( $post_id, $size = 'thumbnail' ) {
*
* @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.

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.7-alpha-38302';
$wp_version = '4.7-alpha-38303';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.