Skip to:
Content

BuddyPress.org

Changeset 6964


Ignore:
Timestamp:
04/30/2013 01:06:41 AM (11 years ago)
Author:
r-a-y
Message:

Introduce bp_core_get_upload_dir().

This function fetches data from the BP root blog's upload directory.

Handy for multisite instances because all uploads are made on the BP root
blog and we need to query the BP root blog for the upload directory data.

This ensures that we only need to use switch_to_blog() once to get what we
need.

Use this function in bp_core_avatar_upload_path() and bp_core_avatar_url().

Fixes #4948.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-core/bp-core-avatars.php

    r6937 r6964  
    784784
    785785/**
     786 * Fetches data from the BP root blog's upload directory.
     787 *
     788 * Handy for multisite instances because all uploads are made on the BP root
     789 * blog and we need to query the BP root blog for the upload directory data.
     790 *
     791 * This function ensures that we only need to use {@link switch_to_blog()}
     792 * once to get what we need.
     793 *
     794 * @since BuddyPress (1.8)
     795 *
     796 * @uses wp_upload_dir()
     797 *
     798 * @param string $type The variable we want to return from the $bp->avatars object.
     799 *  Only 'upload_path' and 'url' are supported.
     800 * @return string
     801 */
     802function bp_core_get_upload_dir( $type = 'upload_path' ) {
     803    $bp = buddypress();
     804
     805    switch ( $type ) {
     806        case 'upload_path' :
     807            $constant = 'BP_AVATAR_UPLOAD_PATH';
     808            $key      = 'basedir';
     809
     810            break;
     811
     812        case 'url' :
     813            $constant = 'BP_AVATAR_URL';
     814            $key      = 'baseurl';
     815
     816            break;
     817
     818        default :
     819            return false;
     820
     821            break;
     822    }
     823
     824    // See if the value has already been calculated and stashed in the $bp global
     825    if ( isset( $bp->avatar->$type ) ) {
     826        $retval = $bp->avatar->$type;
     827    } else {
     828        // If this value has been set in a constant, just use that
     829        if ( defined( $constant ) ) {
     830            $retval = constant( $constant );
     831        } else {
     832
     833            // Use cached upload dir data if available
     834            if ( ! empty( $bp->avatar->upload_dir ) ) {
     835                $upload_dir = $bp->avatar->upload_dir;
     836
     837            // No cache, so query for it
     838            } else {
     839                // We need to switch to the root blog on multisite installs
     840                if ( is_multisite() ) {
     841                    switch_to_blog( bp_get_root_blog_id() );
     842                }
     843
     844                // Get upload directory information from current site
     845                $upload_dir = wp_upload_dir();
     846
     847                // Will bail if not switched
     848                restore_current_blog();
     849
     850                // Stash upload directory data for later use
     851                $bp->avatar->upload_dir = $upload_dir;
     852            }
     853
     854            // Directory does not exist and cannot be created
     855            if ( ! empty( $upload_dir['error'] ) ) {
     856                $retval = '';
     857
     858            } else {
     859                $retval = $upload_dir[$key];
     860
     861                // If $key is 'baseurl', check to see if we're on SSL
     862                // Workaround for WP13941, WP15928, WP19037.
     863                if ( $key == 'baseurl' && is_ssl() ) {
     864                    $retval = str_replace( 'http://', 'https://', $retval );
     865                }
     866            }
     867
     868        }
     869
     870        // Stash in $bp for later use
     871        $bp->avatar->$type = $retval;
     872    }
     873
     874    return $retval;
     875}
     876
     877/**
    786878 * bp_core_avatar_upload_path()
    787879 *
     
    792884 */
    793885function bp_core_avatar_upload_path() {
    794     $bp = buddypress();
    795 
    796     // See if the value has already been calculated and stashed in the $bp global
    797     if ( isset( $bp->avatar->upload_path ) ) {
    798         $basedir = $bp->avatar->upload_path;
    799     } else {
    800         // If this value has been set in a constant, just use that
    801         if ( defined( 'BP_AVATAR_UPLOAD_PATH' ) ) {
    802             $basedir = BP_AVATAR_UPLOAD_PATH;
    803         } else {
    804             if ( !bp_is_root_blog() ) {
    805                 // Switch dynamically in order to support BP_ENABLE_MULTIBLOG
    806                 switch_to_blog( bp_get_root_blog_id() );
    807             }
    808 
    809             // Get upload directory information from current site
    810             $upload_dir = wp_upload_dir();
    811 
    812             // Directory does not exist and cannot be created
    813             if ( !empty( $upload_dir['error'] ) ) {
    814                 $basedir = '';
    815 
    816             } else {
    817                 $basedir = $upload_dir['basedir'];
    818             }
    819 
    820             // Will bail if not switched
    821             restore_current_blog();
    822         }
    823 
    824         // Stash in $bp for later use
    825         $bp->avatar->upload_path = $basedir;
    826     }
    827 
    828     return apply_filters( 'bp_core_avatar_upload_path', $basedir );
     886    return apply_filters( 'bp_core_avatar_upload_path', bp_core_get_upload_dir() );
    829887}
    830888
     
    838896 */
    839897function bp_core_avatar_url() {
    840     $bp = buddypress();
    841 
    842     // See if the value has already been calculated and stashed in the $bp global
    843     if ( isset( $bp->avatar->url ) ) {
    844         $baseurl = $bp->avatar->url;
    845 
    846     } else {
    847         // If this value has been set in a constant, just use that
    848         if ( defined( 'BP_AVATAR_URL' ) ) {
    849             $baseurl = BP_AVATAR_URL;
    850         } else {
    851             // Get upload directory information from current site
    852             $upload_dir = wp_upload_dir();
    853 
    854             // Directory does not exist and cannot be created
    855             if ( !empty( $upload_dir['error'] ) ) {
    856                 $baseurl = '';
    857 
    858             } else {
    859                 $baseurl = $upload_dir['baseurl'];
    860 
    861                 // If we're using https, update the protocol. Workaround for WP13941, WP15928, WP19037.
    862                 if ( is_ssl() )
    863                     $baseurl = str_replace( 'http://', 'https://', $baseurl );
    864 
    865                 // If multisite, and current blog does not match root blog, make adjustments
    866                 if ( is_multisite() && bp_get_root_blog_id() != get_current_blog_id() )
    867                     $baseurl = trailingslashit( get_blog_option( bp_get_root_blog_id(), 'home' ) ) . get_blog_option( bp_get_root_blog_id(), 'upload_path' );
    868             }
    869         }
    870 
    871         // Stash in $bp for later use
    872         $bp->avatar->url = $baseurl;
    873     }
    874 
    875     return apply_filters( 'bp_core_avatar_url', $baseurl );
     898    return apply_filters( 'bp_core_avatar_url', bp_core_get_upload_dir( 'url' ) );
    876899}
    877900
Note: See TracChangeset for help on using the changeset viewer.