[ Index ]

PHP Cross Reference of BBPress

title

Body

[close]

/src/includes/core/ -> update.php (source)

   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          // 2.6 Alpha
 342          if ( $raw_db_version < 261 ) {
 343  
 344              /**
 345               * Upgrade user favorites and subscriptions
 346               *
 347               * @link https://bbpress.trac.wordpress.org/ticket/2959
 348               */
 349              if ( ! bbp_is_large_install() ) {
 350                  bbp_admin_upgrade_user_favorites();
 351                  bbp_admin_upgrade_user_topic_subscriptions();
 352                  bbp_admin_upgrade_user_forum_subscriptions();
 353              } else {
 354                  bbp_add_pending_upgrade( 'bbp-user-favorites-move' );
 355                  bbp_add_pending_upgrade( 'bbp-user-topic-subscriptions-move' );
 356                  bbp_add_pending_upgrade( 'bbp-user-forum-subscriptions-move' );
 357  
 358                  // Set strategy to pre-2.6 on large network
 359                  update_option( '_bbp_engagements_strategy', 'user' );
 360              }
 361          }
 362  
 363          // 2.6 Beta/RC/GM
 364          if ( $raw_db_version < 262 ) {
 365  
 366              /**
 367               * Upgrade user engagements
 368               *
 369               * @link https://bbpress.trac.wordpress.org/ticket/3068
 370               */
 371              if ( ! bbp_is_large_install() ) {
 372                  bbp_admin_upgrade_user_engagements();
 373              } else {
 374                  bbp_add_pending_upgrade( 'bbp-user-topic-engagements-move' );
 375  
 376                  // Set strategy to pre-2.6 on large network
 377                  update_option( '_bbp_engagements_strategy', 'user' );
 378              }
 379          }
 380      }
 381  
 382      /** All done! *************************************************************/
 383  
 384      // Bump the version
 385      bbp_version_bump();
 386  
 387      // Delete rewrite rules to force a flush
 388      bbp_delete_rewrite_rules();
 389  }
 390  
 391  /**
 392   * Redirect user to the "What's New" page on activation
 393   *
 394   * @since 2.2.0 bbPress (r4389)
 395   *
 396   * @internal Used internally to redirect bbPress to the about page on activation
 397   *
 398   * @return If network admin or bulk activation
 399   */
 400  function bbp_add_activation_redirect() {
 401  
 402      // Bail if activating from network, or bulk
 403      if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) {
 404          return;
 405      }
 406  
 407      // Add the redirect trigger
 408      update_user_option( get_current_user_id(), '_bbp_activation_redirect', true );
 409  }
 410  
 411  /**
 412   * Redirect user to "What's New" page on activation
 413   *
 414   * @since 2.2.0 bbPress (r4389)
 415   *
 416   * @internal Used internally to redirect bbPress to the about page on activation
 417   *
 418   * @return If no transient, or in network admin, or is bulk activation
 419   */
 420  function bbp_do_activation_redirect() {
 421  
 422      // Bail if no redirect trigger
 423      if ( ! get_user_option( '_bbp_activation_redirect' ) ) {
 424          return;
 425      }
 426  
 427      // Delete the redirect trigger
 428      delete_user_option( get_current_user_id(), '_bbp_activation_redirect' );
 429  
 430      // Bail if activating from network, or bulk
 431      if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) {
 432          return;
 433      }
 434  
 435      // Bail if the current user cannot see the about page
 436      if ( ! current_user_can( 'bbp_about_page' ) ) {
 437          return;
 438      }
 439  
 440      // Redirect to bbPress about page
 441      bbp_redirect( add_query_arg( array( 'page' => 'bbp-about' ), admin_url( 'index.php' ) ) );
 442  }
 443  
 444  /**
 445   * Hooked to the 'bbp_activate' action, this helper function automatically makes
 446   * the current user a Key Master in the forums if they just activated bbPress,
 447   * regardless of the bbp_allow_global_access() setting.
 448   *
 449   * @since 2.4.0 bbPress (r4910)
 450   *
 451   * @internal Used to internally make the current user a keymaster on activation
 452   *
 453   * @return If user can't activate plugins or is already a keymaster
 454   */
 455  function bbp_make_current_user_keymaster() {
 456  
 457      // Catch all, to prevent premature user initialization
 458      if ( ! did_action( 'set_current_user' ) ) {
 459          return;
 460      }
 461  
 462      // Bail if not logged in or already a member of this site
 463      if ( ! is_user_logged_in() ) {
 464          return;
 465      }
 466  
 467      // Bail if the current user can't activate plugins since previous pageload
 468      if ( ! current_user_can( 'activate_plugins' ) ) {
 469          return;
 470      }
 471  
 472      // Cannot use bbp_get_current_user_id() during activation process
 473      $user_id = get_current_user_id();
 474  
 475      // Get the current blog ID, to know if they should be promoted here
 476      $blog_id = get_current_blog_id();
 477  
 478      // Bail if user is not actually a member of this site
 479      if ( ! is_user_member_of_blog( $user_id, $blog_id ) ) {
 480          return;
 481      }
 482  
 483      // Bail if the current user already has a forum role to prevent
 484      // unexpected role and capability escalation.
 485      if ( bbp_get_user_role( $user_id ) ) {
 486          return;
 487      }
 488  
 489      // Make the current user a keymaster
 490      bbp_set_user_role( $user_id, bbp_get_keymaster_role() );
 491  
 492      // Reload the current user so caps apply immediately
 493      wp_get_current_user();
 494  }
 495  
 496  /** Pending Upgrades **********************************************************/
 497  
 498  /**
 499   * Return the number of pending upgrades
 500   *
 501   * @since 2.6.0 bbPress (r6895)
 502   *
 503   * @return int
 504   */
 505  function bbp_get_pending_upgrade_count() {
 506      return count( (array) bbp_get_pending_upgrades() );
 507  }
 508  
 509  /**
 510   * Return an array of pending upgrades
 511   *
 512   * @since 2.6.0 bbPress (r6895)
 513   *
 514   * @return array
 515   */
 516  function bbp_get_pending_upgrades() {
 517      return (array) get_option( '_bbp_db_pending_upgrades', array() );
 518  }
 519  
 520  /**
 521   * Add an upgrade ID to pending upgrades array
 522   *
 523   * @since 2.6.0 bbPress (r6895)
 524   *
 525   * @param string $upgrade_id
 526   */
 527  function bbp_add_pending_upgrade( $upgrade_id = '' ) {
 528  
 529      // Get the pending upgrades option
 530      $pending = bbp_get_pending_upgrades();
 531  
 532      // Maybe add upgrade ID to end of pending array
 533      if ( false === array_search( $upgrade_id, $pending, true ) ) {
 534          array_push( $pending, $upgrade_id );
 535      }
 536  
 537      // Update and return
 538      return update_option( '_bbp_db_pending_upgrades', $pending );
 539  }
 540  
 541  /**
 542   * Add an upgrade ID to pending upgrades array
 543   *
 544   * @since 2.6.0 bbPress (r6895)
 545   *
 546   * @param string $upgrade_id
 547   */
 548  function bbp_remove_pending_upgrade( $upgrade_id = '' ) {
 549  
 550      // Get the pending upgrades option
 551      $pending = bbp_get_pending_upgrades();
 552  
 553      // Maybe remove upgrade ID from pending array
 554      if ( isset( $pending[ $upgrade_id ] ) ) {
 555          unset( $pending[ $upgrade_id ] );
 556      }
 557  
 558      // Update and return
 559      return update_option( '_bbp_db_pending_upgrades', $pending );
 560  }
 561  
 562  /**
 563   * Delete all pending upgrades
 564   *
 565   * @since 2.6.0 bbPress (r6895)
 566   */
 567  function bbp_clear_pending_upgrades() {
 568      return delete_option( '_bbp_db_pending_upgrades' );
 569  }
 570  
 571  /**
 572   * Maybe append an upgrade count to a string
 573   *
 574   * @since 2.6.0 bbPress (r6896)
 575   *
 576   * @param string $string
 577   *
 578   * @return string
 579   */
 580  function bbp_maybe_append_pending_upgrade_count( $string = '' ) {
 581  
 582      // Look for an upgrade count
 583      $count = bbp_get_pending_upgrade_count();
 584  
 585      // Append the count to the string
 586      if ( ! empty( $count ) ) {
 587          $suffix = ' <span class="awaiting-mod count-' . absint( $count ) . '"><span class="pending-count">' . bbp_number_format( $count ) . '</span></span>';
 588          $string = "{$string}{$suffix}";
 589      }
 590  
 591      // Return the string, maybe with a count
 592      return $string;
 593  }


Generated: Thu Sep 19 01:01:28 2019 Cross-referenced by PHPXref 0.7.1