Exemplo n.º 1
0
 /**
  * The {@link do_shortcode()} callback function.
  *
  * Attempts to convert a URL into embed HTML. Starts by checking the URL against the regex of the registered embed handlers.
  * If none of the regex matches and it's enabled, then the URL will be given to the {@link nxt_oEmbed} class.
  *
  * @uses nxt_oembed_get()
  * @uses nxt_parse_args()
  * @uses nxt_embed_defaults()
  * @uses nxt_Embed::maybe_make_link()
  * @uses get_option()
  * @uses current_user_can()
  * @uses nxt_cache_get()
  * @uses nxt_cache_set()
  * @uses get_post_meta()
  * @uses update_post_meta()
  *
  * @param array $attr Shortcode attributes.
  * @param string $url The URL attempting to be embedded.
  * @return string The embed HTML on success, otherwise the original URL.
  */
 function shortcode($attr, $url = '')
 {
     global $post;
     if (empty($url)) {
         return '';
     }
     $rawattr = $attr;
     $attr = nxt_parse_args($attr, nxt_embed_defaults());
     // kses converts & into & and we need to undo this
     // See http://core.trac.nxtclass.org/ticket/11311
     $url = str_replace('&', '&', $url);
     // Look for known internal handlers
     ksort($this->handlers);
     foreach ($this->handlers as $priority => $handlers) {
         foreach ($handlers as $id => $handler) {
             if (preg_match($handler['regex'], $url, $matches) && is_callable($handler['callback'])) {
                 if (false !== ($return = call_user_func($handler['callback'], $matches, $attr, $url, $rawattr))) {
                     return apply_filters('embed_handler_html', $return, $url, $attr);
                 }
             }
         }
     }
     $post_ID = !empty($post->ID) ? $post->ID : null;
     if (!empty($this->post_ID)) {
         // Potentially set by nxt_Embed::cache_oembed()
         $post_ID = $this->post_ID;
     }
     // Unknown URL format. Let oEmbed have a go.
     if ($post_ID) {
         // Check for a cached result (stored in the post meta)
         $cachekey = '_oembed_' . md5($url . serialize($attr));
         if ($this->usecache) {
             $cache = get_post_meta($post_ID, $cachekey, true);
             // Failures are cached
             if ('{{unknown}}' === $cache) {
                 return $this->maybe_make_link($url);
             }
             if (!empty($cache)) {
                 return apply_filters('embed_oembed_html', $cache, $url, $attr, $post_ID);
             }
         }
         // Use oEmbed to get the HTML
         $attr['discover'] = apply_filters('embed_oembed_discover', false) && author_can($post_ID, 'unfiltered_html');
         $html = nxt_oembed_get($url, $attr);
         // Cache the result
         $cache = $html ? $html : '{{unknown}}';
         update_post_meta($post_ID, $cachekey, $cache);
         // If there was a result, return it
         if ($html) {
             return apply_filters('embed_oembed_html', $html, $url, $attr, $post_ID);
         }
     }
     // Still unknown
     return $this->maybe_make_link($url);
 }
Exemplo n.º 2
0
 /**
  * Base function so BP components / plugins can parse links to be embedded.
  * View an example to add support in {@link bp_activity_embed()}.
  *
  * @uses apply_filters() Filters cache.
  * @uses do_action() To save cache.
  * @uses nxt_oembed_get() Connects to oEmbed provider and returns HTML on success.
  * @uses nxt_Embed::maybe_make_link() Process URL for hyperlinking on oEmbed failure.
  * @param int $id ID to do the caching for.
  * @param string $url The URL attempting to be embedded.
  * @param array $attr Shortcode attributes from {@link nxt_Embed::shortcode()}.
  * @param array $rawattr Untouched shortcode attributes from {@link nxt_Embed::shortcode()}.
  * @return string The embed HTML on success, otherwise the original URL.
  */
 function parse_oembed($id, $url, $attr, $rawattr)
 {
     $id = intval($id);
     if ($id) {
         // Setup the cachekey
         $cachekey = '_oembed_' . md5($url . serialize($attr));
         // Let components / plugins grab their cache
         $cache = '';
         $cache = apply_filters('bp_embed_get_cache', $cache, $id, $cachekey, $url, $attr, $rawattr);
         // Grab cache and return it if available
         if (!empty($cache)) {
             return apply_filters('bp_embed_oembed_html', $cache, $url, $attr, $rawattr);
             // If no cache, ping the oEmbed provider and cache the result
         } else {
             $html = nxt_oembed_get($url, $attr);
             $cache = $html ? $html : $url;
             // Let components / plugins save their cache
             do_action('bp_embed_update_cache', $cache, $cachekey, $id);
             // If there was a result, return it
             if ($html) {
                 return apply_filters('bp_embed_oembed_html', $html, $url, $attr, $rawattr);
             }
         }
     }
     // Still unknown
     return $this->maybe_make_link($url);
 }