Skip to content

Instantly share code, notes, and snippets.

@spacedmonkey
Last active April 19, 2017 18:33
Show Gist options
  • Save spacedmonkey/6a8754f7723ce81b0390780d47dff158 to your computer and use it in GitHub Desktop.
Save spacedmonkey/6a8754f7723ce81b0390780d47dff158 to your computer and use it in GitHub Desktop.
<?php
/**
* Loop through all options and save in the blog meta table
*/
class Blog_Meta_Migration extends WP_CLI_Command {
/**
* Loop through all options and save in the blog meta table
*
* ## OPTIONS
*
* ## EXAMPLES
*
* wp blog-meta-migration
*
*/
public function __invoke( $args, $assoc_args ) {
global $wpdb;
$blog_id = get_current_blog_id();
$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" );
// Number of options returned by query
$found_options = count( $alloptions_db) ;
// Generate progess bar
$progress = new \cli\progress\Bar( 'Progress', $found_options );
// Counter for number of post processed
$counter_processed = 0;
// The Loop
foreach( $alloptions_db as $o ){
$added = add_blog_meta( $blog_id, $o->option_name, $o->option_value, true );
if ( false !== $added ) {
$counter_processed++;
}
// Move the processbar on
$progress->tick();
}
$progress->finish();
if( $found_options == 0) {
WP_CLI::error( "No options found" );
} elseif( $counter_processed == 0 ) {
WP_CLI::error( "No options processed" );
} else {
WP_CLI::success( "Processing compelete. $counter_processed of $found_options where processed successfully." );
}
}
}
WP_CLI::add_command( 'blog-meta-migration', 'Blog_Meta_Migration' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment