Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
06/02/2024 01:12:39 AM (14 months ago)
Author:
espellcaste
Message:

WPCS: Part V: miscellaneous fixes for some of the files of the core component.

Follow-up to [13883], [13886], [13887], and [13888]

See #9164 and #7228

File:
1 edited

Legend:

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

    r13890 r13891  
    7474 * @since 3.0.0
    7575 *
    76  * @return array See wp_upload_dir().
     76 * @param array $args {
     77 *    @type int    $object_id        The ID of the object (Required).
     78 *    @type string $object_directory The object directory (eg: members, groups) (Required).
     79 * }
     80 * @return array
    7781 */
    7882function bp_attachments_cover_image_upload_dir( $args = array() ) {
     
    102106
    103107    // Set the subdir.
    104     $subdir  = '/' . $r['object_directory'] . '/' . $r['object_id'] . '/cover-image';
     108    $subdir = '/' . $r['object_directory'] . '/' . $r['object_id'] . '/cover-image';
    105109
    106110    $upload_dir = bp_attachments_uploads_dir_get();
     
    111115     * @since 2.4.0
    112116     *
    113      * @param array $value      Array containing the path, URL, and other helpful settings.
    114      * @param array $upload_dir The original Uploads dir.
     117     * @param array $image_upload_data Array containing the path, URL, and other helpful settings.
     118     * @param array $upload_dir        The original Uploads dir.
    115119     */
    116     return apply_filters( 'bp_attachments_cover_image_upload_dir', array(
    117         'path'    => $upload_dir['basedir'] . $subdir,
    118         'url'     => set_url_scheme( $upload_dir['baseurl'] ) . $subdir,
    119         'subdir'  => $subdir,
    120         'basedir' => $upload_dir['basedir'],
    121         'baseurl' => set_url_scheme( $upload_dir['baseurl'] ),
    122         'error'   => false,
    123     ), $upload_dir );
     120    return apply_filters(
     121        'bp_attachments_cover_image_upload_dir',
     122        array(
     123            'path'    => $upload_dir['basedir'] . $subdir,
     124            'url'     => set_url_scheme( $upload_dir['baseurl'] ) . $subdir,
     125            'subdir'  => $subdir,
     126            'basedir' => $upload_dir['basedir'],
     127            'baseurl' => set_url_scheme( $upload_dir['baseurl'] ),
     128            'error'   => false,
     129        ),
     130        $upload_dir
     131    );
    124132}
    125133
     
    197205            // Loop to explode keys using '|'.
    198206            foreach ( $allowed_types as $allowed_type ) {
    199                 $t = explode( '|', $allowed_type );
     207                $t    = explode( '|', $allowed_type );
    200208                $exts = array_merge( $exts, (array) $t );
    201209            }
     
    417425                'crop_h'        => $r['crop_h'],
    418426                'crop_x'        => $r['crop_x'],
    419                 'crop_y'        => $r['crop_y']
     427                'crop_y'        => $r['crop_y'],
    420428            )
    421429        );
    422430
    423     // It's a cover image we need to fit it to feature's dimensions.
     431        // It's a cover image we need to fit it to feature's dimensions.
    424432    } elseif ( 'cover_image' === $type ) {
    425433        $cover_image = bp_attachments_cover_image_generate_file(
     
    427435                'file'            => $image_file_path,
    428436                'component'       => $r['component'],
    429                 'cover_image_dir' => $attachment_data['path']
     437                'cover_image_dir' => $attachment_data['path'],
    430438            )
    431439        );
     
    520528            $attachment_data = trailingslashit( $type_dir ) . $r['file'];
    521529        }
    522 
    523530    } else {
    524531        $file = false;
    525532
    526533        // Open the directory and get the first file.
    527         if ( $att_dir = opendir( $type_dir ) ) {
     534        $att_dir = opendir( $type_dir );
     535        if ( $att_dir ) {
    528536
    529537            while ( false !== ( $attachment_file = readdir( $att_dir ) ) ) {
     
    604612        'file_data_name'      => 'file',
    605613        'multipart_params'    => array(
    606             'action'          => 'bp_upload_attachment',
    607             '_wpnonce'        => wp_create_nonce( 'bp-uploader' ),
     614            'action'   => 'bp_upload_attachment',
     615            '_wpnonce' => wp_create_nonce( 'bp-uploader' ),
    608616        ),
    609617        'url'                 => admin_url( 'admin-ajax.php', 'relative' ),
    610618        'flash_swf_url'       => includes_url( 'js/plupload/plupload.flash.swf' ),
    611619        'silverlight_xap_url' => includes_url( 'js/plupload/plupload.silverlight.xap' ),
    612         'filters' => array(
    613             'max_file_size'   => $max_upload_size . 'b',
     620        'filters'             => array(
     621            'max_file_size' => $max_upload_size . 'b',
    614622        ),
    615623        'multipart'           => true,
     
    625633
    626634    $settings = array(
    627         'defaults' => $defaults,
    628         'browser'  => array(
     635        'defaults'      => $defaults,
     636        'browser'       => array(
    629637            'mobile'    => wp_is_mobile(),
    630638            'supported' => _device_can_upload(),
     
    692700            /* translators: %s: File name. */
    693701            'error_uploading'           => __( '“%s” has failed to upload.', 'buddypress' ),
    694             'has_avatar_warning'        => __( 'If you'd like to delete the existing profile photo but not upload a new one, please use the delete tab.', 'buddypress' )
     702            'has_avatar_warning'        => __( 'If you'd like to delete the existing profile photo but not upload a new one, please use the delete tab.', 'buddypress' ),
    695703        )
    696704    );
     
    705713 * to set specific script data.
    706714 *
    707  * @param string $class Name of the class extending BP_Attachment (eg: BP_Attachment_Avatar).
     715 * @param string $class_name Name of the class extending BP_Attachment (eg: BP_Attachment_Avatar).
    708716 * @return null|WP_Error
    709717 */
    710 function bp_attachments_enqueue_scripts( $class = '' ) {
     718function bp_attachments_enqueue_scripts( $class_name = '' ) {
     719    $class = $class_name;
     720
    711721    // Enqueue me just once per page, please.
    712722    if ( did_action( 'bp_attachments_enqueue_scripts' ) ) {
     
    719729
    720730    // Get an instance of the class and get the script data.
    721     $attachment  = new $class;
     731    $attachment  = new $class();
    722732    $script_data = $attachment->script_data();
    723733
     
    766776
    767777    // Merge other arguments.
    768     $ui_args = array_intersect_key( $args, array(
    769         'file_data_name' => true,
    770         'browse_button'  => true,
    771         'container'      => true,
    772         'drop_element'   => true,
    773     ) );
     778    $ui_args = array_intersect_key(
     779        $args,
     780        array(
     781            'file_data_name' => true,
     782            'browse_button'  => true,
     783            'container'      => true,
     784            'drop_element'   => true,
     785        )
     786    );
    774787
    775788    $defaults = array_merge( $defaults, $ui_args );
     
    780793
    781794    if ( isset( $args['mime_types'] ) && $args['mime_types'] ) {
    782         $defaults['filters']['mime_types'] =  array( array( 'extensions' => $args['mime_types'] ) );
     795        $defaults['filters']['mime_types'] = array( array( 'extensions' => $args['mime_types'] ) );
    783796    }
    784797
     
    793806        // Include the cropping informations for avatars.
    794807        $settings['crop'] = array(
    795             'full_h'  => bp_core_avatar_full_height(),
    796             'full_w'  => bp_core_avatar_full_width(),
     808            'full_h' => bp_core_avatar_full_height(),
     809            'full_w' => bp_core_avatar_full_width(),
    797810        );
    798811
     
    811824        // Init the Avatar nav.
    812825        $avatar_nav = array(
    813             'upload'  => array(
     826            'upload' => array(
    814827                'id'      => 'upload',
    815828                'caption' => __( 'Upload', 'buddypress' ),
    816                 'order'   => 0
     829                'order'   => 0,
    817830            ),
    818             'delete'  => array(
     831            'delete' => array(
    819832                'id'      => 'delete',
    820833                'caption' => __( 'Delete', 'buddypress' ),
    821834                'order'   => 100,
    822                 'hide'    => (int) ! $has_avatar
     835                'hide'    => (int) ! $has_avatar,
    823836            ),
    824837        );
     
    851864                'id'      => 'camera',
    852865                'caption' => __( 'Take Photo', 'buddypress' ),
    853                 'order'   => 10
     866                'order'   => 10,
    854867            );
    855868
    856869            // Set warning messages.
    857870            $strings['camera_warnings'] = array(
    858                 'requesting'  => __( 'Please allow us to access to your camera.', 'buddypress'),
    859                 'loading'     => __( 'Please wait as we access your camera.', 'buddypress' ),
    860                 'loaded'      => __( 'Camera loaded. Click on the "Capture" button to take your photo.', 'buddypress' ),
    861                 'noaccess'    => __( 'It looks like you do not have a webcam or we were unable to get permission to use your webcam. Please upload a photo instead.', 'buddypress' ),
    862                 'errormsg'    => __( 'Your browser is not supported. Please upload a photo instead.', 'buddypress' ),
    863                 'videoerror'  => __( 'Video error. Please upload a photo instead.', 'buddypress' ),
    864                 'ready'       => __( 'Your profile photo is ready. Click on the "Save" button to use this photo.', 'buddypress' ),
    865                 'nocapture'   => __( 'No photo was captured. Click on the "Capture" button to take your photo.', 'buddypress' ),
     871                'requesting' => __( 'Please allow us to access to your camera.', 'buddypress' ),
     872                'loading'    => __( 'Please wait as we access your camera.', 'buddypress' ),
     873                'loaded'     => __( 'Camera loaded. Click on the "Capture" button to take your photo.', 'buddypress' ),
     874                'noaccess'   => __( 'It looks like you do not have a webcam or we were unable to get permission to use your webcam. Please upload a photo instead.', 'buddypress' ),
     875                'errormsg'   => __( 'Your browser is not supported. Please upload a photo instead.', 'buddypress' ),
     876                'videoerror' => __( 'Video error. Please upload a photo instead.', 'buddypress' ),
     877                'ready'      => __( 'Your profile photo is ready. Click on the "Save" button to use this photo.', 'buddypress' ),
     878                'nocapture'  => __( 'No photo was captured. Click on the "Capture" button to take your photo.', 'buddypress' ),
    866879            );
    867880        }
     
    886899        $settings['nav'] = bp_sort_by_key( apply_filters( 'bp_attachments_avatar_nav', $avatar_nav, $object ), 'order', 'num' );
    887900
    888     // Specific to BuddyPress cover images.
     901        // Specific to BuddyPress cover images.
    889902    } elseif ( 'bp_cover_image_upload' === $defaults['multipart_params']['action'] ) {
    890903
     
    908921
    909922        // Set warning messages.
    910         $strings['cover_image_warnings'] = apply_filters( 'bp_attachments_cover_image_ui_warnings', array(
    911             'dimensions'  => sprintf(
    912                 /* translators: 1: the advised width size in pixels. 2: the advised height size in pixels. */
    913                 __( 'For better results, make sure to upload an image that is larger than %1$spx wide, and %2$spx tall.', 'buddypress' ),
    914                 (int) $cover_dimensions['width'],
    915                 (int) $cover_dimensions['height']
    916             ),
    917         ) );
     923        $strings['cover_image_warnings'] = apply_filters(
     924            'bp_attachments_cover_image_ui_warnings',
     925            array(
     926                'dimensions' => sprintf(
     927                    /* translators: 1: the advised width size in pixels. 2: the advised height size in pixels. */
     928                    __( 'For better results, make sure to upload an image that is larger than %1$spx wide, and %2$spx tall.', 'buddypress' ),
     929                    (int) $cover_dimensions['width'],
     930                    (int) $cover_dimensions['height']
     931                ),
     932            )
     933        );
    918934    }
    919935
     
    936952    }
    937953
    938     wp_enqueue_script ( 'bp-plupload' );
    939     wp_localize_script( 'bp-plupload', 'BP_Uploader', array( 'strings' => $strings, 'settings' => $settings ) );
     954    wp_enqueue_script( 'bp-plupload' );
     955    wp_localize_script(
     956        'bp-plupload',
     957        'BP_Uploader',
     958        array(
     959            'strings'  => $strings,
     960            'settings' => $settings,
     961        )
     962    );
    940963
    941964    /**
     
    9871010                    $can = (bool) groups_is_user_admin( bp_loggedin_user_id(), $args['item_id'] ) || bp_current_user_can( 'bp_moderate' );
    9881011                }
    989             // User profile photo.
     1012                // User profile photo.
    9901013            } elseif ( bp_is_active( 'members' ) && 'user' === $args['object'] ) {
    9911014                $can = bp_loggedin_user_id() === (int) $args['item_id'] || bp_current_user_can( 'bp_moderate' );
    9921015            }
    993         /**
    994          * No avatar arguments, fallback to bp_user_can_create_groups()
    995          * or bp_is_item_admin()
    996          */
     1016            /**
     1017             * No avatar arguments, fallback to bp_user_can_create_groups()
     1018             * or bp_is_item_admin()
     1019             */
     1020        } elseif ( bp_is_group_create() ) {
     1021                $can = bp_user_can_create_groups();
    9971022        } else {
    998             if ( bp_is_group_create() ) {
    999                 $can = bp_user_can_create_groups();
    1000             } else {
    1001                 $can = bp_is_item_admin();
    1002             }
     1023            $can = bp_is_item_admin();
    10031024        }
    10041025    }
     
    10271048        wp_send_json( $response );
    10281049
    1029     /**
    1030     * Send specific json response
    1031     * the html4 Plupload handler requires a text/html content-type for older IE.
    1032     * See https://core.trac.wordpress.org/ticket/31037
    1033     */
     1050        /**
     1051        * Send specific json response
     1052        * the html4 Plupload handler requires a text/html content-type for older IE.
     1053        * See https://core.trac.wordpress.org/ticket/31037
     1054        */
    10341055    } else {
    10351056        echo wp_json_encode( $response );
     
    10451066 *
    10461067 * @param string $slug Template part slug. eg 'uploader' for 'uploader.php'.
    1047  * @return bool
    10481068 */
    10491069function bp_attachments_get_template_part( $slug ) {
     
    11371157
    11381158    // Current component is not supported.
    1139     if ( ! in_array( $component, $settings['components'] ) ) {
     1159    if ( ! in_array( $component, $settings['components'], true ) ) {
    11401160        return false;
    11411161    }
     
    11551175function bp_attachments_get_cover_image_dimensions( $component = 'members' ) {
    11561176    // Let's prevent notices when setting the warning strings.
    1157     $default = array( 'width' => 0, 'height' => 0 );
     1177    $default = array(
     1178        'width'  => 0,
     1179        'height' => 0,
     1180    );
    11581181
    11591182    $settings = bp_attachments_get_cover_image_settings( $component );
     
    12011224    }
    12021225
    1203     if ( ( bp_is_group_admin_page() && 'group-cover-image' == bp_get_group_current_admin_tab() )
     1226    if ( ( bp_is_group_admin_page() && 'group-cover-image' === bp_get_group_current_admin_tab() )
    12041227        || ( bp_is_group_create() && bp_is_group_creation_step( 'group-cover-image' ) ) ) {
    12051228        $retval = ! bp_disable_group_cover_image_uploads();
     
    12221245    }
    12231246
    1224     $cover_src = bp_attachments_get_attachment( 'url', array(
    1225         'item_id'   => $user_id,
    1226     ) );
     1247    $cover_src = bp_attachments_get_attachment(
     1248        'url',
     1249        array(
     1250            'item_id' => $user_id,
     1251        )
     1252    );
    12271253
    12281254    return (bool) apply_filters( 'bp_attachments_get_user_has_cover_image', $cover_src, $user_id );
     
    12431269    }
    12441270
    1245     $cover_src = bp_attachments_get_attachment( 'url', array(
    1246         'object_dir' => 'groups',
    1247         'item_id'    => $group_id,
    1248     ) );
     1271    $cover_src = bp_attachments_get_attachment(
     1272        'url',
     1273        array(
     1274            'object_dir' => 'groups',
     1275            'item_id'    => $group_id,
     1276        )
     1277    );
    12491278
    12501279    return (bool) apply_filters( 'bp_attachments_get_group_has_cover_image', $cover_src, $group_id );
     
    13301359        'cover_file'     => $cover_file,
    13311360        'cover_basename' => $cover_basename,
    1332         'is_too_small'   => $is_too_small
     1361        'is_too_small'   => $is_too_small,
    13331362    );
    13341363}
     
    13381367 *
    13391368 * @since 2.4.0
    1340  *
    1341  * @return string|null A json object containing success data if the upload succeeded,
    1342  *                     error message otherwise.
    13431369 */
    13441370function bp_attachments_cover_image_ajax_upload() {
     1371
    13451372    if ( ! bp_is_post_request() ) {
    13461373        wp_die();
     
    13501377
    13511378    // Sending the json response will be different if the current Plupload runtime is html4.
    1352     $is_html4 = ! empty( $_POST['html4' ] );
     1379    $is_html4 = ! empty( $_POST['html4'] );
    13531380
    13541381    if ( empty( $_POST['bp_params'] ) ) {
     
    13831410    // Member's cover image.
    13841411    if ( 'user' === $bp_params['object'] ) {
    1385         $object_data = array( 'dir' => 'members', 'component' => 'members' );
     1412        $object_data = array(
     1413            'dir'       => 'members',
     1414            'component' => 'members',
     1415        );
    13861416
    13871417        if ( ! bp_displayed_user_id() && ! empty( $bp_params['item_id'] ) ) {
    1388             $needs_reset = array( 'key' => 'displayed_user', 'value' => $bp->displayed_user );
     1418            $needs_reset            = array(
     1419                'key'   => 'displayed_user',
     1420                'value' => $bp->displayed_user,
     1421            );
    13891422            $bp->displayed_user->id = $bp_params['item_id'];
    13901423        }
    13911424
    1392     // Group's cover image.
     1425        // Group's cover image.
    13931426    } elseif ( 'group' === $bp_params['object'] ) {
    1394         $object_data = array( 'dir' => 'groups', 'component' => 'groups' );
     1427        $object_data = array(
     1428            'dir'       => 'groups',
     1429            'component' => 'groups',
     1430        );
    13951431
    13961432        if ( ! bp_get_current_group_id() && ! empty( $bp_params['item_id'] ) ) {
    1397             $needs_reset = array( 'component' => 'groups', 'key' => 'current_group', 'value' => $bp->groups->current_group );
     1433            $needs_reset               = array(
     1434                'component' => 'groups',
     1435                'key'       => 'current_group',
     1436                'value'     => $bp->groups->current_group,
     1437            );
    13981438            $bp->groups->current_group = groups_get_group( $bp_params['item_id'] );
    13991439        }
    14001440
    1401     // Other object's cover image.
     1441        // Other object's cover image.
    14021442    } else {
    14031443        $object_data = apply_filters( 'bp_attachments_cover_image_object_dir', array(), $bp_params['object'] );
     
    14271467
    14281468    $cover_image_attachment = new BP_Attachment_Cover_Image();
    1429     $uploaded = $cover_image_attachment->upload( $_FILES );
     1469    $uploaded               = $cover_image_attachment->upload( $_FILES );
    14301470
    14311471    // Reset objects.
     
    14401480    if ( ! empty( $uploaded['error'] ) ) {
    14411481        // Upload error response.
    1442         bp_attachments_json_response( false, $is_html4, array(
    1443             'type'    => 'upload_error',
    1444             'message' => sprintf(
    1445                 /* translators: %s: the upload error message */
    1446                 __( 'Upload Failed! Error was: %s', 'buddypress' ),
    1447                 $uploaded['error']
    1448             ),
    1449         ) );
     1482        bp_attachments_json_response(
     1483            false,
     1484            $is_html4,
     1485            array(
     1486                'type'    => 'upload_error',
     1487                'message' => sprintf(
     1488                    /* translators: %s: the upload error message */
     1489                    __( 'Upload Failed! Error was: %s', 'buddypress' ),
     1490                    $uploaded['error']
     1491                ),
     1492            )
     1493        );
    14501494    }
    14511495
     
    14561500    // The BP Attachments Uploads Dir is not set, stop.
    14571501    if ( ! $bp_attachments_uploads_dir ) {
    1458         bp_attachments_json_response( false, $is_html4, array(
    1459             'type'    => 'upload_error',
    1460             'message' => $error_message,
    1461         ) );
     1502        bp_attachments_json_response(
     1503            false,
     1504            $is_html4,
     1505            array(
     1506                'type'    => 'upload_error',
     1507                'message' => $error_message,
     1508            )
     1509        );
    14621510    }
    14631511
     
    14671515    if ( 1 === validate_file( $cover_dir ) || ! is_dir( $cover_dir ) ) {
    14681516        // Upload error response.
    1469         bp_attachments_json_response( false, $is_html4, array(
    1470             'type'    => 'upload_error',
    1471             'message' => $error_message,
    1472         ) );
     1517        bp_attachments_json_response(
     1518            false,
     1519            $is_html4,
     1520            array(
     1521                'type'    => 'upload_error',
     1522                'message' => $error_message,
     1523            )
     1524        );
    14731525    }
    14741526
     
    14801532     * class, let's use it.
    14811533     */
    1482     $cover = bp_attachments_cover_image_generate_file( array(
    1483         'file'            => $uploaded['file'],
    1484         'component'       => $object_data['component'],
    1485         'cover_image_dir' => $cover_dir
    1486     ), $cover_image_attachment );
     1534    $cover = bp_attachments_cover_image_generate_file(
     1535        array(
     1536            'file'            => $uploaded['file'],
     1537            'component'       => $object_data['component'],
     1538            'cover_image_dir' => $cover_dir,
     1539        ),
     1540        $cover_image_attachment
     1541    );
    14871542
    14881543    if ( ! $cover ) {
    1489         bp_attachments_json_response( false, $is_html4, array(
    1490             'type'    => 'upload_error',
    1491             'message' => $error_message,
    1492         ) );
     1544        bp_attachments_json_response(
     1545            false,
     1546            $is_html4,
     1547            array(
     1548                'type'    => 'upload_error',
     1549                'message' => $error_message,
     1550            )
     1551        );
    14931552    }
    14941553
     
    15531612
    15541613    // Finally return the cover image url to the UI.
    1555     bp_attachments_json_response( true, $is_html4, array(
    1556         'name'          => $name,
    1557         'url'           => $cover_url,
    1558         'feedback_code' => $feedback_code,
    1559     ) );
     1614    bp_attachments_json_response(
     1615        true,
     1616        $is_html4,
     1617        array(
     1618            'name'          => $name,
     1619            'url'           => $cover_url,
     1620            'feedback_code' => $feedback_code,
     1621        )
     1622    );
    15601623}
    15611624add_action( 'wp_ajax_bp_cover_image_upload', 'bp_attachments_cover_image_ajax_upload' );
     
    15651628 *
    15661629 * @since 2.4.0
    1567  *
    1568  * @return string|null A json object containing success data if the cover image was deleted
    1569  *                     error message otherwise.
    15701630 */
    15711631function bp_attachments_cover_image_ajax_delete() {
     1632
    15721633    if ( ! bp_is_post_request() ) {
    15731634        wp_send_json_error();
     
    15941655        $dir       = 'members';
    15951656
    1596     // Set it for any other cases.
     1657        // Set it for any other cases.
    15971658    } else {
    15981659        $component = $args['object'] . 's';
     
    16011662
    16021663    // Handle delete.
    1603     if ( bp_attachments_delete_file( array( 'item_id' => $args['item_id'], 'object_dir' => $dir, 'type' => 'cover-image' ) ) ) {
     1664    if ( bp_attachments_delete_file(
     1665        array(
     1666            'item_id'    => $args['item_id'],
     1667            'object_dir' => $dir,
     1668            'type'       => 'cover-image',
     1669        )
     1670    ) ) {
    16041671        $item_id = (int) $args['item_id'];
    16051672
     
    16401707
    16411708    } else {
    1642         wp_send_json_error( array(
    1643             'feedback_code' => 2,
    1644         ) );
     1709        wp_send_json_error(
     1710            array(
     1711                'feedback_code' => 2,
     1712            )
     1713        );
    16451714    }
    16461715}
     
    17291798        $supported_file = bp_attachments_get_file_object( $file );
    17301799
    1731         if ( is_null( $supported_file) ) {
     1800        if ( is_null( $supported_file ) ) {
    17321801            continue;
    17331802        }
     
    17621831        $supported_file = bp_attachments_get_file_object( $file );
    17631832
    1764         if ( is_null( $supported_file) ) {
     1833        if ( is_null( $supported_file ) ) {
    17651834            continue;
    17661835        }
Note: See TracChangeset for help on using the changeset viewer.