| | 1 | <?php |
| | 2 | |
| | 3 | /** |
| | 4 | * BuddyPress Tools panel |
| | 5 | * |
| | 6 | * @since BuddyPress (2.0.0) |
| | 7 | */ |
| | 8 | |
| | 9 | /** |
| | 10 | * Render the BuddyPress Tools page. |
| | 11 | * |
| | 12 | * @since BuddyPress (2.0.0) |
| | 13 | */ |
| | 14 | function bp_core_admin_tools() { |
| | 15 | ?> |
| | 16 | <div class="wrap"> |
| | 17 | <?php screen_icon( 'buddypress'); ?> |
| | 18 | |
| | 19 | <h2><?php esc_html_e( 'BuddyPress Tools', 'buddypress' ) ?></h2> |
| | 20 | |
| | 21 | <p> |
| | 22 | <?php esc_html_e( 'BuddyPress keeps track of various relationships between users, groups, and activity items. Occasionally these relationships become out of sync, most often after an import, update, or migration.', 'buddypress' ); ?> |
| | 23 | <?php esc_html_e( 'Use the tools below to manually recalculate these relationships.', 'buddypress' ); ?> |
| | 24 | </p> |
| | 25 | <p class="description"><?php esc_html_e( 'Some of these tools create substantial database overhead. Avoid running more than one repair job at a time.', 'buddypress' ); ?></p> |
| | 26 | |
| | 27 | <form class="settings" method="post" action=""> |
| | 28 | <table class="form-table"> |
| | 29 | <tbody> |
| | 30 | <tr valign="top"> |
| | 31 | <th scope="row"><?php esc_html_e( 'Data to Repair:', 'buddypress' ) ?></th> |
| | 32 | <td> |
| | 33 | <fieldset> |
| | 34 | <legend class="screen-reader-text"><span><?php esc_html_e( 'Repair', 'buddypress' ) ?></span></legend> |
| | 35 | |
| | 36 | <?php foreach ( bp_admin_repair_list() as $item ) : ?> |
| | 37 | |
| | 38 | <label><input type="checkbox" class="checkbox" name="<?php echo esc_attr( $item[0] ) . '" id="' . esc_attr( str_replace( '_', '-', $item[0] ) ); ?>" value="1" /> <?php echo esc_html( $item[1] ); ?></label><br /> |
| | 39 | |
| | 40 | <?php endforeach; ?> |
| | 41 | |
| | 42 | </fieldset> |
| | 43 | </td> |
| | 44 | </tr> |
| | 45 | </tbody> |
| | 46 | </table> |
| | 47 | |
| | 48 | <fieldset class="submit"> |
| | 49 | <input class="button-primary" type="submit" name="bp-tools-submit" value="<?php esc_attr_e( 'Repair Items', 'buddypress' ); ?>" /> |
| | 50 | <?php wp_nonce_field( 'bp-do-counts' ); ?> |
| | 51 | </fieldset> |
| | 52 | </form> |
| | 53 | </div> |
| | 54 | <?php |
| | 55 | } |
| | 56 | |
| | 57 | /** |
| | 58 | * Handle the processing and feedback of the admin tools page. |
| | 59 | * |
| | 60 | * @since BuddyPress (2.0.0) |
| | 61 | */ |
| | 62 | function bp_admin_repair_handler() { |
| | 63 | if ( ! bp_is_post_request() ) { |
| | 64 | return; |
| | 65 | } |
| | 66 | |
| | 67 | if ( empty( $_POST['bp-tools-submit'] ) ) { |
| | 68 | return; |
| | 69 | } |
| | 70 | |
| | 71 | check_admin_referer( 'bp-do-counts' ); |
| | 72 | |
| | 73 | // Stores messages |
| | 74 | $messages = array(); |
| | 75 | |
| | 76 | wp_cache_flush(); |
| | 77 | |
| | 78 | foreach ( (array) bp_admin_repair_list() as $item ) { |
| | 79 | if ( isset( $item[2] ) && isset( $_POST[$item[0]] ) && 1 === absint( $_POST[$item[0]] ) && is_callable( $item[2] ) ) { |
| | 80 | $messages[] = call_user_func( $item[2] ); |
| | 81 | } |
| | 82 | } |
| | 83 | |
| | 84 | if ( count( $messages ) ) { |
| | 85 | foreach ( $messages as $message ) { |
| | 86 | bp_admin_tools_feedback( $message[1] ); |
| | 87 | } |
| | 88 | } |
| | 89 | } |
| | 90 | add_action( bp_core_admin_hook(), 'bp_admin_repair_handler' ); |
| | 91 | |
| | 92 | /** |
| | 93 | * Get the array of the repair list. |
| | 94 | * |
| | 95 | * @return array |
| | 96 | */ |
| | 97 | function bp_admin_repair_list() { |
| | 98 | // Members: |
| | 99 | // - member count |
| | 100 | $repair_list = array( |
| | 101 | 20 => array( |
| | 102 | 'bp-total-member-count', |
| | 103 | __( 'Count total members', 'buddypress' ), |
| | 104 | 'bp_admin_repair_count_members', |
| | 105 | ), |
| | 106 | ); |
| | 107 | |
| | 108 | // Friends: |
| | 109 | // - user friend count |
| | 110 | if ( bp_is_active( 'friends' ) ) { |
| | 111 | $repair_list[0] = array( |
| | 112 | 'bp-user-friends', |
| | 113 | __( 'Count friends for each user', 'buddypress' ), |
| | 114 | 'bp_admin_repair_friend_count', |
| | 115 | ); |
| | 116 | } |
| | 117 | |
| | 118 | // Groups: |
| | 119 | // - user group count |
| | 120 | if ( bp_is_active( 'groups' ) ) { |
| | 121 | $repair_list[10] = array( |
| | 122 | 'bp-group-count', |
| | 123 | __( 'Count groups for each user', 'buddypress' ), |
| | 124 | 'bp_admin_repair_group_count', |
| | 125 | ); |
| | 126 | } |
| | 127 | |
| | 128 | ksort( $repair_list ); |
| | 129 | |
| | 130 | return (array) apply_filters( 'bp_repair_list', $repair_list ); |
| | 131 | } |
| | 132 | |
| | 133 | /** |
| | 134 | * Recalculate friend counts for each user. |
| | 135 | * |
| | 136 | * @since BuddyPress (2.0.0) |
| | 137 | * |
| | 138 | * @return array |
| | 139 | */ |
| | 140 | function bp_admin_repair_friend_count() { |
| | 141 | global $wpdb, $bp; |
| | 142 | |
| | 143 | if ( ! bp_is_active( 'friends' ) ) { |
| | 144 | return; |
| | 145 | } |
| | 146 | |
| | 147 | $statement = __( 'Counting the number of friends for each user… %s', 'buddypress' ); |
| | 148 | $result = __( 'Failed!', 'buddypress' ); |
| | 149 | |
| | 150 | $sql_delete = "DELETE FROM {$wpdb->usermeta} WHERE meta_key IN ( 'total_friend_count' );"; |
| | 151 | if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) { |
| | 152 | return array( 1, sprintf( $statement, $result ) ); |
| | 153 | } |
| | 154 | |
| | 155 | // Walk through all users on the site |
| | 156 | $total_users = $wpdb->get_row( "SELECT count(ID) as c FROM {$wpdb->users}" )->c; |
| | 157 | |
| | 158 | $updated = array(); |
| | 159 | if ( $total_users > 0 ) { |
| | 160 | $per_query = 500; |
| | 161 | $offset = 0; |
| | 162 | while ( $offset < $total_users ) { |
| | 163 | // Only bother updating counts for users who actually have friendships |
| | 164 | $friendships = $wpdb->get_results( $wpdb->prepare( "SELECT initiator_user_id, friend_user_id FROM {$bp->friends->table_name} WHERE is_confirmed = 1 AND ( ( initiator_user_id > %d AND initiator_user_id <= %d ) OR ( friend_user_id > %d AND friend_user_id <= %d ) )", $offset, $offset + $per_query, $offset, $offset + $per_query ) ); |
| | 165 | |
| | 166 | // The previous query will turn up duplicates, so we |
| | 167 | // filter them here |
| | 168 | foreach ( $friendships as $friendship ) { |
| | 169 | if ( ! isset( $updated[ $friendship->initiator_user_id ] ) ) { |
| | 170 | BP_Friends_Friendship::total_friend_count( $friendship->initiator_user_id ); |
| | 171 | $updated[ $friendship->initiator_user_id ] = 1; |
| | 172 | } |
| | 173 | |
| | 174 | if ( ! isset( $updated[ $friendship->friend_user_id ] ) ) { |
| | 175 | BP_Friends_Friendship::total_friend_count( $friendship->friend_user_id ); |
| | 176 | $updated[ $friendship->friend_user_id ] = 1; |
| | 177 | } |
| | 178 | } |
| | 179 | |
| | 180 | $offset += $per_query; |
| | 181 | } |
| | 182 | } else { |
| | 183 | return array( 2, sprintf( $statement, $result ) ); |
| | 184 | } |
| | 185 | |
| | 186 | return array( 0, sprintf( $statement, __( 'Complete!', 'buddypress' ) ) ); |
| | 187 | } |
| | 188 | |
| | 189 | /** |
| | 190 | * Recalculate group counts for each user. |
| | 191 | * |
| | 192 | * @since BuddyPress (2.0.0) |
| | 193 | * |
| | 194 | * @return array |
| | 195 | */ |
| | 196 | function bp_admin_repair_group_count() { |
| | 197 | global $wpdb, $bp; |
| | 198 | |
| | 199 | if ( ! bp_is_active( 'groups' ) ) { |
| | 200 | return; |
| | 201 | } |
| | 202 | |
| | 203 | $statement = __( 'Counting the number of groups for each user… %s', 'buddypress' ); |
| | 204 | $result = __( 'Failed!', 'buddypress' ); |
| | 205 | |
| | 206 | $sql_delete = "DELETE FROM {$wpdb->usermeta} WHERE meta_key IN ( 'total_group_count' );"; |
| | 207 | if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) { |
| | 208 | return array( 1, sprintf( $statement, $result ) ); |
| | 209 | } |
| | 210 | |
| | 211 | // Walk through all users on the site |
| | 212 | $total_users = $wpdb->get_row( "SELECT count(ID) as c FROM {$wpdb->users}" )->c; |
| | 213 | |
| | 214 | if ( $total_users > 0 ) { |
| | 215 | $per_query = 500; |
| | 216 | $offset = 0; |
| | 217 | while ( $offset < $total_users ) { |
| | 218 | // But only bother to update counts for users that have groups |
| | 219 | $users = $wpdb->get_col( $wpdb->prepare( "SELECT user_id FROM {$bp->groups->table_name_members} WHERE is_confirmed = 1 AND is_banned = 0 AND user_id > %d AND user_id <= %d", $offset, $offset + $per_query ) ); |
| | 220 | |
| | 221 | foreach ( $users as $user ) { |
| | 222 | BP_Groups_Member::refresh_total_group_count_for_user( $user ); |
| | 223 | } |
| | 224 | |
| | 225 | $offset += $per_query; |
| | 226 | } |
| | 227 | } else { |
| | 228 | return array( 2, sprintf( $statement, $result ) ); |
| | 229 | } |
| | 230 | |
| | 231 | return array( 0, sprintf( $statement, __( 'Complete!', 'buddypress' ) ) ); |
| | 232 | } |
| | 233 | |
| | 234 | /** |
| | 235 | * Recalculate the total number of active site members. |
| | 236 | * |
| | 237 | * @since BuddyPress (2.0.0) |
| | 238 | */ |
| | 239 | function bp_admin_repair_count_members() { |
| | 240 | $statement = __( 'Counting the number of active members on the site… %s', 'buddypress' ); |
| | 241 | delete_transient( 'bp_active_member_count' ); |
| | 242 | bp_core_get_active_member_count(); |
| | 243 | return array( 0, sprintf( $statement, __( 'Complete!', 'buddypress' ) ) ); |
| | 244 | } |
| | 245 | |
| | 246 | /** |
| | 247 | * Assemble admin notices relating success/failure of repair processes. |
| | 248 | * |
| | 249 | * @since BuddyPress (2.0.0) |
| | 250 | * |
| | 251 | * @param string $message Feedback message. |
| | 252 | * @param unknown $class Unused. |
| | 253 | */ |
| | 254 | function bp_admin_tools_feedback( $message, $class = false ) { |
| | 255 | if ( is_string( $message ) ) { |
| | 256 | $message = '<p>' . $message . '</p>'; |
| | 257 | $class = $class ? $class : 'updated'; |
| | 258 | } elseif ( is_wp_error( $message ) ) { |
| | 259 | $errors = $message->get_error_messages(); |
| | 260 | |
| | 261 | switch ( count( $errors ) ) { |
| | 262 | case 0: |
| | 263 | return false; |
| | 264 | break; |
| | 265 | |
| | 266 | case 1: |
| | 267 | $message = '<p>' . $errors[0] . '</p>'; |
| | 268 | break; |
| | 269 | |
| | 270 | default: |
| | 271 | $message = '<ul>' . "\n\t" . '<li>' . implode( '</li>' . "\n\t" . '<li>', $errors ) . '</li>' . "\n" . '</ul>'; |
| | 272 | break; |
| | 273 | } |
| | 274 | |
| | 275 | $class = $class ? $class : 'error'; |
| | 276 | } else { |
| | 277 | return false; |
| | 278 | } |
| | 279 | |
| | 280 | $message = '<div id="message" class="' . esc_attr( $class ) . '">' . $message . '</div>'; |
| | 281 | $message = str_replace( "'", "\'", $message ); |
| | 282 | $lambda = create_function( '', "echo '$message';" ); |
| | 283 | |
| | 284 | add_action( 'admin_notices', $lambda ); |
| | 285 | |
| | 286 | return $lambda; |
| | 287 | } |
| | 288 | |
| | 289 | /** |
| | 290 | * Render the Available Tools page. |
| | 291 | * |
| | 292 | * We register this page on Network Admin as a top-level home for our |
| | 293 | * BuddyPress tools. This displays the default content. |
| | 294 | * |
| | 295 | * @since BuddyPress (2.0.0) |
| | 296 | */ |
| | 297 | function bp_core_admin_available_tools_page() { |
| | 298 | ?> |
| | 299 | <div class="wrap"> |
| | 300 | <h2><?php esc_attr_e( 'Tools', 'buddypress' ) ?></h2> |
| | 301 | |
| | 302 | <?php do_action( 'bp_network_tool_box' );?> |
| | 303 | |
| | 304 | </div> |
| | 305 | <?php |
| | 306 | } |
| | 307 | |
| | 308 | /** |
| | 309 | * Render an introduction of BuddyPress tools on Available Tools page. |
| | 310 | * |
| | 311 | * @since BuddyPress (2.0.0) |
| | 312 | */ |
| | 313 | function bp_core_admin_available_tools_intro() { |
| | 314 | $query_arg = array( |
| | 315 | 'page' => 'bp-tools' |
| | 316 | ); |
| | 317 | |
| | 318 | $page = bp_core_do_network_admin() ? 'admin.php' : 'tools.php' ; |
| | 319 | $url = add_query_arg( $query_arg, bp_get_admin_url( $page ) ); |
| | 320 | ?> |
| | 321 | <div class="tool-box"> |
| | 322 | <h3 class="title"><?php esc_html_e( 'BuddyPress Tools', 'buddypress' ) ?></h3> |
| | 323 | <p> |
| | 324 | <?php esc_html_e( 'BuddyPress keeps track of various relationships between users, groups, and activity items. Occasionally these relationships become out of sync, most often after an import, update, or migration.', 'buddypress' ); ?> |
| | 325 | <?php printf( _x( 'Use the %s.', 'buddypress tools intro', 'buddypress' ), '<a href="' . esc_url( $url ) . '">' . esc_html__( 'BuddyPress Tools', 'buddypress' ) . '</a>' ); ?> |
| | 326 | </p> |
| | 327 | </div> |
| | 328 | <?php |
| | 329 | } |
| | 330 | No newline at end of file |