$attachment_id, 'post_mime_type' => $checked['type'], ) ); clean_post_cache( $attachment_id ); return array( 'url' => (string) wp_get_attachment_url( $attachment_id ), 'thumb' => (string) wp_get_attachment_image_url( $attachment_id, 'thumbnail' ), 'width' => (int) ( $fresh['width'] ?? 0 ), 'height' => (int) ( $fresh['height'] ?? 0 ), 'sizes' => array_keys( (array) ( $fresh['sizes'] ?? array() ) ), 'bytes' => file_exists( $target ) ? (int) filesize( $target ) : 0, ); } /** * Is this something WordPress would have accepted as an upload? * * @param array $upload One entry of $_FILES. * @return array{ext:string,type:string}|WP_Error */ private static function check_upload( array $upload ) { if ( ! empty( $upload['error'] ) || empty( $upload['tmp_name'] ) || ! is_uploaded_file( $upload['tmp_name'] ) ) { return new WP_Error( 'atr_replace_upload', __( 'The file did not arrive in one piece.', 'ajax-thumbnail-rebuild' ) ); } $name = sanitize_file_name( (string) ( $upload['name'] ?? '' ) ); $checked = wp_check_filetype_and_ext( $upload['tmp_name'], $name ); $ext = strtolower( (string) ( $checked['ext'] ?? '' ) ); $type = (string) ( $checked['type'] ?? '' ); if ( '' === $ext || '' === $type || ! in_array( $type, get_allowed_mime_types(), true ) ) { return new WP_Error( 'atr_replace_filetype', __( 'That kind of file cannot be uploaded here.', 'ajax-thumbnail-rebuild' ) ); } if ( 0 !== strpos( $type, 'image/' ) || ! file_is_displayable_image( $upload['tmp_name'] ) ) { return new WP_Error( 'atr_replace_not_image', __( 'That file is not an image WordPress can read.', 'ajax-thumbnail-rebuild' ) ); } return array( 'ext' => $ext, 'type' => $type, ); } /** * Remove what the old picture left behind, so nothing of it survives the * replacement under a name the new sizes happen not to use. * * @param int $attachment_id Attachment. * @param array $metadata Its metadata as it stands. * @param string $attached The file it currently serves. * @param string $target Where the new file is about to go. */ private static function delete_old_files( int $attachment_id, array $metadata, string $attached, string $target ): void { // The AVIF and WebP copies first: they are found from the metadata that is about to change. ATR_Copies::delete_for_attachment( $attachment_id ); $directory = trailingslashit( dirname( $attached ) ); $paths = array( $attached ); foreach ( (array) ( $metadata['sizes'] ?? array() ) as $size ) { if ( ! empty( $size['file'] ) ) { $paths[] = $directory . $size['file']; } } foreach ( array_unique( $paths ) as $path ) { // Not the file we are about to write over: that one is simply replaced. if ( $path !== $target && file_exists( $path ) ) { wp_delete_file( $path ); } } } /** * @param string $from Uploaded temporary file. * @param string $to Where it belongs. */ private static function move( string $from, string $to ): bool { /* The old file goes first rather than being written over. Writing over it needs permission on the file itself, which is not a given: anything put there over FTP, or by a WP-CLI run as another user, belongs to somebody else. Removing it needs permission on the folder, which is the one this already checked. */ if ( file_exists( $to ) ) { wp_delete_file( $to ); } if ( ! move_uploaded_file( $from, $to ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions return false; } $permissions = defined( 'FS_CHMOD_FILE' ) ? FS_CHMOD_FILE : ( fileperms( dirname( $to ) ) & 0000666 ); if ( $permissions ) { chmod( $to, $permissions ); // phpcs:ignore WordPress.WP.AlternativeFunctions } clearstatcache( true, $to ); return true; } }