Skip to content

Instantly share code, notes, and snippets.

@UmeshSingla
Last active November 16, 2017 11:17
Show Gist options
  • Save UmeshSingla/59888f84b514e31a6298a00ddfdeb8d9 to your computer and use it in GitHub Desktop.
Save UmeshSingla/59888f84b514e31a6298a00ddfdeb8d9 to your computer and use it in GitHub Desktop.
Smush PNG to JPG, Update image paths for converted images in case it wasn't updated by plugin. Make sure to enable debug log, in order to have a log of what all images are updated.
<?php
add_action('current_screen', 'smush_update_image_url');
function smush_update_image_url() {
global $wpsmush_db;
if( !function_exists('get_current_screen')) {
return;
}
$current_screen = get_current_screen();
if( empty( $current_screen ) ) {
return;
}
$current_page = $current_screen->base;
echo "<!-- $current_page -->";
if ( 'media_page_wp-smush-bulk' != $current_page ) {
return;
}
if( is_object( $wpsmush_db ) ) {
//get the list of converted images
$converted_images = $wpsmush_db->converted_images();
echo "<!-- <pre>";
print_r( $converted_images );
echo " </pre> -->";
if( is_array( $converted_images) && !empty( $converted_images ) ) {
//Iterate through all the image ids and Update the image URL in post content
foreach ( $converted_images as $image ) {
$updated = smush_replace_url( $image );
}
}
}
}
//Iterate over image size to request url update in post content of an attachment
function smush_replace_url( $id ) {
//Current file URL
$c_url = wp_get_attachment_url( $id );
$filepath = get_attached_file( $id );
//Get the original file path
$original_file = get_post_meta( $id, WP_SMUSH_PREFIX . 'original_file', true );
$pngjpg_savings = get_post_meta( $id, WP_SMUSH_PREFIX . 'pngjpg_savings', true );
//Return early if there are no pngjpg savings
if( empty( $pngjpg_savings) || !is_array( $pngjpg_savings ) ) {
return;
}
$metadata = wp_get_attachment_metadata( $id );
$sizes = array();
if( !empty( $metadata ) && !empty( $metadata['sizes'] ) ) {
$sizes = $metadata['sizes'];
}
if( empty( $original_file ) ) {
//Get GUID
$guid = get_the_guid( $id );
}
$basename = !empty( $original_file ) ? basename( $original_file ) : basename( $guid );
$base_url = dirname( $c_url );
$png_url = $base_url . '/' . $basename;
//Iterate over various converted sizes of an attachment
foreach ( $pngjpg_savings as $image_size => $savings ) {
//If there are no savings, skip
if ( empty( $savings ) ) {
continue;
}
if ( 'full' == $image_size ) {
$file_png = str_replace( basename( $filepath ), basename( $png_url ), $filepath );
if ( empty( $c_url ) || empty( $png_url ) || !file_exists( $file_png ) ) {
continue;
}
$updated = smush_update_url( $png_url, $c_url );
} elseif ( key_exists( $image_size, $sizes ) ) {
//Only if the image size is available in registered image sizes list
$file = $sizes[ $image_size ]['file'];
echo "<!--" . $file . "-->";
$info = pathinfo( $file );
if ( ! empty( $info['filename'] ) ) {
//Old PNG URL
$image_url = $base_url . '/' . $info['filename'] . '.png';
//Get the current URL for image size
$c_url = str_replace( basename( $c_url ), $file, $c_url );
$filepath = str_replace(basename($filepath), $file, $filepath );
$file_png = str_replace( basename( $filepath ), basename( $image_url ), $filepath );
if ( empty( $c_url ) || empty( $image_url ) || !file_exists( $file_png ) ) {
continue;
}
$updated = smush_update_url( $image_url, $c_url );
}
}
}
}
//Perform the replacement
function smush_update_url( $o_url, $c_url ) {
//Update In Post Content, Loop Over a set of posts to avoid the query failure for large sites
global $wpdb;
//Get existing Images with current URL
$file = str_replace(site_url(), '', $o_url );
$replace = str_replace(site_url(), '', $c_url );
$query = "SELECT ID, post_content FROM $wpdb->posts WHERE post_content LIKE '%$file%' LIMIT 10";
echo "<!--" . $query . '-->';
$rows = $wpdb->get_results( $query, ARRAY_A );
if ( empty( $rows ) || ! is_array( $rows ) ) {
return false;
}
echo "<!-- <pre>";
print_r( $rows );
echo " </pre> -->";
//Iterate over rows to update post content
foreach ( $rows as $row ) {
// replace old URLs with new URLs.
$post_content = $row["post_content"];
$post_content = str_replace( $file, $replace, $post_content );
// Update Post content
return $wpdb->update(
$wpdb->posts,
array(
'post_content' => $post_content,
),
array(
'ID' => $row['ID'],
)
);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment