/** * 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 edit_site_page() { global $wpdb, $current_site, $wp_version; if (isset($_POST['update']) && isset($_GET['id'])) { $site = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->site} WHERE id=%d", $_GET['id'])); if (!$site) { wp_die(__('Invalid network ID selected', 'njsl-networks')); } switch_to_site((int) $_GET['id']); $upload_handling = get_site_option('ms_files_rewriting'); $primary_blog_id = $current_site->blog_id; restore_current_site(); if (isset($_POST['upload_handling']) && $_POST['upload_handling'] != 'auto') { // Update the site / network update_site((int) $_GET['id'], $_POST['domain'], $_POST['path'], array('ms_files_rewriting' => $_POST['upload_handling'] == 'php' ? '1' : '0')); // Update the primary blog / site if ($_POST['upload_handling'] == 'direct' && $upload_handling) { // If switching from PHP to direct serving, need to fix upload_url_path // on the network's primary site (if one exists) if (!empty($primary_blog_id)) { $upload_dir = WP_CONTENT_DIR . '/uploads'; $current_siteurl = get_option('siteurl'); $new_siteurl = untrailingslashit(get_blogaddress_by_id($primary_blog_id)); $upload_url = str_replace($current_siteurl, $new_siteurl, WP_CONTENT_URL); $upload_url = $upload_url . '/uploads'; if (defined('MULTISITE')) { $ms_dir = '/sites/' . $primary_blog_id; } else { $ms_dir = '/' . $primary_blog_id; } $upload_dir .= $ms_dir; $upload_url .= $ms_dir; update_blog_option($primary_blog_id, 'upload_path', $upload_dir); update_blog_option($primary_blog_id, 'upload_url_path', $upload_url); } } else { if ($_POST['upload_handling'] == 'php') { // If switching to PHP serving, remove upload_url_path to allow // native processing to work if (!empty($primary_blog_id)) { delete_blog_option($primary_blog_id, 'upload_path', ''); delete_blog_option($primary_blog_id, 'upload_url_path', ''); } } } } else { update_site((int) $_GET['id'], $_POST['domain'], $_POST['path']); if (isset($_POST['upload_handling']) && $_POST['upload_handling'] == 'auto') { switch_to_site((int) $_GET['id']); delete_site_option('ms_files_rewriting'); restore_current_site(); } } $_GET['updated'] = 'true'; $_GET['action'] = 'saved'; } else { $site = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->site} WHERE id=%d", $_GET['id'])); if (!$site) { wp_die(__('Invalid network ID selected', 'njsl-networks')); } switch_to_site((int) $_GET['id']); $upload_handling = get_site_option('ms_files_rewriting'); restore_current_site(); if (is_bool($upload_handling)) { $upload_default = $upload_handling; $upload_handling = 'auto'; } else { if ($upload_handling === '0') { $upload_handling = 'direct'; } else { $upload_handling = 'php'; } } if (!isset($upload_default)) { $upload_default = 'Unknown'; } else { $upload_default = $upload_default ? 'PHP' : 'Direct'; } /* strip off the action tag */ $queryStr = add_query_arg(array('action' => false)); ?> <div class="wrap"> <div class="icon32" id="icon-ms-admin"><br></div> <h2><?php _e('Edit Network', 'njsl-networks'); ?> : <a href="http://<?php echo $site->domain . $site->path; ?> "><?php echo $site->domain . $site->path; ?> </a></h2> <form method="post" action="<?php echo $queryStr; ?> "> <table class="form-table"> <tr class="form-field"><th scope="row"><label for="domain"><?php _e('Domain', 'njsl-networks'); ?> </label></th><td> http://<input type="text" id="domain" name="domain" value="<?php echo $site->domain; ?> "></td></tr> <tr class="form-field"><th scope="row"><label for="path"><?php _e('Path', 'njsl-networks'); ?> </label></th><td><input type="text" id="path" name="path" value="<?php echo $site->path; ?> " /></td></tr> <?php if (version_compare($wp_version, '3.5', '>=')) { ?> <tr class="form-field"> <th scope="row"><label for="upload_handling"><?php _e('Upload Handling', 'njsl-networks'); ?> </label></th> <td> <select name="upload_handling" id="upload_handling"> <option value="auto" <?php selected($upload_handling, 'auto'); ?> ><?php printf(__('Auto (%s)', 'njsl-networks'), $upload_default); ?> </option> <option value="direct" <?php selected($upload_handling, 'direct'); ?> ><?php _e('Direct (WP 3.5+)', 'njsl-networks'); ?> </option> <option value="php" <?php selected($upload_handling, 'php'); ?> ><?php _e('PHP (WP < 3.5)', 'njsl-networks'); ?> </option> </select> <p class="alert"> <strong><?php _e('WARNING', 'njsl-networks'); ?> :</strong> <?php _e("Changing this setting may change your root site's upload path, making uploaded files inaccessible.", 'njsl-networks'); ?> </p> </td> </tr> <?php } ?> </table> <?php if (has_action('add_edit_site_option')) { ?> <h3>Options:</h3> <table class="form-table"> <?php do_action('add_edit_site_option'); ?> </table> <?php } ?> <p> <input type="hidden" name="siteId" value="<?php echo $site->id; ?> " /> <?php submit_button(__('Update Network', 'njsl-networks'), 'primary', 'update', false); ?> <a class="button" href="<?php echo $this->listPage; ?> "><?php _e('Cancel', 'njsl-networks'); ?> </a> </p> </form> </div> <?php } }