703 | | ?> |
| 703 | /** |
| 704 | * BP_Embed |
| 705 | * |
| 706 | * Extends WP_Embed class for use with BuddyPress. |
| 707 | * |
| 708 | * @package BuddyPress Core |
| 709 | * @since 1.3 |
| 710 | * @see WP_Embed |
| 711 | */ |
| 712 | class BP_Embed extends WP_Embed { |
| 713 | |
| 714 | /** |
| 715 | * PHP4 constructor |
| 716 | */ |
| 717 | function BP_Embed() { |
| 718 | return $this->__construct(); |
| 719 | } |
| 720 | |
| 721 | /** |
| 722 | * PHP5 constructor |
| 723 | */ |
| 724 | function __construct() { |
| 725 | global $wp_embed; |
| 726 | |
| 727 | // Make sure we populate the WP_Embed handlers array. |
| 728 | // These are providers that use a regex callback on the URL in question. |
| 729 | // Do not confuse with oEmbed providers, which require an external ping. |
| 730 | // Used in WP_Embed::shortcode() |
| 731 | $this->handlers = $wp_embed->handlers; |
| 732 | |
| 733 | if( !defined( 'BP_EMBED_DISABLE_ACTIVITY' ) ) { |
| 734 | add_filter( 'bp_get_activity_content_body', array(&$this, 'autoembed'), 9 ); |
| 735 | add_filter( 'bp_get_activity_content_body', array(&$this, 'run_shortcode'), 8 ); |
| 736 | } |
| 737 | |
| 738 | if( !defined( 'BP_EMBED_DISABLE_ACTIVITY_REPLIES' ) ) { |
| 739 | add_filter( 'bp_get_activity_content', array(&$this, 'autoembed'), 9 ); |
| 740 | add_filter( 'bp_get_activity_content', array(&$this, 'run_shortcode'), 8 ); |
| 741 | } |
| 742 | |
| 743 | if( !defined( 'BP_EMBED_DISABLE_FORUM_POSTS' ) ) { |
| 744 | add_filter( 'bp_get_the_topic_post_content', array(&$this, 'autoembed'), 9 ); |
| 745 | add_filter( 'bp_get_the_topic_post_content', array(&$this, 'run_shortcode'), 8 ); |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | /** |
| 750 | * Base function so BP components / plugins can parse links to be embedded. |
| 751 | * View examples to add support in {@link bp_embed_init()}. |
| 752 | * Overrides {@link WP_Embed::shortcode()}. |
| 753 | * |
| 754 | * @uses wp_parse_args() |
| 755 | * @uses wp_embed_defaults() |
| 756 | * @uses _wp_oembed_get_object() |
| 757 | * @uses wp_oembed_get() Connects to oEmbed provider and returns HTML on success. |
| 758 | * @uses WP_Embed::maybe_make_link() Process URL for hyperlinking on oEmbed failure. |
| 759 | * @param string $url The URL attempting to be embedded. |
| 760 | * @param array $attr Shortcode attributes from {@link WP_Embed::shortcode()}. |
| 761 | * @return string The embed HTML on success, otherwise the original URL. |
| 762 | */ |
| 763 | function shortcode( $attr, $url = '' ) { |
| 764 | if ( empty($url) ) |
| 765 | return ''; |
| 766 | |
| 767 | $rawattr = $attr; |
| 768 | $attr = wp_parse_args( $attr, wp_embed_defaults() ); |
| 769 | |
| 770 | // Look for known internal handlers |
| 771 | ksort( $this->handlers ); |
| 772 | foreach ( $this->handlers as $priority => $handlers ) { |
| 773 | foreach ( $handlers as $id => $handler ) { |
| 774 | if ( preg_match( $handler['regex'], $url, $matches ) && is_callable( $handler['callback'] ) ) { |
| 775 | if ( false !== $return = call_user_func( $handler['callback'], $matches, $attr, $url, $rawattr ) ) |
| 776 | return apply_filters( 'embed_handler_html', $return, $url, $attr ); |
| 777 | } |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | // Get object ID |
| 782 | $id = apply_filters( 'bp_embed_object_id', $id ); |
| 783 | |
| 784 | // Is oEmbed discovery on? |
| 785 | $attr['discover'] = apply_filters( 'bp_embed_oembed_discover', false ); |
| 786 | |
| 787 | // Set up a new WP oEmbed object to check URL with registered oEmbed providers |
| 788 | require_once( ABSPATH . WPINC . '/class-oembed.php' ); |
| 789 | $oembed_obj = _wp_oembed_get_object(); |
| 790 | |
| 791 | // If oEmbed discovery is true, skip oEmbed provider check |
| 792 | $is_oembed_link = false; |
| 793 | if ( !$attr['discover'] ) { |
| 794 | foreach ( (array)$oembed_obj->providers as $provider_matchmask => $provider ) { |
| 795 | $regex = ( $is_regex = $provider[1] ) ? $provider_matchmask : '#' . str_replace( '___wildcard___', '(.+)', preg_quote( str_replace( '*', '___wildcard___', $provider_matchmask ), '#' ) ) . '#i'; |
| 796 | |
| 797 | if ( preg_match( $regex, $url ) ) |
| 798 | $is_oembed_link = true; |
| 799 | } |
| 800 | |
| 801 | // If URL doesn't match a WP oEmbed provider, stop parsing and return link |
| 802 | if ( !$is_oembed_link ) |
| 803 | return $this->maybe_make_link( $url ); |
| 804 | } |
| 805 | |
| 806 | // Check to see if an object ID was passed |
| 807 | if ( $id ) { |
| 808 | // Setup the cachekey |
| 809 | $cachekey = '_oembed_' . md5( $url . serialize( $attr ) ); |
| 810 | |
| 811 | // Let components / plugins grab their cache |
| 812 | $cache = ''; |
| 813 | $cache = apply_filters( 'bp_embed_get_cache', $cache, $id, $cachekey, $url, $attr, $rawattr ); |
| 814 | |
| 815 | // Grab cache and return it if available |
| 816 | if ( !empty($cache) ) { |
| 817 | return apply_filters( 'embed_oembed_html', $cache, $url, $attr, $rawattr ); |
| 818 | } |
| 819 | // If no cache, ping the oEmbed provider and cache the result |
| 820 | else { |
| 821 | $html = wp_oembed_get( $url, $attr ); |
| 822 | $cache = ( $html ) ? $html : $url; |
| 823 | |
| 824 | // Let components / plugins save their cache |
| 825 | do_action( 'bp_embed_update_cache', $cache, $cachekey, $id ); |
| 826 | |
| 827 | // If there was a result, return it |
| 828 | if ( $html ) |
| 829 | return apply_filters( 'embed_oembed_html', $html, $url, $attr, $rawattr ); |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | // Still unknown |
| 834 | return $this->maybe_make_link( $url ); |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | ?> |
| 839 | No newline at end of file |