set_image_backup_path( $image_size, $backup_path ); $meta->save(); return $backup_path; } /** * Looks for registered backups and remove all files found. * Also, wipes removed files from image meta. * * @param int[] $image_ids Array of attachment ids. * @return void */ public static function remove_many( array $image_ids ): void { foreach ( $image_ids as $image_id ) { self::remove( $image_id ); } } /** * Removes one or all backups for a specific image. * Also, wipes removed files from image meta. * * @param int $image_id Attachment id. * @param string|null $image_size Image size (e.g. 'full', 'thumbnail', etc.). All backups will be removed if no size provided. * * @return bool Returns true if backups were removed successfully, false otherwise. */ public static function remove( int $image_id, ?string $image_size = null ): bool { $meta = new Image_Meta( $image_id ); $backups = $meta->get_image_backup_paths(); if ( empty( $backups ) ) { return false; } if ( $image_size ) { if ( ! key_exists( $image_size, $backups ) ) { return false; } self::delete_backup_file( $image_id, $image_size, $backups[ $image_size ] ); $meta->remove_image_backup_path( $image_size ); $meta->save(); return true; } foreach ( $backups as $image_size => $backup_path ) { self::delete_backup_file( $image_id, $image_size, $backup_path ); $meta->remove_image_backup_path( $image_size ); } $meta->save(); return true; } private static function delete_backup_file( int $image_id, string $image_size, string $backup_path ): void { $resolved_path = Image_Backup_Path_Validator::resolve( $backup_path ); if ( null === $resolved_path ) { Logger::warn( "Skipped removing invalid backup path for image {$image_id} and size {$image_size}" ); return; } try { File_System::delete( $resolved_path, false, 'f' ); } catch ( File_System_Operation_Error $fsoe ) { Logger::error( "Error while removing a backup for image {$image_id} and size {$image_size}" ); } } }