Example #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.
  * Next, checks the URL against the regex of registered {@link nxt_oEmbed} providers if oEmbed discovery is false.
  * 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.
  *
  * @uses nxt_parse_args()
  * @uses nxt_embed_defaults()
  * @uses current_user_can()
  * @uses _nxt_oembed_get_object()
  * @uses nxt_Embed::maybe_make_link()
  *
  * @param array $attr Shortcode attributes.
  * @param string $url The URL attempting to be embeded.
  * @return string The embed HTML on success, otherwise the original URL.
  */
 function shortcode($attr, $url = '')
 {
     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 $hid => $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);
                 }
             }
         }
     }
     // Get object ID
     $id = apply_filters('embed_post_id', 0);
     // Is oEmbed discovery on?
     $attr['discover'] = apply_filters('bp_embed_oembed_discover', false) && current_user_can('unfiltered_html');
     // Set up a new nxt oEmbed object to check URL with registered oEmbed providers
     require_once ABSPATH . nxtINC . '/class-oembed.php';
     $oembed_obj = _nxt_oembed_get_object();
     // If oEmbed discovery is true, skip oEmbed provider check
     $is_oembed_link = false;
     if (!$attr['discover']) {
         foreach ((array) $oembed_obj->providers as $provider_matchmask => $provider) {
             $regex = ($is_regex = $provider[1]) ? $provider_matchmask : '#' . str_replace('___wildcard___', '(.+)', preg_quote(str_replace('*', '___wildcard___', $provider_matchmask), '#')) . '#i';
             if (preg_match($regex, $url)) {
                 $is_oembed_link = true;
             }
         }
         // If url doesn't match a nxt oEmbed provider, stop parsing
         if (!$is_oembed_link) {
             return $this->maybe_make_link($url);
         }
     }
     return $this->parse_oembed($id, $url, $attr, $rawattr);
 }
Example #2
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);
 }
Example #3
0
 /**
  * Connects to a oEmbed provider and returns the result.
  *
  * @param string $provider The URL to the oEmbed provider.
  * @param string $url The URL to the content that is desired to be embedded.
  * @param array $args Optional arguments. Usually passed from a shortcode.
  * @return bool|object False on failure, otherwise the result in the form of an object.
  */
 function fetch($provider, $url, $args = '')
 {
     $args = nxt_parse_args($args, nxt_embed_defaults());
     $provider = add_query_arg('maxwidth', (int) $args['width'], $provider);
     $provider = add_query_arg('maxheight', (int) $args['height'], $provider);
     $provider = add_query_arg('url', urlencode($url), $provider);
     foreach (array('json', 'xml') as $format) {
         $result = $this->_fetch_with_format($provider, $format);
         if (is_nxt_error($result) && 'not-implemented' == $result->get_error_code()) {
             continue;
         }
         return $result && !is_nxt_error($result) ? $result : false;
     }
     return false;
 }