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