Last active
August 29, 2015 14:21
-
-
Save boonebgorges/2beb1379569c3bd85803 to your computer and use it in GitHub Desktop.
wp-cli command for testing https://core.trac.wordpress.org/ticket/30261
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
<?php | |
// Bail if WP-CLI is not present. | |
if ( !defined( 'WP_CLI' ) ) return; | |
class BBG_Command extends WP_CLI_Command { | |
public function generate_shared_terms( $args, $assoc_args ) { | |
global $wpdb; | |
$defaults = array( | |
'count' => 10, | |
'from_taxonomy' => 'post_tag', | |
'to_taxonomy' => 'category', | |
); | |
$r = array_merge( $defaults, $assoc_args ); | |
// Create some base terms in the 'category' taxonomy. | |
$_args = array( 'term', 'generate', 'category' ); | |
$_assoc_args = array( 'count' => $r['count'], 'max_depth' => 4 ); | |
WP_CLI::run_command( $_args, $_assoc_args ); | |
// Get the IDs of the terms just created. | |
$source_term_ids = $wpdb->get_col( "SELECT * FROM $wpdb->terms ORDER BY term_id DESC limit " . intval( $r['count'] ) ); | |
$c = array( | |
'foo1' => array( 'start' => $source_term_ids[0], 'count' => floor( $r['count'] / 2 ) ), | |
'foo2' => array( 'start' => $source_term_ids[0], 'count' => $r['count'] - 1 ), | |
'foo3' => array( 'start' => $source_term_ids[0], 'count' => floor( $r['count'] / 3 ) ), | |
'foo4' => array( 'start' => $source_term_ids[0], 'count' => floor( $r['count'] / 2 ) ), | |
); | |
foreach ( $c as $ctax => $cdata ) { | |
register_taxonomy( $ctax, 'post' ); | |
$to_tax_obj = get_taxonomy( $ctax ); | |
$label = $to_tax_obj->labels->singular_name; | |
$notify = \WP_CLI\Utils\make_progress_bar( 'Generating shared terms in taxonomy ' . $ctax, $r['count'] ); | |
for ( $i = $cdata['start']; $i < ( $cdata['start'] + $cdata['count'] ); $i++ ) { | |
// Create a new term. | |
$term = wp_insert_term( $label . ' ' . rand( 0, 9999999 ), $ctax ); | |
if ( is_wp_error( $term ) ) { | |
WP_CLI::warning( $term ); | |
} else { | |
$wpdb->update( $wpdb->term_taxonomy, | |
array( 'term_id' => $i ), | |
array( 'term_taxonomy_id' => $term['term_taxonomy_id'] ) | |
); | |
} | |
$notify->tick(); | |
} | |
$notify->finish(); | |
} | |
} | |
} | |
WP_CLI::add_command( 'bbg', 'BBG_Command' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment