WordPress.org

Make WordPress Core

Opened 11 years ago

Closed 10 years ago

#11998 closed defect (bug) (fixed)

switch_to_blog and wp_upload_dir, with wpmu 2.8.4a

Reported by: wpmuguru Owned by:
Milestone: 3.0 Priority: normal
Severity: normal Version:
Component: Multisite Keywords: has-patch
Focuses: Cc:

Description

MU Trac Ticket: http://trac.mu.wordpress.org/ticket/1154

When you use the switch_to_blog, wp_upload_dir doesn't take care of the blog_id you switch to.

Here is an example, the action is running on blog id 4, but the target is blog id 14

array(6) {
  ["path"]=>
  string(74) "/data/web/srvph1/zeclic.fr/www/htdocs/wp-content/blogs.dir/4/files/2009/11"
  ["url"]=>
  string(62) "http://fandebd.zeclic.fr/wp-content/blogs.dir/14/files/2009/11"
  ["subdir"]=>
  string(8) "/2009/11"
  ["basedir"]=>
  string(67) "/data/web/srvph1/zeclic.fr/www/htdocs/wp-content/blogs.dir/4/files"
  ["baseurl"]=>
  string(54) "http://fandebd.zeclic.fr/wp-content/blogs.dir/14/files"
  ["error"]=>
  bool(false)
}

Cause is /wp-includes/functions.php Line 2057 to 2060:

	if ( defined('UPLOADS')) {
		$dir = ABSPATH . UPLOADS;
		$url = trailingslashit( $siteurl ) . UPLOADS;
	}

Correction for 'basedir' is :

        global $switched;
        if ( defined('UPLOADS') && $switched === false) {
		$dir = ABSPATH . UPLOADS;
		$url = trailingslashit( $siteurl ) . UPLOADS;
	}

But 'path' key doesn't change. It's modified by another filter /wp-includes/wpmu-functions.php :

function mu_upload_dir( $uploads ) {
	$dir = $uploads[ 'basedir' ];
	if( defined( 'BLOGUPLOADDIR' ) )
		$dir = constant( 'BLOGUPLOADDIR' );
	$dir = untrailingslashit( $dir ) . $uploads[ 'subdir' ];
	$uploads[ 'path' ] = $dir;

	return $uploads;
}
add_filter( 'upload_dir', 'mu_upload_dir' );

Patch is :

function mu_upload_dir( $uploads ) {
        global $switched;
	$dir = $uploads[ 'basedir' ];
	if( defined( 'BLOGUPLOADDIR' ) && switched === false )
		$dir = constant( 'BLOGUPLOADDIR' );
	$dir = untrailingslashit( $dir ) . $uploads[ 'subdir' ];
	$uploads[ 'path' ] = $dir;

	return $uploads;
}
add_filter( 'upload_dir', 'mu_upload_dir' );

perhaps we can put the two patch only on wpmu-functions.php ....

my wpmu_version = '2.8.4a';

Attachments (2)

11998.patch (922 bytes) - added by ocean90 10 years ago.
11998.2.patch (952 bytes) - added by ocean90 10 years ago.
used isset and removed $GLOBALS

Download all attachments as: .zip

Change History (8)

#1 @nacin
10 years ago

  • Milestone changed from Unassigned to 3.0

#2 follow-up: @wpmuguru
10 years ago

Have you verified this is still a problem?

#3 @andreasnrb
10 years ago

I think it still is a problem or something very similar still exists.
Was using wp_upload_dir but it always pointed to current blog and not the switch to blog.
Had to use WP_CONTENT_DIR constant instead.

@ocean90
10 years ago

#4 in reply to: ↑ 2 @ocean90
10 years ago

  • Keywords has-patch added

Replying to wpmuguru:

Have you verified this is still a problem?

Yes, it is. The attached patch should fix it. Tested with:

function u() {
	switch_to_blog( 4 );
	var_dump( wp_upload_dir() );
	restore_current_blog();
}
add_action('admin_init', 'u');

@ocean90
10 years ago

used isset and removed $GLOBALS

#5 @wpmuguru
10 years ago

(In [14510]) add upload dir support to switch to blog, props ocean90, see #11998

#6 @ocean90
10 years ago

  • Resolution set to fixed
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.