Skip to content

Commit

Permalink
Improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
av3nger committed Dec 13, 2024
1 parent 2b739bc commit 667bdb6
Showing 1 changed file with 38 additions and 10 deletions.
48 changes: 38 additions & 10 deletions app/class-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,13 @@ public function upload_image( $metadata, int $attachment_id, string $action = ''
if ( ! isset( $metadata['file'] ) ) {
update_post_meta( $attachment_id, '_cloudflare_image_skip', true );
do_action( 'cf_images_error', 404, __( 'Media file not found', 'cf-images' ) );
/* translators: %d: attachment ID */
do_action( 'cf_images_log', sprintf( esc_html__( 'Unable to offload image. Media file not found. Attachment ID: %d.', 'cf-images' ), absint( $attachment_id ) ) );
do_action(
'cf_images_log',
sprintf( /* translators: %d: attachment ID */
esc_html__( 'Unable to offload image. Media file not found. Attachment ID: %d.', 'cf-images' ),
absint( $attachment_id )
)
);
return $metadata;
}

Expand All @@ -414,8 +419,13 @@ public function upload_image( $metadata, int $attachment_id, string $action = ''
if ( ! wp_attachment_is_image( $attachment_id ) || false !== strpos( $mime, 'image/svg' ) ) {
update_post_meta( $attachment_id, '_cloudflare_image_skip', true );
do_action( 'cf_images_error', 415, __( 'Unsupported media type', 'cf-images' ) );
/* translators: %d: attachment ID */
do_action( 'cf_images_log', sprintf( esc_html__( 'Unable to offload image. Unsupported media type. Attachment ID: %d.', 'cf-images' ), absint( $attachment_id ) ) );
do_action(
'cf_images_log',
sprintf( /* translators: %d: attachment ID */
esc_html__( 'Unable to offload image. Unsupported media type. Attachment ID: %d.', 'cf-images' ),
absint( $attachment_id )
)
);
return $metadata;
}

Expand Down Expand Up @@ -466,8 +476,14 @@ public function upload_image( $metadata, int $attachment_id, string $action = ''
}
} catch ( Exception $e ) {
do_action( 'cf_images_error', $e->getCode(), $e->getMessage() );
/* translators: %1$d: attachment ID, %2$s: error message */
do_action( 'cf_images_log', sprintf( esc_html__( 'Unable to offload image. Attachment ID: %1$d. Error: %2$s', 'cf-images' ), absint( $attachment_id ), esc_html( $e->getMessage() ) ) );
do_action(
'cf_images_log',
sprintf( /* translators: %1$d: attachment ID, %2$s: error message */
esc_html__( 'Unable to offload image. Attachment ID: %1$d. Error: %2$s', 'cf-images' ),
absint( $attachment_id ),
esc_html( $e->getMessage() )
)
);
}

return $metadata;
Expand Down Expand Up @@ -502,8 +518,14 @@ public function remove_from_cloudflare( int $post_id ) {
}
} catch ( Exception $e ) {
do_action( 'cf_images_error', $e->getCode(), $e->getMessage() );
/* translators: %1$d: attachment ID, %2$s: error message */
do_action( 'cf_images_log', sprintf( esc_html__( 'Unable to remove image from Cloudflare. Attachment ID: %1$d. Error: %2$s', 'cf-images' ), absint( $post_id ), esc_html( $e->getMessage() ) ) );
do_action(
'cf_images_log',
sprintf( /* translators: %1$d: attachment ID, %2$s: error message */
esc_html__( 'Unable to remove image from Cloudflare. Attachment ID: %1$d. Error: %2$s', 'cf-images' ),
absint( $post_id ),
esc_html( $e->getMessage() )
)
);
}
}

Expand Down Expand Up @@ -680,8 +702,14 @@ public function ajax_restore_image( $attachment_id = null ) {
wp_create_image_subsizes( $original, $attachment_id );
} catch ( Exception $e ) {
do_action( 'cf_images_error', $e->getCode(), $e->getMessage() );
/* translators: %1$d: attachment ID, %2$s: error message */
do_action( 'cf_images_log', sprintf( esc_html__( 'Unable to restore image. Attachment ID: %1$d. Error: %2$s', 'cf-images' ), absint( $attachment_id ), esc_html( $e->getMessage() ) ) );
do_action(
'cf_images_log',
sprintf( /* translators: %1$d: attachment ID, %2$s: error message */
esc_html__( 'Unable to restore image. Attachment ID: %1$d. Error: %2$s', 'cf-images' ),
absint( $attachment_id ),
esc_html( $e->getMessage() )
)
);
}

delete_post_meta( $attachment_id, '_cloudflare_image_offloaded' );
Expand Down

0 comments on commit 667bdb6

Please sign in to comment.