Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
11/15/2015 07:13:42 PM (11 years ago)
Author:
tw2113
Message:

More documentation cleanup for part of BP-Core component.

See #6398.

File:
1 edited

Legend:

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

    r10194 r10355  
    5050     * @since 2.3.0
    5151     * @since 2.4.0 Add the $upload_dir_filter_args argument to the $arguments array
     52     * @uses                                    sanitize_key()
     53     * @uses                                    wp_max_upload_size()
     54     * @uses                                    bp_parse_args()
     55     * @uses                                    BP_Attachment->set_upload_error_strings()
     56     * @uses                                    BP_Attachment->set_upload_dir()
    5257     *
    5358     * @param array|string $args {
     
    6469     *                                          Defaults to 0 (optional).
    6570     * }
    66      * @uses  sanitize_key()
    67      * @uses  wp_max_upload_size()
    68      * @uses  bp_parse_args()
    69      * @uses  BP_Attachment->set_upload_error_strings()
    70      * @uses  BP_Attachment->set_upload_dir()
    7171     */
    7272    public function __construct( $args = '' ) {
    73         // Upload action and the file input name are required parameters
     73        // Upload action and the file input name are required parameters.
    7474        if ( empty( $args['action'] ) || empty( $args['file_input'] ) ) {
    7575            return false;
    7676        }
    7777
    78         // Sanitize the action ID and the file input name
     78        // Sanitize the action ID and the file input name.
    7979        $this->action     = sanitize_key( $args['action'] );
    8080        $this->file_input = sanitize_key( $args['file_input'] );
     
    9292                $this->{$key} = $this->set_upload_error_strings( $param );
    9393
    94             // Sanitize the base dir
     94            // Sanitize the base dir.
    9595            } elseif ( 'base_dir' === $key ) {
    9696                $this->{$key} = sanitize_title( $param );
    9797
    98             // Sanitize the upload dir filter arg to pass
     98            // Sanitize the upload dir filter arg to pass.
    9999            } elseif ( 'upload_dir_filter_args' === $key ) {
    100100                $this->{$key} = (int) $param;
    101101
    102             // Action & File input are already set and sanitized
     102            // Action & File input are already set and sanitized.
    103103            } elseif ( 'action' !== $key && 'file_input' !== $key ) {
    104104                $this->{$key} = $param;
     
    106106        }
    107107
    108         // Set the path/url and base dir for uploads
     108        // Set the path/url and base dir for uploads.
    109109        $this->set_upload_dir();
    110110    }
     
    118118     */
    119119    public function set_upload_dir() {
    120         // Set the directory, path, & url variables
     120        // Set the directory, path, & url variables.
    121121        $this->upload_dir  = bp_upload_dir();
    122122
     
    128128        $this->url         = $this->upload_dir['baseurl'];
    129129
    130         // Ensure URL is https if SSL is set/forced
     130        // Ensure URL is https if SSL is set/forced.
    131131        if ( is_ssl() ) {
    132132            $this->url = str_replace( 'http://', 'https://', $this->url );
     
    142142            $this->url         = trailingslashit( $this->url  ) . $this->base_dir;
    143143
    144             // Finally create the base dir
     144            // Finally create the base dir.
    145145            $this->create_dir();
    146146        }
     
    155155     *
    156156     * @param array $param A list of error messages to add to BuddyPress core ones.
    157      *
    158      * @return array the list of upload errors
     157     * @return array $upload_errors The list of upload errors.
    159158     */
    160159    public function set_upload_error_strings( $param = array() ) {
     
    204203     * @since 2.3.0
    205204     *
    206      * @param  array       $file              The appropriate entry the from $_FILES superglobal.
    207      * @param  string      $upload_dir_filter A specific filter to be applied to 'upload_dir' (optional).
    208      * @param  string|null $time              Optional. Time formatted in 'yyyy/mm'. Default null.
    209      *
    210205     * @uses   wp_handle_upload()        To upload the file
    211206     * @uses   add_filter()              To temporarly overrides WordPress uploads data
     
    213208     * @uses   apply_filters()           Call 'bp_attachment_upload_overrides' to include specific upload overrides
    214209     *
    215      * @return array                     On success, returns an associative array of file attributes.
    216      *                                   On failure, returns an array containing the error message
    217      *                                   (eg: array( 'error' => $message ) )
     210     * @param  array       $file              The appropriate entry the from $_FILES superglobal.
     211     * @param  string      $upload_dir_filter A specific filter to be applied to 'upload_dir' (optional).
     212     * @param  string|null $time              Optional. Time formatted in 'yyyy/mm'. Default null.
     213     * @return array On success, returns an associative array of file attributes.
     214     *               On failure, returns an array containing the error message
     215     *               (eg: array( 'error' => $message ) )
    218216     */
    219217    public function upload( $file, $upload_dir_filter = '', $time = null ) {
     
    238236        add_filter( 'wp_handle_upload_prefilter', array( $this, 'validate_upload' ), 10, 1 );
    239237
    240         // Set Default overrides
     238        // Set Default overrides.
    241239        $overrides = array(
    242240            'action'               => $this->action,
     
    275273        }
    276274
    277         // Make sure the file will be uploaded in the attachment directory
     275        // Make sure the file will be uploaded in the attachment directory.
    278276        if ( ! empty( $upload_dir_filter ) ) {
    279277            add_filter( 'upload_dir', $upload_dir_filter, 10, $this->upload_dir_filter_args );
    280278        }
    281279
    282         // Upload the attachment
     280        // Upload the attachment.
    283281        $this->attachment = wp_handle_upload( $file[ $this->file_input ], $overrides, $time );
    284282
    285         // Restore WordPress Uploads data
     283        // Restore WordPress Uploads data.
    286284        if ( ! empty( $upload_dir_filter ) ) {
    287285            remove_filter( 'upload_dir', $upload_dir_filter, 10, $this->upload_dir_filter_args );
    288286        }
    289287
    290         // Remove the pre WordPress 4.0 static filter
     288        // Remove the pre WordPress 4.0 static filter.
    291289        remove_filter( 'wp_handle_upload_prefilter', array( $this, 'validate_upload' ), 10, 1 );
    292290
    293         // Finally return the uploaded file or the error
     291        // Finally return the uploaded file or the error.
    294292        return $this->attachment;
    295293    }
     
    300298     * In case of a multisite, the mime types are already restricted by
    301299     * the 'upload_filetypes' setting. BuddyPress will respect this setting.
     300     *
    302301     * @see check_upload_mimes()
    303302     *
     
    310309        $valid_mimes = array();
    311310
    312         // Set the allowed mimes for the upload
     311        // Set the allowed mimes for the upload.
    313312        foreach ( (array) $this->allowed_mime_types as $ext ) {
    314313            foreach ( $wp_mimes as $ext_pattern => $mime ) {
     
    333332     *
    334333     * @param  array $file The temporary file attributes (before it has been moved).
    335      *
    336334     * @return array The file.
    337335     */
    338336    public function validate_upload( $file = array() ) {
    339         // Bail if already an error
     337        // Bail if already an error.
    340338        if ( ! empty( $file['error'] ) ) {
    341339            return $file;
     
    346344        }
    347345
    348         // Return the file
     346        // Return the file.
    349347        return $file;
    350348    }
     
    394392     */
    395393    public function create_dir() {
    396         // Bail if no specific base dir is set
     394        // Bail if no specific base dir is set.
    397395        if ( empty( $this->base_dir ) ) {
    398396            return false;
    399397        }
    400398
    401         // Check if upload path already exists
     399        // Check if upload path already exists.
    402400        if ( ! is_dir( $this->upload_path ) ) {
    403401
    404             // If path does not exist, attempt to create it
     402            // If path does not exist, attempt to create it.
    405403            if ( ! wp_mkdir_p( $this->upload_path ) ) {
    406404                return false;
     
    408406        }
    409407
    410         // Directory exists
     408        // Directory exists.
    411409        return true;
    412410    }
     
    452450        }
    453451
    454         // Check image file pathes
     452        // Check image file pathes.
    455453        $path_error = __( 'Cropping the file failed: the file path is not allowed.', 'buddypress' );
    456454
    457         // Make sure it's coming from an uploaded file
     455        // Make sure it's coming from an uploaded file.
    458456        if ( false === strpos( $r['original_file'], $this->upload_path ) ) {
    459457            $wp_error->add( 'crop_error', $path_error );
     
    464462         * If no destination file is provided, WordPress will use a default name
    465463         * and will write the file in the source file's folder.
    466          * If a destination file is provided, we need to make sure it's going into uploads
     464         * If a destination file is provided, we need to make sure it's going into uploads.
    467465         */
    468466        if ( ! empty( $r['dst_file'] ) && false === strpos( $r['dst_file'], $this->upload_path ) ) {
     
    471469        }
    472470
    473         // Check image file types
     471        // Check image file types.
    474472        $check_types = array( 'src_file' => array( 'file' => $r['original_file'], 'error' => _x( 'source file', 'Attachment source file', 'buddypress' ) ) );
    475473        if ( ! empty( $r['dst_file'] ) ) {
     
    478476
    479477        /**
    480          * WordPress image supported types
     478         * WordPress image supported types.
    481479         * @see wp_attachment_is()
    482480         */
     
    499497        }
    500498
    501         // Add the image.php to the required WordPress files, if it's not already the case
     499        // Add the image.php to the required WordPress files, if it's not already the case.
    502500        $required_files = array_flip( $this->required_wp_files );
    503501        if ( ! isset( $required_files['image'] ) ) {
     
    505503        }
    506504
    507         // Load the files
     505        // Load the files.
    508506        $this->includes();
    509507
    510         // Finally crop the image
     508        // Finally crop the image.
    511509        return wp_crop_image( $r['original_file'], (int) $r['crop_x'], (int) $r['crop_y'], (int) $r['crop_w'], (int) $r['crop_h'], (int) $r['dst_w'], (int) $r['dst_h'], $r['src_abs'], $r['dst_file'] );
    512510    }
     
    545543     */
    546544    public static function get_image_data( $file ) {
    547         // Try to get image basic data
     545        // Try to get image basic data.
    548546        list( $width, $height, $sourceImageType ) = @getimagesize( $file );
    549547
     
    553551        }
    554552
    555         // Initialize the image data
     553        // Initialize the image data.
    556554        $image_data = array(
    557555            'width'  => $width,
     
    567565        }
    568566
    569         // Now try to get image's meta data
     567        // Now try to get image's meta data.
    570568        $meta = wp_read_image_metadata( $file );
    571569
    572570        if ( ! empty( $meta ) ) {
    573             // Before 4.0 the Orientation wasn't included
     571            // Before 4.0 the Orientation wasn't included.
    574572            if ( ! isset( $meta['orientation'] ) &&
    575573                is_callable( 'exif_read_data' ) &&
     
    583581            }
    584582
    585             // Now add the metas to image data
     583            // Now add the metas to image data.
    586584            $image_data['meta'] = $meta;
    587585        }
     
    602600     * @since  2.4.0
    603601     *
    604      * @param  string $attachment_type The attachment type (eg: avatar or cover_image). Required.
    605      * @param  array  array $args {
     602     * @param string $attachment_type The attachment type (eg: avatar or cover_image). Required.
     603     * @param array $args {
    606604     *     @type string $file     Absolute path to the image file (required).
    607605     *     @type int    $max_w    Max width attribute for the editor's resize method (optional).
     
    612610     *     @type bool   $save     Whether to use the editor's save method or not (optional).
    613611     * }
    614      *
    615612     * @return string|WP_Image_Editor|WP_Error The edited image path or the WP_Image_Editor object in case of success,
    616613     *                                         an WP_Error object otherwise.
     
    636633        }
    637634
    638         // Get the image editor
     635        // Get the image editor.
    639636        $editor = wp_get_image_editor( $r['file'] );
    640637
     
    648645            $rotated = $editor->rotate( $r['rotate'] );
    649646
    650             // Stop in case of error
     647            // Stop in case of error.
    651648            if ( is_wp_error( $rotated ) ) {
    652649                return $rotated;
     
    657654            $resized = $editor->resize( $r['max_w'], $r['max_h'], $r['crop'] );
    658655
    659             // Stop in case of error
     656            // Stop in case of error.
    660657            if ( is_wp_error( $resized ) ) {
    661658                return $resized;
     
    663660        }
    664661
    665         // Use the editor save method to get a path to the edited image
     662        // Use the editor save method to get a path to the edited image.
    666663        if ( true === $r['save'] ) {
    667664            return $editor->save( $editor->generate_filename() );
    668665
    669         // Need to do some other edit actions or use a specific method to save file
     666        // Need to do some other edit actions or use a specific method to save file.
    670667        } else {
    671668            return $editor;
Note: See TracChangeset for help on using the changeset viewer.