PluginProbe
bbPress / 2.6.17
bbPress v2.6.17
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 2.2 2.2.1 All 72 releases
bbpress / includes / admin / tools / upgrade.php

upgrade.php in bbPress 2.6.17, at includes/admin/tools/upgrade.php

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