| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Optml_Attachment_Replace |
| 5 |
* |
| 6 |
* @since 4.0.0 |
| 7 |
*/ |
| 8 |
class Optml_Attachment_Replace { |
| 9 |
|
| 10 |
/** |
| 11 |
* Attachment ID. |
| 12 |
* |
| 13 |
* @var int |
| 14 |
*/ |
| 15 |
private $attachment_id; |
| 16 |
|
| 17 |
/** |
| 18 |
* File. |
| 19 |
* |
| 20 |
* @var array |
| 21 |
*/ |
| 22 |
private $file; |
| 23 |
|
| 24 |
/** |
| 25 |
* Attachment. |
| 26 |
* |
| 27 |
* @var \Optml_Attachment_Model |
| 28 |
*/ |
| 29 |
private $attachment; |
| 30 |
|
| 31 |
/** |
| 32 |
* New attachment. |
| 33 |
* |
| 34 |
* @var \Optml_Attachment_Model |
| 35 |
*/ |
| 36 |
private $new_attachment; |
| 37 |
|
| 38 |
/** |
| 39 |
* Constructor. |
| 40 |
* |
| 41 |
* @param int $attachment_id Attachment ID. |
| 42 |
* @param array $file File. |
| 43 |
*/ |
| 44 |
public function __construct( $attachment_id, $file ) { |
| 45 |
$this->attachment_id = $attachment_id; |
| 46 |
$this->file = $file; |
| 47 |
$this->attachment = new Optml_Attachment_Model( $attachment_id ); |
| 48 |
|
| 49 |
if ( ! function_exists( 'WP_Filesystem' ) ) { |
| 50 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 51 |
} |
| 52 |
|
| 53 |
WP_Filesystem(); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Replace the attachment. |
| 58 |
* |
| 59 |
* @return bool|WP_Error |
| 60 |
*/ |
| 61 |
public function replace() { |
| 62 |
if ( ! file_exists( $this->file['tmp_name'] ) ) { |
| 63 |
return new WP_Error( 'file_error', __( 'Error uploading file.', 'optimole-wp' ) ); |
| 64 |
} |
| 65 |
|
| 66 |
$original_file = $this->attachment->get_source_file_path(); |
| 67 |
$old_sizes_urls = $this->attachment->get_all_image_sizes_urls(); |
| 68 |
|
| 69 |
if ( ! file_exists( $original_file ) ) { |
| 70 |
return new WP_Error( 'file_error', __( 'Original file does not exist.', 'optimole-wp' ) ); |
| 71 |
} |
| 72 |
|
| 73 |
$original_filetype = wp_check_filetype( $original_file ); |
| 74 |
$uploaded_filetype = wp_check_filetype( $this->file['name'] ); |
| 75 |
|
| 76 |
if ( $original_filetype['type'] !== $uploaded_filetype['type'] ) { |
| 77 |
return new WP_Error( 'file_error', __( 'The uploaded file type does not match the original file type.', 'optimole-wp' ) ); |
| 78 |
} |
| 79 |
|
| 80 |
global $wp_filesystem; |
| 81 |
|
| 82 |
if ( ! $wp_filesystem->move( $this->file['tmp_name'], $original_file, true ) ) { |
| 83 |
return new WP_Error( 'file_error', __( 'Could not move file.', 'optimole-wp' ) ); |
| 84 |
} |
| 85 |
|
| 86 |
$wp_filesystem->chmod( $original_file, FS_CHMOD_FILE ); |
| 87 |
|
| 88 |
$this->remove_all_image_sizes(); |
| 89 |
|
| 90 |
clean_attachment_cache( $this->attachment_id ); |
| 91 |
|
| 92 |
$metadata = wp_generate_attachment_metadata( $this->attachment_id, $original_file ); |
| 93 |
|
| 94 |
if ( isset( $metadata['sizes'] ) ) { |
| 95 |
$this->replace_image_sizes_links( $metadata['sizes'], $old_sizes_urls ); |
| 96 |
} |
| 97 |
|
| 98 |
wp_update_attachment_metadata( $this->attachment_id, $metadata ); |
| 99 |
$this->new_attachment = new Optml_Attachment_Model( $this->attachment_id ); |
| 100 |
|
| 101 |
$this->handle_scaled_images(); |
| 102 |
|
| 103 |
do_action( 'optml_attachment_replaced', $this->attachment_id ); |
| 104 |
|
| 105 |
return true; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Remove all image sizes files. |
| 110 |
* |
| 111 |
* @return void |
| 112 |
*/ |
| 113 |
private function remove_all_image_sizes() { |
| 114 |
$all_image_sizes_paths = $this->attachment->get_all_image_sizes_paths(); |
| 115 |
global $wp_filesystem; |
| 116 |
|
| 117 |
foreach ( $all_image_sizes_paths as $path ) { |
| 118 |
if ( file_exists( $path ) ) { |
| 119 |
$wp_filesystem->delete( $path ); |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Handle scaled images. |
| 126 |
* |
| 127 |
* @return bool |
| 128 |
*/ |
| 129 |
private function handle_scaled_images() { |
| 130 |
global $wp_filesystem; |
| 131 |
|
| 132 |
$old_scaled = $this->attachment->is_scaled(); |
| 133 |
$new_scaled = $this->new_attachment->is_scaled(); |
| 134 |
$replacer = new Optml_Attachment_Db_Renamer( true ); |
| 135 |
|
| 136 |
$new_file_path = $this->new_attachment->get_source_file_path(); |
| 137 |
$file = apply_filters( 'update_attached_file', $new_file_path, $this->attachment_id ); |
| 138 |
|
| 139 |
// New is scaled, but old is not scaled. We don't replace anything. |
| 140 |
if ( $old_scaled === $new_scaled || ( ! $old_scaled && $new_scaled ) ) { |
| 141 |
return true; |
| 142 |
} |
| 143 |
|
| 144 |
// Delete the old scaled version and replace scaled URLs with non-scaled URLs. |
| 145 |
if ( $old_scaled && ! $new_scaled ) { |
| 146 |
$main_file_url = $this->attachment->get_main_url(); |
| 147 |
$unscaled_file = $this->attachment->get_filename_with_ext(); |
| 148 |
$old_scaled_file = $this->attachment->get_filename_with_ext( true ); |
| 149 |
$old_scaled_url = str_replace( $unscaled_file, $old_scaled_file, $main_file_url ); |
| 150 |
|
| 151 |
$replacer->replace( $old_scaled_url, $main_file_url ); |
| 152 |
|
| 153 |
// replace the old year/month/item-scaled.ext with the new year/month/item.ext |
| 154 |
$scaled_path = str_replace( $unscaled_file, $old_scaled_file, $this->attachment->get_source_file_path() ); |
| 155 |
if ( file_exists( $scaled_path ) ) { |
| 156 |
$wp_filesystem->delete( $scaled_path ); |
| 157 |
} |
| 158 |
|
| 159 |
update_attached_file( $this->attachment_id, sprintf( '%s/%s', $this->attachment->get_metadata_prefix_path(), $unscaled_file ) ); |
| 160 |
} |
| 161 |
|
| 162 |
return true; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Replace image sizes links. |
| 167 |
* |
| 168 |
* @param array $new_sizes New sizes. |
| 169 |
* @param array $old_sizes_urls Old sizes URLs. |
| 170 |
* |
| 171 |
* @return void |
| 172 |
*/ |
| 173 |
private function replace_image_sizes_links( $new_sizes, $old_sizes_urls ) { |
| 174 |
$replacer = new Optml_Attachment_Db_Renamer( true ); |
| 175 |
|
| 176 |
foreach ( $old_sizes_urls as $size => $old_url ) { |
| 177 |
// If the size is not in the new sizes, we need to use the original URL. |
| 178 |
if ( ! isset( $new_sizes[ $size ], $new_sizes[ $size ]['file'] ) ) { |
| 179 |
$replacer->replace( $old_url, $this->attachment->get_main_url() ); |
| 180 |
continue; |
| 181 |
} |
| 182 |
|
| 183 |
// If the size is in the new sizes, we need to use the new URL. |
| 184 |
$new_url = str_replace( $this->attachment->get_filename_with_ext(), $new_sizes[ $size ]['file'], $this->attachment->get_main_url() ); |
| 185 |
$replacer->replace( $old_url, $new_url ); |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|