| 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' ) ); |
| | 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 | */ |
| | 4165 | function 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 | } |