Skip to content

Instantly share code, notes, and snippets.

@tomjn
Last active May 24, 2023 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomjn/fce8d691694f6f002cbc956c11d8c346 to your computer and use it in GitHub Desktop.
Save tomjn/fce8d691694f6f002cbc956c11d8c346 to your computer and use it in GitHub Desktop.
Replace missing attachments with placekitten placeholders, useful for local imports of databases without the uploads folder
<?php
/**
* Local Kitten images.
*
* Filters image URLs to load files from placekitten if they aren't present locally.
*
* @wordpress-plugin
* Plugin Name: Local Kitten Images
* Version: 1.0.2
* Description: Loads missing image files from placekitten when using local environment.
* Author: Human Made
* License: GPL v3
*/
add_filter( 'wp_get_attachment_image_src', 'localkitten_environment_attachment_image_src', 10, 2 );
add_filter( 'wp_calculate_image_srcset', 'localkitten_environment_wp_calculate_image_srcset', 10, 5 );
add_filter( 'wp_get_attachment_url', 'localkitten_wp_get_attachment_url', 10, 2 );
/**
* Returns the placeholder image service URL, with an optional seed
*
* @param string $seed optional seed if the service supports it
* @return string placeholder service URL ready for width and height appending
*/
function localkitten_get_service_url( $seed = '' ) : string {
return 'https://placekitten.com/';
/*if ( ! empty( $seed ) ) {
return 'https://picsum.photos/seed/' . $seed . '/';
}
return 'https://picsum.photos/';
return 'https://placebear.com/';*/
}
/**
* Filter image URLs for missing files to placekitten.
*
* @param array|false $image Array of image data, or boolean false if no image is available.
* [ string: url, int: width, int: height, bool: is_resized ]
* @param int attachment ID
* @return array Filtered image.
*/
function localkitten_environment_attachment_image_src( $image, $attachment_id ) {
if ( ! isset( $image[0] ) || ! is_array( $image ) ) {
return $image;
}
// Return early if we have a file locally.
$file = get_attached_file( $attachment_id );
if ( ! empty( $file ) && file_exists( $file ) ) {
return '';
}
// Don't process remote image URLs.
$image_host = parse_url( $image[0], PHP_URL_HOST );
$site_host = parse_url( site_url(), PHP_URL_HOST );
if ( ! ( strpos( $site_host, $image_host ) > 0 ) && ! ( strpos( $image_host, $site_host ) > 0 ) ) {
return $image;
}
$image[0] = localkitten_get_service_url( $attachment_id ) . $image[1] . '/' . $image[2];
return $image;
}
/**
* Filters srcset URLs.
*
* @param array $sources srcset sources
* @param array $size_array array of image sizes
* @param string $image_src source URL
* @param array $image_meta meta
* @param int $attachment_id ID of attachment
* @return array srcset sources with replaced URLs
*/
function localkitten_environment_wp_calculate_image_srcset( array $sources, $size_array, $image_src, $image_meta, $attachment_id ) : array {
// Return early if this is a locally uploaded asset.
$file = get_attached_file( $attachment_id );
if ( ! empty( $file ) && file_exists( $file ) ) {
return $sources;
}
// Return early if we don't have sizes.
if ( empty( $size_array ) ) {
return $sources;
}
$new_sources = [
[
'url' => localkitten_get_service_url() . $size_array[0] . '/' . $size_array[1],
'descriptor' => 'w',
'value' => $size_array[0],
],
];
return $new_sources;
}
/**
* Override attachment URLs
*
* @param string|mixed $url the URL or falsey
* @param int $attachment_id ID of the attachment to grab the URL for
* @return string|mixed the new URL or false
*/
function localkitten_wp_get_attachment_url( $url, $attachment_id ) {
// Only process images.
if ( ! wp_attachment_is_image( $attachment_id ) ) {
return $url;
}
// Don't do this is we have the file locally.
$file = get_attached_file( $attachment_id );
if ( ! empty( $file ) && file_exists( $file ) ) {
return $url;
}
$image = wp_get_attachment_metadata( $attachment_id, true );
// We need the width/height to use a placeholder, return early
// if we don't have them.
if ( ! $image ) {
return $url;
}
return localkitten_get_service_url( $attachment_id ) . $image['width'] . '/' . $image['height'];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment