Skip to:
Content

BuddyPress.org

Changeset 13176


Ignore:
Timestamp:
12/11/2021 12:31:50 PM (2 years ago)
Author:
imath
Message:

Introduce 2 functions to get an avatar version and the avatars history

  • bp_avatar_get_version() can be used to get a version of an avatar according to its timestamp. Since [13175] the BP_Attachment_Avatar class is using a timestamp inside the avatar file name instead of a generated unique string (uniqid()), this change allows us to request avatars according to the timestamp included into their names.
  • bp_avatar_get_avatars_history() can be used to get the file objects list of previously uploaded avatars.

Props vapvarun, oztaser

See #8581

File:
1 edited

Legend:

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

    r13152 r13176  
    21232123    }
    21242124}
     2125
     2126/**
     2127 * Get a specific version of an avatar from its history.
     2128 *
     2129 * @since 10.0.0
     2130 *
     2131 * @param int        $item_id   The item ID we need the avatar version for.
     2132 * @param string     $object    The object the item ID relates to.
     2133 * @param int|string $timestamp An integer Unix timestamp or a date string of the format 'Y-m-d h:i:s'.
     2134 * @param string     $type      The type of avatar we need. Possible values are `thumb` and `full`.
     2135 * @return array                A list of matching results, an empty array if no avatars were found.
     2136 */
     2137function bp_avatar_get_version( $item_id = 0, $object = 'user', $timestamp = '', $type = 'full' ) {
     2138    if ( ! $item_id || ! $timestamp ) {
     2139        return array();
     2140    }
     2141
     2142    if ( ! is_numeric( $timestamp ) ) {
     2143        $timestamp = strtotime( $timestamp );
     2144    }
     2145
     2146    $avatar_id = $timestamp . '-bpfull';
     2147    if ( 'full' !== $type ) {
     2148        $avatar_id = $timestamp . '-bpthumb';
     2149    }
     2150
     2151    $avatar_dir = 'avatars';
     2152    if ( 'user' !== $object ) {
     2153        $avatar_dir = sanitize_key( $object ) . '-avatars';
     2154    }
     2155
     2156    // The object avatar directory we are looking into to get the avatar url.
     2157    $object_avatar_dir = trailingslashit( bp_core_avatar_upload_path() ) . $avatar_dir . '/' . $item_id;
     2158
     2159    return bp_attachments_list_directory_files_recursively( $object_avatar_dir, $avatar_id );
     2160}
     2161
     2162/**
     2163 * Get the list of previous avatars in history
     2164 *
     2165 * @since 10.0.0
     2166 *
     2167 * @param int    $item_id The item ID we need the avatar version for.
     2168 * @param string $object  The object the item ID relates to.
     2169 * @param string $type    Get the `full`, `thumb` or `both` versions.
     2170 * @return array          The list of previous uploaded avatars.
     2171 */
     2172function bp_avatar_get_avatars_history( $item_id = 0, $object = 'user', $type = 'full' ) {
     2173    if ( ! $item_id ) {
     2174        return array();
     2175    }
     2176
     2177    $avatar_dir = 'avatars';
     2178    if ( 'user' !== $object ) {
     2179        $avatar_dir = sanitize_key( $object ) . '-avatars';
     2180    }
     2181
     2182    // The user avatar directory we are looking into to get the avatar url.
     2183    $history_dir      = trailingslashit( bp_core_avatar_upload_path() ) . $avatar_dir . '/' . $item_id . '/history';
     2184    $historic_avatars = bp_attachments_list_directory_files( $history_dir );
     2185
     2186    if ( ! $historic_avatars ) {
     2187        return array();
     2188    }
     2189
     2190    $avatars     = array();
     2191    $history_url = trailingslashit( bp_core_avatar_url() ) .  $avatar_dir . '/' . $item_id . '/history';
     2192
     2193    foreach ( $historic_avatars as $historic_avatar ) {
     2194        $prefix = str_replace( array( '-bpfull', '-bpthumb' ), '', $historic_avatar->id );
     2195        $gmdate = gmdate( 'Y-m-d H:i:s', $historic_avatar->last_modified );
     2196        $date   = strtotime( get_date_from_gmt( $gmdate ) );
     2197
     2198        $avatars[ $historic_avatar->id ] = (object) array(
     2199            'id'   => $historic_avatar->id,
     2200            'name' => $historic_avatar->name,
     2201            'date' => sprintf(
     2202                '%1$s (%2$s)',
     2203                date_i18n( get_option( 'date_format' ), $date ),
     2204                date_i18n( get_option( 'time_format' ), $date )
     2205            ),
     2206            'type' => str_replace( $prefix . '-bp', '', $historic_avatar->id ),
     2207            'url'  => $history_url . '/' . $historic_avatar->name,
     2208        );
     2209    }
     2210
     2211    if ( 'both' === $type ) {
     2212        return $avatars;
     2213    }
     2214
     2215    return wp_filter_object_list( $avatars, array( 'type' => $type ) );
     2216}
Note: See TracChangeset for help on using the changeset viewer.