public static function wpTearDownAfterClass() { foreach (self::$site_ids as $id) { wpmu_delete_blog($id, true); } wp_update_network_site_counts(); }
public static function wpTearDownAfterClass() { global $wpdb; foreach (self::$site_ids as $id) { wpmu_delete_blog($id, true); } foreach (self::$network_ids as $id) { $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id)); $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->site} WHERE id= %d", $id)); } wp_update_network_site_counts(); }
/** * Update the count of sites for the current network. * * If enabled through the 'enable_live_network_counts' filter, update the sites count * on a network when a site is created or its status is updated. * * @since 3.7.0 */ function wp_maybe_update_network_site_counts() { $is_small_network = !wp_is_large_network('sites'); /** * Filter whether to update network site or user counts when a new site is created. * * @since 3.7.0 * * @see wp_is_large_network() * * @param bool $small_network Whether the network is considered small. * @param string $context Context. Either 'users' or 'sites'. */ if (!apply_filters('enable_live_network_counts', $is_small_network, 'sites')) { return; } wp_update_network_site_counts(); }
/** * Move a blog from one network to another * * @since 1.3 * * @param integer $site_id ID of blog to move * @param integer $new_network_id ID of destination network */ function move_site($site_id, $new_network_id) { global $wpdb; // Get the site $site = get_blog_details($site_id); // Bail if site does not exist if (empty($site)) { return new WP_Error('blog_not_exist', __('Site does not exist.', 'wp-multi-network')); } // Main sites cannot be moved, to prevent breakage if (is_main_site_for_network($site->blog_id)) { return true; } // Cast new network ID $new_network_id = (int) $new_network_id; // Return early if site does not need to be moved if ($new_network_id === (int) $site->site_id) { return true; } // Store the old network ID for using later $old_network_id = (int) $site->site_id; $old_network = wp_get_network($old_network_id); // No change if ($old_network_id === $new_network_id) { return new WP_Error('blog_not_moved', __('Site was not moved.', 'wp-multi-network')); } // New network is not zero if (0 !== $new_network_id) { $new_network = wp_get_network($new_network_id); if (empty($new_network)) { return new WP_Error('network_not_exist', __('Network does not exist.', 'wp-multi-network')); } // Tweak the domain and path if needed // If the site domain is the same as the network domain on a subdomain // install, don't prepend old "hostname" if (is_subdomain_install() && $site->domain !== $old_network->domain) { $ex_dom = substr($site->domain, 0, strpos($site->domain, '.') + 1); $domain = $ex_dom . $new_network->domain; } else { $domain = $new_network->domain; } $path = substr($site->path, strlen($old_network->path)); // New network is zero (orphan) } else { // Mock a zero network object $new_network = new WP_Network((object) array('domain' => 'network.zero', 'path' => '/', 'id' => '0')); // Set domain & path to that of the current site $domain = $site->domain; $path = $site->path; } // Move the site is the blogs table $where = array('blog_id' => $site->blog_id); $update = array('site_id' => $new_network->id, 'domain' => $domain, 'path' => $path); $update_result = $wpdb->update($wpdb->blogs, $update, $where); // Bail if site could not be moved if (empty($update_result)) { return new WP_Error('blog_not_moved', __('Site could not be moved.', 'wp-multi-network')); } // Update old network count if (0 !== $old_network_id) { switch_to_network($old_network_id); wp_update_network_site_counts(); restore_current_network(); } // Update new network count if (0 !== $new_network_id) { switch_to_network($new_network_id); wp_update_network_site_counts(); restore_current_network(); } // Change relevant blog options $options_table = $wpdb->get_blog_prefix($site->blog_id) . 'options'; $old_domain = $old_network->domain . $old_network->path; $new_domain = $new_network->domain . $new_network->path; // Update all site options foreach (network_options_list() as $option_name) { $sql = "SELECT * FROM {$options_table} WHERE option_name = %s"; $prep = $wpdb->prepare($sql, $option_name); $option = $wpdb->get_row($prep); // No value, so skip it if (empty($option)) { continue; } $new_value = str_replace($old_domain, $new_domain, $option->option_value); update_blog_option($site->blog_id, $option_name, $new_value); } // Delete rewrite rules for site at old URL delete_blog_option($site_id, 'rewrite_rules'); // Site moved do_action('move_site', $site_id, $old_network_id, $new_network_id); }
/** * Update the count of sites for the current network. * * If enabled through the 'enable_live_network_counts' filter, update the sites count * on a network when a site is created or its status is updated. * * @since 3.7.0 * * @uses wp_update_network_site_counts() */ function wp_maybe_update_network_site_counts() { $is_small_network = !wp_is_large_network('sites'); /** * Filter the decision to update network user and site counts in real time. * * @since 3.7.0 * * @param bool $small_network Based on wp_is_large_network( $context ). * @param string $context Context. Either 'users' or 'sites'. */ if (!apply_filters('enable_live_network_counts', $is_small_network, 'sites')) { return; } wp_update_network_site_counts(); }
/** * Move a site to a new network * * @since 1.3 * * @param integer $site_id ID of site to move * @param integer $new_network_id ID of destination network */ function move_site($site_id = 0, $new_network_id = 0) { global $wpdb; // Get the site $site = get_blog_details($site_id); // Bail if site does not exist if (empty($site)) { return new WP_Error('blog_not_exist', __('Site does not exist.', 'wp-multi-network')); } // Main sites cannot be moved, to prevent breakage if (is_main_site_for_network($site->blog_id)) { return true; } // Cast new network ID $new_network_id = (int) $new_network_id; // Return early if site does not need to be moved if ($new_network_id === (int) $site->site_id) { return true; } // Move the site is the blogs table $where = array('blog_id' => $site->blog_id); $update = array('site_id' => $new_network_id); $result = $wpdb->update($wpdb->blogs, $update, $where); // Bail if site could not be moved if (empty($result)) { return new WP_Error('blog_not_moved', __('Site could not be moved.', 'wp-multi-network')); } // Update old network count if (0 !== $site->site_id) { switch_to_network($site->site_id); wp_update_network_site_counts(); restore_current_network(); } // Update new network count if (0 !== $new_network_id) { switch_to_network($new_network_id); wp_update_network_site_counts(); restore_current_network(); } // Refresh blog details refresh_blog_details($site_id); // Clean network caches clean_network_cache(array_filter(array($site->site_id, $new_network_id))); // Site moved do_action('move_site', $site_id, $site->site_id, $new_network_id); // Return the new network ID as confirmation return $new_network_id; }