[ Root ] [ Search ] [ Index ]

PHP Cross Reference of WordPress MU 2.9.2

Provided by Yoast

title

Body

[close]

/wp-admin/ -> wpmu-terms.php (source)

   1  <?php
   2  require_once ( 'admin.php' );
   3  $title = __( 'WordPress MU &rsaquo; Admin &rsaquo; Global Terms' );
   4  $parent_file = 'wpmu-admin.php';
   5  
   6  // handle forms
   7  switch( $_GET[ 'action' ] ) {
   8      case "fixblog":
   9          check_admin_referer( 'fixblog' );
  10          if ( isset( $_POST[ 'id' ] ) && $_POST[ 'id' ] != '' ) {
  11              fix_blog_terms( (int)$_POST[ 'id' ] );
  12              wp_redirect( 'wpmu-terms.php?updated=termsfixed' );
  13              die();
  14          }
  15      break;
  16  }
  17  
  18  include ('admin-header.php');
  19  
  20  if ( is_site_admin() == false ) {
  21      wp_die( __( 'You do not have permission to access this page.' ) );
  22  }
  23  
  24  if ( isset( $_GET[ 'updated' ] ) ) {
  25      switch( $_GET[ 'updated' ] ) {
  26          case "termsfixed":
  27              ?><div id="message" class="updated fade"><p><?php _e( 'Terms Updated.' ) ?></p></div><?php
  28          break;
  29      }
  30  }
  31  ?>
  32  <div class="wrap">
  33  <h2><?php _e( 'Global Terms' ); ?></h2>
  34  <p><?php _e( 'WordPress MU uses a global taxonomy table to keep track of all the tags and categories used on all the blogs. Occasionally this information can become out of sync with the local blog taxonomy tables.' ); ?></p>
  35  <p><?php _e( 'This problem is often caused by adding a blog using MySQL directly rather than going through the WordPress importer. It can also happen when a plugin manipulates the terms tables directly without going through the WordPress API.' ); ?></p>
  36  <p><?php _e( 'Please make a backup of the terms, term_taxonomy and term_relationships tables before running this.' ); ?></p>
  37  <?php
  38  switch( $_GET[ 'action' ] ) {
  39      default:
  40          echo '<form method="post" action="wpmu-terms.php?action=fixblog">';
  41          wp_nonce_field( "fixblog" );
  42          echo '<p>' . __( 'Enter a blog_id to fix the categories and terms of that blog.' ) . '</p>';
  43          echo '<p><strong>' . __( 'Blog id:' ) . '</strong> <input type="text" name="id" value="" /></p>';
  44          echo '<p><input type="checkbox" name="renameterms" value="1" /> ' . __( 'Rename terms <sup>*</sup>' ) . '</p>';
  45          echo '<input type="submit" value="Fix Terms" />';
  46          echo '</form>';
  47          echo '<p>' . __( '<sup>*</sup> WordPress can have terms (tags or categories) with names and slugs that do not match. WordPress MU forces the slug to be a sanitized version of the name. To preserve blog URLs this page can rename the name to match the slug. It will replace underscores with spaces and the first letter will be upper case.' ) . '</p>';
  48      break;
  49  }
  50  
  51  function fix_single_blog_term( $term ) {
  52      global $wpdb;
  53      $new_term = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->sitecategories} WHERE category_nicename = %s", $term->slug ) );
  54      if ( !$new_term ) {
  55          // term not found in global table, create it!
  56          global_terms( $term->term_id );
  57          $new_term = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->sitecategories} WHERE category_nicename = %s", $term->slug ) );
  58      }
  59      if ( $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->terms} WHERE term_id = %d AND slug = %s", $new_term->cat_ID, $new_term->category_nicename ) ) ) {
  60          return true; // term_id is ok
  61      } elseif ( $existing_term = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->terms} WHERE term_id = %d", $new_term->term_id ) ) ) {
  62          // recurse to fix the existing term that's in the way
  63          fix_single_blog_term( $existing_term ); 
  64      }
  65      // fix the term!
  66      global_terms( $term->term_id );
  67      return true;
  68  }
  69  
  70  function fix_blog_terms( $id = 0 ) {
  71      global $wpdb;
  72  
  73      if ( $id == 0 ) {
  74          $id = $wpdb->blogid;
  75      } else {
  76          switch_to_blog( $id );
  77      }
  78      
  79      $maxterm = $wpdb->get_var( "SELECT max(cat_ID) FROM {$wpdb->sitecategories}" );
  80      $rows = $wpdb->get_results( "SELECT * FROM {$wpdb->terms}" );
  81      foreach( $rows as $row ) {
  82          if ( isset( $_POST[ 'renameterms' ] ) && sanitize_title( $row->name ) != $row->slug ) {
  83              if ( ! $name = $wpdb->get_var( $wpdb->prepare( "SELECT cat_name FROM {$wpdb->sitecategories} WHERE category_nicename = %s", $row->slug ) ) ) {
  84                  $name = str_replace( '_', ' ', ucfirst( $row->slug ) );
  85              }
  86              $wpdb->update( $wpdb->terms, array( 'name' => $name ), array('term_id' => $row->term_id) );
  87          }
  88          if ( $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->sitecategories} WHERE cat_ID = %d AND category_nicename = %s", $row->term_id, $row->slug ) ) )
  89              continue;
  90          $term_id = $row->term_id + mt_rand( $maxterm+100, $maxterm+4000 );
  91          if( get_option( 'default_category' ) == $row->term_id )
  92              update_option( 'default_category', $term_id );
  93  
  94          $wpdb->update( $wpdb->terms, array( 'term_id' => $term_id ), array( 'term_id' => $row->term_id ) );
  95          $wpdb->update( $wpdb->term_taxonomy, array( 'term_id' => $term_id ), array( 'term_id' => $row->term_id ) );
  96          $wpdb->update( $wpdb->term_taxonomy, array( 'parent' => $term_id ), array( 'parent' => $row->term_id ) );
  97  
  98          clean_term_cache( $row->term_id );
  99      }
 100      $rows = $wpdb->get_results( "SELECT * FROM {$wpdb->terms}" );
 101      foreach( $rows as $row ) {
 102          if ( null == $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->sitecategories} WHERE cat_ID = %d AND category_nicename = %s", $row->term_id, $row->slug ) ) ) {
 103              fix_single_blog_term( $row );
 104              clean_term_cache( $row->term_id );
 105          }
 106      }
 107      restore_current_blog();
 108  }
 109  ?>
 110  </div>
 111  <?php include ('./admin-footer.php'); ?>


Generated: Mon May 3 12:25:32 2010 Cross-referenced by PHPXref 0.7