From dd691394f2de7ca4270c2936fd0ea7741d97af5d Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Tue, 16 Jan 2024 17:01:30 +0000 Subject: [PATCH 01/42] Media: Consider inline image CSS width to backfill `width` and `height` attributes. Prior to this changeset, WordPress core would use the original image size, which in the particular case of inline images would be severely off, as they are usually very small. This could lead to incorrect application of `fetchpriority="high"` and other performance optimizations. Props westonruter, flixos90, joemcgill, mukesh27. Fixes #59352. git-svn-id: https://develop.svn.wordpress.org/trunk@57294 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/media.php | 7 ++ .../media/wpImgTagAddWidthAndHeightAttr.php | 85 +++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 tests/phpunit/tests/media/wpImgTagAddWidthAndHeightAttr.php diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 9cf301e95c11a..38ec2213b7506 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -2117,6 +2117,13 @@ function wp_img_tag_add_width_and_height_attr( $image, $context, $attachment_id $size_array = wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id ); if ( $size_array ) { + // If the width is enforced through style (e.g. in an inline image), calculate the dimension attributes. + $style_width = preg_match( '/style="width:\s*(\d+)px;"/', $image, $match_width ) ? (int) $match_width[1] : 0; + if ( $style_width ) { + $size_array[1] = (int) round( $size_array[1] * $style_width / $size_array[0] ); + $size_array[0] = $style_width; + } + $hw = trim( image_hwstring( $size_array[0], $size_array[1] ) ); return str_replace( 'attachment->create_upload_object( $file ); + self::$attachment_width = 680; + self::$attachment_height = 1024; + } + + public static function tear_down_after_class() { + wp_delete_attachment( self::$attachment_id, true ); + parent::tear_down_after_class(); + } + + /** + * Tests that `wp_img_tag_add_width_and_height_attr()` adds dimension attributes to an image when they are missing. + * + * @ticket 50367 + */ + public function test_add_width_and_height_when_missing() { + $image_tag = ''; + + $this->assertSame( + '', + wp_img_tag_add_width_and_height_attr( $image_tag, 'the_content', self::$attachment_id ) + ); + } + + /** + * Tests that `wp_img_tag_add_width_and_height_attr()` does not add dimension attributes when disabled via filter. + * + * @ticket 50367 + */ + public function test_do_not_add_width_and_height_when_disabled_via_filter() { + add_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' ); + $image_tag = ''; + + $this->assertSame( + $image_tag, + wp_img_tag_add_width_and_height_attr( $image_tag, 'the_content', self::$attachment_id ) + ); + } + + /** + * Tests that `wp_img_tag_add_width_and_height_attr()` does not add dimension attributes to an image without src. + * + * @ticket 50367 + */ + public function test_do_not_add_width_and_height_without_src() { + $image_tag = ''; + + $this->assertSame( + $image_tag, + wp_img_tag_add_width_and_height_attr( $image_tag, 'the_content', self::$attachment_id ) + ); + } + + /** + * Tests that `wp_img_tag_add_width_and_height_attr()` respects the style attribute from the inline image format to + * correctly set width and height based on that. + * + * @ticket 59352 + */ + public function test_consider_inline_image_style_attr_to_set_width_and_height() { + // '85px' is the original width (680px) divided by 8, so the expected height is equivalently 1024/8=128. + $image_tag = ''; + + $this->assertSame( + '', + wp_img_tag_add_width_and_height_attr( $image_tag, 'the_content', self::$attachment_id ) + ); + } +} From 35add7378e093c8808ef9c08ee4b07f71b14963d Mon Sep 17 00:00:00 2001 From: Joe Dolson Date: Tue, 16 Jan 2024 17:29:58 +0000 Subject: [PATCH 02/42] Administration: Remove empty form `action` attributes. Remove the `action` attribute in the login language selector, privacy forms, and classic widget forms. An empty `action` attribute is invalid HTML4 and unsupported HTML5. The `action` attribute is optional, but must have a valid URL when provided. Props Malae, audrasjb, bartkleinreesink, nicolefurlan, shubhamsedani, costdev, peterwilsoncc, rajinsharwar, joedolson. Fixes #58226. git-svn-id: https://develop.svn.wordpress.org/trunk@57295 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/options-privacy.php | 4 ++-- src/wp-admin/widgets-form.php | 2 +- src/wp-login.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/wp-admin/options-privacy.php b/src/wp-admin/options-privacy.php index 6441a43491ac4..92de2ca9b9b68 100644 --- a/src/wp-admin/options-privacy.php +++ b/src/wp-admin/options-privacy.php @@ -270,7 +270,7 @@ static function ( $body_class ) { -
+ - +
- +

'inactive-widgets-control-remove' ); diff --git a/src/wp-login.php b/src/wp-login.php index 0641cd52e6a40..9eeac4a96ce46 100644 --- a/src/wp-login.php +++ b/src/wp-login.php @@ -364,7 +364,7 @@ function login_footer( $input_id = '' ) { if ( ! empty( $languages ) ) { ?>

- +