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

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

644 lines 15.9 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 (r3764)
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 */
155 function bbp_setup_updater() {
156
157 // Bail if no update needed
158 if ( ! bbp_is_update() ) {
159 return;
160 }
161
162 // Call the automated updater
163 bbp_version_updater();
164 }
165
166 /**
167 * Runs when a new site is created in a multisite network, and bbPress is active
168 * on that site (hooked to `bbp_new_site`)
169 *
170 * @since 2.6.0 bbPress (r6779)
171 */
172 function bbp_setup_new_site( $site_id = 0 ) {
173
174 // Look for initial content
175 $created = is_multisite()
176 ? get_blog_option( $site_id, '_bbp_flag_initial_content', false )
177 : get_option( '_bbp_flag_initial_content', false );
178
179 // Maybe create the initial content
180 if ( ! empty( $created ) ) {
181 bbp_create_initial_content();
182
183 // Flag initial content as created
184 is_multisite()
185 ? update_blog_option( $site_id, '_bbp_flag_initial_content', true )
186 : update_option( '_bbp_flag_initial_content', true );
187 }
188 }
189
190 /**
191 * Create a default forum, topic, and reply
192 *
193 * @since 2.1.0 bbPress (r3767)
194 *
195 * @param array $args Array of arguments to override default values
196 */
197 function bbp_create_initial_content( $args = array() ) {
198
199 // Cannot use bbp_get_current_user_id() during activation process
200 $user_id = get_current_user_id();
201
202 // Parse arguments against default values
203 $r = bbp_parse_args(
204 $args,
205 array(
206 'forum_author' => $user_id,
207 'forum_parent' => 0,
208 'forum_status' => 'publish',
209 'forum_title' => esc_html__( 'General', 'bbpress' ),
210 'forum_content' => esc_html__( 'General Discussion', 'bbpress' ),
211
212 'topic_author' => $user_id,
213 'topic_title' => esc_html__( 'Hello World!', 'bbpress' ),
214 'topic_content' => esc_html__( 'This is the very first topic in these forums.', 'bbpress' ),
215
216 'reply_author' => $user_id,
217 'reply_content' => esc_html__( 'And this is the very first reply.', 'bbpress' ),
218 ),
219 'create_initial_content'
220 );
221
222 // Use the same time for each post
223 // phpcs:disable WordPress.DateTime.RestrictedFunctions.date_date
224 $current_time = time();
225 $forum_time = date( 'Y-m-d H:i:s', $current_time - 60 * 60 * 80 );
226 $topic_time = date( 'Y-m-d H:i:s', $current_time - 60 * 60 * 60 );
227 $reply_time = date( 'Y-m-d H:i:s', $current_time - 60 * 60 * 40 );
228 // phpcs:enable
229
230 // Create the initial forum
231 $forum_id = bbp_insert_forum(
232 array(
233 'post_author' => $r['forum_author'],
234 'post_parent' => $r['forum_parent'],
235 'post_status' => $r['forum_status'],
236 'post_title' => $r['forum_title'],
237 'post_content' => $r['forum_content'],
238 'post_date' => $forum_time
239 )
240 );
241
242 // Create the initial topic
243 $topic_id = bbp_insert_topic(
244 array(
245 'post_author' => $r['topic_author'],
246 'post_parent' => $forum_id,
247 'post_title' => $r['topic_title'],
248 'post_content' => $r['topic_content'],
249 'post_date' => $topic_time,
250 ),
251 array(
252 'forum_id' => $forum_id
253 )
254 );
255
256 // Create the initial reply
257 $reply_id = bbp_insert_reply(
258 array(
259 'post_author' => $r['reply_author'],
260 'post_parent' => $topic_id,
261 'post_content' => $r['reply_content'],
262 'post_date' => $reply_time
263 ),
264 array(
265 'forum_id' => $forum_id,
266 'topic_id' => $topic_id
267 )
268 );
269
270 return array(
271 'forum_id' => $forum_id,
272 'topic_id' => $topic_id,
273 'reply_id' => $reply_id
274 );
275 }
276
277 /**
278 * The version updater looks at what the current database version is, and
279 * runs whatever other code is needed.
280 *
281 * This is most-often used when the data schema changes, but should also be used
282 * to correct issues with bbPress meta-data silently on software update.
283 *
284 * @since 2.2.0 bbPress (r4104)
285 */
286 function bbp_version_updater() {
287
288 // Get the raw database version
289 $raw_db_version = (int) bbp_get_db_version_raw();
290
291 // Only run updater if previous installation exists
292 if ( ! empty( $raw_db_version ) ) {
293
294 /** 2.0 Branch ********************************************************/
295
296 // 2.0, 2.0.1, 2.0.2, 2.0.3
297 // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
298 if ( $raw_db_version < 200 ) {
299 // No changes
300 }
301
302 /** 2.1 Branch ********************************************************/
303
304 // 2.1, 2.1.1
305 if ( $raw_db_version < 211 ) {
306
307 /**
308 * Repair private and hidden forum data
309 *
310 * @link https://bbpress.trac.wordpress.org/ticket/1891
311 */
312 bbp_admin_repair_forum_visibility();
313 }
314
315 /** 2.2 Branch ********************************************************/
316
317 // 2.2.x
318 if ( $raw_db_version < 220 ) {
319
320 // Remove any old bbPress roles
321 bbp_remove_roles();
322
323 // Remove capabilities
324 bbp_remove_caps();
325 }
326
327 /** 2.3 Branch ********************************************************/
328
329 // 2.3.x
330 // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
331 if ( $raw_db_version < 230 ) {
332 // No changes
333 }
334
335 /** 2.4 Branch ********************************************************/
336
337 // 2.4.x
338 // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
339 if ( $raw_db_version < 240 ) {
340 // No changes
341 }
342
343 /** 2.5 Branch ********************************************************/
344
345 // 2.5.x
346 // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
347 if ( $raw_db_version < 250 ) {
348 // No changes
349 }
350
351 /** 2.6 Branch ********************************************************/
352
353 // Smaller installs run the upgrades directly
354 if ( ! bbp_is_large_install() ) {
355
356 /**
357 * Upgrade user favorites and subscriptions
358 */
359 if ( $raw_db_version < 261 ) {
360 bbp_admin_upgrade_user_favorites();
361 bbp_admin_upgrade_user_topic_subscriptions();
362 bbp_admin_upgrade_user_forum_subscriptions();
363 }
364
365 /**
366 * Upgrade user engagements
367 */
368 if ( $raw_db_version < 262 ) {
369 bbp_admin_upgrade_user_engagements();
370 }
371
372 /**
373 * Repair forum hidden reply count
374 */
375 if ( $raw_db_version < 263 ) {
376 bbp_admin_repair_forum_hidden_reply_count();
377 }
378
379 // Large installs require manual intervention
380 } else {
381
382 /**
383 * Upgrade user favorites and subscriptions
384 */
385 if ( $raw_db_version < 261 ) {
386 bbp_add_pending_upgrade( 'bbp-user-favorites-move' );
387 bbp_add_pending_upgrade( 'bbp-user-topic-subscriptions-move' );
388 bbp_add_pending_upgrade( 'bbp-user-forum-subscriptions-move' );
389
390 // Set strategy to pre-2.6 on large network
391 update_option( '_bbp_engagements_strategy', 'user' );
392 }
393
394 /**
395 * Upgrade user engagements
396 */
397 if ( $raw_db_version < 262 ) {
398 bbp_add_pending_upgrade( 'bbp-user-topic-engagements-move' );
399
400 // Set strategy to pre-2.6 on large network
401 update_option( '_bbp_engagements_strategy', 'user' );
402 }
403
404 /**
405 * Upgrade user engagements
406 */
407 if ( $raw_db_version < 263 ) {
408 bbp_add_pending_upgrade( 'bbp-forum-hidden-replies' );
409 }
410 }
411 }
412
413 /** All done! *************************************************************/
414
415 // Bump the version
416 bbp_version_bump();
417
418 // Delete rewrite rules to force a flush
419 bbp_delete_rewrite_rules();
420 }
421
422 /**
423 * Redirect user to the "What's New" page on activation
424 *
425 * @since 2.2.0 bbPress (r4389)
426 *
427 * @internal Used internally to redirect bbPress to the about page on activation
428 *
429 * @return If network admin or bulk activation
430 */
431 function bbp_add_activation_redirect() {
432
433 // Bail if activating from network, or bulk
434 if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) {
435 return;
436 }
437
438 // Add the redirect trigger
439 update_user_option( get_current_user_id(), '_bbp_activation_redirect', true );
440 }
441
442 /**
443 * Redirect user to "What's New" page on activation
444 *
445 * @since 2.2.0 bbPress (r4389)
446 *
447 * @internal Used internally to redirect bbPress to the about page on activation
448 *
449 * @return If no transient, or in network admin, or is bulk activation
450 */
451 function bbp_do_activation_redirect() {
452
453 // Bail if no redirect trigger
454 if ( ! get_user_option( '_bbp_activation_redirect' ) ) {
455 return;
456 }
457
458 // Delete the redirect trigger
459 delete_user_option( get_current_user_id(), '_bbp_activation_redirect' );
460
461 // Bail if activating from network, or bulk
462 if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) {
463 return;
464 }
465
466 // Bail if the current user cannot see the about page
467 if ( ! current_user_can( 'bbp_about_page' ) ) {
468 return;
469 }
470
471 // Redirect to bbPress about page
472 bbp_redirect( add_query_arg( array( 'page' => 'bbp-about' ), admin_url( 'index.php' ) ) );
473 }
474
475 /**
476 * Hooked to the 'bbp_activate' action, this helper function automatically makes
477 * the current user a Key Master in the forums if they just activated bbPress,
478 * regardless of the bbp_allow_global_access() setting.
479 *
480 * @since 2.4.0 bbPress (r4910)
481 *
482 * @internal Used to internally make the current user a keymaster on activation
483 *
484 * @return If user can't activate plugins or is already a keymaster
485 */
486 function bbp_make_current_user_keymaster() {
487
488 // Catch all, to prevent premature user initialization
489 if ( ! did_action( 'set_current_user' ) ) {
490 return;
491 }
492
493 // Bail if not logged in or already a member of this site
494 if ( ! is_user_logged_in() ) {
495 return;
496 }
497
498 // Bail if the current user can't activate plugins since previous pageload
499 if ( ! current_user_can( 'activate_plugins' ) ) {
500 return;
501 }
502
503 // Cannot use bbp_get_current_user_id() during activation process
504 $user_id = get_current_user_id();
505
506 // Get the current blog ID, to know if they should be promoted here
507 $blog_id = get_current_blog_id();
508
509 // Bail if user is not actually a member of this site
510 if ( ! is_user_member_of_blog( $user_id, $blog_id ) ) {
511 return;
512 }
513
514 // Bail if the current user already has a forum role to prevent
515 // unexpected role and capability escalation.
516 if ( bbp_get_user_role( $user_id ) ) {
517 return;
518 }
519
520 // Make the current user a keymaster
521 bbp_set_user_role( $user_id, bbp_get_keymaster_role() );
522
523 // Reload the current user so caps apply immediately
524 wp_get_current_user();
525 }
526
527 /** Pending Upgrades **********************************************************/
528
529 /**
530 * Return the number of pending upgrades
531 *
532 * @since 2.6.0 bbPress (r6895)
533 *
534 * @param string $type Type of pending upgrades (upgrade|repair|empty)
535 *
536 * @return int
537 */
538 function bbp_get_pending_upgrade_count( $type = '' ) {
539 return count( (array) bbp_get_pending_upgrades( $type ) );
540 }
541
542 /**
543 * Return an array of pending upgrades
544 *
545 * @since 2.6.0 bbPress (r6895)
546 *
547 * @param string $type Type of pending upgrades (upgrade|repair|empty)
548 *
549 * @return array
550 */
551 function bbp_get_pending_upgrades( $type = '' ) {
552
553 // Get the pending upgrades
554 $retval = (array) get_option( '_bbp_db_pending_upgrades', array() );
555
556 // Looking for a specific type?
557 if ( ! empty( $type ) ) {
558 $tools = bbp_get_admin_repair_tools( $type );
559 $plucked = array_keys( wp_list_pluck( $tools, 'type' ) );
560 $retval = array_intersect( $retval, $plucked );
561 }
562
563 return (array) $retval;
564 }
565
566 /**
567 * Add an upgrade ID to pending upgrades array
568 *
569 * @since 2.6.0 bbPress (r6895)
570 *
571 * @param string $upgrade_id
572 */
573 function bbp_add_pending_upgrade( $upgrade_id = '' ) {
574
575 // Get the pending upgrades option
576 $pending = bbp_get_pending_upgrades();
577
578 // Maybe add upgrade ID to end of pending array
579 if ( false === array_search( $upgrade_id, $pending, true ) ) {
580 array_push( $pending, $upgrade_id );
581 }
582
583 // Update and return
584 return update_option( '_bbp_db_pending_upgrades', $pending );
585 }
586
587 /**
588 * Add an upgrade ID to pending upgrades array
589 *
590 * @since 2.6.0 bbPress (r6895)
591 *
592 * @param string $upgrade_id
593 */
594 function bbp_remove_pending_upgrade( $upgrade_id = '' ) {
595
596 // Get the pending upgrades option
597 $pending = bbp_get_pending_upgrades();
598
599 // Look for this upgrade ID
600 $index = array_search( $upgrade_id, $pending, true );
601
602 // Maybe remove upgrade ID from pending array
603 if ( false !== $index ) {
604 unset( $pending[ $index ] );
605 }
606
607 // Update and return
608 return update_option( '_bbp_db_pending_upgrades', $pending );
609 }
610
611 /**
612 * Delete all pending upgrades
613 *
614 * @since 2.6.0 bbPress (r6895)
615 */
616 function bbp_clear_pending_upgrades() {
617 return delete_option( '_bbp_db_pending_upgrades' );
618 }
619
620 /**
621 * Maybe append an upgrade count to a string
622 *
623 * @since 2.6.0 bbPress (r6896)
624 *
625 * @param string $string Text to append count to
626 * @param string $type Type of pending upgrades (upgrade|repair|empty)
627 *
628 * @return string
629 */
630 function bbp_maybe_append_pending_upgrade_count( $string = '', $type = '' ) {
631
632 // Look for an upgrade count
633 $count = bbp_get_pending_upgrade_count( $type );
634
635 // Append the count to the string
636 if ( ! empty( $count ) ) {
637 $suffix = ' <span class="awaiting-mod count-' . absint( $count ) . '"><span class="pending-count">' . bbp_number_format( $count ) . '</span></span>';
638 $string = "{$string}{$suffix}";
639 }
640
641 // Return the string, maybe with a count
642 return $string;
643 }
644