Skip to:
Content

BuddyPress.org


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

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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 & and we need to undo this
     1150        // See http://core.trac.wordpress.org/ticket/11311
     1151        $url = str_replace( '&', '&', $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?>
Note: See TracChangeset for help on using the changeset viewer.