Skip to:
Content

bbPress.org

Ticket #2510: 2510.01.diff

File 2510.01.diff, 18.3 KB (added by thebrandonallen, 11 years ago)
  • includes/admin/tools.php

    diff --git includes/admin/tools.php b/includes/admin/tools.php
    index c0c1d6b..23f4d58 100644
    function bbp_admin_repair_list() { 
    186186 *
    187187 * @since bbPress (r2613)
    188188 *
    189  * @uses bbp_get_reply_post_type() To get the reply post type
    190189 * @uses wpdb::query() To run our recount sql queries
    191190 * @uses is_wp_error() To check if the executed query returned {@link WP_Error}
     191 * @uses bbp_get_topic_post_type() To get the topic post type
     192 * @uses bbp_get_reply_post_type() To get the reply post type
     193 * @uses bbp_get_public_status_id() To get the public status id
     194 * @uses bbp_get_closed_status_id() To get the closed status id
    192195 * @return array An array of the status code and the message
    193196 */
    194197function bbp_admin_repair_topic_reply_count() {
    function bbp_admin_repair_topic_reply_count() { 
    229232 *
    230233 * @since bbPress (r2613)
    231234 *
    232  * @uses bbp_get_reply_post_type() To get the reply post type
    233235 * @uses wpdb::query() To run our recount sql queries
    234236 * @uses is_wp_error() To check if the executed query returned {@link WP_Error}
     237 * @uses bbp_get_topic_post_type() To get the topic post type
     238 * @uses bbp_get_reply_post_type() To get the reply post type
     239 * @uses bbp_get_public_status_id() To get the public status id
     240 * @uses bbp_get_closed_status_id() To get the closed status id
    235241 * @return array An array of the status code and the message
    236242 */
    237243function bbp_admin_repair_topic_voice_count() {
    function bbp_admin_repair_topic_voice_count() { 
    274280 *
    275281 * @uses wpdb::query() To run our recount sql queries
    276282 * @uses is_wp_error() To check if the executed query returned {@link WP_Error}
     283 * @uses bbp_get_reply_post_type() To get the reply post type
     284 * @uses bbp_get_trash_status_id() To get the trash status id
     285 * @uses bbp_get_spam_status_id() To get the spam status id
    277286 * @return array An array of the status code and the message
    278287 */
    279288function bbp_admin_repair_topic_hidden_reply_count() {
    function bbp_admin_repair_topic_hidden_reply_count() { 
    286295        if ( is_wp_error( $wpdb->query( $sql_delete ) ) )
    287296                return array( 1, sprintf( $statement, $result ) );
    288297
    289         $sql = "INSERT INTO `{$wpdb->postmeta}` (`post_id`, `meta_key`, `meta_value`) (SELECT `post_parent`, '_bbp_reply_count_hidden', COUNT(`post_status`) as `meta_value` FROM `{$wpdb->posts}` WHERE `post_type` = '" . bbp_get_reply_post_type() . "' AND `post_status` IN ( '" . implode( "','", array( bbp_get_trash_status_id(), bbp_get_spam_status_id() ) ) . "') GROUP BY `post_parent`);";
     298        // Post types and status
     299        $rpt = bbp_get_reply_post_type();
     300        $tps = bbp_get_trash_status_id();
     301        $sps = bbp_get_spam_status_id();
     302
     303        $sql = "INSERT INTO `{$wpdb->postmeta}` (`post_id`, `meta_key`, `meta_value`) (SELECT `post_parent`, '_bbp_reply_count_hidden', COUNT(`post_status`) as `meta_value` FROM `{$wpdb->posts}` WHERE `post_type` = '{$rpt}' AND `post_status` IN ( '{$tps}', '{$sps}' ) GROUP BY `post_parent`);";
    290304        if ( is_wp_error( $wpdb->query( $sql ) ) )
    291305                return array( 2, sprintf( $statement, $result ) );
    292306
    function bbp_admin_repair_topic_hidden_reply_count() { 
    299313 * @since bbPress (r4395)
    300314 *
    301315 * @global WPDB $wpdb
     316 * @uses bbp_get_forum_post_type() To get the forum post type
    302317 * @return If a wp_error() occurs and no converted forums are found
    303318 */
    304319function bbp_admin_repair_group_forum_relationship() {
    function bbp_admin_repair_group_forum_relationship() { 
    320335                                                                        LEFT JOIN `{$wpdb->postmeta}` AS `forummeta`
    321336                                                                                ON `forum`.`ID` = `forummeta`.`post_id`
    322337                                                                                AND `forummeta`.`meta_key` = '_bbp_old_forum_id'
    323                                                                 WHERE `forum`.`post_type` = 'forum'
     338                                                                WHERE `forum`.`post_type` = '" . bbp_get_forum_post_type() . "'
    324339                                                                GROUP BY `forum`.`ID`;" );
    325340
    326341        // Bail if forum IDs returned an error
    function bbp_admin_repair_forum_reply_count() { 
    499514 *
    500515 * @since bbPress (r3889)
    501516 *
    502  * @uses bbp_get_reply_post_type() To get the reply post type
     517 * @uses bbp_get_topic_post_type() To get the topic post type
     518 * @uses bbp_get_public_status_id() To get the public status id
    503519 * @uses wpdb::query() To run our recount sql queries
    504520 * @uses is_wp_error() To check if the executed query returned {@link WP_Error}
    505521 * @return array An array of the status code and the message
    function bbp_admin_repair_user_topic_count() { 
    545561 * @since bbPress (r2613)
    546562 *
    547563 * @uses bbp_get_reply_post_type() To get the reply post type
     564 * @uses bbp_get_public_status_id() To get the public status id
    548565 * @uses wpdb::query() To run our recount sql queries
    549566 * @uses is_wp_error() To check if the executed query returned {@link WP_Error}
    550567 * @return array An array of the status code and the message
    function bbp_admin_repair_user_reply_count() { 
    589606 *
    590607 * @since bbPress (r2613)
    591608 *
    592  * @uses bbp_get_topic_post_type() To get the topic post type
    593609 * @uses wpdb::query() To run our recount sql queries
    594610 * @uses is_wp_error() To check if the executed query returned {@link WP_Error}
     611 * @uses bbp_get_topic_post_type() To get the topic post type
     612 * @uses bbp_get_public_status_id() To get the public status id
    595613 * @return array An array of the status code and the message
    596614 */
    597615function bbp_admin_repair_user_favorites() {
    function bbp_admin_repair_user_favorites() { 
    651669 *
    652670 * @since bbPress (r2668)
    653671 *
    654  * @uses bbp_get_topic_post_type() To get the topic post type
    655672 * @uses wpdb::query() To run our recount sql queries
    656673 * @uses is_wp_error() To check if the executed query returned {@link WP_Error}
     674 * @uses bbp_get_topic_post_type() To get the topic post type
     675 * @uses bbp_get_public_status_id() To get the public status id
    657676 * @return array An array of the status code and the message
    658677 */
    659678function bbp_admin_repair_user_topic_subscriptions() {
    function bbp_admin_repair_user_topic_subscriptions() { 
    712731 *
    713732 * @since bbPress (r5155)
    714733 *
    715  * @uses bbp_get_forum_post_type() To get the topic post type
    716734 * @uses wpdb::query() To run our recount sql queries
    717735 * @uses is_wp_error() To check if the executed query returned {@link WP_Error}
     736 * @uses bbp_get_forum_post_type() To get the forum post type
     737 * @uses bbp_get_public_status_id() To get the public status id
    718738 * @return array An array of the status code and the message
    719739 */
    720740function bbp_admin_repair_user_forum_subscriptions() {
    function bbp_admin_repair_user_roles() { 
    831851 *
    832852 * @uses wpdb::query() To run our recount sql queries
    833853 * @uses is_wp_error() To check if the executed query returned {@link WP_Error}
     854 * @uses bbp_get_forum_post_type() To get the forum post type
     855 * @uses bbp_get_topic_post_type() To get the topic post type
     856 * @uses bbp_get_reply_post_type() To get the reply post type
     857 * @uses bbp_get_public_status_id() To get the public status id
    834858 * @return array An array of the status code and the message
    835859 */
    836860function bbp_admin_repair_freshness() {
    function bbp_admin_repair_freshness() { 
    842866        // First, delete everything.
    843867        if ( is_wp_error( $wpdb->query( "DELETE FROM `$wpdb->postmeta` WHERE `meta_key` IN ( '_bbp_last_reply_id', '_bbp_last_topic_id', '_bbp_last_active_id', '_bbp_last_active_time' );" ) ) )
    844868                return array( 1, sprintf( $statement, $result ) );
     869               
     870        // Post types and status
     871        $fpt = bbp_get_forum_post_type();
     872        $tpt = bbp_get_topic_post_type();
     873        $rpt = bbp_get_reply_post_type();
     874        $pps = bbp_get_public_status_id();
    845875
    846876        // Next, give all the topics with replies the ID their last reply.
    847877        if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
    848878                        ( SELECT `topic`.`ID`, '_bbp_last_reply_id', MAX( `reply`.`ID` )
    849879                        FROM `$wpdb->posts` AS `topic` INNER JOIN `$wpdb->posts` AS `reply` ON `topic`.`ID` = `reply`.`post_parent`
    850                         WHERE `reply`.`post_status` IN ( '" . bbp_get_public_status_id() . "' ) AND `topic`.`post_type` = 'topic' AND `reply`.`post_type` = 'reply'
     880                        WHERE `reply`.`post_status` = '{$pps}' AND `topic`.`post_type` = '{$tpt}' AND `reply`.`post_type` = '{$rpt}'
    851881                        GROUP BY `topic`.`ID` );" ) ) )
    852882                return array( 2, sprintf( $statement, $result ) );
    853883
    function bbp_admin_repair_freshness() { 
    856886                        ( SELECT `ID`, '_bbp_last_reply_id', 0
    857887                        FROM `$wpdb->posts` AS `topic` LEFT JOIN `$wpdb->postmeta` AS `reply`
    858888                        ON `topic`.`ID` = `reply`.`post_id` AND `reply`.`meta_key` = '_bbp_last_reply_id'
    859                         WHERE `reply`.`meta_id` IS NULL AND `topic`.`post_type` = 'topic' );" ) ) )
     889                        WHERE `reply`.`meta_id` IS NULL AND `topic`.`post_type` = '{$tpt}' );" ) ) )
    860890                return array( 3, sprintf( $statement, $result ) );
    861891
    862892        // Now we give all the forums with topics the ID their last topic.
    863893        if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
    864894                        ( SELECT `forum`.`ID`, '_bbp_last_topic_id', `topic`.`ID`
    865895                        FROM `$wpdb->posts` AS `forum` INNER JOIN `$wpdb->posts` AS `topic` ON `forum`.`ID` = `topic`.`post_parent`
    866                         WHERE `topic`.`post_status` IN ( '" . bbp_get_public_status_id() . "' ) AND `forum`.`post_type` = 'forum' AND `topic`.`post_type` = 'topic'
     896                        WHERE `topic`.`post_status` = '{$pps}' AND `forum`.`post_type` = '{$fpt}' AND `topic`.`post_type` = '{$tpt}'
    867897                        GROUP BY `forum`.`ID` );" ) ) )
    868898                return array( 4, sprintf( $statement, $result ) );
    869899
    function bbp_admin_repair_freshness() { 
    872902                        ( SELECT `ID`, '_bbp_last_topic_id', 0
    873903                        FROM `$wpdb->posts` AS `forum` LEFT JOIN `$wpdb->postmeta` AS `topic`
    874904                        ON `forum`.`ID` = `topic`.`post_id` AND `topic`.`meta_key` = '_bbp_last_topic_id'
    875                         WHERE `topic`.`meta_id` IS NULL AND `forum`.`post_type` = 'forum' );" ) ) )
     905                        WHERE `topic`.`meta_id` IS NULL AND `forum`.`post_type` = '{$fpt}' );" ) ) )
    876906                return array( 5, sprintf( $statement, $result ) );
    877907
    878908        // After that, we give all the topics with replies the ID their last reply (again, this time for a different reason).
    879909        if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
    880910                        ( SELECT `topic`.`ID`, '_bbp_last_active_id', MAX( `reply`.`ID` )
    881911                        FROM `$wpdb->posts` AS `topic` INNER JOIN `$wpdb->posts` AS `reply` ON `topic`.`ID` = `reply`.`post_parent`
    882                         WHERE `reply`.`post_status` IN ( '" . bbp_get_public_status_id() . "' ) AND `topic`.`post_type` = 'topic' AND `reply`.`post_type` = 'reply'
     912                        WHERE `reply`.`post_status` = '{$pps}' AND `topic`.`post_type` = '{$tpt}' AND `reply`.`post_type` = '{$rpt}'
    883913                        GROUP BY `topic`.`ID` );" ) ) )
    884914                return array( 6, sprintf( $statement, $result ) );
    885915
    function bbp_admin_repair_freshness() { 
    888918                        ( SELECT `ID`, '_bbp_last_active_id', `ID`
    889919                        FROM `$wpdb->posts` AS `topic` LEFT JOIN `$wpdb->postmeta` AS `reply`
    890920                        ON `topic`.`ID` = `reply`.`post_id` AND `reply`.`meta_key` = '_bbp_last_active_id'
    891                         WHERE `reply`.`meta_id` IS NULL AND `topic`.`post_type` = 'topic' );" ) ) )
     921                        WHERE `reply`.`meta_id` IS NULL AND `topic`.`post_type` = '{$tpt}' );" ) ) )
    892922                return array( 7, sprintf( $statement, $result ) );
    893923
    894924        // Give topics with replies their last update time.
    895925        if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
    896926                        ( SELECT `topic`.`ID`, '_bbp_last_active_time', MAX( `reply`.`post_date` )
    897927                        FROM `$wpdb->posts` AS `topic` INNER JOIN `$wpdb->posts` AS `reply` ON `topic`.`ID` = `reply`.`post_parent`
    898                         WHERE `reply`.`post_status` IN ( '" . bbp_get_public_status_id() . "' ) AND `topic`.`post_type` = 'topic' AND `reply`.`post_type` = 'reply'
     928                        WHERE `reply`.`post_status` = '{$pps}' AND `topic`.`post_type` = '{$tpt}' AND `reply`.`post_type` = '{$rpt}'
    899929                        GROUP BY `topic`.`ID` );" ) ) )
    900930                return array( 8, sprintf( $statement, $result ) );
    901931
    function bbp_admin_repair_freshness() { 
    904934                        ( SELECT `ID`, '_bbp_last_active_time', `post_date`
    905935                        FROM `$wpdb->posts` AS `topic` LEFT JOIN `$wpdb->postmeta` AS `reply`
    906936                        ON `topic`.`ID` = `reply`.`post_id` AND `reply`.`meta_key` = '_bbp_last_active_time'
    907                         WHERE `reply`.`meta_id` IS NULL AND `topic`.`post_type` = 'topic' );" ) ) )
     937                        WHERE `reply`.`meta_id` IS NULL AND `topic`.`post_type` = '{$tpt}' );" ) ) )
    908938                return array( 9, sprintf( $statement, $result ) );
    909939
    910940        // Forums need to know what their last active item is as well. Now it gets a bit more complex to do in the database.
    911         $forums = $wpdb->get_col( "SELECT `ID` FROM `$wpdb->posts` WHERE `post_type` = 'forum' and `post_status` != 'auto-draft';" );
     941        $forums = $wpdb->get_col( "SELECT `ID` FROM `$wpdb->posts` WHERE `post_type` = '{$fpt}' and `post_status` != 'auto-draft';" );
    912942        if ( is_wp_error( $forums ) )
    913943                return array( 10, sprintf( $statement, $result ) );
    914944
    function bbp_admin_repair_freshness() { 
    937967 *
    938968 * @uses wpdb::get_col() To run our recount sql queries
    939969 * @uses is_wp_error() To check if the executed query returned {@link WP_Error}
     970 * @uses bbp_get_forum_post_type() To get the forum post type
    940971 * @return array An array of the status code and the message
    941972 */
    942973function bbp_admin_repair_sticky() {
    function bbp_admin_repair_sticky() { 
    944975
    945976        $statement = __( 'Repairing the sticky topic to the parent forum relationships… %s', 'bbpress' );
    946977        $result    = __( 'Failed!', 'bbpress' );
    947         $forums    = $wpdb->get_col( "SELECT ID FROM `{$wpdb->posts}` WHERE `post_type` = 'forum';" );
     978        $forums    = $wpdb->get_col( "SELECT ID FROM `{$wpdb->posts}` WHERE `post_type` = '" . bbp_get_forum_post_type() . "';" );
    948979
    949980        // Bail if no forums found
    950981        if ( empty( $forums ) || is_wp_error( $forums ) )
    function bbp_admin_repair_forum_visibility() { 
    10181049 *
    10191050 * @uses wpdb::query() To run our recount sql queries
    10201051 * @uses is_wp_error() To check if the executed query returned {@link WP_Error}
     1052 * @uses bbp_get_forum_post_type() To get the forum post type
     1053 * @uses bbp_get_topic_post_type() To get the topic post type
     1054 * @uses bbp_get_reply_post_type() To get the reply post type
    10211055 * @return array An array of the status code and the message
    10221056 */
    10231057function bbp_admin_repair_forum_meta() {
    function bbp_admin_repair_forum_meta() { 
    10291063        // First, delete everything.
    10301064        if ( is_wp_error( $wpdb->query( "DELETE FROM `$wpdb->postmeta` WHERE `meta_key` = '_bbp_forum_id';" ) ) )
    10311065                return array( 1, sprintf( $statement, $result ) );
     1066               
     1067        // Post types and status
     1068        $fpt = bbp_get_forum_post_type();
     1069        $tpt = bbp_get_topic_post_type();
     1070        $rpt = bbp_get_reply_post_type();
    10321071
    10331072        // Next, give all the topics with replies the ID their last reply.
    10341073        if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
    10351074                        ( SELECT `forum`.`ID`, '_bbp_forum_id', `forum`.`post_parent`
    10361075                        FROM `$wpdb->posts`
    10371076                                AS `forum`
    1038                         WHERE `forum`.`post_type` = 'forum'
     1077                        WHERE `forum`.`post_type` = '{$fpt}'
    10391078                        GROUP BY `forum`.`ID` );" ) ) )
    10401079                return array( 2, sprintf( $statement, $result ) );
    10411080
    function bbp_admin_repair_forum_meta() { 
    10441083                        ( SELECT `topic`.`ID`, '_bbp_forum_id', `topic`.`post_parent`
    10451084                        FROM `$wpdb->posts`
    10461085                                AS `topic`
    1047                         WHERE `topic`.`post_type` = 'topic'
     1086                        WHERE `topic`.`post_type` = '{$tpt}'
    10481087                        GROUP BY `topic`.`ID` );" ) ) )
    10491088                return array( 3, sprintf( $statement, $result ) );
    10501089
    function bbp_admin_repair_forum_meta() { 
    10561095                        INNER JOIN `$wpdb->posts`
    10571096                                AS `topic`
    10581097                                ON `reply`.`post_parent` = `topic`.`ID`
    1059                         WHERE `topic`.`post_type` = 'topic'
    1060                                 AND `reply`.`post_type` = 'reply'
     1098                        WHERE `topic`.`post_type` = '{$tpt}'
     1099                                AND `reply`.`post_type` = '{$rpt}'
    10611100                        GROUP BY `reply`.`ID` );" ) ) )
    10621101                return array( 4, sprintf( $statement, $result ) );
    10631102
    function bbp_admin_repair_forum_meta() { 
    10721111 *
    10731112 * @uses wpdb::query() To run our recount sql queries
    10741113 * @uses is_wp_error() To check if the executed query returned {@link WP_Error}
     1114 * @uses bbp_get_topic_post_type() To get the topic post type
     1115 * @uses bbp_get_reply_post_type() To get the reply post type
    10751116 * @return array An array of the status code and the message
    10761117 */
    10771118function bbp_admin_repair_topic_meta() {
    function bbp_admin_repair_topic_meta() { 
    10841125        if ( is_wp_error( $wpdb->query( "DELETE FROM `$wpdb->postmeta` WHERE `meta_key` = '_bbp_topic_id';" ) ) )
    10851126                return array( 1, sprintf( $statement, $result ) );
    10861127
     1128        // Post types and status
     1129        $tpt = bbp_get_topic_post_type();
     1130        $rpt = bbp_get_reply_post_type();
     1131
    10871132        // Next, give all the topics with replies the ID their last reply.
    10881133        if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
    10891134                        ( SELECT `topic`.`ID`, '_bbp_topic_id', `topic`.`ID`
    10901135                        FROM `$wpdb->posts`
    10911136                                AS `topic`
    1092                         WHERE `topic`.`post_type` = 'topic'
     1137                        WHERE `topic`.`post_type` = '{$tpt}'
    10931138                        GROUP BY `topic`.`ID` );" ) ) )
    10941139                return array( 3, sprintf( $statement, $result ) );
    10951140
    function bbp_admin_repair_topic_meta() { 
    11011146                        INNER JOIN `$wpdb->posts`
    11021147                                AS `topic`
    11031148                                ON `reply`.`post_parent` = `topic`.`ID`
    1104                         WHERE `topic`.`post_type` = 'topic'
    1105                                 AND `reply`.`post_type` = 'reply'
     1149                        WHERE `topic`.`post_type` = '{$tpt}'
     1150                                AND `reply`.`post_type` = '{$rpt}'
    11061151                        GROUP BY `reply`.`ID` );" ) ) )
    11071152                return array( 4, sprintf( $statement, $result ) );
    11081153
    function bbp_admin_reset() { 
    11901235 *
    11911236 * @uses check_admin_referer() To verify the nonce and the referer
    11921237 * @uses wp_cache_flush() To flush the cache
     1238 * @uses bbp_get_forum_post_type() To get the forum post type
     1239 * @uses bbp_get_topic_post_type() To get the topic post type
     1240 * @uses bbp_get_reply_post_type() To get the reply post type
    11931241 */
    11941242function bbp_admin_reset_handler() {
    11951243
    function bbp_admin_reset_handler() { 
    12151263
    12161264        /** Posts *****************************************************************/
    12171265
     1266        // Post types and status
     1267        $fpt = bbp_get_forum_post_type();
     1268        $tpt = bbp_get_topic_post_type();
     1269        $rpt = bbp_get_reply_post_type();
     1270
    12181271        $statement  = __( 'Deleting Posts… %s', 'bbpress' );
    1219         $sql_posts  = $wpdb->get_results( "SELECT `ID` FROM `{$wpdb->posts}` WHERE `post_type` IN ('forum', 'topic', 'reply')", OBJECT_K );
    1220         $sql_delete = "DELETE FROM `{$wpdb->posts}` WHERE `post_type` IN ('forum', 'topic', 'reply')";
     1272        $sql_posts  = $wpdb->get_results( "SELECT `ID` FROM `{$wpdb->posts}` WHERE `post_type` IN ('{$fpt}', '{$tpt}', '{$rpt}')", OBJECT_K );
     1273        $sql_delete = "DELETE FROM `{$wpdb->posts}` WHERE `post_type` IN ('{$fpt}', '{$tpt}', '{$rpt}')";
    12211274        $result     = is_wp_error( $wpdb->query( $sql_delete ) ) ? $failed : $success;
    12221275        $messages[] = sprintf( $statement, $result );
    12231276