web/wp-admin/import/utw.php
branchwordpress
changeset 109 03b0d1493584
child 132 4d4862461b8d
equal deleted inserted replaced
-1:000000000000 109:03b0d1493584
       
     1 <?php
       
     2 /**
       
     3  * The Ultimate Tag Warrior Importer.
       
     4  *
       
     5  * @package WordPress
       
     6  * @subpackage Importer
       
     7  */
       
     8 
       
     9 /**
       
    10  * Ultimate Tag Warrior Converter to 2.3 taxonomy.
       
    11  *
       
    12  * This converts the Ultimate Tag Warrior tags to the 2.3 WordPress taxonomy.
       
    13  *
       
    14  * @since 2.3.0
       
    15  */
       
    16 class UTW_Import {
       
    17 
       
    18 	function header()  {
       
    19 		echo '<div class="wrap">';
       
    20 		screen_icon();
       
    21 		echo '<h2>'.__('Import Ultimate Tag Warrior').'</h2>';
       
    22 		echo '<p>'.__('Steps may take a few minutes depending on the size of your database. Please be patient.').'<br /><br /></p>';
       
    23 	}
       
    24 
       
    25 	function footer() {
       
    26 		echo '</div>';
       
    27 	}
       
    28 
       
    29 	function greet() {
       
    30 		echo '<div class="narrow">';
       
    31 		echo '<p>'.__('Howdy! This imports tags from Ultimate Tag Warrior 3 into WordPress tags.').'</p>';
       
    32 		echo '<p>'.__('This has not been tested on any other versions of Ultimate Tag Warrior. Mileage may vary.').'</p>';
       
    33 		echo '<p>'.__('To accommodate larger databases for those tag-crazy authors out there, we have made this into an easy 5-step program to help you kick that nasty UTW habit. Just keep clicking along and we will let you know when you are in the clear!').'</p>';
       
    34 		echo '<p><strong>'.__('Don&#8217;t be stupid - backup your database before proceeding!').'</strong></p>';
       
    35 		echo '<form action="admin.php?import=utw&amp;step=1" method="post">';
       
    36 		echo '<p class="submit"><input type="submit" name="submit" class="button" value="'.esc_attr__('Step 1').'" /></p>';
       
    37 		echo '</form>';
       
    38 		echo '</div>';
       
    39 	}
       
    40 
       
    41 
       
    42 	function dispatch () {
       
    43 		if ( empty( $_GET['step'] ) ) {
       
    44 			$step = 0;
       
    45 		} else {
       
    46 			$step = (int) $_GET['step'];
       
    47 		}
       
    48 
       
    49 		if ( $step > 1 )
       
    50 			check_admin_referer('import-utw');
       
    51 
       
    52 		// load the header
       
    53 		$this->header();
       
    54 
       
    55 		switch ( $step ) {
       
    56 			case 0 :
       
    57 				$this->greet();
       
    58 				break;
       
    59 			case 1 :
       
    60 				$this->import_tags();
       
    61 				break;
       
    62 			case 2 :
       
    63 				$this->import_posts();
       
    64 				break;
       
    65 			case 3:
       
    66 				$this->import_t2p();
       
    67 				break;
       
    68 			case 4:
       
    69 				$this->cleanup_import();
       
    70 				break;
       
    71 		}
       
    72 
       
    73 		// load the footer
       
    74 		$this->footer();
       
    75 	}
       
    76 
       
    77 
       
    78 	function import_tags ( ) {
       
    79 		echo '<div class="narrow">';
       
    80 		echo '<p><h3>'.__('Reading UTW Tags&#8230;').'</h3></p>';
       
    81 
       
    82 		$tags = $this->get_utw_tags();
       
    83 
       
    84 		// if we didn't get any tags back, that's all there is folks!
       
    85 		if ( !is_array($tags) ) {
       
    86 			echo '<p>' . __('No Tags Found!') . '</p>';
       
    87 			return false;
       
    88 		}
       
    89 		else {
       
    90 
       
    91 			// if there's an existing entry, delete it
       
    92 			if ( get_option('utwimp_tags') ) {
       
    93 				delete_option('utwimp_tags');
       
    94 			}
       
    95 
       
    96 			add_option('utwimp_tags', $tags);
       
    97 
       
    98 
       
    99 			$count = count($tags);
       
   100 
       
   101 			echo '<p>' . sprintf( _n('Done! <strong>%s</strong> tag were read.', 'Done! <strong>%s</strong> tags were read.', $count), $count ) . '<br /></p>';
       
   102 			echo '<p>' . __('The following tags were found:') . '</p>';
       
   103 
       
   104 			echo '<ul>';
       
   105 
       
   106 			foreach ( $tags as $tag_id => $tag_name ) {
       
   107 
       
   108 				echo '<li>' . $tag_name . '</li>';
       
   109 
       
   110 			}
       
   111 
       
   112 			echo '</ul>';
       
   113 
       
   114 			echo '<br />';
       
   115 
       
   116 			echo '<p>' . __('If you don&#8217;t want to import any of these tags, you should delete them from the UTW tag management page and then re-run this import.') . '</p>';
       
   117 
       
   118 
       
   119 		}
       
   120 
       
   121 		echo '<form action="admin.php?import=utw&amp;step=2" method="post">';
       
   122 		wp_nonce_field('import-utw');
       
   123 		echo '<p class="submit"><input type="submit" name="submit" class="button" value="'.esc_attr__('Step 2').'" /></p>';
       
   124 		echo '</form>';
       
   125 		echo '</div>';
       
   126 	}
       
   127 
       
   128 
       
   129 	function import_posts ( ) {
       
   130 		echo '<div class="narrow">';
       
   131 		echo '<p><h3>'.__('Reading UTW Post Tags&#8230;').'</h3></p>';
       
   132 
       
   133 		// read in all the UTW tag -> post settings
       
   134 		$posts = $this->get_utw_posts();
       
   135 
       
   136 		// if we didn't get any tags back, that's all there is folks!
       
   137 		if ( !is_array($posts) ) {
       
   138 			echo '<p>' . __('No posts were found to have tags!') . '</p>';
       
   139 			return false;
       
   140 		}
       
   141 		else {
       
   142 
       
   143 			// if there's an existing entry, delete it
       
   144 			if ( get_option('utwimp_posts') ) {
       
   145 				delete_option('utwimp_posts');
       
   146 			}
       
   147 
       
   148 			add_option('utwimp_posts', $posts);
       
   149 
       
   150 
       
   151 			$count = count($posts);
       
   152 
       
   153 			echo '<p>' . sprintf( _n('Done! <strong>%s</strong> tag to post relationships were read.', 'Done! <strong>%s</strong> tags to post relationships were read.', $count), $count ) . '<br /></p>';
       
   154 
       
   155 		}
       
   156 
       
   157 		echo '<form action="admin.php?import=utw&amp;step=3" method="post">';
       
   158 		wp_nonce_field('import-utw');
       
   159 		echo '<p class="submit"><input type="submit" name="submit" class="button" value="'.esc_attr__('Step 3').'" /></p>';
       
   160 		echo '</form>';
       
   161 		echo '</div>';
       
   162 
       
   163 	}
       
   164 
       
   165 
       
   166 	function import_t2p ( ) {
       
   167 
       
   168 		echo '<div class="narrow">';
       
   169 		echo '<p><h3>'.__('Adding Tags to Posts&#8230;').'</h3></p>';
       
   170 
       
   171 		// run that funky magic!
       
   172 		$tags_added = $this->tag2post();
       
   173 
       
   174 		echo '<p>' . sprintf( _n( 'Done! <strong>%s</strong> tag were added!', 'Done! <strong>%s</strong> tags were added!', $tags_added ), $tags_added ) . '<br /></p>';
       
   175 
       
   176 		echo '<form action="admin.php?import=utw&amp;step=4" method="post">';
       
   177 		wp_nonce_field('import-utw');
       
   178 		echo '<p class="submit"><input type="submit" name="submit" class="button" value="'.esc_attr__('Step 4').'" /></p>';
       
   179 		echo '</form>';
       
   180 		echo '</div>';
       
   181 
       
   182 	}
       
   183 
       
   184 
       
   185 	function get_utw_tags ( ) {
       
   186 
       
   187 		global $wpdb;
       
   188 
       
   189 		// read in all the tags from the UTW tags table: should be wp_tags
       
   190 		$tags_query = "SELECT tag_id, tag FROM " . $wpdb->prefix . "tags";
       
   191 
       
   192 		$tags = $wpdb->get_results($tags_query);
       
   193 
       
   194 		// rearrange these tags into something we can actually use
       
   195 		foreach ( $tags as $tag ) {
       
   196 
       
   197 			$new_tags[$tag->tag_id] = $tag->tag;
       
   198 
       
   199 		}
       
   200 
       
   201 		return $new_tags;
       
   202 
       
   203 	}
       
   204 
       
   205 	function get_utw_posts ( ) {
       
   206 
       
   207 		global $wpdb;
       
   208 
       
   209 		// read in all the posts from the UTW post->tag table: should be wp_post2tag
       
   210 		$posts_query = "SELECT tag_id, post_id FROM " . $wpdb->prefix . "post2tag";
       
   211 
       
   212 		$posts = $wpdb->get_results($posts_query);
       
   213 
       
   214 		return $posts;
       
   215 
       
   216 	}
       
   217 
       
   218 
       
   219 	function tag2post ( ) {
       
   220 
       
   221 		// get the tags and posts we imported in the last 2 steps
       
   222 		$tags = get_option('utwimp_tags');
       
   223 		$posts = get_option('utwimp_posts');
       
   224 
       
   225 		// null out our results
       
   226 		$tags_added = 0;
       
   227 
       
   228 		// loop through each post and add its tags to the db
       
   229 		foreach ( $posts as $this_post ) {
       
   230 
       
   231 			$the_post = (int) $this_post->post_id;
       
   232 			$the_tag = (int) $this_post->tag_id;
       
   233 
       
   234 			// what's the tag name for that id?
       
   235 			$the_tag = $tags[$the_tag];
       
   236 
       
   237 			// screw it, just try to add the tag
       
   238 			wp_add_post_tags($the_post, $the_tag);
       
   239 
       
   240 			$tags_added++;
       
   241 
       
   242 		}
       
   243 
       
   244 		// that's it, all posts should be linked to their tags properly, pending any errors we just spit out!
       
   245 		return $tags_added;
       
   246 
       
   247 
       
   248 	}
       
   249 
       
   250 
       
   251 	function cleanup_import ( ) {
       
   252 
       
   253 		delete_option('utwimp_tags');
       
   254 		delete_option('utwimp_posts');
       
   255 
       
   256 		$this->done();
       
   257 
       
   258 	}
       
   259 
       
   260 
       
   261 	function done ( ) {
       
   262 
       
   263 		echo '<div class="narrow">';
       
   264 		echo '<p><h3>'.__('Import Complete!').'</h3></p>';
       
   265 
       
   266 		echo '<p>' . __('OK, so we lied about this being a 5-step program! You&#8217;re done!') . '</p>';
       
   267 
       
   268 		echo '<p>' . __('Now wasn&#8217;t that easy?') . '</p>';
       
   269 
       
   270 		echo '</div>';
       
   271 
       
   272 	}
       
   273 
       
   274 
       
   275 	function UTW_Import ( ) {
       
   276 
       
   277 		// Nothing.
       
   278 
       
   279 	}
       
   280 
       
   281 }
       
   282 
       
   283 
       
   284 // create the import object
       
   285 $utw_import = new UTW_Import();
       
   286 
       
   287 // add it to the import page!
       
   288 register_importer('utw', 'Ultimate Tag Warrior', __('Import Ultimate Tag Warrior tags into WordPress tags.'), array($utw_import, 'dispatch'));
       
   289 
       
   290 ?>