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