/**
 * Get term data for terms in BuddyPress taxonomies.
 *
 * Note that term data is from the `bp_get_taxonomy_term_site_id()`, which on some
 * multisite configurations may not be the same as the current site.
 *
 * @since 2.7.0
 *
 * @see get_term_by() for a full description of function and parameters.
 *
 * @param string     $field    Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id'
 * @param string|int $value    Search for this term value
 * @param string     $taxonomy Taxonomy name. Optional, if `$field` is 'term_taxonomy_id'.
 * @param string     $output   Constant OBJECT, ARRAY_A, or ARRAY_N
 * @param string     $filter   Optional, default is raw or no WordPress defined filter will applied.
 *
 * @return WP_Term|bool WP_Term instance on success. Will return false if `$taxonomy` does not exist
 *                      or `$term` was not found.
 */
function bp_get_term_by($field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw')
{
    $site_id = bp_get_taxonomy_term_site_id($taxonomy);
    $switched = false;
    if ($site_id !== get_current_blog_id()) {
        switch_to_blog($site_id);
        bp_register_taxonomies();
        $switched = true;
    }
    $term = get_term_by($field, $value, $taxonomy, $output, $filter);
    if ($switched) {
        restore_current_blog();
    }
    return $term;
}
/**
 * Initialize an update or installation of BuddyPress.
 *
 * BuddyPress's version updater looks at what the current database version is,
 * and runs whatever other code is needed - either the "update" or "install"
 * code.
 *
 * This is most often used when the data schema changes, but should also be used
 * to correct issues with BuddyPress metadata silently on software update.
 *
 * @since 1.7.0
 */
function bp_version_updater()
{
    // Get the raw database version.
    $raw_db_version = (int) bp_get_db_version_raw();
    /**
     * Filters the default components to activate for a new install.
     *
     * @since 1.7.0
     *
     * @param array $value Array of default components to activate.
     */
    $default_components = apply_filters('bp_new_install_default_components', array('activity' => 1, 'members' => 1, 'settings' => 1, 'xprofile' => 1, 'notifications' => 1));
    require_once ABSPATH . 'wp-admin/includes/upgrade.php';
    require_once buddypress()->plugin_dir . '/bp-core/admin/bp-core-admin-schema.php';
    $switched_to_root_blog = false;
    // Make sure the current blog is set to the root blog.
    if (!bp_is_root_blog()) {
        switch_to_blog(bp_get_root_blog_id());
        bp_register_taxonomies();
        $switched_to_root_blog = true;
    }
    // Install BP schema and activate only Activity and XProfile.
    if (bp_is_install()) {
        // Apply schema and set Activity and XProfile components as active.
        bp_core_install($default_components);
        bp_update_option('bp-active-components', $default_components);
        bp_core_add_page_mappings($default_components, 'delete');
        bp_core_install_emails();
        // Upgrades.
    } else {
        // Run the schema install to update tables.
        bp_core_install();
        // Version 1.5.0.
        if ($raw_db_version < 1801) {
            bp_update_to_1_5();
            bp_core_add_page_mappings($default_components, 'delete');
        }
        // Version 1.6.0.
        if ($raw_db_version < 6067) {
            bp_update_to_1_6();
        }
        // Version 1.9.0.
        if ($raw_db_version < 7553) {
            bp_update_to_1_9();
        }
        // Version 1.9.2.
        if ($raw_db_version < 7731) {
            bp_update_to_1_9_2();
        }
        // Version 2.0.0.
        if ($raw_db_version < 7892) {
            bp_update_to_2_0();
        }
        // Version 2.0.1.
        if ($raw_db_version < 8311) {
            bp_update_to_2_0_1();
        }
        // Version 2.2.0.
        if ($raw_db_version < 9181) {
            bp_update_to_2_2();
        }
        // Version 2.3.0.
        if ($raw_db_version < 9615) {
            bp_update_to_2_3();
        }
        // Version 2.5.0.
        if ($raw_db_version < 10440) {
            bp_update_to_2_5();
        }
        // Version 2.7.0.
        if ($raw_db_version < 11105) {
            bp_update_to_2_7();
        }
    }
    /* All done! *************************************************************/
    // Bump the version.
    bp_version_bump();
    if ($switched_to_root_blog) {
        restore_current_blog();
    }
}
/**
 * Delete emails and restore from defaults.
 *
 * @since 2.5.0
 *
 * @return array
 */
function bp_admin_reinstall_emails()
{
    $switched = false;
    // Switch to the root blog, where the email posts live.
    if (!bp_is_root_blog()) {
        switch_to_blog(bp_get_root_blog_id());
        bp_register_taxonomies();
        $switched = true;
    }
    $emails = get_posts(array('fields' => 'ids', 'post_status' => 'publish', 'post_type' => bp_get_email_post_type(), 'posts_per_page' => 200, 'suppress_filters' => false));
    if ($emails) {
        foreach ($emails as $email_id) {
            wp_trash_post($email_id);
        }
    }
    // Make sure we have no orphaned email type terms.
    $email_types = get_terms(bp_get_email_tax_type(), array('fields' => 'ids', 'hide_empty' => false, 'update_term_meta_cache' => false));
    if ($email_types) {
        foreach ($email_types as $term_id) {
            wp_delete_term((int) $term_id, bp_get_email_tax_type());
        }
    }
    require_once buddypress()->plugin_dir . '/bp-core/admin/bp-core-admin-schema.php';
    bp_core_install_emails();
    if ($switched) {
        restore_current_blog();
    }
    return array(0, __('Emails have been successfully reinstalled.', 'buddypress'));
}
/**
 * Remove taxonomy terms on a BuddyPress object.
 *
 * @since 2.3.0
 *
 * @see wp_remove_object_terms() for a full description of function and parameters.
 *
 * @param int          $object_id Object ID.
 * @param string|array $terms     Term or terms to remove.
 * @param string       $taxonomy  Taxonomy name.
 * @return bool|WP_Error True on success, false or WP_Error on failure.
 */
function bp_remove_object_terms($object_id, $terms, $taxonomy)
{
    $is_root_blog = bp_is_root_blog();
    if (!$is_root_blog) {
        switch_to_blog(bp_get_root_blog_id());
        bp_register_taxonomies();
    }
    $retval = wp_remove_object_terms($object_id, $terms, $taxonomy);
    if (!$is_root_blog) {
        restore_current_blog();
    }
    return $retval;
}