Guest User

Untitled

a guest
Oct 17th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. <?php
  2. /**
  3. * List terms (categories) with the same name for each of the blog with a sticker.
  4. *
  5. * This is a one-off, but could be easily abstracted to be more versatile.
  6. *
  7. * @subcommand list-multiple-terms-with-the-same-name
  8. * @synopsis [--taxonomy=<category>]
  9. */
  10.  
  11. function list_multiple_terms_with_the_same_name( $args, $assoc_args ) {
  12. $tax = $assoc_args['taxonomy'] ?? 'category';
  13.  
  14. $current_blog_id = get_current_blog_id();
  15. // Can be list of blog ids in the network
  16. $blog_ids = [ $current_blog_id ];
  17. $ret = [];
  18.  
  19. $get_function = function() {
  20. $categories = get_terms( $tax, [ 'hide_empty' => false, 'fields' => 'id=>name' ] );
  21. return array_unique( array_diff_assoc( $categories, array_unique( $categories ) ) );
  22. };
  23.  
  24.  
  25. WP_CLI::line( sprintf( 'Found %d blogs with "%s" sticker', count( $blog_ids ), $sticker ) );
  26. foreach( $blog_ids as $blog_id ) {
  27. switch_to_blog( $blog_id );
  28. $dupes = $get_function();
  29. if ( $dupes ) {
  30. $ret[ $blog_id ] = [
  31. 'bloginfo' => [
  32. 'name' => get_bloginfo( 'name' ),
  33. 'url' => get_bloginfo( 'url' ),
  34. ],
  35. 'duplicateCategories' => array_map( function( $cat ) { return sprintf("%s : %s", $cat, esc_url_raw( admin_url( "edit-tags.php?taxonomy=category&post_type=post&s={$cat}" ) ) ); }, array_values( $dupes ) )
  36. ];
  37. }
  38. $this->stop_the_insanity();
  39. }
  40.  
  41. WP_CLI::line( sprintf( '%d blogs have categories with duplicate names', count( $ret ) ) );
  42.  
  43. WP_CLI::line( wp_json_encode( $ret, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ) );
  44.  
  45. switch_to_blog( $current_blog_id );
  46. }
Add Comment
Please, Sign In to add comment