[ 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      /* translators: %s: the result of the action performed by the repair tool */
 177      $statement = __( 'Counting the number of friends for each user&hellip; %s', 'buddypress' );
 178      $result    = __( 'Failed!', 'buddypress' );
 179  
 180      $sql_delete = "DELETE FROM {$wpdb->usermeta} WHERE meta_key IN ( 'total_friend_count' );";
 181      if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) {
 182          return array( 1, sprintf( $statement, $result ) );
 183      }
 184  
 185      $bp = buddypress();
 186  
 187      // Walk through all users on the site.
 188      $total_users = $wpdb->get_row( "SELECT count(ID) as c FROM {$wpdb->users}" )->c;
 189  
 190      $updated = array();
 191      if ( $total_users > 0 ) {
 192          $per_query = 500;
 193          $offset = 0;
 194          while ( $offset < $total_users ) {
 195              // Only bother updating counts for users who actually have friendships.
 196              $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 ) );
 197  
 198              // The previous query will turn up duplicates, so we
 199              // filter them here.
 200              foreach ( $friendships as $friendship ) {
 201                  if ( ! isset( $updated[ $friendship->initiator_user_id ] ) ) {
 202                      BP_Friends_Friendship::total_friend_count( $friendship->initiator_user_id );
 203                      $updated[ $friendship->initiator_user_id ] = 1;
 204                  }
 205  
 206                  if ( ! isset( $updated[ $friendship->friend_user_id ] ) ) {
 207                      BP_Friends_Friendship::total_friend_count( $friendship->friend_user_id );
 208                      $updated[ $friendship->friend_user_id ] = 1;
 209                  }
 210              }
 211  
 212              $offset += $per_query;
 213          }
 214      } else {
 215          return array( 2, sprintf( $statement, $result ) );
 216      }
 217  
 218      return array( 0, sprintf( $statement, __( 'Complete!', 'buddypress' ) ) );
 219  }
 220  
 221  /**
 222   * Recalculate group counts for each user.
 223   *
 224   * @since 2.0.0
 225   *
 226   * @return array
 227   */
 228  function bp_admin_repair_group_count() {
 229      global $wpdb;
 230  
 231      if ( ! bp_is_active( 'groups' ) ) {
 232          return;
 233      }
 234  
 235      /* translators: %s: the result of the action performed by the repair tool */
 236      $statement = __( 'Counting the number of groups for each user&hellip; %s', 'buddypress' );
 237      $result    = __( 'Failed!', 'buddypress' );
 238  
 239      $sql_delete = "DELETE FROM {$wpdb->usermeta} WHERE meta_key IN ( 'total_group_count' );";
 240      if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) {
 241          return array( 1, sprintf( $statement, $result ) );
 242      }
 243  
 244      $bp = buddypress();
 245  
 246      // Walk through all users on the site.
 247      $total_users = $wpdb->get_row( "SELECT count(ID) as c FROM {$wpdb->users}" )->c;
 248  
 249      if ( $total_users > 0 ) {
 250          $per_query = 500;
 251          $offset = 0;
 252          while ( $offset < $total_users ) {
 253              // But only bother to update counts for users that have groups.
 254              $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 ) );
 255  
 256              foreach ( $users as $user ) {
 257                  BP_Groups_Member::refresh_total_group_count_for_user( $user );
 258              }
 259  
 260              $offset += $per_query;
 261          }
 262      } else {
 263          return array( 2, sprintf( $statement, $result ) );
 264      }
 265  
 266      return array( 0, sprintf( $statement, __( 'Complete!', 'buddypress' ) ) );
 267  }
 268  
 269  /**
 270   * Recalculate user-to-blog relationships and useful blog meta data.
 271   *
 272   * @since 2.1.0
 273   *
 274   * @return array
 275   */
 276  function bp_admin_repair_blog_records() {
 277  
 278      /* translators: %s: the result of the action performed by the repair tool */
 279      $statement = __( 'Repopulating Blogs records&hellip; %s', 'buddypress' );
 280  
 281      // Default to failure text.
 282      $result    = __( 'Failed!',   'buddypress' );
 283  
 284      // Default to unrepaired.
 285      $repair    = false;
 286  
 287      // Run function if blogs component is active.
 288      if ( bp_is_active( 'blogs' ) ) {
 289          $repair = bp_blogs_record_existing_blogs();
 290      }
 291  
 292      // Setup success/fail messaging.
 293      if ( true === $repair ) {
 294          $result = __( 'Complete!', 'buddypress' );
 295      }
 296  
 297      // All done!
 298      return array( 0, sprintf( $statement, $result ) );
 299  }
 300  
 301  /**
 302   * Recalculate the total number of active site members.
 303   *
 304   * @since 2.0.0
 305   */
 306  function bp_admin_repair_count_members() {
 307      /* translators: %s: the result of the action performed by the repair tool */
 308      $statement = __( 'Counting the number of active members on the site&hellip; %s', 'buddypress' );
 309      delete_transient( 'bp_active_member_count' );
 310      bp_core_get_active_member_count();
 311      return array( 0, sprintf( $statement, __( 'Complete!', 'buddypress' ) ) );
 312  }
 313  
 314  /**
 315   * Repair user last_activity data.
 316   *
 317   * Re-runs the migration from usermeta introduced in BP 2.0.
 318   *
 319   * @since 2.0.0
 320   */
 321  function bp_admin_repair_last_activity() {
 322      /* translators: %s: the result of the action performed by the repair tool */
 323      $statement = __( 'Determining last activity dates for each user&hellip; %s', 'buddypress' );
 324      bp_last_activity_migrate();
 325      return array( 0, sprintf( $statement, __( 'Complete!', 'buddypress' ) ) );
 326  }
 327  
 328  /**
 329   * Assemble admin notices relating success/failure of repair processes.
 330   *
 331   * @since 2.0.0
 332   *
 333   * @param string      $message Feedback message.
 334   * @param string|bool $class   Unused.
 335   * @return false|Closure
 336   */
 337  function bp_admin_tools_feedback( $message, $class = false ) {
 338      if ( is_string( $message ) ) {
 339          $message = '<p>' . $message . '</p>';
 340          $class = $class ? $class : 'updated';
 341      } elseif ( is_wp_error( $message ) ) {
 342          $errors = $message->get_error_messages();
 343  
 344          switch ( count( $errors ) ) {
 345              case 0:
 346                  return false;
 347  
 348              case 1:
 349                  $message = '<p>' . $errors[0] . '</p>';
 350                  break;
 351  
 352              default:
 353                  $message = '<ul>' . "\n\t" . '<li>' . implode( '</li>' . "\n\t" . '<li>', $errors ) . '</li>' . "\n" . '</ul>';
 354                  break;
 355          }
 356  
 357          $class = $class ? $class : 'error';
 358      } else {
 359          return false;
 360      }
 361  
 362      $message = '<div id="message" class="' . esc_attr( $class ) . '">' . $message . '</div>';
 363      $message = str_replace( "'", "\'", $message );
 364      $lambda  = function() use ( $message ) { echo $message; };
 365  
 366      add_action( bp_core_do_network_admin() ? 'network_admin_notices' : 'admin_notices', $lambda );
 367  
 368      return $lambda;
 369  }
 370  
 371  /**
 372   * Render the Available Tools page.
 373   *
 374   * We register this page on Network Admin as a top-level home for our
 375   * BuddyPress tools. This displays the default content.
 376   *
 377   * @since 2.0.0
 378   */
 379  function bp_core_admin_available_tools_page() {
 380      ?>
 381      <div class="wrap">
 382          <h1><?php esc_attr_e( 'Tools', 'buddypress' ) ?></h1>
 383  
 384          <?php
 385  
 386          /**
 387           * Fires inside the markup used to display the Available Tools page.
 388           *
 389           * @since 2.0.0
 390           */
 391          do_action( 'bp_network_tool_box' ); ?>
 392  
 393      </div>
 394      <?php
 395  }
 396  
 397  /**
 398   * Render an introduction of BuddyPress tools on Available Tools page.
 399   *
 400   * @since 2.0.0
 401   */
 402  function bp_core_admin_available_tools_intro() {
 403      $query_arg = array(
 404          'page' => 'bp-tools'
 405      );
 406  
 407      $page = bp_core_do_network_admin() ? 'admin.php' : 'tools.php' ;
 408      $url  = add_query_arg( $query_arg, bp_get_admin_url( $page ) );
 409      ?>
 410      <div class="card tool-box">
 411          <h2><?php esc_html_e( 'BuddyPress Tools', 'buddypress' ) ?></h2>
 412          <p>
 413              <?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' ); ?>
 414              <?php
 415              printf(
 416                  /* translators: %s: the link to the BuddyPress repair tools */
 417                  esc_html_x( 'Use the %s to repair these relationships.', 'buddypress tools intro', 'buddypress' ),
 418                  '<a href="' . esc_url( $url ) . '">' . esc_html__( 'BuddyPress Tools', 'buddypress' ) . '</a>'
 419              );
 420              ?>
 421          </p>
 422      </div>
 423      <?php
 424  }
 425  
 426  /**
 427   * Delete emails and restore from defaults.
 428   *
 429   * @since 2.5.0
 430   *
 431   * @return array
 432   */
 433  function bp_admin_reinstall_emails() {
 434      $switched = false;
 435  
 436      // Switch to the root blog, where the email posts live.
 437      if ( ! bp_is_root_blog() ) {
 438          switch_to_blog( bp_get_root_blog_id() );
 439          bp_register_taxonomies();
 440  
 441          $switched = true;
 442      }
 443  
 444      $emails = get_posts( array(
 445          'fields'           => 'ids',
 446          'post_status'      => 'publish',
 447          'post_type'        => bp_get_email_post_type(),
 448          'posts_per_page'   => 200,
 449          'suppress_filters' => false,
 450      ) );
 451  
 452      if ( $emails ) {
 453          foreach ( $emails as $email_id ) {
 454              wp_trash_post( $email_id );
 455          }
 456      }
 457  
 458      // Make sure we have no orphaned email type terms.
 459      $email_types = get_terms( bp_get_email_tax_type(), array(
 460          'fields'                 => 'ids',
 461          'hide_empty'             => false,
 462          'update_term_meta_cache' => false,
 463      ) );
 464  
 465      if ( $email_types ) {
 466          foreach ( $email_types as $term_id ) {
 467              wp_delete_term( (int) $term_id, bp_get_email_tax_type() );
 468          }
 469      }
 470  
 471      require_once( buddypress()->plugin_dir . '/bp-core/admin/bp-core-admin-schema.php' );
 472      bp_core_install_emails();
 473  
 474      if ( $switched ) {
 475          restore_current_blog();
 476      }
 477  
 478      return array( 0, __( 'Emails have been successfully reinstalled.', 'buddypress' ) );
 479  }
 480  
 481  /**
 482   * Add notice on the "Tools > BuddyPress" page if more sites need recording.
 483   *
 484   * This notice only shows up in the network admin dashboard.
 485   *
 486   * @since 2.6.0
 487   */
 488  function bp_core_admin_notice_repopulate_blogs_resume() {
 489      $screen = get_current_screen();
 490      if ( 'tools_page_bp-tools-network' !== $screen->id ) {
 491          return;
 492      }
 493  
 494      if ( '' === bp_get_option( '_bp_record_blogs_offset' ) ) {
 495          return;
 496      }
 497  
 498      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>';
 499  }
 500  add_action( 'network_admin_notices', 'bp_core_admin_notice_repopulate_blogs_resume' );
 501  
 502  /**
 503   * Add BuddyPress debug info to the WordPress Site Health info screen.
 504   *
 505   * @since 5.0.0
 506   *
 507   * @param  array $debug_info The Site's debug info.
 508   * @return array             The Site's debug info, including the BuddyPress specific ones.
 509   */
 510  function bp_core_admin_debug_information( $debug_info = array() ) {
 511      global $wp_settings_fields;
 512      $active_components = array_intersect_key( bp_core_get_components(), buddypress()->active_components );
 513      $bp_settings       = array();
 514  
 515      foreach ( $wp_settings_fields['buddypress'] as $section => $settings ) {
 516          $prefix       = '';
 517          $component_id = str_replace( 'bp_', '', $section );
 518  
 519          if ( isset( $active_components[ $component_id ]['title'] ) ) {
 520              $prefix = $active_components[ $component_id ]['title'] .': ';
 521          }
 522  
 523          foreach( $settings as $bp_setting ) {
 524              $reverse = (
 525                  strpos( $bp_setting['id'], 'hide' ) !== false ||
 526                  strpos( $bp_setting['id'], 'restrict' ) !== false ||
 527                  strpos( $bp_setting['id'], 'disable' ) !== false
 528              );
 529  
 530              if ( ! isset( $bp_setting['id'] ) || '_bp_theme_package_id' === $bp_setting['id'] ) {
 531                  continue;
 532              }
 533  
 534              $bp_setting_value = bp_get_option( $bp_setting['id'] );
 535              if ( '0' === $bp_setting_value || '1' === $bp_setting_value ) {
 536                  if ( ( $reverse && '0' === $bp_setting_value ) || ( ! $reverse && '1' === $bp_setting_value ) ) {
 537                      $bp_setting_value = __( 'Yes', 'buddypress' );
 538                  } else {
 539                      $bp_setting_value = __( 'No', 'buddypress' );
 540                  }
 541              }
 542  
 543              // Make sure to show the setting is reversed when site info is copied to clipboard.
 544              $bp_settings_id = $bp_setting['id'];
 545              if ( $reverse ) {
 546                  $bp_settings_id = '! ' . $bp_settings_id;
 547              }
 548  
 549              $bp_settings[ $bp_settings_id ] = array(
 550                  'label' => $prefix . $bp_setting['title'],
 551                  'value' => $bp_setting_value,
 552              );
 553          }
 554      }
 555  
 556      $debug_info['buddypress'] = array(
 557          'label'  => __( 'BuddyPress', 'buddypress' ),
 558          'fields' => array_merge(
 559              array(
 560                  'version' => array(
 561                      'label' => __( 'Version', 'buddypress' ),
 562                      'value' => bp_get_version(),
 563                  ),
 564                  'active_components' => array(
 565                      'label' => __( 'Active components', 'buddypress' ),
 566                      'value' => implode( wp_list_pluck( $active_components, 'title' ), ', ' ),
 567                  ),
 568                  'template_pack' => array(
 569                      'label' => __( 'Active template pack', 'buddypress' ),
 570                      'value' => bp_get_theme_compat_name() . ' ' . bp_get_theme_compat_version(),
 571                  ),
 572              ),
 573              $bp_settings
 574          )
 575      );
 576  
 577      return $debug_info;
 578  }
 579  add_filter( 'debug_information', 'bp_core_admin_debug_information' );


Generated: Mon Apr 6 01:01:29 2020 Cross-referenced by PHPXref 0.7.1