Skip to:
Content

bbPress.org


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
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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;
Note: See TracChangeset for help on using the changeset viewer.