web/wp-content/plugins/bbpress/includes/core/update.php
changeset 196 5e8dcbe22c24
child 204 09a1c134465b
equal deleted inserted replaced
195:c7c0fbc09788 196:5e8dcbe22c24
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * bbPress Updater
       
     5  *
       
     6  * @package bbPress
       
     7  * @subpackage Updater
       
     8  */
       
     9 
       
    10 // Exit if accessed directly
       
    11 if ( !defined( 'ABSPATH' ) ) exit;
       
    12 
       
    13 /**
       
    14  * If there is no raw DB version, this is the first installation
       
    15  *
       
    16  * @since bbPress (r3764)
       
    17  *
       
    18  * @uses get_option()
       
    19  * @uses bbp_get_db_version() To get bbPress's database version
       
    20  * @return bool True if update, False if not
       
    21  */
       
    22 function bbp_is_install() {
       
    23 	return ! bbp_get_db_version_raw();
       
    24 }
       
    25 
       
    26 /**
       
    27  * Compare the bbPress version to the DB version to determine if updating
       
    28  *
       
    29  * @since bbPress (r3421)
       
    30  *
       
    31  * @uses get_option()
       
    32  * @uses bbp_get_db_version() To get bbPress's database version
       
    33  * @return bool True if update, False if not
       
    34  */
       
    35 function bbp_is_update() {
       
    36 	$raw    = (int) bbp_get_db_version_raw();
       
    37 	$cur    = (int) bbp_get_db_version();
       
    38 	$retval = (bool) ( $raw < $cur );
       
    39 	return $retval;
       
    40 }
       
    41 
       
    42 /**
       
    43  * Determine if bbPress is being activated
       
    44  *
       
    45  * Note that this function currently is not used in bbPress core and is here
       
    46  * for third party plugins to use to check for bbPress activation.
       
    47  *
       
    48  * @since bbPress (r3421)
       
    49  *
       
    50  * @return bool True if activating bbPress, false if not
       
    51  */
       
    52 function bbp_is_activation( $basename = '' ) {
       
    53 	$bbp = bbpress();
       
    54 
       
    55 	$action = false;
       
    56 	if ( ! empty( $_REQUEST['action'] ) && ( '-1' != $_REQUEST['action'] ) )
       
    57 		$action = $_REQUEST['action'];
       
    58 	elseif ( ! empty( $_REQUEST['action2'] ) && ( '-1' != $_REQUEST['action2'] ) )
       
    59 		$action = $_REQUEST['action2'];
       
    60 
       
    61 	// Bail if not activating
       
    62 	if ( empty( $action ) || !in_array( $action, array( 'activate', 'activate-selected' ) ) )
       
    63 		return false;
       
    64 
       
    65 	// The plugin(s) being activated
       
    66 	if ( $action == 'activate' ) {
       
    67 		$plugins = isset( $_GET['plugin'] ) ? array( $_GET['plugin'] ) : array();
       
    68 	} else {
       
    69 		$plugins = isset( $_POST['checked'] ) ? (array) $_POST['checked'] : array();
       
    70 	}
       
    71 
       
    72 	// Set basename if empty
       
    73 	if ( empty( $basename ) && !empty( $bbp->basename ) )
       
    74 		$basename = $bbp->basename;
       
    75 
       
    76 	// Bail if no basename
       
    77 	if ( empty( $basename ) )
       
    78 		return false;
       
    79 
       
    80 	// Is bbPress being activated?
       
    81 	return in_array( $basename, $plugins );
       
    82 }
       
    83 
       
    84 /**
       
    85  * Determine if bbPress is being deactivated
       
    86  *
       
    87  * @since bbPress (r3421)
       
    88  * @return bool True if deactivating bbPress, false if not
       
    89  */
       
    90 function bbp_is_deactivation( $basename = '' ) {
       
    91 	$bbp = bbpress();
       
    92 
       
    93 	$action = false;
       
    94 	if ( ! empty( $_REQUEST['action'] ) && ( '-1' != $_REQUEST['action'] ) )
       
    95 		$action = $_REQUEST['action'];
       
    96 	elseif ( ! empty( $_REQUEST['action2'] ) && ( '-1' != $_REQUEST['action2'] ) )
       
    97 		$action = $_REQUEST['action2'];
       
    98 
       
    99 	// Bail if not deactivating
       
   100 	if ( empty( $action ) || !in_array( $action, array( 'deactivate', 'deactivate-selected' ) ) )
       
   101 		return false;
       
   102 
       
   103 	// The plugin(s) being deactivated
       
   104 	if ( $action == 'deactivate' ) {
       
   105 		$plugins = isset( $_GET['plugin'] ) ? array( $_GET['plugin'] ) : array();
       
   106 	} else {
       
   107 		$plugins = isset( $_POST['checked'] ) ? (array) $_POST['checked'] : array();
       
   108 	}
       
   109 
       
   110 	// Set basename if empty
       
   111 	if ( empty( $basename ) && !empty( $bbp->basename ) )
       
   112 		$basename = $bbp->basename;
       
   113 
       
   114 	// Bail if no basename
       
   115 	if ( empty( $basename ) )
       
   116 		return false;
       
   117 
       
   118 	// Is bbPress being deactivated?
       
   119 	return in_array( $basename, $plugins );
       
   120 }
       
   121 
       
   122 /**
       
   123  * Update the DB to the latest version
       
   124  *
       
   125  * @since bbPress (r3421)
       
   126  * @uses update_option()
       
   127  * @uses bbp_get_db_version() To get bbPress's database version
       
   128  */
       
   129 function bbp_version_bump() {
       
   130 	$db_version = bbp_get_db_version();
       
   131 	update_option( '_bbp_db_version', $db_version );
       
   132 }
       
   133 
       
   134 /**
       
   135  * Setup the bbPress updater
       
   136  *
       
   137  * @since bbPress (r3419)
       
   138  *
       
   139  * @uses bbp_version_updater()
       
   140  * @uses bbp_version_bump()
       
   141  * @uses flush_rewrite_rules()
       
   142  */
       
   143 function bbp_setup_updater() {
       
   144 
       
   145 	// Bail if no update needed
       
   146 	if ( ! bbp_is_update() )
       
   147 		return;
       
   148 
       
   149 	// Call the automated updater
       
   150 	bbp_version_updater();
       
   151 }
       
   152 
       
   153 /**
       
   154  * Create a default forum, topic, and reply
       
   155  *
       
   156  * @since bbPress (r3767)
       
   157  * @param array $args Array of arguments to override default values
       
   158  */
       
   159 function bbp_create_initial_content( $args = array() ) {
       
   160 
       
   161 	$defaults = array(
       
   162 		'forum_parent'  => 0,
       
   163 		'forum_status'  => 'publish',
       
   164 		'forum_title'   => __( 'General',                                  'bbpress' ),
       
   165 		'forum_content' => __( 'General chit-chat',                        'bbpress' ),
       
   166 		'topic_title'   => __( 'Hello World!',                             'bbpress' ),
       
   167 		'topic_content' => __( 'I am the first topic in your new forums.', 'bbpress' ),
       
   168 		'reply_title'   => __( 'Re: Hello World!',                         'bbpress' ),
       
   169 		'reply_content' => __( 'Oh, and this is what a reply looks like.', 'bbpress' ),
       
   170 	);
       
   171 	$r = bbp_parse_args( $args, $defaults, 'create_initial_content' );
       
   172 	extract( $r );
       
   173 
       
   174 	// Create the initial forum
       
   175 	$forum_id = bbp_insert_forum( array(
       
   176 		'post_parent'  => $forum_parent,
       
   177 		'post_status'  => $forum_status,
       
   178 		'post_title'   => $forum_title,
       
   179 		'post_content' => $forum_content
       
   180 	) );
       
   181 
       
   182 	// Create the initial topic
       
   183 	$topic_id = bbp_insert_topic(
       
   184 		array(
       
   185 			'post_parent'  => $forum_id,
       
   186 			'post_title'   => $topic_title,
       
   187 			'post_content' => $topic_content
       
   188 		),
       
   189 		array( 'forum_id'  => $forum_id )
       
   190 	);
       
   191 
       
   192 	// Create the initial reply
       
   193 	$reply_id = bbp_insert_reply(
       
   194 		array(
       
   195 			'post_parent'  => $topic_id,
       
   196 			'post_title'   => $reply_title,
       
   197 			'post_content' => $reply_content
       
   198 		),
       
   199 		array(
       
   200 			'forum_id'     => $forum_id,
       
   201 			'topic_id'     => $topic_id
       
   202 		)
       
   203 	);
       
   204 
       
   205 	return array(
       
   206 		'forum_id' => $forum_id,
       
   207 		'topic_id' => $topic_id,
       
   208 		'reply_id' => $reply_id
       
   209 	);
       
   210 }
       
   211 
       
   212 /**
       
   213  * bbPress's version updater looks at what the current database version is, and
       
   214  * runs whatever other code is needed.
       
   215  *
       
   216  * This is most-often used when the data schema changes, but should also be used
       
   217  * to correct issues with bbPress meta-data silently on software update.
       
   218  *
       
   219  * @since bbPress (r4104)
       
   220  */
       
   221 function bbp_version_updater() {
       
   222 
       
   223 	// Get the raw database version
       
   224 	$raw_db_version = (int) bbp_get_db_version_raw();
       
   225 
       
   226 	/** 2.0 Branch ************************************************************/
       
   227 
       
   228 	// 2.0, 2.0.1, 2.0.2, 2.0.3
       
   229 	if ( $raw_db_version < 200 ) {
       
   230 		// Do nothing
       
   231 	}
       
   232 
       
   233 	/** 2.1 Branch ************************************************************/
       
   234 
       
   235 	// 2.1, 2.1.1
       
   236 	if ( $raw_db_version < 211 ) {
       
   237 
       
   238 		/**
       
   239 		 * Repair private and hidden forum data
       
   240 		 *
       
   241 		 * @link http://bbpress.trac.wordpress.org/ticket/1891
       
   242 		 */
       
   243 		bbp_admin_repair_forum_visibility();
       
   244 	}
       
   245 
       
   246 	/** 2.2 Branch ************************************************************/
       
   247 
       
   248 	// 2.2
       
   249 	if ( $raw_db_version < 220 ) {
       
   250 
       
   251 		// Remove bbPress 1.1 roles (BuddyPress)
       
   252 		remove_role( 'member'    );
       
   253 		remove_role( 'inactive'  );
       
   254 		remove_role( 'blocked'   );
       
   255 		remove_role( 'moderator' );
       
   256 		remove_role( 'keymaster' );
       
   257 
       
   258 		// Refresh capabilities
       
   259 		bbp_remove_caps();
       
   260 	}
       
   261 
       
   262 	/** All done! *************************************************************/
       
   263 
       
   264 	// Bump the version
       
   265 	bbp_version_bump();
       
   266 
       
   267 	// Delete rewrite rules to force a flush
       
   268 	bbp_delete_rewrite_rules();
       
   269 }
       
   270 
       
   271 /**
       
   272  * Redirect user to bbPress's What's New page on activation
       
   273  *
       
   274  * @since bbPress (r4389)
       
   275  *
       
   276  * @internal Used internally to redirect bbPress to the about page on activation
       
   277  *
       
   278  * @uses is_network_admin() To bail if being network activated
       
   279  * @uses set_transient() To drop the activation transient for 30 seconds
       
   280  *
       
   281  * @return If network admin or bulk activation
       
   282  */
       
   283 function bbp_add_activation_redirect() {
       
   284 
       
   285 	// Bail if activating from network, or bulk
       
   286 	if ( is_network_admin() || isset( $_GET['activate-multi'] ) )
       
   287 		return;
       
   288 
       
   289 	// Add the transient to redirect
       
   290     set_transient( '_bbp_activation_redirect', true, 30 );
       
   291 }