[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-core/admin/ -> bp-core-admin-tools.php (source)

   1  <?php
   2  /**
   3   * BuddyPress Tools panel.
   4   *
   5   * @package BuddyPress
   6   * @subpackage Core
   7   * @since 2.0.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * Render the BuddyPress Tools page.
  15   *
  16   * @since 2.0.0
  17   */
  18  function bp_core_admin_tools() {
  19      ?>
  20      <div class="wrap">
  21  
  22          <h1><?php esc_html_e( 'BuddyPress Tools', 'buddypress' ) ?></h1>
  23  
  24          <p><?php esc_html_e( 'BuddyPress keeps track of various relationships between members, groups, and activity items. Occasionally these relationships become out of sync, most often after an import, update, or migration.', 'buddypress' ); ?></p>
  25          <p><?php esc_html_e( 'Use the tools below to manually recalculate these relationships.', 'buddypress' ); ?>
  26          </p>
  27          <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>
  28  
  29          <form class="settings" method="post" action="">
  30  
  31              <fieldset>
  32                  <legend><?php esc_html_e( 'Repair tools', 'buddypress' ) ?></legend>
  33  
  34                  <div class="checkbox">
  35                  <?php foreach ( bp_admin_repair_list() as $item ) : ?>
  36                      <label for="<?php echo esc_attr( str_replace( '_', '-', $item[0] ) ); ?>"><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>
  37                  <?php endforeach; ?>
  38                  </div>
  39  
  40                  <p class="submit">
  41                      <input class="button-primary" type="submit" name="bp-tools-submit" value="<?php esc_attr_e( 'Repair Items', 'buddypress' ); ?>" />
  42                      <?php wp_nonce_field( 'bp-do-counts' ); ?>
  43                  </p>
  44  
  45              </fieldset>
  46  
  47          </form>
  48  
  49      </div>
  50  
  51      <?php
  52  }
  53  
  54  /**
  55   * Handle the processing and feedback of the admin tools page.
  56   *
  57   * @since 2.0.0
  58   */
  59  function bp_admin_repair_handler() {
  60      if ( ! bp_is_post_request() || empty( $_POST['bp-tools-submit'] ) ) {
  61          return;
  62      }
  63  
  64      check_admin_referer( 'bp-do-counts' );
  65  
  66      // Bail if user cannot moderate.
  67      $capability = bp_core_do_network_admin() ? 'manage_network_options' : 'manage_options';
  68      if ( ! bp_current_user_can( $capability ) ) {
  69          return;
  70      }
  71  
  72      wp_cache_flush();
  73      $messages = array();
  74  
  75      foreach ( (array) bp_admin_repair_list() as $item ) {
  76          if ( isset( $item[2] ) && isset( $_POST[$item[0]] ) && 1 === absint( $_POST[$item[0]] ) && is_callable( $item[2] ) ) {
  77              $messages[] = call_user_func( $item[2] );
  78          }
  79      }
  80  
  81      if ( count( $messages ) ) {
  82          foreach ( $messages as $message ) {
  83              bp_admin_tools_feedback( $message[1] );
  84          }
  85      }
  86  }
  87  add_action( bp_core_admin_hook(), 'bp_admin_repair_handler' );
  88  
  89  /**
  90   * Get the array of the repair list.
  91   *
  92   * @return array
  93   */
  94  function bp_admin_repair_list() {
  95      $repair_list = array();
  96  
  97      // Members:
  98      // - member count
  99      // - last_activity migration (2.0).
 100      $repair_list[20] = array(
 101          'bp-total-member-count',
 102          __( 'Repair total members count.', 'buddypress' ),
 103          'bp_admin_repair_count_members',
 104      );
 105  
 106      $repair_list[25] = array(
 107          'bp-last-activity',
 108          __( 'Repair member "last activity" data.', 'buddypress' ),
 109          'bp_admin_repair_last_activity',
 110      );
 111  
 112      // Friends:
 113      // - user friend count.
 114      if ( bp_is_active( 'friends' ) ) {
 115          $repair_list[0] = array(
 116              'bp-user-friends',
 117              __( 'Repair total friends count for each member.', 'buddypress' ),
 118              'bp_admin_repair_friend_count',
 119          );
 120      }
 121  
 122      // Groups:
 123      // - user group count.
 124      if ( bp_is_active( 'groups' ) ) {
 125          $repair_list[10] = array(
 126              'bp-group-count',
 127              __( 'Repair total groups count for each member.', 'buddypress' ),
 128              'bp_admin_repair_group_count',
 129          );
 130      }
 131  
 132      // Blogs:
 133      // - user blog count.
 134      if ( bp_is_active( 'blogs' ) ) {
 135          $repair_list[90] = array(
 136              'bp-blog-records',
 137              __( 'Repopulate site tracking records.', 'buddypress' ),
 138              'bp_admin_repair_blog_records',
 139          );
 140      }
 141  
 142      // Emails:
 143      // - reinstall emails.
 144      $repair_list[100] = array(
 145          'bp-reinstall-emails',
 146          __( 'Reinstall emails (delete and restore from defaults).', 'buddypress' ),
 147          'bp_admin_reinstall_emails',
 148      );
 149  
 150      ksort( $repair_list );
 151  
 152      /**
 153       * Filters the array of the repair list.
 154       *
 155       * @since 2.0.0
 156       *
 157       * @param array $repair_list Array of values for the Repair list options.
 158       */
 159      return (array) apply_filters( 'bp_repair_list', $repair_list );
 160  }
 161  
 162  /**
 163   * Recalculate friend counts for each user.
 164   *
 165   * @since 2.0.0
 166   *
 167   * @return array
 168   */
 169  function bp_admin_repair_friend_count() {
 170      global $wpdb;
 171  
 172      if ( ! bp_is_active( 'friends' ) ) {
 173          return;
 174      }
 175  
 176      $statement = __( 'Counting the number of friends for each user&hellip; %s', 'buddypress' );
 177      $result    = __( 'Failed!', 'buddypress' );
 178  
 179      $sql_delete = "DELETE FROM {$wpdb->usermeta} WHERE meta_key IN ( 'total_friend_count' );";
 180      if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) {
 181          return array( 1, sprintf( $statement, $result ) );
 182      }
 183  
 184      $bp = buddypress();
 185  
 186      // Walk through all users on the site.
 187      $total_users = $wpdb->get_row( "SELECT count(ID) as c FROM {$wpdb->users}" )->c;
 188  
 189      $updated = array();
 190      if ( $total_users > 0 ) {
 191          $per_query = 500;
 192          $offset = 0;
 193          while ( $offset < $total_users ) {
 194              // Only bother updating counts for users who actually have friendships.
 195              $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 ) );
 196  
 197              // The previous query will turn up duplicates, so we
 198              // filter them here.
 199              foreach ( $friendships as $friendship ) {
 200                  if ( ! isset( $updated[ $friendship->initiator_user_id ] ) ) {
 201                      BP_Friends_Friendship::total_friend_count( $friendship->initiator_user_id );
 202                      $updated[ $friendship->initiator_user_id ] = 1;
 203                  }
 204  
 205                  if ( ! isset( $updated[ $friendship->friend_user_id ] ) ) {
 206                      BP_Friends_Friendship::total_friend_count( $friendship->friend_user_id );
 207                      $updated[ $friendship->friend_user_id ] = 1;
 208                  }
 209              }
 210  
 211              $offset += $per_query;
 212          }
 213      } else {
 214          return array( 2, sprintf( $statement, $result ) );
 215      }
 216  
 217      return array( 0, sprintf( $statement, __( 'Complete!', 'buddypress' ) ) );
 218  }
 219  
 220  /**
 221   * Recalculate group counts for each user.
 222   *
 223   * @since 2.0.0
 224   *
 225   * @return array
 226   */
 227  function bp_admin_repair_group_count() {
 228      global $wpdb;
 229  
 230      if ( ! bp_is_active( 'groups' ) ) {
 231          return;
 232      }
 233  
 234      $statement = __( 'Counting the number of groups for each user&hellip; %s', 'buddypress' );
 235      $result    = __( 'Failed!', 'buddypress' );
 236  
 237      $sql_delete = "DELETE FROM {$wpdb->usermeta} WHERE meta_key IN ( 'total_group_count' );";
 238      if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) {
 239          return array( 1, sprintf( $statement, $result ) );
 240      }
 241  
 242      $bp = buddypress();
 243  
 244      // Walk through all users on the site.
 245      $total_users = $wpdb->get_row( "SELECT count(ID) as c FROM {$wpdb->users}" )->c;
 246  
 247      if ( $total_users > 0 ) {
 248          $per_query = 500;
 249          $offset = 0;
 250          while ( $offset < $total_users ) {
 251              // But only bother to update counts for users that have groups.
 252              $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 ) );
 253  
 254              foreach ( $users as $user ) {
 255                  BP_Groups_Member::refresh_total_group_count_for_user( $user );
 256              }
 257  
 258              $offset += $per_query;
 259          }
 260      } else {
 261          return array( 2, sprintf( $statement, $result ) );
 262      }
 263  
 264      return array( 0, sprintf( $statement, __( 'Complete!', 'buddypress' ) ) );
 265  }
 266  
 267  /**
 268   * Recalculate user-to-blog relationships and useful blog meta data.
 269   *
 270   * @since 2.1.0
 271   *
 272   * @return array
 273   */
 274  function bp_admin_repair_blog_records() {
 275  
 276      // Description of this tool, displayed to the user.
 277      $statement = __( 'Repopulating Blogs records&hellip; %s', 'buddypress' );
 278  
 279      // Default to failure text.
 280      $result    = __( 'Failed!',   'buddypress' );
 281  
 282      // Default to unrepaired.
 283      $repair    = false;
 284  
 285      // Run function if blogs component is active.
 286      if ( bp_is_active( 'blogs' ) ) {
 287          $repair = bp_blogs_record_existing_blogs();
 288      }
 289  
 290      // Setup success/fail messaging.
 291      if ( true === $repair ) {
 292          $result = __( 'Complete!', 'buddypress' );
 293      }
 294  
 295      // All done!
 296      return array( 0, sprintf( $statement, $result ) );
 297  }
 298  
 299  /**
 300   * Recalculate the total number of active site members.
 301   *
 302   * @since 2.0.0
 303   */
 304  function bp_admin_repair_count_members() {
 305      $statement = __( 'Counting the number of active members on the site&hellip; %s', 'buddypress' );
 306      delete_transient( 'bp_active_member_count' );
 307      bp_core_get_active_member_count();
 308      return array( 0, sprintf( $statement, __( 'Complete!', 'buddypress' ) ) );
 309  }
 310  
 311  /**
 312   * Repair user last_activity data.
 313   *
 314   * Re-runs the migration from usermeta introduced in BP 2.0.
 315   *
 316   * @since 2.0.0
 317   */
 318  function bp_admin_repair_last_activity() {
 319      $statement = __( 'Determining last activity dates for each user&hellip; %s', 'buddypress' );
 320      bp_last_activity_migrate();
 321      return array( 0, sprintf( $statement, __( 'Complete!', 'buddypress' ) ) );
 322  }
 323  
 324  /**
 325   * Assemble admin notices relating success/failure of repair processes.
 326   *
 327   * @since 2.0.0
 328   *
 329   * @param string      $message Feedback message.
 330   * @param string|bool $class   Unused.
 331   * @return false|Closure
 332   */
 333  function bp_admin_tools_feedback( $message, $class = false ) {
 334      if ( is_string( $message ) ) {
 335          $message = '<p>' . $message . '</p>';
 336          $class = $class ? $class : 'updated';
 337      } elseif ( is_wp_error( $message ) ) {
 338          $errors = $message->get_error_messages();
 339  
 340          switch ( count( $errors ) ) {
 341              case 0:
 342                  return false;
 343  
 344              case 1:
 345                  $message = '<p>' . $errors[0] . '</p>';
 346                  break;
 347  
 348              default:
 349                  $message = '<ul>' . "\n\t" . '<li>' . implode( '</li>' . "\n\t" . '<li>', $errors ) . '</li>' . "\n" . '</ul>';
 350                  break;
 351          }
 352  
 353          $class = $class ? $class : 'error';
 354      } else {
 355          return false;
 356      }
 357  
 358      $message = '<div id="message" class="' . esc_attr( $class ) . '">' . $message . '</div>';
 359      $message = str_replace( "'", "\'", $message );
 360      $lambda  = function() use ( $message ) { echo $message; };
 361  
 362      add_action( bp_core_do_network_admin() ? 'network_admin_notices' : 'admin_notices', $lambda );
 363  
 364      return $lambda;
 365  }
 366  
 367  /**
 368   * Render the Available Tools page.
 369   *
 370   * We register this page on Network Admin as a top-level home for our
 371   * BuddyPress tools. This displays the default content.
 372   *
 373   * @since 2.0.0
 374   */
 375  function bp_core_admin_available_tools_page() {
 376      ?>
 377      <div class="wrap">
 378          <h1><?php esc_attr_e( 'Tools', 'buddypress' ) ?></h1>
 379  
 380          <?php
 381  
 382          /**
 383           * Fires inside the markup used to display the Available Tools page.
 384           *
 385           * @since 2.0.0
 386           */
 387          do_action( 'bp_network_tool_box' ); ?>
 388  
 389      </div>
 390      <?php
 391  }
 392  
 393  /**
 394   * Render an introduction of BuddyPress tools on Available Tools page.
 395   *
 396   * @since 2.0.0
 397   */
 398  function bp_core_admin_available_tools_intro() {
 399      $query_arg = array(
 400          'page' => 'bp-tools'
 401      );
 402  
 403      $page = bp_core_do_network_admin() ? 'admin.php' : 'tools.php' ;
 404      $url  = add_query_arg( $query_arg, bp_get_admin_url( $page ) );
 405      ?>
 406      <div class="card tool-box">
 407          <h2><?php esc_html_e( 'BuddyPress Tools', 'buddypress' ) ?></h2>
 408          <p>
 409              <?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' ); ?>
 410              <?php printf( esc_html_x( 'Use the %s to repair these relationships.', 'buddypress tools intro', 'buddypress' ), '<a href="' . esc_url( $url ) . '">' . esc_html__( 'BuddyPress Tools', 'buddypress' ) . '</a>' ); ?>
 411          </p>
 412      </div>
 413      <?php
 414  }
 415  
 416  /**
 417   * Delete emails and restore from defaults.
 418   *
 419   * @since 2.5.0
 420   *
 421   * @return array
 422   */
 423  function bp_admin_reinstall_emails() {
 424      $switched = false;
 425  
 426      // Switch to the root blog, where the email posts live.
 427      if ( ! bp_is_root_blog() ) {
 428          switch_to_blog( bp_get_root_blog_id() );
 429          bp_register_taxonomies();
 430  
 431          $switched = true;
 432      }
 433  
 434      $emails = get_posts( array(
 435          'fields'           => 'ids',
 436          'post_status'      => 'publish',
 437          'post_type'        => bp_get_email_post_type(),
 438          'posts_per_page'   => 200,
 439          'suppress_filters' => false,
 440      ) );
 441  
 442      if ( $emails ) {
 443          foreach ( $emails as $email_id ) {
 444              wp_trash_post( $email_id );
 445          }
 446      }
 447  
 448      // Make sure we have no orphaned email type terms.
 449      $email_types = get_terms( bp_get_email_tax_type(), array(
 450          'fields'                 => 'ids',
 451          'hide_empty'             => false,
 452          'update_term_meta_cache' => false,
 453      ) );
 454  
 455      if ( $email_types ) {
 456          foreach ( $email_types as $term_id ) {
 457              wp_delete_term( (int) $term_id, bp_get_email_tax_type() );
 458          }
 459      }
 460  
 461      require_once( buddypress()->plugin_dir . '/bp-core/admin/bp-core-admin-schema.php' );
 462      bp_core_install_emails();
 463  
 464      if ( $switched ) {
 465          restore_current_blog();
 466      }
 467  
 468      return array( 0, __( 'Emails have been successfully reinstalled.', 'buddypress' ) );
 469  }
 470  
 471  /**
 472   * Add notice on the "Tools > BuddyPress" page if more sites need recording.
 473   *
 474   * This notice only shows up in the network admin dashboard.
 475   *
 476   * @since 2.6.0
 477   */
 478  function bp_core_admin_notice_repopulate_blogs_resume() {
 479      $screen = get_current_screen();
 480      if ( 'tools_page_bp-tools-network' !== $screen->id ) {
 481          return;
 482      }
 483  
 484      if ( '' === bp_get_option( '_bp_record_blogs_offset' ) ) {
 485          return;
 486      }
 487  
 488      echo '<div class="error"><p>' . __( 'It looks like you have more sites to record. Resume recording by checking the "Repopulate site tracking records" option.', 'buddypress' ) . '</p></div>';
 489  }
 490  add_action( 'network_admin_notices', 'bp_core_admin_notice_repopulate_blogs_resume' );
 491  
 492  /**
 493   * Add BuddyPress debug info to the WordPress Site Health info screen.
 494   *
 495   * @since 5.0.0
 496   *
 497   * @param  array $debug_info The Site's debug info.
 498   * @return array             The Site's debug info, including the BuddyPress specific ones.
 499   */
 500  function bp_core_admin_debug_information( $debug_info = array() ) {
 501      global $wp_settings_fields;
 502      $active_components = array_intersect_key( bp_core_get_components(), buddypress()->active_components );
 503      $bp_settings       = array();
 504  
 505      foreach ( $wp_settings_fields['buddypress'] as $section => $settings ) {
 506          $prefix       = '';
 507          $component_id = str_replace( 'bp_', '', $section );
 508  
 509          if ( isset( $active_components[ $component_id ]['title'] ) ) {
 510              $prefix = $active_components[ $component_id ]['title'] .': ';
 511          }
 512  
 513          foreach( $settings as $bp_setting ) {
 514              $reverse = (
 515                  strpos( $bp_setting['id'], 'hide' ) !== false ||
 516                  strpos( $bp_setting['id'], 'restrict' ) !== false ||
 517                  strpos( $bp_setting['id'], 'disable' ) !== false
 518              );
 519  
 520              if ( ! isset( $bp_setting['id'] ) || '_bp_theme_package_id' === $bp_setting['id'] ) {
 521                  continue;
 522              }
 523  
 524              $bp_setting_value = bp_get_option( $bp_setting['id'] );
 525              if ( '0' === $bp_setting_value || '1' === $bp_setting_value ) {
 526                  if ( ( $reverse && '0' === $bp_setting_value ) || ( ! $reverse && '1' === $bp_setting_value ) ) {
 527                      $bp_setting_value = __( 'Yes', 'buddypress' );
 528                  } else {
 529                      $bp_setting_value = __( 'No', 'buddypress' );
 530                  }
 531              }
 532  
 533              // Make sure to show the setting is reversed when site info is copied to clipboard.
 534              $bp_settings_id = $bp_setting['id'];
 535              if ( $reverse ) {
 536                  $bp_settings_id = '! ' . $bp_settings_id;
 537              }
 538  
 539              $bp_settings[ $bp_settings_id ] = array(
 540                  'label' => $prefix . $bp_setting['title'],
 541                  'value' => $bp_setting_value,
 542              );
 543          }
 544      }
 545  
 546      $debug_info['buddypress'] = array(
 547          'label'  => __( 'BuddyPress', 'buddypress' ),
 548          'fields' => array_merge(
 549              array(
 550                  'version' => array(
 551                      'label' => __( 'Version', 'buddypress' ),
 552                      'value' => bp_get_version(),
 553                  ),
 554                  'active_components' => array(
 555                      'label' => __( 'Active components', 'buddypress' ),
 556                      'value' => implode( wp_list_pluck( $active_components, 'title' ), ', ' ),
 557                  ),
 558                  'template_pack' => array(
 559                      'label' => __( 'Active template pack', 'buddypress' ),
 560                      'value' => bp_get_theme_compat_name() . ' ' . bp_get_theme_compat_version(),
 561                  ),
 562              ),
 563              $bp_settings
 564          )
 565      );
 566  
 567      return $debug_info;
 568  }
 569  add_filter( 'debug_information', 'bp_core_admin_debug_information' );


Generated: Wed Dec 11 01:01:37 2019 Cross-referenced by PHPXref 0.7.1