Skip to:
Content

BuddyPress.org

Changeset 9621


Ignore:
Timestamp:
03/16/2015 06:59:09 PM (10 years ago)
Author:
djpaul
Message:

Activity: improve blog post excerpts by showing rich media from the post.

Prior to this change, when we created excerpts for new/updated blog post activity items, we'd try to use any single image referenced in that blog post, and use it to decorate and bring interactivity to the activity stream item. This was inconsistently applied -- it was dependant on the length of the original post -- and ignored other possible media sources, such as oEmbeds and video/audio shortcodes.

Now, we use the media extractor (r9619) to extract comprehensive media information from a new/updated blog post, and use the information to build a much richer excerpt for blogs posts for activity items.

See #6177

Location:
trunk/src
Files:
3 edited

Legend:

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

    r9475 r9621  
    387387 * Truncate long activity entries when viewed in activity streams.
    388388 *
     389 * This method can only be used inside the Activity loop.
     390 *
    389391 * @since BuddyPress (1.5.0)
    390392 *
     
    403405    global $activities_template;
    404406
     407    /**
     408     * Provides a filter that lets you choose whether to skip this filter on a per-activity basis.
     409     *
     410     * @param bool $maybe_truncate_text If true, text should be checked to see if it needs truncating.
     411     * @since BuddyPress (2.3.0)
     412     */
     413    $maybe_truncate_text = apply_filters( 'bp_activity_maybe_truncate_entry',
     414        ! in_array( $activities_template->activity->type, array( 'new_blog_post', ), true )
     415    );
     416
    405417    // The full text of the activity update should always show on the single activity screen
    406     if ( bp_is_single_activity() )
     418    if ( ! $maybe_truncate_text || bp_is_single_activity() ) {
    407419        return $text;
     420    }
    408421
    409422    /**
  • trunk/src/bp-activity/bp-activity-functions.php

    r9471 r9621  
    18701870    );
    18711871
    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 
    18771872    if ( ! empty( $activity_args['content'] ) ) {
    18781873        // Create the excerpt.
    1879         $activity_excerpt = bp_create_excerpt( $activity_args['content'] );
     1874        $activity_summary = bp_activity_create_summary( $activity_args['content'], $activity_args );
    18801875
    18811876        // Backward compatibility filter for blog posts.
    18821877        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 );
    18841879        } else {
    1885             $activity_args['content'] = $activity_excerpt;
     1880            $activity_args['content'] = $activity_summary;
    18861881        }
    18871882    }
     
    19641959
    19651960    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 );
    19711962
    19721963        // Backward compatibility filter for the blogs component.
    19731964        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 );
    19751966        } else {
    1976             $activity->content = $activity_excerpt;
     1967            $activity->content = $activity_summary;
    19771968        }
    19781969    }
     
    25862577 * and removes the rest of the images from the string.
    25872578 *
     2579 * As of BuddyPress 2.3, this function is no longer in use.
     2580 *
    25882581 * @since BuddyPress (1.2.0)
    25892582 *
     
    26542647     */
    26552648    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 */
     2663function 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 );
    26562804}
    26572805
  • trunk/src/bp-blogs/bp-blogs-activity.php

    r9471 r9621  
    353353    $r = wp_parse_args( $args, $defaults );
    354354
    355     // Remove large images and replace them with just one image thumbnail
    356     if ( ! empty( $r['content'] ) ) {
    357         $r['content'] = bp_activity_thumbnail_content_images( $r['content'], $r['primary_link'], $r );
    358     }
    359 
    360355    if ( ! empty( $r['action'] ) ) {
    361356
     
    377372         * @since BuddyPress (1.2.0)
    378373         *
    379          * @param string $value Generated excerpt from content for the activity stream.
     374         * @param string $value Generated summary from content for the activity stream.
    380375         * @param string $value Content for the activity stream.
    381376         * @param array  $r     Array of arguments used for the activity stream item.
    382377         */
    383         $r['content'] = apply_filters( 'bp_blogs_record_activity_content', bp_create_excerpt( $r['content'] ), $r['content'], $r );
     378        $r['content'] = apply_filters( 'bp_blogs_record_activity_content', bp_activity_create_summary( $r['content'], $r ), $r['content'], $r );
    384379    }
    385380
Note: See TracChangeset for help on using the changeset viewer.