WordPress.org

Make WordPress Core

Ticket #18951: 18951.patch

File 18951.patch, 4.3 KB (added by F J Kaiser, 7 years ago)

Draft of wp_default_img() inside /wp-includes/media.php

  • 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