Skip to content

Commit

Permalink
Additional changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mukeshpanchal27 committed Oct 10, 2023
1 parent 8c8ab43 commit b823f87
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 100 deletions.
78 changes: 0 additions & 78 deletions modules/images/dominant-color-images/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,84 +153,6 @@ function dominant_color_img_tag_add_dominant_color( $filtered_image, $context, $
}
add_filter( 'wp_content_img_tag', 'dominant_color_img_tag_add_dominant_color', 20, 3 );

// We don't need to use this filter anymore as the filter wp_content_img_tag is used instead.
if ( version_compare( '6', $GLOBALS['wp_version'], '>=' ) ) {

/**
* Filter the content to allow us to filter the image tags.
*
* @since 1.2.0
*
* @param string $content The content to filter.
* @param string $context The context of the content.
* @return string The updated $content.
*/
function dominant_color_filter_content_tags( $content, $context = null ) {
if ( null === $context ) {
$context = current_filter();
}

if ( ! preg_match_all( '/<(img)\s[^>]+>/', $content, $matches, PREG_SET_ORDER ) ) {
return $content;
}

// List of the unique `img` tags found in $content.
$images = array();

foreach ( $matches as $match ) {
list( $tag, $tag_name ) = $match;

switch ( $tag_name ) {
case 'img':
if ( preg_match( '/wp-image-([0-9]+)/i', $tag, $class_id ) ) {
$attachment_id = absint( $class_id[1] );

if ( $attachment_id ) {
// If exactly the same image tag is used more than once, overwrite it.
// All identical tags will be replaced later with 'str_replace()'.
$images[ $tag ] = $attachment_id;
break;
}
}
$images[ $tag ] = 0;
break;
}
}

// Reduce the array to unique attachment IDs.
$attachment_ids = array_unique( array_filter( array_values( $images ) ) );

if ( count( $attachment_ids ) > 1 ) {
/*
* Warm the object cache with post and meta information for all found.
* images to avoid making individual database calls.
*/
_prime_post_caches( $attachment_ids, false, true );
}

foreach ( $matches as $match ) {
// Filter an image match.
if ( empty( $images[ $match[0] ] ) ) {
continue;
}

$filtered_image = $match[0];
$attachment_id = $images[ $match[0] ];
$filtered_image = dominant_color_img_tag_add_dominant_color( $filtered_image, $context, $attachment_id );

if ( null !== $filtered_image && $filtered_image !== $match[0] ) {
$content = str_replace( $match[0], $filtered_image, $content );
}
}
return $content;
}

$filters = array( 'the_content', 'the_excerpt', 'widget_text_content', 'widget_block_content' );
foreach ( $filters as $filter ) {
add_filter( $filter, 'dominant_color_filter_content_tags', 20 );
}
}

/**
* Add CSS needed for to show the dominant color as an image background.
*
Expand Down
14 changes: 4 additions & 10 deletions modules/images/webp-uploads/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -745,26 +745,20 @@ function webp_uploads_get_image_sizes_additional_mime_type_support() {
/**
* Updates the quality of WebP image sizes generated by WordPress to 82.
*
* Prior to WordPress 6.1, the MIME type was not correctly provided to this filter, so for those versions this function
* simply returns 82 universally (since all other MIME types already use 82 anyway).
*
* @since 1.7.0
*
* @global string $wp_version The WordPress version string.
* @since n.e.x.t Bump minimum WP to 6.3 so remove WP 6.1 related checks.
*
* @param int $quality Quality level between 1 (low) and 100 (high).
* @param string $mime_type Image mime type.
* @return int The updated quality for mime types.
*/
function webp_uploads_modify_webp_quality( $quality, $mime_type ) {
global $wp_version;

// Below WP 6.1 or for WebP images, always return 82 (other MIME types were already using 82 by default anyway).
if ( version_compare( $wp_version, '6.1', '<' ) || 'image/webp' === $mime_type ) {
// For WebP images, always return 82 (other MIME types were already using 82 by default anyway).
if ( 'image/webp' === $mime_type ) {
return 82;
}

// Return default quality for non-WebP images in WP 6.1+.
// Return default quality for non-WebP images in WP.
return $quality;
}
add_filter( 'wp_editor_set_quality', 'webp_uploads_modify_webp_quality', 10, 2 );
Expand Down
4 changes: 0 additions & 4 deletions modules/images/webp-uploads/image-edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,6 @@ function webp_uploads_restore_image( $attachment_id, $data ) {
* @return bool True if editing image thumbnails is enabled, false otherwise.
*/
function webp_uploads_image_edit_thumbnails_separately() {
if ( version_compare( get_bloginfo( 'version' ), '6.3', '<' ) ) {
return true;
}

/** This filter is documented in wp-admin/includes/image-edit.php */
return (bool) apply_filters( 'image_edit_thumbnails_separately', false );
}
3 changes: 1 addition & 2 deletions server-timing/defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,7 @@ static function ( $hook_name ) use ( $hooks_to_measure ) {
)
);
},
// phpcs:ignore PHPCompatibility.Constants.NewConstants.php_int_minFound
defined( 'PHP_INT_MIN' ) ? PHP_INT_MIN : ~PHP_INT_MAX
PHP_INT_MIN
);
}

Expand Down
7 changes: 1 addition & 6 deletions tests/modules/images/webp-uploads/load-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -977,12 +977,7 @@ public function it_should_return_correct_quality_for_mime_types() {
global $wp_version;
$this->assertSame( 82, webp_uploads_modify_webp_quality( 90, 'image/webp' ), 'WebP image quality should always be 82.' );
$this->assertSame( 82, webp_uploads_modify_webp_quality( 82, 'image/webp' ), 'WebP image quality should always be 82.' );

if ( version_compare( $wp_version, '6.1', '<' ) ) {
$this->assertSame( 82, webp_uploads_modify_webp_quality( 86, 'image/jpeg' ), 'JPEG image quality should always return 82 quality for WP version lower than 6.1.' );
} else {
$this->assertSame( 80, webp_uploads_modify_webp_quality( 80, 'image/jpeg' ), 'JPEG image quality should return default quality provided from WP filter wp_editor_set_quality.' );
}
$this->assertSame( 80, webp_uploads_modify_webp_quality( 80, 'image/jpeg' ), 'JPEG image quality should return default quality provided from WP filter wp_editor_set_quality.' );
}

/**
Expand Down

0 comments on commit b823f87

Please sign in to comment.