PluginProbe
bbPress / 2.6.19
bbPress v2.6.19
2.6.19 2.6.18 2.6.17 trunk 2.0 2.0-beta-1 2.0-beta-2b 2.0-beta-3 2.0-beta-3b 2.0-rc-2 2.0-rc-3 2.0-rc-4 2.0-rc-5 2.0.1 2.0.2 2.0.3 2.1 2.1-beta-1 2.1-rc1 2.1-rc2 2.1-rc3 2.1-rc4 2.1.1 2.1.2 2.1.3 All 74 releases
bbpress / includes / admin / tools / reset.php

reset.php in bbPress 2.6.19, at includes/admin/tools/reset.php

296 lines 9.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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.6.0 bbPress (r6277)
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 <?php if ( ! is_multisite() ) : ?>
47 <tr valign="top">
48 <th scope="row"><?php esc_html_e( 'Delete imported users?', 'bbpress' ); ?></th>
49 <td>
50 <fieldset>
51 <legend class="screen-reader-text"><span><?php esc_html_e( "Say it ain't so!", 'bbpress' ); ?></span></legend>
52 <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>
53 <p class="description"><?php esc_html_e( 'Proceeding without this checked removes the meta-data necessary to delete these users later.', 'bbpress' ); ?></p>
54 </fieldset>
55 </td>
56 </tr>
57 <?php endif; ?>
58 <tr valign="top">
59 <th scope="row"><?php esc_html_e( 'Do you really want to do this?', 'bbpress' ); ?></th>
60 <td>
61 <fieldset>
62 <legend class="screen-reader-text"><span><?php esc_html_e( "Say it ain't so!", 'bbpress' ); ?></span></legend>
63 <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>
64 <p class="description"><?php esc_html_e( 'Backup your database before proceeding.', 'bbpress' ); ?></p>
65 </fieldset>
66 </td>
67 </tr>
68 </tbody>
69 </table>
70
71 <fieldset class="submit">
72 <input class="button-primary" type="submit" name="submit" value="<?php esc_attr_e( 'Reset bbPress', 'bbpress' ); ?>" />
73 <?php wp_nonce_field( 'bbpress-reset' ); ?>
74 </fieldset>
75 </form>
76 </div>
77
78 <?php
79 }
80
81 /**
82 * Handle a bbPress admin area reset request.
83 *
84 * @since 2.1.0 bbPress (r3884)
85 */
86 function bbp_admin_reset_handler() {
87
88 // Bail if not resetting.
89 if ( ! bbp_is_post_request() || empty( $_POST['bbpress-are-you-sure'] ) ) {
90 return;
91 }
92
93 // Only keymasters can proceed.
94 if ( ! bbp_is_user_keymaster() ) {
95 return;
96 }
97
98 // Bail if not referred from resetter
99 check_admin_referer( 'bbpress-reset' );
100
101 // Reset all of bbPress
102 bbp_admin_reset_database();
103 }
104
105 /**
106 * Wrapper for determining admin reset query feedback presented to a user.
107 *
108 * @since 2.6.0 bbPress (r6759)
109 *
110 * @param array $args Array of query, message, and possible responses
111 *
112 * @return string
113 */
114 function bbp_admin_reset_query_feedback( $args = array() ) {
115 static $defaults = null;
116
117 // Only set defaults one time to avoid hitting the GetText API repeatedly
118 if ( null === $defaults ) {
119 $defaults = array(
120 'query' => '',
121 'message' => esc_html__( 'Resetting&hellip;', 'bbpress' ),
122 'responses' => array(
123 'success' => esc_html__( 'Success!', 'bbpress' ),
124 'failure' => esc_html__( 'Failed!', 'bbpress' ),
125 'skipped' => esc_html__( 'Skipped.', 'bbpress' )
126 )
127 );
128 }
129
130 // Parse arguments
131 $r = bbp_parse_args( $args, $defaults, 'admin_reset_query_feedback' );
132
133 // Success/Failure based on query error
134 if ( ! empty( $r['query'] ) ) {
135 $query = bbp_db()->query( $r['query'] );
136 $result = ! is_wp_error( $query )
137 ? $r['responses']['success']
138 : $r['responses']['failure'];
139
140 // Skip if empty
141 } else {
142 $result = $r['responses']['skipped'];
143 }
144
145 // Return feedback
146 return sprintf( $r['message'], $result );
147 }
148
149 /**
150 * Perform a bbPress database reset.
151 *
152 * @since 2.6.0 bbPress (r6750)
153 */
154 function bbp_admin_reset_database() {
155
156 // Define variables.
157 $messages = array();
158 $sql_meta = array();
159 $bbp_db = bbp_db();
160
161 // Flush the whole cache; things are about to get ugly.
162 wp_cache_flush();
163
164 /** Posts *****************************************************************/
165
166 // Post types and status.
167 $fpt = bbp_get_forum_post_type();
168 $tpt = bbp_get_topic_post_type();
169 $rpt = bbp_get_reply_post_type();
170
171 // Get post IDs
172 $sql_posts = $bbp_db->get_results( "SELECT `ID` FROM `{$bbp_db->posts}` WHERE `post_type` IN ('{$fpt}', '{$tpt}', '{$rpt}')", OBJECT_K );
173 if ( ! empty( $sql_posts ) ) {
174
175 // Meta data
176 foreach ( $sql_posts as $key => $value ) {
177 $sql_meta[] = $key;
178 }
179 $sql_meta = implode( "', '", $sql_meta );
180
181 // Delete posts
182 $messages[] = bbp_admin_reset_query_feedback(
183 array(
184 'query' => "DELETE FROM `{$bbp_db->posts}` WHERE `post_type` IN ('{$fpt}', '{$tpt}', '{$rpt}')",
185 /* translators: %s: Status of the deletion process */
186 'message' => esc_html__( 'Removing Forums, Topics, and Replies&hellip; %s', 'bbpress' )
187 )
188 );
189
190 /** Post Meta *********************************************************/
191
192 if ( ! empty( $sql_posts ) ) {
193 $messages[] = bbp_admin_reset_query_feedback(
194 array(
195 'query' => "DELETE FROM `{$bbp_db->postmeta}` WHERE `post_id` IN ('{$sql_meta}')",
196 /* translators: %s: Status of the meta deletion process */
197 'message' => esc_html__( 'Removing Forum, Topic, and Reply Meta Data&hellip; %s', 'bbpress' )
198 )
199 );
200 }
201
202 /** Post Revisions ****************************************************/
203
204 if ( ! empty( $sql_posts ) ) {
205 $messages[] = bbp_admin_reset_query_feedback(
206 array(
207 'query' => "DELETE FROM `{$bbp_db->posts}` WHERE `post_parent` IN ('{$sql_meta}') AND `post_type` = 'revision'",
208 /* translators: %s: Status of the revision deletion process */
209 'message' => esc_html__( 'Removing Revision Data&hellip; %s', 'bbpress' )
210 )
211 );
212 }
213 }
214
215 /** Topic Tags ************************************************************/
216
217 $messages[] = bbp_admin_reset_query_feedback(
218 array(
219 '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'",
220 /* translators: %s: Status of the tag deletion process */
221 'message' => esc_html__( 'Deleting Topic Tags&hellip; %s', 'bbpress' )
222 )
223 );
224
225 /** User ******************************************************************/
226
227 // First, if we're deleting previously imported users, delete them now
228 if ( ! is_multisite() && ! empty( $_POST['bbpress-delete-imported-users'] ) ) {
229 $sql_users = array_unique( array_map( 'intval', $bbp_db->get_col( "SELECT `user_id` FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '_bbp_old_user_id'" ) ) );
230
231 if ( ! empty( $sql_users ) ) {
232 require_once ABSPATH . 'wp-admin/includes/user.php';
233 $deleted = true;
234 foreach ( $sql_users as $user_id ) {
235 if ( ! wp_delete_user( (int) $user_id ) ) {
236 $deleted = false;
237 }
238 }
239
240 $messages[] = sprintf(
241 /* translators: %s: Status of the user deletion process */
242 esc_html__( 'Deleting Imported Users&hellip; %s', 'bbpress' ),
243 $deleted ? esc_html__( 'Success!', 'bbpress' ) : esc_html__( 'Failed!', 'bbpress' )
244 );
245 }
246 }
247
248 // Remove only this site's user options on multisite. Converter metadata is global.
249 $meta_key = is_multisite()
250 ? $bbp_db->get_blog_prefix() . '_bbp_'
251 : '_bbp_';
252 $like = is_multisite()
253 ? $bbp_db->esc_like( $meta_key ) . '%'
254 : '%' . $bbp_db->esc_like( $meta_key ) . '%';
255
256 $messages[] = bbp_admin_reset_query_feedback(
257 array(
258 'query' => $bbp_db->prepare( "DELETE FROM `{$bbp_db->usermeta}` WHERE `meta_key` LIKE %s", $like ),
259 /* translators: %s: Status of the user meta deletion process */
260 'message' => esc_html__( 'Deleting bbPress Specific User Meta&hellip; %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&hellip; %s', 'bbpress' )
273 )
274 );
275 }
276
277 /** Options ***************************************************************/
278
279 bbp_delete_options();
280 $messages[] = esc_html__( 'Deleting Settings&hellip; Success!', 'bbpress' );
281
282 /** Roles *****************************************************************/
283
284 bbp_remove_roles();
285 bbp_remove_caps();
286 $messages[] = esc_html__( 'Removing Roles and Capabilities&hellip; 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