function get_default_sizes() { $wp_embed_defaults = wp_embed_defaults(); $height = intval(get_option('documentcloud_default_height', $wp_embed_defaults['height'])); $width = intval(get_option('documentcloud_default_width', $wp_embed_defaults['width'])); $full_width = intval(get_option('documentcloud_full_width', WP_DocumentCloud::DEFAULT_EMBED_FULL_WIDTH)); return array('height' => $height, 'width' => $width, 'full_width' => $full_width); }
/** * Handles text attachments on their attachment pages. * @author Justin Tadlock <*****@*****.**> * @since 3.0.0 */ function tamatebako_attachment_text($mime = '', $file = '') { $embed_defaults = wp_embed_defaults(); $text = '<object class="text" type="' . esc_attr($mime) . '" data="' . esc_url($file) . '" width="' . esc_attr($embed_defaults['width']) . '" height="' . esc_attr($embed_defaults['height']) . '">'; $text .= '<param name="src" value="' . esc_url($file) . '" />'; $text .= '</object>'; return $text; }
/** * Bootstrap this plugin * * @param string $file */ public static function bootstrap($file) { self::$file = $file; Pronamic_Google_Maps_Plugin::bootstrap(); Pronamic_Google_Maps_Widget::bootstrap(); Pronamic_Google_Maps_Shortcodes::bootstrap(); // Actions and hooks add_action('init', array(__CLASS__, 'init')); add_filter('parse_query', array(__CLASS__, 'parse_query'), 1000); // Options $embed_size = wp_embed_defaults(); self::$defaultWidth = $embed_size['width']; self::$defaultHeight = $embed_size['height']; }
function shortcode($attr, $url = '') { global $rte; if (empty($url)) { return ''; } $rawattr = $attr; $attr = wp_parse_args($attr, wp_embed_defaults()); // kses converts & into & and we need to undo this // See http://core.trac.wordpress.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); } } } } // Check for a cached result (stored in the post meta) $cachekey = '_oembed_' . md5($url . serialize($attr)); if ($this->usecache) { $cache = $this->rte->get_cache($cachekey); // Failures are cached if ('{{unknown}}' === $cache) { return $this->maybe_make_link($url); } if (!empty($cache)) { return apply_filters('embed_oembed_html', $cache, $url, $attr); } } // Use oEmbed to get the HTML $attr['discover'] = apply_filters('embed_oembed_discover', false) && current_user_can('unfiltered_html'); $html = wp_oembed_get($url, $attr); // Cache the result $cache = $html ? $html : '{{unknown}}'; $this->rte->set_cache($cachekey, $cache, 3600 * 24 * 7); // one week // If there was a result, return it if ($html) { return apply_filters('embed_oembed_html', $html, $url, $attr); } // Still unknown return $this->maybe_make_link($url); }
protected function render() { $settings = $this->get_settings(); if (empty($settings['link'])) { return; } $this->_current_instance = $settings; add_filter('oembed_result', [$this, 'filter_oembed_result'], 50, 3); $video_html = wp_oembed_get($settings['link']['url'], wp_embed_defaults()); remove_filter('oembed_result', [$this, 'filter_oembed_result'], 50); if ($video_html) { ?> <div class="elementor-soundcloud-wrapper"> <?php echo $video_html; ?> </div> <?php } }
public function test_shortcode_should_cache_failure_in_post_meta_for_known_post() { $GLOBALS['post'] = $this->factory()->post->create_and_get(); $url = 'https://example.com/'; $expected = '<a href="' . esc_url($url) . '">' . esc_html($url) . '</a>'; $key_suffix = md5($url . serialize(wp_embed_defaults($url))); $cachekey = '_oembed_' . $key_suffix; $cachekey_time = '_oembed_time_' . $key_suffix; add_filter('pre_oembed_result', '__return_empty_string'); $actual = $this->wp_embed->shortcode(array(), $url); remove_filter('pre_oembed_result', '__return_empty_string'); $this->assertEquals($expected, $actual); $this->assertEquals('{{unknown}}', get_post_meta($GLOBALS['post']->ID, $cachekey, true)); $this->assertEmpty(get_post_meta($GLOBALS['post']->ID, $cachekey_time, true)); // Result should be cached. $actual = $this->wp_embed->shortcode(array(), $url); $this->assertEquals($expected, $actual); }
/** * 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 WP_oEmbed} class. * * @uses wp_oembed_get() * @uses wp_parse_args() * @uses wp_embed_defaults() * @uses WP_Embed::maybe_make_link() * @uses get_option() * @uses current_user_can() * @uses wp_cache_get() * @uses wp_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 = wp_parse_args($attr, wp_embed_defaults()); // kses converts & into & and we need to undo this // See http://core.trac.wordpress.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 WP_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 = wp_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); }
/** * 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 WP_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 wp_parse_args() * @uses wp_embed_defaults() * @uses current_user_can() * @uses _wp_oembed_get_object() * @uses WP_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 = wp_parse_args($attr, wp_embed_defaults()); // kses converts & into & and we need to undo this // See http://core.trac.wordpress.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 WP oEmbed object to check URL with registered oEmbed providers require_once ABSPATH . WPINC . '/class-oembed.php'; $oembed_obj = _wp_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 WP oEmbed provider, stop parsing if (!$is_oembed_link) { return $this->maybe_make_link($url); } } return $this->parse_oembed($id, $url, $attr, $rawattr); }
/** * 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 = wp_parse_args($args, wp_embed_defaults()); $provider = add_query_arg('format', 'json', $provider); // JSON is easier to deal with than XML $provider = add_query_arg('maxwidth', $args['width'], $provider); $provider = add_query_arg('maxheight', $args['height'], $provider); $provider = add_query_arg('url', urlencode($url), $provider); if (!($result = wp_remote_retrieve_body(wp_remote_get($provider)))) { return false; } $result = trim($result); // JSON? // Example content: http://vimeo.com/api/oembed.json?url=http%3A%2F%2Fvimeo.com%2F240975 if ($data = json_decode($result)) { return $data; } elseif (function_exists('simplexml_load_string')) { $errors = libxml_use_internal_errors('true'); $data = simplexml_load_string($result); libxml_use_internal_errors($errors); if (is_object($data)) { return $data; } } return false; }
protected function render() { $settings = $this->get_settings(); if ('hosted' !== $settings['video_type']) { add_filter('oembed_result', [$this, 'filter_oembed_result'], 50, 3); $video_link = 'youtube' === $settings['video_type'] ? $settings['link'] : $settings['vimeo_link']; if (empty($video_link)) { return; } $video_html = wp_oembed_get($video_link, wp_embed_defaults()); remove_filter('oembed_result', [$this, 'filter_oembed_result'], 50); } else { $video_html = wp_video_shortcode($this->get_hosted_params()); } if ($video_html) { ?> <div class="elementor-video-wrapper"> <?php echo $video_html; if ($this->has_image_overlay()) { ?> <div class="elementor-custom-embed-image-overlay" style="background-image: url(<?php echo $settings['image_overlay']['url']; ?> );"> <?php if ('yes' === $settings['show_play_icon']) { ?> <div class="elementor-custom-embed-play"> <i class="fa fa-play-circle"></i> </div> <?php } ?> </div> <?php } ?> </div> <?php } else { echo $settings['link']; } }
/** * A very simple markdown parser. * * @since 150113 First documented version. * * @param string $string Input string to convert. * @param array $args Any additional behavioral args. * * @return string Markdown converted to HTML markup. */ public function markdown($string, array $args = []) { if (!($string = trim((string) $string))) { return $string; // Not possible. } $default_args = ['oembed' => false, 'breaks' => true, 'no_p' => false]; $args = array_merge($default_args, $args); $args = array_intersect_key($args, $default_args); $oembed = (bool) $args['oembed']; $breaks = (bool) $args['breaks']; $no_p = (bool) $args['no_p']; if ($oembed && strpos($string, '://') !== false) { $_spcsm = $this->spcsmTokens($string, [], __FUNCTION__); $_oembed_args = array_merge(wp_embed_defaults(), ['discover' => false]); $_spcsm['string'] = preg_replace_callback('/^\\s*(https?:\\/\\/[^\\s"]+)\\s*$/im', function ($m) use($_oembed_args) { $oembed = wp_oembed_get($m[1], $_oembed_args); return $oembed ? $oembed : $m[0]; }, $_spcsm['string']); $string = $this->spcsmRestore($_spcsm); unset($_spcsm, $_oembed_args); // Housekeeping. } if (is_null($parsedown =& $this->cacheKey(__FUNCTION__, 'parsedown'))) { $parsedown = new \ParsedownExtra(); // Singleton. } $parsedown->setBreaksEnabled($breaks); $html = $parsedown->text($string); if ($no_p) { // Remove `<p></p>` wrap? $html = preg_replace('/^\\<p\\>/i', '', $html); $html = preg_replace('/\\<\\/p\\>$/i', '', $html); } return $html; // Gotta love Parsedown :-) }
/** * 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. */ public function fetch($provider, $url, $args = '') { $args = wp_parse_args($args, wp_embed_defaults($url)); $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); /** * Filter the oEmbed URL to be fetched. * * @since 2.9.0 * * @param string $provider URL of the oEmbed provider. * @param string $url URL of the content to be embedded. * @param array $args Optional arguments, usually passed from a shortcode. */ $provider = apply_filters('oembed_fetch_url', $provider, $url, $args); foreach (array('json', 'xml') as $format) { $result = $this->_fetch_with_format($provider, $format); if (is_wp_error($result) && 'not-implemented' == $result->get_error_code()) { continue; } return $result && !is_wp_error($result) ? $result : false; } return false; }
/** * 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 WP_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 wp_parse_args() * @uses wp_embed_defaults() * @uses current_user_can() * @uses _wp_oembed_get_object() * @uses WP_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. */ public function shortcode($attr, $url = '') { if (empty($url)) { return ''; } $rawattr = $attr; $attr = wp_parse_args($attr, wp_embed_defaults()); // Use kses to convert & into & and we need to undo this // See https://core.trac.wordpress.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))) { /** * Filters the oEmbed handler result for the provided URL. * * @since 1.5.0 * * @param string $return Handler callback for the oEmbed. * @param string $url URL attempting to be embedded. * @param array $attr Shortcode attributes. */ return apply_filters('embed_handler_html', $return, $url, $attr); } } } } /** * Filters the embed object ID. * * @since 1.5.0 * * @param int $value Value of zero. */ $id = apply_filters('embed_post_id', 0); $unfiltered_html = current_user_can('unfiltered_html'); $default_discovery = false; // Since 4.4, WordPress is now an oEmbed provider. if (function_exists('wp_oembed_register_route')) { $unfiltered_html = true; $default_discovery = true; } /** * Filters whether or not oEmbed discovery is on. * * @since 1.5.0 * @since 2.5.0 Default status of oEmbed discovery has been switched * to true to apply changes introduced in WordPress 4.4 * * @param bool $default_discovery Current status of oEmbed discovery. */ $attr['discover'] = apply_filters('bp_embed_oembed_discover', $default_discovery) && $unfiltered_html; // Set up a new WP oEmbed object to check URL with registered oEmbed providers. require_once ABSPATH . WPINC . '/class-oembed.php'; $oembed_obj = _wp_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 WP oEmbed provider, stop parsing. if (!$is_oembed_link) { return $this->maybe_make_link($url); } } return $this->parse_oembed($id, $url, $attr, $rawattr); }
function shortcode($attr, $url = '') { global $post; if (empty($url)) { return ''; } $rawattr = $attr; $attr = wp_parse_args($attr, wp_embed_defaults()); // kses converts & into & and we need to undo this // See http://core.trac.wordpress.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); } } } } $transient_name = $this->widget_id . '_' . md5($url); // Store the value of the disable_related option. // If it changes we need to clear transient data containing the html // and update the transient containing the disable_related value $related_transient = get_transient($transient_name . '_related'); if (false !== $related_transient && $related_transient != $this->disable_related) { delete_transient($transient_name); set_transient($transient_name . '_related', $this->disable_related, 60 * 60 * 12); } $transient = get_transient($transient_name); // return the transient html value if its available if (false !== $transient) { return apply_filters('embed_oembed_html', $transient, $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 = wp_oembed_get($url, $attr); // If there was a result, return it if ($html) { // if 'youtube' is found in the html and disable related is true, add rel=0 paramater if (false !== strpos($html, 'youtube') && true == $this->disable_related) { $html = $this->disable_youtube_related($html); } set_transient($transient_name, $html, 60 * 60 * 12); // We need to know if the disable_related value of this widget changes // and clear transient data when it does set_transient($transient_name . '_related', $this->disable_related, 60 * 60 * 12); return apply_filters('embed_oembed_html', $html, $url, $attr, $post_ID); } return $this->maybe_make_link($url); }
/** * Handles text attachments on their attachment pages. Uses the `<object>` element to embed media * in the pages. * * @since 0.3.0 * @access public * @param string $mime attachment mime type * @param string $file attachment file URL * @return string */ function hybrid_text_attachment($mime = '', $file = '') { $embed_defaults = wp_embed_defaults(); return sprintf('<object type="%1$s" data="%2$s" width="%3$s" height="%4$s"><param name="src" value="%2$s" /></object>', esc_attr($mime), esc_url($file), absint($embed_defaults['width']), absint($embed_defaults['height'])); }
/** * 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 WP_oEmbed} class. * * @uses wp_oembed_get() * @uses wp_parse_args() * @uses wp_embed_defaults() * @uses WP_Embed::maybe_make_link() * @uses get_option() * @uses author_can() * @uses wp_cache_get() * @uses wp_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 = '' ) { $post = get_post(); if ( empty( $url ) ) return ''; $rawattr = $attr; $attr = wp_parse_args( $attr, wp_embed_defaults() ); // kses converts & into & and we need to undo this // See http://core.trac.wordpress.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 ) ) /** * Filter the returned embed handler. * * @since 2.9.0 * * @param mixed $return The shortcode callback function to call. * @param string $url The attempted embed URL. * @param array $attr An array of shortcode attributes. */ 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 WP_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 ) ) /** * Filter the cached oEmbed HTML. * * @since 2.9.0 * * @param mixed $cache The cached HTML result, stored in post meta. * @param string $url The attempted embed URL. * @param array $attr An array of shortcode attributes. * @param int $post_ID Post ID. */ return apply_filters( 'embed_oembed_html', $cache, $url, $attr, $post_ID ); } /** * Filter whether to inspect the given URL for discoverable <link> tags. * * @see WP_oEmbed::discover() * * @param bool false Whether to enable <link> tag discovery. Default false. */ $attr['discover'] = ( apply_filters( 'embed_oembed_discover', false ) && author_can( $post_ID, 'unfiltered_html' ) ); // Use oEmbed to get the HTML $html = wp_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 ) { /** This filter is documented in wp-includes/class-wp-embed.php */ return apply_filters( 'embed_oembed_html', $html, $url, $attr, $post_ID ); } } // Still unknown return $this->maybe_make_link( $url ); }
/** * 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 WP_oEmbed} class. * * @uses wp_oembed_get() * @uses wp_parse_args() * @uses wp_embed_defaults() * @uses WP_Embed::maybe_make_link() * @uses get_option() * @uses current_user_can() * @uses wp_cache_get() * @uses wp_cache_set() * @uses get_post_meta() * @uses update_post_meta() * * @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 = '') { global $post, $_wp_using_ext_object_cache; if (empty($url)) { return ''; } $rawattr = $attr; $attr = wp_parse_args($attr, wp_embed_defaults()); // 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 $return; } } } } $post_ID = !empty($post->ID) ? $post->ID : null; if (!empty($this->post_ID)) { // Potentially set by WP_Embed::cache_oembed() $post_ID = $this->post_ID; } // Unknown URL format. Let oEmbed have a go. if ($post_ID && get_option('embed_useoembed')) { // Check for a cached result (stored in the post meta) $cachekey = '_oembed_' . md5($url . implode('|', $attr)); if ($this->usecache) { $cache = $_wp_using_ext_object_cache ? wp_cache_get("{$post_ID}_{$cachekey}", 'oembed') : get_post_meta($post_ID, $cachekey, true); // Failures are cached if ('{{unknown}}' === $cache) { return $this->maybe_make_link($url); } if (!empty($cache)) { return $cache; } } // Use oEmbed to get the HTML $attr['discover'] = author_can($post_ID, 'unfiltered_html'); $html = wp_oembed_get($url, $attr); // Cache the result $cache = $html ? $html : '{{unknown}}'; if ($_wp_using_ext_object_cache) { wp_cache_set("{$post_ID}_{$cachekey}", $cache, 'oembed'); } else { update_post_meta($post_ID, $cachekey, $cache); } // If there was a result, return it if ($html) { return $html; } } // Still unknown return $this->maybe_make_link($url); }
/** * Outputs the first embedded item in the activity oEmbed template. * * @since 2.6.0 */ function bp_activity_embed_media() { // Bail if oEmbed request explicitly hides media. if (isset($_GET['hide_media']) && true == wp_validate_boolean($_GET['hide_media'])) { /** * Do something after media is rendered for an activity oEmbed item. * * @since 2.6.0 */ do_action('bp_activity_embed_after_media'); return; } /** * Should we display media in the oEmbed template? * * @since 2.6.0 * * @param bool $retval Defaults to true. */ $allow_media = apply_filters('bp_activity_embed_display_media', true); // Find oEmbeds from only WP registered providers. bp_remove_all_filters('oembed_providers'); $media = bp_core_extract_media_from_content($GLOBALS['activities_template']->activity->content, 'embeds'); bp_restore_all_filters('oembed_providers'); // oEmbeds have precedence over inline video / audio. if (isset($media['embeds']) && true === $allow_media) { // Autoembed first URL. $oembed_defaults = wp_embed_defaults(); $oembed_args = array('width' => $oembed_defaults['width'], 'height' => $oembed_defaults['height'], 'discover' => true); $url = $media['embeds'][0]['url']; $cachekey = '_oembed_response_' . md5($url . serialize($oembed_args)); // Try to fetch oEmbed response from meta. $oembed = bp_activity_get_meta(bp_get_activity_id(), $cachekey); // No cache, so fetch full oEmbed response now! if ('' === $oembed) { $o = _wp_oembed_get_object(); $oembed = $o->fetch($o->get_provider($url, $oembed_args), $url, $oembed_args); // Cache oEmbed response. bp_activity_update_meta(bp_get_activity_id(), $cachekey, $oembed); } $content = ''; /** * Filters the default embed display max width. * * This is used if the oEmbed response does not return a thumbnail width. * * @since 2.6.0 * * @param int $width. */ $width = (int) apply_filters('bp_activity_embed_display_media_width', 550); // Set thumbnail. if ('photo' === $oembed->type) { $thumbnail = $oembed->url; } elseif (isset($oembed->thumbnail_url)) { $thumbnail = $oembed->thumbnail_url; /* Non-oEmbed standard attributes */ // Mixcloud } elseif (isset($oembed->image)) { $thumbnail = $oembed->image; // ReverbNation } elseif (isset($oembed->{'thumbnail-url'})) { $thumbnail = $oembed->{'thumbnail-url'}; } // Display thumb and related oEmbed meta. if (true === isset($thumbnail)) { $play_icon = $caption = ''; // Add play icon for non-photos. if ('photo' !== $oembed->type) { /** * ion-play icon from Ionicons. * * @link http://ionicons.com/ * @license MIT */ $play_icon = <<<EOD <svg id="Layer_1" style="enable-background:new 0 0 512 512;" version="1.1" viewBox="0 0 512 512" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><path d="M405.2,232.9L126.8,67.2c-3.4-2-6.9-3.2-10.9-3.2c-10.9,0-19.8,9-19.8,20H96v344h0.1c0,11,8.9,20,19.8,20 c4.1,0,7.5-1.4,11.2-3.4l278.1-165.5c6.6-5.5,10.8-13.8,10.8-23.1C416,246.7,411.8,238.5,405.2,232.9z"/></svg> EOD; $play_icon = sprintf('<a rel="nofollow" class="play-btn" href="%1$s" onclick="top.location.href=\'%1$s\'">%2$s</a>', esc_url($url), $play_icon); } // Thumb width $thumb_width = isset($oembed->thumbnail_width) && 'photo' !== $oembed->type && (int) $oembed->thumbnail_width < 550 ? (int) $oembed->thumbnail_width : $width; $float_width = 350; // Set up thumb. $content = sprintf('<div class="thumb" style="max-width:%1$spx">%2$s<a href="%3$s" rel="nofollow" onclick="top.location.href=\'%3$s\'"><img src="%4$s" /></a></div>', $thumb_width, $play_icon, esc_url($url), esc_url($thumbnail)); // Show title. if (isset($oembed->title)) { $caption .= sprintf('<p class="caption-title"><strong>%s</strong></p>', apply_filters('single_post_title', $oembed->title)); } // Show description (non-oEmbed standard) if (isset($oembed->description)) { $caption .= sprintf('<div class="caption-description">%s</div>', apply_filters('bp_activity_get_embed_excerpt', $oembed->description)); } // Show author info. if (isset($oembed->provider_name) && isset($oembed->author_name)) { /* translators: By [oEmbed author] on [oEmbed provider]. eg. By BuddyPress on YouTube. */ $anchor_text = sprintf(__('By %1$s on %2$s', 'buddypress'), $oembed->author_name, $oembed->provider_name); } elseif (isset($oembed->provider_name)) { $anchor_text = sprintf(__('View on %s', 'buddypress'), $oembed->provider_name); } if (true === isset($anchor_text)) { $caption .= sprintf('<a rel="nofollow" href="%1$s" onclick="top.location.href=\'%1$s\'">%2$s</a>', esc_url($url), apply_filters('the_title', $anchor_text)); } // Set up caption. if ('' !== $caption) { $css_class = isset($oembed->provider_name) ? sprintf(' provider-%s', sanitize_html_class(strtolower($oembed->provider_name))) : ''; $caption = sprintf('<div class="caption%1$s" style="width:%2$s">%3$s</div>', $css_class, $thumb_width > $float_width ? 100 . '%' : round(($width - (int) $thumb_width) / $width * 100) . '%', $caption); $content .= $caption; } } // Print rich content. if ('' !== $content) { printf('<div class="bp-activity-embed-display-media %s" style="max-width:%spx">%s</div>', $thumb_width < $float_width ? 'two-col' : 'one-col', $thumb_width < $float_width ? $width : $thumb_width, $content); } // Video / audio. } elseif (true === $allow_media) { // Call BP_Embed if it hasn't already loaded. bp_embed_init(); // Run shortcode and embed routine. $content = buddypress()->embed->run_shortcode($GLOBALS['activities_template']->activity->content); $content = buddypress()->embed->autoembed($content); // Try to find inline video / audio. $media = bp_core_extract_media_from_content($content, 96); // Video takes precedence. HTML5-only. if (isset($media['videos']) && 'shortcodes' === $media['videos'][0]['source']) { printf('<video controls preload="metadata"><source src="%1$s"><p>%2$s</p></video>', esc_url($media['videos'][0]['url']), esc_html__('Your browser does not support HTML5 video', 'buddypress')); // No video? Try audio. HTML5-only. } elseif (isset($media['audio']) && 'shortcodes' === $media['audio'][0]['source']) { printf('<audio controls preload="metadata"><source src="%1$s"><p>%2$s</p></audio>', esc_url($media['audio'][0]['url']), esc_html__('Your browser does not support HTML5 audio', 'buddypress')); } } /** This hook is documented in /bp-activity/bp-activity-embeds.php */ do_action('bp_activity_embed_after_media'); }
/** * 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 WP_oEmbed} class. * * @param array $attr { * Shortcode attributes. Optional. * * @type int $width Width of the embed in pixels. * @type int $height Height of the embed in pixels. * } * @param string $url The URL attempting to be embedded. * @return string|false The embed HTML on success, otherwise the original URL. * `->maybe_make_link()` can return false on failure. */ public function shortcode($attr, $url = '') { $post = get_post(); if (empty($url) && !empty($attr['src'])) { $url = $attr['src']; } $this->last_url = $url; if (empty($url)) { $this->last_attr = $attr; return ''; } $rawattr = $attr; $attr = wp_parse_args($attr, wp_embed_defaults($url)); $this->last_attr = $attr; // kses converts & into & and we need to undo this // See https://core.trac.wordpress.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))) { /** * Filter the returned embed handler. * * @since 2.9.0 * * @see WP_Embed::shortcode() * * @param mixed $return The shortcode callback function to call. * @param string $url The attempted embed URL. * @param array $attr An array of shortcode attributes. */ 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 WP_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) $key_suffix = md5($url . serialize($attr)); $cachekey = '_oembed_' . $key_suffix; $cachekey_time = '_oembed_time_' . $key_suffix; /** * Filter the oEmbed TTL value (time to live). * * @since 4.0.0 * * @param int $time Time to live (in seconds). * @param string $url The attempted embed URL. * @param array $attr An array of shortcode attributes. * @param int $post_ID Post ID. */ $ttl = apply_filters('oembed_ttl', DAY_IN_SECONDS, $url, $attr, $post_ID); $cache = get_post_meta($post_ID, $cachekey, true); $cache_time = get_post_meta($post_ID, $cachekey_time, true); if (!$cache_time) { $cache_time = 0; } $cached_recently = time() - $cache_time < $ttl; if ($this->usecache || $cached_recently) { // Failures are cached. Serve one if we're using the cache. if ('{{unknown}}' === $cache) { return $this->maybe_make_link($url); } if (!empty($cache)) { /** * Filter the cached oEmbed HTML. * * @since 2.9.0 * * @see WP_Embed::shortcode() * * @param mixed $cache The cached HTML result, stored in post meta. * @param string $url The attempted embed URL. * @param array $attr An array of shortcode attributes. * @param int $post_ID Post ID. */ return apply_filters('embed_oembed_html', $cache, $url, $attr, $post_ID); } } /** * Filter whether to inspect the given URL for discoverable link tags. * * @since 2.9.0 * * @see WP_oEmbed::discover() * * @param bool $enable Whether to enable `<link>` tag discovery. Default false. */ $attr['discover'] = apply_filters('embed_oembed_discover', false) && author_can($post_ID, 'unfiltered_html'); // Use oEmbed to get the HTML $html = wp_oembed_get($url, $attr); // Maybe cache the result if ($html) { update_post_meta($post_ID, $cachekey, $html); update_post_meta($post_ID, $cachekey_time, time()); } elseif (!$cache) { update_post_meta($post_ID, $cachekey, '{{unknown}}'); } // If there was a result, return it if ($html) { /** This filter is documented in wp-includes/class-wp-embed.php */ return apply_filters('embed_oembed_html', $html, $url, $attr, $post_ID); } } // Still unknown return $this->maybe_make_link($url); }
/** * 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 = wp_parse_args($args, wp_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); $provider = apply_filters('oembed_fetch_url', $provider, $url, $args); foreach (array('json', 'xml') as $format) { $result = $this->_fetch_with_format($provider, $format); if (is_wp_error($result) && 'not-implemented' == $result->get_error_code()) { continue; } return $result && !is_wp_error($result) ? $result : false; } return false; }
/** * Via WordPress. * * @since 151125 OEmbed utilities. * * @param string $string String to parse. */ protected function viaWordPress(string $string) : string { if (!$this->c::isWordPress() || !$this->c::canCallFunc('wp_oembed_get') || !$this->c::canCallFunc('wp_embed_defaults')) { throw $this->c::issue('Unable to oEmbed via WordPress.'); } $oembed_args = array_merge(wp_embed_defaults(), ['discover' => false]); $string = preg_replace_callback('/^\\s*(https?:\\/\\/[^\\s"]+)\\s*$/uim', function ($m) use($oembed_args) { $oembed = wp_oembed_get($m[1], $oembed_args); return $oembed ? $oembed : $m[0]; }, $string); return $string; }
/** * Handles video attachments on attachment pages. Add other video types to the <object> element. * * @since 0.2.2 * @param string $mime attachment mime type * @param string $file attachment file URL * @return string */ function hybrid_video_attachment($mime = false, $file = false) { $embed_defaults = wp_embed_defaults(); if ($mime == 'video/asf') { $mime = 'video/x-ms-wmv'; } $video = '<object type="' . esc_attr($mime) . '" class="player video" data="' . esc_url($file) . '" width="' . esc_attr($embed_defaults['width']) . '" height="' . esc_attr($embed_defaults['height']) . '">'; $video .= '<param name="src" value="' . esc_url($file) . '" />'; $video .= '<param name="autoplay" value="false" />'; $video .= '<param name="allowfullscreen" value="true" />'; $video .= '<param name="controller" value="true" />'; $video .= '</object>'; return $video; }
/** * Retrieves embedded videos from the post content. This script only searches for embeds used by * the WordPress embed functionality. * * @since 0.1.0 * @access public * @global object $wp_embed The global WP_Embed object. * @param array $args Arguments for the [embed] shortcode. * @return string */ function post_format_tools_get_video($args = array()) { global $wp_embed; /* If this is not a 'video' post, return. */ if (!has_post_format('video')) { return false; } /* Merge the input arguments and the defaults. */ $args = wp_parse_args($args, wp_embed_defaults()); /* Get the post content. */ $content = get_the_content(); /* Set the default $embed variable to false. */ $embed = false; /* Use WP's built in WP_Embed class methods to handle the dirty work. */ add_filter('post_format_tools_video_shortcode_embed', array($wp_embed, 'run_shortcode')); add_filter('post_format_tools_video_auto_embed', array($wp_embed, 'autoembed')); /* We don't want to return a link when an embed doesn't work. Filter this to return false. */ add_filter('embed_maybe_make_link', '__return_false'); /* Check for matches against the [embed] shortcode. */ preg_match_all('|\\[embed.*?](.*?)\\[/embed\\]|i', $content, $matches, PREG_SET_ORDER); /* If matches were found, loop through them to see if we can hit the jackpot. */ if (is_array($matches)) { foreach ($matches as $value) { /* Apply filters (let WP handle this) to get an embedded video. */ $embed = apply_filters('post_format_tools_video_shortcode_embed', '[embed width="' . absint($args['width']) . '" height="' . absint($args['height']) . '"]' . $value[1] . '[/embed]'); /* If no embed, continue looping through the array of matches. */ if (empty($embed)) { continue; } } } /* If no embed at this point and the user has 'auto embeds' turned on, let's check for URLs in the post. */ if (empty($embed) && get_option('embed_autourls')) { preg_match_all('|^\\s*(https?://[^\\s"]+)\\s*$|im', $content, $matches, PREG_SET_ORDER); /* If URL matches are found, loop through them to see if we can get an embed. */ if (is_array($matches)) { foreach ($matches as $value) { /* Let WP work its magic with the 'autoembed' method. */ $embed = apply_filters('post_format_tools_video_auto_embed', $value[0]); /* If no embed, continue looping through the array of matches. */ if (empty($embed)) { continue; } } } } /* Remove the maybe make link filter. */ remove_filter('embed_maybe_make_link', '__return_false'); /* Return the embed. */ return $embed; }
/** * Returns an array with URLs to players for some embedded media. */ function amt_get_embedded_media($post) { // Post content pre-processing // At this point we give devs the opportunity to inject raw URLs of // supported embeddable media, so that they can be picked up by // the algorithms below. // Array of URLs of supported embeddable media. $external_media_urls = apply_filters('amt_embedded_media_external', array(), $post); // Store post body $post_body = $post->post_content; // Attach the external media URLs to the post content. //$post_body .= sprintf( '\n%s\n', implode('\n', $external_media_urls) ); $post_body .= PHP_EOL . implode(PHP_EOL, $external_media_urls) . PHP_EOL; // Format of the array // Embeds are grouped by type images/videos/sounds // Embedded media are added to any group as an associative array. $embedded_media_urls = array('images' => array(), 'videos' => array(), 'sounds' => array()); // Find Videos // // Keys: // page - URL to a HTML page that contains the object. // player - URL to the player that can be used in an iframe. // thumbnail - URL to a preview image // Youtube // Supported: // - http://www.youtube.com/watch?v=VIDEO_ID //$pattern = '#youtube.com/watch\?v=([-|~_0-9A-Za-z]+)#'; //$pattern = '#http:\/\/(?:www.)?youtube.com\/.*v=(\w*)#i'; $pattern = '#https?:\\/\\/(?:www.)?youtube.com\\/.*v=([a-zA-Z0-9_-]+)#i'; preg_match_all($pattern, $post_body, $matches); //var_dump($matches); if ($matches) { // $matches[0] contains a list of YT video URLS // $matches[1] contains a list of YT video IDs // Add matches to $embedded_media_urls foreach ($matches[0] as $youtube_video_url) { // First we verify that this is an embedded Youtube video and not // one that is just linked. We confirm this by checking if the // relevant oembed custom field has been created. // Get cached HTML data for embedded youtube videos. // Do it like WordPress. // See source code: // - class-wp-embed.php: line 177 [[ $cachekey = '_oembed_' . md5( $url . serialize( $attr ) ); ]] // - media.php: line 1332 [[ function wp_embed_defaults ]] // If no attributes have been used in the [embed] shortcode, $attr is an empty string. $attr = ''; $attr = wp_parse_args($attr, wp_embed_defaults()); $cachekey = '_oembed_' . md5($youtube_video_url . serialize($attr)); $cache = get_post_meta($post->ID, $cachekey, true); //var_dump($cache); if (empty($cache)) { continue; } // Get image info from the cached HTML preg_match('#.*v=([a-zA-Z0-9_-]+)#', $youtube_video_url, $video_url_info); //var_dump($video_url_info); $youtube_video_id = $video_url_info[1]; $item = array('type' => 'youtube', 'page' => 'https://www.youtube.com/watch?v=' . $youtube_video_id, 'player' => 'https://youtube.com/v/' . $youtube_video_id, 'thumbnail' => apply_filters('amt_oembed_youtube_image_preview', 'https://img.youtube.com/vi/' . $youtube_video_id . '/sddefault.jpg', $youtube_video_id), 'width' => apply_filters('amt_oembed_youtube_player_width', '640'), 'height' => apply_filters('amt_oembed_youtube_player_height', '480')); //array_unshift( $embedded_media_urls['videos'], $item ); array_push($embedded_media_urls['videos'], $item); } } // Vimeo // Supported: // - http://vimeo.com/VIDEO_ID // Check output of: http://vimeo.com/api/v2/video/VIDEO_ID.xml // INVALID METHOD: 'thumbnail' => 'https://i.vimeocdn.com/video/' . $vimeo_video_id . '_640.jpg' //$pattern = '#vimeo.com/([-|~_0-9A-Za-z]+)#'; $pattern = '#https?:\\/\\/(?:www.)?vimeo.com\\/(\\d+)#i'; preg_match_all($pattern, $post_body, $matches); //var_dump($matches); if ($matches) { // $matches[0] contains a list of Vimeo video URLS // $matches[1] contains a list of Vimeo video IDs // Add matches to $embedded_media_urls foreach ($matches[0] as $vimeo_video_url) { // First we verify that this is an embedded Vimeo video and not // one that is just linked. We confirm this by checking if the // relevant oembed custom field has been created. // Get cached HTML data for embedded Vimeo videos. // Do it like WordPress. // See source code: // - class-wp-embed.php: line 177 [[ $cachekey = '_oembed_' . md5( $url . serialize( $attr ) ); ]] // - media.php: line 1332 [[ function wp_embed_defaults ]] // If no attributes have been used in the [embed] shortcode, $attr is an empty string. $attr = ''; $attr = wp_parse_args($attr, wp_embed_defaults()); $cachekey = '_oembed_' . md5($vimeo_video_url . serialize($attr)); $cache = get_post_meta($post->ID, $cachekey, true); //var_dump($cache); if (empty($cache)) { continue; } // Get image info from the cached HTML preg_match('#.*vimeo.com\\/(\\d+)#', $vimeo_video_url, $video_url_info); //var_dump($video_url_info); $vimeo_video_id = $video_url_info[1]; $item = array('type' => 'vimeo', 'page' => 'https://vimeo.com/' . $vimeo_video_id, 'player' => 'https://player.vimeo.com/video/' . $vimeo_video_id, 'thumbnail' => apply_filters('amt_oembed_vimeo_image_preview', '', $vimeo_video_id), 'width' => apply_filters('amt_oembed_vimeo_player_width', '640'), 'height' => apply_filters('amt_oembed_vimeo_player_height', '480')); array_push($embedded_media_urls['videos'], $item); } } // Vine // Supported: // - https://vine.co/v/VIDEO_ID // Also check output of: https://vine.co/v/bwBYItOUKrw/card $pattern = '#https?:\\/\\/(?:www.)?vine.co\\/v\\/([a-zA-Z0-9_-]+)#i'; preg_match_all($pattern, $post_body, $matches); //var_dump($matches); if ($matches) { // $matches[0] contains a list of Vimeo video URLS // $matches[1] contains a list of Vimeo video IDs // Add matches to $embedded_media_urls foreach ($matches[0] as $vine_video_url) { // First we verify that this is an embedded Vine video and not // one that is just linked. We confirm this by checking if the // relevant oembed custom field has been created. // Get cached HTML data for embedded Vine videos. // Do it like WordPress. // See source code: // - class-wp-embed.php: line 177 [[ $cachekey = '_oembed_' . md5( $url . serialize( $attr ) ); ]] // - media.php: line 1332 [[ function wp_embed_defaults ]] // If no attributes have been used in the [embed] shortcode, $attr is an empty string. $attr = ''; $attr = wp_parse_args($attr, wp_embed_defaults()); $cachekey = '_oembed_' . md5($vine_video_url . serialize($attr)); $cache = get_post_meta($post->ID, $cachekey, true); //var_dump($cache); if (empty($cache)) { continue; } // Get id info from the cached HTML preg_match('#.*vine.co\\/v\\/([a-zA-Z0-9_-]+)#', $vine_video_url, $video_url_info); //var_dump($video_url_info); $vine_video_id = $video_url_info[1]; $item = array('type' => 'vine', 'page' => 'https://vine.co/v/' . $vine_video_id, 'player' => 'https://vine.co/v/' . $vine_video_id . '/embed/simple', 'thumbnail' => apply_filters('amt_oembed_vine_image_preview', '', $vine_video_id), 'width' => apply_filters('amt_oembed_vine_player_width', '600'), 'height' => apply_filters('amt_oembed_vine_player_height', '600')); array_push($embedded_media_urls['videos'], $item); } } // Find Sounds // // Keys: // page - URL to a HTML page that contains the object. // player - URL to the player that can be used in an iframe. // thumbnail - URL to a preview image -= ALWAYS EMPTY, but needed for the player twitter card. // Soundcloud // Supported: // - https://soundcloud.com/USER_ID/TRACK_ID // player: // https://w.soundcloud.com/player/?url=https://api.soundcloud.com/tracks/117455833 $pattern = '#https?:\\/\\/(?:www.)?soundcloud.com\\/[^/]+\\/[a-zA-Z0-9_-]+#i'; preg_match_all($pattern, $post_body, $matches); //var_dump($matches); if ($matches) { // $matches[0] contains a list of Soundcloud URLS // Add matches to $embedded_media_urls foreach ($matches[0] as $soundcloud_url) { // First we verify that this is an embedded Soundcloud audio and not // one that is just linked. We confirm this by checking if the // relevant oembed custom field has been created. // Get cached HTML data for embedded Soundcloud audios. // Do it like WordPress. // See source code: // - class-wp-embed.php: line 177 [[ $cachekey = '_oembed_' . md5( $url . serialize( $attr ) ); ]] // - media.php: line 1332 [[ function wp_embed_defaults ]] // If no attributes have been used in the [embed] shortcode, $attr is an empty string. $attr = ''; $attr = wp_parse_args($attr, wp_embed_defaults()); $cachekey = '_oembed_' . md5($soundcloud_url . serialize($attr)); $cache = get_post_meta($post->ID, $cachekey, true); //var_dump($cache); if (empty($cache)) { continue; } $item = array('type' => 'soundcloud', 'page' => $soundcloud_url, 'player' => 'https://w.soundcloud.com/player/?url=' . $soundcloud_url, 'thumbnail' => apply_filters('amt_oembed_soundcloud_image_preview', '', $soundcloud_url), 'width' => apply_filters('amt_oembed_soundcloud_player_width', '640'), 'height' => apply_filters('amt_oembed_soundcloud_player_height', '164')); array_push($embedded_media_urls['sounds'], $item); } } // Find Images // // Keys: // page - URL to a HTML page that contains the object. // player - URL to the player that can be used in an iframe. // thumbnail - URL to thumbnail // image - URL to image // alt - alt text // width - image width // height - image height // Flickr // // Supported: // Embedded URLs MUST be of Format: http://www.flickr.com/photos/USER_ID/IMAGE_ID/ // // Sizes: // t - Thumbnail (100x) // q - Square 150 (150x150) // s - Small 240 (140x) // n - Small 320 (320x) // m - Medium 500 (500x) // z - Medium 640 (640x) // c - Large 800 (800x) // b - Large 900 (900x) // l - Large 1024 (1024x) DOES NOT WORK // h - High 1600 (1600x) DOES NOT WORK // $pattern = '#https?:\\/\\/(?:www.)?flickr.com\\/photos\\/[^\\/]+\\/[^\\/]+\\/#i'; //$pattern = '#https?://(?:www.)?flickr.com/photos/[^/]+/[^/]+/#i'; preg_match_all($pattern, $post_body, $matches); //var_dump($matches); if ($matches) { // $matches[0] contains a list of Flickr image page URLS // Add matches to $embedded_media_urls foreach ($matches[0] as $flick_page_url) { // Get cached HTML data for embedded images. // Do it like WordPress. // See source code: // - class-wp-embed.php: line 177 [[ $cachekey = '_oembed_' . md5( $url . serialize( $attr ) ); ]] // - media.php: line 1332 [[ function wp_embed_defaults ]] // If no attributes have been used in the [embed] shortcode, $attr is an empty string. $attr = ''; $attr = wp_parse_args($attr, wp_embed_defaults()); $cachekey = '_oembed_' . md5($flick_page_url . serialize($attr)); $cache = get_post_meta($post->ID, $cachekey, true); //var_dump($cache); if (empty($cache)) { continue; } // Get image info from the cached HTML preg_match('#<img src="([^"]+)" alt="([^"]+)" width="([\\d]+)" height="([\\d]+)" \\/>#i', $cache, $img_info); //var_dump($img_info); if (!empty($img_info)) { $item = array('type' => 'flickr', 'page' => $flick_page_url, 'player' => $flick_page_url . 'lightbox/', 'thumbnail' => str_replace('z.jpg', 'q.jpg', $img_info[1]), 'image' => $img_info[1], 'alt' => $img_info[2], 'width' => $img_info[3], 'height' => $img_info[4]); array_push($embedded_media_urls['images'], $item); } } } /** // Instagram // // Supported: // Embedded URLs MUST be of Format: https://instagram.com/p/IMAGE_ID/ // $pattern = '#https?:\/\/(?:www.)?instagram.com\/p\/[^\/]+\/#i'; preg_match_all( $pattern, $post_body, $matches ); //var_dump($matches); if ($matches) { // $matches[0] contains a list of Flickr image page URLS // Add matches to $embedded_media_urls foreach( $matches[0] as $instagram_page_url ) { // Get cached HTML data for embedded images. // Do it like WordPress. // See source code: // - class-wp-embed.php: line 177 [[ $cachekey = '_oembed_' . md5( $url . serialize( $attr ) ); ]] // - media.php: line 1332 [[ function wp_embed_defaults ]] // If no attributes have been used in the [embed] shortcode, $attr is an empty string. $attr = ''; $attr = wp_parse_args( $attr, wp_embed_defaults() ); $cachekey = '_oembed_' . md5( $instagram_page_url . serialize( $attr ) ); $cache = get_post_meta( $post->ID, $cachekey, true ); var_dump($cache); // Get image info from the cached HTML preg_match( '#target="_top">(.*)<\/a>#i', $cache, $img_info ); //var_dump($img_info); if ( ! empty( $img_info ) ) { $item = array( 'page' => $instagram_page_url, 'player' => $instagram_page_url . 'lightbox/', 'thumbnail' => str_replace( 'z.jpg', 'q.jpg', $img_info[1] ), // size q BEFORE CHANGING this check if the 150x150 is hardcoded into any metadata generator. It is in Twitter cards. 'image' => $img_info[1], // size z 'alt' => $img_info[1], 'width' => '640', 'height' => '640', ); array_unshift( $embedded_media_urls['images'], $item ); } } } */ // Allow filtering of the embedded media array $embedded_media_urls = apply_filters('amt_embedded_media', $embedded_media_urls, $post->ID); //var_dump($embedded_media_urls); return $embedded_media_urls; }
function galleria_galleria_css_head() { $galleria_galleria = get_option('galleria_galleria'); $color = $galleria_galleria['color']; $wp_default_sizes = wp_embed_defaults(); $height = $galleria_galleria['height'] ? $galleria_galleria['height'] : $wp_default_sizes['height']; $width = $galleria_galleria['width'] ? $galleria_galleria['width'] : $wp_default_sizes['width']; ?> <script type="text/javascript"> document.documentElement.className += ' galleria-galleria-active'; </script> <?php echo "<style type='text/css'>\r\n\t.galleria-gallery{ width: {$width}px; height: {$height}px;}\r\n\t.galleria-container{background-color:{$color}; }\r\n\t.galleria-galleria-active .galleria-gallery .gallery {display:none;} \r\n\t.galleria-galleria-active .galleria-gallery{background-color:{$color}; }\r\n\t</style>"; }