BBP_Converter
Main BBP_Converter Class
Description
Source
File: bp-forums/admin/converter.php
class BBP_Converter { /** * The main Forums Converter loader * * @since bbPress (r3813) * @uses BBP_Converter::includes() Include the required files * @uses BBP_Converter::setup_actions() Setup the actions */ public function __construct() { // "I wonder where I'll float next." if ( empty( $_SERVER['REQUEST_METHOD'] ) ) return; // Bail if request is not correct switch ( strtoupper( $_SERVER['REQUEST_METHOD'] ) ) { // Converter is converting case 'POST' : if ( ( empty( $_POST['action'] ) || ( 'bbconverter_process' != $_POST['action'] ) ) ) return; break; // Some other admin page case 'GET' : if ( ( empty( $_GET['page'] ) || ( 'bbp-converter' != $_GET['page'] ) ) ) return; break; } // Proceed with the actions $this->setup_actions(); } /** * Setup the default actions * * @since bbPress (r3813) * @uses add_action() To add various actions */ private function setup_actions() { // Attach to the admin head with our ajax requests cycle and css add_action( 'bbp_admin_head', array( $this, 'admin_head' ) ); // Attach the bbConverter admin settings action to the WordPress admin init action. add_action( 'bbp_register_admin_settings', array( $this, 'register_admin_settings' ) ); // Attach to the admin ajax request to process cycles add_action( 'wp_ajax_bbconverter_process', array( $this, 'process_callback' ) ); } /** * Register the settings * * @since bbPress (r3813) * @uses add_settings_section() To add our own settings section * @uses add_settings_field() To add various settings fields * @uses register_setting() To register various settings */ public function register_admin_settings() { // Add the main section add_settings_section( 'bbpress_converter_main', __( 'Import Forums', 'buddyboss' ), 'bbp_converter_setting_callback_main_section', 'bbpress_converter' ); // System Select add_settings_field( '_bbp_converter_platform', __( 'Select Platform', 'buddyboss' ), 'bbp_converter_setting_callback_platform', 'bbpress_converter', 'bbpress_converter_main' ); register_setting ( 'bbpress_converter_main', '_bbp_converter_platform', 'sanitize_title' ); // Database Server add_settings_field( '_bbp_converter_db_server', __( 'Database Server', 'buddyboss' ), 'bbp_converter_setting_callback_dbserver', 'bbpress_converter', 'bbpress_converter_main' ); register_setting ( 'bbpress_converter_main', '_bbp_converter_db_server', 'sanitize_title' ); // Database Server Port add_settings_field( '_bbp_converter_db_port', __( 'Database Port', 'buddyboss' ), 'bbp_converter_setting_callback_dbport', 'bbpress_converter', 'bbpress_converter_main' ); register_setting ( 'bbpress_converter_main', '_bbp_converter_db_port', 'sanitize_title' ); // Database Name add_settings_field( '_bbp_converter_db_name', __( 'Database Name', 'buddyboss' ), 'bbp_converter_setting_callback_dbname', 'bbpress_converter', 'bbpress_converter_main' ); register_setting ( 'bbpress_converter_main', '_bbp_converter_db_name', 'sanitize_title' ); // Database User add_settings_field( '_bbp_converter_db_user', __( 'Database User', 'buddyboss' ), 'bbp_converter_setting_callback_dbuser', 'bbpress_converter', 'bbpress_converter_main' ); register_setting ( 'bbpress_converter_main', '_bbp_converter_db_user', 'sanitize_title' ); // Database Pass add_settings_field( '_bbp_converter_db_pass', __( 'Database Password', 'buddyboss' ), 'bbp_converter_setting_callback_dbpass', 'bbpress_converter', 'bbpress_converter_main' ); register_setting ( 'bbpress_converter_main', '_bbp_converter_db_pass', 'sanitize_title' ); // Database Prefix add_settings_field( '_bbp_converter_db_prefix', __( 'Table Prefix', 'buddyboss' ), 'bbp_converter_setting_callback_dbprefix', 'bbpress_converter', 'bbpress_converter_main' ); register_setting ( 'bbpress_converter_main', '_bbp_converter_db_prefix', 'sanitize_title' ); // Add the options section add_settings_section( 'bbpress_converter_opt', __( '', 'buddyboss' ), 'bbp_converter_setting_callback_options_section', 'bbpress_converter' ); // Rows Limit add_settings_field( '_bbp_converter_rows', __( 'Rows Limit', 'buddyboss' ), 'bbp_converter_setting_callback_rows', 'bbpress_converter', 'bbpress_converter_opt' ); register_setting ( 'bbpress_converter_opt', '_bbp_converter_rows', 'intval' ); // Delay Time add_settings_field( '_bbp_converter_delay_time', __( 'Delay Time', 'buddyboss' ), 'bbp_converter_setting_callback_delay_time', 'bbpress_converter', 'bbpress_converter_opt' ); register_setting ( 'bbpress_converter_opt', '_bbp_converter_delay_time', 'intval' ); // Convert Users ? add_settings_field( '_bbp_converter_convert_users', __( 'Convert Users', 'buddyboss' ), 'bbp_converter_setting_callback_convert_users', 'bbpress_converter', 'bbpress_converter_opt' ); register_setting ( 'bbpress_converter_opt', '_bbp_converter_convert_users', 'intval' ); // Restart add_settings_field( '_bbp_converter_restart', __( 'Start Over', 'buddyboss' ), 'bbp_converter_setting_callback_restart', 'bbpress_converter', 'bbpress_converter_opt' ); register_setting ( 'bbpress_converter_opt', '_bbp_converter_restart', 'intval' ); // Clean add_settings_field( '_bbp_converter_clean', __( 'Purge Previous Import', 'buddyboss' ), 'bbp_converter_setting_callback_clean', 'bbpress_converter', 'bbpress_converter_opt' ); register_setting ( 'bbpress_converter_opt', '_bbp_converter_clean', 'intval' ); } /** * Admin scripts * * @since bbPress (r3813) */ public function admin_head() { ?> <style media="screen"> /*<![CDATA[*/ div.bbp-converter-updated, div.bbp-converter-warning { border-radius: 3px 3px 3px 3px; border-style: solid; border-width: 1px; padding: 5px 5px 5px 5px; } div.bbp-converter-updated { height: 300px; overflow: auto; display: none; background-color: #FFFFE0; border-color: #E6DB55; font-family: monospace; font-weight: bold; } div.bbp-converter-updated p { margin: 0.5em 0; padding: 2px; float: left; clear: left; } div.bbp-converter-updated p.loading { padding: 2px 20px 2px 2px; background-image: url('<?php echo admin_url(); ?>images/wpspin_light.gif'); background-repeat: no-repeat; background-position: center right; } #bbp-converter-stop { display:none; } #bbp-converter-progress { display:none; } /*]]>*/ </style> <script language="javascript"> var bbconverter_is_running = false; var bbconverter_run_timer; var bbconverter_delay_time = 0; function bbconverter_grab_data() { var values = {}; jQuery.each(jQuery('#bbp-converter-settings').serializeArray(), function(i, field) { values[field.name] = field.value; }); if( values['_bbp_converter_restart'] ) { jQuery('#_bbp_converter_restart').removeAttr("checked"); } if( values['_bbp_converter_delay_time'] ) { bbconverter_delay_time = values['_bbp_converter_delay_time'] * 1000; } values['action'] = 'bbconverter_process'; values['_ajax_nonce'] = '<?php echo wp_create_nonce( 'bbp_converter_process' ); ?>'; return values; } function bbconverter_start() { if( false == bbconverter_is_running ) { bbconverter_is_running = true; jQuery('#bbp-converter-start').hide(); jQuery('#bbp-converter-stop').show(); jQuery('#bbp-converter-progress').show(); bbconverter_log( '<p class="loading"><?php esc_html_e( 'Starting Conversion', 'buddyboss' ); ?></p>' ); bbconverter_run(); } } function bbconverter_run() { jQuery.post(ajaxurl, bbconverter_grab_data(), function(response) { var response_length = response.length - 1; response = response.substring(0,response_length); bbconverter_success(response); }); } function bbconverter_stop() { jQuery('#bbp-converter-start').show(); jQuery('#bbp-converter-stop').hide(); jQuery('#bbp-converter-progress').hide(); jQuery('#bbp-converter-message p').removeClass( 'loading' ); bbconverter_is_running = false; clearTimeout( bbconverter_run_timer ); } function bbconverter_success(response) { bbconverter_log(response); if ( response == '<p class="loading"><?php esc_html_e( 'Conversion Complete', 'buddyboss' ); ?></p>' || response.indexOf('error') > -1 ) { bbconverter_log('<p><?php esc_html_e('Repair any missing information:', 'buddyboss' ); ?> <a href="<?php echo admin_url(); ?>admin.php?page=bbp-repair"><?php esc_html_e( 'Continue', 'buddyboss'); ?></a></p>'); bbconverter_stop(); } else if( bbconverter_is_running ) { // keep going jQuery('#bbp-converter-progress').show(); clearTimeout( bbconverter_run_timer ); bbconverter_run_timer = setTimeout( 'bbconverter_run()', bbconverter_delay_time ); } else { bbconverter_stop(); } } function bbconverter_log(text) { if ( jQuery('#bbp-converter-message').css('display') == 'none' ) { jQuery('#bbp-converter-message').show(); } if ( text ) { jQuery('#bbp-converter-message p').removeClass( 'loading' ); jQuery('#bbp-converter-message').prepend( text ); } } </script> <?php } /** * Wrap the converter output in paragraph tags, so styling can be applied * * @since bbPress (r4052) * * @param string $output */ private static function converter_output( $output = '' ) { // Get the last query $before = '<p class="loading">'; $after = '</p>'; $query = get_option( '_bbp_converter_query' ); if ( ! empty( $query ) ) $before = '<p class="loading" title="' . esc_attr( $query ) . '">'; echo $before . $output . $after; } /** * Callback processor * * @since bbPress (r3813) */ public function process_callback() { // Verify intent check_ajax_referer( 'bbp_converter_process' ); // Bail if user cannot view import page if ( ! current_user_can( 'bbp_tools_import_page' ) ) { wp_die( '0' ); } if ( ! ini_get( 'safe_mode' ) ) { set_time_limit( 0 ); ini_set( 'memory_limit', '256M' ); ini_set( 'implicit_flush', '1' ); ignore_user_abort( true ); } // Save step and count so that it can be restarted. if ( ! get_option( '_bbp_converter_step' ) || ( !empty( $_POST['_bbp_converter_restart'] ) ) ) { update_option( '_bbp_converter_step', 1 ); update_option( '_bbp_converter_start', 0 ); } $step = (int) get_option( '_bbp_converter_step', 1 ); $min = (int) get_option( '_bbp_converter_start', 0 ); $count = ! empty( $_POST['_bbp_converter_rows'] ) ? min( max( (int) $_POST['_bbp_converter_rows'], 1 ), 5000 ) : 100; $max = ( $min + $count ) - 1; $start = $min; // Bail if platform did not get saved $platform = !empty( $_POST['_bbp_converter_platform' ] ) ? sanitize_text_field( $_POST['_bbp_converter_platform' ] ) : get_option( '_bbp_converter_platform' ); if ( empty( $platform ) ) return; // Include the appropriate converter. $converter = bbp_new_converter( $platform ); switch ( $step ) { // STEP 1. Clean all tables. case 1 : if ( !empty( $_POST['_bbp_converter_clean'] ) ) { if ( $converter->clean( $start ) ) { update_option( '_bbp_converter_step', $step + 1 ); update_option( '_bbp_converter_start', 0 ); $this->sync_table( true ); if ( empty( $start ) ) { $this->converter_output( __( 'No data to clean', 'buddyboss' ) ); } } else { update_option( '_bbp_converter_start', $max + 1 ); $this->converter_output( sprintf( __( 'Deleting previously converted data (%1$s - %2$s)', 'buddyboss' ), $min, $max ) ); } } else { update_option( '_bbp_converter_step', $step + 1 ); update_option( '_bbp_converter_start', 0 ); } break; // STEP 2. Convert users. case 2 : if ( !empty( $_POST['_bbp_converter_convert_users'] ) ) { if ( $converter->convert_users( $start ) ) { update_option( '_bbp_converter_step', $step + 1 ); update_option( '_bbp_converter_start', 0 ); if ( empty( $start ) ) { $this->converter_output( __( 'No users to convert', 'buddyboss' ) ); } } else { update_option( '_bbp_converter_start', $max + 1 ); $this->converter_output( sprintf( __( 'Converting users (%1$s - %2$s)', 'buddyboss' ), $min, $max ) ); } } else { update_option( '_bbp_converter_step', $step + 1 ); update_option( '_bbp_converter_start', 0 ); } break; // STEP 3. Clean passwords. case 3 : if ( !empty( $_POST['_bbp_converter_convert_users'] ) ) { if ( $converter->clean_passwords( $start ) ) { update_option( '_bbp_converter_step', $step + 1 ); update_option( '_bbp_converter_start', 0 ); if ( empty( $start ) ) { $this->converter_output( __( 'No passwords to clear', 'buddyboss' ) ); } } else { update_option( '_bbp_converter_start', $max + 1 ); $this->converter_output( sprintf( __( 'Delete users WordPress default passwords (%1$s - %2$s)', 'buddyboss' ), $min, $max ) ); } } else { update_option( '_bbp_converter_step', $step + 1 ); update_option( '_bbp_converter_start', 0 ); } break; // STEP 4. Convert forums. case 4 : if ( $converter->convert_forums( $start ) ) { update_option( '_bbp_converter_step', $step + 1 ); update_option( '_bbp_converter_start', 0 ); if ( empty( $start ) ) { $this->converter_output( __( 'No forums to convert', 'buddyboss' ) ); } } else { update_option( '_bbp_converter_start', $max + 1 ); $this->converter_output( sprintf( __( 'Converting forums (%1$s - %2$s)', 'buddyboss' ), $min, $max ) ); } break; // STEP 5. Convert forum parents. case 5 : if ( $converter->convert_forum_parents( $start ) ) { update_option( '_bbp_converter_step', $step + 1 ); update_option( '_bbp_converter_start', 0 ); if ( empty( $start ) ) { $this->converter_output( __( 'No forum parents to convert', 'buddyboss' ) ); } } else { update_option( '_bbp_converter_start', $max + 1 ); $this->converter_output( sprintf( __( 'Calculating forum hierarchy (%1$s - %2$s)', 'buddyboss' ), $min, $max ) ); } break; // STEP 6. Convert discussions. case 6 : if ( $converter->convert_topics( $start ) ) { update_option( '_bbp_converter_step', $step + 1 ); update_option( '_bbp_converter_start', 0 ); if ( empty( $start ) ) { $this->converter_output( __( 'No discussions to convert', 'buddyboss' ) ); } } else { update_option( '_bbp_converter_start', $max + 1 ); $this->converter_output( sprintf( __( 'Converting discussions (%1$s - %2$s)', 'buddyboss' ), $min, $max ) ); } break; // STEP 7. Stick discussions. case 7 : if ( $converter->convert_topic_stickies( $start ) ) { update_option( '_bbp_converter_step', $step + 1 ); update_option( '_bbp_converter_start', 0 ); if ( empty( $start ) ) { $this->converter_output( __( 'No stickies to stick', 'buddyboss' ) ); } } else { update_option( '_bbp_converter_start', $max + 1 ); $this->converter_output( sprintf( __( 'Calculating discussion stickies (%1$s - %2$s)', 'buddyboss' ), $min, $max ) ); } break; // STEP 8. Stick to front discussion (Super Sicky). case 8 : if ( $converter->convert_topic_super_stickies( $start ) ) { update_option( '_bbp_converter_step', $step + 1 ); update_option( '_bbp_converter_start', 0 ); if ( empty( $start ) ) { $this->converter_output( __( 'No super stickies to stick', 'buddyboss' ) ); } } else { update_option( '_bbp_converter_start', $max + 1 ); $this->converter_output( sprintf( __( 'Calculating discussion super stickies (%1$s - %2$s)', 'buddyboss' ), $min, $max ) ); } break; // STEP 9. Convert tags. case 9 : if ( $converter->convert_tags( $start ) ) { update_option( '_bbp_converter_step', $step + 1 ); update_option( '_bbp_converter_start', 0 ); if ( empty( $start ) ) { $this->converter_output( __( 'No tags to convert', 'buddyboss' ) ); } } else { update_option( '_bbp_converter_start', $max + 1 ); $this->converter_output( sprintf( __( 'Converting discussion tags (%1$s - %2$s)', 'buddyboss' ), $min, $max ) ); } break; // STEP 10. Convert replies. case 10 : if ( $converter->convert_replies( $start ) ) { update_option( '_bbp_converter_step', $step + 1 ); update_option( '_bbp_converter_start', 0 ); if ( empty( $start ) ) { $this->converter_output( __( 'No replies to convert', 'buddyboss' ) ); } } else { update_option( '_bbp_converter_start', $max + 1 ); $this->converter_output( sprintf( __( 'Converting replies (%1$s - %2$s)', 'buddyboss' ), $min, $max ) ); } break; // STEP 11. Convert reply_to parents. case 11 : if ( $converter->convert_reply_to_parents( $start ) ) { update_option( '_bbp_converter_step', $step + 1 ); update_option( '_bbp_converter_start', 0 ); if ( empty( $start ) ) { $this->converter_output( __( 'No reply_to parents to convert', 'buddyboss' ) ); } } else { update_option( '_bbp_converter_start', $max + 1 ); $this->converter_output( sprintf( __( 'Calculating reply_to parents (%1$s - %2$s)', 'buddyboss' ), $min, $max ) ); } break; default : delete_option( '_bbp_converter_step' ); delete_option( '_bbp_converter_start' ); delete_option( '_bbp_converter_query' ); $this->converter_output( __( 'Conversion Complete', 'buddyboss' ) ); break; } } /** * Create Tables for fast syncing * * @since bbPress (r3813) */ public function sync_table( $drop = false ) { global $wpdb; $table_name = $wpdb->prefix . 'bbp_converter_translator'; if ( ! empty( $drop ) && $wpdb->get_var( "SHOW TABLES LIKE '{$table_name}'" ) == $table_name ) $wpdb->query( "DROP TABLE {$table_name}" ); require_once( ABSPATH . '/wp-admin/includes/upgrade.php' ); if ( !empty( $wpdb->charset ) ) { $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset"; } if ( !empty( $wpdb->collate ) ) { $charset_collate .= " COLLATE $wpdb->collate"; } /** Translator ****************************************************/ $sql = array(); $max_index_length = 191; $sql[] = "CREATE TABLE {$table_name} ( meta_id mediumint(8) unsigned not null auto_increment, value_type varchar(25) null, value_id bigint(20) unsigned not null default '0', meta_key varchar(255) null, meta_value varchar(255) null, PRIMARY KEY (meta_id), KEY value_id (value_id), KEY meta_join (meta_key({$max_index_length}), meta_value({$max_index_length})) ) {$charset_collate};"; dbDelta( $sql ); } }
Methods
- __construct — The main Forums Converter loader
- admin_head — Admin scripts
- converter_output — Wrap the converter output in paragraph tags, so styling can be applied
- process_callback — Callback processor
- register_admin_settings — Register the settings
- setup_actions — Setup the default actions
- sync_table — Create Tables for fast syncing
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.