Skip to:
Content

BuddyPress.org

Changeset 4709


Ignore:
Timestamp:
07/18/2011 10:43:22 PM (9 years ago)
Author:
djpaul
Message:

Add oembed support to activity stream items and forum posts. Fixes #2707, massive props r-a-y

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-activity/bp-activity-functions.php

    r4674 r4709  
    117117 * @param int $secondary_item_id In the case of at-mentions, this is the mentioner's id
    118118 * @param int $total_items The total number of notifications to format
    119  * @param str $format 'string' to get a BuddyBar-compatible notification, 'array' otherwise 
     119 * @param str $format 'string' to get a BuddyBar-compatible notification, 'array' otherwise
    120120 */
    121121function bp_activity_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
     
    139139        break;
    140140    }
    141                
     141
    142142    if ( 'string' == $format ) {
    143143        $return = apply_filters( $filter, '<a href="' . $at_mention_link . '" title="' . $at_mention_title . '">' . $text . '</a>', $at_mention_link, (int)$total_items, $activity_id, $poster_user_id );
     
    994994}
    995995
     996/** Embeds *******************************************************************/
     997
     998/**
     999 * Grabs the activity ID and attempts to retrieve the oEmbed cache (if it exists)
     1000 * during the activity loop.  If no cache and link is embeddable, cache it.
     1001 *
     1002 * @see BP_Embed
     1003 * @see bp_embed_activity_cache()
     1004 * @see bp_embed_activity_save_cache()
     1005 * @package BuddyPress Activity
     1006 * @since 1.3
     1007 */
     1008function bp_activity_embed() {
     1009    add_filter( 'embed_post_id',         'bp_get_activity_id'                  );
     1010    add_filter( 'bp_embed_get_cache',    'bp_embed_activity_cache',      10, 3 );
     1011    add_action( 'bp_embed_update_cache', 'bp_embed_activity_save_cache', 10, 3 );
     1012}
     1013add_action( 'activity_loop_start', 'bp_activity_embed' );
     1014
     1015/**
     1016 * Wrapper function for {@link bp_activity_get_meta()}.
     1017 * Used during {@link BP_Embed::parse_oembed()} via {@link bp_activity_embed()}.
     1018 *
     1019 * @package BuddyPress Activity
     1020 * @since 1.3
     1021 */
     1022function bp_embed_activity_cache( $cache, $id, $cachekey ) {
     1023    return bp_activity_get_meta( $id, $cachekey );
     1024}
     1025
     1026/**
     1027 * Wrapper function for {@link bp_activity_update_meta()}.
     1028 * Used during {@link BP_Embed::parse_oembed()} via {@link bp_activity_embed()}.
     1029 *
     1030 * @package BuddyPress Activity
     1031 * @since 1.3
     1032 */
     1033function bp_embed_activity_save_cache( $cache, $cachekey, $id ) {
     1034    bp_activity_update_meta( $id, $cachekey, $cache );
     1035}
    9961036?>
  • trunk/bp-core/bp-core-classes.php

    r4678 r4709  
    10831083}
    10841084
     1085/**
     1086 * BP_Embed
     1087 *
     1088 * Extends WP_Embed class for use with BuddyPress.
     1089 *
     1090 * @package BuddyPress Core
     1091 * @since 1.3
     1092 * @see WP_Embed
     1093 */
     1094class BP_Embed extends WP_Embed {
     1095    /**
     1096     * Constructor
     1097     *
     1098     * @global unknown $wp_embed
     1099     */
     1100    function __construct() {
     1101        global $wp_embed;
     1102
     1103        // Make sure we populate the WP_Embed handlers array.
     1104        // These are providers that use a regex callback on the URL in question.
     1105        // Do not confuse with oEmbed providers, which require an external ping.
     1106        // Used in WP_Embed::shortcode()
     1107        $this->handlers = $wp_embed->handlers;
     1108
     1109        if ( bp_use_embed_in_activity() ) {
     1110            add_filter( 'bp_get_activity_content_body', array( &$this, 'autoembed' ), 8 );
     1111            add_filter( 'bp_get_activity_content_body', array( &$this, 'run_shortcode' ), 7 );
     1112        }
     1113
     1114        if ( bp_use_embed_in_activity_replies() ) {
     1115            add_filter( 'bp_get_activity_content', array( &$this, 'autoembed' ), 8 );
     1116            add_filter( 'bp_get_activity_content', array( &$this, 'run_shortcode' ), 7 );
     1117        }
     1118
     1119        if ( bp_use_embed_in_forum_posts() ) {
     1120            add_filter( 'bp_get_the_topic_post_content', array( &$this, 'autoembed' ), 8 );
     1121            add_filter( 'bp_get_the_topic_post_content', array( &$this, 'run_shortcode' ), 7 );
     1122        }
     1123    }
     1124
     1125    /**
     1126     * The {@link do_shortcode()} callback function.
     1127     *
     1128     * Attempts to convert a URL into embed HTML. Starts by checking the URL against the regex of the registered embed handlers.
     1129     * Next, checks the URL against the regex of registered {@link WP_oEmbed} providers if oEmbed discovery is false.
     1130     * If none of the regex matches and it's enabled, then the URL will be passed to {@link BP_Embed::parse_oembed()} for oEmbed parsing.
     1131     *
     1132     * @uses wp_parse_args()
     1133     * @uses wp_embed_defaults()
     1134     * @uses current_user_can()
     1135     * @uses _wp_oembed_get_object()
     1136     * @uses WP_Embed::maybe_make_link()
     1137     *
     1138     * @param array $attr Shortcode attributes.
     1139     * @param string $url The URL attempting to be embeded.
     1140     * @return string The embed HTML on success, otherwise the original URL.
     1141     */
     1142    function shortcode( $attr, $url = '' ) {
     1143        if ( empty( $url ) )
     1144            return '';
     1145
     1146        $rawattr = $attr;
     1147        $attr = wp_parse_args( $attr, wp_embed_defaults() );
     1148
     1149        // kses converts & into &amp; and we need to undo this
     1150        // See http://core.trac.wordpress.org/ticket/11311
     1151        $url = str_replace( '&amp;', '&', $url );
     1152
     1153        // Look for known internal handlers
     1154        ksort( $this->handlers );
     1155        foreach ( $this->handlers as $priority => $handlers ) {
     1156            foreach ( $handlers as $id => $handler ) {
     1157                if ( preg_match( $handler['regex'], $url, $matches ) && is_callable( $handler['callback'] ) ) {
     1158                    if ( false !== $return = call_user_func( $handler['callback'], $matches, $attr, $url, $rawattr ) )
     1159                        return apply_filters( 'embed_handler_html', $return, $url, $attr );
     1160                }
     1161            }
     1162        }
     1163
     1164        // Get object ID
     1165        $id = apply_filters( 'embed_post_id', $id );
     1166
     1167        // Is oEmbed discovery on?
     1168        $attr['discover'] = ( apply_filters( 'bp_embed_oembed_discover', false ) && current_user_can( 'unfiltered_html' ) );
     1169
     1170        // Set up a new WP oEmbed object to check URL with registered oEmbed providers
     1171        require_once( ABSPATH . WPINC . '/class-oembed.php' );
     1172        $oembed_obj = _wp_oembed_get_object();
     1173
     1174        // If oEmbed discovery is true, skip oEmbed provider check
     1175        $is_oembed_link = false;
     1176        if ( !$attr['discover'] ) {
     1177            foreach ( (array)$oembed_obj->providers as $provider_matchmask => $provider ) {
     1178                $regex = ( $is_regex = $provider[1] ) ? $provider_matchmask : '#' . str_replace( '___wildcard___', '(.+)', preg_quote( str_replace( '*', '___wildcard___', $provider_matchmask ), '#' ) ) . '#i';
     1179
     1180                if ( preg_match( $regex, $url ) )
     1181                    $is_oembed_link = true;
     1182            }
     1183
     1184            // If url doesn't match a WP oEmbed provider, stop parsing
     1185            if ( !$is_oembed_link )
     1186                return $this->maybe_make_link( $url );
     1187        }
     1188
     1189        return $this->parse_oembed( $id, $url, $attr, $rawattr );
     1190    }
     1191
     1192    /**
     1193     * Base function so BP components / plugins can parse links to be embedded.
     1194     * View an example to add support in {@link bp_activity_embed()}.
     1195     *
     1196     * @uses apply_filters() Filters cache.
     1197     * @uses do_action() To save cache.
     1198     * @uses wp_oembed_get() Connects to oEmbed provider and returns HTML on success.
     1199     * @uses WP_Embed::maybe_make_link() Process URL for hyperlinking on oEmbed failure.
     1200     * @param int $id ID to do the caching for.
     1201     * @param string $url The URL attempting to be embedded.
     1202     * @param array $attr Shortcode attributes from {@link WP_Embed::shortcode()}.
     1203     * @param array $rawattr Untouched shortcode attributes from {@link WP_Embed::shortcode()}.
     1204     * @return string The embed HTML on success, otherwise the original URL.
     1205     */
     1206    function parse_oembed( $id, $url, $attr, $rawattr ) {
     1207        if ( $id ) {
     1208            // Setup the cachekey
     1209            $cachekey = '_oembed_' . md5( $url . serialize( $attr ) );
     1210
     1211            // Let components / plugins grab their cache
     1212            $cache = '';
     1213            $cache = apply_filters( 'bp_embed_get_cache', $cache, $id, $cachekey, $url, $attr, $rawattr );
     1214
     1215            // Grab cache and return it if available
     1216            if ( !empty( $cache ) ) {
     1217                return apply_filters( 'embed_oembed_html', $cache, $url, $attr, $rawattr );
     1218
     1219            // If no cache, ping the oEmbed provider and cache the result
     1220            } else {
     1221                $html = wp_oembed_get( $url, $attr );
     1222                $cache = ( $html ) ? $html : $url;
     1223
     1224                // Let components / plugins save their cache
     1225                do_action( 'bp_embed_update_cache', $cache, $cachekey, $id );
     1226
     1227                // If there was a result, return it
     1228                if ( $html )
     1229                    return apply_filters( 'bp_embed_oembed_html', $html, $url, $attr, $rawattr );
     1230            }
     1231        }
     1232
     1233        // Still unknown
     1234        return $this->maybe_make_link( $url );
     1235    }
     1236}
    10851237?>
  • trunk/bp-core/bp-core-functions.php

    r4678 r4709  
    1919function bp_get_option( $option_name, $default = '' ) {
    2020    $value = get_blog_option( bp_get_root_blog_id(), $option_name, $default );
    21    
     21
    2222    return apply_filters( 'bp_get_option', $value );
    2323}
     
    7979function bp_core_get_page_meta() {
    8080    $page_ids = bp_get_option( 'bp-pages' );
    81  
     81
    8282    // Upgrading from an earlier version of BP pre-1.3
    83     if ( !isset( $page_ids['members'] ) && $ms_page_ids = get_site_option( 'bp-pages' ) ) { 
     83    if ( !isset( $page_ids['members'] ) && $ms_page_ids = get_site_option( 'bp-pages' ) ) {
    8484        $page_blog_id = bp_is_multiblog_mode() ? get_current_blog_id() : bp_get_root_blog_id();
    8585
     
    9090        }
    9191    }
    92    
     92
    9393    return apply_filters( 'bp_core_get_page_meta', $page_ids );
    9494}
     
    698698function bp_core_record_activity() {
    699699    global $bp;
    700    
     700
    701701    if ( !is_user_logged_in() )
    702702        return false;
    703    
     703
    704704    $user_id = $bp->loggedin_user->id;
    705    
     705
    706706    if ( bp_core_is_user_spammer( $user_id ) || bp_core_is_user_deleted( $user_id ) )
    707707        return false;
     
    912912}
    913913add_action( 'bp_init', 'bp_core_add_ajax_hook' );
     914
     915/**
     916 * Initializes {@link BP_Embed} after everything is loaded.
     917 *
     918 * @global object $bp BuddyPress global settings
     919 * @package BuddyPress Core
     920 * @since 1.3
     921 */
     922function bp_embed_init() {
     923    global $bp;
     924
     925    if ( empty( $bp->embed ) )
     926        $bp->embed = new BP_Embed();
     927}
     928add_action( 'bp_init', 'bp_embed_init' );
    914929
    915930/**
     
    10751090 */
    10761091function bp_is_root_blog( $blog_id = 0 ) {
    1077    
    10781092    // Assume false
    10791093    $is_root_blog = false;
     
    11191133
    11201134        define( 'BP_ROOT_BLOG', $root_blog_id );
    1121        
     1135
    11221136    // Root blog is defined
    11231137    } else {
     
    12211235 *
    12221236 * @uses apply_filters() Filter 'bp_is_username_compatibility_mode' to alter
    1223  * @return bool False when compatibility mode is disabled (default); true when enabled 
     1237 * @return bool False when compatibility mode is disabled (default); true when enabled
    12241238 */
    12251239function bp_is_username_compatibility_mode() {
     
    12321246 * Note that BP_ENABLE_MULTIBLOG is different from (but dependent on) WP Multisite. "Multiblog" is
    12331247 * a BP setup that allows BP content to be viewed in the theme, and with the URL, of every blog
    1234  * on the network. Thus, instead of having all 'boonebgorges' links go to 
     1248 * on the network. Thus, instead of having all 'boonebgorges' links go to
    12351249 *   http://example.com/members/boonebgorges
    12361250 * on the root blog, each blog will have its own version of the same profile content, eg
     
    12451259 *
    12461260 * @uses apply_filters() Filter 'bp_is_multiblog_mode' to alter
    1247  * @return bool False when multiblog mode is disabled (default); true when enabled 
     1261 * @return bool False when multiblog mode is disabled (default); true when enabled
    12481262 */
    12491263function bp_is_multiblog_mode() {
     
    12641278 *
    12651279 * @uses apply_filters() Filter 'bp_use_wp_admin_bar' to alter
    1266  * @return bool False when WP Admin Bar support is disabled (default); true when enabled 
     1280 * @return bool False when WP Admin Bar support is disabled (default); true when enabled
    12671281 */
    12681282function bp_use_wp_admin_bar() {
    12691283    return apply_filters( 'bp_use_wp_admin_bar', defined( 'BP_USE_WP_ADMIN_BAR' ) && BP_USE_WP_ADMIN_BAR );
     1284}
     1285
     1286/**
     1287 * Are oembeds allowed in activity items?
     1288 *
     1289 * @return bool False when activity embed support is disabled; true when enabled (default)
     1290 * @since 1.3
     1291 */
     1292function bp_use_embed_in_activity() {
     1293    return apply_filters( 'bp_use_oembed_in_activity', !defined( 'BP_EMBED_DISABLE_ACTIVITY' ) || !BP_EMBED_DISABLE_ACTIVITY );
     1294}
     1295
     1296/**
     1297 * Are oembeds allwoed in acitivity replies?
     1298 *
     1299 * @return bool False when activity replies embed support is disabled; true when enabled (default)
     1300 * @since 1.3
     1301 */
     1302function bp_use_embed_in_activity_replies() {
     1303    return apply_filters( 'bp_use_embed_in_activity_replies', !defined( 'BP_EMBED_DISABLE_ACTIVITY_REPLIES' ) || !BP_EMBED_DISABLE_ACTIVITY_REPLIES );
     1304}
     1305
     1306/**
     1307 * Are oembeds allowed on forum posts?
     1308 *
     1309 * @return bool False when form post embed support is disabled; true when enabled (default)
     1310 * @since 1.3
     1311 */
     1312function bp_use_embed_in_forum_posts() {
     1313    return apply_filters( 'bp_use_embed_in_forum_posts', !defined( 'BP_EMBED_DISABLE_FORUM_POSTS' ) || !BP_EMBED_DISABLE_FORUM_POSTS );
    12701314}
    12711315
     
    13731417    do_action( 'bp_do_404', $redirect );
    13741418
    1375     $wp_query->set_404(); 
    1376     status_header( 404 ); 
     1419    $wp_query->set_404();
     1420    status_header( 404 );
    13771421    nocache_headers();
    13781422
  • trunk/bp-forums/bp-forums-functions.php

    r4658 r4709  
    515515add_action( 'bp_forums_new_post',  'bp_core_clear_cache' );
    516516
     517
     518/** Embeds *******************************************************************/
     519
     520/**
     521 * Grabs the topic post ID and attempts to retrieve the oEmbed cache (if it exists)
     522 * during the forum topic loop.  If no cache and link is embeddable, cache it.
     523 *
     524 * @see BP_Embed
     525 * @see bp_embed_forum_cache()
     526 * @see bp_embed_forum_save_cache()
     527 * @package BuddyPress_Forums
     528 * @since 1.3
     529 */
     530function bp_forums_embed() {
     531    add_filter( 'embed_post_id',         'bp_get_the_topic_post_id'         );
     532    add_filter( 'bp_embed_get_cache',    'bp_embed_forum_cache',      10, 3 );
     533    add_action( 'bp_embed_update_cache', 'bp_embed_forum_save_cache', 10, 3 );
     534}
     535add_action( 'topic_loop_start', 'bp_forums_embed' );
     536
     537/**
     538 * Wrapper function for {@link bb_get_postmeta()}.
     539 * Used during {@link BP_Embed::parse_oembed()} via {@link bp_forums_embed()}.
     540 *
     541 * @package BuddyPress_Forums
     542 * @since 1.3
     543 */
     544function bp_embed_forum_cache( $cache, $id, $cachekey ) {
     545    return bb_get_postmeta( $id, $cachekey );
     546}
     547
     548/**
     549 * Wrapper function for {@link bb_update_postmeta()}.
     550 * Used during {@link BP_Embed::parse_oembed()} via {@link bp_forums_embed()}.
     551 *
     552 * @package BuddyPress_Forums
     553 * @since 1.3
     554 */
     555function bp_embed_forum_save_cache( $cache, $cachekey, $id ) {
     556    bb_update_postmeta( $id, $cachekey, $cache );
     557}
    517558?>
Note: See TracChangeset for help on using the changeset viewer.