Skip to content

Commit 0fda089

Browse files
authored
Merge pull request #221 from humanmade/wp-6.4-compat
Update the image tag filtering for WP 6+
2 parents 9396171 + f8d852c commit 0fda089

File tree

2 files changed

+68
-131
lines changed

2 files changed

+68
-131
lines changed

inc/cropper/namespace.php

Lines changed: 67 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,21 @@ function setup() {
7272
* the Core one doesn't work with Tachyon due to the sizing details
7373
* being stored in the query string.
7474
*/
75-
remove_filter( 'the_content', 'wp_filter_content_tags' );
76-
// Runs very late to ensure images have passed through Tachyon first.
77-
add_filter( 'the_content', __NAMESPACE__ . '\\filter_content_tags', 999999 );
75+
add_filter( 'wp_img_tag_add_width_and_height_attr', __NAMESPACE__ . '\\img_tag_add_attr', 10, 2 );
76+
add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', __NAMESPACE__ . '\\img_tag_add_attr', 10, 2 );
77+
78+
/**
79+
* Filters an img tag within the content for a given context.
80+
*
81+
* @param string $filtered_image Full img tag with attributes that will replace the source img tag.
82+
* @param string $context Additional context, like the current filter name or the function name from where this was called.
83+
* @param int $attachment_id The image attachment ID. May be 0 in case the image is not an attachment.
84+
* @return string Full img tag with attributes that will replace the source img tag.
85+
*/
86+
add_filter( 'wp_content_img_tag', __NAMESPACE__ . '\\content_img_tag', 10, 3 );
87+
88+
// Ensure the get dimensions function understands Tachyon.
89+
add_filter( 'wp_image_src_get_dimensions', __NAMESPACE__ . '\\src_get_dimensions', 10, 4 );
7890

7991
// Calculate srcset based on zoom modifiers.
8092
add_filter( 'wp_calculate_image_srcset', __NAMESPACE__ . '\\image_srcset', 10, 5 );
@@ -155,7 +167,9 @@ function rest_api_fields( WP_REST_Response $response ) : WP_REST_Response {
155167

156168
if ( isset( $data['source_url'] ) && $data['media_type'] === 'image' ) {
157169
$data['original_url'] = $data['source_url'];
158-
$data['source_url'] = tachyon_url( $data['source_url'] );
170+
if ( function_exists( 'tachyon_url' ) ) {
171+
$data['source_url'] = tachyon_url( $data['source_url'] );
172+
}
159173

160174
// Add focal point.
161175
$focal_point = get_post_meta( $data['id'], '_focal_point', true );
@@ -441,6 +455,10 @@ function skip_attachment( int $attachment_id ) : bool {
441455
* @return array
442456
*/
443457
function attachment_thumbs( $response, $attachment ) : array {
458+
if ( ! function_exists( 'tachyon_url' ) ) {
459+
return $response;
460+
}
461+
444462
if ( ! is_array( $response ) || wp_attachment_is_image( $attachment ) ) {
445463
return $response;
446464
}
@@ -884,143 +902,50 @@ function massage_meta_data_for_orientation( array $meta_data ) {
884902
}
885903

886904
/**
887-
* Filters 'img' elements in post content to add 'srcset' and 'sizes' attributes.
888-
*
889-
* @param string $content The raw post content to be filtered.
890-
* @param string $context The current filter context.
905+
* Add our special handlers for width & height attrs and srcset attributes.
891906
*
892-
* @return string Converted content with 'srcset' and 'sizes' attributes added to images.
907+
* @param string $filtered_image Full img tag with attributes that will replace the source img tag.
908+
* @param string $context Additional context, like the current filter name or the function name from where this was called.
909+
* @param int $attachment_id The image attachment ID. May be 0 in case the image is not an attachment.
910+
* @return string Full img tag with attributes that will replace the source img tag.
893911
*/
894-
function filter_content_tags( string $content, string $context = null ) : string {
895-
if ( null === $context ) {
896-
$context = current_filter();
897-
}
898-
899-
if ( false === strpos( $content, '<img' ) ) {
900-
return $content;
912+
function content_img_tag( string $filtered_image, string $context, int $attachment_id ) : string {
913+
if ( ! defined( 'TACHYON_URL' ) || strpos( $filtered_image, TACHYON_URL ) === false ) {
914+
return $filtered_image;
901915
}
902916

903-
$images = Tachyon::parse_images_from_html( $content );
904-
if ( empty( $images ) ) {
905-
// No images, leave early.
906-
return $content;
917+
if ( $attachment_id === 0 ) {
918+
return $filtered_image;
907919
}
908920

909-
// This bit is from Core.
910-
$selected_images = [];
911-
$attachment_ids = [];
921+
$image_meta = wp_get_attachment_metadata( $attachment_id );
912922

913-
foreach ( $images['img_url'] as $key => $img_url ) {
914-
if ( strpos( $img_url, TACHYON_URL ) !== 0 ) {
915-
// It's not a Tachyon URL.
916-
continue;
917-
}
918-
919-
$image_data = [
920-
'full_tag' => $images[0][ $key ],
921-
'link_url' => $images['link_url'][ $key ],
922-
'img_tag' => $images['img_tag'][ $key ],
923-
'img_url' => $images['img_url'][ $key ],
924-
];
925-
926-
$image = $image_data['img_tag'];
927-
928-
if ( false === strpos( $image, ' srcset=' ) && preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) && absint( $class_id[1] ) ) {
929-
$attachment_id = $class_id[1];
930-
$image_data['id'] = $attachment_id;
931-
/*
932-
* If exactly the same image tag is used more than once, overwrite it.
933-
* All identical tags will be replaced later with 'str_replace()'.
934-
*/
935-
$selected_images[ $image ] = $image_data;
936-
// Overwrite the ID when the same image is included more than once.
937-
$attachment_ids[ $attachment_id ] = true;
938-
}
923+
// Add 'width' and 'height' attributes if applicable.
924+
if ( ! str_contains( $filtered_image, ' width=' ) && ! str_contains( $filtered_image, ' height=' ) ) {
925+
$filtered_image = add_width_and_height_attr( $filtered_image, $image_meta );
939926
}
940927

941-
if ( empty( $attachment_ids ) ) {
942-
// No WP attachments, nothing further to do.
943-
return $content;
928+
// Add 'srcset' and 'sizes' attributes if applicable.
929+
if ( ! str_contains( $filtered_image, ' srcset=' ) ) {
930+
$filtered_image = add_srcset_and_sizes_attr( $filtered_image, $image_meta, $attachment_id );
944931
}
945932

946-
/*
947-
* Warm the object cache with post and meta information for all found
948-
* images to avoid making individual database calls.
949-
*/
950-
_prime_post_caches( array_keys( $attachment_ids ), false, true );
951-
952-
// Whether to add the lazy loading attribute.
953-
$add_loading_attr = wp_lazy_loading_enabled( 'img', $context );
954-
955-
foreach ( $selected_images as $image => $image_data ) {
956-
$attachment_id = $image_data['id'];
957-
958-
// The ID returned here may not always be an image, eg. if the attachment
959-
// has been deleted but is still referenced in the content or if the content
960-
// has been migrated and the attachment IDs no longer correlate to the right
961-
// post table entries.
962-
if ( ! wp_attachment_is_image( $attachment_id ) ) {
963-
continue;
964-
}
965-
966-
$image_meta = wp_get_attachment_metadata( $attachment_id );
967-
if ( ! $image_meta || ! is_array( $image_meta ) ) {
968-
trigger_error( sprintf( 'Could not retrieve image meta data for Attachment ID "%s"', (string) $attachment_id ), E_USER_WARNING );
969-
continue;
970-
}
971-
972-
/**
973-
* Filters whether to add the missing `width` and `height` HTML attributes to the img tag. Default `true`.
974-
*
975-
* Returning anything else than `true` will not add the attributes.
976-
*
977-
* @since 5.5.0
978-
*
979-
* @param bool $value The filtered value, defaults to `true`.
980-
* @param string $image The HTML `img` tag where the attribute should be added.
981-
* @param string $context Additional context about how the function was called or where the img tag is.
982-
* @param int $attachment_id The image attachment ID.
983-
*/
984-
$add_hw = apply_filters( 'wp_img_tag_add_width_and_height_attr', true, $image, $context, $attachment_id );
985-
986-
/**
987-
* Filters whether to add the `srcset` and `sizes` HTML attributes to the img tag. Default `true`.
988-
*
989-
* Returning anything else than `true` will not add the attributes.
990-
*
991-
* @since 5.5.0
992-
*
993-
* @param bool $value The filtered value, defaults to `true`.
994-
* @param string $image The HTML `img` tag where the attribute should be added.
995-
* @param string $context Additional context about how the function was called or where the img tag is.
996-
* @param int $attachment_id The image attachment ID.
997-
*/
998-
$add_srcset = apply_filters( 'wp_img_tag_add_srcset_and_sizes_attr', true, $image, $context, $attachment_id );
999-
1000-
$filtered_image = $image;
1001-
1002-
// Add 'width' and 'height' attributes if applicable.
1003-
if ( $add_hw && $attachment_id > 0 && false === strpos( $filtered_image, ' width=' ) && false === strpos( $filtered_image, ' height=' ) ) {
1004-
$filtered_image = add_width_and_height_attr( $filtered_image, $image_meta, $attachment_id );
1005-
}
1006-
1007-
// Add 'srcset' and 'sizes' attributes if applicable.
1008-
if ( $add_srcset && $attachment_id > 0 && false === strpos( $filtered_image, ' srcset=' ) ) {
1009-
$filtered_image = add_srcset_and_sizes_attr( $filtered_image, $image_meta, $attachment_id );
1010-
}
1011-
1012-
// Add 'loading' attribute if applicable.
1013-
if ( $add_loading_attr && false === strpos( $filtered_image, ' loading=' ) ) {
1014-
$filtered_image = wp_img_tag_add_loading_attr( $filtered_image, $context );
1015-
}
1016-
1017-
// Update the content.
1018-
if ( $filtered_image !== $image ) {
1019-
$content = str_replace( $image, $filtered_image, $content );
1020-
}
1021-
}
933+
return $filtered_image;
934+
}
1022935

1023-
return $content;
936+
/**
937+
* Filters whether to add various attributes to the img tag markup.
938+
*
939+
* We override this to ensure compatibility with Tachyon & smart media.
940+
*
941+
* @param bool $value The filtered value, defaults to <code>true</code>.
942+
* @param string $image The HTML <code>img</code> tag where the attribute should be added.
943+
* @param string $context Additional context about how the function was called or where the img tag is.
944+
* @param int $attachment_id The image attachment ID.
945+
* @return bool The filtered value, defaults to <code>true</code>.
946+
*/
947+
function img_tag_add_attr( bool $value, string $image ) : bool {
948+
return ! defined( 'TACHYON_URL' ) || strpos( $image, TACHYON_URL ) === false ? $value : false;
1024949
}
1025950

1026951
/**
@@ -1082,6 +1007,18 @@ function get_img_src_dimensions( string $image_src, array $image_meta ) {
10821007
return [ $width, $height ];
10831008
}
10841009

1010+
/**
1011+
* Filters the default method for getting image dimensions.
1012+
*
1013+
* @param array $dimensions List of width and height dimensions.
1014+
* @param string $image_src The current image src URL.
1015+
* @param array $image_meta Attachment metadata.
1016+
* @return void
1017+
*/
1018+
function src_get_dimensions( $dimensions, $image_src, $image_meta ) {
1019+
return get_img_src_dimensions( $image_src, $image_meta ) ?: $dimensions;
1020+
}
1021+
10851022
/**
10861023
* Adds 'width' and 'height' attributes to an existing 'img' element.
10871024
*
@@ -1198,7 +1135,7 @@ function image_srcset( array $sources, array $size_array, string $image_src, arr
11981135
list( $width, $height ) = array_map( 'absint', $size_array );
11991136

12001137
// Ensure this is _not_ a tachyon image, not always the case when parsing from post content.
1201-
if ( strpos( $image_src, TACHYON_URL ) === false ) {
1138+
if ( ! defined( 'TACHYON_URL' ) || strpos( $image_src, TACHYON_URL ) === false ) {
12021139
// If the aspect ratio requested matches a custom crop size, pull that
12031140
// crop (in case there's a user custom crop). Otherwise just use the
12041141
// given dimensions.

plugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Description: Advanced media tools that take advantage of Rekognition and Tachyon.
55
* Author: Human Made Limited
66
* License: GPL-3.0
7-
* Version: 0.4.3
7+
* Version: 0.5.0
88
*/
99

1010
namespace HM\Media;

0 commit comments

Comments
 (0)