/** * Modify the domain and path of an existing site - and update all of its blogs * @param integer id ID of site to modify * @param string $domain new domain for site * @param string $path new path for site * @param array $meta meta keys and values to be updated */ function update_site($id, $domain, $path = '', $meta = null) { global $wpdb, $url_dependent_blog_options, $wp_version; if (!site_exists((int) $id)) { return new WP_Error('site_not_exist', __('Network does not exist.', 'njsl-networks')); } $query = "SELECT * FROM {$wpdb->site} WHERE id=" . (int) $id; $site = $wpdb->get_row($query); if (!$site) { return new WP_Error('site_not_exist', __('Network does not exist.', 'njsl-networks')); } $domain = untrailingslashit($domain); $update = array('domain' => $domain); if ($path != '') { $path = trim($path, '/'); $path = trailingslashit('/' . $path); $update['path'] = $path; } $where = array('id' => (int) $id); $update_result = $wpdb->update($wpdb->site, $update, $where); if (false === $update_result) { return new WP_Error('site_not_updatable', __('Network could not be updated.', 'njsl-networks')); } if (!empty($meta) && is_array($meta)) { switch_to_site($id); foreach ($meta as $option => $value) { if (false === update_site_option($option, $value)) { $wpdb->insert($wpdb->sitemeta, array('site_id' => $wpdb->siteid, 'meta_key' => $option, 'meta_value' => $value)); } } restore_current_site(); } // No URL values updated -- quit before needlessly // processing a bunch of URL-specific values and triggering hooks if (!$update_result) { return; } $path = $path != '' ? $path : $site->path; $fullPath = untrailingslashit($domain . $path); $oldPath = untrailingslashit($site->domain . $site->path); /** also updated any associated blogs */ $query = "SELECT * FROM {$wpdb->blogs} WHERE site_id=" . (int) $id; $blogs = $wpdb->get_results($query); if ($blogs) { foreach ($blogs as $blog) { $update = array(); if ($site->domain !== $domain) { $update['domain'] = str_replace($site->domain, $domain, $blog->domain); } if ($site->path !== $path) { $search = sprintf('|^%s|', preg_quote($site->path, '|')); $update['path'] = preg_replace($search, $path, $blog->path, 1); } if (empty($update)) { continue; } $blog_id = (int) $blog->blog_id; switch_to_blog($blog_id); $wpdb->update($wpdb->blogs, $update, array('blog_id' => $blog_id)); foreach ($url_dependent_blog_options as $option_name) { // TODO: pop upload_url_path off list if ms_files_rewriting is disabled $option_value = get_option($option_name); if ($option_value) { $newValue = str_replace($oldPath, $fullPath, $option_value->option_value); update_option($option_name, $newValue); } } restore_current_blog(); refresh_blog_details($blog_id); } } do_action('wpmu_update_site', $id, array('domain' => $site->domain, 'path' => $site->path)); do_action('wpms_update_network', $id, array('domain' => $site->domain, 'path' => $site->path)); }
function delete_multiple_site_page() { global $wpdb; if (isset($_POST['delete_multiple']) && isset($_POST['deleted_sites'])) { foreach ($_POST['deleted_sites'] as $deleted_site) { $result = delete_site((int) $deleted_site, isset($_POST['override'])); if (is_a($result, 'WP_Error')) { wp_die($result->get_error_message()); } } $_GET['deleted'] = 'yes'; $_GET['action'] = 'saved'; } else { /** ensure a list of sites was sent */ if (!isset($_POST['allsites'])) { wp_die(__('You have not selected any networks to delete.')); } /** Ensure that all sites are integers */ $allsites = array_map(create_function('$val', 'return (int)$val;'), $_POST['allsites']); /** ensure each site is valid */ foreach ($allsites as $site) { if (!site_exists((int) $site)) { wp_die(__('You have selected an invalid network.', 'njsl-networks')); } } /** remove primary site from list */ if (in_array(1, $allsites)) { $sites = array(); foreach ($allsites as $site) { if ($site != 1) { $sites[] = $site; } } $allsites = $sites; } $query = "SELECT * FROM {$wpdb->site} WHERE id IN (" . implode(',', $allsites) . ')'; $site = $wpdb->get_results($query); if (!$site) { wp_die(__('You have selected an invalid network or networks.', 'njsl-networks')); } $query = "SELECT * FROM {$wpdb->blogs} WHERE site_id IN (" . implode(',', $allsites) . ')'; $blogs = $wpdb->get_results($query); ?> <form method="POST" action="<?php echo add_query_arg(array('action' => false, 'id' => false)); ?> "><div> <h2><?php _e('Delete Multiple Networks', 'njsl-networks'); ?> </h2> <?php if ($blogs) { if (RESCUE_ORPHANED_BLOGS && ENABLE_HOLDING_SITE) { ?> <div id="message" class="error"> <h3><?php _e('You have selected the following networks for deletion', 'njsl-networks'); ?> :</h3> <ul> <?php foreach ($site as $deleted_site) { ?> <li><input type="hidden" name="deleted_sites[]" value="<?php echo $deleted_site->id; ?> " /><?php echo $deleted_site->domain . $deleted_site->path; ?> </li> <?php } ?> </ul> <p><?php _e('There are sites assigned to one or more of these networks.', 'njsl-networks'); _e('Deleting them will move these sites to the holding network.', 'njsl-networks'); ?> </p> <p><label for="override"><?php _e('If you still want to delete these networks, check the following box', 'njsl-networks'); ?> :</label> <input type="checkbox" name="override" id="override" /></p> </div> <?php } else { ?> <div id="message" class="error"> <h3><?php _e('You have selected the following networks for deletion', 'njsl-networks'); ?> :</h3> <ul> <?php foreach ($site as $deleted_site) { ?> <li><input type="hidden" name="deleted_sites[]" value="<?php echo $deleted_site->id; ?> " /><?php echo $deleted_site->domain . $deleted_site->path; ?> </li> <?php } ?> </ul> <p><?php _e('There are sites associated with one or more of these networks.', 'njsl-networks'); _e('Deleting them will delete those sites as well.', 'njsl-networks'); ?> </p> <p><label for="override"><?php _e('If you still want to delete these networks, check the following box', 'njsl-networks'); ?> :</label> <input type="checkbox" name="override" id="override" /></p> </div> <?php } } else { ?> <div id="message"> <h3><?php _e('You have selected the following networks for deletion', 'njsl-networks'); ?> :</h3> <ul> <?php foreach ($site as $deleted_site) { ?> <li><input type="hidden" name="deleted_sites[]" value="<?php echo $deleted_site->id; ?> " /><?php echo $deleted_site->domain . $deleted_site->path; ?> </li> <?php } ?> </ul> </div> <?php } ?> <p><?php _e('Are you sure you want to delete these networks?', 'njsl-networks'); ?> </p> <?php submit_button(__('Delete Networks', 'njsl-networks'), 'primary', 'delete_multiple', false); ?> <input type="submit" name="cancel" value="<?php _e('Cancel', 'njsl-networks'); ?> " class="button" /> </div></form> <?php } }