| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Attachment rename class. |
| 5 |
*/ |
| 6 |
class Optml_Attachment_Rename { |
| 7 |
/** |
| 8 |
* The attachment ID. |
| 9 |
* |
| 10 |
* @var int |
| 11 |
*/ |
| 12 |
private $attachment_id; |
| 13 |
|
| 14 |
/** |
| 15 |
* The new filename. |
| 16 |
* |
| 17 |
* @var string |
| 18 |
*/ |
| 19 |
private $new_filename; |
| 20 |
|
| 21 |
/** |
| 22 |
* The attachment model. |
| 23 |
* |
| 24 |
* @var Optml_Attachment_Model |
| 25 |
*/ |
| 26 |
private $attachment; |
| 27 |
|
| 28 |
/** |
| 29 |
* Constructor. |
| 30 |
* |
| 31 |
* @param int $attachment_id Attachment ID. |
| 32 |
* @param string $new_filename New filename. |
| 33 |
* @return void |
| 34 |
*/ |
| 35 |
public function __construct( int $attachment_id, string $new_filename ) { |
| 36 |
$this->attachment_id = $attachment_id; |
| 37 |
$this->attachment = new Optml_Attachment_Model( $attachment_id ); |
| 38 |
$this->new_filename = $new_filename; |
| 39 |
|
| 40 |
if ( ! function_exists( 'WP_Filesystem' ) ) { |
| 41 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 42 |
} |
| 43 |
|
| 44 |
WP_Filesystem(); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Rename the attachment |
| 49 |
* |
| 50 |
* @return bool|WP_Error |
| 51 |
*/ |
| 52 |
public function rename() { |
| 53 |
if ( empty( $this->new_filename ) || sanitize_file_name( $this->new_filename ) === $this->attachment->get_filename_no_ext() ) { |
| 54 |
return true; |
| 55 |
} |
| 56 |
|
| 57 |
$extension = $this->attachment->get_extension(); |
| 58 |
|
| 59 |
$base_dir = trailingslashit( $this->attachment->get_dir_path() ); |
| 60 |
$file_path = $this->attachment->get_source_file_path(); |
| 61 |
|
| 62 |
$new_file_with_ext = sprintf( '%s.%s', $this->new_filename, $extension ); |
| 63 |
$new_unique_filename = wp_unique_filename( $base_dir, $new_file_with_ext ); |
| 64 |
$new_file_path = $base_dir . $new_unique_filename; |
| 65 |
|
| 66 |
global $wp_filesystem; |
| 67 |
|
| 68 |
// Bail if original file doesn't exist. |
| 69 |
if ( ! $wp_filesystem->exists( $file_path ) ) { |
| 70 |
return new WP_Error( 'optml_attachment_file_not_found', __( 'Error renaming file.', 'optimole-wp' ) ); |
| 71 |
} |
| 72 |
|
| 73 |
// Rename the file (move) - moves the original, not the scaled image. |
| 74 |
$moved = $wp_filesystem->move( $file_path, $new_file_path ); |
| 75 |
if ( ! $moved ) { |
| 76 |
return new WP_Error( 'optml_attachment_rename_failed', __( 'Error renaming file.', 'optimole-wp' ) ); |
| 77 |
} |
| 78 |
|
| 79 |
$wp_filesystem->chmod( $new_file_path, FS_CHMOD_FILE ); |
| 80 |
|
| 81 |
// Move the scaled image if it exists. |
| 82 |
if ( $this->attachment->is_scaled() ) { |
| 83 |
$new_unique_filename_no_ext = pathinfo( $new_unique_filename, PATHINFO_FILENAME ); |
| 84 |
|
| 85 |
$scaled_old_file_path = sprintf( '%s/%s-scaled.%s', $this->attachment->get_dir_path(), $this->attachment->get_filename_no_ext(), $extension ); |
| 86 |
$scaled_new_file_with_ext = sprintf( '%s-scaled.%s', $new_unique_filename_no_ext, $extension ); |
| 87 |
|
| 88 |
$new_scaled_file_path = $base_dir . $scaled_new_file_with_ext; |
| 89 |
// Move the scaled image. We also override any leftover scaled files. |
| 90 |
$wp_filesystem->move( $scaled_old_file_path, $new_scaled_file_path, true ); |
| 91 |
} |
| 92 |
|
| 93 |
// Update attachment metadata |
| 94 |
$metadata_update = $this->update_attachment_metadata( $new_file_path ); |
| 95 |
|
| 96 |
if ( $metadata_update === false ) { |
| 97 |
return new WP_Error( 'optml_attachment_metadata_update_failed', __( 'Error renaming file.', 'optimole-wp' ) ); |
| 98 |
} |
| 99 |
|
| 100 |
try { |
| 101 |
$skip_sizes = ! $this->attachment->is_image(); |
| 102 |
|
| 103 |
$replacer = new Optml_Attachment_Db_Renamer( $skip_sizes ); |
| 104 |
$old_url = $this->attachment->get_main_url(); |
| 105 |
$new_url = $this->get_new_url( $new_unique_filename ); |
| 106 |
|
| 107 |
$count = $replacer->replace( $old_url, $new_url ); |
| 108 |
|
| 109 |
if ( $count > 0 ) { |
| 110 |
/** |
| 111 |
* Action triggered after the attachment file is renamed. |
| 112 |
* |
| 113 |
* @param int $attachment_id Attachment ID. |
| 114 |
* @param string $old_url Old attachment URL. |
| 115 |
* @param string $new_url New attachment URL. |
| 116 |
*/ |
| 117 |
do_action( 'optml_after_attachment_url_replace', $this->attachment_id, $old_url, $new_url ); |
| 118 |
} |
| 119 |
} catch ( Exception $e ) { |
| 120 |
return new WP_Error( 'optml_attachment_url_replace_failed', __( 'Error renaming file.', 'optimole-wp' ) ); |
| 121 |
} |
| 122 |
|
| 123 |
do_action( 'optml_attachment_renamed', $this->attachment_id ); |
| 124 |
|
| 125 |
return true; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Update attachment metadata. |
| 130 |
* |
| 131 |
* @param string $new_path New path. |
| 132 |
* @return bool |
| 133 |
*/ |
| 134 |
private function update_attachment_metadata( $new_path ) { |
| 135 |
global $wp_filesystem; |
| 136 |
|
| 137 |
$new_file_name_no_ext = pathinfo( $new_path, PATHINFO_FILENAME ); |
| 138 |
$extension = $this->attachment->get_extension(); |
| 139 |
|
| 140 |
if ( $this->attachment->is_scaled() ) { |
| 141 |
$new_path = sprintf( '%s/%s-scaled.%s', $this->attachment->get_metadata_prefix_path(), $new_file_name_no_ext, $extension ); |
| 142 |
} |
| 143 |
|
| 144 |
$attached_update = update_attached_file( $this->attachment_id, $new_path ); |
| 145 |
|
| 146 |
if ( ! $attached_update ) { |
| 147 |
return false; |
| 148 |
} |
| 149 |
|
| 150 |
// We should bail for non-image attachments here. |
| 151 |
// No need for additional metadata update. |
| 152 |
if ( ! $this->attachment->is_image() ) { |
| 153 |
return true; |
| 154 |
} |
| 155 |
|
| 156 |
// Get current attachment metadata |
| 157 |
$metadata = $this->attachment->get_attachment_metadata(); |
| 158 |
|
| 159 |
if ( empty( $metadata ) ) { |
| 160 |
return false; |
| 161 |
} |
| 162 |
|
| 163 |
// Update file path in metadata |
| 164 |
$original_image = sprintf( '%s.%s', $new_file_name_no_ext, $extension ); |
| 165 |
$meta_file = $original_image; |
| 166 |
|
| 167 |
if ( $this->attachment->is_scaled() ) { |
| 168 |
$meta_file = sprintf( '%s-scaled.%s', $new_file_name_no_ext, $extension ); |
| 169 |
$metadata['original_image'] = $original_image; |
| 170 |
} |
| 171 |
|
| 172 |
if ( isset( $metadata['file'] ) ) { |
| 173 |
$metadata['file'] = sprintf( '%s/%s', $this->attachment->get_metadata_prefix_path(), $meta_file ); |
| 174 |
} |
| 175 |
|
| 176 |
// Update image sizes if they exist |
| 177 |
if ( isset( $metadata['sizes'] ) && is_array( $metadata['sizes'] ) ) { |
| 178 |
$already_moved_paths = []; |
| 179 |
|
| 180 |
foreach ( $metadata['sizes'] as $size => $size_data ) { |
| 181 |
if ( ! isset( $size_data['file'] ) ) { |
| 182 |
continue; |
| 183 |
} |
| 184 |
$size_suffix = $size_data['width'] . 'x' . $size_data['height']; |
| 185 |
$new_size_file = sprintf( '%s-%s.%s', $new_file_name_no_ext, $size_suffix, $extension ); |
| 186 |
|
| 187 |
$old_size_file_path = sprintf( '%s/%s', $this->attachment->get_dir_path(), $size_data['file'] ); |
| 188 |
$new_size_file_path = sprintf( '%s/%s', $this->attachment->get_dir_path(), $new_size_file ); |
| 189 |
|
| 190 |
$move = $wp_filesystem->move( $old_size_file_path, $new_size_file_path ); |
| 191 |
|
| 192 |
if ( $move || in_array( $old_size_file_path, $already_moved_paths, true ) ) { |
| 193 |
$already_moved_paths[] = $old_size_file_path; |
| 194 |
$metadata['sizes'][ $size ]['file'] = $new_size_file; |
| 195 |
$already_moved_paths = array_unique( $already_moved_paths ); |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
$metadata_update = wp_update_attachment_metadata( $this->attachment_id, $metadata ); |
| 201 |
|
| 202 |
if ( ! $metadata_update ) { |
| 203 |
return false; |
| 204 |
} |
| 205 |
|
| 206 |
return true; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Get the new main attached file URL. |
| 211 |
* |
| 212 |
* @return string |
| 213 |
*/ |
| 214 |
private function get_new_url( $filename ) { |
| 215 |
$url = $this->attachment->get_main_url(); |
| 216 |
|
| 217 |
return str_replace( |
| 218 |
basename( $url ), |
| 219 |
$filename, |
| 220 |
$url |
| 221 |
); |
| 222 |
} |
| 223 |
} |
| 224 |
|