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 / core / update.php

update.php in bbPress 2.6.19, at includes/core/update.php

665 lines 16.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * bbPress Updater
5 *
6 * @package bbPress
7 * @subpackage Core
8 */
9
10 // Exit if accessed directly
11 defined( 'ABSPATH' ) || exit;
12
13 /**
14 * If there is no raw DB version, this is the first installation
15 *
16 * @since 2.1.0 bbPress (r3765)
17 *
18 * @return bool True if update, False if not
19 */
20 function bbp_is_install() {
21 return ! bbp_get_db_version_raw();
22 }
23
24 /**
25 * Compare the bbPress version to the DB version to determine if updating
26 *
27 * @since 2.0.0 bbPress (r3421)
28 *
29 * @return bool True if update, False if not
30 */
31 function bbp_is_update() {
32 $raw = (int) bbp_get_db_version_raw();
33 $cur = (int) bbp_get_db_version();
34 $retval = (bool) ( $raw < $cur );
35 return $retval;
36 }
37
38 /**
39 * Determine if bbPress is being activated
40 *
41 * Note that this function currently is not used in bbPress core and is here
42 * for third party plugins to use to check for bbPress activation.
43 *
44 * @since 2.0.0 bbPress (r3421)
45 *
46 * @return bool True if activating bbPress, false if not
47 */
48 function bbp_is_activation( $basename = '' ) {
49 global $pagenow;
50
51 $bbp = bbpress();
52 $action = false;
53
54 // Bail if not in admin/plugins
55 if ( ! ( is_admin() && ( 'plugins.php' === $pagenow ) ) ) {
56 return false;
57 }
58
59 if ( ! empty( $_REQUEST['action'] ) && ( '-1' !== $_REQUEST['action'] ) ) {
60 $action = $_REQUEST['action'];
61 } elseif ( ! empty( $_REQUEST['action2'] ) && ( '-1' !== $_REQUEST['action2'] ) ) {
62 $action = $_REQUEST['action2'];
63 }
64
65 // Bail if not activating
66 if ( empty( $action ) || ! in_array( $action, array( 'activate', 'activate-selected' ), true ) ) {
67 return false;
68 }
69
70 // The plugin(s) being activated
71 if ( 'activate' === $action ) {
72 $plugins = isset( $_GET['plugin'] ) ? array( $_GET['plugin'] ) : array();
73 } else {
74 $plugins = isset( $_POST['checked'] ) ? (array) $_POST['checked'] : array();
75 }
76
77 // Set basename if empty
78 if ( empty( $basename ) && ! empty( $bbp->basename ) ) {
79 $basename = $bbp->basename;
80 }
81
82 // Bail if no basename
83 if ( empty( $basename ) ) {
84 return false;
85 }
86
87 // Is bbPress being activated?
88 return in_array( $basename, $plugins, true );
89 }
90
91 /**
92 * Determine if bbPress is being deactivated
93 *
94 * @since 2.0.0 bbPress (r3421)
95 *
96 * @return bool True if deactivating bbPress, false if not
97 */
98 function bbp_is_deactivation( $basename = '' ) {
99 global $pagenow;
100
101 $bbp = bbpress();
102 $action = false;
103
104 // Bail if not in admin/plugins
105 if ( ! ( is_admin() && ( 'plugins.php' === $pagenow ) ) ) {
106 return false;
107 }
108
109 if ( ! empty( $_REQUEST['action'] ) && ( '-1' !== $_REQUEST['action'] ) ) {
110 $action = $_REQUEST['action'];
111 } elseif ( ! empty( $_REQUEST['action2'] ) && ( '-1' !== $_REQUEST['action2'] ) ) {
112 $action = $_REQUEST['action2'];
113 }
114
115 // Bail if not deactivating
116 if ( empty( $action ) || ! in_array( $action, array( 'deactivate', 'deactivate-selected' ), true ) ) {
117 return false;
118 }
119
120 // The plugin(s) being deactivated
121 if ( 'deactivate' === $action ) {
122 $plugins = isset( $_GET['plugin'] ) ? array( $_GET['plugin'] ) : array();
123 } else {
124 $plugins = isset( $_POST['checked'] ) ? (array) $_POST['checked'] : array();
125 }
126
127 // Set basename if empty
128 if ( empty( $basename ) && ! empty( $bbp->basename ) ) {
129 $basename = $bbp->basename;
130 }
131
132 // Bail if no basename
133 if ( empty( $basename ) ) {
134 return false;
135 }
136
137 // Is bbPress being deactivated?
138 return in_array( $basename, $plugins, true );
139 }
140
141 /**
142 * Update the DB to the latest version
143 *
144 * @since 2.0.0 bbPress (r3421)
145 */
146 function bbp_version_bump() {
147 update_option( '_bbp_db_version', bbp_get_db_version() );
148 }
149
150 /**
151 * Setup the bbPress updater
152 *
153 * @since 2.0.0 bbPress (r3419)
154 * @since 2.6.19 bbPress (r7645) Moved to "bbp_current_screen" hook
155 *
156 * @param WP_Screen|null $current_screen Current admin screen.
157 */
158 function bbp_setup_updater( $current_screen = null ) {
159 global $pagenow;
160
161 // Only run during a real site or network admin screen
162 if ( ! $current_screen instanceof WP_Screen ||
163 ( ! $current_screen->in_admin( 'site' ) && ! $current_screen->in_admin( 'network' ) ) ||
164 wp_doing_ajax() ||
165 in_array( $pagenow, array( 'admin-ajax.php', 'admin-post.php' ), true ) ) {
166 return;
167 }
168
169 // Only forum keymasters and site administrators may start an upgrade
170 if ( ! current_user_can( 'bbp_tools_upgrade_page' ) && ! current_user_can( 'manage_options' ) ) {
171 return;
172 }
173
174 // Bail if no update needed
175 if ( ! bbp_is_update() ) {
176 return;
177 }
178
179 // Call the automated updater
180 bbp_version_updater();
181 }
182
183 /**
184 * Runs when a new site is created in a multisite network, and bbPress is active
185 * on that site (hooked to `bbp_new_site`)
186 *
187 * @since 2.6.0 bbPress (r6780)
188 */
189 function bbp_setup_new_site( $site_id = 0 ) {
190
191 // Look for initial content
192 $created = is_multisite()
193 ? get_blog_option( $site_id, '_bbp_flag_initial_content', false )
194 : get_option( '_bbp_flag_initial_content', false );
195
196 // Maybe create the initial content
197 if ( ! empty( $created ) ) {
198 bbp_create_initial_content();
199
200 // Flag initial content as created
201 is_multisite()
202 ? update_blog_option( $site_id, '_bbp_flag_initial_content', true )
203 : update_option( '_bbp_flag_initial_content', true );
204 }
205 }
206
207 /**
208 * Create a default forum, topic, and reply
209 *
210 * @since 2.1.0 bbPress (r3768)
211 *
212 * @param array $args Array of arguments to override default values
213 */
214 function bbp_create_initial_content( $args = array() ) {
215
216 // Cannot use bbp_get_current_user_id() during activation process
217 $user_id = get_current_user_id();
218
219 // Parse arguments against default values
220 $r = bbp_parse_args(
221 $args,
222 array(
223 'forum_author' => $user_id,
224 'forum_parent' => 0,
225 'forum_status' => 'publish',
226 'forum_title' => esc_html__( 'General', 'bbpress' ),
227 'forum_content' => esc_html__( 'General Discussion', 'bbpress' ),
228
229 'topic_author' => $user_id,
230 'topic_title' => esc_html__( 'Hello World!', 'bbpress' ),
231 'topic_content' => esc_html__( 'This is the very first topic in these forums.', 'bbpress' ),
232
233 'reply_author' => $user_id,
234 'reply_content' => esc_html__( 'And this is the very first reply.', 'bbpress' ),
235 ),
236 'create_initial_content'
237 );
238
239 // Use the same time for each post
240 // phpcs:disable WordPress.DateTime.RestrictedFunctions.date_date
241 $current_time = time();
242 $forum_time = date( 'Y-m-d H:i:s', $current_time - 60 * 60 * 80 );
243 $topic_time = date( 'Y-m-d H:i:s', $current_time - 60 * 60 * 60 );
244 $reply_time = date( 'Y-m-d H:i:s', $current_time - 60 * 60 * 40 );
245 // phpcs:enable
246
247 // Create the initial forum
248 $forum_id = bbp_insert_forum(
249 array(
250 'post_author' => $r['forum_author'],
251 'post_parent' => $r['forum_parent'],
252 'post_status' => $r['forum_status'],
253 'post_title' => $r['forum_title'],
254 'post_content' => $r['forum_content'],
255 'post_date' => $forum_time
256 )
257 );
258
259 // Create the initial topic
260 $topic_id = bbp_insert_topic(
261 array(
262 'post_author' => $r['topic_author'],
263 'post_parent' => $forum_id,
264 'post_title' => $r['topic_title'],
265 'post_content' => $r['topic_content'],
266 'post_date' => $topic_time,
267 ),
268 array(
269 'forum_id' => $forum_id
270 )
271 );
272
273 // Create the initial reply
274 $reply_id = bbp_insert_reply(
275 array(
276 'post_author' => $r['reply_author'],
277 'post_parent' => $topic_id,
278 'post_content' => $r['reply_content'],
279 'post_date' => $reply_time
280 ),
281 array(
282 'forum_id' => $forum_id,
283 'topic_id' => $topic_id
284 )
285 );
286
287 return array(
288 'forum_id' => $forum_id,
289 'topic_id' => $topic_id,
290 'reply_id' => $reply_id
291 );
292 }
293
294 /**
295 * The version updater looks at what the current database version is, and
296 * runs whatever other code is needed.
297 *
298 * This is most-often used when the data schema changes, but should also be used
299 * to correct issues with bbPress meta-data silently on software update.
300 *
301 * @since 2.2.0 bbPress (r4105)
302 */
303 function bbp_version_updater() {
304
305 // Get the raw database version
306 $raw_db_version = (int) bbp_get_db_version_raw();
307
308 // Only run updater if previous installation exists
309 if ( ! empty( $raw_db_version ) ) {
310 // Discard converter progress SQL saved by older password upgrades.
311 if ( $raw_db_version < 264 ) {
312 delete_option( '_bbp_converter_query' );
313 }
314
315 /** 2.0 Branch ********************************************************/
316
317 // 2.0, 2.0.1, 2.0.2, 2.0.3
318 // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
319 if ( $raw_db_version < 200 ) {
320 // No changes
321 }
322
323 /** 2.1 Branch ********************************************************/
324
325 // 2.1, 2.1.1
326 if ( $raw_db_version < 211 ) {
327
328 /**
329 * Repair private and hidden forum data
330 *
331 * @link https://bbpress.trac.wordpress.org/ticket/1891
332 */
333 bbp_admin_repair_forum_visibility();
334 }
335
336 /** 2.2 Branch ********************************************************/
337
338 // 2.2.x
339 if ( $raw_db_version < 220 ) {
340
341 // Remove any old bbPress roles
342 bbp_remove_roles();
343
344 // Remove capabilities
345 bbp_remove_caps();
346 }
347
348 /** 2.3 Branch ********************************************************/
349
350 // 2.3.x
351 // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
352 if ( $raw_db_version < 230 ) {
353 // No changes
354 }
355
356 /** 2.4 Branch ********************************************************/
357
358 // 2.4.x
359 // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
360 if ( $raw_db_version < 240 ) {
361 // No changes
362 }
363
364 /** 2.5 Branch ********************************************************/
365
366 // 2.5.x
367 // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
368 if ( $raw_db_version < 250 ) {
369 // No changes
370 }
371
372 /** 2.6 Branch ********************************************************/
373
374 // Smaller installs run the upgrades directly
375 if ( ! bbp_is_large_install() ) {
376
377 /**
378 * Upgrade user favorites and subscriptions
379 */
380 if ( $raw_db_version < 261 ) {
381 bbp_admin_upgrade_user_favorites();
382 bbp_admin_upgrade_user_topic_subscriptions();
383 bbp_admin_upgrade_user_forum_subscriptions();
384 }
385
386 /**
387 * Upgrade user engagements
388 */
389 if ( $raw_db_version < 262 ) {
390 bbp_admin_upgrade_user_engagements();
391 }
392
393 /**
394 * Repair forum hidden reply count
395 */
396 if ( $raw_db_version < 263 ) {
397 bbp_admin_repair_forum_hidden_reply_count();
398 }
399
400 // Large installs require manual intervention
401 } else {
402
403 /**
404 * Upgrade user favorites and subscriptions
405 */
406 if ( $raw_db_version < 261 ) {
407 bbp_add_pending_upgrade( 'bbp-user-favorites-move' );
408 bbp_add_pending_upgrade( 'bbp-user-topic-subscriptions-move' );
409 bbp_add_pending_upgrade( 'bbp-user-forum-subscriptions-move' );
410
411 // Set strategy to pre-2.6 on large network
412 update_option( '_bbp_engagements_strategy', 'user' );
413 }
414
415 /**
416 * Upgrade user engagements
417 */
418 if ( $raw_db_version < 262 ) {
419 bbp_add_pending_upgrade( 'bbp-user-topic-engagements-move' );
420
421 // Set strategy to pre-2.6 on large network
422 update_option( '_bbp_engagements_strategy', 'user' );
423 }
424
425 /**
426 * Upgrade user engagements
427 */
428 if ( $raw_db_version < 263 ) {
429 bbp_add_pending_upgrade( 'bbp-forum-hidden-replies' );
430 }
431 }
432 }
433
434 /** All done! *************************************************************/
435
436 // Bump the version
437 bbp_version_bump();
438
439 // Delete rewrite rules to force a flush
440 bbp_delete_rewrite_rules();
441 }
442
443 /**
444 * Redirect user to the "What's New" page on activation
445 *
446 * @since 2.2.0 bbPress (r4390)
447 *
448 * @internal Used internally to redirect bbPress to the about page on activation
449 *
450 * @return If network admin or bulk activation
451 */
452 function bbp_add_activation_redirect() {
453
454 // Bail if activating from network, or bulk
455 if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) {
456 return;
457 }
458
459 // Add the redirect trigger
460 update_user_option( get_current_user_id(), '_bbp_activation_redirect', true );
461 }
462
463 /**
464 * Redirect user to "What's New" page on activation
465 *
466 * @since 2.2.0 bbPress (r4390)
467 *
468 * @internal Used internally to redirect bbPress to the about page on activation
469 *
470 * @return If no transient, or in network admin, or is bulk activation
471 */
472 function bbp_do_activation_redirect() {
473
474 // Bail if no redirect trigger
475 if ( ! get_user_option( '_bbp_activation_redirect' ) ) {
476 return;
477 }
478
479 // Delete the redirect trigger
480 delete_user_option( get_current_user_id(), '_bbp_activation_redirect' );
481
482 // Bail if activating from network, or bulk
483 if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) {
484 return;
485 }
486
487 // Bail if the current user cannot see the about page
488 if ( ! current_user_can( 'bbp_about_page' ) ) {
489 return;
490 }
491
492 // Redirect to bbPress about page
493 bbp_redirect( add_query_arg( array( 'page' => 'bbp-about' ), admin_url( 'index.php' ) ) );
494 }
495
496 /**
497 * Hooked to the 'bbp_activate' action, this helper function automatically makes
498 * the current user a Key Master in the forums if they just activated bbPress,
499 * regardless of the bbp_allow_global_access() setting.
500 *
501 * @since 2.4.0 bbPress (r4910)
502 *
503 * @internal Used to internally make the current user a keymaster on activation
504 *
505 * @return If user can't activate plugins or is already a keymaster
506 */
507 function bbp_make_current_user_keymaster() {
508
509 // Catch all, to prevent premature user initialization
510 if ( ! did_action( 'set_current_user' ) ) {
511 return;
512 }
513
514 // Bail if not logged in or already a member of this site
515 if ( ! is_user_logged_in() ) {
516 return;
517 }
518
519 // Bail if the current user can't activate plugins since previous pageload
520 if ( ! current_user_can( 'activate_plugins' ) ) {
521 return;
522 }
523
524 // Cannot use bbp_get_current_user_id() during activation process
525 $user_id = get_current_user_id();
526
527 // Get the current blog ID, to know if they should be promoted here
528 $blog_id = get_current_blog_id();
529
530 // Bail if user is not actually a member of this site
531 if ( ! is_user_member_of_blog( $user_id, $blog_id ) ) {
532 return;
533 }
534
535 // Bail if the current user already has a forum role to prevent
536 // unexpected role and capability escalation.
537 if ( bbp_get_user_role( $user_id ) ) {
538 return;
539 }
540
541 // Make the current user a keymaster
542 bbp_set_user_role( $user_id, bbp_get_keymaster_role() );
543
544 // Reload the current user so caps apply immediately
545 wp_get_current_user();
546 }
547
548 /** Pending Upgrades **********************************************************/
549
550 /**
551 * Return the number of pending upgrades
552 *
553 * @since 2.6.0 bbPress (r6896)
554 *
555 * @param string $type Type of pending upgrades (upgrade|repair|empty)
556 *
557 * @return int
558 */
559 function bbp_get_pending_upgrade_count( $type = '' ) {
560 return count( (array) bbp_get_pending_upgrades( $type ) );
561 }
562
563 /**
564 * Return an array of pending upgrades
565 *
566 * @since 2.6.0 bbPress (r6896)
567 *
568 * @param string $type Type of pending upgrades (upgrade|repair|empty)
569 *
570 * @return array
571 */
572 function bbp_get_pending_upgrades( $type = '' ) {
573
574 // Get the pending upgrades
575 $retval = (array) get_option( '_bbp_db_pending_upgrades', array() );
576
577 // Looking for a specific type?
578 if ( ! empty( $type ) ) {
579 $tools = bbp_get_admin_repair_tools( $type );
580 $plucked = array_keys( wp_list_pluck( $tools, 'type' ) );
581 $retval = array_intersect( $retval, $plucked );
582 }
583
584 return (array) $retval;
585 }
586
587 /**
588 * Add an upgrade ID to pending upgrades array
589 *
590 * @since 2.6.0 bbPress (r6896)
591 *
592 * @param string $upgrade_id
593 */
594 function bbp_add_pending_upgrade( $upgrade_id = '' ) {
595
596 // Get the pending upgrades option
597 $pending = bbp_get_pending_upgrades();
598
599 // Maybe add upgrade ID to end of pending array
600 if ( false === array_search( $upgrade_id, $pending, true ) ) {
601 array_push( $pending, $upgrade_id );
602 }
603
604 // Update and return
605 return update_option( '_bbp_db_pending_upgrades', $pending );
606 }
607
608 /**
609 * Add an upgrade ID to pending upgrades array
610 *
611 * @since 2.6.0 bbPress (r6896)
612 *
613 * @param string $upgrade_id
614 */
615 function bbp_remove_pending_upgrade( $upgrade_id = '' ) {
616
617 // Get the pending upgrades option
618 $pending = bbp_get_pending_upgrades();
619
620 // Look for this upgrade ID
621 $index = array_search( $upgrade_id, $pending, true );
622
623 // Maybe remove upgrade ID from pending array
624 if ( false !== $index ) {
625 unset( $pending[ $index ] );
626 }
627
628 // Update and return
629 return update_option( '_bbp_db_pending_upgrades', $pending );
630 }
631
632 /**
633 * Delete all pending upgrades
634 *
635 * @since 2.6.0 bbPress (r6896)
636 */
637 function bbp_clear_pending_upgrades() {
638 return delete_option( '_bbp_db_pending_upgrades' );
639 }
640
641 /**
642 * Maybe append an upgrade count to a string
643 *
644 * @since 2.6.0 bbPress (r6897)
645 *
646 * @param string $string Text to append count to
647 * @param string $type Type of pending upgrades (upgrade|repair|empty)
648 *
649 * @return string
650 */
651 function bbp_maybe_append_pending_upgrade_count( $string = '', $type = '' ) {
652
653 // Look for an upgrade count
654 $count = bbp_get_pending_upgrade_count( $type );
655
656 // Append the count to the string
657 if ( ! empty( $count ) ) {
658 $suffix = ' <span class="awaiting-mod count-' . absint( $count ) . '"><span class="pending-count">' . bbp_number_format( $count ) . '</span></span>';
659 $string = "{$string}{$suffix}";
660 }
661
662 // Return the string, maybe with a count
663 return $string;
664 }
665