Changeset 9621 for trunk/src/bp-activity/bp-activity-functions.php
- Timestamp:
- 03/16/2015 06:59:09 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-activity/bp-activity-functions.php
r9471 r9621 1870 1870 ); 1871 1871 1872 // Remove large images and replace them with just one image thumbnail.1873 if ( ! empty( $activity_args['content'] ) ) {1874 $activity_args['content'] = bp_activity_thumbnail_content_images( $activity_args['content'], $activity_args['primary_link'], $activity_args );1875 }1876 1877 1872 if ( ! empty( $activity_args['content'] ) ) { 1878 1873 // Create the excerpt. 1879 $activity_ excerpt = bp_create_excerpt( $activity_args['content']);1874 $activity_summary = bp_activity_create_summary( $activity_args['content'], $activity_args ); 1880 1875 1881 1876 // Backward compatibility filter for blog posts. 1882 1877 if ( 'blogs' == $activity_post_object->component_id ) { 1883 $activity_args['content'] = apply_filters( 'bp_blogs_record_activity_content', $activity_ excerpt, $activity_args['content'], $activity_args, $post->post_type );1878 $activity_args['content'] = apply_filters( 'bp_blogs_record_activity_content', $activity_summary, $activity_args['content'], $activity_args, $post->post_type ); 1884 1879 } else { 1885 $activity_args['content'] = $activity_ excerpt;1880 $activity_args['content'] = $activity_summary; 1886 1881 } 1887 1882 } … … 1964 1959 1965 1960 if ( ! empty( $post->post_content ) ) { 1966 // Make sure to update the thumbnail image. 1967 $post_content = bp_activity_thumbnail_content_images( $post->post_content, $activity->primary_link, (array) $activity ); 1968 1969 // Generate an excerpt. 1970 $activity_excerpt = bp_create_excerpt( $post_content ); 1961 $activity_summary = bp_activity_create_summary( $post->post_content, (array) $activity ); 1971 1962 1972 1963 // Backward compatibility filter for the blogs component. 1973 1964 if ( 'blogs' == $activity_post_object->component_id ) { 1974 $activity->content = apply_filters( 'bp_blogs_record_activity_content', $activity_ excerpt, $post_content, (array) $activity, $post->post_type );1965 $activity->content = apply_filters( 'bp_blogs_record_activity_content', $activity_summary, $post->post_content, (array) $activity, $post->post_type ); 1975 1966 } else { 1976 $activity->content = $activity_ excerpt;1967 $activity->content = $activity_summary; 1977 1968 } 1978 1969 } … … 2586 2577 * and removes the rest of the images from the string. 2587 2578 * 2579 * As of BuddyPress 2.3, this function is no longer in use. 2580 * 2588 2581 * @since BuddyPress (1.2.0) 2589 2582 * … … 2654 2647 */ 2655 2648 return apply_filters( 'bp_activity_thumbnail_content_images', $content, $matches, $args ); 2649 } 2650 2651 /** 2652 * Create a rich summary of an activity item for the activity stream. 2653 * 2654 * More than just a simple excerpt, the summary could contain oEmbeds and other types of media. 2655 * Currently, it's only used for blog post items, but it will probably be used for all types of 2656 * activity in the future. 2657 * 2658 * @param string $content The content of the activity item. 2659 * @param array $activity_args The data passed to bp_activity_add() or the values from an Activity obj. 2660 * @return string 2661 * @since BuddyPress (2.3.0) 2662 */ 2663 function bp_activity_create_summary( $content, $activity ) { 2664 $args = array( 2665 'width' => isset( $GLOBALS['content_width'] ) ? (int) $GLOBALS['content_width'] : 'medium', 2666 ); 2667 2668 // Get the WP_Post object if this activity type is a blog post. 2669 if ( $activity['type'] === 'new_blog_post' ) { 2670 $content = get_post( $activity['secondary_item_id'] ); 2671 } 2672 2673 2674 /** 2675 * Filter the class name of the media extractor when creating an Activity summary. 2676 * 2677 * Use this filter to change the media extractor used to extract media info for the activity item. 2678 * 2679 * @param string $extractor Class name. 2680 * @param string $content The content of the activity item. 2681 * @param array $activity The data passed to bp_activity_add() or the values from an Activity obj. 2682 * @since BuddyPress (2.3.0) 2683 */ 2684 $extractor = apply_filters( 'bp_activity_create_summary_extractor_class', 'BP_Media_Extractor', $content, $activity ); 2685 $extractor = new $extractor; 2686 2687 /** 2688 * Filter the arguments passed to the media extractor when creating an Activity summary. 2689 * 2690 * @param array $args Array of bespoke data for the media extractor. 2691 * @param string $content The content of the activity item. 2692 * @param array $activity The data passed to bp_activity_add() or the values from an Activity obj. 2693 * @param BP_Media_Extractor $extractor The media extractor object. 2694 * @since BuddyPress (2.3.0) 2695 */ 2696 $args = apply_filters( 'bp_activity_create_summary_extractor_args', $args, $content, $activity, $extractor ); 2697 2698 2699 // Extract media information from the $content. 2700 $media = $extractor->extract( $content, BP_Media_Extractor::ALL, $args ); 2701 2702 // If we converted $content to an object earlier, flip it back to a string. 2703 if ( is_a( $content, 'WP_Post' ) ) { 2704 $content = $content->post_content; 2705 } 2706 2707 $para_count = substr_count( strtolower( wpautop( $content ) ), '<p>' ); 2708 $has_audio = ! empty( $media['has']['audio'] ) && $media['has']['audio']; 2709 $has_videos = ! empty( $media['has']['videos'] ) && $media['has']['videos']; 2710 $has_feat_image = ! empty( $media['has']['featured_images'] ) && $media['has']['featured_images']; 2711 $has_galleries = ! empty( $media['has']['galleries'] ) && $media['has']['galleries']; 2712 $has_images = ! empty( $media['has']['images'] ) && $media['has']['images']; 2713 $has_embeds = false; 2714 2715 // Embeds must be subtracted from the paragraph count. 2716 if ( ! empty( $media['has']['embeds'] ) ) { 2717 $has_embeds = $media['has']['embeds'] > 0; 2718 $para_count -= count( $media['has']['embeds'] ); 2719 } 2720 2721 $extracted_media = array(); 2722 $use_media_type = ''; 2723 $image_source = ''; 2724 2725 // If it's a short article and there's an embed/audio/video, use it. 2726 if ( $para_count <= 3 ) { 2727 if ( $has_embeds ) { 2728 $use_media_type = 'embeds'; 2729 } elseif ( $has_audio ) { 2730 $use_media_type = 'audio'; 2731 } elseif ( $has_videos ) { 2732 $use_media_type = 'videos'; 2733 } 2734 } 2735 2736 // If not, or in any other situation, try to use an image. 2737 if ( ! $use_media_type && $has_images ) { 2738 $use_media_type = 'images'; 2739 $image_source = 'html'; 2740 2741 // Featured Image > Galleries > inline <img>. 2742 if ( $has_feat_image ) { 2743 $image_source = 'featured_images'; 2744 2745 } elseif ( $has_galleries ) { 2746 $image_source = 'galleries'; 2747 } 2748 } 2749 2750 // Extract an item from the $media results. 2751 if ( $use_media_type ) { 2752 if ( $use_media_type === 'images' ) { 2753 $extracted_media = wp_list_filter( $media[ $use_media_type ], array( 'source' => $image_source ) ); 2754 $extracted_media = array_shift( $extracted_media ); 2755 } else { 2756 $extracted_media = array_shift( $media[ $use_media_type ] ); 2757 } 2758 2759 /** 2760 * Filter the results of the media extractor when creating an Activity summary. 2761 * 2762 * @param array $extracted_media Extracted media item. See {@link BP_Media_Extractor::extract()} for format. 2763 * @param string $content Content of the activity item. 2764 * @param array $activity The data passed to bp_activity_add() or the values from an Activity obj. 2765 * @param array $media All results from the media extraction. See {@link BP_Media_Extractor::extract()} for format. 2766 * @param string $use_media_type The kind of media item that was preferentially extracted. 2767 * @param string $image_source If $use_media_type was "images", the preferential source of the image. 2768 * Otherwise empty. 2769 * @since BuddyPress (2.3.0) 2770 */ 2771 $extracted_media = apply_filters( 2772 'bp_activity_create_summary_extractor_result', 2773 $extracted_media, 2774 $content, 2775 $activity, 2776 $media, 2777 $use_media_type, 2778 $image_source 2779 ); 2780 } 2781 2782 // Generate a text excerpt for this activity item (and remove any oEmbeds URLs). 2783 $summary = strip_shortcodes( html_entity_decode( strip_tags( $content ) ) ); 2784 $summary = bp_create_excerpt( preg_replace( '#^\s*(https?://[^\s"]+)\s*$#im', '', $summary ) ); 2785 2786 if ( $use_media_type === 'embeds' ) { 2787 $summary .= PHP_EOL . PHP_EOL . $extracted_media['url']; 2788 } elseif ( $use_media_type === 'images' ) { 2789 $summary .= sprintf( ' <img src="%s">', esc_url( $extracted_media['url'] ) ); 2790 } elseif ( in_array( $use_media_type, array( 'audio', 'videos' ), true ) ) { 2791 $summary .= PHP_EOL . PHP_EOL . $extracted_media['original']; // Full shortcode. 2792 } 2793 2794 /** 2795 * Filters the newly-generated summary for the activity item. 2796 * 2797 * @param string $summary Activity summary HTML. 2798 * @param string $content $content Content of the activity item. 2799 * @param array $activity The data passed to bp_activity_add() or the values from an Activity obj. 2800 * @param array $extracted_media Media item extracted. See {@link BP_Media_Extractor::extract()} for format. 2801 * @since BuddyPress (2.3.0) 2802 */ 2803 return apply_filters( 'bp_activity_create_summary', $summary, $content, $activity, $extracted_media ); 2656 2804 } 2657 2805
Note: See TracChangeset
for help on using the changeset viewer.