/**
 * 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);
}
/**
 * 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;
    $site_id = (int) $site_id;
    // Sanity checks
    $site = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->blogs} WHERE blog_id = %d", $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'));
    }
    // Return early if site does not need to be moved
    if ((int) $new_network_id == $site->site_id) {
        return true;
    }
    // Store the old network ID for using later
    $old_network_id = $site->site_id;
    // Allow 0 network?
    if (ENABLE_NETWORK_ZERO && 0 == $site->site_id) {
        $old_network->domain = 'holding.blogs.local';
        $old_network->path = '/';
        $old_network->id = 0;
        // Make sure old network exists
    } else {
        $old_network = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->site} WHERE id = %d", $site->site_id));
        if (empty($old_network)) {
            return new WP_Error('network_not_exist', __('Network does not exist.', 'wp-multi-network'));
        }
    }
    // Allow 0 network?
    if (ENABLE_NETWORK_ZERO && 0 == $new_network_id) {
        $new_network->domain = 'holding.blogs.local';
        $new_network->path = '/';
        $new_network->id = 0;
        // Make sure destination network exists
    } else {
        $new_network = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->site} WHERE id = %d", $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 as 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 = $new_network->path . substr($site->path, strlen($old_network->path));
    // Move the site
    $update = array('site_id' => $new_network->id, 'domain' => $domain, 'path' => $path);
    $where = array('blog_id' => $site->blog_id);
    $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'));
    }
    // 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) {
        $option = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$options_table} WHERE option_name = %s", $option_name));
        $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');
    do_action('move_site', $site_id, $old_network_id, $new_network_id);
}
Пример #3
0
 /**
  * Modify the domain/path of a network, and update all of its blogs
  *
  * @since 1.3
  *
  * @param integer id ID of network to modify
  * @param string $domain New domain for network
  * @param string $path New path for network
  */
 function update_network($id, $domain, $path = '')
 {
     global $wpdb;
     // Get network
     $network = get_network($id);
     // Bail if network not found
     if (empty($network)) {
         return new WP_Error('network_not_exist', __('Network does not exist.', 'wp-multi-network'));
     }
     // Get main site for this network
     $site_id = get_main_site_for_network($id);
     $path = wp_sanitize_site_path($path);
     // Bail if site URL is invalid
     if (!wp_validate_site_url($domain, $path, $site_id)) {
         return new WP_Error('blog_bad', sprintf(__('The site "%s" is invalid, not available, or already exists.', 'wp-multi-network'), $domain . $path));
     }
     // Set the arrays for updating the db
     $where = array('id' => $network->id);
     $update = array('domain' => $domain, 'path' => $path);
     // Attempt to update the network
     $update_result = $wpdb->update($wpdb->site, $update, $where);
     // Bail if update failed
     if (is_wp_error($update_result)) {
         return new WP_Error('network_not_updated', __('Network could not be updated.', 'wp-multi-network'));
     }
     $path = !empty($path) ? $path : $network->path;
     $full_path = untrailingslashit($domain . $path);
     $old_path = untrailingslashit($network->domain . $network->path);
     // Also update any associated blogs
     $sites = get_sites(array('network_id' => $network->id));
     // Sites found
     if (!empty($sites)) {
         // Loop through sites and update domain/path
         foreach ($sites as $site) {
             // Empty update array
             $update = array();
             // Updating domain
             if ($network->domain !== $domain) {
                 $update['domain'] = str_replace($network->domain, $domain, $site->domain);
             }
             // Updating path
             if ($network->path !== $path) {
                 $search = sprintf('|^%s|', preg_quote($network->path, '|'));
                 $update['path'] = preg_replace($search, $path, $site->path, 1);
             }
             // Skip if not updating
             if (empty($update)) {
                 continue;
             }
             // Update blogs table
             $where = array('blog_id' => (int) $site->blog_id);
             $wpdb->update($wpdb->blogs, $update, $where);
             // Fix options table values
             $option_table = $wpdb->get_blog_prefix($site->blog_id) . 'options';
             // Loop through options and correct a few of them
             foreach (network_options_list() as $option_name) {
                 // Query
                 $sql = "SELECT * FROM {$option_table} WHERE option_name = %s";
                 $prep = $wpdb->prepare($sql, $option_name);
                 $value = $wpdb->get_row($prep);
                 // Update if value exists
                 if (!empty($value) && false !== strpos($value->option_value, $old_path)) {
                     $new_value = str_replace($old_path, $full_path, $value->option_value);
                     update_blog_option($site->blog_id, $option_name, $new_value);
                 }
             }
             // Refresh blog details
             refresh_blog_details($site->blog_id);
         }
     }
     // Update network cache
     clean_network_cache(array($network->id));
     // Network updated
     do_action('update_network', $network->id, array('domain' => $network->domain, 'path' => $network->path));
     // Network updated
     return true;
 }