Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
11/20/2023 05:35:28 AM (3 years ago)
Author:
imath
Message:

Only add a link to view member's latest update if it's been truncated

  • Stop using extract() in bp_get_member_latest_update()
  • Add a new $update parameter to the 'bp_get_member_latest_update' filter. This array contains all needed data to reset the member's latest update.

Fixes #9026
Closes https://github.com/buddypress/buddypress/pull/193

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-members/bp-members-template.php

    r13503 r13647  
    11111111                global $members_template;
    11121112
    1113                 $defaults = array(
    1114                         'length'    => 225,
    1115                         'view_link' => true,
    1116                 );
     1113                if ( ! bp_is_active( 'activity' ) ) {
     1114                        return '';
     1115                }
    11171116
    11181117                $r = bp_parse_args(
    11191118                        $args,
    1120                         $defaults
     1119                        array(
     1120                                'length'    => 225,
     1121                                'view_link' => true,
     1122                        ),
     1123                        'member_latest_update'
    11211124                );
    11221125
    1123                 extract( $r );
    1124 
    1125                 if ( !bp_is_active( 'activity' ) || empty( $members_template->member->latest_update ) || !$update = maybe_unserialize( $members_template->member->latest_update ) )
    1126                         return false;
     1126                $length    = (int) $r['length'];
     1127                $view_link = (bool) $r['view_link'];
     1128
     1129                // Init default update.
     1130                $update_content = '';
     1131                $update         = array(
     1132                        'id'        => 0,
     1133                        'content'   => '',
     1134                        'excerpt'   => '',
     1135                        'permalink' => '',
     1136                );
     1137
     1138                if ( ! empty( $members_template->member->latest_update ) ) {
     1139                        $update = maybe_unserialize( $members_template->member->latest_update );
     1140                }
     1141
     1142                if ( ! empty( $update['content'] ) ) {
     1143                        $excerpt           = trim( strip_tags( bp_create_excerpt( $update['content'], $length ) ) );
     1144                        $update['content'] = trim( strip_tags( $update['content'] ) );
     1145                        $update['excerpt'] = $excerpt;
     1146                } else {
     1147                        $excerpt = '';
     1148                }
     1149
     1150                if ( isset( $update['id'] ) ) {
     1151                        $activity_id = (int) $update['id'];
     1152                        $update['permalink'] = bp_activity_get_permalink( $activity_id );
     1153                }
    11271154
    11281155                /**
     
    11321159                 * @since 2.6.0 Added the `$r` parameter.
    11331160                 *
    1134                  * @param string $value Excerpt of the latest update for current member in the loop.
    1135                  * @param array  $r     Array of parsed arguments.
    1136                  */
    1137                 $update_content = apply_filters( 'bp_get_activity_latest_update_excerpt', trim( strip_tags( bp_create_excerpt( $update['content'], $length ) ) ), $r );
    1138 
    1139                 /* translators: %s: the member latest activity update */
    1140                 $update_content = sprintf( _x( '- "%s"', 'member latest update in member directory', 'buddypress' ), $update_content );
    1141 
    1142                 // If $view_link is true and the text returned by bp_create_excerpt() is different from the original text (ie it's
    1143                 // been truncated), add the "View" link.
    1144                 if ( $view_link && ( $update_content != $update['content'] ) ) {
    1145                         $view = __( 'View', 'buddypress' );
    1146 
    1147                         $update_content .= '<span class="activity-read-more"><a href="' . bp_activity_get_permalink( $update['id'] ) . '" rel="nofollow">' . $view . '</a></span>';
     1161                 * @param string $excerpt Excerpt of the latest update for current member in the loop.
     1162                 * @param array  $r       Array of parsed arguments.
     1163                 */
     1164                $excerpt = apply_filters( 'bp_get_activity_latest_update_excerpt', $excerpt, $r );
     1165
     1166                // If we have an excerpt, set its output and eventually add a link to view the full activity.
     1167                if ( $excerpt ) {
     1168                        /* translators: %s: the member latest activity update */
     1169                        $update_content = sprintf( _x( '- &quot;%s&quot;', 'member latest update in member directory', 'buddypress' ), $excerpt );
     1170
     1171                        /*
     1172                        * If `$view_link` is true and the text returned by `bp_create_excerpt()` is different
     1173                        * from the original text (ie it's been truncated), add the "View" link.
     1174                        */
     1175                        if ( $view_link && $update['permalink'] && ( strlen( $excerpt ) < strlen( $update['content'] ) ) ) {
     1176                                $update_content      = sprintf(
     1177                                        '%1$s<span class="activity-read-more"><a href="%2$s" rel="nofollow">%3$s</a></span>',
     1178                                        $update_content . "\n",
     1179                                        esc_url( $update['permalink'] ),
     1180                                        esc_html__( 'View', 'buddypress' )
     1181                                );
     1182                        }
    11481183                }
    11491184
     
    11531188                 * @since 1.2.0
    11541189                 * @since 2.6.0 Added the `$r` parameter.
     1190                 * @since 12.0.0 Added the `$update` parameter.
    11551191                 *
    11561192                 * @param string $update_content Formatted latest update for current member.
    11571193                 * @param array  $r              Array of parsed arguments.
    1158                  */
    1159                 return apply_filters( 'bp_get_member_latest_update', $update_content, $r );
     1194                 * @param array  $update         Array of the latest activity data.
     1195                 */
     1196                return apply_filters( 'bp_get_member_latest_update', $update_content, $r, $update );
    11601197        }
    11611198
Note: See TracChangeset for help on using the changeset viewer.