BBP_Converter

Main BBP_Converter Class.

Defined (1)

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

/includes/admin/converter.php  
  1. class BBP_Converter { 
  2.  
  3. /** 
  4. * The main bbPress Converter loader 
  5. * @since bbPress (r3813) 
  6. * @uses BBP_Converter::includes() Include the required files 
  7. * @uses BBP_Converter::setup_actions() Setup the actions 
  8. */ 
  9. public function __construct() { 
  10.  
  11. // "I wonder where I'll float next." 
  12. if ( empty( $_SERVER['REQUEST_METHOD'] ) ) 
  13. return; 
  14.  
  15. // Bail if request is not correct 
  16. switch ( strtoupper( $_SERVER['REQUEST_METHOD'] ) ) { 
  17.  
  18. // Converter is converting 
  19. case 'POST' : 
  20. if ( ( empty( $_POST['action'] ) || ( 'bbconverter_process' != $_POST['action'] ) ) ) 
  21. return; 
  22.  
  23. break; 
  24.  
  25. // Some other admin page 
  26. case 'GET' : 
  27. if ( ( empty( $_GET['page'] ) || ( 'bbp-converter' != $_GET['page'] ) ) ) 
  28. return; 
  29.  
  30. break; 
  31.  
  32. // Proceed with the actions 
  33. $this->setup_actions(); 
  34.  
  35. /** 
  36. * Setup the default actions 
  37. * @since bbPress (r3813) 
  38. * @uses add_action() To add various actions 
  39. */ 
  40. private function setup_actions() { 
  41.  
  42. // Attach to the admin head with our ajax requests cycle and css 
  43. add_action( 'bbp_admin_head', array( $this, 'admin_head' ) ); 
  44.  
  45. // Attach the bbConverter admin settings action to the WordPress admin init action. 
  46. add_action( 'bbp_register_admin_settings', array( $this, 'register_admin_settings' ) ); 
  47.  
  48. // Attach to the admin ajax request to process cycles 
  49. add_action( 'wp_ajax_bbconverter_process', array( $this, 'process_callback' ) ); 
  50.  
  51. /** 
  52. * Register the settings 
  53. * @since bbPress (r3813) 
  54. * @uses add_settings_section() To add our own settings section 
  55. * @uses add_settings_field() To add various settings fields 
  56. * @uses register_setting() To register various settings 
  57. */ 
  58. public function register_admin_settings() { 
  59.  
  60. // Add the main section 
  61. add_settings_section( 'bbpress_converter_main', __( 'Database Settings', 'bbpress' ), 'bbp_converter_setting_callback_main_section', 'bbpress_converter' ); 
  62.  
  63. // System Select 
  64. add_settings_field( '_bbp_converter_platform', __( 'Select Platform', 'bbpress' ), 'bbp_converter_setting_callback_platform', 'bbpress_converter', 'bbpress_converter_main' ); 
  65. register_setting ( 'bbpress_converter_main', '_bbp_converter_platform', 'sanitize_title' ); 
  66.  
  67. // Database Server 
  68. add_settings_field( '_bbp_converter_db_server', __( 'Database Server', 'bbpress' ), 'bbp_converter_setting_callback_dbserver', 'bbpress_converter', 'bbpress_converter_main' ); 
  69. register_setting ( 'bbpress_converter_main', '_bbp_converter_db_server', 'sanitize_title' ); 
  70.  
  71. // Database Server Port 
  72. add_settings_field( '_bbp_converter_db_port', __( 'Database Port', 'bbpress' ), 'bbp_converter_setting_callback_dbport', 'bbpress_converter', 'bbpress_converter_main' ); 
  73. register_setting ( 'bbpress_converter_main', '_bbp_converter_db_port', 'sanitize_title' ); 
  74.  
  75. // Database Name 
  76. add_settings_field( '_bbp_converter_db_name', __( 'Database Name', 'bbpress' ), 'bbp_converter_setting_callback_dbname', 'bbpress_converter', 'bbpress_converter_main' ); 
  77. register_setting ( 'bbpress_converter_main', '_bbp_converter_db_name', 'sanitize_title' ); 
  78.  
  79. // Database User 
  80. add_settings_field( '_bbp_converter_db_user', __( 'Database User', 'bbpress' ), 'bbp_converter_setting_callback_dbuser', 'bbpress_converter', 'bbpress_converter_main' ); 
  81. register_setting ( 'bbpress_converter_main', '_bbp_converter_db_user', 'sanitize_title' ); 
  82.  
  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.  
  87. // Database Prefix 
  88. add_settings_field( '_bbp_converter_db_prefix', __( 'Table Prefix', 'bbpress' ), 'bbp_converter_setting_callback_dbprefix', 'bbpress_converter', 'bbpress_converter_main' ); 
  89. register_setting ( 'bbpress_converter_main', '_bbp_converter_db_prefix', 'sanitize_title' ); 
  90.  
  91. // Add the options section 
  92. add_settings_section( 'bbpress_converter_opt', __( 'Options', 'bbpress' ), 'bbp_converter_setting_callback_options_section', 'bbpress_converter' ); 
  93.  
  94. // Rows Limit 
  95. add_settings_field( '_bbp_converter_rows', __( 'Rows Limit', 'bbpress' ), 'bbp_converter_setting_callback_rows', 'bbpress_converter', 'bbpress_converter_opt' ); 
  96. register_setting ( 'bbpress_converter_opt', '_bbp_converter_rows', 'intval' ); 
  97.  
  98. // Delay Time 
  99. add_settings_field( '_bbp_converter_delay_time', __( 'Delay Time', 'bbpress' ), 'bbp_converter_setting_callback_delay_time', 'bbpress_converter', 'bbpress_converter_opt' ); 
  100. register_setting ( 'bbpress_converter_opt', '_bbp_converter_delay_time', 'intval' ); 
  101.  
  102. // Convert Users ? 
  103. add_settings_field( '_bbp_converter_convert_users', __( 'Convert Users', 'bbpress' ), 'bbp_converter_setting_callback_convert_users', 'bbpress_converter', 'bbpress_converter_opt' ); 
  104. register_setting ( 'bbpress_converter_opt', '_bbp_converter_convert_users', 'intval' ); 
  105.  
  106. // Restart 
  107. add_settings_field( '_bbp_converter_restart', __( 'Start Over', 'bbpress' ), 'bbp_converter_setting_callback_restart', 'bbpress_converter', 'bbpress_converter_opt' ); 
  108. register_setting ( 'bbpress_converter_opt', '_bbp_converter_restart', 'intval' ); 
  109.  
  110. // Clean 
  111. add_settings_field( '_bbp_converter_clean', __( 'Purge Previous Import', 'bbpress' ), 'bbp_converter_setting_callback_clean', 'bbpress_converter', 'bbpress_converter_opt' ); 
  112. register_setting ( 'bbpress_converter_opt', '_bbp_converter_clean', 'intval' ); 
  113.  
  114. /** 
  115. * Admin scripts 
  116. * @since bbPress (r3813) 
  117. */ 
  118. public function admin_head() { ?> 
  119.  
  120. <style type="text/css" media="screen"> 
  121. /*<![CDATA[*/ 
  122.  
  123. div.bbp-converter-updated,  
  124. div.bbp-converter-warning { 
  125. border-radius: 3px 3px 3px 3px; 
  126. border-style: solid; 
  127. border-width: 1px; 
  128. padding: 5px 5px 5px 5px; 
  129.  
  130. div.bbp-converter-updated { 
  131. height: 300px; 
  132. overflow: auto; 
  133. display: none; 
  134. background-color: #FFFFE0; 
  135. border-color: #E6DB55; 
  136. font-family: monospace; 
  137. font-weight: bold; 
  138.  
  139. div.bbp-converter-updated p { 
  140. margin: 0.5em 0; 
  141. padding: 2px; 
  142. float: left; 
  143. clear: left; 
  144.  
  145. div.bbp-converter-updated p.loading { 
  146. padding: 2px 20px 2px 2px; 
  147. background-image: url('<?php echo admin_url(); ?>images/wpspin_light.gif'); 
  148. background-repeat: no-repeat; 
  149. background-position: center right; 
  150.  
  151. #bbp-converter-stop { 
  152. display:none; 
  153.  
  154. #bbp-converter-progress { 
  155. display:none; 
  156.  
  157. /*]]>*/ 
  158. </style> 
  159.  
  160. <script language="javascript"> 
  161.  
  162. var bbconverter_is_running = false; 
  163. var bbconverter_run_timer; 
  164. var bbconverter_delay_time = 0; 
  165.  
  166. function bbconverter_grab_data() { 
  167. var values = {}; 
  168. jQuery.each(jQuery('#bbp-converter-settings').serializeArray(), function(i, field) { 
  169. values[field.name] = field.value; 
  170. }); 
  171.  
  172. if( values['_bbp_converter_restart'] ) { 
  173. jQuery('#_bbp_converter_restart').removeAttr("checked"); 
  174.  
  175. if( values['_bbp_converter_delay_time'] ) { 
  176. bbconverter_delay_time = values['_bbp_converter_delay_time'] * 1000; 
  177.  
  178. values['action'] = 'bbconverter_process'; 
  179. values['_ajax_nonce'] = '<?php echo wp_create_nonce( 'bbp_converter_process' ); ?>'; 
  180.  
  181. return values; 
  182.  
  183. function bbconverter_start() { 
  184. if( false == bbconverter_is_running ) { 
  185. bbconverter_is_running = true; 
  186. jQuery('#bbp-converter-start').hide(); 
  187. jQuery('#bbp-converter-stop').show(); 
  188. jQuery('#bbp-converter-progress').show(); 
  189. bbconverter_log( '<p class="loading"><?php esc_html_e( 'Starting Conversion', 'bbpress' ); ?></p>' ); 
  190. bbconverter_run(); 
  191.  
  192. function bbconverter_run() { 
  193. jQuery.post(ajaxurl, bbconverter_grab_data(), function(response) { 
  194. var response_length = response.length - 1; 
  195. response = response.substring(0, response_length); 
  196. bbconverter_success(response); 
  197. }); 
  198.  
  199. function bbconverter_stop() { 
  200. jQuery('#bbp-converter-start').show(); 
  201. jQuery('#bbp-converter-stop').hide(); 
  202. jQuery('#bbp-converter-progress').hide(); 
  203. jQuery('#bbp-converter-message p').removeClass( 'loading' ); 
  204. bbconverter_is_running = false; 
  205. clearTimeout( bbconverter_run_timer ); 
  206.  
  207. function bbconverter_success(response) { 
  208. bbconverter_log(response); 
  209.  
  210. if ( response == '<p class="loading"><?php esc_html_e( 'Conversion Complete', 'bbpress' ); ?></p>' || response.indexOf('error') > -1 ) { 
  211. bbconverter_log('<p>Repair any missing information: <a href="<?php echo admin_url(); ?>tools.php?page=bbp-repair">Continue</a></p>'); 
  212. bbconverter_stop(); 
  213. } else if( bbconverter_is_running ) { // keep going 
  214. jQuery('#bbp-converter-progress').show(); 
  215. clearTimeout( bbconverter_run_timer ); 
  216. bbconverter_run_timer = setTimeout( 'bbconverter_run()', bbconverter_delay_time ); 
  217. } else { 
  218. bbconverter_stop(); 
  219.  
  220. function bbconverter_log(text) { 
  221. if ( jQuery('#bbp-converter-message').css('display') == 'none' ) { 
  222. jQuery('#bbp-converter-message').show(); 
  223. if ( text ) { 
  224. jQuery('#bbp-converter-message p').removeClass( 'loading' ); 
  225. jQuery('#bbp-converter-message').prepend( text ); 
  226.  
  227. </script> 
  228.  
  229. <?php 
  230.  
  231. /** 
  232. * Wrap the converter output in paragraph tags, so styling can be applied 
  233. * @since bbPress (r4052) 
  234. * @param string $output 
  235. */ 
  236. private static function converter_output( $output = '' ) { 
  237.  
  238. // Get the last query 
  239. $before = '<p class="loading">'; 
  240. $after = '</p>'; 
  241. $query = get_option( '_bbp_converter_query' ); 
  242.  
  243. if ( ! empty( $query ) ) 
  244. $before = '<p class="loading" title="' . esc_attr( $query ) . '">'; 
  245.  
  246. echo $before . $output . $after; 
  247.  
  248. /** 
  249. * Callback processor 
  250. * @since bbPress (r3813) 
  251. */ 
  252. public function process_callback() { 
  253.  
  254. // Verify intent 
  255. check_ajax_referer( 'bbp_converter_process' ); 
  256.  
  257. // Bail if user cannot view import page 
  258. if ( ! current_user_can( 'bbp_tools_import_page' ) ) { 
  259. wp_die( '0' ); 
  260.  
  261. if ( ! ini_get( 'safe_mode' ) ) { 
  262. set_time_limit( 0 ); 
  263. ini_set( 'memory_limit', '256M' ); 
  264. ini_set( 'implicit_flush', '1' ); 
  265. ignore_user_abort( true ); 
  266.  
  267. // Save step and count so that it can be restarted. 
  268. if ( ! get_option( '_bbp_converter_step' ) || ( !empty( $_POST['_bbp_converter_restart'] ) ) ) { 
  269. update_option( '_bbp_converter_step', 1 ); 
  270. update_option( '_bbp_converter_start', 0 ); 
  271.  
  272. $step = (int) get_option( '_bbp_converter_step', 1 ); 
  273. $min = (int) get_option( '_bbp_converter_start', 0 ); 
  274. $count = (int) ! empty( $_POST['_bbp_converter_rows'] ) ? $_POST['_bbp_converter_rows'] : 100; 
  275. $max = ( $min + $count ) - 1; 
  276. $start = $min; 
  277.  
  278. // Bail if platform did not get saved 
  279. $platform = !empty( $_POST['_bbp_converter_platform' ] ) ? $_POST['_bbp_converter_platform' ] : get_option( '_bbp_converter_platform' ); 
  280. if ( empty( $platform ) ) 
  281. return; 
  282.  
  283. // Include the appropriate converter. 
  284. $converter = bbp_new_converter( $platform ); 
  285.  
  286. switch ( $step ) { 
  287.  
  288. // STEP 1. Clean all tables. 
  289. case 1 : 
  290. if ( !empty( $_POST['_bbp_converter_clean'] ) ) { 
  291. if ( $converter->clean( $start ) ) { 
  292. update_option( '_bbp_converter_step', $step + 1 ); 
  293. update_option( '_bbp_converter_start', 0 ); 
  294. $this->sync_table( true ); 
  295. if ( empty( $start ) ) { 
  296. $this->converter_output( __( 'No data to clean', 'bbpress' ) ); 
  297. } else { 
  298. update_option( '_bbp_converter_start', $max + 1 ); 
  299. $this->converter_output( sprintf( __( 'Deleting previously converted data (%1$s - %2$s)', 'bbpress' ), $min, $max ) ); 
  300. } else { 
  301. update_option( '_bbp_converter_step', $step + 1 ); 
  302. update_option( '_bbp_converter_start', 0 ); 
  303.  
  304. break; 
  305.  
  306. // STEP 2. Convert users. 
  307. case 2 : 
  308. if ( !empty( $_POST['_bbp_converter_convert_users'] ) ) { 
  309. if ( $converter->convert_users( $start ) ) { 
  310. update_option( '_bbp_converter_step', $step + 1 ); 
  311. update_option( '_bbp_converter_start', 0 ); 
  312. if ( empty( $start ) ) { 
  313. $this->converter_output( __( 'No users to convert', 'bbpress' ) ); 
  314. } else { 
  315. update_option( '_bbp_converter_start', $max + 1 ); 
  316. $this->converter_output( sprintf( __( 'Converting users (%1$s - %2$s)', 'bbpress' ), $min, $max ) ); 
  317. } else { 
  318. update_option( '_bbp_converter_step', $step + 1 ); 
  319. update_option( '_bbp_converter_start', 0 ); 
  320.  
  321. break; 
  322.  
  323. // STEP 3. Clean passwords. 
  324. case 3 : 
  325. if ( !empty( $_POST['_bbp_converter_convert_users'] ) ) { 
  326. if ( $converter->clean_passwords( $start ) ) { 
  327. update_option( '_bbp_converter_step', $step + 1 ); 
  328. update_option( '_bbp_converter_start', 0 ); 
  329. if ( empty( $start ) ) { 
  330. $this->converter_output( __( 'No passwords to clear', 'bbpress' ) ); 
  331. } else { 
  332. update_option( '_bbp_converter_start', $max + 1 ); 
  333. $this->converter_output( sprintf( __( 'Delete users WordPress default passwords (%1$s - %2$s)', 'bbpress' ), $min, $max ) ); 
  334. } else { 
  335. update_option( '_bbp_converter_step', $step + 1 ); 
  336. update_option( '_bbp_converter_start', 0 ); 
  337.  
  338. break; 
  339.  
  340. // STEP 4. Convert forums. 
  341. case 4 : 
  342. if ( $converter->convert_forums( $start ) ) { 
  343. update_option( '_bbp_converter_step', $step + 1 ); 
  344. update_option( '_bbp_converter_start', 0 ); 
  345. if ( empty( $start ) ) { 
  346. $this->converter_output( __( 'No forums to convert', 'bbpress' ) ); 
  347. } else { 
  348. update_option( '_bbp_converter_start', $max + 1 ); 
  349. $this->converter_output( sprintf( __( 'Converting forums (%1$s - %2$s)', 'bbpress' ), $min, $max ) ); 
  350.  
  351. break; 
  352.  
  353. // STEP 5. Convert forum parents. 
  354. case 5 : 
  355. if ( $converter->convert_forum_parents( $start ) ) { 
  356. update_option( '_bbp_converter_step', $step + 1 ); 
  357. update_option( '_bbp_converter_start', 0 ); 
  358. if ( empty( $start ) ) { 
  359. $this->converter_output( __( 'No forum parents to convert', 'bbpress' ) ); 
  360. } else { 
  361. update_option( '_bbp_converter_start', $max + 1 ); 
  362. $this->converter_output( sprintf( __( 'Calculating forum hierarchy (%1$s - %2$s)', 'bbpress' ), $min, $max ) ); 
  363.  
  364. break; 
  365.  
  366. // STEP 6. Convert topics. 
  367. case 6 : 
  368. if ( $converter->convert_topics( $start ) ) { 
  369. update_option( '_bbp_converter_step', $step + 1 ); 
  370. update_option( '_bbp_converter_start', 0 ); 
  371. if ( empty( $start ) ) { 
  372. $this->converter_output( __( 'No topics to convert', 'bbpress' ) ); 
  373. } else { 
  374. update_option( '_bbp_converter_start', $max + 1 ); 
  375. $this->converter_output( sprintf( __( 'Converting topics (%1$s - %2$s)', 'bbpress' ), $min, $max ) ); 
  376.  
  377. break; 
  378.  
  379. // STEP 7. Stick topics. 
  380. case 7 : 
  381. if ( $converter->convert_topic_stickies( $start ) ) { 
  382. update_option( '_bbp_converter_step', $step + 1 ); 
  383. update_option( '_bbp_converter_start', 0 ); 
  384. if ( empty( $start ) ) { 
  385. $this->converter_output( __( 'No stickies to stick', 'bbpress' ) ); 
  386. } else { 
  387. update_option( '_bbp_converter_start', $max + 1 ); 
  388. $this->converter_output( sprintf( __( 'Calculating topic stickies (%1$s - %2$s)', 'bbpress' ), $min, $max ) ); 
  389.  
  390. break; 
  391.  
  392. // STEP 8. Stick to front topics (Super Sicky). 
  393. case 8 : 
  394. if ( $converter->convert_topic_super_stickies( $start ) ) { 
  395. update_option( '_bbp_converter_step', $step + 1 ); 
  396. update_option( '_bbp_converter_start', 0 ); 
  397. if ( empty( $start ) ) { 
  398. $this->converter_output( __( 'No super stickies to stick', 'bbpress' ) ); 
  399. } else { 
  400. update_option( '_bbp_converter_start', $max + 1 ); 
  401. $this->converter_output( sprintf( __( 'Calculating topic super stickies (%1$s - %2$s)', 'bbpress' ), $min, $max ) ); 
  402.  
  403. break; 
  404.  
  405. // STEP 9. Convert tags. 
  406. case 9 : 
  407. if ( $converter->convert_tags( $start ) ) { 
  408. update_option( '_bbp_converter_step', $step + 1 ); 
  409. update_option( '_bbp_converter_start', 0 ); 
  410. if ( empty( $start ) ) { 
  411. $this->converter_output( __( 'No tags to convert', 'bbpress' ) ); 
  412. } else { 
  413. update_option( '_bbp_converter_start', $max + 1 ); 
  414. $this->converter_output( sprintf( __( 'Converting topic tags (%1$s - %2$s)', 'bbpress' ), $min, $max ) ); 
  415.  
  416. break; 
  417.  
  418. // STEP 10. Convert replies. 
  419. case 10 : 
  420. if ( $converter->convert_replies( $start ) ) { 
  421. update_option( '_bbp_converter_step', $step + 1 ); 
  422. update_option( '_bbp_converter_start', 0 ); 
  423. if ( empty( $start ) ) { 
  424. $this->converter_output( __( 'No replies to convert', 'bbpress' ) ); 
  425. } else { 
  426. update_option( '_bbp_converter_start', $max + 1 ); 
  427. $this->converter_output( sprintf( __( 'Converting replies (%1$s - %2$s)', 'bbpress' ), $min, $max ) ); 
  428.  
  429. break; 
  430.  
  431. // STEP 11. Convert reply_to parents. 
  432. case 11 : 
  433. if ( $converter->convert_reply_to_parents( $start ) ) { 
  434. update_option( '_bbp_converter_step', $step + 1 ); 
  435. update_option( '_bbp_converter_start', 0 ); 
  436. if ( empty( $start ) ) { 
  437. $this->converter_output( __( 'No reply_to parents to convert', 'bbpress' ) ); 
  438. } else { 
  439. update_option( '_bbp_converter_start', $max + 1 ); 
  440. $this->converter_output( sprintf( __( 'Calculating reply_to parents (%1$s - %2$s)', 'bbpress' ), $min, $max ) ); 
  441.  
  442. break; 
  443.  
  444. default : 
  445. delete_option( '_bbp_converter_step' ); 
  446. delete_option( '_bbp_converter_start' ); 
  447. delete_option( '_bbp_converter_query' ); 
  448.  
  449. $this->converter_output( __( 'Conversion Complete', 'bbpress' ) ); 
  450.  
  451. break; 
  452.  
  453. /** 
  454. * Create Tables for fast syncing 
  455. * @since bbPress (r3813) 
  456. */ 
  457. public function sync_table( $drop = false ) { 
  458. global $wpdb; 
  459.  
  460. $table_name = $wpdb->prefix . 'bbp_converter_translator'; 
  461. if ( ! empty( $drop ) && $wpdb->get_var( "SHOW TABLES LIKE '{$table_name}'" ) == $table_name ) 
  462. $wpdb->query( "DROP TABLE {$table_name}" ); 
  463.  
  464. require_once( ABSPATH . '/wp-admin/includes/upgrade.php' ); 
  465.  
  466. if ( !empty( $wpdb->charset ) ) { 
  467. $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset"; 
  468.  
  469. if ( !empty( $wpdb->collate ) ) { 
  470. $charset_collate .= " COLLATE $wpdb->collate"; 
  471.  
  472. /** Translator ****************************************************/ 
  473.  
  474. $sql = "CREATE TABLE {$table_name} ( 
  475. meta_id mediumint(8) unsigned not null auto_increment,  
  476. value_type varchar(25) null,  
  477. value_id bigint(20) unsigned not null default '0',  
  478. meta_key varchar(25) null,  
  479. meta_value varchar(25) null,  
  480. PRIMARY KEY (meta_id),  
  481. KEY value_id (value_id),  
  482. KEY meta_join (meta_key, meta_value) ) {$charset_collate};"; 
  483.  
  484. dbDelta( $sql );