[ Index ]

PHP Cross Reference of BBPress

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * bbPress Admin Tools Reset
   5   *
   6   * @package bbPress
   7   * @subpackage Administration
   8   */
   9  
  10  // Exit if accessed directly
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * Admin reset page
  15   *
  16   * @since 2.0.0 bbPress (r2613)
  17   *
  18   */
  19  function bbp_admin_reset_page() {
  20  ?>
  21  
  22      <div class="wrap">
  23          <h1 class="wp-heading-inline"><?php esc_html_e( 'Forum Tools', 'bbpress' ); ?></h1>
  24          <hr class="wp-header-end">
  25          <h2 class="nav-tab-wrapper"><?php bbp_tools_admin_tabs( 'bbp-reset' ); ?></h2>
  26          <p><?php esc_html_e( 'Revert your forums back to a brand new installation, as if bbPress were never installed. This process cannot be undone.', 'bbpress' ); ?></p>
  27  
  28          <form class="settings" method="post" action="">
  29              <table class="form-table">
  30                  <tbody>
  31                      <tr valign="top">
  32                          <th scope="row"><?php esc_html_e( 'The following data will be removed:', 'bbpress' ) ?></th>
  33                          <td>
  34                              <?php esc_html_e( 'All Forums',           'bbpress' ); ?><br />
  35                              <?php esc_html_e( 'All Topics',           'bbpress' ); ?><br />
  36                              <?php esc_html_e( 'All Replies',          'bbpress' ); ?><br />
  37                              <?php esc_html_e( 'All Topic Tags',       'bbpress' ); ?><br />
  38                              <?php esc_html_e( 'All Meta Data',        'bbpress' ); ?><br />
  39                              <?php esc_html_e( 'Forum Settings',       'bbpress' ); ?><br />
  40                              <?php esc_html_e( 'Forum Activity',       'bbpress' ); ?><br />
  41                              <?php esc_html_e( 'Forum User Roles',     'bbpress' ); ?><br />
  42                              <?php esc_html_e( 'Forum Moderators',     'bbpress' ); ?><br />
  43                              <?php esc_html_e( 'Importer Helper Data', 'bbpress' ); ?><br />
  44                          </td>
  45                      </tr>
  46                      <tr valign="top">
  47                          <th scope="row"><?php esc_html_e( 'Delete imported users?', 'bbpress' ); ?></th>
  48                          <td>
  49                              <fieldset>
  50                                  <legend class="screen-reader-text"><span><?php esc_html_e( "Say it ain't so!", 'bbpress' ); ?></span></legend>
  51                                  <label><input type="checkbox" class="checkbox" name="bbpress-delete-imported-users" id="bbpress-delete-imported-users" value="1" /> <?php esc_html_e( 'This option will delete all previously imported users, and cannot be undone.', 'bbpress' ); ?></label>
  52                                  <p class="description"><?php esc_html_e( 'Proceeding without this checked removes the meta-data necessary to delete these users later.', 'bbpress' ); ?></p>
  53                              </fieldset>
  54                          </td>
  55                      </tr>
  56                      <tr valign="top">
  57                          <th scope="row"><?php esc_html_e( 'Do you really want to do this?', 'bbpress' ); ?></th>
  58                          <td>
  59                              <fieldset>
  60                                  <legend class="screen-reader-text"><span><?php esc_html_e( "Say it ain't so!", 'bbpress' ); ?></span></legend>
  61                                  <label><input type="checkbox" class="checkbox" name="bbpress-are-you-sure" id="bbpress-are-you-sure" value="1" /> <?php esc_html_e( 'This process cannot be undone.', 'bbpress' ); ?></label>
  62                                  <p class="description"><?php esc_html_e( 'Backup your database before proceeding.', 'bbpress' ); ?></p>
  63                              </fieldset>
  64                          </td>
  65                      </tr>
  66                  </tbody>
  67              </table>
  68  
  69              <fieldset class="submit">
  70                  <input class="button-primary" type="submit" name="submit" value="<?php esc_attr_e( 'Reset bbPress', 'bbpress' ); ?>" />
  71                  <?php wp_nonce_field( 'bbpress-reset' ); ?>
  72              </fieldset>
  73          </form>
  74      </div>
  75  
  76  <?php
  77  }
  78  
  79  /**
  80   * Handle a bbPress admin area reset request.
  81   *
  82   * @since 2.0.0 bbPress (r2613)
  83   */
  84  function bbp_admin_reset_handler() {
  85  
  86      // Bail if not resetting.
  87      if ( ! bbp_is_post_request() || empty( $_POST['bbpress-are-you-sure'] ) ) {
  88          return;
  89      }
  90  
  91      // Only keymasters can proceed.
  92      if ( ! bbp_is_user_keymaster() ) {
  93          return;
  94      }
  95  
  96      // Bail if not referred from resetter
  97      check_admin_referer( 'bbpress-reset' );
  98  
  99      // Reset all of bbPress
 100      bbp_admin_reset_database();
 101  }
 102  
 103  /**
 104   * Wrapper for determining admin reset query feedback presented to a user.
 105   *
 106   * @since 2.6.0 bbPress (r6758)
 107   *
 108   * @param array $args Array of query, message, and possible responses
 109   *
 110   * @return string
 111   */
 112  function bbp_admin_reset_query_feedback( $args = array() ) {
 113      static $defaults = null;
 114  
 115      // Only set defaults one time to avoid hitting the GetText API repeatedly
 116      if ( null === $defaults ) {
 117          $defaults = array(
 118              'query'     => '',
 119              'message'   => esc_html__( 'Resetting&hellip;', 'bbpress' ),
 120              'responses' => array(
 121                  'success' => esc_html__( 'Success!', 'bbpress' ),
 122                  'failure' => esc_html__( 'Failed!',  'bbpress' ),
 123                  'skipped' => esc_html__( 'Skipped.', 'bbpress' )
 124              )
 125          );
 126      }
 127  
 128      // Parse arguments
 129      $r = bbp_parse_args( $args, $defaults, 'admin_reset_query_feedback' );
 130  
 131      // Success/Failure based on query error
 132      if ( ! empty( $r['query'] ) ) {
 133          $query  = bbp_db()->query( $r['query'] );
 134          $result = ! is_wp_error( $query )
 135              ? $r['responses']['success']
 136              : $r['responses']['failure'];
 137  
 138      // Skip if empty
 139      } else {
 140          $result = $r['responses']['skipped'];
 141      }
 142  
 143      // Return feedback
 144      return sprintf( $r['message'], $result );
 145  }
 146  
 147  /**
 148   * Perform a bbPress database reset.
 149   *
 150   * @since 2.6.0 bbPress
 151   */
 152  function bbp_admin_reset_database() {
 153  
 154      // Define variables.
 155      $messages = array();
 156      $sql_meta = array();
 157      $bbp_db   = bbp_db();
 158  
 159      // Flush the whole cache; things are about to get ugly.
 160      wp_cache_flush();
 161  
 162      /** Posts *****************************************************************/
 163  
 164      // Post types and status.
 165      $fpt = bbp_get_forum_post_type();
 166      $tpt = bbp_get_topic_post_type();
 167      $rpt = bbp_get_reply_post_type();
 168  
 169      // Get post IDs
 170      $sql_posts = $bbp_db->get_results( "SELECT `ID` FROM `{$bbp_db->posts}` WHERE `post_type` IN ('{$fpt}', '{$tpt}', '{$rpt}')", OBJECT_K );
 171      if ( ! empty( $sql_posts ) ) {
 172  
 173          // Meta data
 174          foreach ( $sql_posts as $key => $value ) {
 175              $sql_meta[] = $key;
 176          }
 177          $sql_meta = implode( "', '", $sql_meta );
 178  
 179          // Delete posts
 180          $messages[] = bbp_admin_reset_query_feedback( array(
 181              'query'   => "DELETE FROM `{$bbp_db->posts}` WHERE `post_type` IN ('{$fpt}', '{$tpt}', '{$rpt}')",
 182              'message' => esc_html__( 'Removing Forums, Topics, and Replies&hellip; %s', 'bbpress' )
 183          ) );
 184  
 185          /** Post Meta *********************************************************/
 186  
 187          if ( ! empty( $sql_posts ) ) {
 188              $messages[] = bbp_admin_reset_query_feedback( array(
 189                  'query'   => "DELETE FROM `{$bbp_db->postmeta}` WHERE `post_id` IN ('{$sql_meta}')",
 190                  'message' => esc_html__( 'Removing Forum, Topic, and Reply Meta Data&hellip; %s', 'bbpress' )
 191              ) );
 192          }
 193  
 194          /** Post Revisions ****************************************************/
 195  
 196          if ( ! empty( $sql_posts ) ) {
 197              $messages[] = bbp_admin_reset_query_feedback( array(
 198                  'query'   => "DELETE FROM `{$bbp_db->posts}` WHERE `post_parent` IN ('{$sql_meta}') AND `post_type` = 'revision'",
 199                  'message' => esc_html__( 'Removing Revision Data&hellip; %s', 'bbpress' )
 200              ) );
 201          }
 202      }
 203  
 204      /** Topic Tags ************************************************************/
 205  
 206      $messages[] = bbp_admin_reset_query_feedback( array(
 207          'query'   => "DELETE a,b,c FROM `{$bbp_db->terms}` AS a LEFT JOIN `{$bbp_db->term_taxonomy}` AS c ON a.term_id = c.term_id LEFT JOIN `{$bbp_db->term_relationships}` AS b ON b.term_taxonomy_id = c.term_taxonomy_id WHERE c.taxonomy = 'topic-tag'",
 208          'message' => esc_html__( 'Deleting Topic Tags&hellip; %s', 'bbpress' )
 209      ) );
 210  
 211      /** User ******************************************************************/
 212  
 213      // First, if we're deleting previously imported users, delete them now
 214      if ( ! empty( $_POST['bbpress-delete-imported-users'] ) ) {
 215          $sql_users = $bbp_db->get_results( "SELECT `user_id` FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '_bbp_old_user_id'", OBJECT_K );
 216  
 217          if ( ! empty( $sql_users ) ) {
 218              $sql_meta = array();
 219              foreach ( $sql_users as $key => $value ) {
 220                  $sql_meta[] = $key;
 221              }
 222  
 223              // Users
 224              $sql_meta   = implode( "', '", $sql_meta );
 225              $messages[] = bbp_admin_reset_query_feedback( array(
 226                  'query'   => "DELETE FROM `{$bbp_db->users}` WHERE `ID` IN ('{$sql_meta}')",
 227                  'message' => esc_html__( 'Deleting Imported Users&hellip; %s', 'bbpress' )
 228              ) );
 229  
 230              // User meta
 231              $messages[] = bbp_admin_reset_query_feedback( array(
 232                  'query'   => "DELETE FROM `{$bbp_db->usermeta}` WHERE `user_id` IN ('{$sql_meta}')",
 233                  'message' => esc_html__( 'Deleting Imported User Meta&hellip; %s', 'bbpress' )
 234              ) );
 235          }
 236      }
 237  
 238      // Next, if we still have users that were not imported delete that meta data
 239      $messages[] = bbp_admin_reset_query_feedback( array(
 240          'query'   => "DELETE FROM `{$bbp_db->usermeta}` WHERE `meta_key` LIKE '%%_bbp_%%'",
 241          'message' => esc_html__( 'Deleting bbPress Specific User Meta&hellip; %s', 'bbpress' )
 242      ) );
 243  
 244      /** Converter *************************************************************/
 245  
 246      $table_name = $bbp_db->prefix . 'bbp_converter_translator';
 247      if ( $bbp_db->get_var( "SHOW TABLES LIKE '{$table_name}'" ) === $table_name ) {
 248          $messages[] = bbp_admin_reset_query_feedback( array(
 249              'query'   => "DROP TABLE {$table_name}",
 250              'message' => esc_html__( 'Dropping Conversion Table&hellip; %s', 'bbpress' )
 251          ) );
 252      }
 253  
 254      /** Options ***************************************************************/
 255  
 256      bbp_delete_options();
 257      $messages[] = esc_html__( 'Deleting Settings&hellip; Success!', 'bbpress' );
 258  
 259      /** Roles *****************************************************************/
 260  
 261      bbp_remove_roles();
 262      bbp_remove_caps();
 263      $messages[] = esc_html__( 'Removing Roles and Capabilities&hellip; Success!', 'bbpress' );
 264  
 265      /** Output ****************************************************************/
 266  
 267      if ( count( $messages ) ) {
 268          foreach ( $messages as $message ) {
 269              bbp_admin_tools_feedback( $message );
 270          }
 271      }
 272  }


Generated: Wed Jul 24 01:01:31 2019 Cross-referenced by PHPXref 0.7.1