PageRenderTime 59ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/bbpress/includes/admin/converter.php

https://github.com/bfay/maniacal-kitten
PHP | 1302 lines | 782 code | 239 blank | 281 comment | 180 complexity | 7debc448c729f42872e90d87b74b37a4 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, AGPL-1.0, LGPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * bbPress Converter
  4. *
  5. * Based on the hard work of Adam Ellis at http://bbconverter.com
  6. *
  7. * @package bbPress
  8. * @subpackage Administration
  9. */
  10. // Exit if accessed directly
  11. if ( !defined( 'ABSPATH' ) ) exit;
  12. /**
  13. * Main BBP_Converter Class
  14. */
  15. class BBP_Converter {
  16. /**
  17. * The main bbPress Converter loader
  18. *
  19. * @since bbPress (r3813)
  20. * @uses BBP_Converter::includes() Include the required files
  21. * @uses BBP_Converter::setup_actions() Setup the actions
  22. */
  23. public function __construct() {
  24. // "I wonder where I'll float next."
  25. if ( empty( $_SERVER['REQUEST_METHOD'] ) )
  26. return;
  27. // Bail if request is not correct
  28. switch ( strtoupper( $_SERVER['REQUEST_METHOD'] ) ) {
  29. // Converter is converting
  30. case 'POST' :
  31. if ( ( empty( $_POST['action'] ) || ( 'bbconverter_process' != $_POST['action'] ) ) )
  32. return;
  33. break;
  34. // Some other admin page
  35. case 'GET' :
  36. if ( ( empty( $_GET['page'] ) || ( 'bbp-converter' != $_GET['page'] ) ) )
  37. return;
  38. break;
  39. }
  40. // Proceed with the actions
  41. $this->setup_actions();
  42. }
  43. /**
  44. * Setup the default actions
  45. *
  46. * @since bbPress (r3813)
  47. * @uses add_action() To add various actions
  48. */
  49. private function setup_actions() {
  50. // Attach to the admin head with our ajax requests cycle and css
  51. add_action( 'bbp_admin_head', array( $this, 'admin_head' ) );
  52. // Attach the bbConverter admin settings action to the WordPress admin init action.
  53. add_action( 'bbp_register_admin_settings', array( $this, 'register_admin_settings' ) );
  54. // Attach to the admin ajax request to process cycles
  55. add_action( 'wp_ajax_bbconverter_process', array( $this, 'process_callback' ) );
  56. }
  57. /**
  58. * Register the settings
  59. *
  60. * @since bbPress (r3813)
  61. * @uses add_settings_section() To add our own settings section
  62. * @uses add_settings_field() To add various settings fields
  63. * @uses register_setting() To register various settings
  64. */
  65. public function register_admin_settings() {
  66. // Add the main section
  67. add_settings_section( 'bbpress_converter_main', __( 'Database Settings', 'bbpress' ), 'bbp_converter_setting_callback_main_section', 'bbpress_converter' );
  68. // System Select
  69. add_settings_field( '_bbp_converter_platform', __( 'Select Platform', 'bbpress' ), 'bbp_converter_setting_callback_platform', 'bbpress_converter', 'bbpress_converter_main' );
  70. register_setting ( 'bbpress_converter_main', '_bbp_converter_platform', 'sanitize_title' );
  71. // Database Server
  72. add_settings_field( '_bbp_converter_db_server', __( 'Database Server', 'bbpress' ), 'bbp_converter_setting_callback_dbserver', 'bbpress_converter', 'bbpress_converter_main' );
  73. register_setting ( 'bbpress_converter_main', '_bbp_converter_db_server', 'sanitize_title' );
  74. // Database Server Port
  75. add_settings_field( '_bbp_converter_db_port', __( 'Database Port', 'bbpress' ), 'bbp_converter_setting_callback_dbport', 'bbpress_converter', 'bbpress_converter_main' );
  76. register_setting ( 'bbpress_converter_main', '_bbp_converter_db_port', 'sanitize_title' );
  77. // Database Name
  78. add_settings_field( '_bbp_converter_db_name', __( 'Database Name', 'bbpress' ), 'bbp_converter_setting_callback_dbname', 'bbpress_converter', 'bbpress_converter_main' );
  79. register_setting ( 'bbpress_converter_main', '_bbp_converter_db_name', 'sanitize_title' );
  80. // Database User
  81. add_settings_field( '_bbp_converter_db_user', __( 'Database User', 'bbpress' ), 'bbp_converter_setting_callback_dbuser', 'bbpress_converter', 'bbpress_converter_main' );
  82. register_setting ( 'bbpress_converter_main', '_bbp_converter_db_user', 'sanitize_title' );
  83. // Database Pass
  84. add_settings_field( '_bbp_converter_db_pass', __( 'Database Password', 'bbpress' ), 'bbp_converter_setting_callback_dbpass', 'bbpress_converter', 'bbpress_converter_main' );
  85. register_setting ( 'bbpress_converter_main', '_bbp_converter_db_pass', 'sanitize_title' );
  86. // Database Prefix
  87. add_settings_field( '_bbp_converter_db_prefix', __( 'Table Prefix', 'bbpress' ), 'bbp_converter_setting_callback_dbprefix', 'bbpress_converter', 'bbpress_converter_main' );
  88. register_setting ( 'bbpress_converter_main', '_bbp_converter_db_prefix', 'sanitize_title' );
  89. // Add the options section
  90. add_settings_section( 'bbpress_converter_opt', __( 'Options', 'bbpress' ), 'bbp_converter_setting_callback_options_section', 'bbpress_converter' );
  91. // Rows Limit
  92. add_settings_field( '_bbp_converter_rows', __( 'Rows Limit', 'bbpress' ), 'bbp_converter_setting_callback_rows', 'bbpress_converter', 'bbpress_converter_opt' );
  93. register_setting ( 'bbpress_converter_opt', '_bbp_converter_rows', 'intval' );
  94. // Delay Time
  95. add_settings_field( '_bbp_converter_delay_time', __( 'Delay Time', 'bbpress' ), 'bbp_converter_setting_callback_delay_time', 'bbpress_converter', 'bbpress_converter_opt' );
  96. register_setting ( 'bbpress_converter_opt', '_bbp_converter_delay_time', 'intval' );
  97. // Convert Users ?
  98. add_settings_field( '_bbp_converter_convert_users', __( 'Convert Users', 'bbpress' ), 'bbp_converter_setting_callback_convert_users', 'bbpress_converter', 'bbpress_converter_opt' );
  99. register_setting ( 'bbpress_converter_opt', '_bbp_converter_convert_users', 'intval' );
  100. // Restart
  101. add_settings_field( '_bbp_converter_restart', __( 'Start Over', 'bbpress' ), 'bbp_converter_setting_callback_restart', 'bbpress_converter', 'bbpress_converter_opt' );
  102. register_setting ( 'bbpress_converter_opt', '_bbp_converter_restart', 'intval' );
  103. // Clean
  104. add_settings_field( '_bbp_converter_clean', __( 'Purge Previous Import', 'bbpress' ), 'bbp_converter_setting_callback_clean', 'bbpress_converter', 'bbpress_converter_opt' );
  105. register_setting ( 'bbpress_converter_opt', '_bbp_converter_clean', 'intval' );
  106. }
  107. /**
  108. * Admin scripts
  109. *
  110. * @since bbPress (r3813)
  111. */
  112. public function admin_head() { ?>
  113. <style type="text/css" media="screen">
  114. /*<![CDATA[*/
  115. div.bbp-converter-updated,
  116. div.bbp-converter-warning {
  117. border-radius: 3px 3px 3px 3px;
  118. border-style: solid;
  119. border-width: 1px;
  120. padding: 5px 5px 5px 5px;
  121. }
  122. div.bbp-converter-updated {
  123. height: 300px;
  124. overflow: auto;
  125. display: none;
  126. background-color: #FFFFE0;
  127. border-color: #E6DB55;
  128. font-family: monospace;
  129. font-weight: bold;
  130. }
  131. div.bbp-converter-updated p {
  132. margin: 0.5em 0;
  133. padding: 2px;
  134. float: left;
  135. clear: left;
  136. }
  137. div.bbp-converter-updated p.loading {
  138. padding: 2px 20px 2px 2px;
  139. background-image: url('<?php echo admin_url(); ?>images/wpspin_light.gif');
  140. background-repeat: no-repeat;
  141. background-position: center right;
  142. }
  143. #bbp-converter-stop {
  144. display:none;
  145. }
  146. #bbp-converter-progress {
  147. display:none;
  148. }
  149. /*]]>*/
  150. </style>
  151. <script language="javascript">
  152. var bbconverter_is_running = false;
  153. var bbconverter_run_timer;
  154. var bbconverter_delay_time = 0;
  155. function bbconverter_grab_data() {
  156. var values = {};
  157. jQuery.each(jQuery('#bbp-converter-settings').serializeArray(), function(i, field) {
  158. values[field.name] = field.value;
  159. });
  160. if( values['_bbp_converter_restart'] ) {
  161. jQuery('#_bbp_converter_restart').removeAttr("checked");
  162. }
  163. if( values['_bbp_converter_delay_time'] ) {
  164. bbconverter_delay_time = values['_bbp_converter_delay_time'] * 1000;
  165. }
  166. values['action'] = 'bbconverter_process';
  167. values['_ajax_nonce'] = '<?php echo wp_create_nonce( 'bbp_converter_process' ); ?>';
  168. return values;
  169. }
  170. function bbconverter_start() {
  171. if( false == bbconverter_is_running ) {
  172. bbconverter_is_running = true;
  173. jQuery('#bbp-converter-start').hide();
  174. jQuery('#bbp-converter-stop').show();
  175. jQuery('#bbp-converter-progress').show();
  176. bbconverter_log( '<p class="loading"><?php _e( 'Starting Conversion', 'bbpress' ); ?></p>' );
  177. bbconverter_run();
  178. }
  179. }
  180. function bbconverter_run() {
  181. jQuery.post(ajaxurl, bbconverter_grab_data(), function(response) {
  182. var response_length = response.length - 1;
  183. response = response.substring(0,response_length);
  184. bbconverter_success(response);
  185. });
  186. }
  187. function bbconverter_stop() {
  188. jQuery('#bbp-converter-start').show();
  189. jQuery('#bbp-converter-stop').hide();
  190. jQuery('#bbp-converter-progress').hide();
  191. jQuery('#bbp-converter-message p').removeClass( 'loading' );
  192. bbconverter_is_running = false;
  193. clearTimeout( bbconverter_run_timer );
  194. }
  195. function bbconverter_success(response) {
  196. bbconverter_log(response);
  197. if ( response == '<p class="loading"><?php _e( 'Conversion Complete', 'bbpress' ); ?></p>' || response.indexOf('error') > -1 ) {
  198. bbconverter_log('<p>Repair any missing information: <a href="<?php echo admin_url(); ?>tools.php?page=bbp-repair">Continue</a></p>');
  199. bbconverter_stop();
  200. } else if( bbconverter_is_running ) { // keep going
  201. jQuery('#bbp-converter-progress').show();
  202. clearTimeout( bbconverter_run_timer );
  203. bbconverter_run_timer = setTimeout( 'bbconverter_run()', bbconverter_delay_time );
  204. } else {
  205. bbconverter_stop();
  206. }
  207. }
  208. function bbconverter_log(text) {
  209. if ( jQuery('#bbp-converter-message').css('display') == 'none' ) {
  210. jQuery('#bbp-converter-message').show();
  211. }
  212. if ( text ) {
  213. jQuery('#bbp-converter-message p').removeClass( 'loading' );
  214. jQuery('#bbp-converter-message').prepend( text );
  215. }
  216. }
  217. </script>
  218. <?php
  219. }
  220. /**
  221. * Wrap the converter output in paragraph tags, so styling can be applied
  222. *
  223. * @since bbPress (r4052)
  224. *
  225. * @param string $output
  226. */
  227. private static function converter_output( $output = '' ) {
  228. // Get the last query
  229. $before = '<p class="loading">';
  230. $after = '</p>';
  231. $query = get_option( '_bbp_converter_query' );
  232. if ( ! empty( $query ) )
  233. $before = '<p class="loading" title="' . esc_attr( $query ) . '">';
  234. echo $before . $output . $after;
  235. }
  236. /**
  237. * Callback processor
  238. *
  239. * @since bbPress (r3813)
  240. */
  241. public function process_callback() {
  242. // Verify intent
  243. check_ajax_referer( 'bbp_converter_process' );
  244. if ( ! ini_get( 'safe_mode' ) ) {
  245. set_time_limit( 0 );
  246. ini_set( 'memory_limit', '256M' );
  247. ini_set( 'implicit_flush', '1' );
  248. ignore_user_abort( true );
  249. }
  250. // Save step and count so that it can be restarted.
  251. if ( ! get_option( '_bbp_converter_step' ) || ( !empty( $_POST['_bbp_converter_restart'] ) ) ) {
  252. update_option( '_bbp_converter_step', 1 );
  253. update_option( '_bbp_converter_start', 0 );
  254. }
  255. $step = (int) get_option( '_bbp_converter_step', 1 );
  256. $min = (int) get_option( '_bbp_converter_start', 0 );
  257. $count = (int) ! empty( $_POST['_bbp_converter_rows'] ) ? $_POST['_bbp_converter_rows'] : 100;
  258. $max = ( $min + $count ) - 1;
  259. $start = $min;
  260. // Bail if platform did not get saved
  261. $platform = !empty( $_POST['_bbp_converter_platform' ] ) ? $_POST['_bbp_converter_platform' ] : get_option( '_bbp_converter_platform' );
  262. if ( empty( $platform ) )
  263. return;
  264. // Include the appropriate converter.
  265. $converter = bbp_new_converter( $platform );
  266. switch ( $step ) {
  267. // STEP 1. Clean all tables.
  268. case 1 :
  269. if ( !empty( $_POST['_bbp_converter_clean'] ) ) {
  270. if ( $converter->clean( $start ) ) {
  271. update_option( '_bbp_converter_step', $step + 1 );
  272. update_option( '_bbp_converter_start', 0 );
  273. $this->sync_table( true );
  274. if ( empty( $start ) ) {
  275. $this->converter_output( __( 'No data to clean', 'bbpress' ) );
  276. }
  277. } else {
  278. update_option( '_bbp_converter_start', $max + 1 );
  279. $this->converter_output( sprintf( __( 'Deleting previously converted data (%1$s - %2$s)', 'bbpress' ), $min, $max ) );
  280. }
  281. } else {
  282. update_option( '_bbp_converter_step', $step + 1 );
  283. update_option( '_bbp_converter_start', 0 );
  284. }
  285. break;
  286. // STEP 2. Convert users.
  287. case 2 :
  288. if ( !empty( $_POST['_bbp_converter_convert_users'] ) ) {
  289. if ( $converter->convert_users( $start ) ) {
  290. update_option( '_bbp_converter_step', $step + 1 );
  291. update_option( '_bbp_converter_start', 0 );
  292. if ( empty( $start ) ) {
  293. $this->converter_output( __( 'No users to convert', 'bbpress' ) );
  294. }
  295. } else {
  296. update_option( '_bbp_converter_start', $max + 1 );
  297. $this->converter_output( sprintf( __( 'Converting users (%1$s - %2$s)', 'bbpress' ), $min, $max ) );
  298. }
  299. } else {
  300. update_option( '_bbp_converter_step', $step + 1 );
  301. update_option( '_bbp_converter_start', 0 );
  302. }
  303. break;
  304. // STEP 3. Clean passwords.
  305. case 3 :
  306. if ( !empty( $_POST['_bbp_converter_convert_users'] ) ) {
  307. if ( $converter->clean_passwords( $start ) ) {
  308. update_option( '_bbp_converter_step', $step + 1 );
  309. update_option( '_bbp_converter_start', 0 );
  310. if ( empty( $start ) ) {
  311. $this->converter_output( __( 'No passwords to clear', 'bbpress' ) );
  312. }
  313. } else {
  314. update_option( '_bbp_converter_start', $max + 1 );
  315. $this->converter_output( sprintf( __( 'Delete users WordPress default passwords (%1$s - %2$s)', 'bbpress' ), $min, $max ) );
  316. }
  317. } else {
  318. update_option( '_bbp_converter_step', $step + 1 );
  319. update_option( '_bbp_converter_start', 0 );
  320. }
  321. break;
  322. // STEP 4. Convert forums.
  323. case 4 :
  324. if ( $converter->convert_forums( $start ) ) {
  325. update_option( '_bbp_converter_step', $step + 1 );
  326. update_option( '_bbp_converter_start', 0 );
  327. if ( empty( $start ) ) {
  328. $this->converter_output( __( 'No forums to convert', 'bbpress' ) );
  329. }
  330. } else {
  331. update_option( '_bbp_converter_start', $max + 1 );
  332. $this->converter_output( sprintf( __( 'Converting forums (%1$s - %2$s)', 'bbpress' ), $min, $max ) );
  333. }
  334. break;
  335. // STEP 5. Convert forum parents.
  336. case 5 :
  337. if ( $converter->convert_forum_parents( $start ) ) {
  338. update_option( '_bbp_converter_step', $step + 1 );
  339. update_option( '_bbp_converter_start', 0 );
  340. if ( empty( $start ) ) {
  341. $this->converter_output( __( 'No forum parents to convert', 'bbpress' ) );
  342. }
  343. } else {
  344. update_option( '_bbp_converter_start', $max + 1 );
  345. $this->converter_output( sprintf( __( 'Calculating forum hierarchy (%1$s - %2$s)', 'bbpress' ), $min, $max ) );
  346. }
  347. break;
  348. // STEP 6. Convert topics.
  349. case 6 :
  350. if ( $converter->convert_topics( $start ) ) {
  351. update_option( '_bbp_converter_step', $step + 1 );
  352. update_option( '_bbp_converter_start', 0 );
  353. if ( empty( $start ) ) {
  354. $this->converter_output( __( 'No topics to convert', 'bbpress' ) );
  355. }
  356. } else {
  357. update_option( '_bbp_converter_start', $max + 1 );
  358. $this->converter_output( sprintf( __( 'Converting topics (%1$s - %2$s)', 'bbpress' ), $min, $max ) );
  359. }
  360. break;
  361. // STEP 7. Convert tags.
  362. case 7 :
  363. if ( $converter->convert_tags( $start ) ) {
  364. update_option( '_bbp_converter_step', $step + 1 );
  365. update_option( '_bbp_converter_start', 0 );
  366. if ( empty( $start ) ) {
  367. $this->converter_output( __( 'No tags to convert', 'bbpress' ) );
  368. }
  369. } else {
  370. update_option( '_bbp_converter_start', $max + 1 );
  371. $this->converter_output( sprintf( __( 'Converting topic tags (%1$s - %2$s)', 'bbpress' ), $min, $max ) );
  372. }
  373. break;
  374. // STEP 8. Convert replies.
  375. case 8 :
  376. if ( $converter->convert_replies( $start ) ) {
  377. update_option( '_bbp_converter_step', $step + 1 );
  378. update_option( '_bbp_converter_start', 0 );
  379. if ( empty( $start ) ) {
  380. $this->converter_output( __( 'No replies to convert', 'bbpress' ) );
  381. }
  382. } else {
  383. update_option( '_bbp_converter_start', $max + 1 );
  384. $this->converter_output( sprintf( __( 'Converting replies (%1$s - %2$s)', 'bbpress' ), $min, $max ) );
  385. }
  386. break;
  387. default :
  388. delete_option( '_bbp_converter_step' );
  389. delete_option( '_bbp_converter_start' );
  390. delete_option( '_bbp_converter_query' );
  391. $this->converter_output( __( 'Conversion Complete', 'bbpress' ) );
  392. break;
  393. }
  394. }
  395. /**
  396. * Create Tables for fast syncing
  397. *
  398. * @since bbPress (r3813)
  399. */
  400. public function sync_table( $drop = false ) {
  401. global $wpdb;
  402. $table_name = $wpdb->prefix . 'bbp_converter_translator';
  403. if ( ! empty( $drop ) && $wpdb->get_var( "SHOW TABLES LIKE '{$table_name}'" ) == $table_name )
  404. $wpdb->query( "DROP TABLE {$table_name}" );
  405. require_once( ABSPATH . '/wp-admin/includes/upgrade.php' );
  406. if ( !empty( $wpdb->charset ) ) {
  407. $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
  408. }
  409. if ( !empty( $wpdb->collate ) ) {
  410. $charset_collate .= " COLLATE $wpdb->collate";
  411. }
  412. /** Translator ****************************************************/
  413. $sql = "CREATE TABLE {$table_name} (
  414. meta_id mediumint(8) unsigned not null auto_increment,
  415. value_type varchar(25) null,
  416. value_id bigint(20) unsigned not null default '0',
  417. meta_key varchar(25) null,
  418. meta_value varchar(25) null,
  419. PRIMARY KEY (meta_id),
  420. KEY value_id (value_id),
  421. KEY meta_join (meta_key, meta_value) ) {$charset_collate};";
  422. dbDelta( $sql );
  423. }
  424. }
  425. /**
  426. * Base class to be extended by specific individual importers
  427. *
  428. * @since bbPress (r3813)
  429. */
  430. abstract class BBP_Converter_Base {
  431. /**
  432. * @var array() This is the field mapping array to process.
  433. */
  434. protected $field_map = array();
  435. /**
  436. * @var object This is the connection to the WordPress datbase.
  437. */
  438. protected $wpdb;
  439. /**
  440. * @var object This is the connection to the other platforms database.
  441. */
  442. protected $opdb;
  443. /**
  444. * @var int This is the max rows to process at a time.
  445. */
  446. public $max_rows;
  447. /**
  448. * @var array() Map of topic to forum. It is for optimization.
  449. */
  450. private $map_topicid_to_forumid = array();
  451. /**
  452. * @var array() Map of from old forum ids to new forum ids. It is for optimization.
  453. */
  454. private $map_forumid = array();
  455. /**
  456. * @var array() Map of from old topic ids to new topic ids. It is for optimization.
  457. */
  458. private $map_topicid = array();
  459. /**
  460. * @var array() Map of from old user ids to new user ids. It is for optimization.
  461. */
  462. private $map_userid = array();
  463. /**
  464. * @var str This is the charset for your wp database.
  465. */
  466. public $charset;
  467. /**
  468. * @var boolean Sync table available.
  469. */
  470. public $sync_table = false;
  471. /**
  472. * @var str Sync table name.
  473. */
  474. public $sync_table_name;
  475. /** Methods ***************************************************************/
  476. /**
  477. * This is the constructor and it connects to the platform databases.
  478. */
  479. public function __construct() {
  480. $this->setup_globals();
  481. }
  482. private function setup_globals() {
  483. global $wpdb;
  484. /** Get database connections ******************************************/
  485. $this->wpdb = $wpdb;
  486. $this->max_rows = (int) $_POST['_bbp_converter_rows'];
  487. $this->opdb = new wpdb( $_POST['_bbp_converter_db_user'], $_POST['_bbp_converter_db_pass'], $_POST['_bbp_converter_db_name'], $_POST['_bbp_converter_db_server'] );
  488. $this->opdb->prefix = $_POST['_bbp_converter_db_prefix'];
  489. /**
  490. * Error Reporting
  491. */
  492. $this->wpdb->show_errors();
  493. $this->opdb->show_errors();
  494. /**
  495. * Syncing
  496. */
  497. $this->sync_table_name = $this->wpdb->prefix . 'bbp_converter_translator';
  498. if ( $this->wpdb->get_var( "SHOW TABLES LIKE '" . $this->sync_table_name . "'" ) == $this->sync_table_name ) {
  499. $this->sync_table = true;
  500. } else {
  501. $this->sync_table = false;
  502. }
  503. /**
  504. * Charset
  505. */
  506. if ( empty( $this->wpdb->charset ) ) {
  507. $this->charset = 'UTF8';
  508. } else {
  509. $this->charset = $this->wpdb->charset;
  510. }
  511. /**
  512. * Default mapping.
  513. */
  514. /** Forum Section *****************************************************/
  515. $this->field_map[] = array(
  516. 'to_type' => 'forum',
  517. 'to_fieldname' => 'post_status',
  518. 'default' => 'publish'
  519. );
  520. $this->field_map[] = array(
  521. 'to_type' => 'forum',
  522. 'to_fieldname' => 'comment_status',
  523. 'default' => 'closed'
  524. );
  525. $this->field_map[] = array(
  526. 'to_type' => 'forum',
  527. 'to_fieldname' => 'ping_status',
  528. 'default' => 'closed'
  529. );
  530. $this->field_map[] = array(
  531. 'to_type' => 'forum',
  532. 'to_fieldname' => 'post_type',
  533. 'default' => 'forum'
  534. );
  535. /** Topic Section *****************************************************/
  536. $this->field_map[] = array(
  537. 'to_type' => 'topic',
  538. 'to_fieldname' => 'post_status',
  539. 'default' => 'publish'
  540. );
  541. $this->field_map[] = array(
  542. 'to_type' => 'topic',
  543. 'to_fieldname' => 'comment_status',
  544. 'default' => 'closed'
  545. );
  546. $this->field_map[] = array(
  547. 'to_type' => 'topic',
  548. 'to_fieldname' => 'ping_status',
  549. 'default' => 'closed'
  550. );
  551. $this->field_map[] = array(
  552. 'to_type' => 'topic',
  553. 'to_fieldname' => 'post_type',
  554. 'default' => 'topic'
  555. );
  556. /** Post Section ******************************************************/
  557. $this->field_map[] = array(
  558. 'to_type' => 'reply',
  559. 'to_fieldname' => 'post_status',
  560. 'default' => 'publish'
  561. );
  562. $this->field_map[] = array(
  563. 'to_type' => 'reply',
  564. 'to_fieldname' => 'comment_status',
  565. 'default' => 'closed'
  566. );
  567. $this->field_map[] = array(
  568. 'to_type' => 'reply',
  569. 'to_fieldname' => 'ping_status',
  570. 'default' => 'closed'
  571. );
  572. $this->field_map[] = array(
  573. 'to_type' => 'reply',
  574. 'to_fieldname' => 'post_type',
  575. 'default' => 'reply'
  576. );
  577. /** User Section ******************************************************/
  578. $this->field_map[] = array(
  579. 'to_type' => 'user',
  580. 'to_fieldname' => 'role',
  581. 'default' => get_option( 'default_role' )
  582. );
  583. }
  584. /**
  585. * Convert Forums
  586. */
  587. public function convert_forums( $start = 1 ) {
  588. return $this->convert_table( 'forum', $start );
  589. }
  590. /**
  591. * Convert Topics / Threads
  592. */
  593. public function convert_topics( $start = 1 ) {
  594. return $this->convert_table( 'topic', $start );
  595. }
  596. /**
  597. * Convert Posts
  598. */
  599. public function convert_replies( $start = 1 ) {
  600. return $this->convert_table( 'reply', $start );
  601. }
  602. /**
  603. * Convert Users
  604. */
  605. public function convert_users( $start = 1 ) {
  606. return $this->convert_table( 'user', $start );
  607. }
  608. /**
  609. * Convert Tags
  610. */
  611. public function convert_tags( $start = 1 ) {
  612. return $this->convert_table( 'tags', $start );
  613. }
  614. /**
  615. * Convert Table
  616. *
  617. * @param string to type
  618. * @param int Start row
  619. */
  620. public function convert_table( $to_type, $start ) {
  621. // Are we usig a sync table, or postmeta?
  622. if ( $this->wpdb->get_var( "SHOW TABLES LIKE '" . $this->sync_table_name . "'" ) == $this->sync_table_name ) {
  623. $this->sync_table = true;
  624. } else {
  625. $this->sync_table = false;
  626. }
  627. // Set some defaults
  628. $has_insert = false;
  629. $from_tablename = '';
  630. $field_list = $from_tables = $tablefield_array = array();
  631. // Toggle Table Name based on $to_type (destination)
  632. switch ( $to_type ) {
  633. case 'user' :
  634. $tablename = $this->wpdb->users;
  635. break;
  636. case 'tags' :
  637. $tablename = '';
  638. break;
  639. default :
  640. $tablename = $this->wpdb->posts;
  641. }
  642. // Get the fields from the destination table
  643. if ( !empty( $tablename ) ) {
  644. $tablefield_array = $this->get_fields( $tablename );
  645. }
  646. /** Step 1 ************************************************************/
  647. // Loop through the field maps, and look for to_type matches
  648. foreach ( $this->field_map as $item ) {
  649. // Yay a match, and we have a from table, too
  650. if ( ( $item['to_type'] == $to_type ) && !empty( $item['from_tablename'] ) ) {
  651. // $from_tablename was set from a previous loop iteration
  652. if ( ! empty( $from_tablename ) ) {
  653. // Doing some joining
  654. if ( !in_array( $item['from_tablename'], $from_tables ) && in_array( $item['join_tablename'], $from_tables ) ) {
  655. $from_tablename .= ' ' . $item['join_type'] . ' JOIN ' . $this->opdb->prefix . $item['from_tablename'] . ' AS ' . $item['from_tablename'] . ' ' . $item['join_expression'];
  656. }
  657. // $from_tablename needs to be set
  658. } else {
  659. $from_tablename = $item['from_tablename'] . ' AS ' . $item['from_tablename'];
  660. }
  661. // Specific FROM expression data used
  662. if ( !empty( $item['from_expression'] ) ) {
  663. // No 'WHERE' in expression
  664. if ( stripos( $from_tablename, "WHERE" ) === false ) {
  665. $from_tablename .= ' ' . $item['from_expression'];
  666. // 'WHERE' in expression, so replace with 'AND'
  667. } else {
  668. $from_tablename .= ' ' . str_replace( "WHERE", "AND", $item['from_expression'] );
  669. }
  670. }
  671. // Add tablename and fieldname to arrays, formatted for querying
  672. $from_tables[] = $item['from_tablename'];
  673. $field_list[] = 'convert(' . $item['from_tablename'] . '.' . $item['from_fieldname'] . ' USING "' . $this->charset . '") AS ' . $item['from_fieldname'];
  674. }
  675. }
  676. /** Step 2 ************************************************************/
  677. // We have a $from_tablename, so we want to get some data to convert
  678. if ( !empty( $from_tablename ) ) {
  679. // Get some data from the old forums
  680. $field_list = array_unique( $field_list );
  681. $forum_query = 'SELECT ' . implode( ',', $field_list ) . ' FROM ' . $this->opdb->prefix . $from_tablename . ' LIMIT ' . $start . ', ' . $this->max_rows;
  682. $forum_array = $this->opdb->get_results( $forum_query, ARRAY_A );
  683. // Set this query as the last one ran
  684. update_option( '_bbp_converter_query', $forum_query );
  685. // Query returned some results
  686. if ( !empty( $forum_array ) ) {
  687. // Loop through results
  688. foreach ( (array) $forum_array as $forum ) {
  689. // Reset some defaults
  690. $insert_post = $insert_postmeta = $insert_data = array();
  691. // Loop through field map, again...
  692. foreach ( $this->field_map as $row ) {
  693. // Types matchand to_fieldname is present. This means
  694. // we have some work to do here.
  695. if ( ( $row['to_type'] == $to_type ) && ! is_null( $row['to_fieldname'] ) ) {
  696. // This row has a destination that matches one of the
  697. // columns in this table.
  698. if ( in_array( $row['to_fieldname'], $tablefield_array ) ) {
  699. // Allows us to set default fields.
  700. if ( isset( $row['default'] ) ) {
  701. $insert_post[$row['to_fieldname']] = $row['default'];
  702. // Translates a field from the old forum.
  703. } elseif ( isset( $row['callback_method'] ) ) {
  704. if ( ( 'callback_userid' == $row['callback_method'] ) && empty( $_POST['_bbp_converter_convert_users'] ) ) {
  705. $insert_post[$row['to_fieldname']] = $forum[$row['from_fieldname']];
  706. } else {
  707. $insert_post[$row['to_fieldname']] = call_user_func_array( array( $this, $row['callback_method'] ), array( $forum[$row['from_fieldname']], $forum ) );
  708. }
  709. // Maps the field from the old forum.
  710. } else {
  711. $insert_post[$row['to_fieldname']] = $forum[$row['from_fieldname']];
  712. }
  713. // Destination field is not empty, so we might need
  714. // to do some extra work or set a default.
  715. } elseif ( !empty( $row['to_fieldname'] ) ) {
  716. // Allows us to set default fields.
  717. if ( isset( $row['default'] ) ) {
  718. $insert_postmeta[$row['to_fieldname']] = $row['default'];
  719. // Translates a field from the old forum.
  720. } elseif ( isset( $row['callback_method'] ) ) {
  721. if ( ( $row['callback_method'] == 'callback_userid' ) && ( 0 == $_POST['_bbp_converter_convert_users'] ) ) {
  722. $insert_postmeta[$row['to_fieldname']] = $forum[$row['from_fieldname']];
  723. } else {
  724. $insert_postmeta[$row['to_fieldname']] = call_user_func_array( array( $this, $row['callback_method'] ), array( $forum[$row['from_fieldname']], $forum ) );
  725. }
  726. // Maps the field from the old forum.
  727. } else {
  728. $insert_postmeta[$row['to_fieldname']] = $forum[$row['from_fieldname']];
  729. }
  730. }
  731. }
  732. }
  733. /** Step 3 ************************************************/
  734. // Something to insert into the destination field
  735. if ( count( $insert_post ) > 0 || ( $to_type == 'tags' && count( $insert_postmeta ) > 0 ) ) {
  736. switch ( $to_type ) {
  737. /** New user **************************************/
  738. case 'user':
  739. if ( username_exists( $insert_post['user_login'] ) ) {
  740. $insert_post['user_login'] = 'imported_' . $insert_post['user_login'];
  741. }
  742. if ( email_exists( $insert_post['user_email'] ) ) {
  743. $insert_post['user_email'] = 'imported_' . $insert_post['user_email'];
  744. }
  745. $post_id = wp_insert_user( $insert_post );
  746. if ( is_numeric( $post_id ) ) {
  747. foreach ( $insert_postmeta as $key => $value ) {
  748. add_user_meta( $post_id, $key, $value, true );
  749. if ( '_id' == substr( $key, -3 ) && ( true === $this->sync_table ) ) {
  750. $this->wpdb->insert( $this->sync_table_name, array( 'value_type' => 'user', 'value_id' => $post_id, 'meta_key' => $key, 'meta_value' => $value ) );
  751. }
  752. }
  753. }
  754. break;
  755. /** New Topic-Tag *********************************/
  756. case 'tags':
  757. $post_id = wp_set_object_terms( $insert_postmeta['objectid'], $insert_postmeta['name'], 'topic-tag', true );
  758. break;
  759. /** Forum, Topic, Reply ***************************/
  760. default:
  761. $post_id = wp_insert_post( $insert_post );
  762. if ( is_numeric( $post_id ) ) {
  763. foreach ( $insert_postmeta as $key => $value ) {
  764. add_post_meta( $post_id, $key, $value, true );
  765. // Forums need to save their old ID for group forum association
  766. if ( ( 'forum' == $to_type ) && ( '_bbp_forum_id' == $key ) )
  767. add_post_meta( $post_id, '_bbp_old_forum_id', $value );
  768. // Topics need an extra bit of metadata
  769. // to be keyed to the new post_id
  770. if ( ( 'topic' == $to_type ) && ( '_bbp_topic_id' == $key ) ) {
  771. // Update the live topic ID
  772. update_post_meta( $post_id, $key, $post_id );
  773. // Save the old topic ID
  774. add_post_meta( $post_id, '_bbp_old_topic_id', $value );
  775. if ( '_id' == substr( $key, -3 ) && ( true === $this->sync_table ) ) {
  776. $this->wpdb->insert( $this->sync_table_name, array( 'value_type' => 'post', 'value_id' => $post_id, 'meta_key' => '_bbp_topic_id', 'meta_value' => $post_id ) );
  777. $this->wpdb->insert( $this->sync_table_name, array( 'value_type' => 'post', 'value_id' => $post_id, 'meta_key' => '_bbp_old_topic_id', 'meta_value' => $value ) );
  778. }
  779. } elseif ( '_id' == substr( $key, -3 ) && ( true === $this->sync_table ) ) {
  780. $this->wpdb->insert( $this->sync_table_name, array( 'value_type' => 'post', 'value_id' => $post_id, 'meta_key' => $key, 'meta_value' => $value ) );
  781. }
  782. }
  783. }
  784. break;
  785. }
  786. $has_insert = true;
  787. }
  788. }
  789. }
  790. }
  791. return ! $has_insert;
  792. }
  793. public function convert_forum_parents( $start ) {
  794. $has_update = false;
  795. if ( !empty( $this->sync_table ) ) {
  796. $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;
  797. } else {
  798. $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;
  799. }
  800. update_option( '_bbp_converter_query', $query );
  801. $forum_array = $this->wpdb->get_results( $query );
  802. foreach ( (array) $forum_array as $row ) {
  803. $parent_id = $this->callback_forumid( $row->meta_value );
  804. $this->wpdb->query( 'UPDATE ' . $this->wpdb->posts . ' SET post_parent = "' . $parent_id . '" WHERE ID = "' . $row->value_id . '" LIMIT 1' );
  805. $has_update = true;
  806. }
  807. return ! $has_update;
  808. }
  809. /**
  810. * This method deletes data from the wp database.
  811. */
  812. public function clean( $start ) {
  813. $start = 0;
  814. $has_delete = false;
  815. /** Delete bbconverter topics/forums/posts ****************************/
  816. if ( true === $this->sync_table ) {
  817. $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;
  818. } else {
  819. $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;
  820. }
  821. update_option( '_bbp_converter_query', $query );
  822. $posts = $this->wpdb->get_results( $query, ARRAY_A );
  823. if ( isset( $posts[0] ) && ! empty( $posts[0]['value_id'] ) ) {
  824. foreach ( (array) $posts as $value ) {
  825. wp_delete_post( $value['value_id'], true );
  826. }
  827. $has_delete = true;
  828. }
  829. /** Delete bbconverter users ******************************************/
  830. if ( true === $this->sync_table ) {
  831. $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;
  832. } else {
  833. $query = 'SELECT user_id AS value_id FROM ' . $this->wpdb->usermeta . ' WHERE meta_key = "_bbp_user_id" LIMIT ' . $this->max_rows;
  834. }
  835. update_option( '_bbp_converter_query', $query );
  836. $users = $this->wpdb->get_results( $query, ARRAY_A );
  837. if ( !empty( $users ) ) {
  838. foreach ( $users as $value ) {
  839. wp_delete_user( $value['value_id'] );
  840. }
  841. $has_delete = true;
  842. }
  843. unset( $posts );
  844. unset( $users );
  845. return ! $has_delete;
  846. }
  847. /**
  848. * This method deletes passwords from the wp database.
  849. *
  850. * @param int Start row
  851. */
  852. public function clean_passwords( $start ) {
  853. $has_delete = false;
  854. /** Delete bbconverter passwords **************************************/
  855. $query = 'SELECT user_id, meta_value FROM ' . $this->wpdb->usermeta . ' WHERE meta_key = "_bbp_password" LIMIT ' . $start . ', ' . $this->max_rows;
  856. update_option( '_bbp_converter_query', $query );
  857. $bbconverter = $this->wpdb->get_results( $query, ARRAY_A );
  858. if ( !empty( $bbconverter ) ) {
  859. foreach ( $bbconverter as $value ) {
  860. if ( is_serialized( $value['meta_value'] ) ) {
  861. $this->wpdb->query( 'UPDATE ' . $this->wpdb->users . ' ' . 'SET user_pass = "" ' . 'WHERE ID = "' . $value['user_id'] . '"' );
  862. } else {
  863. $this->wpdb->query( 'UPDATE ' . $this->wpdb->users . ' ' . 'SET user_pass = "' . $value['meta_value'] . '" ' . 'WHERE ID = "' . $value['user_id'] . '"' );
  864. $this->wpdb->query( 'DELETE FROM ' . $this->wpdb->usermeta . ' WHERE meta_key = "_bbp_password" AND user_id = "' . $value['user_id'] . '"' );
  865. }
  866. }
  867. $has_delete = true;
  868. }
  869. return ! $has_delete;
  870. }
  871. /**
  872. * This method implements the authentication for the different forums.
  873. *
  874. * @param string Unencoded password.
  875. */
  876. abstract protected function authenticate_pass( $password, $hash );
  877. /**
  878. * Info
  879. */
  880. abstract protected function info();
  881. /**
  882. * This method grabs appropriate fields from the table specified
  883. *
  884. * @param string The table name to grab fields from
  885. */
  886. private function get_fields( $tablename ) {
  887. $rval = array();
  888. $field_array = $this->wpdb->get_results( 'DESCRIBE ' . $tablename, ARRAY_A );
  889. foreach ( $field_array as $field ) {
  890. $rval[] = $field['Field'];
  891. }
  892. if ( $tablename == $this->wpdb->users ) {
  893. $rval[] = 'role';
  894. $rval[] = 'yim';
  895. $rval[] = 'aim';
  896. $rval[] = 'jabber';
  897. }
  898. return $rval;
  899. }
  900. /** Callbacks *************************************************************/
  901. /**
  902. * Run password through wp_hash_password()
  903. *
  904. * @param string $username
  905. * @param string $password
  906. */
  907. public function callback_pass( $username, $password ) {
  908. $user = $this->wpdb->get_row( $this->wpdb->prepare( 'SELECT * FROM ' . $this->wpdb->users . ' WHERE user_login = "%s" AND user_pass = "" LIMIT 1', $username ) );
  909. if ( !empty( $user ) ) {
  910. $usermeta = $this->wpdb->get_row( 'SELECT * FROM ' . $this->wpdb->usermeta . ' WHERE meta_key = "_bbp_password" AND user_id = "' . $user->ID . '" LIMIT 1' );
  911. if ( !empty( $usermeta ) ) {
  912. if ( $this->authenticate_pass( $password, $usermeta->meta_value ) ) {
  913. $this->wpdb->query( 'UPDATE ' . $this->wpdb->users . ' ' . 'SET user_pass = "' . wp_hash_password( $password ) . '" ' . 'WHERE ID = "' . $user->ID . '"' );
  914. $this->wpdb->query( 'DELETE FROM ' . $this->wpdb->usermeta . ' WHERE meta_key = "_bbp_password" AND user_id = "' . $user->ID . '"' );
  915. }
  916. }
  917. }
  918. }
  919. /**
  920. * A mini cache system to reduce database calls to forum ID's
  921. *
  922. * @param string $field
  923. * @return string
  924. */
  925. private function callback_forumid( $field ) {
  926. if ( !isset( $this->map_forumid[$field] ) ) {
  927. if ( !empty( $this->sync_table ) ) {
  928. $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 ) );
  929. } else {
  930. $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 ) );
  931. }
  932. if ( !is_null( $row ) ) {
  933. $this->map_forumid[$field] = $row->value_id;
  934. } else {
  935. $this->map_forumid[$field] = 0;
  936. }
  937. }
  938. return $this->map_forumid[$field];
  939. }
  940. /**
  941. * A mini cache system to reduce database calls to topic ID's
  942. *
  943. * @param string $field
  944. * @return string
  945. */
  946. private function callback_topicid( $field ) {
  947. if ( !isset( $this->map_topicid[$field] ) ) {
  948. if ( !empty( $this->sync_table ) ) {
  949. $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 ) );
  950. } else {
  951. $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 ) );
  952. }
  953. if ( !is_null( $row ) ) {
  954. $this->map_topicid[$field] = $row->value_id;
  955. } else {
  956. $this->map_topicid[$field] = 0;
  957. }
  958. }
  959. return $this->map_topicid[$field];
  960. }
  961. /**
  962. * A mini cache system to reduce database calls to user ID's
  963. *
  964. * @param string $field
  965. * @return string
  966. */
  967. private function callback_userid( $field ) {
  968. if ( !isset( $this->map_userid[$field] ) ) {
  969. if ( !empty( $this->sync_table ) ) {
  970. $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 ) );
  971. } else {
  972. $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 ) );
  973. }
  974. if ( !is_null( $row ) ) {
  975. $this->map_userid[$field] = $row->value_id;
  976. } else {
  977. if ( !empty( $_POST['_bbp_converter_convert_users'] ) && ( $_POST['_bbp_converter_convert_users'] == 1 ) ) {
  978. $this->map_userid[$field] = 0;
  979. } else {
  980. $this->map_userid[$field] = $field;
  981. }
  982. }
  983. }
  984. return $this->map_userid[$field];
  985. }
  986. /**
  987. * A mini cache system to reduce database calls map topics ID's to forum ID's
  988. *
  989. * @param string $field
  990. * @return string
  991. */
  992. private function callback_topicid_to_forumid( $field ) {
  993. $topicid = $this->callback_topicid( $field );
  994. if ( empty( $topicid ) ) {
  995. $this->map_topicid_to_forumid[$topicid] = 0;
  996. } elseif ( ! isset( $this->map_topicid_to_forumid[$topicid] ) ) {
  997. $row = $this->wpdb->get_row( 'SELECT post_parent FROM ' . $this->wpdb->posts . ' WHERE ID = "' . $topicid . '" LIMIT 1' );
  998. if ( !is_null( $row ) ) {
  999. $this->map_topicid_to_forumid[$topicid] = $row->post_parent;
  1000. } else {
  1001. $this->map_topicid_to_forumid[$topicid] = 0;
  1002. }
  1003. }
  1004. return $this->map_topicid_to_forumid[$topicid];
  1005. }
  1006. protected function callback_slug( $field ) {
  1007. return sanitize_title( $field );
  1008. }
  1009. protected function callback_negative( $field ) {
  1010. if ( $field < 0 ) {
  1011. return 0;
  1012. } else {
  1013. return $field;
  1014. }
  1015. }
  1016. protected function callback_html( $field ) {
  1017. require_once( bbpress()->admin->admin_dir . 'parser.php' );
  1018. $bbcode = BBCode::getInstance();
  1019. return html_entity_decode( $bbcode->Parse( $field ) );
  1020. }
  1021. protected function callback_null( $field ) {
  1022. if ( is_null( $field ) ) {
  1023. return '';
  1024. } else {
  1025. return $field;
  1026. }
  1027. }
  1028. protected function callback_datetime( $field ) {
  1029. if ( is_numeric( $field ) ) {
  1030. return date( 'Y-m-d H:i:s', $field );
  1031. } else {
  1032. return date( 'Y-m-d H:i:s', strtotime( $field ) );
  1033. }
  1034. }
  1035. }
  1036. /**
  1037. * This is a function that is purposely written to look like a "new" statement.
  1038. * It is basically a dynamic loader that will load in the platform conversion
  1039. * of your choice.
  1040. *
  1041. * @param string $platform Name of valid platform class.
  1042. */
  1043. function bbp_new_converter( $platform ) {
  1044. $found = false;
  1045. if ( $curdir = opendir( bbpress()->admin->admin_dir . 'converters/' ) ) {
  1046. while ( $file = readdir( $curdir ) ) {
  1047. if ( stristr( $file, '.php' ) && stristr( $file, 'index' ) === FALSE ) {
  1048. $file = preg_replace( '/.php/', '', $file );
  1049. if ( $platform == $file ) {
  1050. $found = true;
  1051. continue;
  1052. }
  1053. }
  1054. }
  1055. closedir( $curdir );
  1056. }
  1057. if ( true === $found ) {
  1058. require_once( bbpress()->admin->admin_dir . 'converters/' . $platform . '.php' );
  1059. return new $platform;
  1060. } else {
  1061. return null;
  1062. }
  1063. }