Skip to content

Commit 6b7864c

Browse files
authored
Merge pull request #73 from wpgaurav/claude/add-image-srcset-OP5W8
Claude/add image srcset op5 w8
2 parents bfb362c + cc3aa36 commit 6b7864c

3 files changed

Lines changed: 127 additions & 2 deletions

File tree

acf-blocks.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Plugin Name: ACF Blocks
44
* Plugin URI: https://github.com/wpgaurav/acf-blocks-plugin
55
* Description: A collection of ACF Pro blocks for the WordPress block editor with automatic field group registration.
6-
* Version: 2.5.0
6+
* Version: 2.5.1
77
* Requires at least: 6.0
88
* Requires PHP: 7.4
99
* Author: Gaurav Tiwari
@@ -19,7 +19,7 @@
1919
}
2020

2121
// Plugin constants
22-
define( 'ACF_BLOCKS_VERSION', '2.5.0' );
22+
define( 'ACF_BLOCKS_VERSION', '2.5.1' );
2323
define( 'ACF_BLOCKS_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
2424
define( 'ACF_BLOCKS_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
2525
define( 'ACF_BLOCKS_PLUGIN_FILE', __FILE__ );

includes/compat.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,19 @@ function acf_blocks_resolve_image( $image, $fallback_alt = '', $size = 'medium'
252252
// Case 3: Direct URL string
253253
if ( is_string( $image ) && filter_var( $image, FILTER_VALIDATE_URL ) ) {
254254
$result['src'] = $image;
255+
// Try to find an attachment ID for same-domain URLs (e.g. localized images).
256+
$attachment_id = acf_blocks_url_to_attachment_id( $image );
257+
if ( $attachment_id ) {
258+
$sized = wp_get_attachment_image_src( $attachment_id, $size );
259+
if ( $sized ) {
260+
$result['src'] = $sized[0];
261+
}
262+
$img_alt = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
263+
if ( $img_alt && empty( $fallback_alt ) ) {
264+
$result['alt'] = $img_alt;
265+
}
266+
$result = acf_blocks_attach_srcset( $result, $attachment_id, $size );
267+
}
255268
return $result;
256269
}
257270

@@ -286,6 +299,35 @@ function acf_blocks_attach_srcset( $result, $attachment_id, $size ) {
286299
return $result;
287300
}
288301

302+
/**
303+
* Try to find a WordPress attachment ID from a same-domain image URL.
304+
*
305+
* Uses attachment_url_to_postid() which looks up the _wp_attached_file meta.
306+
* Only runs the lookup for URLs that belong to the current site to avoid
307+
* unnecessary database queries for external images.
308+
*
309+
* @param string $url The image URL to look up.
310+
* @return int Attachment ID, or 0 if not found or external URL.
311+
*/
312+
function acf_blocks_url_to_attachment_id( $url ) {
313+
$site_host = wp_parse_url( home_url(), PHP_URL_HOST );
314+
$url_host = wp_parse_url( $url, PHP_URL_HOST );
315+
316+
if ( ! $url_host || ! $site_host ) {
317+
return 0;
318+
}
319+
320+
// Only look up same-domain or subdomain URLs.
321+
$site_bare = preg_replace( '/^www\./i', '', $site_host );
322+
$url_bare = preg_replace( '/^www\./i', '', $url_host );
323+
324+
if ( $url_bare !== $site_bare && ! str_ends_with( $url_bare, '.' . $site_bare ) ) {
325+
return 0;
326+
}
327+
328+
return (int) attachment_url_to_postid( $url );
329+
}
330+
289331
/**
290332
* Get a nested repeater from flat block data.
291333
*

includes/image-localizer.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ function acf_blocks_download_external_image( $url ) {
207207

208208
// Already downloaded — return cached path.
209209
if ( file_exists( $target_path ) ) {
210+
// Ensure a WP attachment exists for srcset support.
211+
acf_blocks_ensure_localised_attachment( $target_path, $target_url, $filename );
210212
return $target_url;
211213
}
212214

@@ -246,9 +248,90 @@ function acf_blocks_download_external_image( $url ) {
246248
return false;
247249
}
248250

251+
// Register as a WP attachment so WordPress generates image sizes for srcset.
252+
acf_blocks_ensure_localised_attachment( $target_path, $target_url, $filename );
253+
249254
return $target_url;
250255
}
251256

257+
/**
258+
* Ensure a localized image file has a corresponding WordPress attachment.
259+
*
260+
* Creates an attachment post and generates image metadata (thumbnails)
261+
* if one does not already exist. This enables srcset/sizes support for
262+
* images downloaded by the localizer.
263+
*
264+
* @param string $file_path Absolute path to the image file.
265+
* @param string $file_url Public URL of the image file.
266+
* @param string $filename The filename (e.g. "md5hash.jpg").
267+
* @return int Attachment ID, or 0 on failure.
268+
*/
269+
function acf_blocks_ensure_localised_attachment( $file_path, $file_url, $filename ) {
270+
// Check if an attachment already exists for this file.
271+
$existing_id = acf_blocks_get_attachment_id_by_file( $file_path );
272+
if ( $existing_id ) {
273+
return $existing_id;
274+
}
275+
276+
// Determine MIME type.
277+
$mime_type = wp_check_filetype( $filename )['type'];
278+
if ( ! $mime_type ) {
279+
return 0;
280+
}
281+
282+
// Build the relative path WordPress expects for _wp_attached_file meta.
283+
$upload_dir = wp_upload_dir();
284+
$relative_path = str_replace( trailingslashit( $upload_dir['basedir'] ), '', $file_path );
285+
286+
$attachment_data = array(
287+
'post_title' => sanitize_file_name( pathinfo( $filename, PATHINFO_FILENAME ) ),
288+
'post_mime_type' => $mime_type,
289+
'post_status' => 'inherit',
290+
);
291+
292+
$attachment_id = wp_insert_attachment( $attachment_data, $file_path );
293+
294+
if ( ! $attachment_id || is_wp_error( $attachment_id ) ) {
295+
return 0;
296+
}
297+
298+
// Set the correct relative path so attachment_url_to_postid() can find it.
299+
update_post_meta( $attachment_id, '_wp_attached_file', $relative_path );
300+
301+
// Generate image sizes and metadata.
302+
if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) {
303+
require_once ABSPATH . 'wp-admin/includes/image.php';
304+
}
305+
306+
$metadata = wp_generate_attachment_metadata( $attachment_id, $file_path );
307+
if ( ! empty( $metadata ) ) {
308+
wp_update_attachment_metadata( $attachment_id, $metadata );
309+
}
310+
311+
return $attachment_id;
312+
}
313+
314+
/**
315+
* Find an existing attachment ID by its file path.
316+
*
317+
* @param string $file_path Absolute path to the image file.
318+
* @return int Attachment ID, or 0 if not found.
319+
*/
320+
function acf_blocks_get_attachment_id_by_file( $file_path ) {
321+
$upload_dir = wp_upload_dir();
322+
$relative_path = str_replace( trailingslashit( $upload_dir['basedir'] ), '', $file_path );
323+
324+
global $wpdb;
325+
$attachment_id = $wpdb->get_var(
326+
$wpdb->prepare(
327+
"SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_wp_attached_file' AND meta_value = %s LIMIT 1",
328+
$relative_path
329+
)
330+
);
331+
332+
return $attachment_id ? (int) $attachment_id : 0;
333+
}
334+
252335
/**
253336
* Get (and create if needed) the local image storage directory.
254337
*

0 commit comments

Comments
 (0)