/** * Problem: the various *_site_options() functions operate only on the current network * Workaround: change the current network * * @since 1.3 * @param integer $new_network ID of network to manipulate */ function switch_to_network($new_network = 0, $validate = false) { global $wpdb, $switched_network, $switched_network_stack, $current_site; if (empty($new_network)) { $new_network = $current_site->id; } if (true == $validate && !network_exists($new_network)) { return false; } if (empty($switched_network_stack)) { $switched_network_stack = array(); } array_push($switched_network_stack, $current_site); /** * If we're switching to the same network id that we're on, * set the right vars, do the associated actions, but skip * the extra unnecessary work */ if ($current_site->id === $new_network) { do_action('switch_network', $current_site->id, $current_site->id); $switched_network = true; return true; } // Switch the current site over $prev_site_id = $current_site->id; $current_site = wp_get_network($new_network); // Figure out the current network's main site. if (!isset($current_site->blog_id)) { $current_site->blog_id = get_main_site_for_network($current_site); } if (!isset($current_site->site_name)) { $current_site->site_name = get_network_name(); } $wpdb->siteid = $current_site->id; $GLOBALS['site_id'] = $current_site->id; do_action('switch_network', $current_site->id, $prev_site_id); $switched_network = true; return true; }
/** * 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; }