Skip to:
Content

BuddyPress.org

Changeset 11875


Ignore:
Timestamp:
03/02/2018 07:37:55 PM (8 years ago)
Author:
boonebgorges
Message:

Centralize the generation of the cover image upload directory.

By using bp_attachments_cover_image_upload_dir() wherever the cover image
directory is needed, we ensure that it's possible to filter consistently, for
installations (such as multi-network installations) that may want a custom
location for these files.

Props rekmla, imath.
Fixes #7265.

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

Legend:

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

    r11874 r11875  
    8282         */
    8383        return apply_filters( 'bp_attachments_uploads_dir_get', $retval, $data );
     84}
     85
     86/**
     87 * Gets the upload dir array for cover images.
     88 *
     89 * @since 3.0.0
     90 *
     91 * @return array See wp_upload_dir().
     92 */
     93function bp_attachments_cover_image_upload_dir( $args = array() ) {
     94        // Default values are for profiles.
     95        $object_id = bp_displayed_user_id();
     96
     97        if ( empty( $object_id ) ) {
     98                $object_id = bp_loggedin_user_id();
     99        }
     100
     101        $object_directory = 'members';
     102
     103        // We're in a group, edit default values.
     104        if ( bp_is_group() || bp_is_group_create() ) {
     105                $object_id        = bp_get_current_group_id();
     106                $object_directory = 'groups';
     107        }
     108
     109        $r = bp_parse_args( $args, array(
     110                'object_id' => $object_id,
     111                'object_directory' => $object_directory,
     112        ), 'cover_image_upload_dir' );
     113
     114
     115        // Set the subdir.
     116        $subdir  = '/' . $r['object_directory'] . '/' . $r['object_id'] . '/cover-image';
     117
     118        $upload_dir = bp_attachments_uploads_dir_get();
     119
     120        /**
     121         * Filters the cover image upload directory.
     122         *
     123         * @since 2.4.0
     124         *
     125         * @param array $value      Array containing the path, URL, and other helpful settings.
     126         * @param array $upload_dir The original Uploads dir.
     127         */
     128        return apply_filters( 'bp_attachments_cover_image_upload_dir', array(
     129                'path'    => $upload_dir['basedir'] . $subdir,
     130                'url'     => set_url_scheme( $upload_dir['baseurl'] ) . $subdir,
     131                'subdir'  => $subdir,
     132                'basedir' => $upload_dir['basedir'],
     133                'baseurl' => set_url_scheme( $upload_dir['baseurl'] ),
     134                'error'   => false,
     135        ), $upload_dir );
    84136}
    85137
     
    313365                }
    314366        } elseif ( 'cover_image' === $type ) {
    315                 $attachment_data = bp_attachments_uploads_dir_get();
     367                $attachment_data = bp_attachments_cover_image_upload_dir();
    316368
    317369                // The BP Attachments Uploads Dir is not set, stop.
     
    11311183        }
    11321184
     1185        $upload_dir = bp_attachments_cover_image_upload_dir();
     1186
    11331187        // Make sure the file is inside the Cover Image Upload path.
    1134         if ( false === strpos( $args['file'], $cover_image_class->upload_path ) ) {
     1188        if ( false === strpos( $args['file'], $upload_dir['basedir'] ) ) {
    11351189                return false;
    11361190        }
     
    12911345        $error_message = __( 'There was a problem uploading the cover image.', 'buddypress' );
    12921346
    1293         $bp_attachments_uploads_dir = bp_attachments_uploads_dir_get();
     1347        $bp_attachments_uploads_dir = bp_attachments_cover_image_upload_dir();
    12941348
    12951349        // The BP Attachments Uploads Dir is not set, stop.
  • trunk/src/bp-core/classes/class-bp-attachment-cover-image.php

    r11447 r11875  
    9797         */
    9898        public function upload_dir_filter( $upload_dir = array() ) {
    99                 // Default values are for profiles.
    100                 $object_id = bp_displayed_user_id();
    101 
    102                 if ( empty( $object_id ) ) {
    103                         $object_id = bp_loggedin_user_id();
    104                 }
    105 
    106                 $object_directory = 'members';
    107 
    108                 // We're in a group, edit default values.
    109                 if ( bp_is_group() || bp_is_group_create() ) {
    110                         $object_id        = bp_get_current_group_id();
    111                         $object_directory = 'groups';
    112                 }
    113 
    114                 // Set the subdir.
    115                 $subdir  = '/' . $object_directory . '/' . $object_id . '/cover-image';
    116 
    117                 /**
    118                  * Filters the cover image upload directory.
    119                  *
    120                  * @since 2.4.0
    121                  *
    122                  * @param array $value      Array containing the path, URL, and other helpful settings.
    123                  * @param array $upload_dir The original Uploads dir.
    124                  */
    125                 return apply_filters( 'bp_attachments_cover_image_upload_dir', array(
    126                         'path'    => $this->upload_path . $subdir,
    127                         'url'     => $this->url . $subdir,
    128                         'subdir'  => $subdir,
    129                         'basedir' => $this->upload_path,
    130                         'baseurl' => $this->url,
    131                         'error'   => false
    132                 ), $upload_dir );
     99                return bp_attachments_cover_image_upload_dir();
    133100        }
    134101
Note: See TracChangeset for help on using the changeset viewer.