WordPress.org

Make WordPress Core

Ticket #18947: 18947_2.patch

File 18947_2.patch, 5.3 KB (added by F J Kaiser, 3 years ago)

Adds a boolean argument to the function that switches between a flat numerical indexed array or a an associative array containing the complete image size data. Added to provide backwards compability to not break existsing plugins using the function.

  • wp-includes/media.php

     
    264264} 
    265265 
    266266/** 
     267 * Default images for themes 
     268 *  
     269 * Builds an default <img> for use in themes or plugins before any other images are added. 
     270 * Resizes & crops the image using the built-in (retireved via `get_intermediate_image_sizes();`)  
     271 * or custom image (added via `add_image_size();`) sizes. 
     272 *  
     273 * Retrieves calculated resize dimension @uses image_resize_dimensions(); 
     274 * Builds the width and height string @uses image_hwstring(); 
     275 *  
     276 * @param $args (array) 
     277 *              string $url URl to the given default image. 
     278 *              string $size Optional. Default is 'medium'. 
     279 *              string (optional) $alt Image Description for the alt attribute. 
     280 *              string (optional) $title Image Description for the title attribute. 
     281 *              string (optional) $align Part of the class name for aligning the image. 
     282 *              string (optional) $echo Wheter to return or echo the $image 
     283 * @return string HTML IMG element for given image attachment 
     284 */ 
     285function wp_default_img( $attr ) 
     286{ 
     287        // Sizes registered via add_image_size(); 
     288        global $_wp_additional_image_sizes; 
     289 
     290        $defaults = array( 
     291                 'size'         => 'medium' 
     292                ,'classes'      => false 
     293                ,'alt'          => false 
     294                ,'title'        => false 
     295                ,'align'        => 'none' 
     296                ,'echo'         => true  
     297        ); 
     298 
     299        $attr = wp_parse_args( $attr, $defaults ); 
     300 
     301        if ( 'thumb' === $attr['size'] ) 
     302                $attr['size'] = 'thumbnail'; 
     303 
     304        // Size in built in sizes - call size setting from DB 
     305        # behavoir in here, dependent on outcome of @link http://core.trac.wordpress.org/ticket/18947 
     306        if ( ! in_array( $attr['size'], array_keys( $_wp_additional_image_sizes ) ) ) 
     307        { 
     308                $sizes                          = get_intermediate_image_sizes(); 
     309                // Get option - gladly autoloaded/can use wp_cache_get(); 
     310                $size_data['width']     = intval( get_option( "{$attr['size']}_size_w") ); 
     311                $size_data['height']= intval( get_option( "{$attr['size']}_size_h") ); 
     312                // Not sure how this will behave if cropped is false (autoloaded option not added) 
     313                $size_data['crop']      = get_option( "{$attr['size']}_crop" ) ? get_option( "{$attr['size']}_crop" ) : false; 
     314        } 
     315        // Size array from global registered additional/custom sizes array 
     316        else  
     317        { 
     318                $size_data = $_wp_additional_image_sizes[ $attr['size'] ]; 
     319        } 
     320 
     321        // Retrieve image width & height 
     322        $img_info       = @getimagesize( $attr['url'] ); 
     323 
     324        // Calculate final dimensions - if "crop" was set to true during add_image_size(), the img will get ... cropped 
     325        $end_sizes      = image_resize_dimensions( $img_info[0], $img_info[1], $size_data['width'], $size_data['height'], $size_data['crop'] ); 
     326 
     327        // defaults to px units - can't get changed, as applying units is not possible 
     328        $hwstring       = ' '.trim( image_hwstring( $end_sizes[4], $end_sizes[5] ) ); 
     329 
     330        // Attributes: 
     331        // Not made required as users tend to do funky things (...and lock screen readers out) 
     332        $attr['alt'] = $attr['alt'] ? ' alt="'.esc_attr( $attr['alt'] ).'"' : ''; 
     333 
     334        if ( ! $attr['title'] ) 
     335        { 
     336                $mime = explode( "/", $img_info['mime'] ); 
     337                $attr['title'] = sprintf( __('default image of type: %1$s'), ucfirst( $mime[1] ) ); 
     338        } 
     339        $attr['title'] = $attr['title'] ? ' title="'.esc_attr( $attr['title'] ).'"' : ''; 
     340 
     341        $attr['classes'] = "wp-img-default ".esc_attr( $attr['classes'] ? $attr['classes'] : '' ); 
     342        $attr['align'] = $attr['align'] ? "align".esc_attr( $attr['align'] ) : ''; 
     343        $attr['size'] = "size-".esc_attr( $attr['size'] ); 
     344 
     345        // Allow filtering of the default attributes 
     346        $attributes     = apply_filters( 'wp_default_img_attr', $attr ); 
     347 
     348        // Build class attribute, considering that maybe some attribute was unset via the filter 
     349        $classes  = ' class="'; 
     350        $classes .= 'wp-img-default'.esc_attr( $attr['classes'] ? ' '.$attr['classes'] : '' ); 
     351        $classes .= $attr['align'] ? ' '.esc_attr( $attr['align'] ) : ''; 
     352        $classes .= $attr['size'] ? ' '.esc_attr( $attr['size'] ).'" ' : '" '; 
     353 
     354        $url            = trim( $attr['url'] ); 
     355        $image          = "<img src='{$url}'{$hwstring}{$classes}{$attr['alt']}{$attr['title']} />"; 
     356 
     357        // Allow filtering of output 
     358        $image          = apply_filters( 'wp_default_img', $image ); 
     359 
     360        if ( ! $attr['echo'] ) 
     361                return $image; 
     362 
     363        return print $image; 
     364} 
     365 
     366/** 
    267367 * Calculates the new dimensions for a downsampled image. 
    268368 * 
    269369 * If either width or height are empty, no constraint is applied on 
     
    579679/** 
    580680 * Get the available image sizes 
    581681 * @since 3.0.0 
     682 * @param bool $keys_only Whether to return only the keys or an assoc array of  
     683 *                      image sizes with their associated size & crop values as sub array. 
    582684 * @return array Returns a filtered array of image size strings 
    583685 */ 
    584 function get_intermediate_image_sizes() { 
     686function get_intermediate_image_sizes( $keys_only = true ) { 
    585687        global $_wp_additional_image_sizes; 
    586688        $image_sizes = array('thumbnail', 'medium', 'large'); // Standard sizes 
     689        if ( ! $keys_only ) { 
     690                foreach ( $image_sizes as $size ) { 
     691                        $image_sizes[ $size ]['width']  = intval( get_option( "{$size}_size_w") ); 
     692                        $image_sizes[ $size ]['height'] = intval( get_option( "{$size}_size_h") ); 
     693                        // Crop false per default if not set 
     694                        $image_sizes[ $size ]['crop']   = get_option( "{$size}_crop" ) ? get_option( "{$size}_crop" ) : false; 
     695                } 
     696        } 
    587697        if ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) ) 
    588698                $image_sizes = array_merge( $image_sizes, array_keys( $_wp_additional_image_sizes ) ); 
    589699