Skip to:
Content

bbPress.org


Ignore:
Timestamp:
06/10/2017 11:19:08 PM (9 years ago)
Author:
johnjamesjacoby
Message:

Tools: Audit repair & upgrade tools.

  • Use escaped gettext variant functions where applicable
  • Use Query/Prepared/Results paradigm for direct database queries for easier step-through debugging
  • Fix a few small bugs in the engagements upgrade functions (none that affect 2.5 or beta/RC users)
  • General code formatting improvements and typo fixes
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/admin/tools/repair.php

    r6497 r6523  
    22
    33/**
    4  * bbPress Admin Repairs Page
     4 * bbPress Admin Repair Page
    55 *
    66 * @package bbPress
     
    3232    <div class="wrap">
    3333        <h1><?php esc_html_e( 'Forum Tools', 'bbpress' ); ?></h1>
    34         <h2 class="nav-tab-wrapper"><?php bbp_tools_admin_tabs( __( 'Repair Forums', 'bbpress' ) ); ?></h2>
     34        <h2 class="nav-tab-wrapper"><?php bbp_tools_admin_tabs( esc_html__( 'Repair Forums', 'bbpress' ) ); ?></h2>
    3535
    3636        <p><?php esc_html_e( 'bbPress keeps track of relationships between forums, topics, replies, topic-tags, favorites, subscriptions, and users. Occasionally these relationships become out of sync, most often after an import or migration. Use the tools below to manually recalculate these relationships.', 'bbpress' ); ?></p>
     
    175175    // Define variables
    176176    $bbp_db    = bbp_db();
    177     $statement = __( 'Counting the number of replies in each topic&hellip; %s', 'bbpress' );
    178     $result    = __( 'Failed!', 'bbpress' );
     177    $statement = esc_html__( 'Counting the number of replies in each topic&hellip; %s', 'bbpress' );
     178    $result    = esc_html__( 'Failed!', 'bbpress' );
    179179
    180180    // Post types and status
     
    210210    }
    211211
    212     return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
     212    return array( 0, sprintf( $statement, esc_html__( 'Complete!', 'bbpress' ) ) );
    213213}
    214214
     
    230230    // Define variables
    231231    $bbp_db    = bbp_db();
    232     $statement = __( 'Counting the number of voices in each topic&hellip; %s', 'bbpress' );
    233     $result    = __( 'Failed!', 'bbpress' );
     232    $statement = esc_html__( 'Counting the number of voices in each topic&hellip; %s', 'bbpress' );
     233    $result    = esc_html__( 'Failed!', 'bbpress' );
    234234
    235235    $sql_delete = "DELETE FROM {$bbp_db->postmeta} WHERE meta_key IN ('_bbp_voice_count', '_bbp_engagement')";
     
    268268    }
    269269
    270     return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
     270    return array( 0, sprintf( $statement, esc_html__( 'Complete!', 'bbpress' ) ) );
    271271}
    272272
     
    288288    // Define variables
    289289    $bbp_db    = bbp_db();
    290     $statement = __( 'Counting the number of pending, spammed, and trashed replies in each topic&hellip; %s', 'bbpress' );
    291     $result    = __( 'Failed!', 'bbpress' );
     290    $statement = esc_html__( 'Counting the number of pending, spammed, and trashed replies in each topic&hellip; %s', 'bbpress' );
     291    $result    = esc_html__( 'Failed!', 'bbpress' );
    292292
    293293    $sql_delete = "DELETE FROM `{$bbp_db->postmeta}` WHERE `meta_key` = '_bbp_reply_count_hidden'";
     
    307307    }
    308308
    309     return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
     309    return array( 0, sprintf( $statement, esc_html__( 'Complete!', 'bbpress' ) ) );
    310310}
    311311
     
    326326    // Define variables
    327327    $bbp_db    = bbp_db();
    328     $statement = __( 'Counting the number of topics in each forum&hellip; %s', 'bbpress' );
    329     $result    = __( 'Failed!', 'bbpress' );
     328    $statement = esc_html__( 'Counting the number of topics in each forum&hellip; %s', 'bbpress' );
     329    $result    = esc_html__( 'Failed!', 'bbpress' );
    330330
    331331    $sql_delete = "DELETE FROM {$bbp_db->postmeta} WHERE meta_key IN ( '_bbp_topic_count', '_bbp_total_topic_count', '_bbp_topic_count_hidden' )";
     
    344344    }
    345345
    346     return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
     346    return array( 0, sprintf( $statement, esc_html__( 'Complete!', 'bbpress' ) ) );
    347347}
    348348
     
    364364
    365365    // Define variables
    366     $statement = __( 'Counting the number of topics in each topic-tag&hellip; %s', 'bbpress' );
    367     $result    = __( 'Failed!', 'bbpress' );
     366    $statement = esc_html__( 'Counting the number of topics in each topic-tag&hellip; %s', 'bbpress' );
     367    $result    = esc_html__( 'Failed!', 'bbpress' );
    368368    $tax_id    = bbp_get_topic_tag_tax_id();
    369369    $terms     = get_terms( $tax_id, array( 'hide_empty' => false ) );
     
    395395    clean_term_cache( $ints, '', false );
    396396
    397     return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
     397    return array( 0, sprintf( $statement, esc_html__( 'Complete!', 'bbpress' ) ) );
    398398}
    399399
     
    414414    // Define variables
    415415    $bbp_db    = bbp_db();
    416     $statement = __( 'Counting the number of replies in each forum&hellip; %s', 'bbpress' );
    417     $result    = __( 'Failed!', 'bbpress' );
     416    $statement = esc_html__( 'Counting the number of replies in each forum&hellip; %s', 'bbpress' );
     417    $result    = esc_html__( 'Failed!', 'bbpress' );
    418418
    419419    // Post type
     
    441441    }
    442442
    443     return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
     443    return array( 0, sprintf( $statement, esc_html__( 'Complete!', 'bbpress' ) ) );
    444444}
    445445
     
    459459    // Define variables
    460460    $bbp_db      = bbp_db();
    461     $statement   = __( 'Counting the number of topics each user has created&hellip; %s', 'bbpress' );
    462     $result      = __( 'Failed!', 'bbpress' );
     461    $statement   = esc_html__( 'Counting the number of topics each user has created&hellip; %s', 'bbpress' );
     462    $result      = esc_html__( 'Failed!', 'bbpress' );
    463463
    464464    $sql_select  = "SELECT `post_author`, COUNT(DISTINCT `ID`) as `_count` FROM `{$bbp_db->posts}` WHERE `post_type` = '" . bbp_get_topic_post_type() . "' AND `post_status` = '" . bbp_get_public_status_id() . "' GROUP BY `post_author`";
     
    493493    }
    494494
    495     return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
     495    return array( 0, sprintf( $statement, esc_html__( 'Complete!', 'bbpress' ) ) );
    496496}
    497497
     
    511511    // Define variables
    512512    $bbp_db    = bbp_db();
    513     $statement   = __( 'Counting the number of topics to which each user has replied&hellip; %s', 'bbpress' );
    514     $result      = __( 'Failed!', 'bbpress' );
     513    $statement   = esc_html__( 'Counting the number of topics to which each user has replied&hellip; %s', 'bbpress' );
     514    $result      = esc_html__( 'Failed!', 'bbpress' );
    515515
    516516    $sql_select  = "SELECT `post_author`, COUNT(DISTINCT `ID`) as `_count` FROM `{$bbp_db->posts}` WHERE `post_type` = '" . bbp_get_reply_post_type() . "' AND `post_status` = '" . bbp_get_public_status_id() . "' GROUP BY `post_author`";
     
    545545    }
    546546
    547     return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
     547    return array( 0, sprintf( $statement, esc_html__( 'Complete!', 'bbpress' ) ) );
    548548}
    549549
     
    563563    // Define variables
    564564    $bbp_db    = bbp_db();
    565     $statement = __( 'Removing unpublished topics from user favorites&hellip; %s', 'bbpress' );
    566     $result    = __( 'Failed!', 'bbpress' );
     565    $statement = esc_html__( 'Removing unpublished topics from user favorites&hellip; %s', 'bbpress' );
     566    $result    = esc_html__( 'Failed!', 'bbpress' );
    567567
    568568    // Query for users with favorites
     
    599599
    600600    if ( !count( $values ) ) {
    601         $result = __( 'Nothing to remove!', 'bbpress' );
     601        $result = esc_html__( 'Nothing to remove!', 'bbpress' );
    602602        return array( 0, sprintf( $statement, $result ) );
    603603    }
     
    616616    }
    617617
    618     return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
     618    return array( 0, sprintf( $statement, esc_html__( 'Complete!', 'bbpress' ) ) );
    619619}
    620620
     
    634634    // Define variables
    635635    $bbp_db    = bbp_db();
    636     $statement = __( 'Removing trashed topics from user subscriptions&hellip; %s', 'bbpress' );
    637     $result    = __( 'Failed!', 'bbpress' );
     636    $statement = esc_html__( 'Removing trashed topics from user subscriptions&hellip; %s', 'bbpress' );
     637    $result    = esc_html__( 'Failed!', 'bbpress' );
    638638
    639639    $key       = $bbp_db->prefix . '_bbp_subscriptions';
     
    668668
    669669    if ( !count( $values ) ) {
    670         $result = __( 'Nothing to remove!', 'bbpress' );
     670        $result = esc_html__( 'Nothing to remove!', 'bbpress' );
    671671        return array( 0, sprintf( $statement, $result ) );
    672672    }
     
    685685    }
    686686
    687     return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
     687    return array( 0, sprintf( $statement, esc_html__( 'Complete!', 'bbpress' ) ) );
    688688}
    689689
     
    703703    // Define variables
    704704    $bbp_db    = bbp_db();
    705     $statement = __( 'Removing trashed forums from user subscriptions&hellip; %s', 'bbpress' );
    706     $result    = __( 'Failed!', 'bbpress' );
     705    $statement = esc_html__( 'Removing trashed forums from user subscriptions&hellip; %s', 'bbpress' );
     706    $result    = esc_html__( 'Failed!', 'bbpress' );
    707707
    708708    $key       = $bbp_db->prefix . '_bbp_forum_subscriptions';
     
    737737
    738738    if ( !count( $values ) ) {
    739         $result = __( 'Nothing to remove!', 'bbpress' );
     739        $result = esc_html__( 'Nothing to remove!', 'bbpress' );
    740740        return array( 0, sprintf( $statement, $result ) );
    741741    }
     
    754754    }
    755755
    756     return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
     756    return array( 0, sprintf( $statement, esc_html__( 'Complete!', 'bbpress' ) ) );
    757757}
    758758
     
    772772function bbp_admin_repair_user_roles() {
    773773
    774     $statement    = __( 'Remapping forum role for each user on this site&hellip; %s', 'bbpress' );
     774    $statement    = esc_html__( 'Remapping forum role for each user on this site&hellip; %s', 'bbpress' );
    775775    $changed      = 0;
    776776    $role_map     = bbp_get_user_role_map();
     
    779779    // Bail if no role map exists
    780780    if ( empty( $role_map ) ) {
    781         return array( 1, sprintf( $statement, __( 'Failed!', 'bbpress' ) ) );
     781        return array( 1, sprintf( $statement, esc_html__( 'Failed!', 'bbpress' ) ) );
    782782    }
    783783
     
    811811    }
    812812
    813     $result = sprintf( __( 'Complete! %s users updated.', 'bbpress' ), bbp_number_format( $changed ) );
     813    $result = sprintf( esc_html__( 'Complete! %s users updated.', 'bbpress' ), bbp_number_format( $changed ) );
    814814
    815815    return array( 0, sprintf( $statement, $result ) );
     
    817817
    818818/**
    819  * Recaches the last post in every topic and forum
     819 * Repair the last post in every topic and forum
    820820 *
    821821 * @since 2.0.0 bbPress (r3040)
     
    827827 * @uses bbp_get_reply_post_type() To get the reply post type
    828828 * @uses bbp_get_public_status_id() To get the public status id
    829  * @uses bbp_is_forum_category() To check if the forum is a ategory
     829 * @uses bbp_is_forum_category() To check if the forum is a category
    830830 * @uses bbp_update_forum() To update the forums forum id
    831831 * @return array An array of the status code and the message
     
    835835    // Define variables
    836836    $bbp_db    = bbp_db();
    837     $statement = __( 'Recomputing latest post in every topic and forum&hellip; %s', 'bbpress' );
    838     $result    = __( 'Failed!', 'bbpress' );
     837    $statement = esc_html__( 'Recomputing latest post in every topic and forum&hellip; %s', 'bbpress' );
     838    $result    = esc_html__( 'Failed!', 'bbpress' );
    839839
    840840    // First, delete everything.
     
    942942
    943943    // Complete results
    944     return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
    945 }
    946 
    947 /**
    948  * Repairs the relationship of sticky topics to the actual parent forum
     944    return array( 0, sprintf( $statement, esc_html__( 'Complete!', 'bbpress' ) ) );
     945}
     946
     947/**
     948 * Repair the relationship of sticky topics to the actual parent forum
    949949 *
    950950 * @since 2.3.0 bbPress (r4695)
     
    963963    // Define variables
    964964    $bbp_db    = bbp_db();
    965     $statement = __( 'Repairing the sticky topic to the parent forum relationships&hellip; %s', 'bbpress' );
    966     $result    = __( 'Failed!', 'bbpress' );
     965    $statement = esc_html__( 'Repairing the sticky topic to the parent forum relationships&hellip; %s', 'bbpress' );
     966    $result    = esc_html__( 'Failed!', 'bbpress' );
    967967
    968968    $forums    = $bbp_db->get_col( "SELECT ID FROM `{$bbp_db->posts}` WHERE `post_type` = '" . bbp_get_forum_post_type() . "'" );
     
    994994            // If the topic is not a super sticky, and the forum ID does not
    995995            // match the topic's forum ID, unset the forum's sticky meta.
    996             if ( ! bbp_is_topic_super_sticky( $topic_id ) && $forum_id !== bbp_get_topic_forum_id( $topic_id ) ) {
     996            if ( ! bbp_is_topic_super_sticky( $topic_id ) && ( $forum_id !== bbp_get_topic_forum_id( $topic_id ) ) ) {
    997997                unset( $forum_stickies[ $forum_id ][ $id ] );
    998998            }
     
    10001000
    10011001        // Get sticky topic ID's, or use empty string
    1002         $stickers = empty( $forum_stickies[ $forum_id ] ) ? '' : array_values( $forum_stickies[ $forum_id ] );
     1002        $stickers = ! empty( $forum_stickies[ $forum_id ] )
     1003            ? array_values( $forum_stickies[ $forum_id ] )
     1004            : '';
    10031005
    10041006        // Update the forum's sticky topics meta
     
    10071009
    10081010    // Complete results
    1009     return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
     1011    return array( 0, sprintf( $statement, esc_html__( 'Complete!', 'bbpress' ) ) );
    10101012}
    10111013
     
    10131015 * Repair closed topics
    10141016 *
    1015  * Closed topics that are missing the postmeta "_bbp_status" key value "publish"
    1016  * result in unexpected behaviour, primarily this would have only occured if you
     1017 * Closed topics that are missing the post-meta "_bbp_status" key value "publish"
     1018 * result in unexpected behavior, primarily this would have only occurred if you
    10171019 * had imported forums from another forum package previous to bbPress v2.6,
    10181020 * https://bbpress.trac.wordpress.org/ticket/2577
     
    10311033    // Define variables
    10321034    $bbp_db        = bbp_db();
    1033     $statement     = __( 'Repairing closed topics&hellip; %s', 'bbpress' );
    1034     $result        = __( 'No closed topics to repair.', 'bbpress' );
     1035    $statement     = esc_html__( 'Repairing closed topics&hellip; %s', 'bbpress' );
     1036    $result        = esc_html__( 'No closed topics to repair.', 'bbpress' );
    10351037    $changed       = 0;
    10361038
    1037     $closed_topics = $bbp_db->get_col( "SELECT ID FROM `{$bbp_db->posts}` WHERE `post_type` = '" . bbp_get_topic_post_type() . "' AND `post_status` = 'closed'" );
     1039    // Results
     1040    $query         = "SELECT ID FROM `{$bbp_db->posts}` WHERE `post_type` = '%s' AND `post_status` = 'closed'";
     1041    $prepare       = $bbp_db->prepare( $query, bbp_get_topic_post_type() );
     1042    $closed_topics = $bbp_db->get_col( $prepare );
    10381043
    10391044    // Bail if no closed topics found
     
    10651070
    10661071/**
    1067  * Recaches the private and hidden forums
     1072 * Repair the private and hidden forums
    10681073 *
    10691074 * @since 2.2.0 bbPress (r4104)
     
    10731078 */
    10741079function bbp_admin_repair_forum_visibility() {
    1075     $statement = __( 'Recalculating forum visibility &hellip; %s', 'bbpress' );
     1080    $statement = esc_html__( 'Recalculating forum visibility&hellip; %s', 'bbpress' );
    10761081
    10771082    // Bail if queries returned errors
    10781083    if ( ! bbp_repair_forum_visibility() ) {
    1079         return array( 2, sprintf( $statement, __( 'Failed!',   'bbpress' ) ) );
     1084        return array( 2, sprintf( $statement, esc_html__( 'Failed!',   'bbpress' ) ) );
    10801085
    10811086    // Complete results
    10821087    } else {
    1083         return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
    1084     }
    1085 }
    1086 
    1087 /**
    1088  * Recaches the parent forum meta for each topic and reply
     1088        return array( 0, sprintf( $statement, esc_html__( 'Complete!', 'bbpress' ) ) );
     1089    }
     1090}
     1091
     1092/**
     1093 * Repair the parent forum meta for each topic and reply
    10891094 *
    10901095 * @since 2.1.0 bbPress (r3876)
     
    11001105    // Define variables
    11011106    $bbp_db    = bbp_db();
    1102     $statement = __( 'Recalculating the forum for each post &hellip; %s', 'bbpress' );
    1103     $result    = __( 'Failed!', 'bbpress' );
     1107    $statement = esc_html__( 'Recalculating the forum for each post&hellip; %s', 'bbpress' );
     1108    $result    = esc_html__( 'Failed!', 'bbpress' );
    11041109
    11051110    // First, delete everything.
     
    11371142
    11381143    // Complete results
    1139     return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
    1140 }
    1141 
    1142 /**
    1143  * Recaches the topic for each post
     1144    return array( 0, sprintf( $statement, esc_html__( 'Complete!', 'bbpress' ) ) );
     1145}
     1146
     1147/**
     1148 * Repair the topic for each post
    11441149 *
    11451150 * @since 2.1.0 bbPress (r3876)
     
    11551160    // Define variables
    11561161    $bbp_db    = bbp_db();
    1157     $statement = __( 'Recalculating the topic for each post &hellip; %s', 'bbpress' );
    1158     $result    = __( 'Failed!', 'bbpress' );
     1162    $statement = esc_html__( 'Recalculating the topic for each post&hellip; %s', 'bbpress' );
     1163    $result    = esc_html__( 'Failed!', 'bbpress' );
    11591164
    11601165    // First, delete everything.
     
    11921197
    11931198    // Complete results
    1194     return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
     1199    return array( 0, sprintf( $statement, esc_html__( 'Complete!', 'bbpress' ) ) );
    11951200}
    11961201
     
    12101215    // Define variables
    12111216    $bbp_db    = bbp_db();
    1212     $statement = __( 'Recalculating reply menu order &hellip; %s', 'bbpress' );
    1213     $result    = __( 'No reply positions to recalculate.',         'bbpress' );
     1217    $statement = esc_html__( 'Recalculating reply menu order&hellip; %s', 'bbpress' );
     1218    $result    = esc_html__( 'No reply positions to recalculate.',         'bbpress' );
    12141219
    12151220    // Delete cases where `_bbp_reply_to` was accidentally set to itself
     
    12491254    wp_cache_flush();
    12501255
    1251     return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
    1252 }
     1256    return array( 0, sprintf( $statement, esc_html__( 'Complete!', 'bbpress' ) ) );
     1257}
Note: See TracChangeset for help on using the changeset viewer.