PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/Thane2376/death-edge.ru
PHP | 1479 lines | 891 code | 270 blank | 318 comment | 205 complexity | e8fce5a7fc4a69a438833333f26bf5bf MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, LGPL-3.0, AGPL-1.0
  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 esc_html_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 esc_html_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. Stick topics.
  362. case 7 :
  363. if ( $converter->convert_topic_stickies( $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 stickies to stick', 'bbpress' ) );
  368. }
  369. } else {
  370. update_option( '_bbp_converter_start', $max + 1 );
  371. $this->converter_output( sprintf( __( 'Calculating topic stickies (%1$s - %2$s)', 'bbpress' ), $min, $max ) );
  372. }
  373. break;
  374. // STEP 8. Stick to front topics (Super Sicky).
  375. case 8 :
  376. if ( $converter->convert_topic_super_stickies( $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 super stickies to stick', 'bbpress' ) );
  381. }
  382. } else {
  383. update_option( '_bbp_converter_start', $max + 1 );
  384. $this->converter_output( sprintf( __( 'Calculating topic super stickies (%1$s - %2$s)', 'bbpress' ), $min, $max ) );
  385. }
  386. break;
  387. // STEP 9. Convert tags.
  388. case 9 :
  389. if ( $converter->convert_tags( $start ) ) {
  390. update_option( '_bbp_converter_step', $step + 1 );
  391. update_option( '_bbp_converter_start', 0 );
  392. if ( empty( $start ) ) {
  393. $this->converter_output( __( 'No tags to convert', 'bbpress' ) );
  394. }
  395. } else {
  396. update_option( '_bbp_converter_start', $max + 1 );
  397. $this->converter_output( sprintf( __( 'Converting topic tags (%1$s - %2$s)', 'bbpress' ), $min, $max ) );
  398. }
  399. break;
  400. // STEP 10. Convert replies.
  401. case 10 :
  402. if ( $converter->convert_replies( $start ) ) {
  403. update_option( '_bbp_converter_step', $step + 1 );
  404. update_option( '_bbp_converter_start', 0 );
  405. if ( empty( $start ) ) {
  406. $this->converter_output( __( 'No replies to convert', 'bbpress' ) );
  407. }
  408. } else {
  409. update_option( '_bbp_converter_start', $max + 1 );
  410. $this->converter_output( sprintf( __( 'Converting replies (%1$s - %2$s)', 'bbpress' ), $min, $max ) );
  411. }
  412. break;
  413. // STEP 11. Convert reply_to parents.
  414. case 11 :
  415. if ( $converter->convert_reply_to_parents( $start ) ) {
  416. update_option( '_bbp_converter_step', $step + 1 );
  417. update_option( '_bbp_converter_start', 0 );
  418. if ( empty( $start ) ) {
  419. $this->converter_output( __( 'No reply_to parents to convert', 'bbpress' ) );
  420. }
  421. } else {
  422. update_option( '_bbp_converter_start', $max + 1 );
  423. $this->converter_output( sprintf( __( 'Calculating reply_to parents (%1$s - %2$s)', 'bbpress' ), $min, $max ) );
  424. }
  425. break;
  426. default :
  427. delete_option( '_bbp_converter_step' );
  428. delete_option( '_bbp_converter_start' );
  429. delete_option( '_bbp_converter_query' );
  430. $this->converter_output( __( 'Conversion Complete', 'bbpress' ) );
  431. break;
  432. }
  433. }
  434. /**
  435. * Create Tables for fast syncing
  436. *
  437. * @since bbPress (r3813)
  438. */
  439. public function sync_table( $drop = false ) {
  440. global $wpdb;
  441. $table_name = $wpdb->prefix . 'bbp_converter_translator';
  442. if ( ! empty( $drop ) && $wpdb->get_var( "SHOW TABLES LIKE '{$table_name}'" ) == $table_name )
  443. $wpdb->query( "DROP TABLE {$table_name}" );
  444. require_once( ABSPATH . '/wp-admin/includes/upgrade.php' );
  445. if ( !empty( $wpdb->charset ) ) {
  446. $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
  447. }
  448. if ( !empty( $wpdb->collate ) ) {
  449. $charset_collate .= " COLLATE $wpdb->collate";
  450. }
  451. /** Translator ****************************************************/
  452. $sql = "CREATE TABLE {$table_name} (
  453. meta_id mediumint(8) unsigned not null auto_increment,
  454. value_type varchar(25) null,
  455. value_id bigint(20) unsigned not null default '0',
  456. meta_key varchar(25) null,
  457. meta_value varchar(25) null,
  458. PRIMARY KEY (meta_id),
  459. KEY value_id (value_id),
  460. KEY meta_join (meta_key, meta_value) ) {$charset_collate};";
  461. dbDelta( $sql );
  462. }
  463. }
  464. /**
  465. * Base class to be extended by specific individual importers
  466. *
  467. * @since bbPress (r3813)
  468. */
  469. abstract class BBP_Converter_Base {
  470. /**
  471. * @var array() This is the field mapping array to process.
  472. */
  473. protected $field_map = array();
  474. /**
  475. * @var object This is the connection to the WordPress datbase.
  476. */
  477. protected $wpdb;
  478. /**
  479. * @var object This is the connection to the other platforms database.
  480. */
  481. protected $opdb;
  482. /**
  483. * @var int This is the max rows to process at a time.
  484. */
  485. public $max_rows;
  486. /**
  487. * @var array() Map of topic to forum. It is for optimization.
  488. */
  489. private $map_topicid_to_forumid = array();
  490. /**
  491. * @var array() Map of from old forum ids to new forum ids. It is for optimization.
  492. */
  493. private $map_forumid = array();
  494. /**
  495. * @var array() Map of from old topic ids to new topic ids. It is for optimization.
  496. */
  497. private $map_topicid = array();
  498. /**
  499. * @var array() Map of from old reply_to ids to new reply_to ids. It is for optimization.
  500. */
  501. private $map_reply_to = array();
  502. /**
  503. * @var array() Map of from old user ids to new user ids. It is for optimization.
  504. */
  505. private $map_userid = array();
  506. /**
  507. * @var str This is the charset for your wp database.
  508. */
  509. public $charset;
  510. /**
  511. * @var boolean Sync table available.
  512. */
  513. public $sync_table = false;
  514. /**
  515. * @var str Sync table name.
  516. */
  517. public $sync_table_name;
  518. /** Methods ***************************************************************/
  519. /**
  520. * This is the constructor and it connects to the platform databases.
  521. */
  522. public function __construct() {
  523. $this->setup_globals();
  524. }
  525. private function setup_globals() {
  526. global $wpdb;
  527. /** Get database connections ******************************************/
  528. $this->wpdb = $wpdb;
  529. $this->max_rows = (int) $_POST['_bbp_converter_rows'];
  530. $this->opdb = new wpdb( $_POST['_bbp_converter_db_user'], $_POST['_bbp_converter_db_pass'], $_POST['_bbp_converter_db_name'], $_POST['_bbp_converter_db_server'] );
  531. $this->opdb->prefix = $_POST['_bbp_converter_db_prefix'];
  532. /**
  533. * Error Reporting
  534. */
  535. $this->wpdb->show_errors();
  536. $this->opdb->show_errors();
  537. /**
  538. * Syncing
  539. */
  540. $this->sync_table_name = $this->wpdb->prefix . 'bbp_converter_translator';
  541. if ( $this->wpdb->get_var( "SHOW TABLES LIKE '" . $this->sync_table_name . "'" ) == $this->sync_table_name ) {
  542. $this->sync_table = true;
  543. } else {
  544. $this->sync_table = false;
  545. }
  546. /**
  547. * Charset
  548. */
  549. if ( empty( $this->wpdb->charset ) ) {
  550. $this->charset = 'UTF8';
  551. } else {
  552. $this->charset = $this->wpdb->charset;
  553. }
  554. /**
  555. * Default mapping.
  556. */
  557. /** Forum Section *****************************************************/
  558. $this->field_map[] = array(
  559. 'to_type' => 'forum',
  560. 'to_fieldname' => 'post_status',
  561. 'default' => 'publish'
  562. );
  563. $this->field_map[] = array(
  564. 'to_type' => 'forum',
  565. 'to_fieldname' => 'comment_status',
  566. 'default' => 'closed'
  567. );
  568. $this->field_map[] = array(
  569. 'to_type' => 'forum',
  570. 'to_fieldname' => 'ping_status',
  571. 'default' => 'closed'
  572. );
  573. $this->field_map[] = array(
  574. 'to_type' => 'forum',
  575. 'to_fieldname' => 'post_type',
  576. 'default' => 'forum'
  577. );
  578. /** Topic Section *****************************************************/
  579. $this->field_map[] = array(
  580. 'to_type' => 'topic',
  581. 'to_fieldname' => 'post_status',
  582. 'default' => 'publish'
  583. );
  584. $this->field_map[] = array(
  585. 'to_type' => 'topic',
  586. 'to_fieldname' => 'comment_status',
  587. 'default' => 'closed'
  588. );
  589. $this->field_map[] = array(
  590. 'to_type' => 'topic',
  591. 'to_fieldname' => 'ping_status',
  592. 'default' => 'closed'
  593. );
  594. $this->field_map[] = array(
  595. 'to_type' => 'topic',
  596. 'to_fieldname' => 'post_type',
  597. 'default' => 'topic'
  598. );
  599. /** Post Section ******************************************************/
  600. $this->field_map[] = array(
  601. 'to_type' => 'reply',
  602. 'to_fieldname' => 'post_status',
  603. 'default' => 'publish'
  604. );
  605. $this->field_map[] = array(
  606. 'to_type' => 'reply',
  607. 'to_fieldname' => 'comment_status',
  608. 'default' => 'closed'
  609. );
  610. $this->field_map[] = array(
  611. 'to_type' => 'reply',
  612. 'to_fieldname' => 'ping_status',
  613. 'default' => 'closed'
  614. );
  615. $this->field_map[] = array(
  616. 'to_type' => 'reply',
  617. 'to_fieldname' => 'post_type',
  618. 'default' => 'reply'
  619. );
  620. /** User Section ******************************************************/
  621. $this->field_map[] = array(
  622. 'to_type' => 'user',
  623. 'to_fieldname' => 'role',
  624. 'default' => get_option( 'default_role' )
  625. );
  626. }
  627. /**
  628. * Convert Forums
  629. */
  630. public function convert_forums( $start = 1 ) {
  631. return $this->convert_table( 'forum', $start );
  632. }
  633. /**
  634. * Convert Topics / Threads
  635. */
  636. public function convert_topics( $start = 1 ) {
  637. return $this->convert_table( 'topic', $start );
  638. }
  639. /**
  640. * Convert Posts
  641. */
  642. public function convert_replies( $start = 1 ) {
  643. return $this->convert_table( 'reply', $start );
  644. }
  645. /**
  646. * Convert Users
  647. */
  648. public function convert_users( $start = 1 ) {
  649. return $this->convert_table( 'user', $start );
  650. }
  651. /**
  652. * Convert Tags
  653. */
  654. public function convert_tags( $start = 1 ) {
  655. return $this->convert_table( 'tags', $start );
  656. }
  657. /**
  658. * Convert Table
  659. *
  660. * @param string to type
  661. * @param int Start row
  662. */
  663. public function convert_table( $to_type, $start ) {
  664. // Are we usig a sync table, or postmeta?
  665. if ( $this->wpdb->get_var( "SHOW TABLES LIKE '" . $this->sync_table_name . "'" ) == $this->sync_table_name ) {
  666. $this->sync_table = true;
  667. } else {
  668. $this->sync_table = false;
  669. }
  670. // Set some defaults
  671. $has_insert = false;
  672. $from_tablename = '';
  673. $field_list = $from_tables = $tablefield_array = array();
  674. // Toggle Table Name based on $to_type (destination)
  675. switch ( $to_type ) {
  676. case 'user' :
  677. $tablename = $this->wpdb->users;
  678. break;
  679. case 'tags' :
  680. $tablename = '';
  681. break;
  682. default :
  683. $tablename = $this->wpdb->posts;
  684. }
  685. // Get the fields from the destination table
  686. if ( !empty( $tablename ) ) {
  687. $tablefield_array = $this->get_fields( $tablename );
  688. }
  689. /** Step 1 ************************************************************/
  690. // Loop through the field maps, and look for to_type matches
  691. foreach ( $this->field_map as $item ) {
  692. // Yay a match, and we have a from table, too
  693. if ( ( $item['to_type'] == $to_type ) && !empty( $item['from_tablename'] ) ) {
  694. // $from_tablename was set from a previous loop iteration
  695. if ( ! empty( $from_tablename ) ) {
  696. // Doing some joining
  697. if ( !in_array( $item['from_tablename'], $from_tables ) && in_array( $item['join_tablename'], $from_tables ) ) {
  698. $from_tablename .= ' ' . $item['join_type'] . ' JOIN ' . $this->opdb->prefix . $item['from_tablename'] . ' AS ' . $item['from_tablename'] . ' ' . $item['join_expression'];
  699. }
  700. // $from_tablename needs to be set
  701. } else {
  702. $from_tablename = $item['from_tablename'] . ' AS ' . $item['from_tablename'];
  703. }
  704. // Specific FROM expression data used
  705. if ( !empty( $item['from_expression'] ) ) {
  706. // No 'WHERE' in expression
  707. if ( stripos( $from_tablename, "WHERE" ) === false ) {
  708. $from_tablename .= ' ' . $item['from_expression'];
  709. // 'WHERE' in expression, so replace with 'AND'
  710. } else {
  711. $from_tablename .= ' ' . str_replace( "WHERE", "AND", $item['from_expression'] );
  712. }
  713. }
  714. // Add tablename and fieldname to arrays, formatted for querying
  715. $from_tables[] = $item['from_tablename'];
  716. $field_list[] = 'convert(' . $item['from_tablename'] . '.' . $item['from_fieldname'] . ' USING "' . $this->charset . '") AS ' . $item['from_fieldname'];
  717. }
  718. }
  719. /** Step 2 ************************************************************/
  720. // We have a $from_tablename, so we want to get some data to convert
  721. if ( !empty( $from_tablename ) ) {
  722. // Get some data from the old forums
  723. $field_list = array_unique( $field_list );
  724. $forum_query = 'SELECT ' . implode( ',', $field_list ) . ' FROM ' . $this->opdb->prefix . $from_tablename . ' LIMIT ' . $start . ', ' . $this->max_rows;
  725. $forum_array = $this->opdb->get_results( $forum_query, ARRAY_A );
  726. // Set this query as the last one ran
  727. update_option( '_bbp_converter_query', $forum_query );
  728. // Query returned some results
  729. if ( !empty( $forum_array ) ) {
  730. // Loop through results
  731. foreach ( (array) $forum_array as $forum ) {
  732. // Reset some defaults
  733. $insert_post = $insert_postmeta = $insert_data = array();
  734. // Loop through field map, again...
  735. foreach ( $this->field_map as $row ) {
  736. // Types matchand to_fieldname is present. This means
  737. // we have some work to do here.
  738. if ( ( $row['to_type'] == $to_type ) && ! is_null( $row['to_fieldname'] ) ) {
  739. // This row has a destination that matches one of the
  740. // columns in this table.
  741. if ( in_array( $row['to_fieldname'], $tablefield_array ) ) {
  742. // Allows us to set default fields.
  743. if ( isset( $row['default'] ) ) {
  744. $insert_post[$row['to_fieldname']] = $row['default'];
  745. // Translates a field from the old forum.
  746. } elseif ( isset( $row['callback_method'] ) ) {
  747. if ( ( 'callback_userid' == $row['callback_method'] ) && empty( $_POST['_bbp_converter_convert_users'] ) ) {
  748. $insert_post[$row['to_fieldname']] = $forum[$row['from_fieldname']];
  749. } else {
  750. $insert_post[$row['to_fieldname']] = call_user_func_array( array( $this, $row['callback_method'] ), array( $forum[$row['from_fieldname']], $forum ) );
  751. }
  752. // Maps the field from the old forum.
  753. } else {
  754. $insert_post[$row['to_fieldname']] = $forum[$row['from_fieldname']];
  755. }
  756. // Destination field is not empty, so we might need
  757. // to do some extra work or set a default.
  758. } elseif ( !empty( $row['to_fieldname'] ) ) {
  759. // Allows us to set default fields.
  760. if ( isset( $row['default'] ) ) {
  761. $insert_postmeta[$row['to_fieldname']] = $row['default'];
  762. // Translates a field from the old forum.
  763. } elseif ( isset( $row['callback_method'] ) ) {
  764. if ( ( $row['callback_method'] == 'callback_userid' ) && ( 0 == $_POST['_bbp_converter_convert_users'] ) ) {
  765. $insert_postmeta[$row['to_fieldname']] = $forum[$row['from_fieldname']];
  766. } else {
  767. $insert_postmeta[$row['to_fieldname']] = call_user_func_array( array( $this, $row['callback_method'] ), array( $forum[$row['from_fieldname']], $forum ) );
  768. }
  769. // Maps the field from the old forum.
  770. } else {
  771. $insert_postmeta[$row['to_fieldname']] = $forum[$row['from_fieldname']];
  772. }
  773. }
  774. }
  775. }
  776. /** Step 3 ************************************************/
  777. // Something to insert into the destination field
  778. if ( count( $insert_post ) > 0 || ( $to_type == 'tags' && count( $insert_postmeta ) > 0 ) ) {
  779. switch ( $to_type ) {
  780. /** New user **************************************/
  781. case 'user':
  782. if ( username_exists( $insert_post['user_login'] ) ) {
  783. $insert_post['user_login'] = 'imported_' . $insert_post['user_login'];
  784. }
  785. if ( email_exists( $insert_post['user_email'] ) ) {
  786. $insert_post['user_email'] = 'imported_' . $insert_post['user_email'];
  787. }
  788. $post_id = wp_insert_user( $insert_post );
  789. if ( is_numeric( $post_id ) ) {
  790. foreach ( $insert_postmeta as $key => $value ) {
  791. add_user_meta( $post_id, $key, $value, true );
  792. if ( '_id' == substr( $key, -3 ) && ( true === $this->sync_table ) ) {
  793. $this->wpdb->insert( $this->sync_table_name, array( 'value_type' => 'user', 'value_id' => $post_id, 'meta_key' => $key, 'meta_value' => $value ) );
  794. }
  795. }
  796. }
  797. break;
  798. /** New Topic-Tag *********************************/
  799. case 'tags':
  800. $post_id = wp_set_object_terms( $insert_postmeta['objectid'], $insert_postmeta['name'], 'topic-tag', true );
  801. $term = get_term_by( 'name', $insert_postmeta['name'], 'topic-tag');
  802. if ( false !== $term ) {
  803. wp_update_term( $term->term_id, 'topic-tag', array(
  804. 'description' => $insert_postmeta['description'],
  805. 'slug' => $insert_postmeta['slug']
  806. ) );
  807. }
  808. break;
  809. /** Forum, Topic, Reply ***************************/
  810. default:
  811. $post_id = wp_insert_post( $insert_post );
  812. if ( is_numeric( $post_id ) ) {
  813. foreach ( $insert_postmeta as $key => $value ) {
  814. add_post_meta( $post_id, $key, $value, true );
  815. // Forums need to save their old ID for group forum association
  816. if ( ( 'forum' == $to_type ) && ( '_bbp_forum_id' == $key ) )
  817. add_post_meta( $post_id, '_bbp_old_forum_id', $value );
  818. // Topics need an extra bit of metadata
  819. // to be keyed to the new post_id
  820. if ( ( 'topic' == $to_type ) && ( '_bbp_topic_id' == $key ) ) {
  821. // Update the live topic ID
  822. update_post_meta( $post_id, $key, $post_id );
  823. // Save the old topic ID
  824. add_post_meta( $post_id, '_bbp_old_topic_id', $value );
  825. if ( '_id' == substr( $key, -3 ) && ( true === $this->sync_table ) ) {
  826. $this->wpdb->insert( $this->sync_table_name, array( 'value_type' => 'post', 'value_id' => $post_id, 'meta_key' => '_bbp_topic_id', 'meta_value' => $post_id ) );
  827. $this->wpdb->insert( $this->sync_table_name, array( 'value_type' => 'post', 'value_id' => $post_id, 'meta_key' => '_bbp_old_topic_id', 'meta_value' => $value ) );
  828. }
  829. } elseif ( '_id' == substr( $key, -3 ) && ( true === $this->sync_table ) ) {
  830. $this->wpdb->insert( $this->sync_table_name, array( 'value_type' => 'post', 'value_id' => $post_id, 'meta_key' => $key, 'meta_value' => $value ) );
  831. }
  832. // Replies need to save their old reply_to ID for hierarchical replies association
  833. if ( ( 'reply' == $to_type ) && ( '_bbp_reply_to' == $key ) ) {
  834. add_post_meta( $post_id, '_bbp_old_reply_to', $value );
  835. }
  836. }
  837. }
  838. break;
  839. }
  840. $has_insert = true;
  841. }
  842. }
  843. }
  844. }
  845. return ! $has_insert;
  846. }
  847. /**
  848. * This method converts old forum heirarchy to new bbPress heirarchy.
  849. */
  850. public function convert_forum_parents( $start ) {
  851. $has_update = false;
  852. if ( !empty( $this->sync_table ) ) {
  853. $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;
  854. } else {
  855. $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;
  856. }
  857. update_option( '_bbp_converter_query', $query );
  858. $forum_array = $this->wpdb->get_results( $query );
  859. foreach ( (array) $forum_array as $row ) {
  860. $parent_id = $this->callback_forumid( $row->meta_value );
  861. $this->wpdb->query( 'UPDATE ' . $this->wpdb->posts . ' SET post_parent = "' . $parent_id . '" WHERE ID = "' . $row->value_id . '" LIMIT 1' );
  862. $has_update = true;
  863. }
  864. return ! $has_update;
  865. }
  866. /**
  867. * This method converts old topic stickies to new bbPress stickies.
  868. *
  869. * @since bbPress (r)
  870. *
  871. * @uses WPDB $wpdb
  872. * @uses bbp_stick_topic() to set the imported topic as sticky
  873. *
  874. */
  875. public function convert_topic_stickies( $start ) {
  876. $has_update = false;
  877. if ( !empty( $this->sync_table ) ) {
  878. $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;
  879. } else {
  880. $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;
  881. }
  882. update_option( '_bbp_converter_query', $query );
  883. $sticky_array = $this->wpdb->get_results( $query );
  884. foreach ( (array) $sticky_array as $row ) {
  885. bbp_stick_topic( $row->value_id );
  886. $has_update = true;
  887. }
  888. return ! $has_update;
  889. }
  890. /**
  891. * This method converts old topic super stickies to new bbPress super stickies.
  892. *
  893. * @since bbPress (r)
  894. *
  895. * @uses WPDB $wpdb
  896. * @uses bbp_stick_topic() to set the imported topic as super sticky
  897. *
  898. */
  899. public function convert_topic_super_stickies( $start ) {
  900. $has_update = false;
  901. if ( !empty( $this->sync_table ) ) {
  902. $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;
  903. } else {
  904. $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;
  905. }
  906. update_option( '_bbp_converter_query', $query );
  907. $sticky_array = $this->wpdb->get_results( $query );
  908. foreach ( (array) $sticky_array as $row ) {
  909. $super = true;
  910. bbp_stick_topic( $row->value_id, $super );
  911. $has_update = true;
  912. }
  913. return ! $has_update;
  914. }
  915. /**
  916. * This method converts old reply_to post id to new bbPress reply_to post id.
  917. */
  918. public function convert_reply_to_parents( $start ) {
  919. $has_update = false;
  920. if ( !empty( $this->sync_table ) ) {
  921. $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;
  922. } else {
  923. $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;
  924. }
  925. update_option( '_bbp_converter_query', $query );
  926. $reply_to_array = $this->wpdb->get_results( $query );
  927. foreach ( (array) $reply_to_array as $row ) {
  928. $reply_to = $this->callback_reply_to( $row->meta_value );
  929. $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' );
  930. $has_update = true;
  931. }
  932. return ! $has_update;
  933. }
  934. /**
  935. * This method deletes data from the wp database.
  936. */
  937. public function clean( $start ) {
  938. $start = 0;
  939. $has_delete = false;
  940. /** Delete bbconverter topics/forums/posts ****************************/
  941. if ( true === $this->sync_table ) {
  942. $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;
  943. } else {
  944. $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;
  945. }
  946. update_option( '_bbp_converter_query', $query );
  947. $posts = $this->wpdb->get_results( $query, ARRAY_A );
  948. if ( isset( $posts[0] ) && ! empty( $posts[0]['value_id'] ) ) {
  949. foreach ( (array) $posts as $value ) {
  950. wp_delete_post( $value['value_id'], true );
  951. }
  952. $has_delete = true;
  953. }
  954. /** Delete bbconverter users ******************************************/
  955. if ( true === $this->sync_table ) {
  956. $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;
  957. } else {
  958. $query = 'SELECT user_id AS value_id FROM ' . $this->wpdb->usermeta . ' WHERE meta_key = "_bbp_user_id" LIMIT ' . $this->max_rows;
  959. }
  960. update_option( '_bbp_converter_query', $query );
  961. $users = $this->wpdb->get_results( $query, ARRAY_A );
  962. if ( !empty( $users ) ) {
  963. foreach ( $users as $value ) {
  964. wp_delete_user( $value['value_id'] );
  965. }
  966. $has_delete = true;
  967. }
  968. unset( $posts );
  969. unset( $users );
  970. return ! $has_delete;
  971. }
  972. /**
  973. * This method deletes passwords from the wp database.
  974. *
  975. * @param int Start row
  976. */
  977. public function clean_passwords( $start ) {
  978. $has_delete = false;
  979. /** Delete bbconverter passwords **************************************/
  980. $query = 'SELECT user_id, meta_value FROM ' . $this->wpdb->usermeta . ' WHERE meta_key = "_bbp_password" LIMIT ' . $start . ', ' . $this->max_rows;
  981. update_option( '_bbp_converter_query', $query );
  982. $bbconverter = $this->wpdb->get_results( $query, ARRAY_A );
  983. if ( !empty( $bbconverter ) ) {
  984. foreach ( $bbconverter as $value ) {
  985. if ( is_serialized( $value['meta_value'] ) ) {
  986. $this->wpdb->query( 'UPDATE ' . $this->wpdb->users . ' ' . 'SET user_pass = "" ' . 'WHERE ID = "' . $value['user_id'] . '"' );
  987. } else {
  988. $this->wpdb->query( 'UPDATE ' . $this->wpdb->users . ' ' . 'SET user_pass = "' . $value['meta_value'] . '" ' . 'WHERE ID = "' . $value['user_id'] . '"' );
  989. $this->wpdb->query( 'DELETE FROM ' . $this->wpdb->usermeta . ' WHERE meta_key = "_bbp_password" AND user_id = "' . $value['user_id'] . '"' );
  990. }
  991. }
  992. $has_delete = true;
  993. }
  994. return ! $has_delete;
  995. }
  996. /**
  997. * This method implements the authentication for the different forums.
  998. *
  999. * @param string Unencoded password.
  1000. */
  1001. abstract protected function authenticate_pass( $password, $hash );
  1002. /**
  1003. * Info
  1004. */
  1005. abstract protected function info();
  1006. /**
  1007. * This method grabs appropriate fields from the table specified
  1008. *
  1009. * @param string The table name to grab fields from
  1010. */
  1011. private function get_fields( $tablename ) {
  1012. $rval = array();
  1013. $field_array = $this->wpdb->get_results( 'DESCRIBE ' . $tablename, ARRAY_A );
  1014. foreach ( $field_array as $field ) {
  1015. $rval[] = $field['Field'];
  1016. }
  1017. if ( $tablename == $this->wpdb->users ) {
  1018. $rval[] = 'role';
  1019. $rval[] = 'yim';
  1020. $rval[] = 'aim';
  1021. $rval[] = 'jabber';
  1022. }
  1023. return $rval;
  1024. }
  1025. /** Callbacks *************************************************************/
  1026. /**
  1027. * Run password through wp_hash_password()
  1028. *
  1029. * @param string $username
  1030. * @param string $password
  1031. */
  1032. public function callback_pass( $username, $password ) {
  1033. $user = $this->wpdb->get_row( $this->wpdb->prepare( 'SELECT * FROM ' . $this->wpdb->users . ' WHERE user_login = "%s" AND user_pass = "" LIMIT 1', $username ) );
  1034. if ( !empty( $user ) ) {
  1035. $usermeta = $this->wpdb->get_row( 'SELECT * FROM ' . $this->wpdb->usermeta . ' WHERE meta_key = "_bbp_password" AND user_id = "' . $user->ID . '" LIMIT 1' );
  1036. if ( !empty( $usermeta ) ) {
  1037. if ( $this->authenticate_pass( $password, $usermeta->meta_value ) ) {
  1038. $this->wpdb->query( 'UPDATE ' . $this->wpdb->users . ' ' . 'SET user_pass = "' . wp_hash_password( $password ) . '" ' . 'WHERE ID = "' . $user->ID . '"' );
  1039. $this->wpdb->query( 'DELETE FROM ' . $this->wpdb->usermeta . ' WHERE meta_key = "_bbp_password" AND user_id = "' . $user->ID . '"' );
  1040. }
  1041. }
  1042. }
  1043. }
  1044. /**
  1045. * A mini cache system to reduce database calls to forum ID's
  1046. *
  1047. * @param string $field
  1048. * @return string
  1049. */
  1050. private function callback_forumid( $field ) {
  1051. if ( !isset( $this->map_forumid[$field] ) ) {
  1052. if ( !empty( $this->sync_table ) ) {
  1053. $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 ) );
  1054. } else {
  1055. $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 ) );
  1056. }
  1057. if ( !is_null( $row ) ) {
  1058. $this->map_forumid[$field] = $row->value_id;
  1059. } else {
  1060. $this->map_forumid[$field] = 0;
  1061. }
  1062. }
  1063. return $this->map_forumid[$field];
  1064. }
  1065. /**
  1066. * A mini cache system to reduce database calls to topic ID's
  1067. *
  1068. * @param string $field
  1069. * @return string
  1070. */
  1071. private function callback_topicid( $field ) {
  1072. if ( !isset( $this->map_topicid[$field] ) ) {
  1073. if ( !empty( $this->sync_table ) ) {
  1074. $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 ) );
  1075. } else {
  1076. $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 ) );
  1077. }
  1078. if ( !is_null( $row ) ) {
  1079. $this->map_topicid[$field] = $row->value_id;
  1080. } else {
  1081. $this->map_topicid[$field] = 0;
  1082. }
  1083. }
  1084. return $this->map_topicid[$field];
  1085. }
  1086. /**
  1087. * A mini cache system to reduce database calls to reply_to post id.
  1088. *
  1089. * @param string $field
  1090. * @return string
  1091. */
  1092. private function callback_reply_to( $field ) {
  1093. if ( !isset( $this->map_reply_to[$field] ) ) {
  1094. if ( !empty( $this->sync_table ) ) {
  1095. $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 ) );
  1096. } else {
  1097. $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 ) );
  1098. }
  1099. if ( !is_null( $row ) ) {
  1100. $this->map_reply_to[$field] = $row->value_id;
  1101. } else {
  1102. $this->map_reply_to[$field] = 0;
  1103. }
  1104. }
  1105. return $this->map_reply_to[$field];
  1106. }
  1107. /**
  1108. * A mini cache system to reduce database calls to user ID's
  1109. *
  1110. * @param string $field
  1111. * @return string
  1112. */
  1113. private function callback_userid( $field ) {
  1114. if ( !isset( $this->map_userid[$field] ) ) {
  1115. if ( !empty( $this->sync_table ) ) {
  1116. $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 ) );
  1117. } else {
  1118. $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 ) );
  1119. }
  1120. if ( !is_null( $row ) ) {
  1121. $this->map_userid[$field] = $row->value_id;
  1122. } else {
  1123. if ( !empty( $_POST['_bbp_converter_convert_users'] ) && ( $_POST['_bbp_converter_convert_users'] == 1 ) ) {
  1124. $this->map_userid[$field] = 0;
  1125. } else {
  1126. $this->map_userid[$field] = $field;
  1127. }
  1128. }
  1129. }
  1130. return $this->map_userid[$field];
  1131. }
  1132. /**
  1133. * A mini cache system to reduce database calls map topics ID's to forum ID's
  1134. *
  1135. * @param string $field
  1136. * @return string
  1137. */
  1138. private function callback_topicid_to_forumid( $field ) {
  1139. $topicid = $this->callback_topicid( $field );
  1140. if ( empty( $topicid ) ) {
  1141. $this->map_topicid_to_forumid[$topicid] = 0;
  1142. } elseif ( ! isset( $this->map_topicid_to_forumid[$topicid] ) ) {
  1143. $row = $this->wpdb->get_row( 'SELECT post_parent FROM ' . $this->wpdb->posts . ' WHERE ID = "' . $topicid . '" LIMIT 1' );
  1144. if ( !is_null( $row ) ) {
  1145. $this->map_topicid_to_forumid[$topicid] = $row->post_parent;
  1146. } else {
  1147. $this->map_topicid_to_forumid[$topicid] = 0;
  1148. }
  1149. }
  1150. return $this->map_topicid_to_forumid[$topicid];
  1151. }
  1152. protected function callback_slug( $field ) {
  1153. return sanitize_title( $field );
  1154. }
  1155. protected function callback_negative( $field ) {
  1156. if ( $field < 0 ) {
  1157. return 0;
  1158. } else {
  1159. return $field;
  1160. }
  1161. }
  1162. protected function callback_html( $field ) {
  1163. require_once( bbpress()->admin->admin_dir . 'parser.php' );
  1164. $bbcode = BBCode::getInstance();
  1165. return html_entity_decode( $bbcode->Parse( $field ) );
  1166. }
  1167. protected function callback_null( $field ) {
  1168. if ( is_null( $field ) ) {
  1169. return '';
  1170. } else {
  1171. return $field;
  1172. }
  1173. }
  1174. protected function callback_datetime( $field ) {
  1175. if ( is_numeric( $field ) ) {
  1176. return date( 'Y-m-d H:i:s', $field );
  1177. } else {
  1178. return date( 'Y-m-d H:i:s', strtotime( $field ) );
  1179. }
  1180. }
  1181. }
  1182. /**
  1183. * This is a function that is purposely written to look like a "new" statement.
  1184. * It is basically a dynamic loader that will load in the platform conversion
  1185. * of your choice.
  1186. *
  1187. * @param string $platform Name of valid platform class.
  1188. */
  1189. function bbp_new_converter( $platform ) {
  1190. $found = false;
  1191. if ( $curdir = opendir( bbpress()->admin->admin_dir . 'converters/' ) ) {
  1192. while ( $file = readdir( $curdir ) ) {
  1193. if ( stristr( $file, '.php' ) && stristr( $file, 'index' ) === FALSE ) {
  1194. $file = preg_replace( '/.php/', '', $file );
  1195. if ( $platform == $file ) {
  1196. $found = true;
  1197. continue;
  1198. }
  1199. }
  1200. }
  1201. closedir( $curdir );
  1202. }
  1203. if ( true === $found ) {
  1204. require_once( bbpress()->admin->admin_dir . 'converters/' . $platform . '.php' );
  1205. return new $platform;
  1206. } else {
  1207. return null;
  1208. }
  1209. }