Skip to:
Content

bbPress.org

Changeset 6601


Ignore:
Timestamp:
06/23/2017 04:21:58 AM (9 years ago)
Author:
johnjamesjacoby
Message:

Admin/Converter: Lazy load admin & converter as needed.

  • Move admin class into its own file
  • Remove admin.php
  • Introduce _setup_ functions to make loading them on the fly easier
  • Simplify converter logic for smoother starting/stopping
  • Improved UI for timer/status updates
  • Remove double-duty variables from BBP_Converter and response data
  • Switch from text-only response value to JSON object for improved flexibility

This allows the converter to return more data, and makes it easier to work with that data.

Todo:

  • Error responses
  • Check that starts are bumping correctly inside of steps
  • Better utilize JSON responses
Location:
trunk/src
Files:
1 added
1 deleted
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bbpress.php

    r6596 r6601  
    204204        /** Versions **********************************************************/
    205205
    206         $this->version    = '2.6-rc-6595';
     206        $this->version    = '2.6-rc-6601';
    207207        $this->db_version = '262';
    208208
     
    375375        // Quick admin check and load if needed
    376376        if ( is_admin() ) {
    377             require $this->includes_dir . 'admin/admin.php';
    378377            require $this->includes_dir . 'admin/actions.php';
    379378        }
  • trunk/src/includes/admin/actions.php

    r6573 r6601  
    1111 *
    1212 *  - bbPress: In {@link bbPress::setup_actions()} in bbpress.php
    13  *  - Admin: More in {@link BBP_Admin::setup_actions()} in admin.php
     13 *  - Admin: More in {@link BBP_Admin::setup_actions()} in class-bbp-admin.php
    1414 *
    1515 * @package bbPress
     
    6262
    6363// Initialize the admin area
    64 add_action( 'bbp_init', 'bbp_admin' );
     64add_action( 'bbp_init', 'bbp_setup_admin' );
    6565
    6666// Reset the menu order
     
    106106add_filter( 'bbp_admin_replies_column_headers', 'bbp_filter_column_headers' );
    107107
     108// Load the converter early (page and AJAX)
     109add_action( 'load-tools_page_bbp-converter', 'bbp_setup_converter', 2 );
     110add_action( 'wp_ajax_bbp_converter_process', 'bbp_setup_converter', 2 );
     111
     112/**
     113 * Setup bbPress admin
     114 *
     115 * @since 2.0.0 bbPress (r1000)
     116 * @since 2.6.0 bbPress (r6598) Moved to actions.php
     117 */
     118function bbp_admin() {
     119    return bbp_setup_admin();
     120}
     121
    108122/**
    109123 * When a new site is created in a multisite installation, run the activation
  • trunk/src/includes/admin/assets/css/admin.css

    r6542 r6601  
    366366}
    367367
    368 #bbp-converter-timer {
    369     position: absolute;
    370     right: 10px;
    371     top: 4px;
    372     padding: 5px;
    373     border: 1px solid #ddd;
    374     background-color: #fafafa;
     368#bbp-converter-status {
     369    font-weight: 400;
     370    font-size: 12px;
     371    color: #aaa;
     372    display: block;
     373    margin-top: 2px;
    375374}
    376375
  • trunk/src/includes/admin/assets/js/converter.js

    r6514 r6601  
    99        start    = $( '#bbp-converter-start'    ),
    1010        restart  = $( '#_bbp_converter_restart' ),
    11         timer    = $( '#bbp-converter-timer'    ),
    12         settings = $( '#bbp-converter-settings' ),
    13 
    14         // Prefetch the settings
    15         options  = bbp_converter_settings();
     11        status   = $( '#bbp-converter-status'   ),
     12        settings = $( '#bbp-converter-settings' );
    1613
    1714    /**
     
    2421    start.on( 'click', function( e ) {
    2522        bbp_converter_user_start();
    26 
    2723        e.preventDefault();
    2824    } );
     
    3733    $( stop ).on( 'click', function( e ) {
    3834        bbp_converter_user_stop();
    39 
    4035        e.preventDefault();
    4136    } );
     
    4944     */
    5045    function bbp_converter_user_start() {
    51         if ( ! BBP_Converter.running ) {
    52             message.addClass( 'started' );
    53             start.hide();
    54             stop.show();
    55 
    56             if ( BBP_Converter.started ) {
    57                 bbp_converter_log( BBP_Converter.strings.start_continue );
    58             } else {
    59                 bbp_converter_log( BBP_Converter.strings.start_start );
    60             }
    61 
    62             bbp_converter_next();
    63         }
    64 
    65         BBP_Converter.started = true;
     46        bbp_converter_start();
    6647    }
    6748
     
    8162
    8263    /**
    83      * Database error
    84      *
    85      * @since 2.6.0 bbPress (r6470)
    86      *
    87      * @returns {void}
    88      */
    89     function bbp_converter_error_db() {
    90         bbp_converter_stop(
    91             BBP_Converter.strings.button_start,
    92             BBP_Converter.strings.import_error_db
    93         );
    94     }
    95 
    96     /**
    97      * Converter error
    98      *
    99      * @since 2.6.0 bbPress (r6470)
    100      *
    101      * @returns {void}
    102      */
    103     function bbp_converter_halt() {
    104         bbp_converter_stop(
    105             BBP_Converter.strings.button_continue,
    106             BBP_Converter.strings.import_error_halt
    107         );
    108     }
    109 
    110     /**
    111      * Converter complete
    112      *
    113      * @since 2.6.0 bbPress (r6470)
    114      *
    115      * @returns {void}
    116      */
    117     function bbp_converter_complete() {
    118         bbp_converter_stop(
    119             BBP_Converter.strings.button_start,
    120             BBP_Converter.strings.import_comelete
    121         );
    122 
    123         BBP_Converter.started = false;
    124     }
    125 
    126     /**
    127      * Handle successful ajax response
    128      *
    129      * @since 2.6.0 bbPress (r6470)
    130      *
    131      * @param {object} response Ajax response
    132      *
    133      * @returns {void}
    134      */
    135     function bbp_converter_process( response ) {
    136         bbp_converter_log( response );
    137 
    138         if ( response === BBP_Converter.strings.import_complete ) {
    139             bbp_converter_complete();
    140 
    141         } else if ( BBP_Converter.running ) {
    142             bbp_converter_next();
    143 
    144         } else if ( BBP_Converter.halt ) {
    145             bbp_converter_halt();
    146         }
    147     }
    148 
    149     /**
    15064     * Return values of converter settings
    15165     *
     
    16680
    16781        if ( values['_bbp_converter_delay_time'] ) {
    168             BBP_Converter.delay = values['_bbp_converter_delay_time'] * 1000;
     82            BBP_Converter.delay = parseInt( values['_bbp_converter_delay_time'], 10 ) * 1000;
    16983        }
    17084
     
    18296     * @returns {void}
    18397     */
    184     function bbp_converter_step() {
    185 
     98    function bbp_converter_post() {
     99        $.post( ajaxurl, bbp_converter_settings(), function( response ) {
     100
     101            // Parse the json response
     102            try {
     103                var data = response.data;
     104
     105                // Success
     106                if ( true === response.success ) {
     107                    bbp_converter_step( data );
     108
     109                // Failure
     110                } else {
     111                    bbp_converter_stop();
     112                }
     113
     114            } catch( e ) {
     115                bbp_converter_stop();
     116            }
     117        }, 'json' );
     118    }
     119
     120    /**
     121     * Process the next step
     122     *
     123     * @since 2.6.0 bbPress (r6600)
     124     *
     125     * @param {object} data
     126     * @returns {void}
     127     */
     128    function bbp_converter_step( data ) {
     129
     130        // Bail if not running
    186131        if ( ! BBP_Converter.running ) {
    187132            return;
    188133        }
    189134
    190         // Check if the settings have changed
    191         options = bbp_converter_settings();
    192 
    193         $.post( ajaxurl, options, function( response ) {
    194 
    195             if ( 'bbp_converter_db_connection_failed' === response ) {
    196                 bbp_converter_error_db();
    197                 return;
    198             }
    199 
    200             bbp_converter_process( response.substring( 0, response.length - 1 ) );
    201         } );
    202     }
    203 
    204     /**
    205      * Proceed to the next converter step
    206      *
    207      * @since 2.6.0 bbPress (r6470)
    208      *
    209      * @returns {void}
    210      */
    211     function bbp_converter_next() {
    212         bbp_converter_timer();
    213 
     135        // Do the step
     136        bbp_converter_log( data.progress );
     137        bbp_converter_status( data );
     138        bbp_converter_wait();
     139
     140        // Done
     141        if ( data.current_step === data.final_step ) {
     142            bbp_converter_stop(
     143                BBP_Converter.strings.button_start,
     144                BBP_Converter.strings.import_complete
     145            );
     146        }
     147    }
     148
     149    /**
     150     * Wait to do the next AJAX request
     151     *
     152     * @since 2.6.0 bbPress (r6600)
     153     *
     154     * @returns {void}
     155     */
     156    function bbp_converter_wait() {
    214157        clearTimeout( BBP_Converter.running );
    215158
     159        // Bail if not running
     160        if ( ! BBP_Converter.running ) {
     161            return;
     162        }
     163
     164        // Wait, then POST
    216165        BBP_Converter.running = setTimeout( function() {
    217             bbp_converter_step();
    218         }, BBP_Converter.delay );
     166            bbp_converter_post();
     167        }, parseInt( BBP_Converter.delay, 10 ) );
     168    }
     169
     170    /**
     171     * Start the converter and set the various flags
     172     *
     173     * @since 2.6.0 bbPress (r6600)
     174     *
     175     * @returns {void}
     176     */
     177    function bbp_converter_start() {
     178        clearTimeout( BBP_Converter.running );
     179        clearInterval( BBP_Converter.status );
     180
     181        BBP_Converter.running = true;
     182
     183        var log = BBP_Converter.strings.start_continue;
     184        if ( BBP_Converter.started ) {
     185            log = BBP_Converter.strings.start_start;
     186            BBP_Converter.started = true;
     187        }
     188
     189        bbp_converter_update(
     190            BBP_Converter.strings.button_continue,
     191            log,
     192            BBP_Converter.strings.status_starting
     193        );
     194
     195        message.addClass( 'started' );
     196        start.hide();
     197        stop.show();
     198
     199        bbp_converter_post();
    219200    }
    220201
     
    230211     */
    231212    function bbp_converter_stop( button, log ) {
    232         start.val( button ).show();
     213        clearTimeout( BBP_Converter.running );
     214        clearInterval( BBP_Converter.status );
     215
     216        BBP_Converter.running = false;
     217        BBP_Converter.status  = false;
     218
     219        if ( ! button ) {
     220            button = BBP_Converter.strings.button_continue;
     221        }
     222
     223        if ( ! log ) {
     224            log = BBP_Converter.strings.status_stopped;
     225        }
     226
     227        bbp_converter_update(
     228            button,
     229            log,
     230            BBP_Converter.strings.status_stopped
     231        );
     232
     233        start.show();
    233234        stop.hide();
    234         timer.text( BBP_Converter.strings.timer_stopped );
    235 
    236         if ( log ) {
    237             bbp_converter_log( log );
    238         }
    239 
    240         clearTimeout( BBP_Converter.running );
    241 
    242         BBP_Converter.running = false;
    243     }
    244 
    245     /**
    246      * Update the timer
     235    }
     236
     237    /**
     238     * Update the various screen texts
     239     *
     240     * @since 2.6.0 bbPress (r6600)
     241     *
     242     * @param {string} b_text
     243     * @param {string} p_text
     244     * @param {string} s_text
     245     *
     246     * @returns {void}
     247     */
     248    function bbp_converter_update( b_text, p_text, s_text ) {
     249        start.val( b_text );
     250        bbp_converter_log( p_text );
     251        status.text( s_text );
     252    }
     253
     254    /**
     255     * Update the status
    247256     *
    248257     * @since 2.6.0 bbPress (r6513)
     
    250259     * @returns {void}
    251260     */
    252     function bbp_converter_timer() {
    253         var remaining = BBP_Converter.delay / 1000;
    254 
    255         timer.text( BBP_Converter.strings.timer_counting.replace( '%s', remaining ) );
    256 
    257         clearInterval( BBP_Converter.timer );
    258 
    259         BBP_Converter.timer = setInterval( function() {
     261    function bbp_converter_status( data ) {
     262        var remaining = parseInt( BBP_Converter.delay, 10 ) / 1000,
     263            step      = parseInt( data.current_step,   10 ) + 1;
     264
     265        status.text( BBP_Converter.strings.status_counting.replace( '%s', remaining ) );
     266        clearInterval( BBP_Converter.status );
     267
     268        BBP_Converter.status = setInterval( function() {
    260269            remaining--;
    261             timer.text( BBP_Converter.strings.timer_counting.replace( '%s', remaining ) );
     270            status.text( BBP_Converter.strings.status_counting.replace( '%s', remaining ) );
    262271
    263272            if ( remaining <= 0 ) {
    264                 clearInterval( BBP_Converter.timer );
    265                 timer.text( BBP_Converter.strings.timer_waiting );
     273                clearInterval( BBP_Converter.status );
     274
     275                if ( parseInt( data.current_step, 10 ) < parseInt( data.final_step, 10 ) ) {
     276                    status.text( BBP_Converter.strings.status_up_next.replace( '%s', step ) );
     277                } else {
     278                    status.text( BBP_Converter.strings.status_complete );
     279                }
    266280            }
    267281        }, 1000 );
     
    278292     */
    279293    function bbp_converter_log( text ) {
    280 
    281         if ( '<div' !== text.substring( 0, 3 ) ) {
    282             text = '<p>' + text + '</p>';
    283         }
     294        text = '<p>' + text + '</p>';
    284295
    285296        message.prepend( text );
  • trunk/src/includes/admin/classes/class-bbp-converter-base.php

    r6573 r6601  
    1313defined( 'ABSPATH' ) || exit;
    1414
     15if ( ! class_exists( 'BBP_Converter_Base' ) ) :
    1516/**
    1617 * Base class to be extended by specific individual importers
     
    8586    public $convert_users = false;
    8687
     88    /**
     89     * @var bool Whether to clean up any old converter data. Default false.
     90     */
     91    public $clean = false;
     92
    8793    /** Methods ***************************************************************/
    8894
     
    103109        /** Sanitize Options **************************************************/
    104110
     111        $this->clean         = ! empty( $_POST['_bbp_converter_clean'] );
    105112        $this->convert_users = (bool) get_option( '_bbp_converter_convert_users', false );
     113        $this->halt          = (bool) get_option( '_bbp_converter_halt',          0     );
    106114        $this->max_rows      = (int)  get_option( '_bbp_converter_rows',          100   );
    107115
     
    133141        // Connection failed
    134142        if ( ! $this->opdb->db_connect( false ) ) {
    135             wp_die( 'bbp_converter_db_connection_failed', esc_html__( 'Database connection failed.', 'bbpress' ) );
     143            $error = new WP_Error( 'bbp_converter_db_connection_failed', esc_html__( 'Database connection failed.', 'bbpress' ) );
     144            wp_send_json_error( $error );
    136145        }
    137146
     
    140149
    141150        /**
    142          * Error Reporting
     151         * Don't wp_die() uncontrollably
    143152         */
    144         $this->wpdb->show_errors();
    145         $this->opdb->show_errors();
     153        $this->wpdb->show_errors( false );
     154        $this->opdb->show_errors( false );
    146155
    147156        /**
     
    624633     */
    625634    public function convert_forum_parents( $start = 1 ) {
    626 
    627635        $has_update = false;
    628 
    629         if ( ! empty( $this->sync_table ) ) {
    630             $query = $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value > 0 LIMIT {$start}, {$this->max_rows}", '_bbp_old_forum_parent_id' );
    631         } else {
    632             $query = $this->wpdb->prepare( "SELECT post_id AS value_id, meta_value FROM {$this->wpdb->postmeta} WHERE meta_key = %s AND meta_value > 0 LIMIT {$start}, {$this->max_rows}", '_bbp_old_forum_parent_id' );
    633         }
    634 
    635         update_option( '_bbp_converter_query', $query );
    636 
    637         $forum_array = $this->wpdb->get_results( $query );
    638 
    639         foreach ( (array) $forum_array as $row ) {
     636        $query      = ! empty( $this->sync_table )
     637            ? $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value > 0 LIMIT {$start}, {$this->max_rows}", '_bbp_old_forum_parent_id' )
     638            : $this->wpdb->prepare( "SELECT post_id AS value_id, meta_value FROM {$this->wpdb->postmeta} WHERE meta_key = %s AND meta_value > 0 LIMIT {$start}, {$this->max_rows}", '_bbp_old_forum_parent_id' );
     639
     640        foreach ( $this->get_results( $query ) as $row ) {
    640641            $parent_id = $this->callback_forumid( $row->meta_value );
    641             $this->wpdb->query( $this->wpdb->prepare( "UPDATE {$this->wpdb->posts} SET post_parent = %d WHERE ID = %d LIMIT 1", $parent_id, $row->value_id ) );
     642            $this->query( $this->wpdb->prepare( "UPDATE {$this->wpdb->posts} SET post_parent = %d WHERE ID = %d LIMIT 1", $parent_id, $row->value_id ) );
    642643            $has_update = true;
    643644        }
     
    652653     */
    653654    public function convert_topic_stickies( $start = 1 ) {
    654 
    655655        $has_update = false;
    656 
    657         if ( ! empty( $this->sync_table ) ) {
    658             $query = $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value = %s LIMIT {$start}, {$this->max_rows}", '_bbp_old_sticky_status_id', 'sticky' );
    659         } else {
    660             $query = $this->wpdb->prepare( "SELECT post_id AS value_id, meta_value FROM {$this->wpdb->postmeta} WHERE meta_key = %s AND meta_value = %s LIMIT {$start}, {$this->max_rows}", '_bbp_old_sticky_status_id', 'sticky' );
    661         }
    662 
    663         update_option( '_bbp_converter_query', $query );
    664 
    665         $sticky_array = $this->wpdb->get_results( $query );
    666 
    667         foreach ( (array) $sticky_array as $row ) {
     656        $query      = ! empty( $this->sync_table )
     657            ? $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value = %s LIMIT {$start}, {$this->max_rows}", '_bbp_old_sticky_status_id', 'sticky' )
     658            : $this->wpdb->prepare( "SELECT post_id AS value_id, meta_value FROM {$this->wpdb->postmeta} WHERE meta_key = %s AND meta_value = %s LIMIT {$start}, {$this->max_rows}", '_bbp_old_sticky_status_id', 'sticky' );
     659
     660        foreach ( $this->get_results( $query ) as $row ) {
    668661            bbp_stick_topic( $row->value_id );
    669662            $has_update = true;
     
    679672     */
    680673    public function convert_topic_super_stickies( $start = 1 ) {
    681 
    682674        $has_update = false;
    683 
    684         if ( ! empty( $this->sync_table ) ) {
    685             $query = $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value = %s LIMIT {$start}, {$this->max_rows}", '_bbp_old_sticky_status_id', 'super-sticky' );
    686         } else {
    687             $query = $this->wpdb->prepare( "SELECT post_id AS value_id, meta_value FROM {$this->wpdb->postmeta} WHERE meta_key = %s AND meta_value = %s LIMIT {$start}, {$this->max_rows}", '_bbp_old_sticky_status_id', 'super-sticky' );
    688         }
    689 
    690         update_option( '_bbp_converter_query', $query );
    691 
    692         $sticky_array = $this->wpdb->get_results( $query );
    693 
    694         foreach ( (array) $sticky_array as $row ) {
     675        $query      = ! empty( $this->sync_table )
     676            ? $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value = %s LIMIT {$start}, {$this->max_rows}", '_bbp_old_sticky_status_id', 'super-sticky' )
     677            : $this->wpdb->prepare( "SELECT post_id AS value_id, meta_value FROM {$this->wpdb->postmeta} WHERE meta_key = %s AND meta_value = %s LIMIT {$start}, {$this->max_rows}", '_bbp_old_sticky_status_id', 'super-sticky' );
     678
     679        foreach ( $this->get_results( $query ) as $row ) {
    695680            $super = true;
    696681            bbp_stick_topic( $row->value_id, $super );
     
    707692     */
    708693    public function convert_topic_closed_topics( $start = 1 ) {
    709 
    710694        $has_update = false;
    711 
    712         if ( ! empty( $this->sync_table ) ) {
    713             $query = $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value = %s LIMIT {$start}, {$this->max_rows}", '_bbp_old_closed_status_id', 'closed' );
    714         } else {
    715             $query = $this->wpdb->prepare( "SELECT post_id AS value_id, meta_value FROM {$this->wpdb->postmeta} WHERE meta_key = %s AND meta_value = %s LIMIT {$start}, {$this->max_rows}", '_bbp_old_closed_status_id', 'closed' );
    716         }
    717 
    718         update_option( '_bbp_converter_query', $query );
    719 
    720         $closed_topic = $this->wpdb->get_results( $query );
    721 
    722         foreach ( (array) $closed_topic as $row ) {
     695        $query      = ! empty( $this->sync_table )
     696            ? $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value = %s LIMIT {$start}, {$this->max_rows}", '_bbp_old_closed_status_id', 'closed' )
     697            : $this->wpdb->prepare( "SELECT post_id AS value_id, meta_value FROM {$this->wpdb->postmeta} WHERE meta_key = %s AND meta_value = %s LIMIT {$start}, {$this->max_rows}", '_bbp_old_closed_status_id', 'closed' );
     698
     699        foreach ( $this->get_results( $query ) as $row ) {
    723700            bbp_close_topic( $row->value_id );
    724701            $has_update = true;
     
    734711     */
    735712    public function convert_reply_to_parents( $start = 1 ) {
    736 
    737713        $has_update = false;
    738 
    739         if ( ! empty( $this->sync_table ) ) {
    740             $query = $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value > 0 LIMIT {$start}, {$this->max_rows}", '_bbp_old_reply_to_id' );
    741         } else {
    742             $query = $this->wpdb->prepare( "SELECT post_id AS value_id, meta_value FROM {$this->wpdb->postmeta} WHERE meta_key = %s AND meta_value > 0 LIMIT {$start}, {$this->max_rows}", '_bbp_old_reply_to_id' );
    743         }
    744 
    745         update_option( '_bbp_converter_query', $query );
    746 
    747         $reply_to_array = $this->wpdb->get_results( $query );
    748 
    749         foreach ( (array) $reply_to_array as $row ) {
     714        $query      = ! empty( $this->sync_table )
     715            ? $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value > 0 LIMIT {$start}, {$this->max_rows}", '_bbp_old_reply_to_id' )
     716            : $this->wpdb->prepare( "SELECT post_id AS value_id, meta_value FROM {$this->wpdb->postmeta} WHERE meta_key = %s AND meta_value > 0 LIMIT {$start}, {$this->max_rows}", '_bbp_old_reply_to_id' );
     717
     718        foreach ( $this->get_results( $query ) as $row ) {
    750719            $reply_to = $this->callback_reply_to( $row->meta_value );
    751             $this->wpdb->query( $this->wpdb->prepare( "UPDATE {$this->wpdb->postmeta} SET meta_value = %s WHERE meta_key = %s AND post_id = %d LIMIT 1", $reply_to, '_bbp_reply_to', $row->value_id ) );
     720            $this->query( $this->wpdb->prepare( "UPDATE {$this->wpdb->postmeta} SET meta_value = %s WHERE meta_key = %s AND post_id = %d LIMIT 1", $reply_to, '_bbp_reply_to', $row->value_id ) );
    752721            $has_update = true;
    753722        }
     
    781750                            AND wp_postmeta2.meta_key = %s
    782751                            LIMIT {$start}, {$this->max_rows}", 'true', '_bbp_old_topic_author_name_id' );
    783 
    784         }
    785 
    786         update_option( '_bbp_converter_query', $query );
    787 
    788         $anonymous_topics = $this->wpdb->get_results( $query );
    789 
    790         foreach ( (array) $anonymous_topics as $row ) {
     752        }
     753
     754        foreach ( $this->get_results( $query ) as $row ) {
    791755            $anonymous_topic_author_id = 0;
    792             $this->wpdb->query( $this->wpdb->prepare( "UPDATE {$this->wpdb->posts} SET post_author = %d WHERE ID = %d LIMIT 1", $anonymous_topic_author_id, $row->topic_id ) );
     756            $this->query( $this->wpdb->prepare( "UPDATE {$this->wpdb->posts} SET post_author = %d WHERE ID = %d LIMIT 1", $anonymous_topic_author_id, $row->topic_id ) );
    793757
    794758            add_post_meta( $row->topic_id, '_bbp_anonymous_name', $row->topic_author );
     
    827791        }
    828792
    829         update_option( '_bbp_converter_query', $query );
    830 
    831         $anonymous_replies = $this->wpdb->get_results( $query );
    832 
    833         foreach ( (array) $anonymous_replies as $row ) {
     793        foreach ( $this->get_results( $query ) as $row ) {
    834794            $anonymous_reply_author_id = 0;
    835             $this->wpdb->query( $this->wpdb->prepare( "UPDATE {$this->wpdb->posts} SET post_author = %d WHERE ID = %d LIMIT 1", $anonymous_reply_author_id, $row->reply_id ) );
     795            $this->query( $this->wpdb->prepare( "UPDATE {$this->wpdb->posts} SET post_author = %d WHERE ID = %d LIMIT 1", $anonymous_reply_author_id, $row->reply_id ) );
    836796
    837797            add_post_meta( $row->reply_id, '_bbp_anonymous_name', $row->reply_author );
     
    853813        /** Delete topics/forums/posts ****************************************/
    854814
    855         if ( true === $this->sync_table ) {
    856             $query = $this->wpdb->prepare( "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 = %s GROUP BY value_id ORDER BY value_id DESC LIMIT {$this->max_rows}", 'post' );
    857         } else {
    858             $query = $this->wpdb->prepare( "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}" );
    859         }
    860 
    861         update_option( '_bbp_converter_query', $query );
    862 
    863         $posts = $this->wpdb->get_results( $query, ARRAY_A );
     815        $query = ! empty( $this->sync_table )
     816            ? $this->wpdb->prepare( "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 = %s GROUP BY value_id ORDER BY value_id DESC LIMIT {$this->max_rows}", 'post' )
     817            : $this->wpdb->prepare( "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}" );
     818
     819        $posts = $this->get_results( $query, ARRAY_A );
    864820
    865821        if ( isset( $posts[0] ) && ! empty( $posts[0]['value_id'] ) ) {
     
    872828        /** Delete users ******************************************************/
    873829
    874         if ( true === $this->sync_table ) {
    875             $query = $this->wpdb->prepare( "SELECT value_id FROM {$this->sync_table_name} INNER JOIN {$this->wpdb->users} ON(value_id = ID) WHERE meta_key = %s AND value_type = %s LIMIT {$this->max_rows}", '_bbp_old_user_id', 'user' );
    876         } else {
    877             $query = $this->wpdb->prepare( "SELECT user_id AS value_id FROM {$this->wpdb->usermeta} WHERE meta_key = %s LIMIT {$this->max_rows}", '_bbp_old_user_id' );
    878         }
    879 
    880         update_option( '_bbp_converter_query', $query );
    881 
    882         $users = $this->wpdb->get_results( $query, ARRAY_A );
     830        $query = ! empty( $this->sync_table )
     831            ? $this->wpdb->prepare( "SELECT value_id FROM {$this->sync_table_name} INNER JOIN {$this->wpdb->users} ON(value_id = ID) WHERE meta_key = %s AND value_type = %s LIMIT {$this->max_rows}", '_bbp_old_user_id', 'user' )
     832            : $this->wpdb->prepare( "SELECT user_id AS value_id FROM {$this->wpdb->usermeta} WHERE meta_key = %s LIMIT {$this->max_rows}", '_bbp_old_user_id' );
     833
     834        $users = $this->get_results( $query, ARRAY_A );
    883835
    884836        if ( ! empty( $users ) ) {
     
    901853     */
    902854    public function clean_passwords( $start = 1 ) {
    903 
    904855        $has_delete = false;
    905856        $query      = $this->wpdb->prepare( "SELECT user_id, meta_value FROM {$this->wpdb->usermeta} WHERE meta_key = %s LIMIT {$start}, {$this->max_rows}", '_bbp_password' );
    906 
    907         update_option( '_bbp_converter_query', $query );
    908 
    909         $converted = $this->wpdb->get_results( $query, ARRAY_A );
     857        $converted  = $this->get_results( $query, ARRAY_A );
    910858
    911859        if ( ! empty( $converted ) ) {
     
    913861            foreach ( $converted as $value ) {
    914862                if ( is_serialized( $value['meta_value'] ) ) {
    915                     $this->wpdb->query( $this->wpdb->prepare( "UPDATE {$this->wpdb->users} SET user_pass = '' WHERE ID = %d", $value['user_id'] ) );
     863                    $this->query( $this->wpdb->prepare( "UPDATE {$this->wpdb->users} SET user_pass = '' WHERE ID = %d", $value['user_id'] ) );
    916864                } else {
    917                     $this->wpdb->query( $this->wpdb->prepare( "UPDATE {$this->wpdb->users} SET user_pass = %s WHERE ID = %d", $value['meta_value'], $value['user_id'] ) );
    918                     $this->wpdb->query( $this->wpdb->prepare( "DELETE FROM {$this->wpdb->usermeta} WHERE meta_key = %s AND user_id = %d", '_bbp_password', $value['user_id'] ) );
     865                    $this->query( $this->wpdb->prepare( "UPDATE {$this->wpdb->users} SET user_pass = %s WHERE ID = %d", $value['meta_value'], $value['user_id'] ) );
     866                    $this->query( $this->wpdb->prepare( "DELETE FROM {$this->wpdb->usermeta} WHERE meta_key = %s AND user_id = %d", '_bbp_password', $value['user_id'] ) );
    919867                }
    920868            }
     
    944892    private function get_fields( $tablename ) {
    945893        $rval        = array();
    946         $field_array = $this->wpdb->get_results( 'DESCRIBE ' . $tablename, ARRAY_A );
     894        $field_array = $this->get_results( 'DESCRIBE ' . $tablename, ARRAY_A );
    947895
    948896        foreach ( $field_array as $field ) {
     
    960908    }
    961909
     910    /** Database Wrappers *****************************************************/
     911
     912    /**
     913     * Update the last query option and return results
     914     *
     915     * @param string $query
     916     * @param string $output
     917     */
     918    private function get_row( $query = '' ) {
     919        update_option( '_bbp_converter_query', $query );
     920
     921        return $this->wpdb->get_row( $query );
     922    }
     923
     924    /**
     925     * Update the last query option and return results
     926     *
     927     * @param string $query
     928     * @param string $output
     929     */
     930    private function get_results( $query = '', $output = OBJECT ) {
     931        update_option( '_bbp_converter_query', $query );
     932
     933        return (array) $this->wpdb->get_results( $query, $output );
     934    }
     935
     936    /**
     937     * Update the last query option and do a general query
     938     *
     939     * @param string $query
     940     */
     941    private function query( $query = '' ) {
     942        update_option( '_bbp_converter_query', $query );
     943
     944        return $this->wpdb->query( $query );
     945    }
     946
    962947    /** Callbacks *************************************************************/
    963948
     
    969954     */
    970955    public function callback_pass( $username, $password ) {
    971         $user = $this->wpdb->get_row( $this->wpdb->prepare( "SELECT * FROM {$this->wpdb->users} WHERE user_login = %s AND user_pass = '' LIMIT 1", $username ) );
     956        $user = $this->get_row( $this->wpdb->prepare( "SELECT * FROM {$this->wpdb->users} WHERE user_login = %s AND user_pass = '' LIMIT 1", $username ) );
    972957        if ( ! empty( $user ) ) {
    973             $usermeta = $this->wpdb->get_row( $this->wpdb->prepare( "SELECT * FROM {$this->wpdb->usermeta} WHERE meta_key = %s AND user_id = %d LIMIT 1", '_bbp_password', $user->ID ) );
     958            $usermeta = $this->get_row( $this->wpdb->prepare( "SELECT * FROM {$this->wpdb->usermeta} WHERE meta_key = %s AND user_id = %d LIMIT 1", '_bbp_password', $user->ID ) );
    974959
    975960            if ( ! empty( $usermeta ) ) {
    976961                if ( $this->authenticate_pass( $password, $usermeta->meta_value ) ) {
    977                     $this->wpdb->query( $this->wpdb->prepare( "UPDATE {$this->wpdb->users} SET user_pass = %s WHERE ID = %d", wp_hash_password( $password ), $user->ID ) );
    978                     $this->wpdb->query( $this->wpdb->prepare( "DELETE FROM {$this->wpdb->usermeta} WHERE meta_key = %s AND user_id = %d", '_bbp_password', $user->ID ) );
     962                    $this->query( $this->wpdb->prepare( "UPDATE {$this->wpdb->users} SET user_pass = %s WHERE ID = %d", wp_hash_password( $password ), $user->ID ) );
     963                    $this->query( $this->wpdb->prepare( "DELETE FROM {$this->wpdb->usermeta} WHERE meta_key = %s AND user_id = %d", '_bbp_password', $user->ID ) );
    979964                }
    980965            }
     
    990975    private function callback_forumid( $field ) {
    991976        if ( ! isset( $this->map_forumid[ $field ] ) ) {
    992             if ( ! empty( $this->sync_table ) ) {
    993                 $row = $this->wpdb->get_row( $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value = %s LIMIT 1", '_bbp_old_forum_id', $field ) );
    994             } else {
    995                 $row = $this->wpdb->get_row( $this->wpdb->prepare( "SELECT post_id AS value_id FROM {$this->wpdb->postmeta} WHERE meta_key = %s AND meta_value = %s LIMIT 1", '_bbp_old_forum_id', $field ) );
    996             }
    997 
    998             if ( ! is_null( $row ) ) {
    999                 $this->map_forumid[ $field ] = $row->value_id;
    1000             } else {
    1001                 $this->map_forumid[ $field ] = 0;
    1002             }
    1003         }
     977            $row = ! empty( $this->sync_table )
     978                ? $this->get_row( $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value = %s LIMIT 1", '_bbp_old_forum_id', $field ) )
     979                : $this->get_row( $this->wpdb->prepare( "SELECT post_id AS value_id FROM {$this->wpdb->postmeta} WHERE meta_key = %s AND meta_value = %s LIMIT 1", '_bbp_old_forum_id', $field ) );
     980
     981            $this->map_forumid[ $field ] = ! is_null( $row )
     982                ? $row->value_id
     983                : 0;
     984        }
     985
    1004986        return $this->map_forumid[ $field ];
    1005987    }
     
    1013995    private function callback_topicid( $field ) {
    1014996        if ( ! isset( $this->map_topicid[ $field ] ) ) {
    1015             if ( ! empty( $this->sync_table ) ) {
    1016                 $row = $this->wpdb->get_row( $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value = %s LIMIT 1", '_bbp_old_topic_id', $field ) );
    1017             } else {
    1018                 $row = $this->wpdb->get_row( $this->wpdb->prepare( "SELECT post_id AS value_id FROM {$this->wpdb->postmeta} WHERE meta_key = %s AND meta_value = %s LIMIT 1", '_bbp_old_topic_id', $field ) );
    1019             }
    1020 
    1021             if ( ! is_null( $row ) ) {
    1022                 $this->map_topicid[ $field ] = $row->value_id;
    1023             } else {
    1024                 $this->map_topicid[ $field ] = 0;
    1025             }
    1026         }
     997            $row = ! empty( $this->sync_table )
     998                ? $this->get_row( $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value = %s LIMIT 1", '_bbp_old_topic_id', $field ) )
     999                : $this->get_row( $this->wpdb->prepare( "SELECT post_id AS value_id FROM {$this->wpdb->postmeta} WHERE meta_key = %s AND meta_value = %s LIMIT 1", '_bbp_old_topic_id', $field ) );
     1000
     1001            $this->map_topicid[ $field ] = ! is_null( $row )
     1002                ? $row->value_id
     1003                : 0;
     1004        }
     1005
    10271006        return $this->map_topicid[ $field ];
    10281007    }
     
    10381017    private function callback_reply_to( $field ) {
    10391018        if ( ! isset( $this->map_reply_to[ $field ] ) ) {
    1040             if ( ! empty( $this->sync_table ) ) {
    1041                 $row = $this->wpdb->get_row( $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value = %s LIMIT 1", '_bbp_old_reply_id', $field ) );
    1042             } else {
    1043                 $row = $this->wpdb->get_row( $this->wpdb->prepare( "SELECT post_id AS value_id FROM {$this->wpdb->postmeta} WHERE meta_key = %s AND meta_value = %s LIMIT 1", '_bbp_old_reply_id', $field ) );
    1044             }
    1045 
    1046             if ( ! is_null( $row ) ) {
    1047                 $this->map_reply_to[ $field ] = $row->value_id;
    1048             } else {
    1049                 $this->map_reply_to[ $field ] = 0;
    1050             }
    1051         }
     1019            $row = ! empty( $this->sync_table )
     1020                ? $this->get_row( $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value = %s LIMIT 1", '_bbp_old_reply_id', $field ) )
     1021                : $this->get_row( $this->wpdb->prepare( "SELECT post_id AS value_id FROM {$this->wpdb->postmeta} WHERE meta_key = %s AND meta_value = %s LIMIT 1", '_bbp_old_reply_id', $field ) );
     1022
     1023            $this->map_reply_to[ $field ] = ! is_null( $row )
     1024                ? $row->value_id
     1025                : 0;
     1026        }
     1027
    10521028        return $this->map_reply_to[ $field ];
    10531029    }
     
    10611037    private function callback_userid( $field ) {
    10621038        if ( ! isset( $this->map_userid[ $field ] ) ) {
    1063             if ( ! empty( $this->sync_table ) ) {
    1064                 $row = $this->wpdb->get_row( $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value = %s LIMIT 1", '_bbp_old_user_id', $field ) );
    1065             } else {
    1066                 $row = $this->wpdb->get_row( $this->wpdb->prepare( "SELECT user_id AS value_id FROM {$this->wpdb->usermeta} WHERE meta_key = %s AND meta_value = %s LIMIT 1", '_bbp_old_user_id', $field ) );
    1067             }
     1039            $row = ! empty( $this->sync_table )
     1040                ? $this->get_row( $this->wpdb->prepare( "SELECT value_id, meta_value FROM {$this->sync_table_name} WHERE meta_key = %s AND meta_value = %s LIMIT 1", '_bbp_old_user_id', $field ) )
     1041                : $this->get_row( $this->wpdb->prepare( "SELECT user_id AS value_id FROM {$this->wpdb->usermeta} WHERE meta_key = %s AND meta_value = %s LIMIT 1", '_bbp_old_user_id', $field ) );
    10681042
    10691043            if ( ! is_null( $row ) ) {
    10701044                $this->map_userid[ $field ] = $row->value_id;
    10711045            } else {
    1072                 if ( true === $this->convert_users ) {
    1073                     $this->map_userid[ $field ] = 0;
    1074                 } else {
    1075                     $this->map_userid[ $field ] = $field;
    1076                 }
     1046                $this->map_userid[ $field ] = ( true === $this->convert_users )
     1047                    ? 0
     1048                    : $field;
    10771049            }
    10781050        }
     1051
    10791052        return $this->map_userid[ $field ];
    10801053    }
     
    10891062     */
    10901063    private function callback_check_anonymous( $field ) {
    1091 
    1092         if ( $this->callback_userid( $field ) == 0 ) {
    1093             $field = 'true';
    1094         } else {
    1095             $field = 'false';
    1096         }
     1064        $field = ( $this->callback_userid( $field ) == 0 )
     1065            ? 'true'
     1066            : 'false';
    10971067
    10981068        return $field;
     
    11101080            $this->map_topicid_to_forumid[ $topicid ] = 0;
    11111081        } elseif ( ! isset( $this->map_topicid_to_forumid[ $topicid ] ) ) {
    1112             $row = $this->wpdb->get_row( $this->wpdb->prepare( "SELECT post_parent FROM {$this->wpdb->posts} WHERE ID = %d LIMIT 1", $topicid ) );
    1113 
    1114             if ( ! is_null( $row ) ) {
    1115                 $this->map_topicid_to_forumid[ $topicid ] = $row->post_parent;
    1116             } else {
    1117                 $this->map_topicid_to_forumid[ $topicid ] = 0;
    1118             }
     1082            $row = $this->get_row( $this->wpdb->prepare( "SELECT post_parent FROM {$this->wpdb->posts} WHERE ID = %d LIMIT 1", $topicid ) );
     1083
     1084            $this->map_topicid_to_forumid[ $topicid ] = ! is_null( $row )
     1085                ? $row->post_parent
     1086                : 0;
    11191087        }
    11201088
     
    11501118    }
    11511119}
     1120endif;
  • trunk/src/includes/admin/classes/class-bbp-converter.php

    r6573 r6601  
    1313defined( 'ABSPATH' ) || exit;
    1414
     15if ( ! class_exists( 'BBP_Converter' ) ) :
    1516/**
    1617 * Main BBP_Converter Class
     
    4950
    5051    /**
     52     * @var string Path to included platforms
     53     */
     54    public $converters_dir = '';
     55
     56    /**
    5157     * The main bbPress Converter loader
    5258     *
     
    5460     */
    5561    public function __construct() {
     62        $this->setup_globals();
    5663        $this->setup_actions();
     64    }
     65
     66    /**
     67     * Admin globals
     68     *
     69     * @since 2.6.0 bbPress (r6598)
     70     */
     71    public function setup_globals() {
     72        $this->converters_dir = bbp_setup_admin()->admin_dir . 'converters/';
    5773    }
    5874
     
    86102            // Vars
    87103            'ajax_nonce' => wp_create_nonce( 'bbp_converter_process' ),
    88             'halt'       => (bool) defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY,
    89             'started'    => (bool) get_option( '_bbp_converter_step',       false ),
    90             'delay'      => (int)  get_option( '_bbp_converter_delay_time', 1     ),
     104            'delay'      => (int)  get_option( '_bbp_converter_delay_time', 2 ),
    91105            'running'    => false,
    92             'complete'   => false,
    93             'timer'      => false,
     106            'status'     => false,
     107            'started'    => (bool) get_option( '_bbp_converter_step', 0 ),
    94108
    95109            // Strings
     
    97111
    98112                // Button text
    99                 'button_start'    => esc_html__( 'Start',    'bbpress' ),
    100                 'button_continue' => esc_html__( 'Continue', 'bbpress' ),
     113                'button_start'        => esc_html__( 'Start',    'bbpress' ),
     114                'button_continue'     => esc_html__( 'Continue', 'bbpress' ),
    101115
    102116                // Start button clicked
    103                 'start_start'    => esc_html__( 'Starting Import',   'bbpress' ),
    104                 'start_continue' => esc_html__( 'Continuing Import', 'bbpress' ),
     117                'start_start'         => esc_html__( 'Starting Import...',   'bbpress' ),
     118                'start_continue'      => esc_html__( 'Continuing Import...', 'bbpress' ),
    105119
    106120                // Import
    107                 'import_complete'     => esc_html__( 'Import Finished',            'bbpress' ),
    108                 'import_stopped_user' => esc_html__( 'Import Stopped (by User)',   'bbpress' ),
    109                 'import_error_db'     => esc_html__( 'Database Connection Failed', 'bbpress' ),
    110                 'import_error_halt'   => esc_html__( 'Import Halted (Error)',      'bbpress' ),
    111 
    112                 // Timer
    113                 'timer_stopped'       => esc_html__( 'Timer: Stopped',    'bbpress' ),
    114                 'timer_waiting'       => esc_html__( 'Timer: Waiting...', 'bbpress' ),
    115                 'timer_counting'      => esc_html__( 'Timer: %s',         'bbpress' )
     121                'import_complete'     => esc_html__( 'Import Finished.',            'bbpress' ),
     122                'import_stopped_user' => esc_html__( 'Import Stopped (by User.)',   'bbpress' ),
     123                'import_error_halt'   => esc_html__( 'Import Halted (Error.)',      'bbpress' ),
     124                'import_error_db'     => esc_html__( 'Database Connection Failed.', 'bbpress' ),
     125
     126                // Status
     127                'status_complete'     => esc_html__( 'Finished',            'bbpress' ),
     128                'status_stopped'      => esc_html__( 'Stopped',             'bbpress' ),
     129                'status_starting'     => esc_html__( 'Starting',            'bbpress' ),
     130                'status_up_next'      => esc_html__( 'Up next: Step %s...', 'bbpress' ),
     131                'status_counting'     => esc_html__( 'Next in %s...',       'bbpress' )
    116132            )
    117133        ) );
     
    138154
    139155    /**
    140      * Wrap the converter output in paragraph tags, so styling can be applied
     156     * Wrap the converter output in HTML, so styling can be applied
    141157     *
    142158     * @since 2.1.0 bbPress (r4052)
     
    144160     * @param string $output
    145161     */
    146     private function converter_output( $output = '' ) {
    147 
    148         // Maybe include last query
    149         $query = get_option( '_bbp_converter_query' );
    150         if ( ! empty( $query ) ) {
    151             $output = $output . '<span class="query">' . esc_attr( $query ) . '</span>';
    152         }
     162    private function converter_response( $output = '' ) {
    153163
    154164        // Maybe prepend the step
    155         $step = ! empty( $this->step )
    156             ? sprintf( '<span class="step">%s:</span> ', $this->step )
    157             : '';
     165        $output = ! empty( $this->step )
     166            ? sprintf( '<span class="step">%s:</span> %s', $this->step, $output )
     167            : $output;
    158168
    159169        // Output
    160         echo $step . $output;
     170        wp_send_json_success( array(
     171            'query'        => get_option( '_bbp_converter_query', '' ),
     172            'current_step' => $this->step,
     173            'final_step'   => $this->max_steps,
     174            'progress'     => $output
     175        ) );
    161176    }
    162177
     
    202217            '_bbp_converter_start' => 0,
    203218
     219            // Halt
     220            '_bbp_converter_halt' => ! empty( $_POST['_bbp_converter_halt'] )
     221                ? (int) $_POST['_bbp_converter_halt']
     222                : 0,
     223
    204224            // Rows
    205225            '_bbp_converter_rows' => ! empty( $_POST['_bbp_converter_rows'] )
     
    322342     */
    323343    private function bump_step() {
    324         update_option( '_bbp_converter_step',  $this->step + 1 );
    325         update_option( '_bbp_converter_start', 0               );
     344
     345        // Start at zero
     346        update_option( '_bbp_converter_start', 0 );
     347
     348        // Don't let step go over max
     349        $step = ( ( $this->step + 1 ) <= $this->max_steps )
     350            ? (int) $this->step + 1
     351            : 0;
     352
     353        // Update or delete
     354        ! empty( $step )
     355            ? update_option( '_bbp_converter_step', $step )
     356            : delete_option( '_bbp_converter_step' );
    326357    }
    327358
     
    444475     */
    445476    private function step_sync_table() {
    446         if ( ! empty( $_POST['_bbp_converter_clean'] ) ) {
     477        if ( true === $this->converter->clean ) {
    447478            if ( $this->converter->clean( $this->start ) ) {
     479                $this->bump_step();
    448480                $this->sync_table( true );
    449                 $this->bump_step();
    450481
    451482                if ( empty( $this->start ) ) {
    452                     $this->converter_output( esc_html__( 'Recreating sync-table', 'bbpress' ) );
     483                    $this->converter_response( esc_html__( 'Recreating sync-table', 'bbpress' ) );
    453484                }
    454485            } else {
    455                 $this->converter_output( sprintf( esc_html__( 'Deleting previously converted data (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
    456486                $this->bump_start();
    457             }
    458         } else {
    459             $this->converter_output( esc_html__( 'Skipping sync-table clean-up', 'bbpress' ) );
     487                $this->converter_response( sprintf( esc_html__( 'Deleting previously converted data (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
     488            }
     489
     490            $this->converter->clean = false;
     491        } else {
     492            $this->bump_step();
    460493            $this->sync_table( false );
    461             $this->bump_step();
     494            $this->converter_response( esc_html__( 'Skipping sync-table clean-up', 'bbpress' ) );
    462495        }
    463496    }
     
    474507
    475508                if ( empty( $this->start ) ) {
    476                     $this->converter_output( esc_html__( 'No users to import', 'bbpress' ) );
     509                    $this->converter_response( esc_html__( 'No users to import', 'bbpress' ) );
    477510                }
    478511            } else {
    479512                $this->bump_start();
    480                 $this->converter_output( sprintf(  esc_html__( 'Converting users (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
    481             }
    482         } else {
    483             $this->bump_step();
    484             $this->converter_output( esc_html__( 'Skipping user clean-up', 'bbpress' ) );
     513                $this->converter_response( sprintf(  esc_html__( 'Converting users (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
     514            }
     515        } else {
     516            $this->bump_step();
     517            $this->converter_response( esc_html__( 'Skipping user clean-up', 'bbpress' ) );
    485518        }
    486519    }
     
    497530
    498531                if ( empty( $this->start ) ) {
    499                     $this->converter_output( esc_html__( 'No passwords to clear', 'bbpress' ) );
     532                    $this->converter_response( esc_html__( 'No passwords to clear', 'bbpress' ) );
    500533                }
    501534            } else {
    502535                $this->bump_start();
    503                 $this->converter_output( sprintf( esc_html__( 'Delete users WordPress default passwords (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
    504             }
    505         } else {
    506             $this->bump_step();
    507             $this->converter_output( esc_html__( 'Skipping password clean-up', 'bbpress' ) );
     536                $this->converter_response( sprintf( esc_html__( 'Delete users WordPress default passwords (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
     537            }
     538        } else {
     539            $this->bump_step();
     540            $this->converter_response( esc_html__( 'Skipping password clean-up', 'bbpress' ) );
    508541        }
    509542    }
     
    519552
    520553            if ( empty( $this->start ) ) {
    521                 $this->converter_output( esc_html__( 'No forums to import', 'bbpress' ) );
    522             }
    523         } else {
    524             $this->bump_start();
    525             $this->converter_output( sprintf( esc_html__( 'Converting forums (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
     554                $this->converter_response( esc_html__( 'No forums to import', 'bbpress' ) );
     555            }
     556        } else {
     557            $this->bump_start();
     558            $this->converter_response( sprintf( esc_html__( 'Converting forums (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
    526559        }
    527560    }
     
    537570
    538571            if ( empty( $this->start ) ) {
    539                 $this->converter_output( esc_html__( 'No forum parents to import', 'bbpress' ) );
    540             }
    541         } else {
    542             $this->bump_start();
    543             $this->converter_output( sprintf( esc_html__( 'Calculating forum hierarchy (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
     572                $this->converter_response( esc_html__( 'No forum parents to import', 'bbpress' ) );
     573            }
     574        } else {
     575            $this->bump_start();
     576            $this->converter_response( sprintf( esc_html__( 'Calculating forum hierarchy (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
    544577        }
    545578    }
     
    555588
    556589            if ( empty( $this->start ) ) {
    557                 $this->converter_output( esc_html__( 'No forum subscriptions to import', 'bbpress' ) );
    558             }
    559         } else {
    560             $this->bump_start();
    561             $this->converter_output( sprintf( esc_html__( 'Converting forum subscriptions (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
     590                $this->converter_response( esc_html__( 'No forum subscriptions to import', 'bbpress' ) );
     591            }
     592        } else {
     593            $this->bump_start();
     594            $this->converter_response( sprintf( esc_html__( 'Converting forum subscriptions (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
    562595        }
    563596    }
     
    573606
    574607            if ( empty( $this->start ) ) {
    575                 $this->converter_output( esc_html__( 'No topics to import', 'bbpress' ) );
    576             }
    577         } else {
    578             $this->bump_start();
    579             $this->converter_output( sprintf( esc_html__( 'Converting topics (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
     608                $this->converter_response( esc_html__( 'No topics to import', 'bbpress' ) );
     609            }
     610        } else {
     611            $this->bump_start();
     612            $this->converter_response( sprintf( esc_html__( 'Converting topics (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
    580613        }
    581614    }
     
    591624
    592625            if ( empty( $this->start ) ) {
    593                 $this->converter_output( esc_html__( 'No anonymous topic authors to import', 'bbpress' ) );
    594             }
    595         } else {
    596             $this->bump_start();
    597             $this->converter_output( sprintf( esc_html__( 'Converting anonymous topic authors (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
     626                $this->converter_response( esc_html__( 'No anonymous topic authors to import', 'bbpress' ) );
     627            }
     628        } else {
     629            $this->bump_start();
     630            $this->converter_response( sprintf( esc_html__( 'Converting anonymous topic authors (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
    598631        }
    599632    }
     
    609642
    610643            if ( empty( $this->start ) ) {
    611                 $this->converter_output( esc_html__( 'No stickies to stick', 'bbpress' ) );
    612             }
    613         } else {
    614             $this->bump_start();
    615             $this->converter_output( sprintf( esc_html__( 'Calculating topic stickies (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
     644                $this->converter_response( esc_html__( 'No stickies to stick', 'bbpress' ) );
     645            }
     646        } else {
     647            $this->bump_start();
     648            $this->converter_response( sprintf( esc_html__( 'Calculating topic stickies (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
    616649        }
    617650    }
     
    627660
    628661            if ( empty( $this->start ) ) {
    629                 $this->converter_output( esc_html__( 'No super stickies to stick', 'bbpress' ) );
    630             }
    631         } else {
    632             $this->bump_start();
    633             $this->converter_output( sprintf( esc_html__( 'Calculating topic super stickies (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
     662                $this->converter_response( esc_html__( 'No super stickies to stick', 'bbpress' ) );
     663            }
     664        } else {
     665            $this->bump_start();
     666            $this->converter_response( sprintf( esc_html__( 'Calculating topic super stickies (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
    634667        }
    635668    }
     
    645678
    646679            if ( empty( $this->start ) ) {
    647                 $this->converter_output( esc_html__( 'No closed topics to close', 'bbpress' ) );
    648             }
    649         } else {
    650             $this->bump_start();
    651             $this->converter_output( sprintf( esc_html__( 'Calculating closed topics (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
     680                $this->converter_response( esc_html__( 'No closed topics to close', 'bbpress' ) );
     681            }
     682        } else {
     683            $this->bump_start();
     684            $this->converter_response( sprintf( esc_html__( 'Calculating closed topics (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
    652685        }
    653686    }
     
    663696
    664697            if ( empty( $this->start ) ) {
    665                 $this->converter_output( esc_html__( 'No topic tags to import', 'bbpress' ) );
    666             }
    667         } else {
    668             $this->bump_start();
    669             $this->converter_output( sprintf( esc_html__( 'Converting topic tags (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
     698                $this->converter_response( esc_html__( 'No topic tags to import', 'bbpress' ) );
     699            }
     700        } else {
     701            $this->bump_start();
     702            $this->converter_response( sprintf( esc_html__( 'Converting topic tags (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
    670703        }
    671704    }
     
    681714
    682715            if ( empty( $this->start ) ) {
    683                 $this->converter_output( esc_html__( 'No topic subscriptions to import', 'bbpress' ) );
    684             }
    685         } else {
    686             $this->bump_start();
    687             $this->converter_output( sprintf( esc_html__( 'Converting topic subscriptions (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
     716                $this->converter_response( esc_html__( 'No topic subscriptions to import', 'bbpress' ) );
     717            }
     718        } else {
     719            $this->bump_start();
     720            $this->converter_response( sprintf( esc_html__( 'Converting topic subscriptions (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
    688721        }
    689722    }
     
    699732
    700733            if ( empty( $this->start ) ) {
    701                 $this->converter_output( esc_html__( 'No favorites to import', 'bbpress' ) );
    702             }
    703         } else {
    704             $this->bump_start();
    705             $this->converter_output( sprintf( esc_html__( 'Converting favorites (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
     734                $this->converter_response( esc_html__( 'No favorites to import', 'bbpress' ) );
     735            }
     736        } else {
     737            $this->bump_start();
     738            $this->converter_response( sprintf( esc_html__( 'Converting favorites (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
    706739        }
    707740    }
     
    717750
    718751            if ( empty( $this->start ) ) {
    719                 $this->converter_output( esc_html__( 'No replies to import', 'bbpress' ) );
    720             }
    721         } else {
    722             $this->bump_start();
    723             $this->converter_output( sprintf( esc_html__( 'Converting replies (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
     752                $this->converter_response( esc_html__( 'No replies to import', 'bbpress' ) );
     753            }
     754        } else {
     755            $this->bump_start();
     756            $this->converter_response( sprintf( esc_html__( 'Converting replies (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
    724757        }
    725758    }
     
    735768
    736769            if ( empty( $this->start ) ) {
    737                 $this->converter_output( esc_html__( 'No anonymous reply authors to import', 'bbpress' ) );
    738             }
    739         } else {
    740             $this->bump_start();
    741             $this->converter_output( sprintf( esc_html__( 'Converting anonymous reply authors (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
     770                $this->converter_response( esc_html__( 'No anonymous reply authors to import', 'bbpress' ) );
     771            }
     772        } else {
     773            $this->bump_start();
     774            $this->converter_response( sprintf( esc_html__( 'Converting anonymous reply authors (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
    742775        }
    743776    }
     
    753786
    754787            if ( empty( $this->start ) ) {
    755                 $this->converter_output( esc_html__( 'No threaded replies to import', 'bbpress' ) );
    756             }
    757         } else {
    758             $this->bump_start();
    759             $this->converter_output( sprintf( esc_html__( 'Calculating threaded replies parents (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
     788                $this->converter_response( esc_html__( 'No threaded replies to import', 'bbpress' ) );
     789            }
     790        } else {
     791            $this->bump_start();
     792            $this->converter_response( sprintf( esc_html__( 'Calculating threaded replies parents (%1$s - %2$s)', 'bbpress' ), $this->min, $this->max ) );
    760793        }
    761794    }
     
    768801    private function step_done() {
    769802        $this->reset();
    770         $this->converter_output( esc_html__( 'Import Finished', 'bbpress' ) );
     803        $this->converter_response( esc_html__( 'Import Finished', 'bbpress' ) );
    771804    }
    772805
     
    826859    }
    827860}
     861endif;
  • trunk/src/includes/admin/tools/converter.php

    r6456 r6601  
    2424    // Default
    2525    $files  = array();
    26     $path   = bbpress()->admin->admin_dir . 'converters/';
     26    $path   = bbp_setup_converter()->converters_dir;
    2727    $curdir = opendir( $path );
    2828
     
    6767    // Create a new converter object if it's found
    6868    if ( isset( $converters[ $platform ] ) ) {
     69
     70        // Include & create the converter
    6971        require_once $converters[ $platform ];
    70         return new $platform;
     72        if ( class_exists( $platform ) ) {
     73            return new $platform;
     74        }
    7175    }
    7276
  • trunk/src/includes/core/abstraction.php

    r6583 r6601  
    1616// Exit if accessed directly
    1717defined( 'ABSPATH' ) || exit;
     18
     19/**
     20 * Setup Admin
     21 *
     22 * This exists outside of "/includes/admin/" because the converter may need to
     23 * be setup to convert the passwords of users that were migrated from another
     24 * forum platform.
     25 *
     26 * @since 2.6.0 bbPress (r2596)
     27 */
     28function bbp_setup_admin() {
     29    $bbp = bbpress();
     30
     31    // Skip if already setup
     32    if ( empty( $bbp->admin ) ) {
     33
     34        // Require the admin class
     35        require_once $bbp->includes_dir . 'admin/classes/class-bbp-admin.php';
     36
     37        // Setup
     38        $bbp->admin = class_exists( 'BBP_Admin' )
     39            ? new BBP_Admin()
     40            : new stdClass();
     41    }
     42
     43    // Return the admin object
     44    return $bbp->admin;
     45}
     46
     47/**
     48 * Setup Converter
     49 *
     50 * This exists outside of "/includes/admin/" because the converter may need to
     51 * be setup to convert the passwords of users that were migrated from another
     52 * forum platform.
     53 *
     54 * @since 2.6.0 bbPress (r2596)
     55 */
     56function bbp_setup_converter() {
     57    $bbp_admin = bbp_setup_admin();
     58
     59    // Skip if already setup
     60    if ( empty( $bbp_admin->converter ) ) {
     61
     62        // Require the converter classes
     63        require_once $bbp_admin->admin_dir . 'classes/class-bbp-converter-base.php';
     64        require_once $bbp_admin->admin_dir . 'classes/class-bbp-converter.php';
     65
     66        // Setup
     67        $bbp_admin->converter = class_exists( 'BBP_Converter' )
     68            ? new BBP_Converter()
     69            : new stdClass();
     70    }
     71
     72    // Return the converter
     73    return $bbp_admin->converter;
     74}
    1875
    1976/**
  • trunk/src/includes/users/functions.php

    r6580 r6601  
    984984    }
    985985
    986     // Setup admin (to include converter)
    987     require_once bbpress()->includes_dir . 'admin/admin.php';
    988 
    989     // Create the admin object
    990     bbp_admin();
    991 
    992     // Convert password
    993     require_once bbpress()->admin->admin_dir . 'converter.php';
    994     require_once bbpress()->admin->admin_dir . 'converters/' . sanitize_key( $row->meta_value ) . '.php';
    995 
    996     // Create the converter
     986    // Try to convert the old password for this user
    997987    $converter = bbp_new_converter( $row->meta_value );
    998988
Note: See TracChangeset for help on using the changeset viewer.