BBP_Converter_Base

Base class to be extended by specific individual importers.

Defined (1)

The class is defined in the following location(s).

/includes/admin/converter.php  
  1. abstract class BBP_Converter_Base { 
  2.  
  3. /** 
  4. * @var array() This is the field mapping array to process. 
  5. */ 
  6. protected $field_map = array(); 
  7.  
  8. /** 
  9. * @var object This is the connection to the WordPress datbase. 
  10. */ 
  11. protected $wpdb; 
  12.  
  13. /** 
  14. * @var object This is the connection to the other platforms database. 
  15. */ 
  16. protected $opdb; 
  17.  
  18. /** 
  19. * @var int This is the max rows to process at a time. 
  20. */ 
  21. public $max_rows; 
  22.  
  23. /** 
  24. * @var array() Map of topic to forum. It is for optimization. 
  25. */ 
  26. private $map_topicid_to_forumid = array(); 
  27.  
  28. /** 
  29. * @var array() Map of from old forum ids to new forum ids. It is for optimization. 
  30. */ 
  31. private $map_forumid = array(); 
  32.  
  33. /** 
  34. * @var array() Map of from old topic ids to new topic ids. It is for optimization. 
  35. */ 
  36. private $map_topicid = array(); 
  37.  
  38. /** 
  39. * @var array() Map of from old reply_to ids to new reply_to ids. It is for optimization. 
  40. */ 
  41. private $map_reply_to = array(); 
  42.  
  43. /** 
  44. * @var array() Map of from old user ids to new user ids. It is for optimization. 
  45. */ 
  46. private $map_userid = array(); 
  47.  
  48. /** 
  49. * @var str This is the charset for your wp database. 
  50. */ 
  51. public $charset; 
  52.  
  53. /** 
  54. * @var boolean Sync table available. 
  55. */ 
  56. public $sync_table = false; 
  57.  
  58. /** 
  59. * @var str Sync table name. 
  60. */ 
  61. public $sync_table_name; 
  62.  
  63. /** Methods ***************************************************************/ 
  64.  
  65. /** 
  66. * This is the constructor and it connects to the platform databases. 
  67. */ 
  68. public function __construct() { 
  69. $this->setup_globals(); 
  70.  
  71. private function setup_globals() { 
  72. global $wpdb; 
  73.  
  74. /** Get database connections ******************************************/ 
  75.  
  76. $this->wpdb = $wpdb; 
  77. $this->max_rows = (int) $_POST['_bbp_converter_rows']; 
  78. $this->opdb = new wpdb( $_POST['_bbp_converter_db_user'], $_POST['_bbp_converter_db_pass'], $_POST['_bbp_converter_db_name'], $_POST['_bbp_converter_db_server'] ); 
  79. $this->opdb->prefix = $_POST['_bbp_converter_db_prefix']; 
  80.  
  81. /** 
  82. * Error Reporting 
  83. */ 
  84. $this->wpdb->show_errors(); 
  85. $this->opdb->show_errors(); 
  86.  
  87. /** 
  88. * Syncing 
  89. */ 
  90. $this->sync_table_name = $this->wpdb->prefix . 'bbp_converter_translator'; 
  91. if ( $this->wpdb->get_var( "SHOW TABLES LIKE '" . $this->sync_table_name . "'" ) == $this->sync_table_name ) { 
  92. $this->sync_table = true; 
  93. } else { 
  94. $this->sync_table = false; 
  95.  
  96. /** 
  97. * Charset 
  98. */ 
  99. if ( empty( $this->wpdb->charset ) ) { 
  100. $this->charset = 'UTF8'; 
  101. } else { 
  102. $this->charset = $this->wpdb->charset; 
  103.  
  104. /** 
  105. * Default mapping. 
  106. */ 
  107.  
  108. /** Forum Section *****************************************************/ 
  109.  
  110. $this->field_map[] = array( 
  111. 'to_type' => 'forum',  
  112. 'to_fieldname' => 'post_status',  
  113. 'default' => 'publish' 
  114. ); 
  115. $this->field_map[] = array( 
  116. 'to_type' => 'forum',  
  117. 'to_fieldname' => 'comment_status',  
  118. 'default' => 'closed' 
  119. ); 
  120. $this->field_map[] = array( 
  121. 'to_type' => 'forum',  
  122. 'to_fieldname' => 'ping_status',  
  123. 'default' => 'closed' 
  124. ); 
  125. $this->field_map[] = array( 
  126. 'to_type' => 'forum',  
  127. 'to_fieldname' => 'post_type',  
  128. 'default' => 'forum' 
  129. ); 
  130.  
  131. /** Topic Section *****************************************************/ 
  132.  
  133. $this->field_map[] = array( 
  134. 'to_type' => 'topic',  
  135. 'to_fieldname' => 'post_status',  
  136. 'default' => 'publish' 
  137. ); 
  138. $this->field_map[] = array( 
  139. 'to_type' => 'topic',  
  140. 'to_fieldname' => 'comment_status',  
  141. 'default' => 'closed' 
  142. ); 
  143. $this->field_map[] = array( 
  144. 'to_type' => 'topic',  
  145. 'to_fieldname' => 'ping_status',  
  146. 'default' => 'closed' 
  147. ); 
  148. $this->field_map[] = array( 
  149. 'to_type' => 'topic',  
  150. 'to_fieldname' => 'post_type',  
  151. 'default' => 'topic' 
  152. ); 
  153.  
  154. /** Post Section ******************************************************/ 
  155.  
  156. $this->field_map[] = array( 
  157. 'to_type' => 'reply',  
  158. 'to_fieldname' => 'post_status',  
  159. 'default' => 'publish' 
  160. ); 
  161. $this->field_map[] = array( 
  162. 'to_type' => 'reply',  
  163. 'to_fieldname' => 'comment_status',  
  164. 'default' => 'closed' 
  165. ); 
  166. $this->field_map[] = array( 
  167. 'to_type' => 'reply',  
  168. 'to_fieldname' => 'ping_status',  
  169. 'default' => 'closed' 
  170. ); 
  171. $this->field_map[] = array( 
  172. 'to_type' => 'reply',  
  173. 'to_fieldname' => 'post_type',  
  174. 'default' => 'reply' 
  175. ); 
  176.  
  177. /** User Section ******************************************************/ 
  178.  
  179. $this->field_map[] = array( 
  180. 'to_type' => 'user',  
  181. 'to_fieldname' => 'role',  
  182. 'default' => get_option( 'default_role' ) 
  183. ); 
  184.  
  185. /** 
  186. * Convert Forums 
  187. */ 
  188. public function convert_forums( $start = 1 ) { 
  189. return $this->convert_table( 'forum', $start ); 
  190.  
  191. /** 
  192. * Convert Topics / Threads 
  193. */ 
  194. public function convert_topics( $start = 1 ) { 
  195. return $this->convert_table( 'topic', $start ); 
  196.  
  197. /** 
  198. * Convert Posts 
  199. */ 
  200. public function convert_replies( $start = 1 ) { 
  201. return $this->convert_table( 'reply', $start ); 
  202.  
  203. /** 
  204. * Convert Users 
  205. */ 
  206. public function convert_users( $start = 1 ) { 
  207. return $this->convert_table( 'user', $start ); 
  208.  
  209. /** 
  210. * Convert Tags 
  211. */ 
  212. public function convert_tags( $start = 1 ) { 
  213. return $this->convert_table( 'tags', $start ); 
  214.  
  215. /** 
  216. * Convert Table 
  217. * @param string to type 
  218. * @param int Start row 
  219. */ 
  220. public function convert_table( $to_type, $start ) { 
  221.  
  222. // Are we usig a sync table, or postmeta? 
  223. if ( $this->wpdb->get_var( "SHOW TABLES LIKE '" . $this->sync_table_name . "'" ) == $this->sync_table_name ) { 
  224. $this->sync_table = true; 
  225. } else { 
  226. $this->sync_table = false; 
  227.  
  228. // Set some defaults 
  229. $has_insert = false; 
  230. $from_tablename = ''; 
  231. $field_list = $from_tables = $tablefield_array = array(); 
  232.  
  233. // Toggle Table Name based on $to_type (destination) 
  234. switch ( $to_type ) { 
  235. case 'user' : 
  236. $tablename = $this->wpdb->users; 
  237. break; 
  238.  
  239. case 'tags' : 
  240. $tablename = ''; 
  241. break; 
  242.  
  243. default : 
  244. $tablename = $this->wpdb->posts; 
  245.  
  246. // Get the fields from the destination table 
  247. if ( !empty( $tablename ) ) { 
  248. $tablefield_array = $this->get_fields( $tablename ); 
  249.  
  250. /** Step 1 ************************************************************/ 
  251.  
  252. // Loop through the field maps, and look for to_type matches 
  253. foreach ( $this->field_map as $item ) { 
  254.  
  255. // Yay a match, and we have a from table, too 
  256. if ( ( $item['to_type'] == $to_type ) && !empty( $item['from_tablename'] ) ) { 
  257.  
  258. // $from_tablename was set from a previous loop iteration 
  259. if ( ! empty( $from_tablename ) ) { 
  260.  
  261. // Doing some joining 
  262. if ( !in_array( $item['from_tablename'], $from_tables ) && in_array( $item['join_tablename'], $from_tables ) ) { 
  263. $from_tablename .= ' ' . $item['join_type'] . ' JOIN ' . $this->opdb->prefix . $item['from_tablename'] . ' AS ' . $item['from_tablename'] . ' ' . $item['join_expression']; 
  264.  
  265. // $from_tablename needs to be set 
  266. } else { 
  267. $from_tablename = $item['from_tablename'] . ' AS ' . $item['from_tablename']; 
  268.  
  269. // Specific FROM expression data used 
  270. if ( !empty( $item['from_expression'] ) ) { 
  271.  
  272. // No 'WHERE' in expression 
  273. if ( stripos( $from_tablename, "WHERE" ) === false ) { 
  274. $from_tablename .= ' ' . $item['from_expression']; 
  275.  
  276. // 'WHERE' in expression, so replace with 'AND' 
  277. } else { 
  278. $from_tablename .= ' ' . str_replace( "WHERE", "AND", $item['from_expression'] ); 
  279.  
  280. // Add tablename and fieldname to arrays, formatted for querying 
  281. $from_tables[] = $item['from_tablename']; 
  282. $field_list[] = 'convert(' . $item['from_tablename'] . '.' . $item['from_fieldname'] . ' USING "' . $this->charset . '") AS ' . $item['from_fieldname']; 
  283.  
  284. /** Step 2 ************************************************************/ 
  285.  
  286. // We have a $from_tablename, so we want to get some data to convert 
  287. if ( !empty( $from_tablename ) ) { 
  288.  
  289. // Get some data from the old forums 
  290. $field_list = array_unique( $field_list ); 
  291. $forum_query = 'SELECT ' . implode( ', ', $field_list ) . ' FROM ' . $this->opdb->prefix . $from_tablename . ' LIMIT ' . $start . ', ' . $this->max_rows; 
  292. $forum_array = $this->opdb->get_results( $forum_query, ARRAY_A ); 
  293.  
  294. // Set this query as the last one ran 
  295. update_option( '_bbp_converter_query', $forum_query ); 
  296.  
  297. // Query returned some results 
  298. if ( !empty( $forum_array ) ) { 
  299.  
  300. // Loop through results 
  301. foreach ( (array) $forum_array as $forum ) { 
  302.  
  303. // Reset some defaults 
  304. $insert_post = $insert_postmeta = $insert_data = array(); 
  305.  
  306. // Loop through field map, again... 
  307. foreach ( $this->field_map as $row ) { 
  308.  
  309. // Types matchand to_fieldname is present. This means 
  310. // we have some work to do here. 
  311. if ( ( $row['to_type'] == $to_type ) && ! is_null( $row['to_fieldname'] ) ) { 
  312.  
  313. // This row has a destination that matches one of the 
  314. // columns in this table. 
  315. if ( in_array( $row['to_fieldname'], $tablefield_array ) ) { 
  316.  
  317. // Allows us to set default fields. 
  318. if ( isset( $row['default'] ) ) { 
  319. $insert_post[$row['to_fieldname']] = $row['default']; 
  320.  
  321. // Translates a field from the old forum. 
  322. } elseif ( isset( $row['callback_method'] ) ) { 
  323. if ( ( 'callback_userid' == $row['callback_method'] ) && empty( $_POST['_bbp_converter_convert_users'] ) ) { 
  324. $insert_post[$row['to_fieldname']] = $forum[$row['from_fieldname']]; 
  325. } else { 
  326. $insert_post[$row['to_fieldname']] = call_user_func_array( array( $this, $row['callback_method'] ), array( $forum[$row['from_fieldname']], $forum ) ); 
  327.  
  328. // Maps the field from the old forum. 
  329. } else { 
  330. $insert_post[$row['to_fieldname']] = $forum[$row['from_fieldname']]; 
  331.  
  332. // Destination field is not empty, so we might need 
  333. // to do some extra work or set a default. 
  334. } elseif ( !empty( $row['to_fieldname'] ) ) { 
  335.  
  336. // Allows us to set default fields. 
  337. if ( isset( $row['default'] ) ) { 
  338. $insert_postmeta[$row['to_fieldname']] = $row['default']; 
  339.  
  340. // Translates a field from the old forum. 
  341. } elseif ( isset( $row['callback_method'] ) ) { 
  342. if ( ( $row['callback_method'] == 'callback_userid' ) && ( 0 == $_POST['_bbp_converter_convert_users'] ) ) { 
  343. $insert_postmeta[$row['to_fieldname']] = $forum[$row['from_fieldname']]; 
  344. } else { 
  345. $insert_postmeta[$row['to_fieldname']] = call_user_func_array( array( $this, $row['callback_method'] ), array( $forum[$row['from_fieldname']], $forum ) ); 
  346.  
  347. // Maps the field from the old forum. 
  348. } else { 
  349. $insert_postmeta[$row['to_fieldname']] = $forum[$row['from_fieldname']]; 
  350.  
  351. /** Step 3 ************************************************/ 
  352.  
  353. // Something to insert into the destination field 
  354. if ( count( $insert_post ) > 0 || ( $to_type == 'tags' && count( $insert_postmeta ) > 0 ) ) { 
  355.  
  356. switch ( $to_type ) { 
  357.  
  358. /** New user **************************************/ 
  359.  
  360. case 'user': 
  361. if ( username_exists( $insert_post['user_login'] ) ) { 
  362. $insert_post['user_login'] = 'imported_' . $insert_post['user_login']; 
  363.  
  364. if ( email_exists( $insert_post['user_email'] ) ) { 
  365. $insert_post['user_email'] = 'imported_' . $insert_post['user_email']; 
  366.  
  367. $post_id = wp_insert_user( $insert_post ); 
  368.  
  369. if ( is_numeric( $post_id ) ) { 
  370.  
  371. foreach ( $insert_postmeta as $key => $value ) { 
  372.  
  373. add_user_meta( $post_id, $key, $value, true ); 
  374.  
  375. if ( '_id' == substr( $key, -3 ) && ( true === $this->sync_table ) ) { 
  376. $this->wpdb->insert( $this->sync_table_name, array( 'value_type' => 'user', 'value_id' => $post_id, 'meta_key' => $key, 'meta_value' => $value ) ); 
  377. break; 
  378.  
  379. /** New Topic-Tag *********************************/ 
  380.  
  381. case 'tags': 
  382. $post_id = wp_set_object_terms( $insert_postmeta['objectid'], $insert_postmeta['name'], 'topic-tag', true ); 
  383. $term = get_term_by( 'name', $insert_postmeta['name'], 'topic-tag'); 
  384. if ( false !== $term ) { 
  385. wp_update_term( $term->term_id, 'topic-tag', array( 
  386. 'description' => $insert_postmeta['description'],  
  387. 'slug' => $insert_postmeta['slug'] 
  388. ) ); 
  389. break; 
  390.  
  391. /** Forum, Topic, Reply ***************************/ 
  392.  
  393. default: 
  394. $post_id = wp_insert_post( $insert_post ); 
  395.  
  396. if ( is_numeric( $post_id ) ) { 
  397.  
  398. foreach ( $insert_postmeta as $key => $value ) { 
  399.  
  400. add_post_meta( $post_id, $key, $value, true ); 
  401.  
  402. // Forums need to save their old ID for group forum association 
  403. if ( ( 'forum' == $to_type ) && ( '_bbp_forum_id' == $key ) ) 
  404. add_post_meta( $post_id, '_bbp_old_forum_id', $value ); 
  405.  
  406. // Topics need an extra bit of metadata 
  407. // to be keyed to the new post_id 
  408. if ( ( 'topic' == $to_type ) && ( '_bbp_topic_id' == $key ) ) { 
  409.  
  410. // Update the live topic ID 
  411. update_post_meta( $post_id, $key, $post_id ); 
  412.  
  413. // Save the old topic ID 
  414. add_post_meta( $post_id, '_bbp_old_topic_id', $value ); 
  415. if ( '_id' == substr( $key, -3 ) && ( true === $this->sync_table ) ) { 
  416. $this->wpdb->insert( $this->sync_table_name, array( 'value_type' => 'post', 'value_id' => $post_id, 'meta_key' => '_bbp_topic_id', 'meta_value' => $post_id ) ); 
  417. $this->wpdb->insert( $this->sync_table_name, array( 'value_type' => 'post', 'value_id' => $post_id, 'meta_key' => '_bbp_old_topic_id', 'meta_value' => $value ) ); 
  418.  
  419. } elseif ( '_id' == substr( $key, -3 ) && ( true === $this->sync_table ) ) { 
  420. $this->wpdb->insert( $this->sync_table_name, array( 'value_type' => 'post', 'value_id' => $post_id, 'meta_key' => $key, 'meta_value' => $value ) ); 
  421.  
  422. // Replies need to save their old reply_to ID for hierarchical replies association 
  423. if ( ( 'reply' == $to_type ) && ( '_bbp_reply_to' == $key ) ) { 
  424. add_post_meta( $post_id, '_bbp_old_reply_to', $value ); 
  425. break; 
  426. $has_insert = true; 
  427.  
  428. return ! $has_insert; 
  429.  
  430. /** 
  431. * This method converts old forum heirarchy to new bbPress heirarchy. 
  432. */ 
  433. public function convert_forum_parents( $start ) { 
  434.  
  435. $has_update = false; 
  436.  
  437. if ( !empty( $this->sync_table ) ) { 
  438. $query = 'SELECT value_id, meta_value FROM ' . $this->sync_table_name . ' WHERE meta_key = "_bbp_forum_parent_id" AND meta_value > 0 LIMIT ' . $start . ', ' . $this->max_rows; 
  439. } else { 
  440. $query = 'SELECT post_id AS value_id, meta_value FROM ' . $this->wpdb->postmeta . ' WHERE meta_key = "_bbp_forum_parent_id" AND meta_value > 0 LIMIT ' . $start . ', ' . $this->max_rows; 
  441.  
  442. update_option( '_bbp_converter_query', $query ); 
  443.  
  444. $forum_array = $this->wpdb->get_results( $query ); 
  445.  
  446. foreach ( (array) $forum_array as $row ) { 
  447. $parent_id = $this->callback_forumid( $row->meta_value ); 
  448. $this->wpdb->query( 'UPDATE ' . $this->wpdb->posts . ' SET post_parent = "' . $parent_id . '" WHERE ID = "' . $row->value_id . '" LIMIT 1' ); 
  449. $has_update = true; 
  450.  
  451. return ! $has_update; 
  452.  
  453. /** 
  454. * This method converts old topic stickies to new bbPress stickies. 
  455. * @since bbPress (r) 
  456. * @uses WPDB $wpdb 
  457. * @uses bbp_stick_topic() to set the imported topic as sticky 
  458. */ 
  459. public function convert_topic_stickies( $start ) { 
  460.  
  461. $has_update = false; 
  462.  
  463. if ( !empty( $this->sync_table ) ) { 
  464. $query = 'SELECT value_id, meta_value FROM ' . $this->sync_table_name . ' WHERE meta_key = "_bbp_old_sticky_status" AND meta_value = "sticky" LIMIT ' . $start . ', ' . $this->max_rows; 
  465. } else { 
  466. $query = 'SELECT post_id AS value_id, meta_value FROM ' . $this->wpdb->postmeta . ' WHERE meta_key = "_bbp_old_sticky_status" AND meta_value = "sticky" LIMIT ' . $start . ', ' . $this->max_rows; 
  467.  
  468. update_option( '_bbp_converter_query', $query ); 
  469.  
  470. $sticky_array = $this->wpdb->get_results( $query ); 
  471.  
  472. foreach ( (array) $sticky_array as $row ) { 
  473. bbp_stick_topic( $row->value_id ); 
  474. $has_update = true; 
  475.  
  476. return ! $has_update; 
  477.  
  478. /** 
  479. * This method converts old topic super stickies to new bbPress super stickies. 
  480. * @since bbPress (r) 
  481. * @uses WPDB $wpdb 
  482. * @uses bbp_stick_topic() to set the imported topic as super sticky 
  483. */ 
  484. public function convert_topic_super_stickies( $start ) { 
  485.  
  486. $has_update = false; 
  487.  
  488. if ( !empty( $this->sync_table ) ) { 
  489. $query = 'SELECT value_id, meta_value FROM ' . $this->sync_table_name . ' WHERE meta_key = "_bbp_old_sticky_status" AND meta_value = "super-sticky" LIMIT ' . $start . ', ' . $this->max_rows; 
  490. } else { 
  491. $query = 'SELECT post_id AS value_id, meta_value FROM ' . $this->wpdb->postmeta . ' WHERE meta_key = "_bbp_old_sticky_status" AND meta_value = "super-sticky" LIMIT ' . $start . ', ' . $this->max_rows; 
  492.  
  493. update_option( '_bbp_converter_query', $query ); 
  494.  
  495. $sticky_array = $this->wpdb->get_results( $query ); 
  496.  
  497. foreach ( (array) $sticky_array as $row ) { 
  498. $super = true; 
  499. bbp_stick_topic( $row->value_id, $super ); 
  500. $has_update = true; 
  501.  
  502. return ! $has_update; 
  503.  
  504. /** 
  505. * This method converts old reply_to post id to new bbPress reply_to post id. 
  506. */ 
  507. public function convert_reply_to_parents( $start ) { 
  508.  
  509. $has_update = false; 
  510.  
  511. if ( !empty( $this->sync_table ) ) { 
  512. $query = 'SELECT value_id, meta_value FROM ' . $this->sync_table_name . ' WHERE meta_key = "_bbp_old_reply_to" AND meta_value > 0 LIMIT ' . $start . ', ' . $this->max_rows; 
  513. } else { 
  514. $query = 'SELECT post_id AS value_id, meta_value FROM ' . $this->wpdb->postmeta . ' WHERE meta_key = "_bbp_old_reply_to" AND meta_value > 0 LIMIT ' . $start . ', ' . $this->max_rows; 
  515.  
  516. update_option( '_bbp_converter_query', $query ); 
  517.  
  518. $reply_to_array = $this->wpdb->get_results( $query ); 
  519.  
  520. foreach ( (array) $reply_to_array as $row ) { 
  521. $reply_to = $this->callback_reply_to( $row->meta_value ); 
  522. $this->wpdb->query( 'UPDATE ' . $this->wpdb->postmeta . ' SET meta_value = "' . $reply_to . '" WHERE meta_key = "_bbp_reply_to" AND post_id = "' . $row->value_id . '" LIMIT 1' ); 
  523. $has_update = true; 
  524.  
  525. return ! $has_update; 
  526.  
  527. /** 
  528. * This method deletes data from the wp database. 
  529. */ 
  530. public function clean( $start ) { 
  531.  
  532. $start = 0; 
  533. $has_delete = false; 
  534.  
  535. /** Delete bbconverter topics/forums/posts ****************************/ 
  536.  
  537. if ( true === $this->sync_table ) { 
  538. $query = 'SELECT value_id FROM ' . $this->sync_table_name . ' INNER JOIN ' . $this->wpdb->posts . ' ON(value_id = ID) WHERE meta_key LIKE "_bbp_%" AND value_type = "post" GROUP BY value_id ORDER BY value_id DESC LIMIT ' . $this->max_rows; 
  539. } else { 
  540. $query = 'SELECT post_id AS value_id FROM ' . $this->wpdb->postmeta . ' WHERE meta_key LIKE "_bbp_%" GROUP BY post_id ORDER BY post_id DESC LIMIT ' . $this->max_rows; 
  541.  
  542. update_option( '_bbp_converter_query', $query ); 
  543.  
  544. $posts = $this->wpdb->get_results( $query, ARRAY_A ); 
  545.  
  546. if ( isset( $posts[0] ) && ! empty( $posts[0]['value_id'] ) ) { 
  547. foreach ( (array) $posts as $value ) { 
  548. wp_delete_post( $value['value_id'], true ); 
  549. $has_delete = true; 
  550.  
  551. /** Delete bbconverter users ******************************************/ 
  552.  
  553. if ( true === $this->sync_table ) { 
  554. $query = 'SELECT value_id FROM ' . $this->sync_table_name . ' INNER JOIN ' . $this->wpdb->users . ' ON(value_id = ID) WHERE meta_key = "_bbp_user_id" AND value_type = "user" LIMIT ' . $this->max_rows; 
  555. } else { 
  556. $query = 'SELECT user_id AS value_id FROM ' . $this->wpdb->usermeta . ' WHERE meta_key = "_bbp_user_id" LIMIT ' . $this->max_rows; 
  557.  
  558. update_option( '_bbp_converter_query', $query ); 
  559.  
  560. $users = $this->wpdb->get_results( $query, ARRAY_A ); 
  561.  
  562. if ( !empty( $users ) ) { 
  563. foreach ( $users as $value ) { 
  564. wp_delete_user( $value['value_id'] ); 
  565. $has_delete = true; 
  566.  
  567. unset( $posts ); 
  568. unset( $users ); 
  569.  
  570. return ! $has_delete; 
  571.  
  572. /** 
  573. * This method deletes passwords from the wp database. 
  574. * @param int Start row 
  575. */ 
  576. public function clean_passwords( $start ) { 
  577.  
  578. $has_delete = false; 
  579.  
  580. /** Delete bbconverter passwords **************************************/ 
  581.  
  582. $query = 'SELECT user_id, meta_value FROM ' . $this->wpdb->usermeta . ' WHERE meta_key = "_bbp_password" LIMIT ' . $start . ', ' . $this->max_rows; 
  583. update_option( '_bbp_converter_query', $query ); 
  584.  
  585. $bbconverter = $this->wpdb->get_results( $query, ARRAY_A ); 
  586.  
  587. if ( !empty( $bbconverter ) ) { 
  588.  
  589. foreach ( $bbconverter as $value ) { 
  590. if ( is_serialized( $value['meta_value'] ) ) { 
  591. $this->wpdb->query( 'UPDATE ' . $this->wpdb->users . ' ' . 'SET user_pass = "" ' . 'WHERE ID = "' . $value['user_id'] . '"' ); 
  592. } else { 
  593. $this->wpdb->query( 'UPDATE ' . $this->wpdb->users . ' ' . 'SET user_pass = "' . $value['meta_value'] . '" ' . 'WHERE ID = "' . $value['user_id'] . '"' ); 
  594. $this->wpdb->query( 'DELETE FROM ' . $this->wpdb->usermeta . ' WHERE meta_key = "_bbp_password" AND user_id = "' . $value['user_id'] . '"' ); 
  595. $has_delete = true; 
  596.  
  597. return ! $has_delete; 
  598.  
  599. /** 
  600. * This method implements the authentication for the different forums. 
  601. * @param string Unencoded password. 
  602. */ 
  603. abstract protected function authenticate_pass( $password, $hash ); 
  604.  
  605. /** 
  606. * Info 
  607. */ 
  608. abstract protected function info(); 
  609.  
  610. /** 
  611. * This method grabs appropriate fields from the table specified 
  612. * @param string The table name to grab fields from 
  613. */ 
  614. private function get_fields( $tablename ) { 
  615. $rval = array(); 
  616. $field_array = $this->wpdb->get_results( 'DESCRIBE ' . $tablename, ARRAY_A ); 
  617.  
  618. foreach ( $field_array as $field ) { 
  619. $rval[] = $field['Field']; 
  620.  
  621. if ( $tablename == $this->wpdb->users ) { 
  622. $rval[] = 'role'; 
  623. $rval[] = 'yim'; 
  624. $rval[] = 'aim'; 
  625. $rval[] = 'jabber'; 
  626. return $rval; 
  627.  
  628. /** Callbacks *************************************************************/ 
  629.  
  630. /** 
  631. * Run password through wp_hash_password() 
  632. * @param string $username 
  633. * @param string $password 
  634. */ 
  635. public function callback_pass( $username, $password ) { 
  636. $user = $this->wpdb->get_row( $this->wpdb->prepare( 'SELECT * FROM ' . $this->wpdb->users . ' WHERE user_login = "%s" AND user_pass = "" LIMIT 1', $username ) ); 
  637. if ( !empty( $user ) ) { 
  638. $usermeta = $this->wpdb->get_row( 'SELECT * FROM ' . $this->wpdb->usermeta . ' WHERE meta_key = "_bbp_password" AND user_id = "' . $user->ID . '" LIMIT 1' ); 
  639.  
  640. if ( !empty( $usermeta ) ) { 
  641. if ( $this->authenticate_pass( $password, $usermeta->meta_value ) ) { 
  642. $this->wpdb->query( 'UPDATE ' . $this->wpdb->users . ' ' . 'SET user_pass = "' . wp_hash_password( $password ) . '" ' . 'WHERE ID = "' . $user->ID . '"' ); 
  643. $this->wpdb->query( 'DELETE FROM ' . $this->wpdb->usermeta . ' WHERE meta_key = "_bbp_password" AND user_id = "' . $user->ID . '"' ); 
  644.  
  645. /** 
  646. * A mini cache system to reduce database calls to forum ID's 
  647. * @param string $field 
  648. * @return string 
  649. */ 
  650. private function callback_forumid( $field ) { 
  651. if ( !isset( $this->map_forumid[$field] ) ) { 
  652. if ( !empty( $this->sync_table ) ) { 
  653. $row = $this->wpdb->get_row( $this->wpdb->prepare( 'SELECT value_id, meta_value FROM ' . $this->sync_table_name . ' WHERE meta_key = "_bbp_forum_id" AND meta_value = "%s" LIMIT 1', $field ) ); 
  654. } else { 
  655. $row = $this->wpdb->get_row( $this->wpdb->prepare( 'SELECT post_id AS value_id FROM ' . $this->wpdb->postmeta . ' WHERE meta_key = "_bbp_forum_id" AND meta_value = "%s" LIMIT 1', $field ) ); 
  656.  
  657. if ( !is_null( $row ) ) { 
  658. $this->map_forumid[$field] = $row->value_id; 
  659. } else { 
  660. $this->map_forumid[$field] = 0; 
  661. return $this->map_forumid[$field]; 
  662.  
  663. /** 
  664. * A mini cache system to reduce database calls to topic ID's 
  665. * @param string $field 
  666. * @return string 
  667. */ 
  668. private function callback_topicid( $field ) { 
  669. if ( !isset( $this->map_topicid[$field] ) ) { 
  670. if ( !empty( $this->sync_table ) ) { 
  671. $row = $this->wpdb->get_row( $this->wpdb->prepare( 'SELECT value_id, meta_value FROM ' . $this->sync_table_name . ' WHERE meta_key = "_bbp_old_topic_id" AND meta_value = "%s" LIMIT 1', $field ) ); 
  672. } else { 
  673. $row = $this->wpdb->get_row( $this->wpdb->prepare( 'SELECT post_id AS value_id FROM ' . $this->wpdb->postmeta . ' WHERE meta_key = "_bbp_old_topic_id" AND meta_value = "%s" LIMIT 1', $field ) ); 
  674.  
  675. if ( !is_null( $row ) ) { 
  676. $this->map_topicid[$field] = $row->value_id; 
  677. } else { 
  678. $this->map_topicid[$field] = 0; 
  679. return $this->map_topicid[$field]; 
  680.  
  681. /** 
  682. * A mini cache system to reduce database calls to reply_to post id. 
  683. * @param string $field 
  684. * @return string 
  685. */ 
  686. private function callback_reply_to( $field ) { 
  687. if ( !isset( $this->map_reply_to[$field] ) ) { 
  688. if ( !empty( $this->sync_table ) ) { 
  689. $row = $this->wpdb->get_row( $this->wpdb->prepare( 'SELECT value_id, meta_value FROM ' . $this->sync_table_name . ' WHERE meta_key = "_bbp_post_id" AND meta_value = "%s" LIMIT 1', $field ) ); 
  690. } else { 
  691. $row = $this->wpdb->get_row( $this->wpdb->prepare( 'SELECT post_id AS value_id FROM ' . $this->wpdb->postmeta . ' WHERE meta_key = "_bbp_post_id" AND meta_value = "%s" LIMIT 1', $field ) ); 
  692.  
  693. if ( !is_null( $row ) ) { 
  694. $this->map_reply_to[$field] = $row->value_id; 
  695. } else { 
  696. $this->map_reply_to[$field] = 0; 
  697. return $this->map_reply_to[$field]; 
  698.  
  699. /** 
  700. * A mini cache system to reduce database calls to user ID's 
  701. * @param string $field 
  702. * @return string 
  703. */ 
  704. private function callback_userid( $field ) { 
  705. if ( !isset( $this->map_userid[$field] ) ) { 
  706. if ( !empty( $this->sync_table ) ) { 
  707. $row = $this->wpdb->get_row( $this->wpdb->prepare( 'SELECT value_id, meta_value FROM ' . $this->sync_table_name . ' WHERE meta_key = "_bbp_user_id" AND meta_value = "%s" LIMIT 1', $field ) ); 
  708. } else { 
  709. $row = $this->wpdb->get_row( $this->wpdb->prepare( 'SELECT user_id AS value_id FROM ' . $this->wpdb->usermeta . ' WHERE meta_key = "_bbp_user_id" AND meta_value = "%s" LIMIT 1', $field ) ); 
  710.  
  711. if ( !is_null( $row ) ) { 
  712. $this->map_userid[$field] = $row->value_id; 
  713. } else { 
  714. if ( !empty( $_POST['_bbp_converter_convert_users'] ) && ( $_POST['_bbp_converter_convert_users'] == 1 ) ) { 
  715. $this->map_userid[$field] = 0; 
  716. } else { 
  717. $this->map_userid[$field] = $field; 
  718. return $this->map_userid[$field]; 
  719.  
  720. /** 
  721. * A mini cache system to reduce database calls map topics ID's to forum ID's 
  722. * @param string $field 
  723. * @return string 
  724. */ 
  725. private function callback_topicid_to_forumid( $field ) { 
  726. $topicid = $this->callback_topicid( $field ); 
  727. if ( empty( $topicid ) ) { 
  728. $this->map_topicid_to_forumid[$topicid] = 0; 
  729. } elseif ( ! isset( $this->map_topicid_to_forumid[$topicid] ) ) { 
  730. $row = $this->wpdb->get_row( 'SELECT post_parent FROM ' . $this->wpdb->posts . ' WHERE ID = "' . $topicid . '" LIMIT 1' ); 
  731.  
  732. if ( !is_null( $row ) ) { 
  733. $this->map_topicid_to_forumid[$topicid] = $row->post_parent; 
  734. } else { 
  735. $this->map_topicid_to_forumid[$topicid] = 0; 
  736.  
  737. return $this->map_topicid_to_forumid[$topicid]; 
  738.  
  739. protected function callback_slug( $field ) { 
  740. return sanitize_title( $field ); 
  741.  
  742. protected function callback_negative( $field ) { 
  743. if ( $field < 0 ) { 
  744. return 0; 
  745. } else { 
  746. return $field; 
  747.  
  748. protected function callback_html( $field ) { 
  749. require_once( bbpress()->admin->admin_dir . 'parser.php' ); 
  750. $bbcode = BBCode::getInstance(); 
  751. return html_entity_decode( $bbcode->Parse( $field ) ); 
  752.  
  753. protected function callback_null( $field ) { 
  754. if ( is_null( $field ) ) { 
  755. return ''; 
  756. } else { 
  757. return $field; 
  758.  
  759. protected function callback_datetime( $field ) { 
  760. if ( is_numeric( $field ) ) { 
  761. return date( 'Y-m-d H:i:s', $field ); 
  762. } else { 
  763. return date( 'Y-m-d H:i:s', strtotime( $field ) );