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


Generated: Sat Dec 7 01:00:51 2024 Cross-referenced by PHPXref 0.7.1