Skip to:
Content

BuddyPress.org

Changeset 13177


Ignore:
Timestamp:
12/11/2021 01:40:24 PM (3 years ago)
Author:
imath
Message:

Make sure the new_avatar activity can use the avatar crop results

Since [13175] the BP_Attachment_Avatar::crop() method include the timestamp the avatar was generated on into its returned array. This array is now transported into the action the function creating a new_avatar activity hooks to. This function (bp_members_new_avatar_activity()) now accepts 3 more parameters included the transported crop results. This makes it possible to use the timestamp when the avatar was generated on as the new_avatar activity recorded time. Doing so, we don't need to add an extra activity meta to store the name of the avatar file to be sure to display the avatar the user had at the time the activity was created.

Props vapvarun, oztaser

See #8581

Location:
trunk/src
Files:
5 edited

Legend:

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

    r13176 r13177  
    11771177 *
    11781178 * @since 2.3.0
     1179 * @since 10.0.0 Adds the `$return` param to eventually return the crop result.
    11791180 *
    11801181 * @param string $data    Base64 encoded image.
    11811182 * @param int    $item_id Item to associate.
    1182  * @return bool True on success, false on failure.
    1183  */
    1184 function bp_avatar_handle_capture( $data = '', $item_id = 0 ) {
     1183 * @param string $return  Whether to get the crop `array` or a `boolean`. Defaults to `boolean`.
     1184 * @return array|bool True on success, false on failure.
     1185 */
     1186function bp_avatar_handle_capture( $data = '', $item_id = 0, $return = 'boolean' ) {
    11851187    if ( empty( $data ) || empty( $item_id ) ) {
    11861188        return false;
     
    12381240        $crop_args = array( 'item_id' => $item_id, 'original_file' => $avatar_to_crop, 'crop_x' => 0, 'crop_y' => 0 );
    12391241
     1242        if ( 'array' === $return ) {
     1243            return bp_core_avatar_handle_crop( $crop_args, 'array' );
     1244        }
     1245
    12401246        return bp_core_avatar_handle_crop( $crop_args );
    12411247    } else {
     
    12481254 *
    12491255 * @since 1.1.0
     1256 * @since 10.0.0 Adds the `$return` param to eventually return the crop result.
    12501257 *
    12511258 * @param array|string $args {
     
    12661273 *     @type int         $crop_y        The vertical starting point of the crop. Default: 0.
    12671274 * }
    1268  * @return bool True on success, false on failure.
    1269  */
    1270 function bp_core_avatar_handle_crop( $args = '' ) {
     1275 * @param string       $return Whether to get the crop `array` or a `boolean`. Defaults to `boolean`.
     1276 * @return array|bool True or the crop result on success, false on failure.
     1277 */
     1278function bp_core_avatar_handle_crop( $args = '', $return = 'boolean' ) {
    12711279
    12721280    $r = bp_parse_args(
     
    13071315    }
    13081316
     1317    if ( 'array' === $return ) {
     1318        return $cropped;
     1319    }
     1320
    13091321    return true;
    13101322}
     
    13531365        }
    13541366
    1355         if ( ! bp_avatar_handle_capture( $webcam_avatar, $avatar_data['item_id'] ) ) {
     1367        $cropped_webcam_avatar = bp_avatar_handle_capture( $webcam_avatar, $avatar_data['item_id'], 'array' );
     1368
     1369        if ( ! $cropped_webcam_avatar ) {
    13561370            wp_send_json_error( array(
    13571371                'feedback_code' => 1
     
    13601374        } else {
    13611375            $return = array(
    1362                 'avatar' => esc_url( bp_core_fetch_avatar( array(
    1363                     'object'  => $avatar_data['object'],
    1364                     'item_id' => $avatar_data['item_id'],
    1365                     'html'    => false,
    1366                     'type'    => 'full',
    1367                 ) ) ),
     1376                'avatar' => esc_url(
     1377                    bp_core_fetch_avatar(
     1378                        array(
     1379                            'object'  => $avatar_data['object'],
     1380                            'item_id' => $avatar_data['item_id'],
     1381                            'html'    => false,
     1382                            'type'    => 'full',
     1383                        )
     1384                    )
     1385                ),
    13681386                'feedback_code' => 2,
    13691387                'item_id'       => $avatar_data['item_id'],
     
    13771395             *
    13781396             * @since 6.0.0
     1397             * @since 10.0.0 Adds a new param: an array containing the full, thumb avatar and the timestamp.
    13791398             *
    1380              * @param string $item_id     Inform about the user id the avatar was set for.
    1381              * @param string $type        Inform about the way the avatar was set ('camera').
    1382              * @param array  $avatar_data Array of parameters passed to the avatar handler.
     1399             * @param string $item_id               Inform about the user id the avatar was set for.
     1400             * @param string $type                  Inform about the way the avatar was set ('camera').
     1401             * @param array  $avatar_data           Array of parameters passed to the crop handler.
     1402             * @param array  $cropped_webcam_avatar Array containing the full, thumb avatar and the timestamp.
    13831403             */
    1384             do_action( 'bp_members_avatar_uploaded', (int) $avatar_data['item_id'], $avatar_data['type'], $avatar_data );
     1404            do_action( 'bp_members_avatar_uploaded', (int) $avatar_data['item_id'], $avatar_data['type'], $avatar_data, $cropped_webcam_avatar );
    13851405
    13861406            wp_send_json_success( $return );
     
    14141434
    14151435    // Handle crop.
    1416     if ( bp_core_avatar_handle_crop( $r ) ) {
     1436    $cropped_avatar = bp_core_avatar_handle_crop( $r, 'array' );
     1437
     1438    if ( $cropped_avatar ) {
    14171439        $return = array(
    1418             'avatar' => esc_url( bp_core_fetch_avatar( array(
    1419                 'object'  => $avatar_data['object'],
    1420                 'item_id' => $avatar_data['item_id'],
    1421                 'html'    => false,
    1422                 'type'    => 'full',
    1423             ) ) ),
     1440            'avatar' => esc_url(
     1441                bp_core_fetch_avatar(
     1442                    array(
     1443                        'object'  => $avatar_data['object'],
     1444                        'item_id' => $avatar_data['item_id'],
     1445                        'html'    => false,
     1446                        'type'    => 'full',
     1447                    )
     1448                )
     1449            ),
    14241450            'feedback_code' => 2,
    14251451            'item_id'       => $avatar_data['item_id'],
     
    14311457
    14321458            /** This action is documented in bp-core/bp-core-avatars.php */
    1433             do_action( 'bp_members_avatar_uploaded', (int) $avatar_data['item_id'], $avatar_data['type'], $r );
     1459            do_action( 'bp_members_avatar_uploaded', (int) $avatar_data['item_id'], $avatar_data['type'], $r, $cropped_avatar );
    14341460        } elseif ( 'group' === $avatar_data['object'] ) {
    14351461            /** This action is documented in bp-groups/bp-groups-screens.php */
    1436             do_action( 'groups_avatar_uploaded', (int) $avatar_data['item_id'], $avatar_data['type'], $r );
     1462            do_action( 'groups_avatar_uploaded', (int) $avatar_data['item_id'], $avatar_data['type'], $r, $cropped_avatar );
    14371463        }
    14381464
  • trunk/src/bp-groups/actions/create.php

    r12433 r13177  
    271271            );
    272272
    273             if ( ! bp_core_avatar_handle_crop( $args ) ) {
     273            $cropped_avatar = bp_core_avatar_handle_crop( $args, 'array' );
     274
     275            if ( ! $cropped_avatar ) {
    274276                bp_core_add_message( __( 'There was an error saving the group profile photo, please try uploading again.', 'buddypress' ), 'error' );
    275277            } else {
     
    278280                 *
    279281                 * @since 2.8.0
     282                 * @since 10.0.0 Adds a new param: an array containing the full, thumb avatar and the timestamp.
    280283                 *
    281                  * @param int    $group_id ID of the group.
    282                  * @param string $type     Avatar type. 'crop' or 'full'.
    283                  * @param array  $args     Array of parameters passed to the avatar handler.
     284                 * @param int    $group_id       ID of the group.
     285                 * @param string $type           Avatar type. 'crop' or 'camera'.
     286                 * @param array  $args           Array of parameters passed to the crop handler.
     287                 * @param array  $cropped_avatar Array containing the full, thumb avatar and the timestamp.
    284288                 */
    285                 do_action( 'groups_avatar_uploaded', bp_get_current_group_id(), 'crop', $args );
     289                do_action( 'groups_avatar_uploaded', bp_get_current_group_id(), 'crop', $args, $cropped_avatar );
    286290
    287291                bp_core_add_message( __( 'The group profile photo was uploaded successfully.', 'buddypress' ) );
  • trunk/src/bp-groups/screens/single/admin/group-avatar.php

    r11923 r13177  
    7575        );
    7676
    77         if ( !bp_core_avatar_handle_crop( $args ) ) {
     77        $cropped_avatar = bp_core_avatar_handle_crop( $args, 'array' );
     78
     79        if ( ! $cropped_avatar ) {
    7880            bp_core_add_message( __( 'There was a problem cropping the group profile photo.', 'buddypress' ), 'error' );
    7981        } else {
     
    8284             *
    8385             * @since 2.8.0
     86             * @since 10.0.0 Adds a new param: an array containing the full, thumb avatar and the timestamp.
    8487             *
    85              * @param int    $group_id ID of the group.
    86              * @param string $type     Avatar type. 'crop' or 'full'.
    87              * @param array  $args     Array of parameters passed to the avatar handler.
     88             * @param int    $group_id       ID of the group.
     89             * @param string $type           Avatar type. 'crop' or 'camera'.
     90             * @param array  $args           Array of parameters passed to the avatar handler.
     91             * @param array  $cropped_avatar Array containing the full, thumb avatar and the timestamp.
    8892             */
    89             do_action( 'groups_avatar_uploaded', bp_get_current_group_id(), 'crop', $args );
     93            do_action( 'groups_avatar_uploaded', bp_get_current_group_id(), 'crop', $args, $cropped_avatar );
    9094            bp_core_add_message( __( 'The new group profile photo was uploaded successfully.', 'buddypress' ) );
    9195        }
  • trunk/src/bp-members/bp-members-activity.php

    r13155 r13177  
    167167 *
    168168 * @since 8.0.0
    169  *
    170  * @param int $user_id The user id the avatar was set for.
    171  */
    172 function bp_members_new_avatar_activity( $user_id = 0 ) {
     169 * @since 10.0.0 Adds the `$type`, `$crop_data` and `$cropped_avatar` parameters.
     170 *
     171 * @param int    $user_id        The user id the avatar was set for.
     172 * @param string $type           The way the avatar was set ('camera' or `crop`).
     173 * @param array  $crop_data      Array of parameters passed to the crop handler.
     174 * @param array  $cropped_avatar Array containing the full, thumb avatar and the timestamp.
     175 */
     176function bp_members_new_avatar_activity( $user_id = 0, $type = '', $crop_data = array(), $cropped_avatar = array() ) {
    173177
    174178    // Bail if activity component is not active.
     
    231235    }
    232236
     237    $recorded_time = '';
     238    if ( isset( $cropped_avatar['timestamp'] ) && $cropped_avatar['timestamp'] ) {
     239        $recorded_time = date( 'Y-m-d H:i:s', $cropped_avatar['timestamp'] );
     240    }
     241
    233242    // Add the activity.
    234     bp_activity_add(
     243    $activity_id = bp_activity_add(
    235244        array(
    236             'user_id'   => $user_id,
    237             'component' => $bp->members->id,
    238             'type'      => 'new_avatar',
     245            'user_id'       => $user_id,
     246            'component'     => $bp->members->id,
     247            'type'          => 'new_avatar',
     248            'recorded_time' => $recorded_time,
    239249        )
    240250    );
    241251}
    242 add_action( 'bp_members_avatar_uploaded', 'bp_members_new_avatar_activity' );
     252add_action( 'bp_members_avatar_uploaded', 'bp_members_new_avatar_activity', 10, 4 );
  • trunk/src/bp-members/screens/change-avatar.php

    r12557 r13177  
    6262        );
    6363
    64         if ( ! bp_core_avatar_handle_crop( $args ) ) {
     64        // Handle crop.
     65        $cropped_avatar = bp_core_avatar_handle_crop( $r, 'array' );
     66
     67        if ( ! $cropped_avatar ) {
    6568            bp_core_add_message( __( 'There was a problem cropping your profile photo.', 'buddypress' ), 'error' );
    6669        } else {
     
    7376             *
    7477             * @since 6.0.0
     78             * @since 10.0.0 Adds a new param: an array containing the full, thumb avatar and the timestamp.
    7579             *
    76              * @param string $item_id Inform about the user id the avatar was set for.
    77              * @param string $value   Inform about the way the avatar was set ('crop').
     80             * @param string $item_id        Inform about the user id the avatar was set for.
     81             * @param string $type           Inform about the way the avatar was set ('camera').
     82             * @param array  $args           Array of parameters passed to the crop handler.
     83             * @param array  $cropped_avatar Array containing the full, thumb avatar and the timestamp.
    7884             */
    79             do_action( 'bp_members_avatar_uploaded', (int) $args['item_id'], 'crop' );
     85            do_action( 'bp_members_avatar_uploaded', (int) $args['item_id'], 'crop', $args, $cropped_avatar );
    8086
    8187            bp_core_add_message( __( 'Your new profile photo was uploaded successfully.', 'buddypress' ) );
Note: See TracChangeset for help on using the changeset viewer.