Guest User

r-a-y

a guest
Apr 29th, 2010
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.04 KB | None | 0 0
  1. <?php
  2. /* IMPORTANT: edit settings in bp-oembed-config.php - no need to edit this file */
  3. global $bp_oembed;
  4.  
  5. require_once(dirname( __FILE__ ) . '/bp-oembed-config.php');
  6.  
  7. if( !function_exists('parse_url_compat') ) {
  8.     function parse_url_compat($url, $component=NULL){
  9.        
  10.         if(!$component) return parse_url($url);
  11.        
  12.         ## PHP 5
  13.         if(phpversion() >= 5)
  14.             return parse_url($url, $component);
  15.    
  16.         ## PHP 4
  17.         $bits = parse_url($url);
  18.        
  19.         switch($component){
  20.             case PHP_URL_SCHEME: return $bits['scheme'];
  21.             case PHP_URL_HOST: return $bits['host'];
  22.             case PHP_URL_PORT: return $bits['port'];
  23.             case PHP_URL_USER: return $bits['user'];
  24.             case PHP_URL_PASS: return $bits['pass'];
  25.             case PHP_URL_PATH: return $bits['path'];
  26.             case PHP_URL_QUERY: return $bits['query'];
  27.             case PHP_URL_FRAGMENT: return $bits['fragment'];
  28.         }
  29.        
  30.     }
  31. }
  32.  
  33. // oembed for activity updates
  34. if($bp_oembed['activity_updates'])
  35.     add_filter('bp_get_activity_content_body','ray_bp_oembed', 9);
  36.  
  37. // oembed for activity comments
  38. if($bp_oembed['activity_comments'])
  39.     add_filter('bp_get_activity_content','ray_bp_oembed', 9);
  40.  
  41. // oembed for forum posts
  42. if($bp_oembed['forum_posts'])
  43.     add_filter('bp_get_the_topic_post_content','ray_bp_oembed', 9);
  44.  
  45. // whitelist hyperlinks
  46. $bp_oembed['whitelist'][] = '<a ';
  47. $bp_oembed['whitelist'][] = '">';
  48. $bp_oembed['whitelist'][] = '<a>';
  49.  
  50. // whitelist BP domain
  51. $bp_oembed['whitelist'][] = parse_url_compat(get_bloginfo('wpurl'), PHP_URL_HOST);
  52.  
  53. /* really stop editing! */
  54.  
  55. function ray_bp_oembed($content) {
  56.     global $bp_oembed;
  57.  
  58.     // WP(MU) 2.9 oEmbed check
  59.     if(!function_exists(wp_oembed_get))
  60.         return $content;
  61.  
  62.     // match URLs - could use some work
  63. //  preg_match_all( '@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', $content, $matches );
  64.     preg_match_all('`.*?((http|https)://[\w#$&+,\/:;[email protected]]+)[^\w#$&+,\/:;[email protected]]*?`i', $content, $matches);
  65.  
  66.     // debug regex
  67.     // print_r($matches[0]);
  68.  
  69.     // if there are no links to parse, return $content now!
  70.     if(empty($matches[0]))
  71.         return $content;
  72.  
  73.     $whitelist = $bp_oembed['whitelist'];
  74.  
  75.     for($i=0;$i<count($matches[0]);$i++) {
  76.         $url = $matches[0][$i];
  77.  
  78.         // check url with whitelist, if url matches any whitelist item, skip from parsing
  79.         foreach ($whitelist as $whitelist_item) {
  80.             if (strpos($url,$whitelist_item) !== false) {
  81.                 continue 2;
  82.             }
  83.         }
  84.  
  85.         $cachekey = '_oembed_' . md5($url);
  86.  
  87.         // grab oEmbed cache depending on BP component
  88.         // not pretty! only looking for activity updates or forum posts ATM
  89.         if(!bp_get_activity_id() && bp_forums_is_installed_correctly()) {
  90.             $cache = bb_get_postmeta(bp_get_the_topic_post_id(), $cachekey);
  91.         }
  92.         else {
  93.             $cache = bp_activity_get_meta( bp_get_activity_id(), $cachekey);       
  94.         }
  95.  
  96.         // cache check - no oEmbed, but cached result, skip rest of loop
  97.         if ( $url === $cache ) {
  98.             continue;
  99.         }
  100.  
  101.         // cache check - yes oEmbed
  102.         if ( !empty($cache) ) {
  103.             $replace = apply_filters( 'embed_oembed_html', $cache, $url, $attr );
  104.         }
  105.         // if no cache, let's start the show!
  106.         else {
  107.             // process url to oEmbed
  108.             $oembed = wp_oembed_get($url); // returns true if link is oEmbed
  109.             //$oembed = file_get_contents("http://autoembed.com/api/?url=".urlencode($url));
  110.  
  111.             if ($oembed) {
  112.                 $replace = apply_filters( 'embed_oembed_html', $oembed, $url, $attr );
  113.                 $replace = str_replace('
  114. ','',$replace); // fix Viddler line break in <object> tag
  115.             }
  116.             else {
  117.                 $replace = $url;
  118.                 // unlike WP's oEmbed, I cache the URL if not oEmbed-dable!
  119.                 // the URL is more useful in the DB than a string called {{unknown}} ;)
  120.             }
  121.  
  122.             // save oEmbed cache depending on BP component
  123.             // the same "not prettiness!"
  124.             if(!bp_get_activity_id() && bp_forums_is_installed_correctly())
  125.                 bb_update_postmeta(bp_get_the_topic_post_id(), $cachekey, $replace);
  126.             else
  127.                 bp_activity_update_meta( bp_get_activity_id(), $cachekey, $replace );
  128.         }
  129.  
  130.         $content = str_replace($url, $replace, $content);
  131.     }
  132.  
  133.     return $content;
  134. }
  135. ?>
Advertisement
Add Comment
Please, Sign In to add comment