[ Index ]

PHP Cross Reference of BBPress

title

Body

[close]

/src/includes/admin/tools/ -> upgrade.php (source)

   1  <?php
   2  
   3  /**
   4   * bbPress Admin Upgrade Functions
   5   *
   6   * @package bbPress
   7   * @subpackage Administration
   8   */
   9  
  10  // Exit if accessed directly
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * Admin repair page
  15   *
  16   * @since 2.6.0 bbPress (r6278)
  17   *
  18   */
  19  function bbp_admin_upgrade_page() {
  20  
  21      // Get the registered upgrade tools
  22      $tools = bbp_admin_repair_list( 'upgrade' ); ?>
  23  
  24      <div class="wrap">
  25          <h1 class="wp-heading-inline"><?php esc_html_e( 'Forum Tools', 'bbpress' ); ?></h1>
  26          <hr class="wp-header-end">
  27          <h2 class="nav-tab-wrapper"><?php bbp_tools_admin_tabs( 'bbp-upgrade' ); ?></h2>
  28  
  29          <p><?php esc_html_e( 'As bbPress improves, occasionally database upgrades are required but some forums are too large to upgrade automatically. Use the tools below to manually run upgrade routines.', 'bbpress' ); ?></p>
  30          <p class="description"><?php esc_html_e( 'Some of these tools create substantial database overhead. Use caution when running more than 1 upgrade at a time.', 'bbpress' ); ?></p>
  31  
  32          <?php bbp_admin_repair_tool_overhead_filters(); ?>
  33  
  34          <form class="settings" method="get" action="">
  35  
  36              <?php bbp_admin_repair_list_search_form(); ?>
  37  
  38              <input type="hidden" name="page" value="bbp-upgrade" />
  39              <?php wp_nonce_field( 'bbpress-do-counts' ); ?>
  40  
  41              <div class="tablenav top">
  42                  <div class="alignleft actions bulkactions">
  43                      <label for="bulk-action-selector-top" class="screen-reader-text"><?php esc_html_e( 'Select bulk action', 'bbpress' ); ?></label>
  44                      <select name="action" id="bulk-action-selector-top">
  45                          <option value="" selected="selected"><?php esc_html_e( 'Bulk Actions', 'bbpress' ); ?></option>
  46                          <option value="run" class="hide-if-no-js"><?php esc_html_e( 'Run', 'bbpress' ); ?></option>
  47                      </select>
  48                      <input type="submit" id="doaction" class="button action" value="<?php esc_attr_e( 'Apply', 'bbpress' ); ?>">
  49                  </div>
  50                  <div class="alignleft actions">
  51  
  52                      <?php bbp_admin_repair_list_components_filter(); ?>
  53  
  54                      <?php bbp_admin_repair_list_versions_filter(); ?>
  55  
  56                      <input type="submit" name="filter_action" id="components-submit" class="button" value="<?php esc_html_e( 'Filter', 'bbpress' ); ?>">
  57                  </div>
  58                  <br class="clear">
  59              </div>
  60              <table class="wp-list-table widefat striped posts">
  61                  <thead>
  62                      <tr>
  63                          <td id="cb" class="manage-column column-cb check-column">
  64                              <label class="screen-reader-text" for="cb-select-all-1">
  65                                  <?php esc_html_e( 'Select All', 'bbpress' ); ?>
  66                              </label>
  67                              <input id="cb-select-all-1" type="checkbox">
  68                          </td>
  69                          <th scope="col" id="description" class="manage-column column-primary column-description"><?php esc_html_e( 'Description', 'bbpress' ); ?></th>
  70                          <th scope="col" id="version" class="manage-column column-version"><?php esc_html_e( 'Version', 'bbpress' ); ?></th>
  71                          <th scope="col" id="components" class="manage-column column-components"><?php esc_html_e( 'Components', 'bbpress' ); ?></th>
  72                          <th scope="col" id="overhead" class="manage-column column-overhead"><?php esc_html_e( 'Overhead', 'bbpress' ); ?></th>
  73                      </tr>
  74                  </thead>
  75  
  76                  <tbody id="the-list">
  77  
  78                      <?php if ( ! empty( $tools ) ) : ?>
  79  
  80                          <?php foreach ( $tools as $item ) : ?>
  81  
  82                              <tr id="bbp-repair-tools" class="inactive">
  83                                  <th scope="row" class="check-column">
  84                                      <label class="screen-reader-text" for="<?php echo esc_attr( str_replace( '_', '-', $item['id'] ) ); ?>"></label>
  85                                      <input type="checkbox" name="checked[]" value="<?php echo esc_attr( $item['id'] ); ?>" id="<?php echo esc_attr( str_replace( '_', '-', $item['id'] ) ); ?>">
  86                                  </th>
  87                                  <td class="bbp-tool-title column-primary column-description" data-colname="<?php esc_html_e( 'Description', 'bbpress' ); ?>">
  88                                      <strong><?php echo esc_html( $item['title'] ); ?></strong><?php
  89  
  90                                          // Optional description
  91                                          if ( ! empty( $item['description'] ) ) :
  92                                              echo esc_html( $item['description'] );
  93                                          endif;
  94  
  95                                      ?><div class="row-actions hide-if-no-js">
  96                                          <span class="run">
  97                                              <a href="<?php bbp_admin_repair_tool_run_url( $item ); ?>" aria-label="<?php printf( esc_html__( 'Run %s', 'bbpress' ), $item['title'] ); ?>" id="<?php echo esc_attr( $item['id'] ); ?>" ><?php esc_html_e( 'Run', 'bbpress' ); ?></a>
  98                                          </span>
  99                                      </div>
 100                                      <button type="button" class="toggle-row">
 101                                          <span class="screen-reader-text"><?php esc_html_e( 'Show more details', 'bbpress' ); ?></span>
 102                                      </button>
 103                                  </td>
 104                                  <td class="column-version desc" data-colname="<?php esc_html_e( 'Version', 'bbpress' ); ?>">
 105                                      <div class="bbp-tool-version">
 106  
 107                                          <?php echo implode( ', ', bbp_get_admin_repair_tool_version( $item ) ); ?>
 108  
 109                                      </div>
 110                                  </td>
 111                                  <td class="column-components desc" data-colname="<?php esc_html_e( 'Components', 'bbpress' ); ?>">
 112                                      <div class="bbp-tool-components">
 113  
 114                                          <?php echo implode( ', ', bbp_get_admin_repair_tool_components( $item ) ); ?>
 115  
 116                                      </div>
 117                                  </td>
 118                                  <td class="column-overhead desc" data-colname="<?php esc_html_e( 'Overhead', 'bbpress' ); ?>">
 119                                      <div class="bbp-tool-overhead">
 120  
 121                                          <?php echo implode( ', ', bbp_get_admin_repair_tool_overhead( $item ) ); ?>
 122  
 123                                      </div>
 124                                  </td>
 125                              </tr>
 126  
 127                          <?php endforeach; ?>
 128  
 129                      <?php else : ?>
 130  
 131                          <tr>
 132                              <td colspan="4">
 133                                  <?php esc_html_e( 'No repair tools match this criteria.', 'bbpress' ); ?>
 134                              </td>
 135                          </tr>
 136  
 137                      <?php endif; ?>
 138  
 139                  </tbody>
 140                  <tfoot>
 141                      <tr>
 142                          <td class="manage-column column-cb check-column">
 143                              <label class="screen-reader-text" for="cb-select-all-2">
 144                                  <?php esc_html_e( 'Select All', 'bbpress' ); ?>
 145                              </label>
 146                              <input id="cb-select-all-2" type="checkbox">
 147                          </td>
 148                          <th scope="col" class="manage-column column-primary column-description"><?php esc_html_e( 'Description', 'bbpress' ); ?></th>
 149                          <th scope="col" class="manage-column column-version"><?php esc_html_e( 'Version', 'bbpress' ); ?></th>
 150                          <th scope="col" class="manage-column column-components"><?php esc_html_e( 'Components', 'bbpress' ); ?></th>
 151                          <th scope="col" class="manage-column column-overhead"><?php esc_html_e( 'Overhead', 'bbpress' ); ?></th>
 152                      </tr>
 153                  </tfoot>
 154              </table>
 155              <div class="tablenav bottom">
 156                  <div class="alignleft actions bulkactions">
 157                      <label for="bulk-action-selector-bottom" class="screen-reader-text"><?php esc_html_e( 'Select bulk action', 'bbpress' ); ?></label>
 158                      <select name="action2" id="bulk-action-selector-bottom">
 159                          <option value="" selected="selected"><?php esc_html_e( 'Bulk Actions', 'bbpress' ); ?></option>
 160                          <option value="run" class="hide-if-no-js"><?php esc_html_e( 'Run', 'bbpress' ); ?></option>
 161                      </select>
 162                      <input type="submit" id="doaction2" class="button action" value="<?php esc_attr_e( 'Apply', 'bbpress' ); ?>">
 163                  </div>
 164              </div>
 165          </form>
 166      </div>
 167  
 168  <?php
 169  }
 170  
 171  /**
 172   * Upgrade user engagements for bbPress 2.6 and higher
 173   *
 174   * @since 2.6.0 bbPress (r6320)
 175   *
 176   * @return array An array of the status code and the message
 177   */
 178  function bbp_admin_upgrade_user_engagements() {
 179  
 180      // Define variables
 181      $bbp_db    = bbp_db();
 182      $statement = esc_html__( 'Upgrading user engagements&hellip; %s', 'bbpress' );
 183      $result    = esc_html__( 'No engagements to upgrade.',            'bbpress' );
 184  
 185      // Delete previous engagements
 186      $sql_delete = "DELETE FROM {$bbp_db->postmeta} WHERE meta_key = '_bbp_engagement'";
 187      if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
 188          return array( 1, sprintf( $statement, $result ) );
 189      }
 190  
 191      // Post types and statuses
 192      $tpt = bbp_get_topic_post_type();
 193      $rpt = bbp_get_reply_post_type();
 194      $pps = bbp_get_public_status_id();
 195      $cps = bbp_get_closed_status_id();
 196      $sql = "INSERT INTO {$bbp_db->postmeta} (post_id, meta_key, meta_value) (
 197              SELECT postmeta.meta_value, '_bbp_engagement', posts.post_author
 198                  FROM {$bbp_db->posts} AS posts
 199                  LEFT JOIN {$bbp_db->postmeta} AS postmeta
 200                      ON posts.ID = postmeta.post_id
 201                      AND postmeta.meta_key = '_bbp_topic_id'
 202                  WHERE posts.post_type IN (%s, %s)
 203                      AND posts.post_status IN (%s, %s)
 204                  GROUP BY postmeta.meta_value, posts.post_author)";
 205  
 206      // Run the big query
 207      $prepare     = $bbp_db->prepare( $sql, $tpt, $rpt, $pps, $cps );
 208      $engagements = $bbp_db->query( $prepare );
 209  
 210      // Bail if no closed topics found
 211      if ( empty( $engagements ) || is_wp_error( $engagements ) ) {
 212          return array( 1, sprintf( $statement, $result ) );
 213      }
 214  
 215      // Complete results
 216      $result = sprintf( _n( 'Complete! %d engagements upgraded.', 'Complete! %d engagements upgraded.', $engagements, 'bbpress' ), $engagements );
 217  
 218      return array( 0, sprintf( $statement, $result ) );
 219  }
 220  
 221  /**
 222   * Upgrade group forum ID mappings after a bbPress 1.x to bbPress 2.x conversion
 223   *
 224   * Previously named: bbp_admin_repair_group_forum_relationships()
 225   *
 226   * @since 2.6.0 bbPress (r4395)
 227   *
 228   * @return If a wp_error() occurs and no converted forums are found
 229   */
 230  function bbp_admin_upgrade_group_forum_relationships() {
 231  
 232      // Define variables
 233      $bbp_db    = bbp_db();
 234      $statement = esc_html__( 'Upgrading BuddyPress group-forum relationships&hellip; %s', 'bbpress' );
 235      $g_count   = 0;
 236      $f_count   = 0;
 237      $s_count   = 0;
 238  
 239      // Copy the BuddyPress filter here, incase BuddyPress is not active
 240      $prefix            = apply_filters( 'bp_core_get_table_prefix', $bbp_db->base_prefix );
 241      $groups_table      = $prefix . 'bp_groups';
 242      $groups_meta_table = $prefix . 'bp_groups_groupmeta';
 243  
 244      // Get the converted forum IDs
 245      $forum_ids = $bbp_db->query( "SELECT `forum`.`ID`, `forummeta`.`meta_value`
 246                                  FROM `{$bbp_db->posts}` AS `forum`
 247                                      LEFT JOIN `{$bbp_db->postmeta}` AS `forummeta`
 248                                          ON `forum`.`ID` = `forummeta`.`post_id`
 249                                          AND `forummeta`.`meta_key` = '_bbp_old_forum_id'
 250                                  WHERE `forum`.`post_type` = '" . bbp_get_forum_post_type() . "'
 251                                  GROUP BY `forum`.`ID`" );
 252  
 253      // Bail if forum IDs returned an error
 254      if ( is_wp_error( $forum_ids ) || empty( $bbp_db->last_result ) ) {
 255          return array( 2, sprintf( $statement, esc_html__( 'Failed!', 'bbpress' ) ) );
 256      }
 257  
 258      // Stash the last results
 259      $results = $bbp_db->last_result;
 260  
 261      // Update each group forum
 262      foreach ( $results as $group_forums ) {
 263  
 264          // Only update if is a converted forum
 265          if ( empty( $group_forums->meta_value ) ) {
 266              continue;
 267          }
 268  
 269          // Attempt to update group meta
 270          $updated = $bbp_db->query( "UPDATE `{$groups_meta_table}` SET `meta_value` = '{$group_forums->ID}' WHERE `meta_key` = 'forum_id' AND `meta_value` = '{$group_forums->meta_value}'" );
 271  
 272          // Bump the count
 273          if ( ! empty( $updated ) && ! is_wp_error( $updated ) ) {
 274              ++$g_count;
 275          }
 276  
 277          // Update group to forum relationship data
 278          $group_id = (int) $bbp_db->get_var( "SELECT `group_id` FROM `{$groups_meta_table}` WHERE `meta_key` = 'forum_id' AND `meta_value` = '{$group_forums->ID}'" );
 279          if ( ! empty( $group_id ) ) {
 280  
 281              // Update the group to forum meta connection in forums
 282              update_post_meta( $group_forums->ID, '_bbp_group_ids', array( $group_id ) );
 283  
 284              // Get the group status
 285              $group_status = $bbp_db->get_var( "SELECT `status` FROM `{$groups_table}` WHERE `id` = '{$group_id}'" );
 286  
 287              // Sync up forum visibility based on group status
 288              switch ( $group_status ) {
 289  
 290                  // Public groups have public forums
 291                  case 'public' :
 292                      bbp_publicize_forum( $group_forums->ID );
 293  
 294                      // Bump the count for output later
 295                      ++$s_count;
 296                      break;
 297  
 298                  // Private/hidden groups have hidden forums
 299                  case 'private' :
 300                  case 'hidden'  :
 301                      bbp_hide_forum( $group_forums->ID );
 302  
 303                      // Bump the count for output later
 304                      ++$s_count;
 305                      break;
 306              }
 307  
 308              // Bump the count for output later
 309              ++$f_count;
 310          }
 311      }
 312  
 313      // Make some logical guesses at the old group root forum
 314      if ( function_exists( 'bp_forums_parent_forum_id' ) ) {
 315          $old_default_forum_id = bp_forums_parent_forum_id();
 316      } elseif ( defined( 'BP_FORUMS_PARENT_FORUM_ID' ) ) {
 317          $old_default_forum_id = (int) BP_FORUMS_PARENT_FORUM_ID;
 318      } else {
 319          $old_default_forum_id = 1;
 320      }
 321  
 322      // Try to get the group root forum
 323      $posts = get_posts( array(
 324          'post_type'   => bbp_get_forum_post_type(),
 325          'meta_key'    => '_bbp_old_forum_id',
 326          'meta_type'   => 'NUMERIC',
 327          'meta_value'  => $old_default_forum_id,
 328          'numberposts' => 1
 329      ) );
 330  
 331      // Found the group root forum
 332      if ( ! empty( $posts ) ) {
 333  
 334          // Rename 'Default Forum'  since it's now visible in sitewide forums
 335          if ( 'Default Forum' === $posts[0]->post_title ) {
 336              wp_update_post( array(
 337                  'ID'         => $posts[0]->ID,
 338                  'post_title' => esc_html__( 'Group Forums', 'bbpress' ),
 339                  'post_name'  => esc_html__( 'group-forums', 'bbpress' ),
 340              ) );
 341          }
 342  
 343          // Update the group forums root metadata
 344          update_option( '_bbp_group_forums_root_id', $posts[0]->ID );
 345      }
 346  
 347      // Remove old bbPress 1.1 roles (BuddyPress)
 348      remove_role( 'member'    );
 349      remove_role( 'inactive'  );
 350      remove_role( 'blocked'   );
 351      remove_role( 'moderator' );
 352      remove_role( 'keymaster' );
 353  
 354      // Complete results
 355      $result = sprintf( esc_html__( 'Complete! %s groups updated; %s forums updated; %s forum statuses synced.', 'bbpress' ), bbp_number_format( $g_count ), bbp_number_format( $f_count ), bbp_number_format( $s_count ) );
 356      return array( 0, sprintf( $statement, $result ) );
 357  }
 358  
 359  /**
 360   * Upgrade user favorites for bbPress 2.6 and higher
 361   *
 362   * @since 2.6.0 bbPress (r6174)
 363   *
 364   * @return array An array of the status code and the message
 365   */
 366  function bbp_admin_upgrade_user_favorites() {
 367  
 368      // Define variables
 369      $bbp_db    = bbp_db();
 370      $statement = esc_html__( 'Upgrading user favorites&hellip; %s', 'bbpress' );
 371      $result    = esc_html__( 'No favorites to upgrade.',            'bbpress' );
 372      $total     = 0;
 373      $old_key   = $bbp_db->prefix . '_bbp_favorites';
 374      $new_key   = '_bbp_favorite';
 375  
 376      // Results
 377      $query     = "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s";
 378      $prepare   = $bbp_db->prepare( $query, $old_key );
 379      $favs      = $bbp_db->get_results( $prepare );
 380  
 381      // Bail if no closed topics found
 382      if ( empty( $favs ) || is_wp_error( $favs ) ) {
 383          return array( 1, sprintf( $statement, $result ) );
 384      }
 385  
 386      // Loop through each user's favorites
 387      foreach ( $favs as $meta ) {
 388  
 389          // Get post IDs
 390          $post_ids = explode( ',', $meta->meta_value );
 391  
 392          // Add user ID to all favorited posts
 393          foreach ( $post_ids as $post_id ) {
 394  
 395              // Skip if already exists
 396              if ( $bbp_db->get_var( $bbp_db->prepare( "SELECT COUNT(*) FROM {$bbp_db->postmeta} WHERE post_id = %d AND meta_key = %s AND meta_value = %d", $post_id, $new_key, $meta->user_id ) ) ) {
 397                  continue;
 398              }
 399  
 400              // Add the post meta
 401              $added = add_post_meta( $post_id, $new_key, $meta->user_id, false );
 402  
 403              // Bump counts if successfully added
 404              if ( ! empty( $added ) ) {
 405                  ++$total;
 406              }
 407          }
 408      }
 409  
 410      // Cleanup
 411      unset( $favs, $added, $post_ids );
 412  
 413      // Complete results
 414      $result = sprintf( _n( 'Complete! %d favorite upgraded.', 'Complete! %d favorites upgraded.', $total, 'bbpress' ), $total );
 415  
 416      return array( 0, sprintf( $statement, $result ) );
 417  }
 418  
 419  /**
 420   * Upgrade user topic subscriptions for bbPress 2.6 and higher
 421   *
 422   * @since 2.6.0 bbPress (r6174)
 423   *
 424   * @return array An array of the status code and the message
 425   */
 426  function bbp_admin_upgrade_user_topic_subscriptions() {
 427  
 428      // Define variables
 429      $bbp_db    = bbp_db();
 430      $statement = esc_html__( 'Upgrading user topic subscriptions&hellip; %s', 'bbpress' );
 431      $result    = esc_html__( 'No topic subscriptions to upgrade.',            'bbpress' );
 432      $total     = 0;
 433      $old_key   = $bbp_db->prefix . '_bbp_subscriptions';
 434      $new_key   = '_bbp_subscription';
 435  
 436      // Results
 437      $query     = "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s ORDER BY user_id";
 438      $prepare   = $bbp_db->prepare( $query, $old_key );
 439      $subs      = $bbp_db->get_results( $prepare );
 440  
 441      // Bail if no topic subscriptions found
 442      if ( empty( $subs ) || is_wp_error( $subs ) ) {
 443          return array( 1, sprintf( $statement, $result ) );
 444      }
 445  
 446      // Loop through each user's topic subscriptions
 447      foreach ( $subs as $meta ) {
 448  
 449          // Get post IDs
 450          $post_ids = explode( ',', $meta->meta_value );
 451  
 452          // Add user ID to all subscribed topics
 453          foreach ( $post_ids as $post_id ) {
 454  
 455              // Skip if already exists
 456              if ( $bbp_db->get_var( $bbp_db->prepare( "SELECT COUNT(*) FROM {$bbp_db->postmeta} WHERE post_id = %d AND meta_key = %s AND meta_value = %d", $post_id, $new_key, $meta->user_id ) ) ) {
 457                  continue;
 458              }
 459  
 460              // Add the post meta
 461              $added = add_post_meta( $post_id, $new_key, $meta->user_id, false );
 462  
 463              // Bump counts if successfully added
 464              if ( ! empty( $added ) ) {
 465                  ++$total;
 466              }
 467          }
 468      }
 469  
 470      // Cleanup
 471      unset( $subs, $added, $post_ids );
 472  
 473      // Complete results
 474      $result = sprintf( _n( 'Complete! %d topic subscription upgraded.', 'Complete! %d topic subscriptions upgraded.', $total, 'bbpress' ), $total );
 475  
 476      return array( 0, sprintf( $statement, $result ) );
 477  }
 478  
 479  /**
 480   * Upgrade user forum subscriptions for bbPress 2.6 and higher
 481   *
 482   * @since 2.6.0 bbPress (r6193)
 483   *
 484   * @return array An array of the status code and the message
 485   */
 486  function bbp_admin_upgrade_user_forum_subscriptions() {
 487  
 488      // Define variables
 489      $bbp_db    = bbp_db();
 490      $statement = esc_html__( 'Upgrading user forum subscriptions&hellip; %s', 'bbpress' );
 491      $result    = esc_html__( 'No forum subscriptions to upgrade.',            'bbpress' );
 492      $total     = 0;
 493      $old_key   = $bbp_db->prefix . '_bbp_forum_subscriptions';
 494      $new_key   = '_bbp_subscription';
 495  
 496      // Results
 497      $query     = "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s ORDER BY user_id";
 498      $prepare   = $bbp_db->prepare( $query, $old_key );
 499      $subs      = $bbp_db->get_results( $prepare );
 500  
 501      // Bail if no forum subscriptions found
 502      if ( empty( $subs ) || is_wp_error( $subs ) ) {
 503          return array( 1, sprintf( $statement, $result ) );
 504      }
 505  
 506      // Loop through each user's forum subscriptions
 507      foreach ( $subs as $meta ) {
 508  
 509          // Get post IDs
 510          $post_ids = explode( ',', $meta->meta_value );
 511  
 512          // Add user ID to all subscribed forums
 513          foreach ( $post_ids as $post_id ) {
 514  
 515              // Skip if already exists
 516              if ( $bbp_db->get_var( $bbp_db->prepare( "SELECT COUNT(*) FROM {$bbp_db->postmeta} WHERE post_id = %d AND meta_key = %s AND meta_value = %d", $post_id, $new_key, $meta->user_id ) ) ) {
 517                  continue;
 518              }
 519  
 520              // Add the post meta
 521              $added = add_post_meta( $post_id, $new_key, $meta->user_id, false );
 522  
 523              // Bump counts if successfully added
 524              if ( ! empty( $added ) ) {
 525                  ++$total;
 526              }
 527          }
 528      }
 529  
 530      // Cleanup
 531      unset( $subs, $added, $post_ids );
 532  
 533      // Complete results
 534      $result = sprintf( _n( 'Complete! %d forum subscription upgraded.', 'Complete! %d forum subscriptions upgraded.', $total, 'bbpress' ), $total );
 535  
 536      return array( 0, sprintf( $statement, $result ) );
 537  }
 538  
 539  /**
 540   * Remove favorites data from user meta for bbPress 2.6 and higher
 541   *
 542   * @since 2.6.0 bbPress (r6281)
 543   *
 544   * @return array An array of the status code and the message
 545   */
 546  function bbp_admin_upgrade_remove_favorites_from_usermeta() {
 547  
 548      // Define variables
 549      $bbp_db    = bbp_db();
 550      $statement = esc_html__( 'Remove favorites from usermeta&hellip; %s', 'bbpress' );
 551      $result    = esc_html__( 'No favorites to remove.',                   'bbpress' );
 552      $total     = 0;
 553      $key       = $bbp_db->prefix . '_bbp_favorites';
 554  
 555      // Results
 556      $query     = "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s ORDER BY user_id";
 557      $prepare   = $bbp_db->prepare( $query, $key );
 558      $favs      = $bbp_db->get_results( $prepare );
 559  
 560      // Bail if no favorites found
 561      if ( empty( $favs ) || is_wp_error( $favs ) ) {
 562          return array( 1, sprintf( $statement, $result ) );
 563      }
 564  
 565      // Delete all user-meta with this key
 566      delete_metadata( 'user', false, $key, false, true );
 567      $total = count( $favs );
 568  
 569      // Complete results
 570      $result = sprintf( _n( 'Complete! %d favorites deleted.', 'Complete! %d favorites deleted.', $total, 'bbpress' ), $total );
 571  
 572      return array( 0, sprintf( $statement, $result ) );
 573  }
 574  
 575  /**
 576   * Remove topic subscriptions data from user meta for bbPress 2.6 and higher
 577   *
 578   * @since 2.6.0 bbPress (r6281)
 579   *
 580   * @return array An array of the status code and the message
 581   */
 582  function bbp_admin_upgrade_remove_topic_subscriptions_from_usermeta() {
 583  
 584      // Define variables
 585      $bbp_db    = bbp_db();
 586      $statement = esc_html__( 'Remove topic subscriptions from usermeta&hellip; %s', 'bbpress' );
 587      $result    = esc_html__( 'No topic subscriptions to remove.',                   'bbpress' );
 588      $total     = 0;
 589      $key       = $bbp_db->prefix . '_bbp_subscriptions';
 590  
 591      // Results
 592      $query     = "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s ORDER BY user_id";
 593      $prepare   = $bbp_db->prepare( $query, $key );
 594      $subs      = $bbp_db->get_results( $prepare );
 595  
 596      // Bail if no forum favorites found
 597      if ( empty( $subs ) || is_wp_error( $subs ) ) {
 598          return array( 1, sprintf( $statement, $result ) );
 599      }
 600  
 601      // Delete all user-meta with this key
 602      delete_metadata( 'user', false, $key, false, true );
 603      $total = count( $subs );
 604  
 605      // Complete results
 606      $result = sprintf( _n( 'Complete! %d topic subscription deleted.', 'Complete! %d topic subscriptions deleted.', $total, 'bbpress' ), $total );
 607  
 608      return array( 0, sprintf( $statement, $result ) );
 609  }
 610  
 611  /**
 612   * Remove topic subscriptions data from user meta for bbPress 2.6 and higher
 613   *
 614   * @since 2.6.0 bbPress (r6281)
 615   *
 616   * @return array An array of the status code and the message
 617   */
 618  function bbp_admin_upgrade_remove_forum_subscriptions_from_usermeta() {
 619  
 620      // Define variables
 621      $bbp_db    = bbp_db();
 622      $statement = esc_html__( 'Remove forum subscriptions from usermeta&hellip; %s', 'bbpress' );
 623      $result    = esc_html__( 'No forum subscriptions to remove.',                   'bbpress' );
 624      $total     = 0;
 625      $key       = $bbp_db->prefix . '_bbp_forum_subscriptions';
 626  
 627      // Query
 628      $query     = "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s ORDER BY user_id";
 629      $prepare   = $bbp_db->prepare( $query, $key );
 630      $subs      = $bbp_db->get_results( $prepare );
 631  
 632      // Bail if no forum favorites found
 633      if ( empty( $subs ) || is_wp_error( $subs ) ) {
 634          return array( 1, sprintf( $statement, $result ) );
 635      }
 636  
 637      // Delete all user-meta with this key
 638      delete_metadata( 'user', false, $key, false, true );
 639      $total = count( $subs );
 640  
 641      // Complete results
 642      $result = sprintf( _n( 'Complete! %d forum subscription deleted.', 'Complete! %d forum subscriptions deleted.', $total, 'bbpress' ), $total );
 643  
 644      return array( 0, sprintf( $statement, $result ) );
 645  }


Generated: Tue Jul 16 01:01:32 2019 Cross-referenced by PHPXref 0.7.1