Skip to:
Content

BuddyPress.org

Changeset 9622


Ignore:
Timestamp:
03/17/2015 06:05:20 PM (10 years ago)
Author:
imath
Message:

Introduces the BuddyPress Attachments API

BP_Attachment is a new class custom and core components can extend to manage file uploads.

  • It is taking care of setting the uploads data (path, url..)
  • It is creating the specific base uploads directory for the component if requested
  • It is taking care of uploading the files into the uploads directory
  • Custom validation rules can be set in order to for instance: restrict mime types or file size
  • If, like avatars, cropping an image file is needed, the class includes a crop method to help you.

bp_upload_dir() is also a new function to make sure the uploads data will always be the one of the site BuddyPress is activated on (the root site).

Props johnjamesjacoby, boonebgorges

See #6278

Location:
trunk/src/bp-core
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-classes.php

    r9619 r9622  
    2222require dirname( __FILE__ ) . '/classes/class-bp-recursive-query.php';
    2323require dirname( __FILE__ ) . '/classes/class-bp-media-extractor.php';
     24require dirname( __FILE__ ) . '/classes/class-bp-attachment.php';
  • trunk/src/bp-core/bp-core-functions.php

    r9615 r9622  
    21482148    return apply_filters( 'bp_core_get_suggestions', $retval, $args );
    21492149}
     2150
     2151/**
     2152 * Set data from the BP root blog's upload directory.
     2153 *
     2154 * Handy for multisite instances because all uploads are made on the BP root
     2155 * blog and we need to query the BP root blog for the upload directory data.
     2156 *
     2157 * This function ensures that we only need to use {@link switch_to_blog()}
     2158 * once to get what we need.
     2159 *
     2160 * @since BuddyPress (2.3.0)
     2161 *
     2162 * @uses  is_multisite()
     2163 * @uses  bp_is_root_blog()
     2164 * @uses  switch_to_blog()
     2165 * @uses  wp_upload_dir()
     2166 * @uses  restore_current_blog()
     2167 */
     2168function bp_upload_dir() {
     2169    $bp = buddypress();
     2170
     2171    if ( empty( $bp->upload_dir ) ) {
     2172        $need_switch = (bool) ( is_multisite() && ! bp_is_root_blog() );
     2173
     2174        // Maybe juggle to root blog
     2175        if ( true === $need_switch ) {
     2176            switch_to_blog( bp_get_root_blog_id() );
     2177        }
     2178
     2179        // Get the upload directory (maybe for root blog)
     2180        $wp_upload_dir = wp_upload_dir();
     2181
     2182        // Maybe juggle back to current blog
     2183        if ( true === $need_switch ) {
     2184            restore_current_blog();
     2185        }
     2186
     2187        // Bail if an error occurred
     2188        if ( ! empty( $wp_upload_dir['error'] ) ) {
     2189            return false;
     2190        }
     2191
     2192        $bp->upload_dir = $wp_upload_dir;
     2193    }
     2194
     2195    return $bp->upload_dir;
     2196}
Note: See TracChangeset for help on using the changeset viewer.