PluginProbe
bbPress / 2.0-beta-3b
bbPress v2.0-beta-3b
2.6.19 2.6.18 2.6.17 trunk 2.0 2.0-beta-1 2.0-beta-2b 2.0-beta-3 2.0-beta-3b 2.0-rc-2 2.0-rc-3 2.0-rc-4 2.0-rc-5 2.0.1 2.0.2 2.0.3 2.1 2.1-beta-1 2.1-rc1 2.1-rc2 2.1-rc3 2.1-rc4 2.1.1 2.1.2 2.1.3 All 74 releases
bbpress / bbp-admin / importers / bbpress.php

bbpress.php in bbPress 2.0-beta-3b, at bbp-admin/importers/bbpress.php

1,117 lines 38.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * bbPress needs this class for its usermeta manipulation.
5 */
6 class bbPress_Importer_BB_Auth {
7 function update_meta( $args = '' ) {
8 $defaults = array( 'id' => 0, 'meta_key' => null, 'meta_value' => null, 'meta_table' => 'usermeta', 'meta_field' => 'user_id', 'cache_group' => 'users' );
9 $args = wp_parse_args( $args, $defaults );
10 extract( $args, EXTR_SKIP );
11
12 return update_user_meta( $id, $meta_key, $meta_value );
13 }
14 }
15
16 if ( !class_exists( 'BPDB' ) ) :
17
18 /**
19 * bbPress needs the DB class to be BPDB, but we want to use WPDB, so we can
20 * extend it and use this.
21 */
22 class BPDB extends WPDB {
23 var $db_servers = array();
24
25 function BPDB( $dbuser, $dbpassword, $dbname, $dbhost ) {
26 $this->__construct( $dbuser, $dbpassword, $dbname, $dbhost );
27 }
28
29 function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) {
30 parent::__construct( $dbuser, $dbpassword, $dbname, $dbhost );
31
32 $args = func_get_args();
33 $args = call_user_func_array( array( &$this, '_init' ), $args );
34
35 if ( $args['host'] )
36 $this->db_servers['dbh_global'] = $args;
37 }
38
39 /**
40 * Determine if a database supports a particular feature.
41 *
42 * Overriden here to work around differences between bbPress', and WordPress', implementation differences.
43 * In particular, when BuddyPress tries to run bbPress' SQL installation script, the collation check always
44 * failed. The capability is long supported by WordPress' minimum required MySQL version, so this is safe.
45 */
46 function has_cap( $db_cap, $_table_name='' ) {
47 if ( 'collation' == $db_cap )
48 return true;
49
50 return parent::has_cap( $db_cap );
51 }
52
53 /**
54 * Initialises the class variables based on provided arguments.
55 * Based on, and taken from, the BackPress class in turn taken from the 1.0 branch of bbPress.
56 */
57 function _init( $args )
58 {
59 if ( 4 == func_num_args() ) {
60 $args = array(
61 'user' => $args,
62 'password' => func_get_arg( 1 ),
63 'name' => func_get_arg( 2 ),
64 'host' => func_get_arg( 3 ),
65 'charset' => defined( 'BBDB_CHARSET' ) ? BBDB_CHARSET : false,
66 'collate' => defined( 'BBDB_COLLATE' ) ? BBDB_COLLATE : false,
67 );
68 }
69
70 $defaults = array(
71 'user' => false,
72 'password' => false,
73 'name' => false,
74 'host' => 'localhost',
75 'charset' => false,
76 'collate' => false,
77 'errors' => false
78 );
79
80 return wp_parse_args( $args, $defaults );
81 }
82
83 function escape_deep( $data ) {
84 if ( is_array( $data ) ) {
85 foreach ( (array) $data as $k => $v ) {
86 if ( is_array( $v ) ) {
87 $data[$k] = $this->_escape( $v );
88 } else {
89 $data[$k] = $this->_real_escape( $v );
90 }
91 }
92 } else {
93 $data = $this->_real_escape( $data );
94 }
95
96 return $data;
97 }
98 }
99
100 endif; // class_exists
101
102 if ( !function_exists( 'bb_cache_users' ) ) :
103 function bb_cache_users( $users ) { }
104 endif;
105
106 /**
107 * bbPress Standalone Importer
108 *
109 * Helps in converting your bbPress Standalone into the new bbPress Plugin
110 *
111 * @package bbPress
112 * @subpackage Importer
113 *
114 * @todo Docs
115 * @todo User Mapping (ref. MT Importer)
116 * @todo Role Mapping Options
117 */
118 class bbPress_Importer {
119
120 /**
121 * @var string Path to bbPress standalone configuration file (bb-config.php)
122 */
123 var $bbconfig = '';
124
125 /**
126 * Load the bbPress environment
127 */
128 function load_bbpress() {
129
130 // BuddyPress Install
131 if ( defined( 'BP_VERSION' ) && bp_is_active( 'forums' ) && bp_forums_is_installed_correctly() ) {
132 global $bp;
133
134 if ( !empty( $bp->forums->bbconfig ) && ( $bp->forums->bbconfig == $this->bbconfig ) )
135 bp_forums_load_bbpress();
136 }
137
138 global $wpdb, $wp_roles, $current_user, $wp_users_object, $wp_taxonomy_object;
139 global $bb, $bbdb, $bb_table_prefix, $bb_current_user, $bb_roles, $bb_queries;
140
141 // Return if we've already run this function or it is BuddyPress
142 if ( is_object( $bbdb ) )
143 return;
144
145 // Config file does not exist
146 if ( !file_exists( $this->bbconfig ) )
147 return false;
148
149 // Set the path constants
150 define( 'BB_PATH', trailingslashit( dirname( $this->bbconfig ) ) );
151 define( 'BACKPRESS_PATH', BB_PATH . 'bb-includes/backpress/' );
152 define( 'BB_INC', 'bb-includes/' );
153
154 require_once( BB_PATH . BB_INC . 'class.bb-query.php' );
155 require_once( BB_PATH . BB_INC . 'class.bb-walker.php' );
156 require_once( BB_PATH . BB_INC . 'functions.bb-core.php' );
157 require_once( BB_PATH . BB_INC . 'functions.bb-forums.php' );
158 require_once( BB_PATH . BB_INC . 'functions.bb-topics.php' );
159 require_once( BB_PATH . BB_INC . 'functions.bb-posts.php' );
160 require_once( BB_PATH . BB_INC . 'functions.bb-topic-tags.php' );
161 require_once( BB_PATH . BB_INC . 'functions.bb-capabilities.php' );
162 require_once( BB_PATH . BB_INC . 'functions.bb-meta.php' );
163 require_once( BB_PATH . BB_INC . 'functions.bb-pluggable.php' );
164 require_once( BB_PATH . BB_INC . 'functions.bb-formatting.php' );
165 require_once( BB_PATH . BB_INC . 'functions.bb-template.php' );
166
167 require_once( BACKPRESS_PATH . 'class.wp-taxonomy.php' );
168 require_once( BB_PATH . BB_INC . 'class.bb-taxonomy.php' );
169
170 $bb = new stdClass();
171 require_once( $this->bbconfig );
172
173 // Setup the global database connection
174 $bbdb = new BPDB( BBDB_USER, BBDB_PASSWORD, BBDB_NAME, BBDB_HOST );
175
176 // Set the table names
177 $bbdb->forums = $bb_table_prefix . 'forums';
178 $bbdb->meta = $bb_table_prefix . 'meta';
179 $bbdb->posts = $bb_table_prefix . 'posts';
180 $bbdb->terms = $bb_table_prefix . 'terms';
181 $bbdb->term_relationships = $bb_table_prefix . 'term_relationships';
182 $bbdb->term_taxonomy = $bb_table_prefix . 'term_taxonomy';
183 $bbdb->topics = $bb_table_prefix . 'topics';
184
185 // Users table
186 if ( isset( $bb->custom_user_table ) )
187 $bbdb->users = $bb->custom_user_table;
188 else
189 $bbdb->users = $bb_table_prefix . 'users';
190
191 // Users meta table
192 if ( isset( $bb->custom_user_meta_table ) )
193 $bbdb->usermeta = $bb->custom_user_meta_table;
194 else
195 $bbdb->usermeta = $bb_table_prefix . 'usermeta';
196
197 // Table prefix
198 $bbdb->prefix = $bb_table_prefix;
199
200 // Not installing
201 define( 'BB_INSTALLING', false );
202
203 // Ghetto role map
204 if ( is_object( $wp_roles ) ) {
205 $bb_roles = $wp_roles;
206 bb_init_roles( $bb_roles );
207 }
208
209 // Call the standard bbPress actions
210 do_action( 'bb_got_roles' );
211 do_action( 'bb_init' );
212 do_action( 'init_roles' );
213
214 // Setup required objects
215 $bb_current_user = $current_user;
216 $wp_users_object = new bbPress_Importer_BB_Auth;
217
218 // Taxonomy object
219 if ( !isset( $wp_taxonomy_object ) )
220 $wp_taxonomy_object = new BB_Taxonomy( $bbdb );
221
222 $wp_taxonomy_object->register_taxonomy( 'bb_topic_tag', 'bb_topic' );
223 }
224
225 /**
226 * Returns the tag name from tag object
227 *
228 * @param object $tag Tag Object
229 * @return string Tag name
230 */
231 function get_tag_name( $tag ) {
232 return $tag->name;
233 }
234
235 /**
236 * Simple check to check if the WP and bb user tables are integrated or not
237 *
238 * @return bool True if integrated, false if not
239 */
240 function is_integrated() {
241 global $bbdb, $wpdb;
242
243 return ( $wpdb->users == $bbdb->users && $wpdb->usermeta == $bbdb->usermeta );
244 }
245
246 /**
247 * Tries to automatically locate the bbPress standalone install path
248 *
249 * @return string Path, if found
250 */
251 function autolocate_bbconfig() {
252
253 // BuddyPress Install
254 if ( defined( 'BP_VERSION' ) && bp_is_active( 'forums' ) && bp_forums_is_installed_correctly() ) {
255 global $bp;
256
257 if ( !empty( $bp->forums->bbconfig ) )
258 return $bp->forums->bbconfig;
259 }
260
261 // Normal install
262 $dirs = array( 'forum', 'forums', 'board', 'discussion', 'bbpress', 'bb', '' );
263 $base = trailingslashit( ABSPATH );
264 $base_dirs = array( $base, dirname( $base ) );
265
266 // Loop through possible directories
267 foreach ( $dirs as $dir ) {
268
269 // Loop through base dir
270 foreach ( $base_dirs as $base_dir ) {
271
272 // Path to try
273 $test_path = $base_dir . $dir . '/bb-config.php';
274
275 // File exists
276 if ( file_exists( $test_path ) ) {
277 return realpath( $test_path );
278 }
279 }
280 }
281
282 // Nothing found
283 return '';
284 }
285
286 /**
287 * Get the bbPress standalone topic favoriters from topic id
288 *
289 * @param int $topic_id Topic id
290 * @return array Topic Favoriters' IDs
291 */
292 function bb_get_topic_favoriters( $topic_id = 0 ) {
293 if ( empty( $topic_id ) )
294 return array();
295
296 global $bbdb;
297
298 return (array) $bbdb->get_col( $bbdb->prepare( "SELECT user_id
299 FROM $bbdb->usermeta
300 WHERE meta_key = '{$bbdb->prefix}favorites'
301 AND FIND_IN_SET( %d, meta_value ) > 0",
302 $topic_id ) );
303 }
304
305 /**
306 * Get the bbPress standalone topic subscribers from topic id
307 *
308 * If the Subscribe to Topic bbPress plugin is active, then subscription
309 * info is taken from that. Otherwise, if the the user is using
310 * bbPress >= 1.1 alpha, then get the info from there.
311 *
312 * @param int $topic_id Topic id
313 * @return array Topic Subscribers' IDs
314 */
315 function bb_get_topic_subscribers( $topic_id = 0 ) {
316 if ( empty( $topic_id ) )
317 return array();
318
319 global $bbdb, $subscribe_to_topic;
320
321 $users = array();
322
323 // The user is using Subscribe to Topic plugin by _ck_, get the subscribers from there
324 if ( !empty( $subscribe_to_topic ) && !empty( $subscribe_to_topic['db'] ) ) {
325 $users = $bbdb->get_col( $bbdb->prepare( "SELECT user
326 FROM {$subscribe_to_topic['db']}
327 WHERE topic = %d
328 AND type = 2", $topic_id ) );
329
330 // The user is using alpha, get the subscribers from built-in functionality
331 } elseif ( function_exists( 'bb_notify_subscribers' ) ) {
332 $users = $bbdb->get_col( $bbdb->prepare( "SELECT `$bbdb->term_relationships`.`object_id`
333 FROM $bbdb->term_relationships, $bbdb->term_taxonomy, $bbdb->terms
334 WHERE `$bbdb->term_relationships`.`term_taxonomy_id` = `$bbdb->term_taxonomy`.`term_taxonomy_id`
335 AND `$bbdb->term_taxonomy`.`term_id` = `$bbdb->terms`.`term_id`
336 AND `$bbdb->term_taxonomy`.`taxonomy` = 'bb_subscribe'
337 AND `$bbdb->terms`.`slug` = 'topic-%d'",
338 $topic_id ) );
339 }
340
341 return (array) $users;
342 }
343
344 function header() { ?>
345
346 <div class="wrap">
347
348 <?php screen_icon(); ?>
349
350 <h2><?php _e( 'bbPress Standalone Importer', 'bbpress' ); ?></h2>
351
352 <?php
353 }
354
355 /**
356 * Output an error message with a button to try again.
357 *
358 * @param type $error
359 * @param type $step
360 */
361 function throw_error( $error, $step ) {
362 echo '<p><strong>' . $error->get_error_message() . '</strong></p>';
363 echo $this->next_step( $step, __( 'Try Again', 'bbpress' ) );
364 }
365
366 /**
367 * Returns the HTML for a link to the next page
368 *
369 * @param type $next_step
370 * @param type $label
371 * @param type $id
372 * @return string
373 */
374 function next_step( $next_step, $label, $id = 'bbpress-import-next-form' ) {
375 $str = '<form action="admin.php?import=bbpress" method="post" id="' . $id . '">';
376 $str .= wp_nonce_field( 'bbp-bbpress-import', '_wpnonce', true, false );
377 $str .= wp_referer_field( false );
378 $str .= '<input type="hidden" name="step" id="step" value="' . esc_attr( $next_step ) . '" />';
379 $str .= '<p><input type="submit" class="button" value="' . esc_attr( $label ) . '" /> <span id="auto-message"></span></p>';
380 $str .= '</form>';
381
382 return $str;
383 }
384
385 /**
386 * Footer
387 */
388 function footer() { ?>
389
390 </div>
391
392 <?php
393 }
394
395 /**
396 * Dispatch
397 */
398 function dispatch() {
399 if ( empty( $_REQUEST['step'] ) )
400 $step = 0;
401 else
402 $step = (int) $_REQUEST['step'];
403
404 $this->header();
405
406 switch ( $step ) {
407 case -1 :
408 $this->cleanup();
409 // Intentional no break
410
411 case 0 :
412 $this->greet();
413 break;
414
415 case 1 :
416 case 2 :
417 case 3 :
418
419 check_admin_referer( 'bbp-bbpress-import' );
420 $result = $this->{ 'step' . $step }();
421
422 break;
423 }
424
425 $this->footer();
426 }
427
428 /**
429 * Greet message
430 */
431 function greet() {
432 global $wpdb, $bbdb;
433
434 // Attempt to autolocate config file
435 $autolocate = $this->autolocate_bbconfig(); ?>
436
437 <div class="narrow">
438
439 <form action="admin.php?import=bbpress" method="post">
440
441 <?php wp_nonce_field( 'bbp-bbpress-import' ); ?>
442
443 <?php if ( get_option( 'bbp_bbpress_path' ) ) : ?>
444
445 <input type="hidden" name="step" value="<?php echo esc_attr( get_option( 'bbp_bbpress_step' ) ); ?>" />
446
447 <p><?php _e( 'It looks like you attempted to convert your bbPress standalone previously and got interrupted.', 'bbpress' ); ?></p>
448
449 <p class="submit">
450 <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="button"><?php _e( 'Cancel &amp; start a new import', 'bbpress' ); ?></a>
451 <input type="submit" class="button-primary" value="<?php esc_attr_e( 'Continue previous import', 'bbpress' ); ?>" />
452 </p>
453
454 <?php else : ?>
455
456 <?php if ( !empty( $autolocate ) ) : ?>
457
458 <div id="message" class="updated">
459 <p><?php _e( 'Existing bbPress standalone installation found. See configuration section for details.', 'bbpress' ); ?></p>
460 </div>
461
462 <?php endif; ?>
463
464 <input type="hidden" name="bbp_bbpress_path" value="true" />
465
466 <p><?php _e( 'This importer allows you to convert your bbPress Standalone into the bbPress Plugin.', 'bbpress' ); ?></p>
467
468 <h3><?php _e( 'Instructions', 'bbpress' ); ?></h3>
469 <ol>
470 <li><?php printf( __( 'Create a <a href="%s">backup</a> of your database and files. If the import process is interrupted for any reason, restore from that backup and re-run the import.', 'bbpress' ), 'http://codex.wordpress.org/WordPress_Backups' ); ?></li>
471 <li><?php _e( 'Seriously... Go back everything up, and don\'t come back until that\'s done. If things go awry, it\'s possible this importer will not be able to complete and your forums will be lost in limbo forever. This is serious business. No, we\'re not kidding.', 'bbpress' ); ?></li>
472 <li><?php _e( 'To reduce memory overhead and avoid possible conflicts please:', 'bbpress' ); ?>
473 <ol>
474 <li>
475 <?php _e( 'Disable all plugins (except bbPress) on both your WordPress and bbPress standalone installations.', 'bbpress' ); ?>
476 </li>
477 <li>
478 <?php _e( 'Switch to a default WordPress theme.', 'bbpress' ); ?>
479 </li>
480 </ol>
481 </li>
482 <li><?php _e( 'Notes on compatibility:', 'bbpress' ); ?>
483 <ol>
484 <li>
485 <?php _e( 'If you are using the alpha version of bbPress 1.1, subscriptions will be ported automatically.', 'bbpress' ); ?>
486 </li>
487 <li>
488 <?php printf( __( 'If you have the <a href="%s">Subscribe to Topic</a> plugin active, then this script will migrate user subscriptions from that plugin.', 'bbpress' ), 'http://bbpress.org/plugins/topic/subscribe-to-topic/' ); ?>
489 </li>
490 <li>
491 <?php printf( __( 'If you are importing an existing BuddyPress Forums installation, we should have found your previous configuration file.', 'bbpress' ), 'http://bbpress.org/plugins/topic/subscribe-to-topic/' ); ?>
492 </li>
493 </ol>
494 </li>
495 <li><?php _e( 'This converter can be a drag on large forums with lots of existing topics and replies. If possible, do this import in a safe place (like a local installation.)', 'bbpress' ); ?></li>
496 </ol>
497
498 <h3><?php _e( 'Configuration', 'bbpress' ); ?></h3>
499 <p><?php _e( 'Enter the full path to your bbPress configuration file i.e. <code>bb-config.php</code>:', 'bbpress' ); ?></p>
500
501 <table class="form-table">
502 <tr>
503 <th scope="row"><label for="bbp_bbpress_path"><?php _e( 'bbPress Standalone Path:', 'bbpress' ); ?></label></th>
504 <td><input type="text" name="bbp_bbpress_path" id="bbp_bbpress_path" class="regular-text" value="<?php echo !empty( $autolocate ) ? $autolocate : trailingslashit( ABSPATH ) . 'bb-config.php'; ?>" /></td>
505 </tr>
506 </table>
507
508 <p class="submit">
509 <input type="hidden" name="step" value="1" />
510 <input type="submit" class="button" value="<?php esc_attr_e( 'Proceed', 'bbpress' ); ?>" />
511 </p>
512
513 <?php endif; ?>
514
515 </form>
516
517 </div>
518 <?php
519 }
520
521 /**
522 * Cleanups all the options used by the importer
523 */
524 function cleanup() {
525 delete_option( 'bbp_bbpress_path' );
526 delete_option( 'bbp_bbpress_step' );
527
528 do_action( 'import_end' );
529 }
530
531 /**
532 * Technically the first half of step 1, this is separated to allow for AJAX
533 * calls. Sets up some variables and options and confirms authentication.
534 *
535 * @return type
536 */
537 function setup() {
538
539 // Get details from _POST
540 if ( !empty( $_POST['bbp_bbpress_path'] ) ) {
541
542 // Store details for later
543 $this->bbconfig = realpath( $_POST['bbp_bbpress_path'] );
544
545 // Update path
546 update_option( 'bbp_bbpress_path', $this->bbconfig );
547
548 // Get details from DB
549 } else {
550 $this->bbconfig = get_option( 'bbp_bbpress_path' );
551 }
552
553 // No config file found
554 if ( empty( $this->bbconfig ) ) { ?>
555
556 <p><?php _e( 'Please enter the path to your bbPress configuration file - <code>bb-config.php</code>.', 'bbpress' ); ?></p>
557 <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'] ) ) ) ) ); ?>" class="button"><?php _e( 'Go Back', 'bbpress' ); ?></a></p>
558
559 <?php
560
561 return false;
562 }
563
564 // Check if the user submitted a directory as the path by mistake
565 if ( is_dir( $this->bbconfig ) && file_exists( trailingslashit( $this->bbconfig ) . 'bb-config.php' ) ) {
566 $this->bbconfig = trailingslashit( $this->bbconfig ) . 'bb-config.php';
567 }
568
569 // Check if the file exists
570 if ( !file_exists( $this->bbconfig ) || is_dir( $this->bbconfig ) ) {
571
572 delete_option( 'bbp_bbpress_path' ); ?>
573
574 <p><?php _e( 'bbPress configuration file <code>bb-config.php</code> doesn\'t exist in the path specified! Please check the path and try again.', 'bbpress' ); ?></p>
575 <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'] ) ) ) ) ); ?>" class="button"><?php _e( 'Go Back', 'bbpress' ); ?></a></p>
576
577 <?php
578
579 return false;
580 }
581
582 $this->load_bbpress();
583
584 remove_filter( 'pre_post_status', 'bb_bozo_pre_post_status', 5, 3 );
585
586 return true;
587 }
588
589 /**
590 * Notes & User Options
591 */
592 function step1() {
593
594 update_option( 'bbp_bbpress_step', 1 );
595
596 $setup = $this->setup();
597 if ( empty( $setup ) ) {
598 return false;
599 } elseif ( is_wp_error( $setup ) ) {
600 $this->throw_error( $setup, 1 );
601 return false;
602 }
603
604 $radio = 'user';
605
606 global $wpdb, $bbdb; ?>
607
608 <h3><?php _e( 'Configuration Options', 'bbpress' ); ?></h3>
609
610 <form action="admin.php?import=bbpress" method="post">
611
612 <?php if ( $this->is_integrated() ) : $radio = 'board'; ?>
613
614 <p><?php _e( '<strong>Auto-detected</strong>: Your WordPress and bbPress user tables are integrated. Proceed to <label for="step_board">importing forums, topics and posts</label>.', 'bbpress' ); ?></p>
615
616 <?php else : ?>
617
618 <ol>
619
620 <li>
621 <?php _e( 'Your WordPress blog is <strong>new</strong> and you don\'t have the fear of losing WordPress users:', 'bbpress' ); ?>
622 <label for="step_user"><?php _e( 'Go to migrating users section.', 'bbpress' ); ?></label>
623 <?php printf( __( '<strong>Note</strong>: The WordPress %1$s and %2$s tables will be renamed (not deleted) so that you can restore them if something goes wrong.', 'bbpress' ), '<code>' . $wpdb->users . '</code>', '<code>' . $wpdb->usermeta . '</code>' ); ?>
624 <?php printf( __( 'Please ensure there are no tables named %1$s and %2$s to avoid renaming conflicts.', 'bbpress' ), '<code>' . $bbdb->prefix . $wpdb->users . '_tmp' . '</code>', '<code>' . $bbdb->prefix . $wpdb->usermeta . '_tmp' . '</code>' ); ?>
625 <?php _e( 'Also, your WordPress and bbPress installs must be in the same database.', 'bbpress' ); ?>
626 </li>
627
628 <li>
629 <?php _e( 'You\'re done with user migration or have the user tables <strong>integrated</strong>:', 'bbpress' ); ?>
630 <label for="step_board"><?php _e( 'Proceed to importing forums, topics and posts section.', 'bbpress' ); ?></label>
631 </li>
632
633 <li>
634 <?php _e( 'You have a well <strong>established</strong> WordPress user base, the user tables are not integrated and you can\'t lose your users:', 'bbpress' ); ?>
635 <?php _e( 'This is currently not yet supported, and will likely not be in the future also as it is highly complex to merge two user sets (which might even conflict).', 'bbpress' ); ?>
636 <?php printf( __( 'Patches are always <a href="%s" title="The last revision containing the custom user migration code">welcome</a>. :)', 'bbpress' ), 'http://bbpress.trac.wordpress.org/ticket/1523' ); ?>
637 </li>
638
639 </ol>
640
641 <?php endif; ?>
642
643 <input type="radio" name="step"<?php disabled( $this->is_integrated() ); ?> value="2"<?php checked( 'user', $radio ); ?> id="step_user" />
644 <label for="step_user"><?php _e( 'Migrate Users', 'bbpress' ); ?></label>
645
646 <input type="radio" name="step" value="3"<?php checked( 'board', $radio ); ?> id="step_board" />
647 <label for="step_board"><?php _e( 'Import Forums, Topics & Posts', 'bbpress' ); ?></label>
648
649 <p class="submit">
650 <?php wp_nonce_field( 'bbp-bbpress-import' ); ?>
651 <input type="submit" class="button" value="<?php esc_attr_e( 'Start!', 'bbpress' ); ?>" />
652 </p>
653
654 </form>
655
656 <?php
657 }
658
659 /**
660 * Check form inputs and start importing users
661 *
662 * @return type
663 */
664 function step2() {
665 do_action( 'import_start' );
666
667 // Set time limit to 0 to avoid time out errors
668 set_time_limit( 0 );
669
670 if ( is_callable( 'ob_implicit_flush' ) )
671 ob_implicit_flush( true );
672
673 update_option( 'bbp_bbpress_step', 2 );
674
675 $setup = $this->setup();
676 if ( empty( $setup ) ) {
677 return false;
678 } elseif ( is_wp_error( $setup ) ) {
679 $this->throw_error( $setup, 2 );
680 return false;
681 }
682
683 global $wpdb, $bbdb; ?>
684
685 <div id="bbpress-import-status">
686
687 <h3><?php _e( 'Importing Users', 'bbpress' ); ?></h3>
688 <p><?php _e( 'We&#8217;re in the process of migrating your users...', 'bbpress' ); ?></p>
689
690 <ol>
691
692 <?php /* Rename the WordPress users and usermeta table */ ?>
693
694 <li>
695 <?php
696
697 if ( $wpdb->query( "RENAME TABLE $wpdb->users TO {$bbdb->prefix}{$wpdb->users}_tmp, $wpdb->usermeta TO {$bbdb->prefix}{$wpdb->usermeta}_tmp" ) !== false ) :
698 printf( __( 'Renamed the <code>%1$s</code> and <code>%2$s</code> tables to <code>%3$s</code> and <code>%4$s</code> respectively.', 'bbpress' ), $wpdb->users, $wpdb->usermeta, $bbdb->prefix . $wpdb->users . '_tmp', $bbdb->prefix . $wpdb->usermeta . '_tmp' );
699 else :
700 printf( __( 'There was a problem dropping the <code>%1$s</code> and <code>%2$s</code> tables. Please check and re-run the script or rename or drop the tables yourself.', 'bbpress' ), $wpdb->users, $wpdb->usermeta ); ?>
701
702 </li></ol>
703
704 <?php
705 return;
706 endif; ?>
707
708 </li>
709
710 <?php /* Duplicate the WordPress users and usermeta table */ ?>
711
712 <li>
713 <?php
714
715 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 ) :
716 printf( __( 'Created the <code>%s</code> table and copied the users from bbPress.', 'bbpress' ), $wpdb->users );
717 else :
718 printf( __( 'There was a problem duplicating the table <code>%1$s</code> to <code>%2$s</code>. Please check and re-run the script or duplicate the table yourself.', 'bbpress' ), $bbdb->users, $wpdb->users ); ?>
719
720 </li></ol>
721
722 <?php
723
724 return;
725 endif; ?>
726
727 </li>
728
729 <li>
730 <?php
731
732 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 ) :
733 printf( __( 'Created the <code>%s</code> table and copied the user information from bbPress.', 'bbpress' ), $wpdb->usermeta );
734 else :
735 printf( __( 'There was a problem duplicating the table <code>%1$s</code> to <code>%2$s</code>. Please check and re-run the script or duplicate the table yourself.', 'bbpress' ), $bbdb->usermeta, $wpdb->usermeta ); ?>
736
737 </li></ol>
738
739 <?php
740
741 return;
742 endif; ?>
743
744 </li>
745
746 <?php
747
748 // Map the user roles by our wish
749 $roles_map = array(
750 'keymaster' => 'administrator',
751 'administrator' => 'bbp_moderator',
752 'moderator' => 'bbp_moderator',
753 'member' => get_option( 'default_role' ),
754 'inactive' => get_option( 'default_role' ),
755 'blocked' => get_option( 'default_role' ),
756 'throttle' => 'throttle'
757 );
758
759 $wp_user_level_map = array(
760 'administrator' => 10,
761 'editor' => 7,
762 'author' => 2,
763 'contributor' => 1,
764 'subscriber' => 0,
765 'throttle' => 0
766 );
767
768 // Apply the WordPress roles to the new users based on their bbPress roles
769 wp_cache_flush();
770 $users = get_users( array( 'fields' => 'all_with_meta', 'orderby' => 'ID' ) );
771
772 foreach ( $users as $user ) {
773
774 // Get the bbPress roles
775 $bb_roles =& $user->{ $bbdb->prefix . 'capabilities' };
776 $converted_roles = $converted_level = array();
777
778 // Loop through each role the user has
779 if ( !empty( $bb_roles ) ) {
780 foreach ( $bb_roles as $bb_role => $bb_role_value ) {
781
782 // If we have one of those in our roles map, add the WP counterpart in the new roles array
783 if ( $roles_map[strtolower( $bb_role )] && !empty( $bb_role_value ) ) {
784 $converted_roles[$roles_map[strtolower( $bb_role )]] = true;
785
786 // Have support for deprecated levels too
787 $converted_level[] = $wp_user_level_map[$roles_map[strtolower( $bb_role )]];
788
789 // We need an admin for future use
790 if ( empty( $admin_user ) && 'administrator' == $roles_map[strtolower( $bb_role )] )
791 $admin_user = $user;
792 }
793
794 }
795 }
796
797 // If we have new roles, then update the user meta
798 if ( count( $converted_roles ) ) {
799 update_user_meta( $user->ID, $wpdb->prefix . 'capabilities', $converted_roles );
800 update_user_meta( $user->ID, $wpdb->prefix . 'user_level', max( $converted_level ) );
801 }
802
803 }
804
805 if ( empty( $admin_user ) || is_wp_error( $admin_user ) ) : /* I ask why */ ?>
806
807 <li>
808 <?php _e( 'There was a problem in getting an administrator of the blog. Now, please go to your blog, set a user as administrator, login as that user and directly go to importing forums, topics and replies section. Note that old logins won\'t work, you would have to edit your database (you can still try logging in as the bbPress keymaster).', 'bbpress' ); ?>
809 </li>
810
811 </ol>
812
813 <?php
814
815 return;
816
817 endif;
818
819 // Logout the user as it won't have any good privileges for us to do the work
820 // wp_clear_auth_cookie();
821
822 // Login the admin so that we have permissions for conversion etc
823 // wp_set_auth_cookie( $admin_user->ID );
824
825 // Set the current user
826 wp_set_current_user( $admin_user->ID, $admin_user->user_login );
827
828 ?>
829
830 <li>
831 <?php printf( __( '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 %1$s. Now, you can only login into your WordPress blog by the bbPress credentials. For the time being, you have been logged in as the first available administrator (Username: %2$s, User ID: %3$s).', 'bbpress' ), get_option( 'default_role' ), $admin_user->user_login, $admin_user->ID ); ?>
832 </li>
833
834 </ol>
835
836 <p><?php _e( 'Your users have all been imported, but wait &#8211; there&#8217;s more! Now we need to import your forums, topics and posts!', 'bbpress' ); ?></p>
837 <?php
838
839 echo $this->next_step( 3, __( 'Import forums, topics and posts &raquo;', 'bbpress' ) ); ?>
840
841 </div>
842
843 <?php
844 }
845
846 /**
847 * Import forums, topics and posts
848 */
849 function step3() {
850 do_action( 'import_start' );
851
852 set_time_limit( 0 );
853 update_option( 'bbp_bbpress_step', 3 );
854
855 $setup = $this->setup();
856 if ( empty( $setup ) ) {
857 return false;
858 } elseif ( is_wp_error( $setup ) ) {
859 $this->throw_error( $setup, 3 );
860 return false;
861 }
862
863 global $wpdb, $bbdb, $bbp; ?>
864
865 <div id="bbpress-import-status">
866
867 <h3><?php _e( 'Importing Forums, Topics And Posts', 'bbpress' ); ?></h3>
868 <p><?php _e( 'We&#8217;re importing your bbPress standalone forums, topics and replies...', 'bbpress' ); ?></p>
869
870 <ol>
871
872 <?php
873
874 if ( !$forums = bb_get_forums() ) {
875 echo "<li><strong>" . __( 'No forums were found!', 'bbpress' ) . "</strong></li></ol>\n";
876 return;
877 }
878
879 echo "<li>" . sprintf( __( 'Total number of forums: %s', 'bbpress' ), count( $forums ) ) . "</li>\n";
880
881 $forum_map = array();
882 $post_statuses = array( 'publish', $bbp->trash_status_id, $bbp->spam_status_id );
883
884 foreach ( (array) $forums as $forum ) {
885 echo "<li>" . sprintf( __( 'Processing forum #%1$s (<a href="%2$s">%3$s</a>)', 'bbpress' ), $forum->forum_id, get_forum_link( $forum->forum_id ), esc_html( $forum->forum_name ) ) . "\n<ul>\n";
886
887 // Insert the forum and add it to the map.
888 $inserted_forum = wp_insert_post( array(
889 'post_author' => get_current_user_id(),
890 'post_content' => $forum->forum_desc,
891 'post_title' => $forum->forum_name,
892 'post_excerpt' => '',
893 'post_status' => 'publish',
894 'comment_status' => 'closed',
895 'ping_status' => 'closed',
896 'post_name' => $forum->forum_slug,
897 'post_parent' => !empty( $forum->forum_parent ) ? $forum_map[$forum->forum_parent] : 0,
898 'post_type' => bbp_get_forum_post_type(),
899 'menu_order' => $forum->forum_order
900 ) );
901
902 $forum_map[$forum->forum_id] = $inserted_forum;
903
904 if ( !empty( $inserted_forum ) && !is_wp_error( $inserted_forum ) ) {
905 echo "<li>" . sprintf( __( 'Added the forum as forum #<a href="%1$s">%2$s</a>', 'bbpress' ), get_permalink( $inserted_forum ), $inserted_forum ) . "</li>\n";
906 } else {
907 echo "<li><em>" . __( 'There was a problem in adding the forum.', 'bbpress' ) . "</em></li></ul></li>\n";
908 continue;
909 }
910
911 $topics_query = new BB_Query( 'topic', array(
912 'forum_id' => $forum->forum_id,
913 'per_page' => -1,
914 'topic_status' => 'all',
915 'order_by' => 'topic_start_time',
916 'order' => 'ASC'
917 ) );
918
919 $topics = $topics_query->results;
920
921 // In standalone, categories can have topics, but this is not the case in plugin
922 // So make the forum category if it doesn't have topics
923 // Else close it if it's a category and has topics
924 if ( bb_get_forum_is_category( $forum->forum_id ) ) {
925
926 if ( count( $topics ) == 0 ) {
927 bbp_categorize_forum( $inserted_forum );
928 echo "<li>" . __( 'The forum is a category and has no topics.', 'bbpress' ) . "</li>\n</ul>\n</li>";
929
930 continue;
931 } else {
932 bbp_close_forum( $inserted_forum );
933 echo "<li>" . __( 'The forum is a category but has topics, so it has been set as closed on the new board.', 'bbpress' ) . "</li>\n";
934 }
935
936 }
937
938 bb_cache_first_posts( $topics );
939
940 echo "<li>" . sprintf( __( 'Total number of topics in the forum: %s', 'bbpress' ), count( $topics ) ) . "</li>\n";
941
942 foreach ( (array) $topics as $topic ) {
943 $first_post = bb_get_first_post( $topic->topic_id );
944
945 // If the topic is public, check if it's open and set the status accordingly
946 $topic_status = $topic->topic_status == 0 ? ( $topic->topic_open == 0 ? $bbp->closed_status_id : $post_statuses[$topic->topic_status] ) : $post_statuses[$topic->topic_status];
947
948 $inserted_topic = wp_insert_post( array(
949 'post_parent' => $inserted_forum,
950 'post_author' => $topic->topic_poster,
951 'post_content' => $first_post->post_text,
952 'post_title' => $topic->topic_title,
953 'post_name' => $topic->topic_slug,
954 'post_status' => $topic_status,
955 'post_date_gmt' => $topic->topic_start_time,
956 'post_date' => get_date_from_gmt( $topic->topic_start_time ),
957 'post_type' => bbp_get_topic_post_type(),
958 'tax_input' => array( 'topic-tag' => array_map( array( $this, 'get_tag_name' ), bb_get_public_tags( $topic->topic_id ) ) )
959 ) );
960
961 if ( !empty( $inserted_topic ) && !is_wp_error( $inserted_topic ) ) {
962
963 // Loginless Posting
964 if ( $topic->topic_poster == 0 ) {
965 update_post_meta( $inserted_topic, '_bbp_anonymous_name', bb_get_post_meta( 'post_author', $first_post->post_id ) );
966 update_post_meta( $inserted_topic, '_bbp_anonymous_email', bb_get_post_meta( 'post_email', $first_post->post_id ) );
967 update_post_meta( $inserted_topic, '_bbp_anonymous_website', bb_get_post_meta( 'post_url', $first_post->post_id ) );
968 }
969
970 // Author IP
971 update_post_meta( $inserted_topic, '_bbp_author_ip', $first_post->poster_ip );
972
973 // Forum topic meta
974 update_post_meta( $inserted_topic, '_bbp_forum_id', $inserted_forum );
975 update_post_meta( $inserted_topic, '_bbp_topic_id', $inserted_topic );
976
977 $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 ) );
978
979 $replies = 0;
980 $hidden_replies = 0;
981 $last_reply = 0;
982 $post = null;
983
984 foreach ( (array) $posts as $post ) {
985
986 // Pingback
987 if ( $post->poster_id == 0 && $pingback_uri = bb_get_post_meta( 'pingback_uri', $post->post_id ) ) {
988 $pingback = wp_insert_comment( wp_filter_comment( array(
989 'comment_post_ID' => $inserted_topic,
990 'comment_author' => bb_get_post_meta( 'pingback_title', $post->post_id ),
991 'comment_author_url' => $pingback_uri,
992 'comment_author_IP' => $post->poster_ip,
993 'comment_date_gmt' => $post->post_time,
994 'comment_date' => get_date_from_gmt( $post->post_time ),
995 'comment_content' => $post->post_text,
996 'comment_approved' => $post->post_status == 0 ? 1 : ( $post->post_status == 2 ? 'spam' : 0 ),
997 'comment_type' => 'pingback'
998 ) ) );
999
1000 // Normal post
1001 } else {
1002 $reply_title = sprintf( __( 'Reply To: %s', 'bbpress' ), $topic->topic_title );
1003
1004 $last_reply = wp_insert_post( array(
1005 'post_parent' => $inserted_topic,
1006 'post_author' => $post->poster_id,
1007 'post_date_gmt' => $post->post_time,
1008 'post_date' => get_date_from_gmt( $post->post_time ),
1009 'post_title' => $reply_title,
1010 'post_name' => sanitize_title_with_dashes( $reply_title ),
1011 'post_status' => $post_statuses[$post->post_status],
1012 'post_type' => bbp_get_reply_post_type(),
1013 'post_content' => $post->post_text
1014 ) );
1015
1016 // Loginless
1017 if ( $post->poster_id == 0 ) {
1018 update_post_meta( $last_reply, '_bbp_anonymous_name', bb_get_post_meta( 'post_author', $post->post_id ) );
1019 update_post_meta( $last_reply, '_bbp_anonymous_email', bb_get_post_meta( 'post_email', $post->post_id ) );
1020 update_post_meta( $last_reply, '_bbp_anonymous_website', bb_get_post_meta( 'post_url', $post->post_id ) );
1021 }
1022
1023 // Author IP
1024 update_post_meta( $last_reply, '_bbp_author_ip', $post->poster_ip );
1025
1026 // Reply Parents
1027 update_post_meta( $last_reply, '_bbp_forum_id', $inserted_forum );
1028 update_post_meta( $last_reply, '_bbp_topic_id', $inserted_topic );
1029
1030 bbp_update_reply_walker( $last_reply );
1031 }
1032
1033 if ( $post->post_status != 0 )
1034 $hidden_replies++;
1035 else
1036 $replies++;
1037 }
1038
1039 // Only add favorites and subscriptions if the topic is public
1040 if ( in_array( $topic_status, array( 'publish', $bbp->closed_status_id ) ) ) {
1041
1042 // Favorites
1043 foreach ( (array) $this->bb_get_topic_favoriters( $topic->topic_id ) as $favoriter )
1044 bbp_add_user_favorite ( $favoriter, $inserted_topic );
1045
1046 // Subscriptions
1047 foreach ( (array) $this->bb_get_topic_subscribers( $topic->topic_id ) as $subscriber )
1048 bbp_add_user_subscription( $subscriber, $inserted_topic );
1049 }
1050
1051 // Topic stickiness
1052 switch ( $topic->topic_sticky ) {
1053
1054 // Forum
1055 case 1 :
1056 bbp_stick_topic( $inserted_topic );
1057 break;
1058
1059 // Front
1060 case 2 :
1061 bbp_stick_topic( $inserted_topic, true );
1062 break;
1063 }
1064
1065 // Last active
1066 $last_active_id = !empty( $last_reply ) ? $last_reply : $inserted_topic;
1067 $last_active_time = !empty( $post ) ? $post->post_time : $first_post->post_time;
1068
1069 // Reply topic meta
1070 update_post_meta( $inserted_topic, '_bbp_last_reply_id', $last_reply );
1071 update_post_meta( $inserted_topic, '_bbp_last_active_id', $last_active_id );
1072 update_post_meta( $inserted_topic, '_bbp_last_active_time', $last_active_time );
1073 update_post_meta( $inserted_topic, '_bbp_reply_count', $replies );
1074 update_post_meta( $inserted_topic, '_bbp_hidden_reply_count', $hidden_replies );
1075
1076 // Voices will be done by recount
1077
1078 bbp_update_topic_walker( $inserted_topic );
1079
1080 echo "<li>" . sprintf( __( 'Added topic #%1$s (<a href="%2$s">%3$s</a>) as topic #<a href="%4$s">%5$s</a> with %6$s replies.', 'bbpress' ), $topic->topic_id, get_topic_link( $topic->topic_id ), esc_html( $topic->topic_title ), get_permalink( $inserted_topic ), $inserted_topic, $replies ) . "</li>\n";
1081 } else {
1082 echo "<li><em>" . sprintf( __( 'There was a problem in adding topic #1$s (<a href="%2$s">%3$s</a>).', 'bbpress' ), $topic->topic_id, get_topic_link( $topic->topic_id ), esc_html( $topic->topic_title ) ) . "</em></li></ul></li>\n";
1083 continue;
1084 }
1085 }
1086
1087 echo "</ul>\n</li>\n";
1088
1089 } ?>
1090
1091 </ol>
1092
1093 <?php
1094
1095 // Clean up database and we're out
1096 $this->cleanup();
1097 do_action( 'import_done', 'bbpress' );
1098
1099 ?>
1100
1101 <p><strong><?php printf( __( 'Your forums, topics and posts have all been imported, but wait &#8211; there&#8217;s more! Now we need to do a <a href="%s">recount</a> to get the counts in sync! Yes, we&#8217;re bad at Math.', 'bbpress' ), add_query_arg( array( 'page' => 'bbp-recount' ), get_admin_url( 0, 'tools.php' ) ) ); ?></strong></p>
1102
1103 <h4><?php printf( __( 'After that it\'s all done. <a href="%s">Have fun!</a> :)', 'bbpress' ), home_url() ); ?></h4>
1104
1105 </div>
1106
1107 <?php
1108 }
1109
1110 } // class bbPress_Importer
1111
1112 $bbpress_import = new bbPress_Importer();
1113
1114 register_importer( 'bbpress', __( 'bbPress Standalone', 'bbpress' ), __( 'Import your bbPress standalone board.', 'bbpress' ), array( $bbpress_import, 'dispatch' ) );
1115
1116 ?>
1117