public function test_switch_to_network() { global $current_site; $other_network_id = $this->factory->network->create(); // Check we're on the main network first $this->assertEquals(1, $current_site->id, 'Current network should be main network before switching'); $this->assertTrue(switch_to_network($other_network_id), 'Switching network should work'); $this->assertEquals($other_network_id, $current_site->id, 'Switching network should switch the network'); $this->assertTrue(restore_current_network(), 'Switching back should work'); $this->assertEquals(1, $current_site->id, 'Switching back should switch the network'); }
/** * Delete an option from a given network * * Switches to the specified network internally to operate on it. * * @param int $network_id ID of network. * @param string $key Option key. * @return boolean|WP_Error True if the option is deleted, false if not deleted. `WP_Error` instance if invalid network ID is passed. */ function delete_network_option($network_id, $key) { if (!switch_to_network($network_id, true)) { return new WP_Error('wpmn.network_missing', __('Network does not exist', 'wp-multi-network'), array('status' => 400)); } $result = delete_site_option($key); restore_current_network(); return $result; }
/** * Output a list-table row * * @since 1.7.0 * * @param object $network */ public function display_row($network) { // Default class $class = ''; // Row class if (get_current_site()->id == $network['id']) { $class = 'active'; } else { $class = 'alternate' === $class ? '' : 'alternate'; } // Start an output buffer ob_start(); ?> <tr class="<?php echo esc_attr($class); ?> "> <?php list($columns, $hidden) = $this->get_column_info(); foreach (array_keys($columns) as $column_name) { // Hidden? $style = in_array($column_name, $hidden) ? ' style="display:none;"' : ''; // Which column? switch ($column_name) { case 'cb': ?> <th scope="row" class="check-column"> <?php if (get_current_site()->id != $network['id'] && $network['id'] != 1) { ?> <input type="checkbox" id="network_<?php echo $network['site_id']; ?> " name="all_networks[]" value="<?php echo esc_attr($network['site_id']); ?> "> <?php } ?> </th> <?php break; case 'domain': ?> <td valign="top" class="<?php echo $column_name; ?> column-<?php echo $column_name; ?> " <?php echo $style; ?> > <?php echo esc_html($network['domain']); ?> </td> <?php break; case 'path': ?> <td valign="top" class="<?php echo $column_name; ?> column-<?php echo $column_name; ?> " <?php echo $style; ?> > <?php echo esc_html($network['path']); ?> </td> <?php break; case 'title': ?> <td valign="top" class="<?php echo $column_name; ?> column-<?php echo $column_name; ?> " <?php echo $style; ?> > <?php switch_to_network($network['id']); $network_admin_url = network_admin_url(); $network_home_url = network_home_url(); restore_current_network(); $myurl = add_query_arg(array('page' => 'networks', 'id' => $network['id'])); ?> <strong> <a href="<?php echo add_query_arg(array('action' => 'edit_network'), $myurl); ?> "><?php echo $network['sitename']; ?> </a> </strong> <?php $actions = array('edit' => '<span class="edit"><a href="' . add_query_arg(array('action' => 'edit_network'), $myurl) . '">' . esc_html__('Edit', 'wp-multi-network') . '</a></span>', 'network_admin' => '<span class="edit"><a href="' . esc_url($network_admin_url) . '">' . esc_html__('Dashboard', 'wp-multi-network') . '</a></span>', 'visit' => '<span class="edit"><a href="' . esc_url($network_home_url) . '">' . esc_html__('Visit', 'wp-multi-network') . '</a></span>'); if (get_current_site()->id != $network['id'] && $network['id'] != 1) { if (current_user_can('manage_network_options', $network['id'])) { $actions['delete'] = '<span class="delete"><a href="' . esc_url(wp_nonce_url(add_query_arg(array('action' => 'delete_network'), $myurl))) . '">' . esc_html__('Delete', 'wp-multi-network') . '</a></span>'; } } $actions = apply_filters('manage_networks_action_links', array_filter($actions), $network['id'], $network['sitename']); echo $this->row_actions($actions); ?> </td> <?php break; case 'blogs': ?> <td valign="top" class="<?php echo $column_name; ?> column-<?php echo $column_name; ?> " <?php echo $style; ?> > <a href="<?php echo wp_get_scheme() . $network['domain'] . $network['blog_path']; ?> wp-admin/network/sites.php" title="<?php esc_attr_e('Sites on this network', 'wp-multi-network'); ?> "> <?php echo $network['blogs']; ?> </a> </td> <?php break; case 'admins': ?> <td valign="top" class="<?php echo $column_name; ?> column-<?php echo $column_name; ?> " <?php echo $style; ?> > <?php if (!empty($network['network_admins'])) { $network_admins = array_filter(maybe_unserialize($network['network_admins'])); if (!empty($network_admins)) { echo join(', ', $network_admins); } } ?> </td> <?php break; case 'plugins': if (has_filter('wpmublogsaction')) { ?> <td valign="top" class="<?php echo $column_name; ?> column-<?php echo $column_name; ?> " <?php echo $style; ?> > <?php do_action('wpmublogsaction', $network['id']); ?> </td> <?php } break; default: ?> <td valign="top" class="<?php echo $column_name; ?> column-<?php echo $column_name; ?> " <?php echo $style; ?> > <?php do_action('manage_sites_custom_column', $column_name, $network['id']); ?> </td> <?php break; } } ?> </tr> <?php // Return the outpub buffer echo ob_get_clean(); }
/** * Handle the request to update a network * * @since 1.7.0 */ private function update_network_handler() { // Cast $network_id = (int) $_POST['network_id']; // Bail if invalid network if (!wp_get_network($network_id)) { wp_die(esc_html__('Invalid network id.', 'wp-multi-network')); } // Title $network_title = isset($_POST['title']) ? $_POST['title'] : ''; // Domain $network_domain = isset($_POST['domain']) ? $_POST['domain'] : ''; // Path $network_path = isset($_POST['path']) ? $_POST['path'] : ''; // Bail if missing fields if (empty($network_title) || empty($network_domain) || empty($network_path)) { $this->handler_redirect(array('page' => 'networks', 'id' => $network_id, 'action' => 'edit_network', 'network_updated' => '0')); } // Update domain & path update_network($network_id, $_POST['domain'], $_POST['path']); // Update network title switch_to_network($network_id); update_site_option('site_name', $_POST['title']); restore_current_network(); // Handle redirect $this->handler_redirect(array('id' => $network_id, 'action' => 'edit_network', 'network_updated' => '1')); }
/** * Generates and displays row action links. * * @since 2.0.0 * @access protected * * @param WP_Network $network Site being acted upon. * @param string $column_name Current column name. * @param string $primary Primary column name. * * @return string Row actions output. */ protected function handle_row_actions($network, $column_name, $primary) { // Bail if not primary column if ($primary !== $column_name) { return; } switch_to_network($network->id); $network_admin_url = network_admin_url(); $network_home_url = network_home_url(); restore_current_network(); $myurl = add_query_arg(array('page' => 'networks', 'id' => $network->id)); $edit_network_url = add_query_arg(array('action' => 'edit_network'), $myurl); $delete_network_url = wp_nonce_url(add_query_arg(array('action' => 'delete_network'), $myurl)); // Empty actions array $actions = array(); // Edit if (current_user_can('edit_network', $network->id)) { $actions['edit'] = '<span class="edit"><a href="' . esc_url($edit_network_url) . '">' . esc_html__('Edit', 'wp-multi-network') . '</a></span>'; } // Dashboard if (current_user_can('manage_networks')) { $actions['network_admin'] = '<span><a href="' . esc_url($network_admin_url) . '">' . esc_html__('Dashboard', 'wp-multi-network') . '</a></span>'; } // Visit $actions['visit'] = '<span><a href="' . esc_url($network_home_url) . '">' . esc_html__('Visit', 'wp-multi-network') . '</a></span>'; // Delete if ($this->can_delete($network)) { $actions['delete'] = '<span class="delete"><a href="' . esc_url($delete_network_url) . '">' . esc_html__('Delete', 'wp-multi-network') . '</a></span>'; } $actions = apply_filters('manage_networks_action_links', array_filter($actions), $network->id, $network->sitename); return $this->row_actions($actions); }
/** * Metabox used to publish the network * * @since 1.7.0 * * @param object $network */ function wpmn_edit_network_publish_metabox($network = null) { // Network ID $network_id = empty($network) ? 0 : $network->id; // Button text $button_text = empty($network) ? esc_html__('Create', 'wp-multi-network') : esc_html__('Update', 'wp-multi-network'); // Button action $action = empty($network) ? 'create' : 'update'; ?> <div class="submitbox"> <div id="minor-publishing"> <div id="misc-publishing-actions"> <?php if (!empty($network)) { // Switch switch_to_network($network->id); ?> <div class="misc-pub-section misc-pub-section-first" id="network"> <span><?php printf(__('Name: <strong>%1$s</strong>', 'wp-user-profiles'), get_site_option('site_name')); ?> </span> </div> <div class="misc-pub-section misc-pub-section-last" id="sites"> <span><?php printf(__('Sites: <strong>%1$s</strong>', 'wp-user-profiles'), get_site_option('blog_count')); ?> </span> </div> <?php // Switch back restore_current_network(); } else { ?> <div class="misc-pub-section misc-pub-section-first" id="sites"> <span><?php esc_html_e('Creating a network with 1 new site.', 'wp-multi-network'); ?> </span> </div> <?php } ?> </div> <div class="clear"></div> </div> <div id="major-publishing-actions"> <a class="button" href="<?php echo esc_url(add_query_arg(array('page' => 'networks'), network_admin_url('admin.php'))); ?> "><?php esc_html_e('Cancel', 'wp-multi-network'); ?> </a> <div id="publishing-action"> <?php submit_button($button_text, 'primary', 'submit', false); ?> <input type="hidden" name="action" value="<?php echo esc_attr($action); ?> "> <input type="hidden" name="network_id" value="<?php echo esc_attr($network_id); ?> "> </div> <div class="clear"></div> </div> </div> <?php }
/** * Output the my networks page * * @since 2.0.0 */ public function page_my_networks() { global $wpdb; ?> <div class="wrap"> <h1><?php esc_html_e('My Networks', 'wp-multi-network'); ?> </h1> <?php $my_networks = user_has_networks(); foreach ($my_networks as $key => $network_id) { $my_networks[$key] = $wpdb->get_row($wpdb->prepare('SELECT s.*, sm.meta_value as site_name, b.blog_id FROM ' . $wpdb->site . ' s LEFT JOIN ' . $wpdb->sitemeta . ' as sm ON sm.site_id = s.id AND sm.meta_key = %s LEFT JOIN ' . $wpdb->blogs . ' b ON s.id = b.site_id AND b.path = s.path WHERE s.id = %d', 'site_name', $network_id)); } // Shameless copy of My Sites ?> <table class="widefat fixed"> <?php $num = count($my_networks); $cols = 1; if ($num >= 20) { $cols = 4; } elseif ($num >= 10) { $cols = 2; } $num_rows = ceil($num / $cols); $split = 0; for ($i = 1; $i <= $num_rows; $i++) { $rows[] = array_slice($my_networks, $split, $cols); $split = $split + $cols; } $c = ''; foreach ($rows as $row) { $c = $c == 'alternate' ? '' : 'alternate'; echo "<tr class='{$c}'>"; $i = 0; foreach ($row as $network) { $s = $i == 3 ? '' : 'border-right: 1px solid #ccc;'; switch_to_network($network->id); ?> <td valign='top' style= <?php echo $s; ?> > <h3><?php echo esc_html($network->site_name); ?> </h3> <p><?php echo apply_filters('mynetworks_network_actions', "<a href='" . network_home_url() . "'>" . esc_html__('Visit', 'wp-multi-network') . "</a> | <a href='" . network_admin_url() . "'>" . esc_html__('Dashboard', 'wp-multi-network') . "</a>", $network); ?> </p> </td> <?php restore_current_network(); $i++; } echo "</tr>"; } ?> </table> </div> <?php }
public function display_rows() { global $current_site; $class = ''; foreach ($this->items as $network) { $class = 'alternate' == $class ? '' : 'alternate'; echo "<tr class='{$class}'>"; list($columns, $hidden) = $this->get_column_info(); foreach (array_keys($columns) as $column_name) { $style = in_array($column_name, $hidden) ? ' style="display:none;"' : ''; switch ($column_name) { case 'cb': ?> <th scope="row" class="check-column"> <input type="checkbox" id="network_<?php echo $network['site_id']; ?> " name="allnetworks[]" value="<?php echo esc_attr($network['site_id']); ?> " /> </th> <?php break; case 'domain': echo "<td class='column-{$column_name} {$column_name}'{$style}>"; ?> <?php echo esc_html($network['domain']); ?> </td> <?php break; case 'path': echo "<td class='column-{$column_name} {$column_name}'{$style}>"; ?> <?php echo esc_html($network['path']); ?> </td> <?php break; case 'sitename': echo "<td class='column-{$column_name} {$column_name}'{$style}>"; switch_to_network($network['id']); $network_admin_url = network_admin_url(); $network_home_url = network_home_url(); restore_current_network(); $myurl = add_query_arg(array('page' => 'networks', 'id' => $network['id'])); ?> <a href="<?php echo add_query_arg(array('action' => 'editnetwork'), $myurl); ?> " class="edit"><?php echo $network['sitename']; ?> </a> <?php $actions = array('edit' => '<span class="edit"><a href="' . add_query_arg(array('action' => 'editnetwork'), $myurl) . '">' . esc_html__('Edit', 'wp-multi-network') . '</a></span>', 'network_admin' => '<span class="edit"><a href="' . esc_url($network_admin_url) . '">' . esc_html__('Dashboard', 'wp-multi-network') . '</a></span>', 'visit' => '<span class="edit"><a href="' . esc_url($network_home_url) . '">' . esc_html__('Visit', 'wp-multi-network') . '</a></span>', 'assign_sites' => '<span class="edit"><a href="' . add_query_arg(array('action' => 'assignblogs'), $myurl) . '">' . esc_html__('Assign Sites', 'wp-multi-network') . '</a></span>'); if ($current_site->id != $network['id'] && $network['id'] != 1) { if (current_user_can('manage_network_options', $network['id'])) { $actions['delete'] = '<span class="delete"><a href="' . esc_url(wp_nonce_url(add_query_arg(array('action' => 'deletenetwork'), $myurl))) . '">' . esc_html__('Delete', 'wp-multi-network') . '</a></span>'; } } $actions = apply_filters('manage_networks_action_links', array_filter($actions), $network['id'], $network['sitename']); echo $this->row_actions($actions); ?> </td> <?php break; case 'blogs': echo "<td valign='top' class='{$column_name} column-{$column_name}'{$style}>"; ?> <a href="http://<?php echo $network['domain'] . $network['blog_path']; ?> wp-admin/network/sites.php" title="<?php esc_attr_e('Sites on this network', 'wp-multi-network'); ?> "><?php echo $network['blogs']; ?> </a> </td> <?php break; case 'admins': echo "<td valign='top' class='{$column_name} column-{$column_name}'{$style}>"; $network_admins = array_filter(maybe_unserialize($network['network_admins'])); if (!empty($network_admins)) { echo join(', ', $network_admins); } ?> </td> <?php break; case 'plugins': ?> <?php if (has_filter('wpmublogsaction')) { echo "<td valign='top' class='{$column_name} column-{$column_name}'{$style}>"; do_action('wpmublogsaction', $network['id']); ?> </td> <?php } break; default: echo "<td class='{$column_name} column-{$column_name}'{$style}>"; do_action('manage_sites_custom_column', $column_name, $network['id']); echo "</td>"; break; } } ?> </tr> <?php } }
/** * Move a site to a new network * * @since 1.3 * * @param integer $site_id ID of site to move * @param integer $new_network_id ID of destination network */ function move_site($site_id = 0, $new_network_id = 0) { 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; } // Move the site is the blogs table $where = array('blog_id' => $site->blog_id); $update = array('site_id' => $new_network_id); $result = $wpdb->update($wpdb->blogs, $update, $where); // Bail if site could not be moved if (empty($result)) { return new WP_Error('blog_not_moved', __('Site could not be moved.', 'wp-multi-network')); } // Update old network count if (0 !== $site->site_id) { switch_to_network($site->site_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(); } // Refresh blog details refresh_blog_details($site_id); // Clean network caches clean_network_cache(array_filter(array($site->site_id, $new_network_id))); // Site moved do_action('move_site', $site_id, $site->site_id, $new_network_id); // Return the new network ID as confirmation return $new_network_id; }