Skip to:
Content

bbPress.org


Ignore:
Timestamp:
07/04/2012 02:17:31 AM (14 years ago)
Author:
johnjamesjacoby
Message:

Admin:

  • Add nonce checks and additional sanity checks to converter, to prevent it being loaded or accessed outside of the converter screen.
  • Use 'load-' actions to conditionally load admin components on their respective screens.
  • Remove post_type checks that are no longer needed as a result of conditionally loading each component.
  • Use get_current_screen() to fill in any extra post_type checks.
  • Sweep through converter, make output more clean, add output method, and store last query in the DB in case of failure.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/plugin/bbp-admin/bbp-converter.php

    r4007 r4053  
    2626     */
    2727    public function __construct() {
     28
     29        // Bail if request is not correct
     30        switch ( strtoupper( $_SERVER['REQUEST_METHOD'] ) ) {
     31
     32            // Converter is converting
     33            case 'POST' :
     34                if ( ( empty( $_POST['action'] ) || ( 'bbconverter_process' !=  $_POST['action'] ) ) )
     35                    return;
     36
     37                break;
     38
     39            // Some other admin page
     40            case 'GET'  :
     41                if ( ( empty( $_GET['page'] ) || ( 'bbp-converter' !=  $_GET['page'] ) ) )
     42                    return;
     43
     44                break;
     45        }
     46
     47        // Proceed with the actions
    2848        $this->setup_actions();
    2949    }
     
    6181
    6282        // Add the main section
    63         add_settings_section( 'bbpress_converter_main',     __( 'Main Settings',    'bbpress' ),  'bbp_converter_setting_callback_main_section', 'bbpress_converter' );
     83        add_settings_section( 'bbpress_converter_main',     __( 'Database Settings', 'bbpress' ),  'bbp_converter_setting_callback_main_section', 'bbpress_converter' );
    6484
    6585        // System Select
     
    140160                border-color: #E6DB55;
    141161                font-family: monospace;
     162                font-weight: bold;
    142163            }
    143164
     
    145166                margin: 0.5em 0;
    146167                padding: 2px;
    147             }
    148 
    149             div.bbp-converter-updated p strong.loading {
    150                 padding: 2px 20px 2px 0;
     168                float: left;
     169                clear: left;
     170            }
     171
     172            div.bbp-converter-updated p.loading {
     173                padding: 2px 20px 2px 2px;
    151174                background-image: url('<?php echo admin_url(); ?>images/wpspin_light.gif');
    152175                background-repeat: no-repeat;
     
    186209
    187210                values['action'] = 'bbconverter_process';
     211                values['_ajax_nonce'] = '<?php echo  wp_create_nonce( 'bbp_converter_process' ); ?>';
     212
    188213                return values;
    189214            }
     
    195220                    jQuery('#bbp-converter-stop').show();
    196221                    jQuery('#bbp-converter-progress').show();
    197                     bbconverter_log( "Starting Conversion..." );
     222                    bbconverter_log( '<p class="loading"><?php _e( 'Starting Conversion', 'bbpress' ); ?></p>' );
    198223                    bbconverter_run();
    199224                }
     
    209234
    210235            function bbconverter_stop() {
     236                jQuery('#bbp-converter-start').show();
     237                jQuery('#bbp-converter-stop').hide();
     238                jQuery('#bbp-converter-progress').hide();
     239                jQuery('#bbp-converter-message p').removeClass( 'loading' );
    211240                bbconverter_is_running = false;
    212                 jQuery('#bbp-converter-message strong').removeClass( 'loading' );
     241                clearTimeout( bbconverter_run_timer );
    213242            }
    214243
     
    216245                bbconverter_log(response);
    217246
    218                 if ( response == 'Conversion Complete' || response.indexOf('error') > -1 ) {
    219                     bbconverter_log('<b>Repair any missing information: <a href="<?php echo admin_url(); ?>tools.php?page=bbp-repair">Continue</a></b>');
    220                     jQuery('#bbp-converter-start').show();
    221                     jQuery('#bbp-converter-stop').hide();
    222                     jQuery('#bbp-converter-progress').hide();
     247                if ( response == '<p class="loading"><?php _e( 'Conversion Complete', 'bbpress' ); ?></p>' || response.indexOf('error') > -1 ) {
     248                    bbconverter_log('<p>Repair any missing information: <a href="<?php echo admin_url(); ?>tools.php?page=bbp-repair">Continue</a></p>');
    223249                    bbconverter_stop();
    224                     clearTimeout( bbconverter_run_timer );
    225250                } else if( bbconverter_is_running ) { // keep going
    226251                    jQuery('#bbp-converter-progress').show();
     
    228253                    bbconverter_run_timer = setTimeout( 'bbconverter_run()', bbconverter_delay_time );
    229254                } else {
    230                     jQuery('#bbp-converter-start').show();
    231                     jQuery('#bbp-converter-stop').hide();
    232                     jQuery('#bbp-converter-progress').hide();
    233                     clearTimeout( bbconverter_run_timer );
     255                    bbconverter_stop();
    234256                }
    235257            }
     
    240262                }
    241263                if ( text ) {
    242                     jQuery('#bbp-converter-message strong').removeClass( 'loading' );
    243                     jQuery('#bbp-converter-message').prepend('<p><strong class="loading">' + text + '</strong></p>');
     264                    jQuery('#bbp-converter-message p').removeClass( 'loading' );
     265                    jQuery('#bbp-converter-message').prepend( text );
    244266                }
    245267            }
     
    251273
    252274    /**
     275     * Wrap the converter output in paragraph tags, so styling can be applied
     276     *
     277     * @since bbPress (r4052)
     278     *
     279     * @param string $output
     280     */
     281    private static function converter_output( $output = '' ) {
     282
     283        // Get the last query
     284        $before = '<p class="loading">';
     285        $after  = '</p>';
     286        $query  = get_option( '_bbp_converter_query' );
     287
     288        if ( ! empty( $query ) )
     289            $before = '<p class="loading" title="' . esc_attr( $query ) . '">';
     290
     291        echo $before . $output . $after;
     292    }
     293
     294    /**
    253295     * Callback processor
    254296     *
     
    256298     */
    257299    public function process_callback() {
     300
     301        // Verify intent
     302        check_ajax_referer( 'bbp_converter_process' );
    258303
    259304        if ( ! ini_get( 'safe_mode' ) ) {
     
    294339                        $this->sync_table();
    295340                        if ( empty( $start ) ) {
    296                             _e( 'No data to clean', 'bbpress' );
     341                            $this->converter_output( __( 'No data to clean', 'bbpress' ) );
    297342                        }
    298343                    } else {
    299344                        update_option( '_bbp_converter_start', $max + 1 );
    300                         printf( __( 'Deleting previously converted data (%1$s - %2$s)', 'bbpress' ), $min, $max );
     345                        $this->converter_output( sprintf( __( 'Deleting previously converted data (%1$s - %2$s)', 'bbpress' ), $min, $max ) );
    301346                    }
    302347                } else {
     
    314359                        update_option( '_bbp_converter_start', 0         );
    315360                        if ( empty( $start ) ) {
    316                             _e( 'No users to convert', 'bbpress' );
     361                            $this->converter_output( __( 'No users to convert', 'bbpress' ) );
    317362                        }
    318363                    } else {
    319364                        update_option( '_bbp_converter_start', $max + 1 );
    320                         printf( __( 'Converting users (%1$s - %2$s)', 'bbpress' ), $min, $max );
     365                        $this->converter_output( sprintf(  __( 'Converting users (%1$s - %2$s)', 'bbpress' ), $min, $max ) );
    321366                    }
    322367                } else {
     
    334379                        update_option( '_bbp_converter_start', 0         );
    335380                        if ( empty( $start ) ) {
    336                             _e( 'No passwords to clear', 'bbpress' );
     381                            $this->converter_output( __( 'No passwords to clear', 'bbpress' ) );
    337382                        }
    338383                    } else {
    339384                        update_option( '_bbp_converter_start', $max + 1 );
    340                         printf( __( 'Delete users wordpress default passwords (%1$s - %2$s)', 'bbpress' ), $min, $max );
     385                        $this->converter_output( sprintf( __( 'Delete users wordpress default passwords (%1$s - %2$s)', 'bbpress' ), $min, $max ) );
    341386                    }
    342387                } else {
     
    353398                    update_option( '_bbp_converter_start', 0         );
    354399                    if ( empty( $start ) ) {
    355                         _e( 'No forums to convert', 'bbpress' );
     400                        $this->converter_output( __( 'No forums to convert', 'bbpress' ) );
    356401                    }
    357402                } else {
    358403                    update_option( '_bbp_converter_start', $max + 1 );
    359                     printf( __( 'Converting forums (%1$s - %2$s)', 'bbpress' ), $min, $max );
     404                    $this->converter_output( sprintf( __( 'Converting forums (%1$s - %2$s)', 'bbpress' ), $min, $max ) );
    360405                }
    361406
     
    364409            // STEP 5. Convert forum parents.
    365410            case 5 :
    366 
    367411                if ( $converter->convert_forum_parents( $start ) ) {
    368412                    update_option( '_bbp_converter_step',  $step + 1 );
    369413                    update_option( '_bbp_converter_start', 0         );
    370414                    if ( empty( $start ) ) {
    371                         _e( 'No forum parents to convert', 'bbpress' );
     415                        $this->converter_output( __( 'No forum parents to convert', 'bbpress' ) );
    372416                    }
    373417                } else {
    374418                    update_option( '_bbp_converter_start', $max + 1 );
    375                     printf( __( 'Calculating forum hierarchy (%1$s - %2$s)', 'bbpress' ), $min, $max );
     419                    $this->converter_output( sprintf( __( 'Calculating forum hierarchy (%1$s - %2$s)', 'bbpress' ), $min, $max ) );
    376420                }
    377421
     
    380424            // STEP 6. Convert topics.
    381425            case 6 :
    382 
    383426                if ( $converter->convert_topics( $start ) ) {
    384427                    update_option( '_bbp_converter_step',  $step + 1 );
    385428                    update_option( '_bbp_converter_start', 0         );
    386429                    if ( empty( $start ) ) {
    387                         _e( 'No topics to convert', 'bbpress' );
     430                        $this->converter_output( __( 'No topics to convert', 'bbpress' ) );
    388431                    }
    389432                } else {
    390433                    update_option( '_bbp_converter_start', $max + 1 );
    391                     printf( __( 'Converting topics (%1$s - %2$s)', 'bbpress' ), $min, $max );
     434                    $this->converter_output( sprintf( __( 'Converting topics (%1$s - %2$s)', 'bbpress' ), $min, $max ) );
    392435                }
    393436
     
    396439            // STEP 7. Convert tags.
    397440            case 7 :
    398 
    399441                if ( $converter->convert_tags( $start ) ) {
    400442                    update_option( '_bbp_converter_step',  $step + 1 );
    401443                    update_option( '_bbp_converter_start', 0         );
    402444                    if ( empty( $start ) ) {
    403                         _e( 'No tags to convert', 'bbpress' );
     445                        $this->converter_output( __( 'No tags to convert', 'bbpress' ) );
    404446                    }
    405447                } else {
    406448                    update_option( '_bbp_converter_start', $max + 1 );
    407                     printf( __( 'Converting topic tags (%1$s - %2$s)', 'bbpress' ), $min, $max );
     449                    $this->converter_output( sprintf( __( 'Converting topic tags (%1$s - %2$s)', 'bbpress' ), $min, $max ) );
    408450                }
    409451
     
    416458                    update_option( '_bbp_converter_start', 0         );
    417459                    if ( empty( $start ) ) {
    418                         _e( 'No replies to convert', 'bbpress' );
     460                        $this->converter_output( __( 'No replies to convert', 'bbpress' ) );
    419461                    }
    420462                } else {
    421463                    update_option( '_bbp_converter_start', $max + 1 );
    422                     printf( __( 'Converting replies (%1$s - %2$s)', 'bbpress' ), $min, $max );
     464                    $this->converter_output( sprintf( __( 'Converting replies (%1$s - %2$s)', 'bbpress' ), $min, $max ) );
    423465                }
    424466
     
    426468
    427469            default :
    428                 delete_option( '_bbp_converter_step' );
     470                delete_option( '_bbp_converter_step'  );
    429471                delete_option( '_bbp_converter_start' );
    430 
    431                 _e( 'Conversion Complete', 'bbpress' );
     472                delete_option( '_bbp_converter_query' );
     473
     474                $this->converter_output( __( 'Conversion Complete', 'bbpress' ) );
    432475
    433476                break;
    434 
    435477        }
    436478    }
     
    813855            $forum_array = $this->opdb->get_results( $forum_query, ARRAY_A );
    814856
    815             // Output the query, for better debugging
    816             printf( __( '<span title="%s">View Query</span>%s', 'bbpress' ), esc_attr( $forum_query ), '<br />' );
     857            // Set this query as the last one ran
     858            update_option( '_bbp_converter_query', $forum_query );
    817859
    818860            // Query returned some results
     
    9581000
    9591001    public function convert_forum_parents( $start ) {
     1002
    9601003        $has_update = false;
    9611004
    962         if ( !empty( $this->sync_table ) ) {
    963             $forum_array = $this->wpdb->get_results( 'SELECT value_id, meta_value FROM ' . $this->sync_table_name . ' WHERE meta_key = "_bbp_forum_parent_id" AND meta_value > 0 LIMIT ' . $start . ', ' . $this->max_rows );
    964         } else {
    965             $forum_array = $this->wpdb->get_results( 'SELECT post_id AS value_id, meta_value FROM ' . $this->wpdb->postmeta . ' WHERE meta_key = "_bbp_forum_parent_id" AND meta_value > 0 LIMIT ' . $start . ', ' . $this->max_rows );
    966         }
     1005        if ( !empty( $this->sync_table ) )
     1006            $query = 'SELECT value_id, meta_value FROM ' . $this->sync_table_name . ' WHERE meta_key = "_bbp_forum_parent_id" AND meta_value > 0 LIMIT ' . $start . ', ' . $this->max_rows;
     1007        else
     1008            $query = 'SELECT post_id AS value_id, meta_value FROM ' . $this->wpdb->postmeta . ' WHERE meta_key = "_bbp_forum_parent_id" AND meta_value > 0 LIMIT ' . $start . ', ' . $this->max_rows;
     1009
     1010        update_option( '_bbp_converter_query', $query );
     1011
     1012        $forum_array = $this->wpdb->get_results( $query );
    9671013
    9681014        foreach ( (array) $forum_array as $row ) {
     
    9791025     */
    9801026    public function clean( $start ) {
     1027
    9811028        $start      = 0;
    9821029        $has_delete = false;
     
    9841031        /** Delete bbconverter topics/forums/posts ****************************/
    9851032
    986         if ( true === $this->sync_table ) {
    987             $bbconverter = $this->wpdb->get_results( 'SELECT value_id FROM ' . $this->sync_table_name . ' INNER JOIN ' . $this->wpdb->posts . ' ON(value_id = ID) WHERE meta_key LIKE "_bbp_%" AND value_type = "post" GROUP BY value_id ORDER BY value_id DESC LIMIT ' . $this->max_rows, ARRAY_A );
    988         } else {
    989             $bbconverter = $this->wpdb->get_results( '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, ARRAY_A );
    990         }
    991 
    992         if ( !empty( $bbconverter ) ) {
    993             foreach ( (array) $bbconverter as $value ) {
     1033        if ( true === $this->sync_table )
     1034            $query = 'SELECT value_id FROM ' . $this->sync_table_name . ' INNER JOIN ' . $this->wpdb->posts . ' ON(value_id = ID) WHERE meta_key LIKE "_bbp_%" AND value_type = "post" GROUP BY value_id ORDER BY value_id DESC LIMIT ' . $this->max_rows;
     1035        else
     1036            $query = 'SELECT post_id AS value_id FROM ' . $this->wpdb->postmeta . ' WHERE meta_key LIKE "_bbp_%" GROUP BY post_id ORDER BY post_id DESC LIMIT ' . $this->max_rows;
     1037
     1038        update_option( '_bbp_converter_query', $query );
     1039
     1040        $posts = $this->wpdb->get_results( $query, ARRAY_A );
     1041
     1042        if ( !empty( $posts ) ) {
     1043            foreach ( (array) $posts as $value ) {
    9941044                wp_delete_post( $value['value_id'], true );
    9951045            }
     
    9991049        /** Delete bbconverter users ******************************************/
    10001050
    1001         if ( true === $this->sync_table ) {
    1002             $bbconverter = $this->wpdb->get_results( 'SELECT value_id FROM ' . $this->sync_table_name . ' INNER JOIN ' . $this->wpdb->users . ' ON(value_id = ID) WHERE meta_key = "_bbp_user_id" AND value_type = "user" LIMIT ' . $this->max_rows, ARRAY_A );
    1003         } else {
    1004             $bbconverter = $this->wpdb->get_results( 'SELECT user_id AS value_id FROM ' . $this->wpdb->usermeta . ' WHERE meta_key = "_bbp_user_id" LIMIT ' . $this->max_rows, ARRAY_A );
    1005         }
    1006 
    1007         if ( !empty( $bbconverter ) ) {
    1008             foreach ( $bbconverter as $value ) {
     1051        if ( true === $this->sync_table )
     1052            $query = 'SELECT value_id FROM ' . $this->sync_table_name . ' INNER JOIN ' . $this->wpdb->users . ' ON(value_id = ID) WHERE meta_key = "_bbp_user_id" AND value_type = "user" LIMIT ' . $this->max_rows;
     1053        else
     1054            $query = 'SELECT user_id AS value_id FROM ' . $this->wpdb->usermeta . ' WHERE meta_key = "_bbp_user_id" LIMIT ' . $this->max_rows;
     1055
     1056        update_option( '_bbp_converter_query', $query );
     1057
     1058        $users = $this->wpdb->get_results( $query, ARRAY_A );
     1059
     1060        if ( !empty( $users ) ) {
     1061            foreach ( $users as $value ) {
    10091062                wp_delete_user( $value['value_id'] );
    10101063            }
     
    10121065        }
    10131066
     1067        unset( $posts );
     1068        unset( $users );
     1069
    10141070        return ! $has_delete;
    10151071    }
     
    10211077     */
    10221078    public function clean_passwords( $start ) {
     1079
    10231080        $has_delete = false;
    10241081
    10251082        /** Delete bbconverter passwords **************************************/
    10261083
    1027         $bbconverter = $this->wpdb->get_results( 'SELECT user_id, meta_value FROM ' . $this->wpdb->usermeta . ' WHERE meta_key = "_bbp_password" LIMIT ' . $start . ', ' . $this->max_rows, ARRAY_A );
     1084        $query       = 'SELECT user_id, meta_value FROM ' . $this->wpdb->usermeta . ' WHERE meta_key = "_bbp_password" LIMIT ' . $start . ', ' . $this->max_rows;
     1085        update_option( '_bbp_converter_query', $query );
     1086
     1087        $bbconverter = $this->wpdb->get_results( $query, ARRAY_A );
     1088
    10281089        if ( !empty( $bbconverter ) ) {
    10291090
     
    10381099            $has_delete = true;
    10391100        }
     1101
    10401102        return ! $has_delete;
    10411103    }
Note: See TracChangeset for help on using the changeset viewer.