Skip to content

Instantly share code, notes, and snippets.

@ptasker
Last active December 9, 2020 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ptasker/479ace246b9482d20732bd8a5cae8ee8 to your computer and use it in GitHub Desktop.
Save ptasker/479ace246b9482d20732bd8a5cae8ee8 to your computer and use it in GitHub Desktop.
<?php
/**
* Returns uploads info for given subsite or primary site.
*
* @param int $blog_id Optional, defaults to primary.
*
* @return array
*
* NOTE: Must be run from primary site.
*/
public function uploads_info( $blog_id = 0 ) {
static $primary_uploads = array();
if ( ! empty( $blog_id ) && is_multisite() ) {
switch_to_blog( $blog_id );
}
$uploads = wp_upload_dir();
$upload_dir = $uploads['basedir'];
if ( ! empty( $blog_id ) && is_multisite() ) {
restore_current_blog();
if ( empty( $primary_uploads ) ) {
$primary_uploads = $this->uploads_info();
}
$main_uploads = $primary_uploads['basedir'];
$uploads['short_basedir'] = str_replace( trailingslashit( $main_uploads ), '', trailingslashit( $upload_dir ) );
if ( defined( 'UPLOADBLOGSDIR' ) && get_site_option( 'ms_files_rewriting' ) ) {
// Get local upload path info from DB
switch_to_blog( $blog_id );
$upload_path = get_option( 'upload_path' );
if ( ! empty( $upload_path ) ) {
$uploads['short_basedir'] = str_replace( trailingslashit( UPLOADBLOGSDIR ), '', trailingslashit( $upload_path ) );
}
restore_current_blog();
}
}
return $uploads;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment