Skip to content

Instantly share code, notes, and snippets.

@polevaultweb
Created January 8, 2016 16:03
Show Gist options
  • Save polevaultweb/d5e2af9150e0495d078b to your computer and use it in GitHub Desktop.
Save polevaultweb/d5e2af9150e0495d078b to your computer and use it in GitHub Desktop.
Restores the functionality of copying of HiDPI (@2x) images during S3 offload of attachments.
<?php
/*
Plugin Name: WP Offload S3 - Copy HiDPI Images
Description: Restores the functionality of copying of HiDPI (@2x) images during S3 offload of attachments.
Author: Delicious Brains
Version: 1.0
Author URI: https://deliciousbrains.com/
*/
add_filter( 'as3cf_attachment_file_paths', 'wpos3_hipdi_add_hidpi_file_paths' );
function wpos3_hipdi_add_hidpi_file_paths( $paths ) {
global $as3cf;
foreach ( $paths as $path ) {
$paths[] = $as3cf->apply_file_suffix( $path, '@2x' );
}
return $paths;
}
add_filter( 'as3cf_find_replace_url_pairs', 'wpos3_hipdi_add_hidpi_find_replace_url_pairs', 10, 7 );
function wpos3_hipdi_add_hidpi_find_replace_url_pairs( $find_replace_pairs, $file_path, $old_url, $new_url, $meta, $old_filepath, $old_meta ) {
global $as3cfpro;
$file_name = basename( $file_path );
$old_filename = $file_name;
if ( ! is_null( $old_filepath ) ) {
$old_filename = basename( $old_filepath );
}
// Replace URLs for @2x images used by most HiDPI plugins
$hidpi_images = array();
foreach ( $find_replace_pairs as $image ) {
$hidpi_path = $as3cfpro->apply_file_suffix( $image['old_path'], '@2x' );
$hidpi_file = basename( $hidpi_path );
$old_hidpi_file = $hidpi_file;
if ( ! is_null( $old_filepath ) ) {
$existing_path = str_replace( basename( $image['new_url'] ), basename( $image['old_url'] ), $image['old_path'] );
$old_hidpi_path = $as3cfpro->apply_file_suffix( $existing_path, '@2x' );
$old_hidpi_file = basename( $old_hidpi_path );
}
$hidpi_images[] = array(
'old_url' => str_replace( $old_filename, $old_hidpi_file, $old_url ),
'new_url' => str_replace( $file_name, $hidpi_file, $new_url ),
);
}
return array_merge( $find_replace_pairs, $hidpi_images );
}
@travelmassive
Copy link

Doesn't work for me with latest version of WP Offload S3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment