bp_activity_create_summary

Create a rich summary of an activity item for the activity stream.

Description

(string) bp_activity_create_summary( (string) $content, (array) $activity ); 

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.

Returns (string)

$summary

Parameters (2)

0. $content (string)
The content of the activity item.
1. $activity (array)
The data passed to bp_activity_add() or the values from an Activity obj.

Usage

  1. if ( !function_exists( 'bp_activity_create_summary' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-activity/bp-activity-functions.php'; 
  3.  
  4. // The content of the activity item. 
  5. $content = ''; 
  6.  
  7. // The data passed to bp_activity_add() or the values 
  8. // from an Activity obj. 
  9. $activity = array(); 
  10.  
  11. // NOTICE! Understand what this does before running. 
  12. $result = bp_activity_create_summary($content, $activity); 
  13.  

Defined (1)

The function is defined in the following location(s).

/bp-activity/bp-activity-functions.php  
  1. function bp_activity_create_summary( $content, $activity ) { 
  2. $args = array( 
  3. 'width' => isset( $GLOBALS['content_width'] ) ? (int) $GLOBALS['content_width'] : 'medium',  
  4. ); 
  5.  
  6. // Get the WP_Post object if this activity type is a blog post. 
  7. if ( $activity['type'] === 'new_blog_post' ) { 
  8. $content = get_post( $activity['secondary_item_id'] ); 
  9.  
  10. /** 
  11. * Filter the class name of the media extractor when creating an Activity summary. 
  12. * Use this filter to change the media extractor used to extract media info for the activity item. 
  13. * @since 2.3.0 
  14. * @param string $extractor Class name. 
  15. * @param string $content The content of the activity item. 
  16. * @param array $activity The data passed to bp_activity_add() or the values from an Activity obj. 
  17. */ 
  18. $extractor = new $extractor; 
  19.  
  20. /** 
  21. * Filter the arguments passed to the media extractor when creating an Activity summary. 
  22. * @since 2.3.0 
  23. * @param array $args Array of bespoke data for the media extractor. 
  24. * @param string $content The content of the activity item. 
  25. * @param array $activity The data passed to bp_activity_add() or the values from an Activity obj. 
  26. * @param BP_Media_Extractor $extractor The media extractor object. 
  27. */ 
  28. $args = apply_filters( 'bp_activity_create_summary_extractor_args', $args, $content, $activity, $extractor ); 
  29.  
  30.  
  31. // Extract media information from the $content. 
  32. $media = $extractor->extract( $content, BP_Media_Extractor::ALL, $args ); 
  33.  
  34. // If we converted $content to an object earlier, flip it back to a string. 
  35. if ( is_a( $content, 'WP_Post' ) ) { 
  36. $content = $content->post_content; 
  37.  
  38. $para_count = substr_count( strtolower( wpautop( $content ) ), '<p>' ); 
  39. $has_audio = ! empty( $media['has']['audio'] ) && $media['has']['audio']; 
  40. $has_videos = ! empty( $media['has']['videos'] ) && $media['has']['videos']; 
  41. $has_feat_image = ! empty( $media['has']['featured_images'] ) && $media['has']['featured_images']; 
  42. $has_galleries = ! empty( $media['has']['galleries'] ) && $media['has']['galleries']; 
  43. $has_images = ! empty( $media['has']['images'] ) && $media['has']['images']; 
  44. $has_embeds = false; 
  45.  
  46. // Embeds must be subtracted from the paragraph count. 
  47. if ( ! empty( $media['has']['embeds'] ) ) { 
  48. $has_embeds = $media['has']['embeds'] > 0; 
  49. $para_count -= count( $media['has']['embeds'] ); 
  50.  
  51. $extracted_media = array(); 
  52. $use_media_type = ''; 
  53. $image_source = ''; 
  54.  
  55. // If it's a short article and there's an embed/audio/video, use it. 
  56. if ( $para_count <= 3 ) { 
  57. if ( $has_embeds ) { 
  58. $use_media_type = 'embeds'; 
  59. } elseif ( $has_audio ) { 
  60. $use_media_type = 'audio'; 
  61. } elseif ( $has_videos ) { 
  62. $use_media_type = 'videos'; 
  63.  
  64. // If not, or in any other situation, try to use an image. 
  65. if ( ! $use_media_type && $has_images ) { 
  66. $use_media_type = 'images'; 
  67. $image_source = 'html'; 
  68.  
  69. // Featured Image > Galleries > inline <img>. 
  70. if ( $has_feat_image ) { 
  71. $image_source = 'featured_images'; 
  72.  
  73. } elseif ( $has_galleries ) { 
  74. $image_source = 'galleries'; 
  75.  
  76. // Extract an item from the $media results. 
  77. if ( $use_media_type ) { 
  78. if ( $use_media_type === 'images' ) { 
  79. $extracted_media = wp_list_filter( $media[ $use_media_type ], array( 'source' => $image_source ) ); 
  80. $extracted_media = array_shift( $extracted_media ); 
  81. } else { 
  82. $extracted_media = array_shift( $media[ $use_media_type ] ); 
  83.  
  84. /** 
  85. * Filter the results of the media extractor when creating an Activity summary. 
  86. * @since 2.3.0 
  87. * @param array $extracted_media Extracted media item. See {@link BP_Media_Extractor::extract()} for format. 
  88. * @param string $content Content of the activity item. 
  89. * @param array $activity The data passed to bp_activity_add() or the values from an Activity obj. 
  90. * @param array $media All results from the media extraction. 
  91. * See {@link BP_Media_Extractor::extract()} for format. 
  92. * @param string $use_media_type The kind of media item that was preferentially extracted. 
  93. * @param string $image_source If $use_media_type was "images", the preferential source of the image. 
  94. * Otherwise empty. 
  95. */ 
  96. $extracted_media = apply_filters
  97. 'bp_activity_create_summary_extractor_result',  
  98. $extracted_media,  
  99. $content,  
  100. $activity,  
  101. $media,  
  102. $use_media_type,  
  103. $image_source 
  104. ); 
  105.  
  106. // Generate a text excerpt for this activity item (and remove any oEmbeds URLs). 
  107. $summary = bp_create_excerpt( html_entity_decode( $content ), 225, array( 
  108. 'html' => false,  
  109. 'filter_shortcodes' => true,  
  110. 'strip_tags' => true,  
  111. 'remove_links' => true 
  112. ) ); 
  113.  
  114. if ( $use_media_type === 'embeds' ) { 
  115. $summary .= PHP_EOL . PHP_EOL . $extracted_media['url']; 
  116. } elseif ( $use_media_type === 'images' ) { 
  117. $summary .= sprintf( ' <img src="%s">', esc_url( $extracted_media['url'] ) ); 
  118. } elseif ( in_array( $use_media_type, array( 'audio', 'videos' ), true ) ) { 
  119. $summary .= PHP_EOL . PHP_EOL . $extracted_media['original']; // Full shortcode. 
  120.  
  121. /** 
  122. * Filters the newly-generated summary for the activity item. 
  123. * @since 2.3.0 
  124. * @param string $summary Activity summary HTML. 
  125. * @param string $content Content of the activity item. 
  126. * @param array $activity The data passed to bp_activity_add() or the values from an Activity obj. 
  127. * @param array $extracted_media Media item extracted. See {@link BP_Media_Extractor::extract()} for format. 
  128. */ 
  129. return apply_filters( 'bp_activity_create_summary', $summary, $content, $activity, $extracted_media );