PluginProbe
Forumax – AI Powered Advanced Community Forum Plugin / trunk
Forumax – AI Powered Advanced Community Forum Plugin vtrunk
2.4.4 2.4.3 2.4.2 2.4.1 2.4.0 trunk 1.0.8 1.1.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 2.0.0 2.1.0 All 29 releases
bbp-core / lib / bbpress / includes / core / update.php

update.php in Forumax – AI Powered Advanced Community Forum Plugin trunk, at lib/bbpress/includes/core/update.php

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