Skip to content

Commit

Permalink
Handle postmeta updates when auto delete is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
arunshenoy99 committed Dec 27, 2024
1 parent 7f05382 commit b3c5c0e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 14 deletions.
46 changes: 34 additions & 12 deletions includes/Images/ImageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,23 +168,42 @@ public function replace_original_with_webp( $media_id_or_path, $webp_file_path )
$upload_dir = wp_upload_dir();
$webp_file_url = trailingslashit( $upload_dir['url'] ) . wp_basename( $webp_file_path );

// Determine if media_id_or_path is a media ID or file path
if ( is_numeric( $media_id_or_path ) ) {
$original_file_path = get_attached_file( $media_id_or_path );
} elseif ( is_string( $media_id_or_path ) && file_exists( $media_id_or_path ) ) {
$original_file_path = $media_id_or_path;
} else {
// Ensure the WebP file exists
if ( ! file_exists( $webp_file_path ) || filesize( $webp_file_path ) === 0 ) {
return new \WP_Error(
'nfd_performance_error',
__( 'Invalid media ID or file path provided.', 'wp-module-performance' )
__( 'WebP file is missing or empty.', 'wp-module-performance' )
);
}

// Ensure the WebP file exists
if ( ! file_exists( $webp_file_path ) || filesize( $webp_file_path ) === 0 ) {
// Determine if $media_id_or_path is a Media ID or file path
if ( is_numeric( $media_id_or_path ) && (int) $media_id_or_path > 0 ) {
// Media ID provided
$original_file_path = get_attached_file( $media_id_or_path );
if ( ! $original_file_path ) {
return new \WP_Error(
'nfd_performance_error',
__( 'Invalid Media ID provided.', 'wp-module-performance' )
);
}
} elseif ( is_string( $media_id_or_path ) && file_exists( $media_id_or_path ) ) {
// File path provided
$original_file_path = $media_id_or_path;

// Store metadata in a transient for later use
$transient_key = 'nfd_webp_metadata_' . md5( $webp_file_path );
set_transient(
$transient_key,
array(
'webp_file_path' => $webp_file_path,
'original_file_path' => $original_file_path,
),
HOUR_IN_SECONDS
);
} else {
return new \WP_Error(
'nfd_performance_error',
__( 'WebP file is missing or empty.', 'wp-module-performance' )
__( 'Invalid Media ID or file path provided.', 'wp-module-performance' )
);
}

Expand All @@ -196,11 +215,14 @@ public function replace_original_with_webp( $media_id_or_path, $webp_file_path )
);
}

// Update the attachment metadata if a media ID was provided
if ( is_numeric( $media_id_or_path ) ) {
// If Media ID is available, update its metadata
if ( is_numeric( $media_id_or_path ) && $media_id_or_path > 0 ) {
update_attached_file( $media_id_or_path, $webp_file_path );

// Regenerate and update attachment metadata
require_once ABSPATH . 'wp-admin/includes/image.php';
$metadata = wp_generate_attachment_metadata( $media_id_or_path, $webp_file_path );

if ( is_wp_error( $metadata ) || empty( $metadata ) ) {
return new \WP_Error(
'nfd_performance_error',
Expand Down
26 changes: 24 additions & 2 deletions includes/Images/ImageUploadListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ public function __construct( $delete_original = false ) {
*/
private function register_hooks() {
add_filter( 'wp_handle_upload', array( $this, 'handle_media_upload' ), 10, 2 );
add_action( 'add_attachment', array( $this, 'process_attachment_metadata' ) );
}

/**
* Intercepts media uploads and optimizes images via the Cloudflare Worker.
*
* @param array $upload The upload array with file data.
* @return array|WP_Error The modified upload array or WP_Error on failure.
* @return array The modified upload array or the original array on failure.
*/
public function handle_media_upload( $upload ) {
$optimized_image_path = $this->image_service->optimize_image( $upload['url'], $upload['file'] );
Expand All @@ -52,11 +53,32 @@ public function handle_media_upload( $upload ) {
}

if ( $this->delete_original ) {
$upload = $this->image_service->replace_original_with_webp( $upload['file'], $optimized_image_path );
$result = $this->image_service->replace_original_with_webp( $upload['file'], $optimized_image_path );
if ( is_wp_error( $result ) ) {
return $upload;
}

return $result;
} else {
$this->image_service->register_webp_as_new_media( $optimized_image_path );
}

return $upload;
}

/**
* Processes attachment metadata after the attachment is created.
*
* @param int $attachment_id The attachment ID.
*/
public function process_attachment_metadata( $attachment_id ) {
$attachment_file = get_attached_file( $attachment_id );
$transient_key = 'nfd_webp_metadata_' . md5( $attachment_file );
$metadata = get_transient( $transient_key );

if ( $metadata ) {
// Update postmeta
update_post_meta( $attachment_id, '_nfd_performance_image_optimized', 1 );
}
}
}

0 comments on commit b3c5c0e

Please sign in to comment.