Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
07/14/2021 02:42:18 PM (5 years ago)
Author:
imath
Message:

Merge the BP Blocks plugin's 'bp/latest-activities' Block

  • Adapt Grunt sass tasks.
  • Add the Block JavaScript source files into src/js/bp-activity/js/blocks.
  • Add the Block Scss source file into src/bp-activity/sass.
  • Generate the development files to ease testing.
  • Make sure the 'bp/embed-activity' block is only registered if embeds are active for the Activity component.

Fixes #8519

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-activity/bp-activity-blocks.php

    r12996 r13001  
    1212        exit;
    1313}
     14
     15/**
     16 * Callback function to render the Latest Activities Block.
     17 *
     18 * @since 9.0.0
     19 *
     20 * @param array $attributes The block attributes.
     21 * @return string           HTML output.
     22 */
     23function bp_activity_render_latest_activities_block( $attributes = array() ) {
     24        $block_args = wp_parse_args(
     25                $attributes,
     26                array(
     27                        'title'         => __( 'Latest updates', 'buddypress' ),
     28                        'maxActivities' => 5,
     29                        'type'          => array( 'activity_update' ),
     30                        'postId'        => 0,
     31                )
     32        );
     33
     34        $max_activities = (int) $block_args['maxActivities'];
     35
     36        // Should we get a specific member's activities?
     37        $member_id = 0;
     38        if ( $block_args['postId'] ) {
     39                $member_id = (int) get_post_field( 'post_author', $block_args['postId'] );
     40        } else {
     41                $member_id = bp_displayed_user_id();
     42        }
     43
     44        // Set the widget's wrapper attributes.
     45        $types              = (array) $block_args['type'];
     46        $classnames         = array_map( 'sanitize_html_class', array_merge( $types, array( 'bp-latest-activities', 'buddypress', 'widget' ) ) );
     47        $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classnames ) ) );
     48
     49        // Set the Block's title.
     50        $widget_content = sprintf( '<h2 class="widget-title">%s</h2>', esc_html( $block_args['title'] ) );
     51
     52        // Avoid conflicts with other activity loops.
     53        $reset_activities_template = null;
     54        if ( ! empty( $GLOBALS['activities_template'] ) ) {
     55                $reset_activities_template = $GLOBALS['activities_template'];
     56        }
     57
     58        $widget_args = array(
     59                'max'          => $max_activities,
     60                'scope'        => 'all',
     61                'user_id'      => $member_id,
     62                'object'       => false,
     63                'action'       => implode( ',', $types ),
     64                'primary_id'   => 0,
     65                'secondary_id' => 0,
     66        );
     67
     68        // Build the activity loop.
     69        if ( 'nouveau' === bp_get_theme_compat_id() ) {
     70                $bp_nouveau = bp_nouveau();
     71
     72                // Globalize the activity widget arguments.
     73                $bp_nouveau->activity->widget_args = $widget_args;
     74
     75                ob_start();
     76                bp_get_template_part( 'activity/widget' );
     77                $widget_content .= ob_get_clean();
     78
     79                // Reset the global.
     80                $bp_nouveau->activity->widget_args = array();
     81        } else {
     82                $activity_loop = sprintf( '<div class="widget-error"><p>%s</p></div>', esc_html__( 'Sorry, there was no activity found. Please try a different filter.', 'buddypress' ) );
     83
     84                if ( bp_has_activities( $widget_args ) ) {
     85                        $activity_loop = '';
     86
     87                        while ( bp_activities() ) {
     88                                bp_the_activity();
     89
     90                                $activity_footer  = '';
     91                                $activity_classes = 'activity-item';
     92                                if ( bp_activity_has_content() ) {
     93                                        $activity_content = bp_get_activity_content_body();
     94                                        $activity_footer  = sprintf(
     95                                                '<footer>
     96                                                        <cite>
     97                                                                <a href="%1$s" class="bp-tooltip" data-bp-tooltip="%2$s">
     98                                                                        %3$s
     99                                                                </a>
     100                                                        </cite>
     101                                                        %4$s
     102                                                </footer>',
     103                                                bp_get_activity_user_link(),
     104                                                bp_get_activity_member_display_name(),
     105                                                bp_get_activity_avatar(
     106                                                        array(
     107                                                                'type'   => 'thumb',
     108                                                                'width'  => '40',
     109                                                                'height' => '40',
     110                                                        )
     111                                                ),
     112                                                bp_insert_activity_meta()
     113                                        );
     114                                } else {
     115                                        $activity_classes .= ' mini';
     116                                        $activity_content  = bp_get_activity_action();
     117                                }
     118
     119                                $activity_loop .= sprintf(
     120                                        '<blockquote>
     121                                                <div class="%1$s">%2$s</div>
     122                                                %3$s
     123                                        </blockquote>',
     124                                        $activity_classes,
     125                                        $activity_content,
     126                                        $activity_footer
     127                                );
     128                        }
     129                }
     130
     131                $widget_content .= sprintf(
     132                        '<div class="activity-list item-list">
     133                                %1$s
     134                        </div>',
     135                        $activity_loop
     136                );
     137        }
     138
     139        // Adds a container to make sure the block is styled even when used into the Columns parent block.
     140        $widget_content = sprintf( '<div class="bp-latest-activities-block">%s</div>', "\n" . $widget_content . "\n" );
     141
     142        // Reset the global template loop.
     143        $GLOBALS['activities_template'] = $reset_activities_template;
     144
     145        // Only add a block wrapper if not loaded into a Widgets sidebar.
     146        if ( ! did_action( 'dynamic_sidebar_before' ) ) {
     147                return sprintf(
     148                        '<div %1$s>%2$s</div>',
     149                        $wrapper_attributes,
     150                        $widget_content
     151                );
     152        }
     153
     154        return $widget_content;
     155}
Note: See TracChangeset for help on using the changeset viewer.