From 67309aa6947f5da2bc6909f22e98642bc668eabc Mon Sep 17 00:00:00 2001 From: Mykyta Synelnikov Date: Mon, 19 Feb 2024 17:07:18 +0200 Subject: [PATCH] - fixed conflict with WebP Uploads; --- includes/core/class-uploader.php | 85 ++++++++++++++++++-------------- 1 file changed, 48 insertions(+), 37 deletions(-) diff --git a/includes/core/class-uploader.php b/includes/core/class-uploader.php index 5a66d74d..19f6e28f 100644 --- a/includes/core/class-uploader.php +++ b/includes/core/class-uploader.php @@ -968,28 +968,29 @@ if ( ! class_exists( 'um\core\Uploader' ) ) { /** * Profile photo image process * - * @param array $response + * @param array $response * @param string $image_path * @param string $src * @param string $key - * @param integer $user_id + * @param int $user_id * @param string $coord - * @param array $crop + * @param array $crop * * @since 2.0.22 * * @return array */ public function profile_photo( $response, $image_path, $src, $key, $user_id, $coord, $crop ) { - $sizes = UM()->options()->get( 'photo_thumb_sizes' ); - + $sizes = UM()->options()->get( 'photo_thumb_sizes' ); $quality = UM()->options()->get( 'image_compression' ); $image = wp_get_image_editor( $image_path ); // Return an implementation that extends WP_Image_Editor $temp_image_path = $image_path; - //refresh image_path to make temporary image permanently after upload - $image_path = pathinfo( $image_path, PATHINFO_DIRNAME ) . DIRECTORY_SEPARATOR . $key . '.' . pathinfo( $image_path, PATHINFO_EXTENSION ); + + // Refresh image_path to make temporary image permanently after upload + $photo_ext = pathinfo( $image_path, PATHINFO_EXTENSION ); + $image_path = pathinfo( $image_path, PATHINFO_DIRNAME ) . DIRECTORY_SEPARATOR . $key . '.' . $photo_ext; if ( ! is_wp_error( $image ) ) { $src_x = $crop[0]; @@ -1004,12 +1005,16 @@ if ( ! class_exists( 'um\core\Uploader' ) ) { $image->resize( $max_w, $src_h ); } - $image->save( $image_path ); + $save_result = $image->save( $image_path ); + + if ( is_wp_error( $save_result ) ) { + // translators: %s is the file src. + wp_send_json_error( sprintf( __( 'Unable to crop image file: %s', 'ultimate-member' ), $src ) ); + } $image->set_quality( $quality ); $sizes_array = array(); - foreach ( $sizes as $size ) { $sizes_array[] = array( 'width' => $size ); } @@ -1020,50 +1025,49 @@ if ( ! class_exists( 'um\core\Uploader' ) ) { unlink( $temp_image_path ); - $src = str_replace( '/' . $key . '_temp.', '/' . $key . '.', $src ); + $basename = $key . '_temp.' . $photo_ext; + $src = str_replace( '/' . $basename, '/' . $save_result['file'], $src ); - $response['image']['source_url'] = $src; - $response['image']['source_path'] = $image_path; - $response['image']['filename'] = wp_basename( $image_path ); + $response['image']['source_url'] = $src; + $response['image']['source_path'] = $save_result['path']; + $response['image']['filename'] = $save_result['file']; - update_user_meta( $this->user_id, $key, wp_basename( wp_basename( $image_path ) ) ); + update_user_meta( $this->user_id, $key, $save_result['file'] ); delete_user_meta( $this->user_id, "{$key}_metadata_temp" ); } else { - wp_send_json_error( esc_js( __( "Unable to crop image file: {$src}", 'ultimate-member' ) ) ); + // translators: %s is the file src. + wp_send_json_error( sprintf( __( 'Unable to crop image file: %s', 'ultimate-member' ), $src ) ); } return $response; } - /** * Cover photo image process * * @param string $src - * @param integer $user_id + * @param int $user_id * @param string $coord - * @param array $crop - * @param array $response + * @param array $crop + * @param array $response * * @since 2.0.22 * * @return array */ public function cover_photo( $response, $image_path, $src, $key, $user_id, $coord, $crop ) { - - $sizes = UM()->options()->get( 'cover_thumb_sizes' ); - + $sizes = UM()->options()->get( 'cover_thumb_sizes' ); $quality = UM()->options()->get( 'image_compression' ); $image = wp_get_image_editor( $image_path ); // Return an implementation that extends WP_Image_Editor $temp_image_path = $image_path; - //refresh image_path to make temporary image permanently after upload - $image_path = pathinfo( $image_path, PATHINFO_DIRNAME ) . DIRECTORY_SEPARATOR . $key . '.' . pathinfo( $image_path, PATHINFO_EXTENSION ); + // Refresh image_path to make temporary image permanently after upload + $photo_ext = pathinfo( $image_path, PATHINFO_EXTENSION ); + $image_path = pathinfo( $image_path, PATHINFO_DIRNAME ) . DIRECTORY_SEPARATOR . $key . '.' . $photo_ext; if ( ! is_wp_error( $image ) ) { - $src_x = $crop[0]; $src_y = $crop[1]; $src_w = $crop[2]; @@ -1076,7 +1080,12 @@ if ( ! class_exists( 'um\core\Uploader' ) ) { $image->resize( $max_w, $src_h ); } - $image->save( $image_path ); + $save_result = $image->save( $image_path ); + + if ( is_wp_error( $save_result ) ) { + // translators: %s is the file src. + wp_send_json_error( sprintf( __( 'Unable to crop image file: %s', 'ultimate-member' ), $src ) ); + } $image->set_quality( $quality ); @@ -1090,7 +1099,7 @@ if ( ! class_exists( 'um\core\Uploader' ) ) { // change filenames of resized images foreach ( $resize as $row ) { - $new_filename = str_replace( "x{$row['height']}" , '', $row['file'] ); + $new_filename = str_replace( "x{$row['height']}", '', $row['file'] ); $old_filename = $row['file']; rename( dirname( $image_path ) . DIRECTORY_SEPARATOR . $old_filename, dirname( $image_path ) . DIRECTORY_SEPARATOR . $new_filename ); @@ -1098,16 +1107,18 @@ if ( ! class_exists( 'um\core\Uploader' ) ) { unlink( $temp_image_path ); - $src = str_replace( '/' . $key . '_temp.', '/' . $key . '.', $src ); + $basename = $key . '_temp.' . $photo_ext; + $src = str_replace( '/' . $basename, '/' . $save_result['file'], $src ); - $response['image']['source_url'] = $src; - $response['image']['source_path'] = $image_path; - $response['image']['filename'] = wp_basename( $image_path ); + $response['image']['source_url'] = $src; + $response['image']['source_path'] = $save_result['path']; + $response['image']['filename'] = $save_result['file']; - update_user_meta( $this->user_id, $key, wp_basename( wp_basename( $image_path ) ) ); + update_user_meta( $this->user_id, $key, $save_result['file'] ); delete_user_meta( $this->user_id, "{$key}_metadata_temp" ); } else { - wp_send_json_error( esc_js( __( "Unable to crop image file: {$src}", 'ultimate-member' ) ) ); + // translators: %s is the file src. + wp_send_json_error( sprintf( __( 'Unable to crop image file: %s', 'ultimate-member' ), $src ) ); } return $response; @@ -1195,15 +1206,15 @@ if ( ! class_exists( 'um\core\Uploader' ) ) { $response = array( 'image' => array( - 'source_url' => $src, - 'source_path' => $image_path, - 'filename' => wp_basename( $image_path ), + 'source_url' => $src, + 'source_path' => $image_path, + 'filename' => wp_basename( $image_path ), ), ); $response = apply_filters( "um_upload_image_process__{$key}", $response, $image_path, $src, $key, $user_id, $coord, $crop ); - if ( ! in_array( $key, array( 'profile_photo', 'cover_photo' ) ) ) { + if ( ! in_array( $key, array( 'profile_photo', 'cover_photo' ), true ) ) { $response = apply_filters( 'um_upload_stream_image_process', $response, $image_path, $src, $key, $user_id, $coord, $crop ); }