Last active
July 18, 2024 23:22
-
-
Save fribibb/e82316b8df6c5843d6a3ee070f0d3652 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Migrate media to Azure. | |
* | |
* Updates existing attachment posts to have the necessary metadata so that the Azure plugin can pick them up. | |
* Runs over blocks of 300 media items at a time. | |
* | |
* @package TMA | |
*/ | |
function azure_db_migrate() { | |
// Only run this script if the query parameter is set. | |
if ( ! isset( $_GET['migrate_azure'] ) ) { | |
return; | |
} | |
// Are they logged in? | |
if ( ! is_user_logged_in() ) { | |
die( 'You must be logged in to run this script.' ); | |
} | |
// Check if the user has permission to run this script. | |
if ( ! current_user_can( 'manage_options' ) ) { | |
die( 'You do not have permission to run this script.' ); | |
} | |
// Start migration. | |
$start_time = microtime( true ); | |
$number_to_migrate = 300; | |
echo '<em>Starting migration of ' . $number_to_migrate . ' items at ' . date( 'Y-m-d H:i:s' ) . '</em><br><br>'; | |
// And that the Azure plugin is active. | |
if ( ! class_exists( 'Windows_Azure_Helper' ) ) { | |
die( "Can't find Azure plugin functions." ); | |
} | |
$posts = get_posts( | |
array( | |
'post_type' => 'attachment', | |
'posts_per_page' => $number_to_migrate, | |
'meta_query' => array( | |
array( | |
// Exclude any media that has already been migrated. | |
'key' => 'windows_azure_storage_info', | |
'compare' => 'NOT EXISTS', | |
), | |
), | |
// Get the most recent media files first. | |
'orderby' => 'ID', | |
'order' => 'DESC', | |
) | |
); | |
foreach ( $posts as $post ) { | |
echo '⏳ <strong>' . $post->ID . '</strong>: \'' . $post->post_title . '\'</br>'; | |
$file = get_post_meta( $post->ID, '_wp_attached_file', true ); | |
$result = migrateMediaItem( $file, $post->ID ); | |
echo '✔ <span style="opacity:0.5;">' . json_encode( $result ) . "</span><br><br>\n\n"; | |
} | |
// Calculate the time this runs for, in seconds. | |
$end_time = microtime( true ); | |
$time_taken = round( $end_time - $start_time, 4 ); | |
echo '<em>Time taken: ' . $time_taken . ' seconds.</em><br><br>'; | |
echo '✅ <strong>Done (' . count( $posts ) . ' items migrated).' . "</strong><br><br>\n\n"; | |
die(); | |
} | |
add_action( 'wp', __NAMESPACE__ . '\azure_db_migrate' ); | |
/** | |
* Helper function to migrate an attachment post so it's picked up by the Azure plugin. | |
*/ | |
function migrateMediaItem( string $file, int $post_id ) { | |
$default_azure_storage_account_container_name = \Windows_Azure_Helper::get_default_container(); | |
$upload_file_name = get_attached_file( $post_id, true ); | |
// Get upload directory. | |
$upload_dir = wp_upload_dir(); | |
$upload_dir['subdir'] = ltrim( $upload_dir['subdir'], '/' ); | |
// Prepare blob name. | |
$relative_file_name = ( '' === $upload_dir['subdir'] ) ? | |
basename( $upload_file_name ) : | |
str_replace( $upload_dir['basedir'] . '/', '', $upload_file_name ); | |
$data['file'] = $upload_file_name; | |
$url = sprintf( | |
'%1$s/%2$s', | |
untrailingslashit( \WindowsAzureStorageUtil::get_storage_url_base() ), | |
$relative_file_name | |
); | |
$metadata = wp_get_attachment_metadata( $post_id ); | |
$file_info = pathinfo( $metadata['file'] ); | |
$file_name = $file_info['basename']; | |
$file_paths = $file_info['dirname']; | |
// Get all image srcs for every size. | |
$thumbnails = array(); | |
if ( isset( $metadata['sizes'] ) && is_array( $metadata['sizes'] ) ) { | |
foreach ( $metadata['sizes'] as $size => $size_data ) { | |
// Output needed: | |
// Array | |
// 0 => string '2020/10/unsplash-Marita-Kavelashvili-300x199.jpg' | |
// 1 => string '2020/10/unsplash-Marita-Kavelashvili-1024x680.jpg' | |
// 2 => string '2020/10/unsplash-Marita-Kavelashvili-150x150.jpg' | |
// 3 => string '2020/10/unsplash-Marita-Kavelashvili-768x510.jpg' | |
// 4 => string '2020/10/unsplash-Marita-Kavelashvili-1536x1020.jpg' | |
// 5 => string '2020/10/unsplash-Marita-Kavelashvili-2048x1360.jpg' | |
// 6 => string '2020/10/unsplash-Marita-Kavelashvili-1200x797.jpg' | |
// 7 => string '2020/10/unsplash-Marita-Kavelashvili-1980x1315.jpg' | |
// 8 => string '2020/10/unsplash-Marita-Kavelashvili.jpg' | |
$thumbnails[] = $file_paths . '/' . $size_data['file']; | |
} | |
} | |
// Add the original image to the thumbnails array. | |
$thumbnails[] = $file_paths . '/' . $file_name; | |
$azure_plugin_version = '4.4.2'; | |
update_post_meta( | |
$post_id, | |
'windows_azure_storage_info', | |
array( | |
'container' => $default_azure_storage_account_container_name, | |
'blob' => $relative_file_name, | |
'url' => $url, | |
'thumbnails' => $thumbnails, | |
'version' => $azure_plugin_version, | |
) | |
); | |
return array( $post_id, $url ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment