Commit 80a2402b authored by Joe Hoyle's avatar Joe Hoyle
Browse files

Added wp-cli migrator command for s3 uplods

parent d3d15a56
Loading
Loading
Loading
Loading
+48 −0
Original line number Diff line number Diff line
<?php

class S3_Uploads_WP_CLI_Command extends WP_CLI_Command {

	/**
	 * @subcommand migrate-attachments
	 * 
	 */
	public function migrate_attachments_to_s3() {

		$attachments = new WP_Query( array(
			'post_type' => 'attachment',
			'posts_per_page' => -1,
			'post_status' => 'all'
		));

		WP_CLI::line( sprintf( 'Attempting to move %d attachments to S3', $attachments->found_posts ) );

		foreach ( $attachments->posts as $attachment ) {

			$this->migrate_attachment_to_s3( array( $attachment->ID ) );
		}
	}

	/**
	 * Migrate a single attachment's files to S3
	 * 
	 * @subcommand migrate-attachment
	 * @synposis <attachment-id>
	 */
	public function migrate_attachment_to_s3( $args ) {

		$old_upload_dir = S3_Uploads::get_instance()->get_original_upload_dir();

		$file = get_post_meta( $args[0], '_wp_attached_file', true );

		if ( file_exists( $path = $old_upload_dir['basedir'] . '/' . $file ) ) {

			copy( $path, get_attached_file( $args[0] ) );
			unlink( $path );
			WP_CLI::success( sprintf( 'Moved attachment %d to S3', $args[0] ) ); 
		} else {
			WP_CLI::line( 'Already moved to S3' );
		}
	}
}

WP_CLI::add_command( 's3-uploads', 'S3_Uploads_WP_CLI_Command' );
 No newline at end of file