function setUp()
 {
     global $wpdb;
     parent::setUp();
     $this->suppress = $wpdb->suppress_errors();
     update_site_option('ms_files_rewriting', 1);
     ms_upload_constants();
 }
	function test_switch_upload_dir() {
		$this->assertTrue( is_main_site() );

		$site = get_current_site();

		$info = wp_upload_dir();
		$this->assertEquals( 'http://' . $site->domain . '/wp-content/uploads/' . gmstrftime('%Y/%m'), $info['url'] );
		$this->assertEquals( ABSPATH . 'wp-content/uploads/' . gmstrftime('%Y/%m'), $info['path'] );
		$this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] );
		$this->assertEquals( '', $info['error'] );

		$user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
		$blog_id = $this->factory->blog->create( array( 'user_id' => $user_id ) );

		switch_to_blog( $blog_id );
		$info = wp_upload_dir();
		$this->assertEquals( 'http://' . $site->domain . '/wp-content/uploads/sites/' . get_current_blog_id() . '/' . gmstrftime('%Y/%m'), $info['url'] );
		$this->assertEquals( ABSPATH . 'wp-content/uploads/sites/' . get_current_blog_id() . '/' . gmstrftime('%Y/%m'), $info['path'] );
		$this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] );
		$this->assertEquals( '', $info['error'] );
		restore_current_blog();

		$info = wp_upload_dir();
		$this->assertEquals( 'http://' . $site->domain . '/wp-content/uploads/' . gmstrftime('%Y/%m'), $info['url'] );
		$this->assertEquals( ABSPATH . 'wp-content/uploads/' . gmstrftime('%Y/%m'), $info['path'] );
		$this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] );
		$this->assertEquals( '', $info['error'] );

		update_site_option( 'ms_files_rewriting', 1 );
		ms_upload_constants();

		$user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
		$blog_id2 = $this->factory->blog->create( array( 'user_id' => $user_id ) );
		$info = wp_upload_dir();
		$this->assertEquals( 'http://' . $site->domain . '/wp-content/uploads/' . gmstrftime('%Y/%m'), $info['url'] );
		$this->assertEquals( ABSPATH . 'wp-content/uploads/' . gmstrftime('%Y/%m'), $info['path'] );
		$this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] );
		$this->assertEquals( '', $info['error'] );

		switch_to_blog( $blog_id2 );
		$info2 = wp_upload_dir();
		$this->assertNotEquals( $info, $info2 );
		$this->assertEquals( get_option( 'siteurl' )  . '/wp-content/blogs.dir/' . get_current_blog_id() . '/files/' . gmstrftime('%Y/%m'), $info2['url'] );
		$this->assertEquals( ABSPATH . 'wp-content/blogs.dir/' . get_current_blog_id() . '/files/' . gmstrftime('%Y/%m'), $info2['path'] );
		$this->assertEquals( gmstrftime('/%Y/%m'), $info2['subdir'] );
		$this->assertEquals( '', $info2['error'] );
		restore_current_blog();
		update_site_option( 'ms_files_rewriting', 0 );
	}
Exemple #3
0
        header('Location: ' . $bootstrap_result);
        exit;
    }
    unset($bootstrap_result);
    $blog_id = $current_blog->blog_id;
    $public = $current_blog->public;
    if (empty($current_blog->site_id)) {
        // This dates to [MU134] and shouldn't be relevant anymore,
        // but it could be possible for arguments passed to insert_blog() etc.
        $current_blog->site_id = 1;
    }
    $site_id = $current_blog->site_id;
    wp_load_core_site_options($site_id);
}
$wpdb->set_prefix($table_prefix, false);
// $table_prefix can be set in sunrise.php
$wpdb->set_blog_id($current_blog->blog_id, $current_blog->site_id);
$table_prefix = $wpdb->get_blog_prefix();
$_wp_switched_stack = array();
$switched = false;
// need to init cache again after blog_id is set
wp_start_object_cache();
if (!$current_site instanceof WP_Network) {
    $current_site = new WP_Network($current_site);
}
if (!$current_blog instanceof WP_Site) {
    $current_blog = new WP_Site($current_blog);
}
// Define upload directory constants
ms_upload_constants();
 /**
  * Add a new network
  *
  * @since 1.3
  *
  * @param array $args  {
  *     Array of arguments.
  *     @type string  $domain           Domain name for new network - for VHOST=no,
  *                                     this should be FQDN, otherwise domain only.
  *     @type string  $path             Path to root of network hierarchy - should
  *                                     be '/' unless WP is cohabiting with another
  *                                     product on a domain.
  *     @type string  $site_name        Name of the root blog to be created on
  *                                     the new network.
  *     @type integer $user_id          ID of the user to add as the site owner.
  *                                     Defaults to current user ID.
  *     @type array   $meta             Array of metadata to save to this network.
  *                                     Defaults to array( 'public' => false ).
  *     @type integer $clone_network    ID of network whose networkmeta values are
  *                                     to be copied - default NULL.
  *     @type array   $options_to_clone Override default network meta options to copy
  *                                     when cloning - default NULL.
  * }
  *
  * @return integer ID of newly created network
  */
 function add_network($args = array())
 {
     global $wpdb;
     // Backward compatibility with old method of passing arguments
     if (!is_array($args) || func_num_args() > 1) {
         _deprecated_argument(__METHOD__, '1.7.0', sprintf(__('Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'wp-multi-network'), __METHOD__, __FILE__));
         // Juggle function parameters
         $func_args = func_get_args();
         $old_args_keys = array(0 => 'domain', 1 => 'path', 2 => 'site_name', 3 => 'clone_network', 4 => 'options_to_clone');
         // Reset array
         $args = array();
         // Rejig args
         foreach ($old_args_keys as $arg_num => $arg_key) {
             if (isset($func_args[$arg_num])) {
                 $args[$arg_key] = $func_args[$arg_num];
             }
         }
     }
     // Parse args
     $r = wp_parse_args($args, array('domain' => '', 'path' => '/', 'site_name' => __('New Network', 'wp-multi-network'), 'user_id' => get_current_user_id(), 'meta' => array('public' => get_option('blog_public', false)), 'clone_network' => false, 'options_to_clone' => array_keys(network_options_to_copy())));
     // Bail if no user with this ID
     if (empty($r['user_id']) || !get_userdata($r['user_id'])) {
         return new WP_Error('network_user', __('User does not exist.', 'wp-multi-network'));
     }
     // Permissive sanitization for super admin usage
     $r['domain'] = str_replace(' ', '', strtolower($r['domain']));
     $r['path'] = str_replace(' ', '', strtolower($r['path']));
     // Check for existing network
     $networks = get_networks(array('domain' => $r['domain'], 'path' => $r['path'], 'number' => '1'));
     // Bail if network already exists
     if (!empty($networks)) {
         return new WP_Error('network_exists', __('Network already exists.', 'wp-multi-network'));
     }
     // Insert new network
     $wpdb->insert($wpdb->site, array('domain' => $r['domain'], 'path' => $r['path']));
     $new_network_id = $wpdb->insert_id;
     // If network was created, create a blog for it too
     if (!empty($new_network_id)) {
         if (!defined('WP_INSTALLING')) {
             define('WP_INSTALLING', true);
         }
         // Switch to the new network so counts are properly bumped
         switch_to_network($new_network_id);
         // Ensure upload constants are envoked
         ms_upload_constants();
         // Create the site for the root of this network
         $new_blog_id = wpmu_create_blog($r['domain'], $r['path'], $r['site_name'], $r['user_id'], $r['meta'], $new_network_id);
         // Switch back to the current network, to avoid any issues
         restore_current_network();
         // Bail if blog could not be created
         if (is_wp_error($new_blog_id)) {
             return $new_blog_id;
         }
         /**
          * Fix upload_path for main sites on secondary networks
          * This applies only to new installs (WP 3.5+)
          */
         // Switch to network (if set & exists)
         if (defined('SITE_ID_CURRENT_SITE') && get_network(SITE_ID_CURRENT_SITE)) {
             $use_files_rewriting = get_network_option(SITE_ID_CURRENT_SITE, 'ms_files_rewriting');
         } else {
             $use_files_rewriting = get_site_option('ms_files_rewriting');
         }
         global $wp_version;
         // Create the upload_path and upload_url_path values
         if (empty($use_files_rewriting) && version_compare($wp_version, '3.7', '<')) {
             // WP_CONTENT_URL is locked to the current site and can't be overridden,
             //  so we have to replace the hostname the hard way
             $current_siteurl = get_option('siteurl');
             $new_siteurl = untrailingslashit(get_blogaddress_by_id($new_blog_id));
             $upload_url = str_replace($current_siteurl, $new_siteurl, WP_CONTENT_URL);
             $upload_url = $upload_url . '/uploads';
             $upload_dir = WP_CONTENT_DIR;
             if (0 === strpos($upload_dir, ABSPATH)) {
                 $upload_dir = substr($upload_dir, strlen(ABSPATH));
             }
             $upload_dir .= '/uploads';
             if (defined('MULTISITE')) {
                 $ms_dir = '/sites/' . $new_blog_id;
             } else {
                 $ms_dir = '/' . $new_blog_id;
             }
             $upload_dir .= $ms_dir;
             $upload_url .= $ms_dir;
             update_blog_option($new_blog_id, 'upload_path', $upload_dir);
             update_blog_option($new_blog_id, 'upload_url_path', $upload_url);
         }
     }
     // Clone network meta from an existing network.
     //
     // We currently use the _options() API to get cache integration for free,
     // but it may be better to read & write directly to $wpdb->sitemeta.
     if (!empty($r['clone_network']) && get_network($r['clone_network'])) {
         // Temporary array
         $options_cache = array();
         // Old network
         foreach ($r['options_to_clone'] as $option) {
             $options_cache[$option] = get_network_option($r['clone_network'], $option);
         }
         // New network
         foreach ($r['options_to_clone'] as $option) {
             // Skip if option isn't available to copy
             if (!isset($options_cache[$option])) {
                 continue;
             }
             // Fix for bug that prevents writing the ms_files_rewriting
             // value for new networks.
             if ('ms_files_rewriting' === $option) {
                 $wpdb->insert($wpdb->sitemeta, array('site_id' => $wpdb->siteid, 'meta_key' => $option, 'meta_value' => $options_cache[$option]));
             } else {
                 update_network_option($new_network_id, $option, $options_cache[$option]);
             }
         }
     }
     // Clean network cache
     clean_network_cache($new_network_id);
     do_action('add_network', $new_network_id, $r);
     return $new_network_id;
 }