/** * Add a Network * * <domain> * : Domain for network * * <path> * : Path for network * * [--site_name=<site_name>] * : Name of new network * * [--clone_network=<clone_network>] * : ID of network to clone * * [--options_to_clone=<options_to_clone>] * : Options to clone to new network * */ public function create($args, $assoc_args) { list($domain, $path) = $args; $defaults = array('site_name' => false, 'clone_network' => false, 'options_to_clone' => false); $assoc_args = wp_parse_args($assoc_args, $defaults); $clone_network = $assoc_args['clone_network']; $options_to_clone = false; if (!empty($clone_network) && !network_exists($clone_network)) { WP_CLI::error(sprintf(__("Clone network %s doesn't exist.", 'wp-multi-network'), $clone_network)); if (!empty($assoc_args['options_to_clone'])) { $options_to_clone = explode(",", $assoc_args['options_to_clone']); } } $network_id = add_network($domain, $path, $assoc_args['site_name'], $clone_network, $options_to_clone); if (is_wp_error($network_id)) { WP_CLI::error($network_id); } WP_CLI::success(sprintf(__('Created network %d.', 'wp-multi-network'), $network_id)); }
/** * 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; $id = (int) $id; // Bail if network does not exist if (!network_exists($id)) { return new WP_Error('network_not_exist', __('Network does not exist.', 'wp-multi-network')); } // Bail if site for network is missing $network = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->site} WHERE id = %d", $id)); if (empty($network)) { return new WP_Error('network_not_exist', __('Network does not exist.', 'wp-multi-network')); } // Set the arrays for updating the db $update = array('domain' => $domain); if (!empty($path)) { $update['path'] = $path; } // Attempt to update the network $where = array('id' => $id); $update_result = $wpdb->update($wpdb->site, $update, $where); // Bail if update failed if (empty($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 = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->blogs} WHERE site_id = %d", $id)); if (!empty($sites)) { // Loop through sites and update domain/path foreach ($sites as $site) { $update = array(); if ($network->domain !== $domain) { $update['domain'] = str_replace($network->domain, $domain, $site->domain); } if ($network->path !== $path) { $search = sprintf('|^%s|', preg_quote($network->path, '|')); $update['path'] = preg_replace($search, $path, $site->path, 1); } if (empty($update)) { continue; } $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) { $option_value = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$option_table} WHERE option_name = %s", $option_name)); if (!empty($option_value)) { $new_value = str_replace($old_path, $full_path, $option_value->option_value); update_blog_option($site->blog_id, $option_name, $new_value); } } } } do_action('update_network', $id, array('domain' => $network->domain, 'path' => $network->path)); }
public function test_network_exists_with_invalid_id() { $this->assertFalse(network_exists(-1), 'Network IDs must be positive'); }
public function delete_multiple_network_page() { global $wpdb; if (isset($_POST['delete_multiple']) && isset($_POST['deleted_networks'])) { foreach ($_POST['deleted_networks'] as $deleted_network) { $result = delete_network((int) $deleted_network, 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 networks was sent if (!isset($_POST['allnetworks'])) { wp_die(esc_html__('You have not selected any networks to delete.', 'wp-multi-network')); } $allnetworks = array_map(create_function('$val', 'return (int)$val;'), $_POST['allnetworks']); // ensure each network is valid foreach ($allnetworks as $network) { if (!network_exists((int) $network)) { wp_die(esc_html__('You have selected an invalid network for deletion.', 'wp-multi-network')); } } // remove primary network from list if (in_array(1, $allnetworks)) { $sites = array(); foreach ($allnetworks as $network) { if ($network != 1) { $sites[] = $network; } } $allnetworks = $sites; } $network = $wpdb->get_results("SELECT * FROM {$wpdb->site} WHERE id IN (" . implode(',', $allnetworks) . ')'); if (empty($network)) { wp_die(esc_html__('You have selected an invalid network or networks for deletion', 'wp-multi-network')); } $sites = $wpdb->get_results("SELECT * FROM {$wpdb->blogs} WHERE site_id IN (" . implode(',', $allnetworks) . ')'); ?> <div class="wrap"> <form method="POST" action="<?php echo $this->admin_url(); ?> "> <h2><?php esc_html_e('Networks', 'wp-multi-network'); ?> </h2> <h3><?php esc_html_e('Delete Multiple Networks', 'wp-multi-network'); ?> </h3> <?php if ($sites) { if (RESCUE_ORPHANED_BLOGS && ENABLE_NETWORK_ZERO) { ?> <div id="message" class="error"> <h3><?php esc_html_e('You have selected the following networks for deletion', 'wp-multi-network'); ?> :</h3> <ul> <?php foreach ($network as $deleted_network) { ?> <li><input type="hidden" name="deleted_networks[]" value="<?php echo esc_attr($deleted_network->id); ?> " /><?php echo esc_html($deleted_network->domain . $deleted_network->path); ?> </li> <?php } ?> </ul> <p><?php esc_html_e('There are blogs associated with one or more of these networks. Deleting them will move these blogs to the holding network.', 'wp-multi-network'); ?> </p> <p><label for="override"><?php esc_html_e('If you still want to delete these networks, check the following box', 'wp-multi-network'); ?> :</label> <input type="checkbox" name="override" id="override" /></p> </div> <?php } else { ?> <div id="message" class="error"> <h3><?php esc_html_e('You have selected the following networks for deletion', 'wp-multi-network'); ?> :</h3> <ul> <?php foreach ($network as $deleted_network) { ?> <li><input type="hidden" name="deleted_networks[]" value="<?php echo esc_attr($deleted_network->id); ?> " /><?php echo esc_html($deleted_network->domain . $deleted_network->path); ?> </li> <?php } ?> </ul> <p><?php esc_html_e('There are blogs associated with one or more of these networks. Deleting them will delete those blogs as well.', 'wp-multi-network'); ?> </p> <p><label for="override"><?php esc_html_e('If you still want to delete these networks, check the following box', 'wp-multi-network'); ?> :</label> <input type="checkbox" name="override" id="override" /></p> </div> <?php } } else { ?> <div id="message"> <h3><?php esc_html_e('You have selected the following networks for deletion', 'wp-multi-network'); ?> :</h3> <ul> <?php foreach ($network as $deleted_network) { ?> <li><input type="hidden" name="deleted_networks[]" value="<?php echo esc_attr($deleted_network->id); ?> " /><?php echo esc_html($deleted_network->domain . $deleted_network->path); ?> </li> <?php } ?> </ul> </div> <?php } ?> <p><?php esc_html_e('Are you sure you want to delete these networks?', 'wp-multi-network'); ?> </p> <input type="submit" name="delete_multiple" value="<?php esc_html_e('Delete Networks', 'wp-multi-network'); ?> " class="button" /> <input type="submit" name="cancel" value="<?php esc_html_e('Cancel', 'wp-multi-network'); ?> " class="button" /> </form> </div> <?php } }