bp_activity_create_summary( string $content, array $activity )
Create a rich summary of an activity item for the activity feed.
Description
More than just a simple excerpt, the summary could contain oEmbeds and other types of media. Currently, it’s only used for blog post items, but it will probably be used for all types of activity in the future.
Parameters
- $content
-
(Required) The content of the activity item.
- $activity
-
(Required) The data passed to bp_activity_add() or the values from an Activity obj.
Return
(string) $summary
Source
File: bp-activity/bp-activity-functions.php
3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 | function bp_activity_create_summary( $content , $activity ) { $args = array ( 'width' => isset( $GLOBALS [ 'content_width' ] ) ? (int) $GLOBALS [ 'content_width' ] : 'medium' , ); // Get the WP_Post object if this activity type is a blog post. if ( $activity [ 'type' ] === 'new_blog_post' ) { $content = get_post( $activity [ 'secondary_item_id' ] ); } /** * Filter the class name of the media extractor when creating an Activity summary. * * Use this filter to change the media extractor used to extract media info for the activity item. * * @since BuddyPress 2.3.0 * * @param string $extractor Class name. * @param string $content The content of the activity item. * @param array $activity The data passed to bp_activity_add() or the values from an Activity obj. */ $extractor = apply_filters( 'bp_activity_create_summary_extractor_class' , 'BP_Media_Extractor' , $content , $activity ); $extractor = new $extractor ; /** * Filter the arguments passed to the media extractor when creating an Activity summary. * * @since BuddyPress 2.3.0 * * @param array $args Array of bespoke data for the media extractor. * @param string $content The content of the activity item. * @param array $activity The data passed to bp_activity_add() or the values from an Activity obj. * @param BP_Media_Extractor $extractor The media extractor object. */ $args = apply_filters( 'bp_activity_create_summary_extractor_args' , $args , $content , $activity , $extractor ); // Extract media information from the $content. $media = $extractor ->extract( $content , BP_Media_Extractor::ALL, $args ); // If we converted $content to an object earlier, flip it back to a string. if ( is_a ( $content , 'WP_Post' ) ) { // For the post and custom post type get the excerpt first. $excerpt = get_the_excerpt( $content ->ID ); // Get the excerpt first if found otherwise it will take the post content. $content = ( $excerpt )?: $content ->post_content; } $para_count = substr_count( strtolower ( wpautop( $content ) ), '<p>' ); $has_audio = ! empty ( $media [ 'has' ][ 'audio' ] ) && $media [ 'has' ][ 'audio' ]; $has_videos = ! empty ( $media [ 'has' ][ 'videos' ] ) && $media [ 'has' ][ 'videos' ]; $has_feat_image = ! empty ( $media [ 'has' ][ 'featured_images' ] ) && $media [ 'has' ][ 'featured_images' ]; $has_galleries = ! empty ( $media [ 'has' ][ 'galleries' ] ) && $media [ 'has' ][ 'galleries' ]; $has_images = ! empty ( $media [ 'has' ][ 'images' ] ) && $media [ 'has' ][ 'images' ]; $has_embeds = false; // Embeds must be subtracted from the paragraph count. if ( ! empty ( $media [ 'has' ][ 'embeds' ] ) ) { $has_embeds = $media [ 'has' ][ 'embeds' ] > 0; $para_count -= $media [ 'has' ][ 'embeds' ]; } $extracted_media = array (); $use_media_type = '' ; $image_source = '' ; // If it's a short article and there's an embed/audio/video, use it. if ( $para_count <= 3 ) { if ( $has_embeds ) { $use_media_type = 'embeds' ; } elseif ( $has_audio ) { $use_media_type = 'audio' ; } elseif ( $has_videos ) { $use_media_type = 'videos' ; } } // If not, or in any other situation, try to use an image. if ( ! $use_media_type && $has_images ) { $use_media_type = 'images' ; $image_source = 'html' ; // Featured Image > Galleries > inline <img>. if ( $has_feat_image ) { $image_source = 'featured_images' ; } elseif ( $has_galleries ) { $image_source = 'galleries' ; } } // Extract an item from the $media results. if ( $use_media_type ) { if ( $use_media_type === 'images' ) { $extracted_media = wp_list_filter( $media [ $use_media_type ], array ( 'source' => $image_source ) ); $extracted_media = array_shift ( $extracted_media ); } else { $extracted_media = array_shift ( $media [ $use_media_type ] ); } /** * Filter the results of the media extractor when creating an Activity summary. * * @since BuddyPress 2.3.0 * * @param array $extracted_media Extracted media item. See {@link BP_Media_Extractor::extract()} for format. * @param string $content Content of the activity item. * @param array $activity The data passed to bp_activity_add() or the values from an Activity obj. * @param array $media All results from the media extraction. * See {@link BP_Media_Extractor::extract()} for format. * @param string $use_media_type The kind of media item that was preferentially extracted. * @param string $image_source If $use_media_type was "images", the preferential source of the image. * Otherwise empty. */ $extracted_media = apply_filters( 'bp_activity_create_summary_extractor_result' , $extracted_media , $content , $activity , $media , $use_media_type , $image_source ); } // Generate a text excerpt for this activity item (and remove any oEmbeds URLs). $summary = bp_create_excerpt( html_entity_decode( $content ), 225, array ( 'html' => false, 'filter_shortcodes' => true, 'strip_tags' => true, 'remove_links' => true ) ); if ( $use_media_type === 'embeds' ) { $summary .= PHP_EOL . PHP_EOL . $extracted_media [ 'url' ]; } elseif ( $use_media_type === 'images' ) { $summary .= sprintf( ' <img src="%s">' , esc_url( $extracted_media [ 'url' ] ) ); } elseif ( in_array( $use_media_type , array ( 'audio' , 'videos' ), true ) ) { $summary .= PHP_EOL . PHP_EOL . $extracted_media [ 'original' ]; // Full shortcode. } /** * Filters the newly-generated summary for the activity item. * * @since BuddyPress 2.3.0 * * @param string $summary Activity summary HTML. * @param string $content Content of the activity item. * @param array $activity The data passed to bp_activity_add() or the values from an Activity obj. * @param array $extracted_media Media item extracted. See {@link BP_Media_Extractor::extract()} for format. */ return apply_filters( 'bp_activity_create_summary' , $summary , $content , $activity , $extracted_media ); } |
Changelog
Version | Description |
---|---|
BuddyPress 2.3.0 | Introduced. |
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.