WordPress.org

Make WordPress Core


Ignore:
Timestamp:
08/22/2016 04:36:42 AM (4 years ago)
Author:
wonderboymusic
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/media.php

    r38296 r38303  
    66 * @subpackage Media
    77 */
     8
     9/**
     10 * Retrieve additional image sizes.
     11 *
     12 * @since 4.7.0
     13 *
     14 * @global array $_wp_additional_image_sizes
     15 *
     16 * @return array Additional images size data.
     17 */
     18function wp_get_additional_image_sizes() {
     19    global $_wp_additional_image_sizes;
     20    if ( ! $_wp_additional_image_sizes ) {
     21        $_wp_additional_image_sizes = array();
     22    }
     23    return $_wp_additional_image_sizes;
     24}
    825
    926/**
     
    2845 *
    2946 * @global int   $content_width
    30  * @global array $_wp_additional_image_sizes
    3147 *
    3248 * @param int          $width   Width of the image in pixels.
     
    4056 */
    4157function image_constrain_size_for_editor( $width, $height, $size = 'medium', $context = null ) {
    42     global $content_width, $_wp_additional_image_sizes;
     58    global $content_width;
     59
     60    $_wp_additional_image_sizes = wp_get_additional_image_sizes();
    4361
    4462    if ( ! $context )
     
    83101            $max_width = min( intval($content_width), $max_width );
    84102        }
    85     } elseif ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) && in_array( $size, array_keys( $_wp_additional_image_sizes ) ) ) {
     103    } elseif ( ! empty( $_wp_additional_image_sizes ) && in_array( $size, array_keys( $_wp_additional_image_sizes ) ) ) {
    86104        $max_width = intval( $_wp_additional_image_sizes[$size]['width'] );
    87105        $max_height = intval( $_wp_additional_image_sizes[$size]['height'] );
    88         if ( intval($content_width) > 0 && 'edit' == $context ) // Only in admin. Assume that theme authors know what they're doing.
    89             $max_width = min( intval($content_width), $max_width );
     106        // Only in admin. Assume that theme authors know what they're doing.
     107        if ( intval( $content_width ) > 0 && 'edit' === $context ) {
     108            $max_width = min( intval( $content_width ), $max_width );
     109        }
    90110    }
    91111    // $size == 'full' has no constraint
     
    259279 * @since 3.9.0
    260280 *
    261  * @global array $_wp_additional_image_sizes
    262  *
    263281 * @param string $name The image size to check.
    264282 * @return bool True if the image size exists, false if not.
    265283 */
    266284function has_image_size( $name ) {
    267     global $_wp_additional_image_sizes;
    268 
    269     return isset( $_wp_additional_image_sizes[ $name ] );
     285    $sizes = wp_get_additional_image_sizes();
     286    return isset( $sizes[ $name ] );
    270287}
    271288
     
    749766 * @since 3.0.0
    750767 *
    751  * @global array $_wp_additional_image_sizes
    752  *
    753768 * @return array Returns a filtered array of image size strings.
    754769 */
    755770function get_intermediate_image_sizes() {
    756     global $_wp_additional_image_sizes;
     771    $_wp_additional_image_sizes = wp_get_additional_image_sizes();
    757772    $image_sizes = array('thumbnail', 'medium', 'medium_large', 'large'); // Standard sizes
    758     if ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) )
     773    if ( ! empty( $_wp_additional_image_sizes ) ) {
    759774        $image_sizes = array_merge( $image_sizes, array_keys( $_wp_additional_image_sizes ) );
     775    }
    760776
    761777    /**
Note: See TracChangeset for help on using the changeset viewer.