コード例 #1
0
 static function get($post_id, $blog_id = 0, $args = array())
 {
     $defaults = array('max_words' => 16, 'max_chars' => 256);
     $args = wp_parse_args($args, $defaults);
     $switched = false;
     if (!empty($blog_id) && $blog_id != get_current_blog_id() && function_exists('switch_to_blog')) {
         switch_to_blog($blog_id);
         $switched = true;
     } else {
         $blog_id = get_current_blog_id();
     }
     $post = get_post($post_id);
     $permalink = get_permalink($post_id);
     $return = array('type' => 'standard', 'permalink' => $permalink, 'image' => '', 'excerpt' => '', 'word_count' => 0, 'secure' => array('image' => ''), 'count' => array('image' => 0, 'video' => 0, 'word' => 0, 'link' => 0));
     if (empty($post->post_password)) {
         $return['excerpt'] = self::get_excerpt($post->post_content, $post->post_excerpt, $args['max_words'], $args['max_chars']);
         $return['count']['word'] = self::get_word_count($post->post_content);
         $return['count']['word_remaining'] = self::get_word_remaining_count($post->post_content, $return['excerpt']);
         $return['count']['link'] = self::get_link_count($post->post_content);
     }
     $extract = Jetpack_Media_Meta_Extractor::extract($blog_id, $post_id, Jetpack_Media_Meta_Extractor::ALL);
     if (empty($extract['has'])) {
         return $return;
     }
     // Prioritize [some] video embeds
     if (!empty($extract['has']['shortcode'])) {
         foreach ($extract['shortcode'] as $type => $data) {
             switch ($type) {
                 case 'wpvideo':
                     if (0 == $return['count']['video']) {
                         $return['type'] = 'video';
                         $return['video'] = esc_url_raw('http://s0.videopress.com/player.swf?guid=' . $extract['shortcode']['wpvideo']['id'][0] . '&isDynamicSeeking=true');
                         $return['image'] = self::get_video_poster('videopress', $extract['shortcode']['wpvideo']['id'][0]);
                         $return['secure']['video'] = preg_replace('@http://[^\\.]+.videopress.com/@', 'https://v0.wordpress.com/', $return['video']);
                         $return['secure']['image'] = str_replace('http://videos.videopress.com', 'https://videos.files.wordpress.com', $return['image']);
                     }
                     $return['count']['video']++;
                     break;
                 case 'youtube':
                     if (0 == $return['count']['video']) {
                         $return['type'] = 'video';
                         $return['video'] = esc_url_raw('http://www.youtube.com/watch?feature=player_embedded&v=' . $extract['shortcode']['youtube']['id'][0]);
                         $return['image'] = self::get_video_poster('youtube', $extract['shortcode']['youtube']['id'][0]);
                         $return['secure']['video'] = self::https($return['video']);
                         $return['secure']['image'] = self::https($return['image']);
                     }
                     $return['count']['video']++;
                     break;
                 case 'vimeo':
                     if (0 == $return['count']['video']) {
                         $return['type'] = 'video';
                         $return['video'] = esc_url_raw('http://vimeo.com/' . $extract['shortcode']['vimeo']['id'][0]);
                         $return['secure']['video'] = self::https($return['video']);
                         $poster_image = get_post_meta($post_id, 'vimeo_poster_image', true);
                         if (!empty($poster_image)) {
                             $return['image'] = $poster_image;
                             $poster_url_parts = parse_url($poster_image);
                             $return['secure']['image'] = 'https://secure-a.vimeocdn.com' . $poster_url_parts['path'];
                         }
                     }
                     $return['count']['video']++;
                     break;
             }
         }
     }
     if (!empty($extract['has']['embed'])) {
         foreach ($extract['embed']['url'] as $embed) {
             if (preg_match('/((youtube|vimeo|dailymotion)\\.com|youtu.be)/', $embed)) {
                 if (0 == $return['count']['video']) {
                     $return['type'] = 'video';
                     $return['video'] = 'http://' . $embed;
                     $return['secure']['video'] = self::https($return['video']);
                     if (false !== strpos($embed, 'youtube')) {
                         $return['image'] = self::get_video_poster('youtube', jetpack_get_youtube_id($return['video']));
                         $return['secure']['image'] = self::https($return['image']);
                     } else {
                         if (false !== strpos($embed, 'youtu.be')) {
                             $youtube_id = jetpack_get_youtube_id($return['video']);
                             $return['video'] = 'http://youtube.com/watch?v=' . $youtube_id . '&feature=youtu.be';
                             $return['secure']['video'] = self::https($return['video']);
                             $return['image'] = self::get_video_poster('youtube', jetpack_get_youtube_id($return['video']));
                             $return['secure']['image'] = self::https($return['image']);
                         } else {
                             if (false !== strpos($embed, 'vimeo')) {
                                 $poster_image = get_post_meta($post_id, 'vimeo_poster_image', true);
                                 if (!empty($poster_image)) {
                                     $return['image'] = $poster_image;
                                     $poster_url_parts = parse_url($poster_image);
                                     $return['secure']['image'] = 'https://secure-a.vimeocdn.com' . $poster_url_parts['path'];
                                 }
                             } else {
                                 if (false !== strpos($embed, 'dailymotion')) {
                                     $return['image'] = str_replace('dailymotion.com/video/', 'dailymotion.com/thumbnail/video/', $embed);
                                     $return['image'] = parse_url($return['image'], PHP_URL_SCHEME) === null ? 'http://' . $return['image'] : $return['image'];
                                     $return['secure']['image'] = self::https($return['image']);
                                 }
                             }
                         }
                     }
                 }
                 $return['count']['video']++;
             }
         }
     }
     // Do we really want to make the video the primary focus of the post?
     if ('video' == $return['type']) {
         $content = wpautop(strip_tags($post->post_content));
         $paragraphs = explode('</p>', $content);
         $number_of_paragraphs = 0;
         foreach ($paragraphs as $i => $paragraph) {
             // Don't include blank lines as a paragraph
             if ('' == trim($paragraph)) {
                 unset($paragraphs[$i]);
                 continue;
             }
             $number_of_paragraphs++;
         }
         $number_of_paragraphs = $number_of_paragraphs - $return['count']['video'];
         // subtract amount for videos..
         // More than 2 paragraph? The video is not the primary focus so we can do some more analysis
         if ($number_of_paragraphs > 2) {
             $return['type'] = 'standard';
         }
     }
     // If we don't have any prioritized embed...
     if ('standard' == $return['type']) {
         if ((!empty($extract['has']['gallery']) || !empty($extract['shortcode']['gallery']['count'])) && !empty($extract['image'])) {
             //... Then we prioritize galleries first (multiple images returned)
             $return['type'] = 'gallery';
             $return['images'] = $extract['image'];
             foreach ($return['images'] as $image) {
                 $return['secure']['images'][] = array('url' => self::ssl_img($image['url']));
                 $return['count']['image']++;
             }
         } else {
             if (!empty($extract['has']['image'])) {
                 // ... Or we try and select a single image that would make sense
                 $content = wpautop(strip_tags($post->post_content));
                 $paragraphs = explode('</p>', $content);
                 $number_of_paragraphs = 0;
                 foreach ($paragraphs as $i => $paragraph) {
                     // Don't include 'actual' captions as a paragraph
                     if (false !== strpos($paragraph, '[caption')) {
                         unset($paragraphs[$i]);
                         continue;
                     }
                     // Don't include blank lines as a paragraph
                     if ('' == trim($paragraph)) {
                         unset($paragraphs[$i]);
                         continue;
                     }
                     $number_of_paragraphs++;
                 }
                 $return['image'] = $extract['image'][0]['url'];
                 $return['secure']['image'] = self::ssl_img($return['image']);
                 $return['count']['image']++;
                 if ($number_of_paragraphs <= 2 && 1 == count($extract['image'])) {
                     // If we have lots of text or images, let's not treat it as an image post, but return its first image
                     $return['type'] = 'image';
                 }
             }
         }
     }
     if ($switched) {
         restore_current_blog();
     }
     return $return;
 }
コード例 #2
0
 public static function extract_images_from_content($content)
 {
     $image_list = Jetpack_Media_Meta_Extractor::get_images_from_html($post->post_content, $image_list);
     return Jetpack_Media_Meta_Extractor::build_image_struct($image_list);
 }
コード例 #3
0
 /**
  * @param $post A post object
  * @param $args (array) Optional args, see defaults list for details
  * @returns array Returns an array of all images meeting the specified criteria in $args
  *
  * Uses Jetpack Post Images
  */
 private static function get_image_fields($post, $args = array())
 {
     $defaults = array('width' => 200, 'height' => 200);
     $args = wp_parse_args($args, $defaults);
     $image_list = array();
     $image_booleans = array();
     $image_booleans['gallery'] = 0;
     $from_slideshow = Jetpack_PostImages::from_slideshow($post->ID, $args['width'], $args['height']);
     if (!empty($from_slideshow)) {
         $srcs = wp_list_pluck($from_slideshow, 'src');
         $image_list = array_merge($image_list, $srcs);
     }
     $from_gallery = Jetpack_PostImages::from_gallery($post->ID);
     if (!empty($from_gallery)) {
         $srcs = wp_list_pluck($from_gallery, 'src');
         $image_list = array_merge($image_list, $srcs);
         $image_booleans['gallery']++;
         // @todo This count isn't correct, will only every count 1
     }
     // @todo Can we check width/height of these efficiently?  Could maybe use query args at least, before we strip them out
     $image_list = Jetpack_Media_Meta_Extractor::get_images_from_html($post->post_content, $image_list);
     if (!empty($image_list)) {
         $retval = array('image' => array());
         $unique_imgs = array_unique($image_list);
         foreach ($image_list as $img) {
             $retval['image'][] = array('url' => $img);
         }
         $image_booleans['image'] = count($retval['image']);
         if (!empty($image_booleans)) {
             $retval['has'] = $image_booleans;
         }
         return $retval;
     } else {
         return array();
     }
 }
コード例 #4
0
    /**
     * @author scotchfield
     * @covers Jetpack_Media_Meta_Extractor::extract
     * @since 3.2
     */
    public function test_mediaextractor_exclude_video_links()
    {
        $post_id = $this->factory->post->create(array('post_author' => '23314024', 'post_date' => '2013-10-25 16:43:34', 'post_date_gmt' => '2013-10-25 16:43:34', 'post_content' => 'Sed dapibus ut mauris imperdiet volutpat. http://vimeo.com/77120044/ Nullam in dolor vel nulla pulvinar accumsan facilisis quis lorem.
			', 'post_title' => 'Sed dapibus ut mauris imperdiet volutpat http vimeo...', 'post_excerpt' => '', 'post_status' => 'publish', 'comment_status' => 'open', 'ping_status' => 'open', 'post_password' => '', 'post_name' => 'sed-dapibus-ut-mauris-imperdiet-volutpat-http-vimeo', 'to_ping' => '', 'pinged' => '
			http://vimeo.com/77120044/', 'post_modified' => '2013-10-28 22:54:50', 'post_modified_gmt' => '2013-10-28 22:54:50', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => 'http://breakmyblog.wordpress.com/2013/10/25/sed-dapibus-ut-mauris-imperdiet-volutpat-http-vimeo/', 'menu_order' => 0, 'post_type' => 'post', 'post_mime_type' => '', 'comment_count' => '0', 'filter' => 'raw'));
        $expected = array('has' => array('link' => 1), 'link' => array(array('url' => 'vimeo.com/77120044/', 'host_reversed' => 'com.vimeo', 'host' => 'vimeo.com')));
        $result = Jetpack_Media_Meta_Extractor::extract(get_current_blog_id(), $post_id, Jetpack_Media_Meta_Extractor::ALL);
        $this->assertEquals($expected, $result);
    }
コード例 #5
0
 static function get($post_id, $blog_id = 0, $args = array())
 {
     $defaults = array('max_words' => 16, 'max_chars' => 256);
     $args = wp_parse_args($args, $defaults);
     $switched = false;
     if (!empty($blog_id) && $blog_id != get_current_blog_id() && function_exists('switch_to_blog')) {
         switch_to_blog($blog_id);
         $switched = true;
     } else {
         $blog_id = get_current_blog_id();
     }
     if (!class_exists('Jetpack_Media_Meta_Extractor')) {
         jetpack_require_lib('class.media-extractor');
     }
     $post = get_post($post_id);
     $permalink = get_permalink($post_id);
     $return = array('type' => 'standard', 'permalink' => $permalink, 'image' => '', 'excerpt' => '', 'word_count' => 0, 'secure' => array('image' => ''), 'count' => array('image' => 0, 'video' => 0, 'word' => 0, 'link' => 0));
     if (empty($post->post_password)) {
         $return['excerpt'] = self::get_excerpt($post->post_content, $post->post_excerpt, $args['max_words'], $args['max_chars']);
         $return['count']['word'] = self::get_word_count($post->post_content);
         $return['count']['word_remaining'] = self::get_word_remaining_count($post->post_content, $return['excerpt']);
         $return['count']['link'] = self::get_link_count($post->post_content);
     }
     $extract = Jetpack_Media_Meta_Extractor::extract($blog_id, $post_id, Jetpack_Media_Meta_Extractor::ALL);
     if (empty($extract['has'])) {
         return $return;
     }
     // Prioritize [some] video embeds
     if (!empty($extract['has']['shortcode'])) {
         foreach ($extract['shortcode'] as $type => $data) {
             switch ($type) {
                 case 'videopress':
                 case 'wpvideo':
                     if (0 == $return['count']['video']) {
                         // If there is no id on the video, then let's just skip this
                         if (!isset($data['id'][0])) {
                             continue;
                         }
                         $guid = $data['id'][0];
                         $video_info = videopress_get_video_details($guid);
                         // Only add the video tags if the guid returns a valid videopress object.
                         if ($video_info instanceof stdClass) {
                             // Continue early if we can't find a Video slug.
                             if (empty($video_info->files->std->mp4)) {
                                 continue;
                             }
                             $url = sprintf('https://videos.files.wordpress.com/%1$s/%2$s', $guid, $video_info->files->std->mp4);
                             $thumbnail = $video_info->poster;
                             if (!empty($thumbnail)) {
                                 $return['image'] = $thumbnail;
                                 $return['secure']['image'] = $thumbnail;
                             }
                             $return['type'] = 'video';
                             $return['video'] = esc_url_raw($url);
                             $return['video_type'] = 'video/mp4';
                             $return['secure']['video'] = $return['video'];
                         }
                     }
                     $return['count']['video']++;
                     break;
                 case 'youtube':
                     if (0 == $return['count']['video']) {
                         $return['type'] = 'video';
                         $return['video'] = esc_url_raw('http://www.youtube.com/watch?feature=player_embedded&v=' . $extract['shortcode']['youtube']['id'][0]);
                         $return['image'] = self::get_video_poster('youtube', $extract['shortcode']['youtube']['id'][0]);
                         $return['secure']['video'] = self::https($return['video']);
                         $return['secure']['image'] = self::https($return['image']);
                     }
                     $return['count']['video']++;
                     break;
                 case 'vimeo':
                     if (0 == $return['count']['video']) {
                         $return['type'] = 'video';
                         $return['video'] = esc_url_raw('http://vimeo.com/' . $extract['shortcode']['vimeo']['id'][0]);
                         $return['secure']['video'] = self::https($return['video']);
                         $poster_image = get_post_meta($post_id, 'vimeo_poster_image', true);
                         if (!empty($poster_image)) {
                             $return['image'] = $poster_image;
                             $poster_url_parts = parse_url($poster_image);
                             $return['secure']['image'] = 'https://secure-a.vimeocdn.com' . $poster_url_parts['path'];
                         }
                     }
                     $return['count']['video']++;
                     break;
             }
         }
     }
     if (!empty($extract['has']['embed'])) {
         foreach ($extract['embed']['url'] as $embed) {
             if (preg_match('/((youtube|vimeo|dailymotion)\\.com|youtu.be)/', $embed)) {
                 if (0 == $return['count']['video']) {
                     $return['type'] = 'video';
                     $return['video'] = 'http://' . $embed;
                     $return['secure']['video'] = self::https($return['video']);
                     if (false !== strpos($embed, 'youtube')) {
                         $return['image'] = self::get_video_poster('youtube', jetpack_get_youtube_id($return['video']));
                         $return['secure']['image'] = self::https($return['image']);
                     } else {
                         if (false !== strpos($embed, 'youtu.be')) {
                             $youtube_id = jetpack_get_youtube_id($return['video']);
                             $return['video'] = 'http://youtube.com/watch?v=' . $youtube_id . '&feature=youtu.be';
                             $return['secure']['video'] = self::https($return['video']);
                             $return['image'] = self::get_video_poster('youtube', jetpack_get_youtube_id($return['video']));
                             $return['secure']['image'] = self::https($return['image']);
                         } else {
                             if (false !== strpos($embed, 'vimeo')) {
                                 $poster_image = get_post_meta($post_id, 'vimeo_poster_image', true);
                                 if (!empty($poster_image)) {
                                     $return['image'] = $poster_image;
                                     $poster_url_parts = parse_url($poster_image);
                                     $return['secure']['image'] = 'https://secure-a.vimeocdn.com' . $poster_url_parts['path'];
                                 }
                             } else {
                                 if (false !== strpos($embed, 'dailymotion')) {
                                     $return['image'] = str_replace('dailymotion.com/video/', 'dailymotion.com/thumbnail/video/', $embed);
                                     $return['image'] = parse_url($return['image'], PHP_URL_SCHEME) === null ? 'http://' . $return['image'] : $return['image'];
                                     $return['secure']['image'] = self::https($return['image']);
                                 }
                             }
                         }
                     }
                 }
                 $return['count']['video']++;
             }
         }
     }
     // Do we really want to make the video the primary focus of the post?
     if ('video' == $return['type']) {
         $content = wpautop(strip_tags($post->post_content));
         $paragraphs = explode('</p>', $content);
         $number_of_paragraphs = 0;
         foreach ($paragraphs as $i => $paragraph) {
             // Don't include blank lines as a paragraph
             if ('' == trim($paragraph)) {
                 unset($paragraphs[$i]);
                 continue;
             }
             $number_of_paragraphs++;
         }
         $number_of_paragraphs = $number_of_paragraphs - $return['count']['video'];
         // subtract amount for videos..
         // More than 2 paragraph? The video is not the primary focus so we can do some more analysis
         if ($number_of_paragraphs > 2) {
             $return['type'] = 'standard';
         }
     }
     // If we don't have any prioritized embed...
     if ('standard' == $return['type']) {
         if ((!empty($extract['has']['gallery']) || !empty($extract['shortcode']['gallery']['count'])) && !empty($extract['image'])) {
             //... Then we prioritize galleries first (multiple images returned)
             $return['type'] = 'gallery';
             $return['images'] = $extract['image'];
             foreach ($return['images'] as $image) {
                 $return['secure']['images'][] = array('url' => self::ssl_img($image['url']));
                 $return['count']['image']++;
             }
         } else {
             if (!empty($extract['has']['image'])) {
                 // ... Or we try and select a single image that would make sense
                 $content = wpautop(strip_tags($post->post_content));
                 $paragraphs = explode('</p>', $content);
                 $number_of_paragraphs = 0;
                 foreach ($paragraphs as $i => $paragraph) {
                     // Don't include 'actual' captions as a paragraph
                     if (false !== strpos($paragraph, '[caption')) {
                         unset($paragraphs[$i]);
                         continue;
                     }
                     // Don't include blank lines as a paragraph
                     if ('' == trim($paragraph)) {
                         unset($paragraphs[$i]);
                         continue;
                     }
                     $number_of_paragraphs++;
                 }
                 $return['image'] = $extract['image'][0]['url'];
                 $return['secure']['image'] = self::ssl_img($return['image']);
                 $return['count']['image']++;
                 if ($number_of_paragraphs <= 2 && 1 == count($extract['image'])) {
                     // If we have lots of text or images, let's not treat it as an image post, but return its first image
                     $return['type'] = 'image';
                 }
             }
         }
     }
     if ($switched) {
         restore_current_blog();
     }
     /**
      * Allow a theme or plugin to inspect and ultimately change the media summary.
      *
      * @since 4.4.0
      *
      * @param array $data The calculated media summary data.
      * @param int $post_id The id of the post this data applies to.
      */
     $return = apply_filters('jetpack_media_summary_output', $return, $post_id);
     return $return;
 }