| | 1 | <?php |
| | 2 | /** |
| | 3 | * bbPress Standalone Importer |
| | 4 | * |
| | 5 | * Helps in converting your bbPress Standalone into the new bbPress Plugin |
| | 6 | * |
| | 7 | * @package bbPress |
| | 8 | * @subpackage Importer |
| | 9 | * |
| | 10 | * @todo Cleanup & Docs |
| | 11 | * @todo i18n |
| | 12 | * @todo User Mapping (ref. MT Importer) |
| | 13 | */ |
| | 14 | class bbPress_Importer { |
| | 15 | |
| | 16 | /** |
| | 17 | * @var string Path to bbPress standalone |
| | 18 | */ |
| | 19 | var $bb_path = ''; |
| | 20 | |
| | 21 | /** |
| | 22 | * @var array User Map |
| | 23 | */ |
| | 24 | var $user_map = array(); |
| | 25 | |
| | 26 | /** |
| | 27 | * @var bool Are we debugging? |
| | 28 | */ |
| | 29 | var $debug = true; |
| | 30 | |
| | 31 | /** |
| | 32 | * Returns the tag name from tag object |
| | 33 | * |
| | 34 | * @param object $tag Tag Object |
| | 35 | * @return string Tag name |
| | 36 | */ |
| | 37 | function get_tag_name( $tag ) { |
| | 38 | return $tag->name; |
| | 39 | } |
| | 40 | |
| | 41 | /** |
| | 42 | * Simple check to check if the WP and bb user tables are integrated or not |
| | 43 | * |
| | 44 | * @return bool True if integrated, false if not |
| | 45 | */ |
| | 46 | function is_integrated() { |
| | 47 | global $bbdb, $wpdb; |
| | 48 | |
| | 49 | return ( $wpdb->users == $bbdb->users && $wpdb->usermeta == $bbdb->usermeta ); |
| | 50 | } |
| | 51 | |
| | 52 | /** |
| | 53 | * Tries to automatically locate the bbPress standalone install path |
| | 54 | * |
| | 55 | * @return string Path, if found |
| | 56 | */ |
| | 57 | function autolocate_bb_path() { |
| | 58 | |
| | 59 | $path = ''; |
| | 60 | $dirs = array( 'forum', 'forums', 'board', 'discussion', 'bb', 'bbpress' ); |
| | 61 | $base = trailingslashit( ABSPATH ); |
| | 62 | $base_dirs = array( $base, dirname( $base ) ); |
| | 63 | |
| | 64 | foreach ( $dirs as $dir ) { |
| | 65 | |
| | 66 | foreach ( $base_dirs as $base_dir ) { |
| | 67 | $test_path = $base_dir . $dir . '/bb-load.php'; |
| | 68 | |
| | 69 | if ( file_exists( $test_path ) ) { |
| | 70 | $path = $test_path; |
| | 71 | break; |
| | 72 | } |
| | 73 | } |
| | 74 | |
| | 75 | } |
| | 76 | |
| | 77 | return $path; |
| | 78 | } |
| | 79 | |
| | 80 | /** |
| | 81 | * Get the bbPress standalone topic favoriters from topic id |
| | 82 | * |
| | 83 | * @param int $topic_id Topic id |
| | 84 | * @return array Topic Favoriters' IDs |
| | 85 | */ |
| | 86 | function bb_get_topic_favoriters( $topic_id = 0 ) { |
| | 87 | if ( empty( $topic_id ) ) |
| | 88 | return array(); |
| | 89 | |
| | 90 | global $bbdb; |
| | 91 | |
| | 92 | return (array) $bbdb->get_col( $bbdb->prepare( "SELECT user_id |
| | 93 | FROM $bbdb->usermeta |
| | 94 | WHERE meta_key = '{$bbdb->prefix}favorites' |
| | 95 | AND FIND_IN_SET( %d, meta_value ) > 0", |
| | 96 | $topic_id ) ); |
| | 97 | } |
| | 98 | |
| | 99 | /** |
| | 100 | * Get the bbPress standalone topic subscribers from topic id |
| | 101 | * |
| | 102 | * If the Subscribe to Topic bbPress plugin is active, then subscription |
| | 103 | * info is taken from that. Otherwise, if the the user is using |
| | 104 | * bbPress >= 1.1 alpha, then get the info from there. |
| | 105 | * |
| | 106 | * @param int $topic_id Topic id |
| | 107 | * @return array Topic Subscribers' IDs |
| | 108 | */ |
| | 109 | function bb_get_topic_subscribers( $topic_id = 0 ) { |
| | 110 | if ( empty( $topic_id ) ) |
| | 111 | return array(); |
| | 112 | |
| | 113 | global $bbdb, $subscribe_to_topic; |
| | 114 | |
| | 115 | $users = array(); |
| | 116 | |
| | 117 | // The user is using Subscribe to Topic plugin by _ck_, get the subscribers from there |
| | 118 | if ( !empty( $subscribe_to_topic ) && !empty( $subscribe_to_topic['db'] ) ) { |
| | 119 | $users = $bbdb->get_col( $bbdb->prepare( "SELECT user |
| | 120 | FROM {$subscribe_to_topic['db']} |
| | 121 | WHERE topic = %d |
| | 122 | AND type = 2", $topic_id ) ); |
| | 123 | |
| | 124 | // The user is using alpha, get the subscribers from built-in functionality |
| | 125 | } elseif ( function_exists( 'bb_notify_subscribers' ) ) { |
| | 126 | $users = $bbdb->get_col( $bbdb->prepare( "SELECT `$bbdb->term_relationships`.`object_id` |
| | 127 | FROM $bbdb->term_relationships, $bbdb->term_taxonomy, $bbdb->terms |
| | 128 | WHERE `$bbdb->term_relationships`.`term_taxonomy_id` = `$bbdb->term_taxonomy`.`term_taxonomy_id` |
| | 129 | AND `$bbdb->term_taxonomy`.`term_id` = `$bbdb->terms`.`term_id` |
| | 130 | AND `$bbdb->term_taxonomy`.`taxonomy` = 'bb_subscribe' |
| | 131 | AND `$bbdb->terms`.`slug` = 'topic-%d'", |
| | 132 | $topic_id ) ); |
| | 133 | } |
| | 134 | |
| | 135 | return (array) $users; |
| | 136 | } |
| | 137 | |
| | 138 | function header() { ?> |
| | 139 | |
| | 140 | <div class="wrap"> |
| | 141 | |
| | 142 | <?php screen_icon(); ?> |
| | 143 | |
| | 144 | <h2><?php _e( 'bbPress Standalone Importer', 'bbpress' ); ?></h2> |
| | 145 | |
| | 146 | <?php |
| | 147 | } |
| | 148 | |
| | 149 | /** |
| | 150 | * Output an error message with a button to try again. |
| | 151 | * |
| | 152 | * @param type $error |
| | 153 | * @param type $step |
| | 154 | */ |
| | 155 | function throw_error( $error, $step ) { |
| | 156 | echo '<p><strong>' . $error->get_error_message() . '</strong></p>'; |
| | 157 | echo $this->next_step( $step, __( 'Try Again', 'bbpress' ) ); |
| | 158 | } |
| | 159 | |
| | 160 | /** |
| | 161 | * Returns the HTML for a link to the next page |
| | 162 | * |
| | 163 | * @param type $next_step |
| | 164 | * @param type $label |
| | 165 | * @param type $id |
| | 166 | * @return string |
| | 167 | */ |
| | 168 | function next_step( $next_step, $label, $id = 'bbpress-import-next-form' ) { |
| | 169 | $str = '<form action="admin.php?import=bbpress" method="post" id="' . $id . '">'; |
| | 170 | $str .= wp_nonce_field( 'bbpress-import', '_wpnonce', true, false ); |
| | 171 | $str .= wp_referer_field( false ); |
| | 172 | $str .= '<input type="hidden" name="step" id="step" value="' . esc_attr( $next_step ) . '" />'; |
| | 173 | $str .= '<p><input type="submit" class="button" value="' . esc_attr( $label ) . '" /> <span id="auto-message"></span></p>'; |
| | 174 | $str .= '</form>'; |
| | 175 | |
| | 176 | return $str; |
| | 177 | } |
| | 178 | |
| | 179 | /** |
| | 180 | * Automatically submit the specified form after $seconds |
| | 181 | * Include a friendly countdown in the element with id=$msg |
| | 182 | * @param type $id |
| | 183 | * @param type $msg |
| | 184 | * @param type $seconds |
| | 185 | */ |
| | 186 | function auto_submit( $id = 'bbpress-import-next-form', $msg = 'auto-message', $seconds = 10 ) { |
| | 187 | ?><script type="text/javascript"> |
| | 188 | next_counter = <?php echo $seconds ?>; |
| | 189 | jQuery(document).ready(function(){ |
| | 190 | bbp_bbpress_msg(); |
| | 191 | }); |
| | 192 | |
| | 193 | function bbp_bbpress_msg() { |
| | 194 | str = '<?php echo esc_js( _e( "Continuing in %d…" , 'bbpress' ) ); ?>'; |
| | 195 | jQuery( '#<?php echo $msg ?>' ).html( str.replace( /%d/, next_counter ) ); |
| | 196 | if ( next_counter <= 0 ) { |
| | 197 | if ( jQuery( '#<?php echo $id ?>' ).length ) { |
| | 198 | jQuery( "#<?php echo $id ?> input[type='submit']" ).hide(); |
| | 199 | str = '<?php echo esc_js( __( "Continuing…" , 'bbpress' ) ); ?> <img src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" alt="" id="processing" align="top" />'; |
| | 200 | jQuery( '#<?php echo $msg ?>' ).html( str ); |
| | 201 | jQuery( '#<?php echo $id ?>' ).submit(); |
| | 202 | return; |
| | 203 | } |
| | 204 | } |
| | 205 | next_counter = next_counter - 1; |
| | 206 | setTimeout('bbp_bbpress_msg()', 1000); |
| | 207 | } |
| | 208 | </script><?php |
| | 209 | } |
| | 210 | |
| | 211 | /** |
| | 212 | * Footer |
| | 213 | */ |
| | 214 | function footer() { ?> |
| | 215 | |
| | 216 | </div> |
| | 217 | |
| | 218 | <?php |
| | 219 | } |
| | 220 | |
| | 221 | /** |
| | 222 | * Dispatch |
| | 223 | */ |
| | 224 | function dispatch() { |
| | 225 | if ( empty( $_REQUEST['step'] ) ) |
| | 226 | $step = 0; |
| | 227 | else |
| | 228 | $step = (int) $_REQUEST['step']; |
| | 229 | |
| | 230 | $this->header(); |
| | 231 | |
| | 232 | switch ( $step ) { |
| | 233 | case -1 : |
| | 234 | $this->cleanup(); |
| | 235 | // Intentional no break |
| | 236 | |
| | 237 | case 0 : |
| | 238 | $this->greet(); |
| | 239 | break; |
| | 240 | |
| | 241 | case 1 : |
| | 242 | case 2 : |
| | 243 | case 3 : |
| | 244 | |
| | 245 | check_admin_referer( 'bbp-bbpress-import' ); |
| | 246 | $result = $this->{ 'step' . $step }(); |
| | 247 | |
| | 248 | break; |
| | 249 | } |
| | 250 | |
| | 251 | $this->footer(); |
| | 252 | } |
| | 253 | |
| | 254 | /** |
| | 255 | * Greet message |
| | 256 | */ |
| | 257 | function greet() { |
| | 258 | $autolocate = $this->autolocate_bb_path(); |
| | 259 | |
| | 260 | ?> |
| | 261 | |
| | 262 | <div class="narrow"> |
| | 263 | |
| | 264 | <form action="admin.php?import=bbpress" method="post"> |
| | 265 | |
| | 266 | <?php wp_nonce_field( 'bbp-bbpress-import' ); ?> |
| | 267 | |
| | 268 | <?php if ( get_option( 'bbp_bbpress_path' ) ) : ?> |
| | 269 | |
| | 270 | <input type="hidden" name="step" value="<?php echo esc_attr( get_option( 'bbp_bbpress_step' ) ); ?>" /> |
| | 271 | |
| | 272 | <p><?php _e( 'It looks like you attempted to convert your bbPress standalone previously and got interrupted.', 'bbpress' ); ?></p> |
| | 273 | |
| | 274 | <p class="submit"> |
| | 275 | <input type="submit" class="button" value="<?php esc_attr_e( 'Continue previous import', 'bbpress' ); ?>" /> |
| | 276 | </p> |
| | 277 | |
| | 278 | <p class="submitbox"><a href="<?php echo esc_url( add_query_arg( array( 'import' => 'bbpress', 'step' => '-1', '_wpnonce' => wp_create_nonce( 'bbp-bbpress-import' ), '_wp_http_referer' => esc_attr( $_SERVER['REQUEST_URI'] ) ) ) ); ?>" class="deletion submitdelete"><?php _e( 'Cancel & start a new import', 'bbpress' ); ?></a></p> |
| | 279 | <p> |
| | 280 | |
| | 281 | <?php else : ?> |
| | 282 | |
| | 283 | <input type="hidden" name="step" value="1" /> |
| | 284 | <input type="hidden" name="bbp_bbpress_path" value="true" /> |
| | 285 | |
| | 286 | <p><?php _e( 'Howdy! This importer allows you to convert your bbPress Standalone into the bbPress Plugin.', 'bbpress' ); ?></p> |
| | 287 | <p><?php _e( 'Please enter the path to your bbPress standalone install, where you\'d find your <code>bb-load.php</code>:', 'bbpress' ); ?></p> |
| | 288 | |
| | 289 | <table class="form-table"> |
| | 290 | |
| | 291 | <tr> |
| | 292 | <th scope="row"><label for="bbp_bbpress_path"><?php _e( 'bbPress Standalone Path', 'bbpress' ); ?></label></th> |
| | 293 | <td><input type="text" name="bbp_bbpress_path" id="bbp_bbpress_path" class="regular-text" value="<?php echo $autolocate; ?>" /></td> |
| | 294 | </tr> |
| | 295 | |
| | 296 | </table> |
| | 297 | |
| | 298 | <?php if ( !empty( $autolocate ) ) : ?> |
| | 299 | <p><?php _e( 'We tried to autolocate the path to your bbPress install and were successful. Yes, we\'re smart. ;)', 'bbpress' ); ?></p> |
| | 300 | <?php endif; ?> |
| | 301 | |
| | 302 | <p><?php _e( "<strong>WARNING:</strong> This can take a really long time if you have a lot of data. Ideally, you should only start this process if you can leave your computer alone while it finishes the import." , 'bbpress' ); ?></p> |
| | 303 | |
| | 304 | <p class="submit"> |
| | 305 | <input type="submit" class="button" value="<?php esc_attr_e( 'Convert!', 'bbpress' ); ?>" /> |
| | 306 | </p> |
| | 307 | |
| | 308 | <p><?php _e( '<strong>NOTE:</strong> If the import process is interrupted for <em>any</em> reason, come back to this page and it will continue from where it stopped automatically.', 'bbpress' ); ?></p> |
| | 309 | |
| | 310 | <noscript> |
| | 311 | <p><?php _e( '<strong>NOTE:</strong> You appear to have JavaScript disabled, so you will need to manually click through each step of this importer. If you enable JavaScript, it will step through automatically.', 'bbpress' ); ?></p> |
| | 312 | </noscript> |
| | 313 | |
| | 314 | <?php endif; ?> |
| | 315 | |
| | 316 | </form> |
| | 317 | |
| | 318 | </div> |
| | 319 | <?php |
| | 320 | } |
| | 321 | |
| | 322 | /** |
| | 323 | * Cleanups all the options used by the importer |
| | 324 | */ |
| | 325 | function cleanup() { |
| | 326 | delete_option( 'bbp_bbpress_path' ); |
| | 327 | delete_option( 'bbp_bbpress_step' ); |
| | 328 | |
| | 329 | do_action( 'import_end' ); |
| | 330 | } |
| | 331 | |
| | 332 | // |
| | 333 | /** |
| | 334 | * Technically the first half of step 1, this is separated to allow for AJAX |
| | 335 | * calls. Sets up some variables and options and confirms authentication. |
| | 336 | * |
| | 337 | * @return type |
| | 338 | */ |
| | 339 | function setup() { |
| | 340 | // Get details from form or from DB |
| | 341 | if ( !empty( $_POST['bbp_bbpress_path'] ) ) { |
| | 342 | // Store details for later |
| | 343 | $this->bb_path = $_POST['bbp_bbpress_path']; |
| | 344 | |
| | 345 | update_option( 'bbp_bbpress_path', $this->bb_path ); |
| | 346 | } else { |
| | 347 | $this->bb_path = get_option( 'bbp_bbpress_path' ); |
| | 348 | } |
| | 349 | |
| | 350 | remove_filter( 'pre_post_status', 'bb_bozo_pre_post_status', 5, 3 ); |
| | 351 | |
| | 352 | if ( empty( $this->bb_path ) ) { ?> |
| | 353 | |
| | 354 | <p><?php _e( 'Please enter the path to your bbPress loader file (<code>bb-load.php</code>).', 'bbpress' ); ?></p> |
| | 355 | <p><a href="<?php echo esc_url( add_query_arg( array( 'import' => 'bbpress', 'step' => '-1', '_wpnonce' => wp_create_nonce( 'bbp-bbpress-import' ), '_wp_http_referer' => esc_attr( remove_query_arg( 'step', $_SERVER['REQUEST_URI'] ) ) ) ) ); ?>"><?php _e( 'Start again', 'bbpress' ); ?></a></p> |
| | 356 | |
| | 357 | <?php |
| | 358 | |
| | 359 | return false; |
| | 360 | } |
| | 361 | |
| | 362 | // Check if the file exists |
| | 363 | if ( !file_exists( $this->bb_path ) ) { |
| | 364 | |
| | 365 | delete_option( 'bbp_bbpress_path' ); ?> |
| | 366 | |
| | 367 | <p><?php _e( 'bbPress loader file (<code>bb-load.php</code>) doesn\'t exist in the path specified! Please check the path and try again.', 'bbpress' ); ?></p> |
| | 368 | <p><a href="<?php echo esc_url( add_query_arg( array( 'import' => 'bbpress', 'step' => '-1', '_wpnonce' => wp_create_nonce( 'bbp-bbpress-import' ), '_wp_http_referer' => esc_attr( remove_query_arg( 'step', $_SERVER['REQUEST_URI'] ) ) ) ) ); ?>"><?php _e( 'Start again', 'bbpress' ); ?></a></p> |
| | 369 | <?php |
| | 370 | return false; |
| | 371 | } else { |
| | 372 | require_once( $this->bb_path ); |
| | 373 | } |
| | 374 | |
| | 375 | return true; |
| | 376 | } |
| | 377 | |
| | 378 | /** |
| | 379 | * Check form inputs and start importing users |
| | 380 | * |
| | 381 | * @return type |
| | 382 | */ |
| | 383 | function step1() { |
| | 384 | do_action( 'import_start' ); |
| | 385 | |
| | 386 | // Set time limit to 0 to avoid time out errors |
| | 387 | set_time_limit( 0 ); |
| | 388 | |
| | 389 | if ( is_callable( 'ob_implicit_flush' ) ) |
| | 390 | ob_implicit_flush( true ); |
| | 391 | |
| | 392 | update_option( 'bbp_bbpress_step', 1 ); |
| | 393 | |
| | 394 | // First run (non-AJAX) |
| | 395 | $setup = $this->setup(); |
| | 396 | if ( empty( $setup ) ) { |
| | 397 | return false; |
| | 398 | } elseif ( is_wp_error( $setup ) ) { |
| | 399 | $this->throw_error( $setup, 1 ); |
| | 400 | return false; |
| | 401 | } |
| | 402 | |
| | 403 | global $wpdb, $bbdb; |
| | 404 | |
| | 405 | ?> |
| | 406 | |
| | 407 | <div id="bbpress-import-status"> |
| | 408 | |
| | 409 | <h3><?php _e( 'Importing Users', 'bbpress' ); ?></h3> |
| | 410 | <p><?php _e( 'We’re in the process of migrating your users...', 'bbpress' ); ?></p> |
| | 411 | |
| | 412 | <ol> |
| | 413 | |
| | 414 | <?php |
| | 415 | |
| | 416 | // Drop the old temp tables while debugging |
| | 417 | if ( $this->debug == true ) |
| | 418 | $wpdb->query( "DROP TABLE IF EXISTS {$bbdb->prefix}{$wpdb->users}_tmp, {$bbdb->prefix}{$wpdb->usermeta}_tmp" ); |
| | 419 | |
| | 420 | // Rename the WordPress users and usermeta table |
| | 421 | |
| | 422 | ?> |
| | 423 | |
| | 424 | <li> |
| | 425 | <?php |
| | 426 | if ( $wpdb->query( "RENAME TABLE $wpdb->users TO {$bbdb->prefix}{$wpdb->users}_tmp, $wpdb->usermeta TO {$bbdb->prefix}{$wpdb->usermeta}_tmp" ) !== false ) : ?> |
| | 427 | Renamed the <code><?php echo $wpdb->users; ?></code> and <code><?php echo $wpdb->usermeta; ?></code> tables to <code><?php echo $bbdb->prefix . $wpdb->users . '_tmp'; ?></code> and <code><?php echo $bbdb->prefix . $wpdb->usermeta . '_tmp'; ?></code> respectively. |
| | 428 | <?php else : ?> |
| | 429 | There was a problem dropping the <code><?php echo $wpdb->users; ?></code> and <code><?php echo $wpdb->usermeta; ?></code> tables. Please check and re-run the script or rename or drop the tables yourself.</li></ol> |
| | 430 | <?php break; endif; ?> |
| | 431 | </li> |
| | 432 | |
| | 433 | <?php /* Duplicate the WordPress users and usermeta table */ ?> |
| | 434 | |
| | 435 | <li> |
| | 436 | <?php if ( $wpdb->query( "CREATE TABLE $wpdb->users ( `ID` BIGINT( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, `user_activation_key` VARCHAR( 60 ) NOT NULL DEFAULT '', KEY ( `user_login` ), KEY( `user_nicename` ) ) SELECT * FROM $bbdb->users ORDER BY `ID`" ) !== false ) : ?> |
| | 437 | Created the <code><?php echo $wpdb->users; ?></code> table and copied the users from bbPress. |
| | 438 | <?php else : ?> |
| | 439 | There was a problem duplicating the table <code><?php echo $bbdb->users; ?></code> to <code><?php echo $wpdb->users; ?></code>. Please check and re-run the script or duplicate the table yourself.</li></ol> |
| | 440 | <?php break; endif; ?> |
| | 441 | </li> |
| | 442 | |
| | 443 | <li> |
| | 444 | <?php if ( $wpdb->query( "CREATE TABLE $wpdb->usermeta ( `umeta_id` BIGINT( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, KEY ( `user_id` ), KEY( `meta_key` ) ) SELECT * FROM $bbdb->usermeta ORDER BY `umeta_id`" ) !== false ) : ?> |
| | 445 | Created the <code><?php echo $wpdb->usermeta; ?></code> table and copied the user information from bbPress. |
| | 446 | <?php else : ?> |
| | 447 | There was a problem duplicating the table <code><?php echo $bbdb->usermeta; ?></code> to <code><?php echo $wpdb->usermeta; ?></code>. Please check and re-run the script or duplicate the table yourself.</li></ol> |
| | 448 | <?php break; endif; ?> |
| | 449 | </li> |
| | 450 | |
| | 451 | <?php |
| | 452 | |
| | 453 | // Map the user roles by our wish |
| | 454 | $roles_map = array( |
| | 455 | 'keymaster' => 'administrator', |
| | 456 | 'administrator' => 'bbp_moderator', |
| | 457 | 'moderator' => 'bbp_moderator', |
| | 458 | 'member' => get_option( 'default_role' ), |
| | 459 | 'inactive' => get_option( 'default_role' ), |
| | 460 | 'blocked' => get_option( 'default_role' ), |
| | 461 | 'throttle' => 'throttle' |
| | 462 | ); |
| | 463 | |
| | 464 | $wp_user_level_map = array( |
| | 465 | 'administrator' => 10, |
| | 466 | 'editor' => 7, |
| | 467 | 'author' => 2, |
| | 468 | 'contributor' => 1, |
| | 469 | 'subscriber' => 0, |
| | 470 | 'throttle' => 0 |
| | 471 | ); |
| | 472 | |
| | 473 | // Apply the WordPress roles to the new users based on their bbPress roles |
| | 474 | wp_cache_flush(); |
| | 475 | $users = get_users( array( 'fields' => 'all_with_meta', 'orderby' => 'ID' ) ); |
| | 476 | |
| | 477 | foreach ( $users as $user ) { |
| | 478 | |
| | 479 | // Get the bbPress roles |
| | 480 | $bb_roles =& $user->{ $bbdb->prefix . 'capabilities' }; |
| | 481 | $converted_roles = $converted_level = array(); |
| | 482 | |
| | 483 | // Loop through each role the user has |
| | 484 | foreach ( $bb_roles as $bb_role => $bb_role_value ) { |
| | 485 | |
| | 486 | // If we have one of those in our roles map, add the WP counterpart in the new roles array |
| | 487 | if ( $roles_map[strtolower( $bb_role )] && !empty( $bb_role_value ) ) { |
| | 488 | $converted_roles[$roles_map[strtolower( $bb_role )]] = true; |
| | 489 | |
| | 490 | // Have support for deprecated levels too |
| | 491 | $converted_level[] = $wp_user_level_map[$roles_map[strtolower( $bb_role )]]; |
| | 492 | |
| | 493 | // We need an admin for future use |
| | 494 | if ( empty( $admin_user ) && 'administrator' == $roles_map[strtolower( $bb_role )] ) |
| | 495 | $admin_user = $user; |
| | 496 | } |
| | 497 | |
| | 498 | } |
| | 499 | |
| | 500 | // If we have new roles, then update the user meta |
| | 501 | if ( count( $converted_roles ) ) { |
| | 502 | update_user_meta( $user->ID, $wpdb->prefix . 'capabilities', $converted_roles ); |
| | 503 | update_user_meta( $user->ID, $wpdb->prefix . 'user_level', max( $converted_level ) ); |
| | 504 | } |
| | 505 | |
| | 506 | } |
| | 507 | |
| | 508 | if ( empty( $admin_user ) || is_wp_error( $admin_user ) ) : /* I ask why */ ?> |
| | 509 | |
| | 510 | <li> |
| | 511 | There was a problem in getting an administrator of the blog. |
| | 512 | Now, please go to your blog, set a user as administrator, login as that user and directly go to <a href="bbs2p.php?mode=topics">converting forums</a>. |
| | 513 | Note that old logins won't work, you would have to edit your database (you can still try logging in as the bbPress keymaster). |
| | 514 | </li> |
| | 515 | |
| | 516 | </ol> |
| | 517 | |
| | 518 | <?php |
| | 519 | |
| | 520 | break; |
| | 521 | |
| | 522 | endif; |
| | 523 | |
| | 524 | // Logout the user as it won't have any good privileges for us to do the work |
| | 525 | wp_clear_auth_cookie(); |
| | 526 | |
| | 527 | // Login the admin so that we have permissions for conversion etc |
| | 528 | wp_set_auth_cookie( $admin_user->ID ); |
| | 529 | |
| | 530 | // Set the current user |
| | 531 | wp_set_current_user( $admin_user->ID, $admin_user->user_login ); |
| | 532 | |
| | 533 | ?> |
| | 534 | |
| | 535 | <li> |
| | 536 | User roles have been successfully mapped based. The bbPress keymaster is WordPress administrator, bbPress administrator and moderators are moderators and rest all WordPress roles are <?php echo get_option( 'default_role' ); ?>. |
| | 537 | Now, you can only login into your WordPress blog by the bbPress credentials. |
| | 538 | For the time being, you have been logged in as the first available administrator (Username: <?php echo $admin_user->user_login; ?>, User ID: <?php echo $admin_user->ID; ?>). |
| | 539 | </li> |
| | 540 | |
| | 541 | </ol> |
| | 542 | |
| | 543 | <p><?php _e( 'Your users have all been imported, but wait – there’s more! Now we need to import your forums, topics and replies!', 'bbpress' ); ?></p> |
| | 544 | <?php |
| | 545 | |
| | 546 | echo $this->next_step( 2, __( 'Import by forums, topics and replies »', 'bbpress' ) ); |
| | 547 | $this->auto_submit(); ?> |
| | 548 | |
| | 549 | </div> |
| | 550 | |
| | 551 | <?php |
| | 552 | } |
| | 553 | |
| | 554 | /** |
| | 555 | * Import forums, topics and replies |
| | 556 | */ |
| | 557 | function step2() { |
| | 558 | do_action( 'import_start' ); |
| | 559 | |
| | 560 | set_time_limit( 0 ); |
| | 561 | update_option( 'bbp_bbpress_step', 2 ); |
| | 562 | $this->bb_path = get_option( 'bbp_bbpress_path' ); ?> |
| | 563 | |
| | 564 | <div id="bbpress-import-status"> |
| | 565 | |
| | 566 | <h3><?php _e( 'Importing Forums, Topics & Replies', 'bbpress' ); ?></h3>'; |
| | 567 | <p><?php _e( 'We’re importing your bbPress standalone forums, topics and replies...', 'bbpress' ); ?></p> |
| | 568 | |
| | 569 | |
| | 570 | <ol> |
| | 571 | |
| | 572 | <?php |
| | 573 | |
| | 574 | if ( !$forums = bb_get_forums() ) { |
| | 575 | echo "<li><strong>No forums were found!</strong></li></ol>\n"; |
| | 576 | break; |
| | 577 | } |
| | 578 | |
| | 579 | if ( $this->debug == true ) |
| | 580 | echo "<li>Total number of forums: " . count( $forums ) . "</li>\n"; |
| | 581 | |
| | 582 | $forum_map = array(); |
| | 583 | $post_statuses = array( 'publish', $bbp->trash_status_id, $bbp->spam_status_id ); |
| | 584 | |
| | 585 | foreach ( (array) $forums as $forum ) { |
| | 586 | echo "<li>Processing forum #$forum->forum_id (<a href='" . get_forum_link( $forum->forum_id ) . "'>" . esc_html( $forum->forum_name ) . "</a>) \n<ul>\n"; |
| | 587 | |
| | 588 | // Insert the forum and add it to the map. |
| | 589 | $inserted_forum = wp_insert_post( array( |
| | 590 | 'post_author' => get_current_user_id(), |
| | 591 | 'post_content' => $forum->forum_desc, |
| | 592 | 'post_title' => $forum->forum_name, |
| | 593 | 'post_excerpt' => '', |
| | 594 | 'post_status' => 'publish', |
| | 595 | 'comment_status' => 'closed', |
| | 596 | 'ping_status' => 'closed', |
| | 597 | 'post_name' => $forum->forum_slug, |
| | 598 | 'post_parent' => !empty( $forum->forum_parent ) ? $forum_map[$forum->forum_parent] : 0, |
| | 599 | 'post_type' => bbp_get_forum_post_type(), |
| | 600 | 'menu_order' => $forum->forum_order |
| | 601 | ) ); |
| | 602 | |
| | 603 | $forum_map[$forum->forum_id] = $inserted_forum; |
| | 604 | |
| | 605 | if ( !empty( $inserted_forum ) && !is_wp_error( $inserted_forum ) ) { |
| | 606 | echo "<li>Added the forum as forum #<a href='" . get_permalink( $inserted_forum ) . "'>$inserted_forum</a></li>\n"; |
| | 607 | } else { |
| | 608 | echo "<li><em>There was a problem in adding the forum.</em></li></ul></li>\n"; |
| | 609 | continue; |
| | 610 | } |
| | 611 | |
| | 612 | $topics_query = new BB_Query( 'topic', array( |
| | 613 | 'forum_id' => $forum->forum_id, |
| | 614 | 'per_page' => -1, |
| | 615 | 'topic_status' => 'all' |
| | 616 | ) ); |
| | 617 | |
| | 618 | $topics = $topics_query->results; |
| | 619 | |
| | 620 | // In standalone, categories can have topics, but this is not the case in plugin |
| | 621 | // So make the forum category if it doesn't have topics |
| | 622 | // Else close it if it's a category and has topics |
| | 623 | if ( bb_get_forum_is_category( $forum->forum_id ) ) { |
| | 624 | |
| | 625 | if ( count( $topics ) == 0 ) { |
| | 626 | bbp_categorize_forum( $inserted_forum ); |
| | 627 | echo "<li>The forum is a category and has no topics.</li>\n</ul>\n</li>"; |
| | 628 | |
| | 629 | continue; |
| | 630 | } else { |
| | 631 | bbp_close_forum( $inserted_forum ); |
| | 632 | echo "<li>The forum is a category but has topics, so it has been set as closed on the new board.</li>\n"; |
| | 633 | } |
| | 634 | |
| | 635 | } |
| | 636 | |
| | 637 | bb_cache_first_posts( $topics ); |
| | 638 | |
| | 639 | if ( $this->debug == true ) |
| | 640 | echo "<li>Total number of topics in the forum: " . count( $topics ) . "</li>\n"; |
| | 641 | |
| | 642 | foreach ( (array) $topics as $topic ) { |
| | 643 | $first_post = bb_get_first_post( $topic->topic_id ); |
| | 644 | |
| | 645 | // If the topic is public, check if it's open and set the status accordingly |
| | 646 | $topic_status = $topic->topic_status == 0 ? ( $topic->topic_open == 0 ? $bbp->closed_status_id : $post_statuses[$topic->topic_status] ) : $post_statuses[$topic->topic_status]; |
| | 647 | |
| | 648 | $inserted_topic = wp_insert_post( array( |
| | 649 | 'post_parent' => $inserted_forum, |
| | 650 | 'post_author' => $topic->topic_poster, |
| | 651 | 'post_content' => $first_post->post_text, |
| | 652 | 'post_title' => $topic->topic_title, |
| | 653 | 'post_name' => $topic->topic_slug, |
| | 654 | 'post_status' => $topic_status, |
| | 655 | 'post_date_gmt' => $topic->topic_start_time, |
| | 656 | 'post_date' => get_date_from_gmt( $topic->topic_start_time ), |
| | 657 | 'post_type' => bbp_get_topic_post_type(), |
| | 658 | 'tax_input' => array( 'topic-tag' => array_map( 'bbs2p_get_tag_name', bb_get_public_tags( $topic->topic_id ) ) ) |
| | 659 | ) ); |
| | 660 | |
| | 661 | if ( !empty( $inserted_topic ) && !is_wp_error( $inserted_topic ) ) { |
| | 662 | |
| | 663 | // Loginless Posting |
| | 664 | if ( $topic->topic_poster == 0 ) { |
| | 665 | update_post_meta( $inserted_topic, '_bbp_anonymous_name', bb_get_post_meta( 'post_author', $first_post->post_id ) ); |
| | 666 | update_post_meta( $inserted_topic, '_bbp_anonymous_email', bb_get_post_meta( 'post_email', $first_post->post_id ) ); |
| | 667 | update_post_meta( $inserted_topic, '_bbp_anonymous_website', bb_get_post_meta( 'post_url', $first_post->post_id ) ); |
| | 668 | } |
| | 669 | |
| | 670 | // Author IP |
| | 671 | update_post_meta( $inserted_topic, '_bbp_author_ip', $first_post->poster_ip ); |
| | 672 | |
| | 673 | // Forum topic meta |
| | 674 | update_post_meta( $inserted_topic, '_bbp_forum_id', $inserted_forum ); |
| | 675 | update_post_meta( $inserted_topic, '_bbp_topic_id', $inserted_topic ); |
| | 676 | |
| | 677 | $posts = bb_cache_posts( $bbdb->prepare( 'SELECT * FROM ' . $bbdb->posts . ' WHERE topic_id = %d AND post_id != %d ORDER BY post_time', $topic->topic_id, $first_post->post_id ) ); |
| | 678 | |
| | 679 | $replies = 0; |
| | 680 | $hidden_replies = 0; |
| | 681 | $last_reply = 0; |
| | 682 | $post = null; |
| | 683 | |
| | 684 | foreach ( (array) $posts as $post ) { |
| | 685 | |
| | 686 | // Pingback |
| | 687 | if ( $post->poster_id == 0 && $pingback_uri = bb_get_post_meta( 'pingback_uri', $post_id ) ) { |
| | 688 | $pingback = wp_insert_comment( wp_filter_comment( array( |
| | 689 | 'comment_post_ID' => $inserted_topic, |
| | 690 | 'comment_author' => bb_get_post_meta( 'pingback_title', $post->post_id ), |
| | 691 | 'comment_author_url' => $pingback_uri, |
| | 692 | 'comment_author_IP' => $post->poster_ip, |
| | 693 | 'comment_date_gmt' => $post->post_time, |
| | 694 | 'comment_date' => get_date_from_gmt( $post->post_time ), |
| | 695 | 'comment_content' => $post->post_text, |
| | 696 | 'comment_approved' => $post->post_status == 0 ? 1 : ( $post->post_status == 2 ? 'spam' : 0 ), |
| | 697 | 'comment_type' => 'pingback' |
| | 698 | ) ) ); |
| | 699 | |
| | 700 | // Normal post |
| | 701 | } else { |
| | 702 | $reply_title = sprintf( __( 'Reply To: %s', 'bbpress' ), $topic->topic_title ); |
| | 703 | |
| | 704 | $last_reply = wp_insert_post( array( |
| | 705 | 'post_parent' => $inserted_topic, |
| | 706 | 'post_author' => $post->poster_id, |
| | 707 | 'post_date_gmt' => $post->post_time, |
| | 708 | 'post_date' => get_date_from_gmt( $post->post_time ), |
| | 709 | 'post_title' => $reply_title, |
| | 710 | 'post_name' => sanitize_title_with_dashes( $reply_text ), |
| | 711 | 'post_status' => $post_statuses[$post->post_status], |
| | 712 | 'post_type' => bbp_get_reply_post_type(), |
| | 713 | 'post_content' => $post->post_text |
| | 714 | ) ); |
| | 715 | |
| | 716 | // Loginless |
| | 717 | if ( $post->poster_id == 0 ) { |
| | 718 | update_post_meta( $last_reply, '_bbp_anonymous_name', bb_get_post_meta( 'post_author', $post->post_id ) ); |
| | 719 | update_post_meta( $last_reply, '_bbp_anonymous_email', bb_get_post_meta( 'post_email', $post->post_id ) ); |
| | 720 | update_post_meta( $last_reply, '_bbp_anonymous_website', bb_get_post_meta( 'post_url', $post->post_id ) ); |
| | 721 | } |
| | 722 | |
| | 723 | // Author IP |
| | 724 | update_post_meta( $last_reply, '_bbp_author_ip', $post->poster_ip ); |
| | 725 | |
| | 726 | // Reply Parents |
| | 727 | update_post_meta( $last_reply, '_bbp_forum_id', $inserted_forum ); |
| | 728 | update_post_meta( $last_reply, '_bbp_topic_id', $inserted_topic ); |
| | 729 | |
| | 730 | bbp_update_reply_walker( $last_reply ); |
| | 731 | } |
| | 732 | |
| | 733 | $replies++; |
| | 734 | |
| | 735 | if ( $post->post_status != 0 ) |
| | 736 | $hidden_replies++; |
| | 737 | } |
| | 738 | |
| | 739 | // Only add favorites and subscriptions if the topic is public |
| | 740 | if ( in_array( $topic_status, array( 'publish', $bbp->closed_status_id ) ) ) { |
| | 741 | |
| | 742 | // Favorites |
| | 743 | foreach ( (array) bbs2p_bb_get_topic_favoriters( $topic->topic_id ) as $favoriter ) |
| | 744 | bbp_add_user_favorite ( $favoriter, $inserted_topic ); |
| | 745 | |
| | 746 | // Subscriptions |
| | 747 | foreach ( (array) bbs2p_bb_get_topic_subscribers( $topic->topic_id ) as $subscriber ) |
| | 748 | bbp_add_user_subscription( $subscriber, $inserted_topic ); |
| | 749 | } |
| | 750 | |
| | 751 | // Topic stickiness |
| | 752 | switch ( $topic->topic_sticky ) { |
| | 753 | |
| | 754 | // Forum |
| | 755 | case 1 : |
| | 756 | bbp_stick_topic( $inserted_topic ); |
| | 757 | break; |
| | 758 | |
| | 759 | // Front |
| | 760 | case 2 : |
| | 761 | bbp_stick_topic( $inserted_topic, true ); |
| | 762 | break; |
| | 763 | } |
| | 764 | |
| | 765 | // Last active time |
| | 766 | $last_active = $post ? $post->post_time : $first_post->post_time; |
| | 767 | |
| | 768 | // Reply topic meta |
| | 769 | update_post_meta( $inserted_topic, '_bbp_last_reply_id', $last_reply ); |
| | 770 | update_post_meta( $inserted_topic, '_bbp_last_active_id', $last_reply ? $last_reply : $inserted_topic ); |
| | 771 | update_post_meta( $inserted_topic, '_bbp_last_active_time', $topic->topic_time ); |
| | 772 | update_post_meta( $inserted_topic, '_bbp_reply_count', $replies - $hidden_replies ); |
| | 773 | update_post_meta( $inserted_topic, '_bbp_hidden_reply_count', $hidden_replies ); |
| | 774 | // Voices will be done by recount |
| | 775 | |
| | 776 | bbp_update_topic_walker( $inserted_topic ); |
| | 777 | |
| | 778 | echo "<li>Added topic #{$topic->topic_id} (<a href='" . get_topic_link( $topic->topic_id ) . "'>" . esc_html( $topic->topic_title ) . "</a>) as topic #<a href='" . get_permalink( $inserted_topic ) . "'>$inserted_topic</a> with $replies replies.</li>\n"; |
| | 779 | } else { |
| | 780 | echo "<li><em>There was a problem in adding topic #{$topic->topic_id} (<a href='" . get_topic_link( $topic->topic_id ) . "'>" . esc_html( $topic->topic_title ) . "</a>).</em></li></ul></li>\n"; |
| | 781 | continue; |
| | 782 | } |
| | 783 | } |
| | 784 | |
| | 785 | echo "</ul>\n</li>\n"; |
| | 786 | |
| | 787 | } ?> |
| | 788 | |
| | 789 | </ol> |
| | 790 | |
| | 791 | <?php |
| | 792 | |
| | 793 | // Clean up database and we're out |
| | 794 | $this->cleanup(); |
| | 795 | do_action( 'import_done', 'bbpress' ); |
| | 796 | |
| | 797 | ?> |
| | 798 | |
| | 799 | <p><?php printf( __( 'Your forums, topics and replies have all been imported, but wait – there’s more! Now we need to do a <a href="%s">recount</a> to get the counts in sync! Yes, we’re bad at Math.', 'bbpress' ), add_query_arg( array( 'page' => 'bbp-recount' ), get_admin_url( 0, 'tools.php' ) ) ); ?></p> |
| | 800 | |
| | 801 | <h3><?php printf( __( 'After that it\'s all done. <a href="%s">Have fun!</a> :)', 'bbpress' ), home_url() ); ?></h3> |
| | 802 | |
| | 803 | </div> |
| | 804 | |
| | 805 | <?php |
| | 806 | } |
| | 807 | |
| | 808 | } // class bbPress_Importer |
| | 809 | |
| | 810 | $bbpress_import = new bbPress_Importer(); |
| | 811 | |
| | 812 | register_importer( 'bbpress', __( 'bbPress Standalone', 'bbpress' ), __( 'Import your bbPress standalone board.', 'bbpress' ), array( $bbpress_import, 'dispatch' ) ); |
| | 813 | |
| | 814 | ?> |