Skip to:
Content

BuddyPress.org

Ticket #5296: 5296.suggestion.patch

File 5296.suggestion.patch, 13.9 KB (added by imath, 11 years ago)
  • bp-core/admin/bp-core-functions.php

    diff --git bp-core/admin/bp-core-functions.php bp-core/admin/bp-core-functions.php
    index e47e7da..ef11105 100644
    add_action( bp_core_admin_hook(), 'bp_core_admin_backpat_menu', 999 ); 
    7474 * @since BuddyPress (1.6)
    7575 */
    7676function bp_core_modify_admin_menu_highlight() {
    77         global $plugin_page, $submenu_file;
     77        global $pagenow, $plugin_page, $submenu_file;
    7878
    7979        // This tweaks the Settings subnav menu to show only one BuddyPress menu item
    8080        if ( ! in_array( $plugin_page, array( 'bp-activity', 'bp-general-settings', ) ) )
    8181                $submenu_file = 'bp-components';
     82
     83        // Network Admin > Tools
     84        if ( in_array( $plugin_page, array( 'bp-tools', 'available-tools' ) ) ) {
     85                $submenu_file = $plugin_page;
     86        }
    8287}
    8388
    8489/**
    function bp_core_admin_tabs( $active_tab = '' ) { 
    388393                '2' => array(
    389394                        'href' => bp_get_admin_url( add_query_arg( array( 'page' => 'bp-settings' ), 'admin.php' ) ),
    390395                        'name' => __( 'Settings', 'buddypress' )
    391                 )
     396                ),
    392397        );
    393398
    394399        // If forums component is active, add additional tab
  • bp-core/admin/bp-core-tools.php

    diff --git bp-core/admin/bp-core-tools.php bp-core/admin/bp-core-tools.php
    index e69de29..32ba8fd 100644
     
     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 */
     14function 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 */
     62function 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}
     90add_action( bp_core_admin_hook(), 'bp_admin_repair_handler' );
     91
     92/**
     93 * Get the array of the repair list.
     94 *
     95 * @return array
     96 */
     97function 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 */
     140function 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&hellip; %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 */
     196function 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&hellip; %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 */
     239function bp_admin_repair_count_members() {
     240        $statement = __( 'Counting the number of active members on the site&hellip; %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 */
     254function 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 */
     297function 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 */
     313function 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
  • bp-core/admin/css/common.css

    diff --git bp-core/admin/css/common.css bp-core/admin/css/common.css
    index 17548ec..e476679 100644
    body.branch-3-7 ul#adminmenu li.toplevel_page_bp-groups_network.current .wp-menu 
    9090body.branch-3-7 ul#adminmenu li.toplevel_page_bp-groups_network.wp-has-current-submenu .wp-menu-image {
    9191        background-position: -61px -2px;
    9292}
     93
     94/* Tools */
     95#adminmenu .toplevel_page_network-tools div.wp-menu-image:before {
     96    content: "";
     97}
     98
    9399th.column-gid {
    94100        width: 60px;
    95101}
  • bp-core/bp-core-admin.php

    diff --git bp-core/bp-core-admin.php bp-core/bp-core-admin.php
    index 4020355..f6cfc3a 100644
    class BP_Admin { 
    118118                require( $this->admin_dir . 'bp-core-functions.php'  );
    119119                require( $this->admin_dir . 'bp-core-components.php' );
    120120                require( $this->admin_dir . 'bp-core-slugs.php'      );
     121                require( $this->admin_dir . 'bp-core-tools.php'      );
    121122        }
    122123
    123124        /**
    class BP_Admin { 
    153154                // Add a link to BuddyPress About page to the admin bar
    154155                add_action( 'admin_bar_menu', array( $this, 'admin_bar_about_link' ), 15 );
    155156
     157                // Add a description of new BuddyPress tools in the available tools page
     158                add_action( 'tool_box', 'bp_core_admin_available_tools_intro' );
     159                add_action( 'bp_network_tool_box', 'bp_core_admin_available_tools_intro' );
     160
    156161                /** Filters ***********************************************************/
    157162
    158163                // Add link to settings page
    class BP_Admin { 
    242247                        'bp_core_admin_settings'
    243248                );
    244249
     250                // For consistency with non-Multisite, we add a Tools menu in
     251                // the Network Admin as a home for our Tools panel
     252                if ( is_multisite() && bp_core_do_network_admin() ) {
     253                        $tools_parent = 'network-tools';
     254
     255                        $hooks[] = add_menu_page(
     256                                __( 'Tools', 'buddypress' ),
     257                                __( 'Tools', 'buddypress' ),
     258                                'manage_network_options',
     259                                $tools_parent,
     260                                'bp_core_tools_top_level_item',
     261                                '',
     262                                24 // just above Settings
     263                        );
     264
     265                        $hooks[] = add_submenu_page(
     266                                $tools_parent,
     267                                __( 'Available Tools', 'buddypress' ),
     268                                __( 'Available Tools', 'buddypress' ),
     269                                'manage_network_options',
     270                                'available-tools',
     271                                'bp_core_admin_available_tools_page'
     272                        );
     273                } else {
     274                        $tools_parent = 'tools.php';
     275                }
     276
     277                $hooks[] = add_submenu_page(
     278                        $tools_parent,
     279                        __( 'BuddyPress Tools', 'buddypress' ),
     280                        __( 'BuddyPress', 'buddypress' ),
     281                        'manage_options',
     282                        'bp-tools',
     283                        'bp_core_admin_tools'
     284                );
     285
    245286                // Fudge the highlighted subnav item when on a BuddyPress admin page
    246287                foreach( $hooks as $hook ) {
    247288                        add_action( "admin_head-$hook", 'bp_core_modify_admin_menu_highlight' );
    class BP_Admin { 
    401442                remove_submenu_page( $this->settings_page, 'bp-page-settings' );
    402443                remove_submenu_page( $this->settings_page, 'bp-settings'      );
    403444
     445                // Network Admin Tools
     446                remove_submenu_page( 'network-tools', 'network-tools' );
     447
    404448                // About and Credits pages
    405449                remove_submenu_page( 'index.php', 'bp-about'   );
    406450                remove_submenu_page( 'index.php', 'bp-credits' );