WordPress.org

Make WordPress Core

Ticket #44801: 44801.diff

File 44801.diff, 5.4 KB (added by flixos90, 15 months ago)
  • src/wp-includes/default-filters.php

     
    503503// Media
    504504add_action( 'wp_playlist_scripts', 'wp_playlist_scripts' );
    505505add_action( 'customize_controls_enqueue_scripts', 'wp_plupload_default_settings' );
     506add_filter( 'editor_max_image_size', 'wp_constrain_editor_max_image_size_to_content_width', 0, 3 );
    506507
    507508// Nav menu
    508509add_filter( 'nav_menu_item_id', '_nav_menu_item_id_use_once', 10, 2 );
  • src/wp-includes/media.php

     
    4343 *
    4444 * @since 2.5.0
    4545 *
    46  * @global int   $content_width
    47  *
    4846 * @param int          $width   Width of the image in pixels.
    4947 * @param int          $height  Height of the image in pixels.
    5048 * @param string|array $size    Optional. Image size. Accepts any valid image size, or an array
     
    5553 * @return array Width and height of what the result image should resize to.
    5654 */
    5755function image_constrain_size_for_editor( $width, $height, $size = 'medium', $context = null ) {
    58         global $content_width;
    59 
    6056        $_wp_additional_image_sizes = wp_get_additional_image_sizes();
    6157
    6258        if ( ! $context ) {
     
    6662        if ( is_array( $size ) ) {
    6763                $max_width  = $size[0];
    6864                $max_height = $size[1];
    69         } elseif ( $size == 'thumb' || $size == 'thumbnail' ) {
     65        } elseif ( in_array( $size, array( 'thumb', 'thumbnail' ), true ) ) {
    7066                $max_width  = intval( get_option( 'thumbnail_size_w' ) );
    7167                $max_height = intval( get_option( 'thumbnail_size_h' ) );
    7268                // last chance thumbnail size defaults
     
    7470                        $max_width  = 128;
    7571                        $max_height = 96;
    7672                }
    77         } elseif ( $size == 'medium' ) {
    78                 $max_width  = intval( get_option( 'medium_size_w' ) );
    79                 $max_height = intval( get_option( 'medium_size_h' ) );
    80 
    81         } elseif ( $size == 'medium_large' ) {
    82                 $max_width  = intval( get_option( 'medium_large_size_w' ) );
    83                 $max_height = intval( get_option( 'medium_large_size_h' ) );
    84 
    85                 if ( intval( $content_width ) > 0 ) {
    86                         $max_width = min( intval( $content_width ), $max_width );
    87                 }
    88         } elseif ( $size == 'large' ) {
    89                 /*
    90                  * We're inserting a large size image into the editor. If it's a really
    91                  * big image we'll scale it down to fit reasonably within the editor
    92                  * itself, and within the theme's content width if it's known. The user
    93                  * can resize it in the editor if they wish.
    94                  */
    95                 $max_width  = intval( get_option( 'large_size_w' ) );
    96                 $max_height = intval( get_option( 'large_size_h' ) );
    97                 if ( intval( $content_width ) > 0 ) {
    98                         $max_width = min( intval( $content_width ), $max_width );
    99                 }
     73        } elseif ( in_array( $size, array( 'medium', 'medium_large', 'large' ), true ) ) {
     74                $max_width  = intval( get_option( $size . '_size_w' ) );
     75                $max_height = intval( get_option( $size . '_size_h' ) );
    10076        } elseif ( ! empty( $_wp_additional_image_sizes ) && in_array( $size, array_keys( $_wp_additional_image_sizes ) ) ) {
    10177                $max_width  = intval( $_wp_additional_image_sizes[ $size ]['width'] );
    10278                $max_height = intval( $_wp_additional_image_sizes[ $size ]['height'] );
    103                 // Only in admin. Assume that theme authors know what they're doing.
    104                 if ( intval( $content_width ) > 0 && 'edit' === $context ) {
    105                         $max_width = min( intval( $content_width ), $max_width );
    106                 }
    10779        } // $size == 'full' has no constraint
    10880        else {
    10981                $max_width  = $width;
     
    41704142                'done' => $done,
    41714143        );
    41724144}
     4145
     4146/**
     4147 * Constrains the maximum image size, taking the $content_width global into account.
     4148 *
     4149 * Theme authors may define a `$content_width` global to specify how wide the typical theme content is.
     4150 * This filter function accounts for that global, adjusting maximum image widths as follows:
     4151 *
     4152 * - For the 'medium_large' and 'large' sizes, use the minimum of the original maximum width and `$content_width`.
     4153 * - For custom image sizes, use the minimum of the original maximum width and `$content_width` if `$context` is 'edit'.
     4154 * - For all other sizes, do not change anything.
     4155 *
     4156 * @since 5.0.0
     4157 *
     4158 * @global int $content_width
     4159 *
     4160 * @param array        $max_image_size An array of the maximum width and height.
     4161 * @param string|array $size           Size of what the result image should be.
     4162 * @param string       $context        Context for what the resizing is for, either 'display' or 'edit'.
     4163 * @return array Filtered $max_image_size, with the width possibly adjusted.
     4164 */
     4165function wp_constrain_editor_max_image_size_to_content_width( $max_image_size, $size, $context ) {
     4166        global $content_width;
     4167
     4168        // Bail if no valid content width is set, or if $size is an array.
     4169        if ( intval( $content_width ) <= 0 || is_array( $size ) ) {
     4170                return $max_image_size;
     4171        }
     4172
     4173        switch ( $size ) {
     4174                case 'thumb':
     4175                case 'thumbnail':
     4176                case 'medium':
     4177                        break;
     4178                case 'medium_large':
     4179                case 'large':
     4180                        $max_image_size[0] = min( intval( $content_width ), $max_image_size[0] );
     4181                        break;
     4182                default:
     4183                        $additional_sizes = wp_get_additional_image_sizes();
     4184
     4185                        // Only in admin. Assume that theme authors know what they're doing.
     4186                        if ( isset( $additional_sizes[ $size ] ) && 'edit' === $context ) {
     4187                                $max_image_size[0] = min( intval( $content_width ), $max_image_size[0] );
     4188                        }
     4189        }
     4190
     4191        return $max_image_size;
     4192}