/**
  * Retrieves oEmbed from url/object ID
  * @since  0.9.5
  * @param  string $url       URL to retrieve oEmbed
  * @param  int    $object_id Object ID
  * @param  array  $args      Arguments for method
  * @return string            html markup with embed or fallback
  */
 public function get_oembed($url, $object_id, $args = array())
 {
     global $wp_embed;
     $oembed_url = esc_url($url);
     self::$object_id = absint($object_id);
     $args = self::$embed_args = wp_parse_args($args, array('object_type' => 'post', 'oembed_args' => self::$embed_args, 'field_id' => false));
     // set the post_ID so oEmbed won't fail
     $wp_embed->post_ID = self::$object_id;
     // Special scenario if NOT a post object
     if (isset($args['object_type']) && $args['object_type'] != 'post') {
         // Ok, we need to hijack the oembed cache system
         self::$hijack = true;
         self::$object_type = $args['object_type'];
         // Gets ombed cache from our object's meta (vs postmeta)
         add_filter('get_post_metadata', array('cmb_Meta_Box_ajax', 'hijack_oembed_cache_get'), 10, 3);
         // Sets ombed cache in our object's meta (vs postmeta)
         add_filter('update_post_metadata', array('cmb_Meta_Box_ajax', 'hijack_oembed_cache_set'), 10, 4);
     }
     $embed_args = '';
     foreach ($args['oembed_args'] as $key => $val) {
         $embed_args .= " {$key}=\"{$val}\"";
     }
     // ping WordPress for an embed
     $check_embed = $wp_embed->run_shortcode('[embed' . $embed_args . ']' . $oembed_url . '[/embed]');
     // fallback that WordPress creates when no oEmbed was found
     $fallback = $wp_embed->maybe_make_link($oembed_url);
     // Send back our embed
     if ($check_embed && $check_embed != $fallback) {
         return '<div class="embed_status">' . $check_embed . '<p><a href="#" class="cmb_remove_file_button" rel="' . $args['field_id'] . '">' . __('Remove Embed', 'cmb') . '</a></p></div>';
     }
     // Otherwise, send back error info that no oEmbeds were found
     return '<p class="ui-state-error-text">' . sprintf(__('No oEmbed Results Found for %s. View more info at', 'cmb'), $fallback) . ' <a href="http://codex.wordpress.org/Embeds" target="_blank">codex.wordpress.org/Embeds</a>.</p>';
 }
Example #2
0
 /**
  * Retrieves oEmbed from url/object ID
  * @since  0.9.5
  * @param  string $url       URL to retrieve oEmbed
  * @param  int    $object_id Object ID
  * @param  array  $args      Arguments for method
  * @return string            html markup with embed or fallback
  */
 public static function get_oembed($url, $object_id, $args = array())
 {
     global $wp_embed;
     $oembed_url = esc_url($url);
     // Sanitize object_id
     self::$object_id = is_numeric($object_id) ? absint($object_id) : sanitize_text_field($object_id);
     $args = wp_parse_args($args, array('object_type' => 'post', 'oembed_args' => self::$embed_args, 'field_id' => false, 'cache_key' => false));
     self::$embed_args =& $args;
     // set the post_ID so oEmbed won't fail
     // wp-includes/class-wp-embed.php, WP_Embed::shortcode(), line 162
     $wp_embed->post_ID = self::$object_id;
     // Special scenario if NOT a post object
     if (isset($args['object_type']) && $args['object_type'] != 'post') {
         if ('options-page' == $args['object_type']) {
             // bogus id to pass some numeric checks
             // Issue with a VERY large WP install?
             $wp_embed->post_ID = 1987645321;
             // Use our own cache key to correspond to this field (vs one cache key per url)
             $args['cache_key'] = $args['field_id'] . '_cache';
         }
         // Ok, we need to hijack the oembed cache system
         self::$hijack = true;
         self::$object_type = $args['object_type'];
         // Gets ombed cache from our object's meta (vs postmeta)
         add_filter('get_post_metadata', array('cmb_Meta_Box_ajax', 'hijack_oembed_cache_get'), 10, 3);
         // Sets ombed cache in our object's meta (vs postmeta)
         add_filter('update_post_metadata', array('cmb_Meta_Box_ajax', 'hijack_oembed_cache_set'), 10, 4);
     }
     $embed_args = '';
     foreach ($args['oembed_args'] as $key => $val) {
         $embed_args .= " {$key}=\"{$val}\"";
     }
     // ping WordPress for an embed
     $check_embed = $wp_embed->run_shortcode('[embed' . $embed_args . ']' . $oembed_url . '[/embed]');
     // fallback that WordPress creates when no oEmbed was found
     $fallback = $wp_embed->maybe_make_link($oembed_url);
     // Send back our embed
     if ($check_embed && $check_embed != $fallback) {
         return '<div class="embed_status">' . $check_embed . '<p class="cmb_remove_wrapper"><a href="#" class="cmb_remove_file_button" rel="' . $args['field_id'] . '">' . __('Remove Embed', 'cmb') . '</a></p></div>';
     }
     // Otherwise, send back error info that no oEmbeds were found
     return '<p class="ui-state-error-text">' . sprintf(__('No oEmbed Results Found for %s. View more info at', 'cmb'), $fallback) . ' <a href="http://codex.wordpress.org/Embeds" target="_blank">codex.wordpress.org/Embeds</a>.</p>';
 }