Created
November 28, 2024 23:16
-
-
Save wpmudev-sls/dea63beb35c0af4474f2e11349564ba1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Smush PNG to JPG URL Fix | |
* Description: Fixes post content URLs for images converted from PNG to JPG but still serve PNG url. | |
* Jira: SLS-6576 | |
* Author: Abdoljabbar Bakhshande | |
* Author URI: https://wpmudev.com | |
* License: GPLv2 or later | |
* | |
* @package WordPress | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; // Exit if accessed directly. | |
} | |
add_action( 'init', function () { | |
if ( ! class_exists('\Smush\Core\Media\Media_Item_Cache') ) { | |
return; | |
} | |
function wdev_replace_url_in_content( $old_url, $new_url ) { | |
global $wpdb; | |
$wild = '%'; | |
$old_url_like = $wild . $wpdb->esc_like( $old_url ) . $wild; | |
$query = $wpdb->prepare( "SELECT ID, post_content FROM $wpdb->posts WHERE post_content LIKE %s", $old_url_like ); | |
$rows = $wpdb->get_results( $query ); | |
if ( empty( $rows ) || ! is_array( $rows ) ) { | |
return true; | |
} | |
$update_count = 0; | |
foreach ( $rows as $row ) { | |
$post_content = $row->post_content; | |
$original_content = $post_content; | |
$post_content = str_replace( $old_url, $new_url, $post_content ); | |
if ($original_content === $post_content) { | |
continue; | |
} | |
$updated = $wpdb->update( | |
$wpdb->posts, | |
array( 'post_content' => $post_content ), | |
array( 'ID' => $row->ID ) | |
); | |
if ( $updated ) { | |
$update_count++; | |
} | |
clean_post_cache( $row->ID ); | |
} | |
return $update_count === count( $rows ); | |
} | |
add_filter( 'wp_smush_scan_library_slice_handle_attachment', 'smush_png2jpg_fix_urls_during_scan', 10, 2 ); | |
function smush_png2jpg_fix_urls_during_scan( $slice_data, $attachment_id ) { | |
// Check if PNG to JPG conversion is enabled | |
$settings = \Smush\Core\Settings::get_instance(); | |
if ( ! $settings->get( 'png_to_jpg' ) ) { | |
return $slice_data; | |
} | |
// Check for PNG to JPG conversion savings | |
$png_jpg_savings = get_post_meta( $attachment_id, 'wp-smush-pngjpg_savings', true ); | |
if ( ! $png_jpg_savings ) { | |
return $slice_data; | |
} | |
try { | |
$media_item = \Smush\Core\Media\Media_Item_Cache::get_instance()->get( $attachment_id ); | |
if ( ! $media_item ) { | |
return $slice_data; | |
} | |
// Get converted PNG files using the Png2Jpg_Optimization class | |
$png2jpg_optimization = new \Smush\Core\Png2Jpg\Png2Jpg_Optimization($media_item); | |
$converted_png_files = $png2jpg_optimization->get_converted_png_files(); | |
if ( ! $converted_png_files || ! is_array( $converted_png_files ) ) { | |
return $slice_data; | |
} | |
$jpg_size_urls = $media_item->get_size_urls(); | |
foreach ( $converted_png_files as $size_key => $png_path ) { | |
if ( ! isset( $jpg_size_urls[ $size_key ] ) ) { | |
continue; | |
} | |
$jpg_url = $jpg_size_urls[ $size_key ]; | |
$upload_dir = wp_upload_dir(); | |
$jpg_relative_path = str_replace($upload_dir['baseurl'] . '/', '', $jpg_url); | |
wdev_replace_url_in_content( $png_path, $jpg_relative_path ); | |
} | |
} catch ( Exception $e ) { | |
// Silent fail | |
} | |
return $slice_data; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment