function bp_blogs_install() { global $wpdb, $bp; if (!empty($wpdb->charset)) { $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}"; } $sql[] = "CREATE TABLE {$bp->blogs->table_name} (\n\t\t \t\tid bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,\n\t\t\t\tuser_id bigint(20) NOT NULL,\n\t\t\t\tblog_id bigint(20) NOT NULL,\n\t\t\t\tKEY user_id (user_id),\n\t\t\t\tKEY blog_id (blog_id)\n\t\t\t ) {$charset_collate};"; $sql[] = "CREATE TABLE {$bp->blogs->table_name_blog_posts} (\n\t\t \t\tid bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,\n\t\t\t\tuser_id bigint(20) NOT NULL,\n\t\t\t\tblog_id bigint(20) NOT NULL,\n\t\t\t\tpost_id bigint(20) NOT NULL,\n\t\t\t\tdate_created datetime NOT NULL,\n\t\t\t\tKEY user_id (user_id),\n\t\t\t\tKEY blog_id (blog_id),\n\t\t\t\tKEY post_id (post_id)\n\t\t\t ) {$charset_collate};"; $sql[] = "CREATE TABLE {$bp->blogs->table_name_blog_comments} (\n\t\t \t\tid bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,\n\t\t\t\tuser_id bigint(20) NOT NULL,\n\t\t\t\tblog_id bigint(20) NOT NULL,\n\t\t\t\tcomment_id bigint(20) NOT NULL,\n\t\t\t\tcomment_post_id bigint(20) NOT NULL,\n\t\t\t\tdate_created datetime NOT NULL,\n\t\t\t\tKEY user_id (user_id),\n\t\t\t\tKEY blog_id (blog_id),\n\t\t\t\tKEY comment_id (comment_id),\n\t\t\t\tKEY comment_post_id (comment_post_id)\n\t\t\t ) {$charset_collate};"; $sql[] = "CREATE TABLE {$bp->blogs->table_name_blogmeta} (\n\t\t\tid bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,\n\t\t\tblog_id bigint(20) NOT NULL,\n\t\t\tmeta_key varchar(255) DEFAULT NULL,\n\t\t\tmeta_value longtext DEFAULT NULL,\n\t\t\tKEY blog_id (blog_id),\n\t\t\tKEY meta_key (meta_key)\n\t\t ) {$charset_collate};"; require_once ABSPATH . 'wp-admin/upgrade-functions.php'; dbDelta($sql); // On first installation - record all existing blogs in the system. if (!(int) get_site_option('bp-blogs-first-install')) { bp_blogs_record_existing_blogs(); add_site_option('bp-blogs-first-install', 1); } else { // Import blog titles and descriptions into the blogmeta table if (get_site_option('bp-blogs-version') <= '0.1.5') { $blog_ids = $wpdb->get_col($wpdb->prepare("SELECT blog_id FROM " . $bp->blogs->table_name)); for ($i = 0; $i < count($blog_ids); $i++) { $name = get_blog_option($blog_ids[$i], 'blogname'); $desc = get_blog_option($blog_ids[$i], 'blogdescription'); bp_blogs_update_blogmeta($blog_ids[$i], 'name', $name); bp_blogs_update_blogmeta($blog_ids[$i], 'description', $desc); bp_blogs_update_blogmeta($blog_ids[$i], 'last_activity', time()); } } } update_site_option('bp-blogs-db-version', BP_BLOGS_DB_VERSION); }
function bp_blogs_install() { global $wpdb, $bp; if ( !empty($wpdb->charset) ) $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset"; $sql[] = "CREATE TABLE {$bp->blogs->table_name} ( id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY, user_id bigint(20) NOT NULL, blog_id bigint(20) NOT NULL, KEY user_id (user_id), KEY blog_id (blog_id) ) {$charset_collate};"; $sql[] = "CREATE TABLE {$bp->blogs->table_name_blogmeta} ( id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY, blog_id bigint(20) NOT NULL, meta_key varchar(255) DEFAULT NULL, meta_value longtext DEFAULT NULL, KEY blog_id (blog_id), KEY meta_key (meta_key) ) {$charset_collate};"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); // On first installation - record all existing blogs in the system. if ( !(int)get_site_option('bp-blogs-first-install') && bp_core_is_multisite() ) { bp_blogs_record_existing_blogs(); add_site_option( 'bp-blogs-first-install', 1 ); } update_site_option( 'bp-blogs-db-version', BP_BLOGS_DB_VERSION ); }
/** * Recalculate user-to-blog relationships and useful blog meta data * * @since BuddyPress (2.1.0) * * @return array */ function bp_admin_repair_blog_records() { // Description of this tool, displayed to the user $statement = __('Repopulating Blogs records… %s', 'buddypress'); // Default to failure text $result = __('Failed!', 'buddypress'); // Default to unrepaired $repair = false; // Run function if blogs component is active if (bp_is_active('blogs')) { $repair = bp_blogs_record_existing_blogs(); } // Setup success/fail messaging if (true === $repair) { $result = __('Complete!', 'buddypress'); } // All done! return array(0, sprintf($statement, $result)); }
function bp_update_db_stuff() { $bp_prefix = bp_core_get_table_prefix(); // Rename the old user activity cached table if needed. if ($nxtdb->get_var("SHOW TABLES LIKE '%{$bp_prefix}bp_activity_user_activity_cached%'")) { $nxtdb->query("RENAME TABLE {$bp_prefix}bp_activity_user_activity_cached TO {$bp->activity->table_name}"); } // Rename fields from pre BP 1.2 if ($nxtdb->get_var("SHOW TABLES LIKE '%{$bp->activity->table_name}%'")) { if ($nxtdb->get_var("SHOW COLUMNS FROM {$bp->activity->table_name} LIKE 'component_action'")) { $nxtdb->query("ALTER TABLE {$bp->activity->table_name} CHANGE component_action type varchar(75) NOT NULL"); } if ($nxtdb->get_var("SHOW COLUMNS FROM {$bp->activity->table_name} LIKE 'component_name'")) { $nxtdb->query("ALTER TABLE {$bp->activity->table_name} CHANGE component_name component varchar(75) NOT NULL"); } } // On first installation - record all existing blogs in the system. if (!(int) $bp->site_options['bp-blogs-first-install']) { bp_blogs_record_existing_blogs(); bp_update_option('bp-blogs-first-install', 1); } if (is_multisite()) { bp_core_add_illegal_names(); } // Update and remove the message threads table if it exists if ($nxtdb->get_var("SHOW TABLES LIKE '%{$bp_prefix}bp_messages_threads%'")) { $update = BP_Messages_Thread::update_tables(); if ($update) { $nxtdb->query("DROP TABLE {$bp_prefix}bp_messages_threads"); } } }
/** * Verify that some BP prerequisites are set up properly, and notify the admin if not. * * On every Dashboard page, this function checks the following: * - that pretty permalinks are enabled. * - that every BP component that needs a WP page for a directory has one. * - that no WP page has multiple BP components associated with it. * The administrator will be shown a notice for each check that fails. * * @global WPDB $wpdb WordPress DB object * @global WP_Rewrite $wp_rewrite * * @since 1.2.0 */ function bp_core_activation_notice() { global $wp_rewrite, $wpdb; // Only the super admin gets warnings. if (!bp_current_user_can('bp_moderate')) { return; } // Bail in user admin. if (is_user_admin()) { return; } // On multisite installs, don't load on a non-root blog, unless do_network_admin is overridden. if (is_multisite() && bp_core_do_network_admin() && !bp_is_root_blog()) { return; } // Bail if in network admin, and BuddyPress is not network activated. if (is_network_admin() && !bp_is_network_activated()) { return; } /** * Check to make sure that the blog setup routine has run. This can't * happen during the wizard because of the order which the components * are loaded. */ if (bp_is_active('blogs')) { $bp = buddypress(); $count = $wpdb->get_var("SELECT COUNT(*) FROM {$bp->blogs->table_name}"); if (empty($count)) { bp_blogs_record_existing_blogs(); } } // Add notice if no rewrite rules are enabled. if (empty($wp_rewrite->permalink_structure)) { bp_core_add_admin_notice(sprintf(__('<strong>BuddyPress is almost ready</strong>. You must <a href="%s">update your permalink structure</a> to something other than the default for it to work.', 'buddypress'), admin_url('options-permalink.php')), 'error'); } // Get BuddyPress instance. $bp = buddypress(); /** * Check for orphaned BP components (BP component is enabled, no WP page exists). */ $orphaned_components = array(); $wp_page_components = array(); // Only components with 'has_directory' require a WP page to function. foreach (array_keys($bp->loaded_components) as $component_id) { if (!empty($bp->{$component_id}->has_directory)) { $wp_page_components[] = array('id' => $component_id, 'name' => isset($bp->{$component_id}->name) ? $bp->{$component_id}->name : ucwords($bp->{$component_id}->id)); } } // Activate and Register are special cases. They are not components but they need WP pages. // If user registration is disabled, we can skip this step. if (bp_get_signup_allowed()) { $wp_page_components[] = array('id' => 'activate', 'name' => __('Activate', 'buddypress')); $wp_page_components[] = array('id' => 'register', 'name' => __('Register', 'buddypress')); } // On the first admin screen after a new installation, this isn't set, so grab it to suppress // a misleading error message. if (empty($bp->pages->members)) { $bp->pages = bp_core_get_directory_pages(); } foreach ($wp_page_components as $component) { if (!isset($bp->pages->{$component['id']})) { $orphaned_components[] = $component['name']; } } // Special case: If the Forums component is orphaned, but the bbPress 1.x installation is // not correctly set up, don't show a nag. (In these cases, it's probably the case that the // user is using bbPress 2.x; see https://buddypress.trac.wordpress.org/ticket/4292. if (isset($bp->forums->name) && in_array($bp->forums->name, $orphaned_components) && !bp_forums_is_installed_correctly()) { $forum_key = array_search($bp->forums->name, $orphaned_components); unset($orphaned_components[$forum_key]); $orphaned_components = array_values($orphaned_components); } if (!empty($orphaned_components)) { $admin_url = bp_get_admin_url(add_query_arg(array('page' => 'bp-page-settings'), 'admin.php')); $notice = sprintf(__('The following active BuddyPress Components do not have associated WordPress Pages: %2$s. <a href="%1$s">Repair</a>', 'buddypress'), esc_url($admin_url), '<strong>' . implode('</strong>, <strong>', $orphaned_components) . '</strong>'); bp_core_add_admin_notice($notice); } // BP components cannot share a single WP page. Check for duplicate assignments, and post a message if found. $dupe_names = array(); $page_ids = (array) bp_core_get_directory_page_ids(); $dupes = array_diff_assoc($page_ids, array_unique($page_ids)); if (!empty($dupes)) { foreach (array_keys($dupes) as $dupe_component) { $dupe_names[] = $bp->pages->{$dupe_component}->title; } // Make sure that there are no duplicate duplicates :). $dupe_names = array_unique($dupe_names); } // If there are duplicates, post a message about them. if (!empty($dupe_names)) { $admin_url = bp_get_admin_url(add_query_arg(array('page' => 'bp-page-settings'), 'admin.php')); $notice = sprintf(__('Each BuddyPress Component needs its own WordPress page. The following WordPress Pages have more than one component associated with them: %2$s. <a href="%1$s">Repair</a>', 'buddypress'), esc_url($admin_url), '<strong>' . implode('</strong>, <strong>', $dupe_names) . '</strong>'); bp_core_add_admin_notice($notice); } }
/** * @group get_order_by */ public function test_get_order_by() { if (!is_multisite()) { return; } $old_user = get_current_user_id(); $u = $this->factory->user->create(); $this->set_current_user($u); $bs = array('foobar' => $this->factory->blog->create(array('title' => 'Foo Bar Blog', 'user_id' => $u, 'path' => '/path' . rand() . time() . '/')), 'barfoo' => $this->factory->blog->create(array('title' => 'Bar foo Blog', 'user_id' => $u, 'path' => '/path' . rand() . time() . '/'))); bp_blogs_record_existing_blogs(); // make the blog public or it won't turn up in generic results foreach ($bs as $b) { update_blog_option($b, 'blog_public', '1'); } // Used to make sure barfoo is older than foobar $b_time = date_i18n('Y-m-d H:i:s', strtotime('-5 minutes')); /* Alphabetical */ $blogs = BP_Blogs_Blog::get('alphabetical', false, false, $u); $blog_ids = wp_list_pluck($blogs['blogs'], 'blog_id'); $this->assertEquals(array($bs['barfoo'], $bs['foobar']), $blog_ids); /* Newest */ update_blog_details($bs['barfoo'], array('registered' => $b_time)); $blogs = BP_Blogs_Blog::get('newest', false, false, $u); $blog_ids = wp_list_pluck($blogs['blogs'], 'blog_id'); $this->assertEquals(array($bs['foobar'], $bs['barfoo']), $blog_ids); /* Active */ bp_blogs_update_blogmeta($bs['barfoo'], 'last_activity', $b_time); $blogs = BP_Blogs_Blog::get('active', false, false, $u); $blog_ids = wp_list_pluck($blogs['blogs'], 'blog_id'); $this->assertEquals(array($bs['foobar'], $bs['barfoo']), $blog_ids); /* Random */ $blogs = BP_Blogs_Blog::get('random', false, false, $u); $this->assertTrue(2 == count($blogs['blogs'])); $this->set_current_user($old_user); }
/** * @group bp_blogs_record_existing_blogs */ public function test_bp_blogs_record_existing_blogs_limit() { if (!is_multisite()) { return; } $old_user = get_current_user_id(); $u = $this->factory->user->create(); $this->set_current_user($u); // Create three sites. $this->factory->blog->create_many(3, array('user_id' => $u)); // Record each site one at a time bp_blogs_record_existing_blogs(array('limit' => 1)); // Assert! $blogs = bp_blogs_get_blogs(array('user_id' => $u)); $this->assertSame(3, (int) $blogs['total']); $this->set_current_user($old_user); }
/** * Verify that some BP prerequisites are set up properly, and notify the admin if not * * On every Dashboard page, this function checks the following: * - that pretty permalinks are enabled * - that a BP-compatible theme is activated * - that every BP component that needs a WP page for a directory has one * - that no WP page has multiple BP components associated with it * The administrator will be shown a notice for each check that fails. * * @package BuddyPress Core */ function bp_core_activation_notice() { global $wp_rewrite, $wpdb, $bp; // Only the super admin gets warnings if (!bp_current_user_can('bp_moderate')) { return; } // On multisite installs, don't load on a non-root blog, unless do_network_admin is // overridden if (is_multisite() && bp_core_do_network_admin() && !bp_is_root_blog()) { return; } // Don't show these messages during setup or upgrade if (!empty($bp->maintenance_mode)) { return; } /** * Check to make sure that the blog setup routine has run. This can't happen during the * wizard because of the order which the components are loaded. We check for multisite here * on the off chance that someone has activated the blogs component and then disabled MS */ if (bp_is_active('blogs')) { $count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$bp->blogs->table_name}")); if (empty($count)) { bp_blogs_record_existing_blogs(); } } /** * Are pretty permalinks enabled? */ if (isset($_POST['permalink_structure'])) { return false; } if (empty($wp_rewrite->permalink_structure)) { bp_core_add_admin_notice(sprintf(__('<strong>BuddyPress is almost ready</strong>. You must <a href="%s">update your permalink structure</a> to something other than the default for it to work.', 'buddypress'), admin_url('options-permalink.php'))); } /** * Are you using a BP-compatible theme? */ // Get current theme info $ct = wp_get_theme(); // Make sure tags is an array to suppress notices if (!isset($ct->tags)) { $ct->tags = array(); } else { $ct->tags = (array) $ct->tags; } // The best way to remove this notice is to add a "buddypress" tag to // your active theme's CSS header. if (!defined('BP_SILENCE_THEME_NOTICE') && !in_array('buddypress', $ct->tags)) { bp_core_add_admin_notice(sprintf(__("You'll need to <a href='%s'>activate a <strong>BuddyPress-compatible theme</strong></a> to take advantage of all of BuddyPress's features. We've bundled a default theme, but you can always <a href='%s'>install some other compatible themes</a> or <a href='%s'>update your existing WordPress theme</a>.", 'buddypress'), admin_url('themes.php'), network_admin_url('theme-install.php?type=tag&s=buddypress&tab=search'), network_admin_url('plugin-install.php?type=term&tab=search&s=%22bp-template-pack%22'))); } /** * Check for orphaned BP components (BP component is enabled, no WP page exists) */ $orphaned_components = array(); $wp_page_components = array(); // Only components with 'has_directory' require a WP page to function foreach ($bp->loaded_components as $component_id => $is_active) { if (!empty($bp->{$component_id}->has_directory)) { $wp_page_components[] = array('id' => $component_id, 'name' => isset($bp->{$component_id}->name) ? $bp->{$component_id}->name : ucwords($bp->{$component_id}->id)); } } // Activate and Register are special cases. They are not components but they need WP pages. // If user registration is disabled, we can skip this step. if (bp_get_signup_allowed()) { $wp_page_components[] = array('id' => 'activate', 'name' => __('Activate', 'buddypress')); $wp_page_components[] = array('id' => 'register', 'name' => __('Register', 'buddypress')); } foreach ($wp_page_components as $component) { if (!isset($bp->pages->{$component['id']})) { $orphaned_components[] = $component['name']; } } // Special case: If the Forums component is orphaned, but the bbPress 1.x installation is // not correctly set up, don't show a nag. (In these cases, it's probably the case that the // user is using bbPress 2.x; see https://buddypress.trac.wordpress.org/ticket/4292 if (isset($bp->forums->name) && in_array($bp->forums->name, $orphaned_components) && !bp_forums_is_installed_correctly()) { $forum_key = array_search($bp->forums->name, $orphaned_components); unset($orphaned_components[$forum_key]); $orphaned_components = array_values($orphaned_components); } if (!empty($orphaned_components)) { $admin_url = bp_get_admin_url(add_query_arg(array('page' => 'bp-page-settings'), 'admin.php')); $notice = sprintf(__('The following active BuddyPress Components do not have associated WordPress Pages: %2$s. <a href="%1$s" class="button-secondary">Repair</a>', 'buddypress'), $admin_url, '<strong>' . implode('</strong>, <strong>', $orphaned_components) . '</strong>'); bp_core_add_admin_notice($notice); } /** * BP components cannot share a single WP page. Check for duplicate assignments, and post * a message if found. */ $dupe_names = array(); $page_ids = (array) bp_core_get_directory_page_ids(); $dupes = array_diff_assoc($page_ids, array_unique($page_ids)); if (!empty($dupes)) { foreach ($dupes as $dupe_component => $dupe_id) { $dupe_names[] = $bp->pages->{$dupe_component}->title; } // Make sure that there are no duplicate duplicates :) $dupe_names = array_unique($dupe_names); } // If there are duplicates, post a message about them if (!empty($dupe_names)) { $admin_url = bp_get_admin_url(add_query_arg(array('page' => 'bp-page-settings'), 'admin.php')); $notice = sprintf(__('Each BuddyPress Component needs its own WordPress page. The following WordPress Pages have more than one component associated with them: %2$s. <a href="%1$s" class="button-secondary">Repair</a>', 'buddypress'), $admin_url, '<strong>' . implode('</strong>, <strong>', $dupe_names) . '</strong>'); bp_core_add_admin_notice($notice); } }
/** * Populate the BP blogs table with existing blogs. * * Warning: By default, this will remove all existing records from the BP * blogs and blogmeta tables before re-populating the tables. * * @since 1.0.0 * @since 2.6.0 Accepts $args as a parameter. * * @param array $args { * Array of arguments. * @type int $offset The offset to use. * @type int $limit The number of blogs to record at one time. * @type array $blog_ids Blog IDs to record. If empty, all blogs will be recorded. * @type array $site_id The network site ID to use. * } * * @return bool */ function bp_blogs_record_existing_blogs($args = array()) { global $wpdb; // Query for all sites in network. $r = bp_parse_args($args, array('offset' => false === bp_get_option('_bp_record_blogs_offset') ? 0 : bp_get_option('_bp_record_blogs_offset'), 'limit' => 50, 'blog_ids' => array(), 'site_id' => $wpdb->siteid), 'record_existing_blogs'); // Truncate all BP blogs tables if starting fresh if (empty($r['offset']) && empty($r['blog_ids'])) { $bp = buddypress(); // Truncate user blogs table $truncate = $wpdb->query("TRUNCATE {$bp->blogs->table_name}"); if (is_wp_error($truncate)) { return false; } // Truncate user blogmeta table $truncate = $wpdb->query("TRUNCATE {$bp->blogs->table_name_blogmeta}"); if (is_wp_error($truncate)) { return false; } } // Multisite if (is_multisite()) { $sql = array(); $sql['select'] = $wpdb->prepare("SELECT blog_id, last_updated FROM {$wpdb->base_prefix}blogs WHERE mature = 0 AND spam = 0 AND deleted = 0 AND site_id = %d", $r['site_id']); // Omit root blog if large network if (wp_is_large_network('users')) { $sql['omit_root_blog'] = $wpdb->prepare("AND blog_id != %d", bp_get_root_blog_id()); } // Filter by selected blog IDs if (!empty($r['blog_ids'])) { $in = implode(',', wp_parse_id_list($r['blog_ids'])); $sql['in'] = "AND blog_id IN ({$in})"; } $sql['orderby'] = 'ORDER BY blog_id ASC'; $sql['limit'] = $wpdb->prepare("LIMIT %d", $r['limit']); if (!empty($r['offset'])) { $sql['offset'] = $wpdb->prepare("OFFSET %d", $r['offset']); } $blogs = $wpdb->get_results(implode(' ', $sql)); // Record a single site. } else { // Just record blog for the current user only. $record = bp_blogs_record_blog($wpdb->blogid, get_current_user_id(), true); if (false === $record) { return false; } else { return true; } } // Bail if there are no blogs if (empty($blogs)) { // Make sure we remove our offset marker if (is_multisite()) { bp_delete_option('_bp_record_blogs_offset'); } return false; } // Loop through users of blogs and record the relationship. foreach ((array) $blogs as $blog) { // Ensure that the cache is clear after the table TRUNCATE above. wp_cache_delete($blog->blog_id, 'blog_meta'); // Get all users. $users = get_users(array('blog_id' => $blog->blog_id, 'fields' => 'ID')); // Continue on if no users exist for this site (how did this happen?). if (empty($users)) { continue; } // Loop through users and record their relationship to this blog. foreach ((array) $users as $user_id) { bp_blogs_add_user_to_blog($user_id, false, $blog->blog_id); // Clear cache bp_blogs_clear_blog_object_cache($blog->blog_id, $user_id); } // Update blog last activity timestamp if (!empty($blog->last_updated) && false !== strtotime($blog->last_updated)) { bp_blogs_update_blogmeta($blog->blog_id, 'last_activity', $blog->last_updated); } } // See if we need to do this again if (is_multisite() && empty($r['blog_ids'])) { $sql['offset'] = $wpdb->prepare(" OFFSET %d", $r['limit'] + $r['offset']); // Check if there are more blogs to record $blog_ids = $wpdb->get_results(implode(' ', $sql)); // We have more blogs; record offset and re-run function if (!empty($blog_ids)) { bp_update_option('_bp_record_blogs_offset', $r['limit'] + $r['offset']); bp_blogs_record_existing_blogs(array('offset' => $r['limit'] + $r['offset'], 'limit' => $r['limit'], 'blog_ids' => $r['blog_ids'], 'site_id' => $r['site_id'])); // Bail since we have more blogs to record. return; // No more blogs; delete offset marker } else { bp_delete_option('_bp_record_blogs_offset'); } } /** * Fires after the BP blogs tables have been populated with existing blogs. * * @since 2.4.0 */ do_action('bp_blogs_recorded_existing_blogs'); // No errors. return true; }