Includes a few different methods, all of which return a similar-format array containing details of any images found. Everything can (should) be called statically, it's just a function-bucket. You can also call Jetpack_PostImages::get_image() to cycle through all of the methods until one of them finds something useful. This file is included verbatim in Jetpack
 /**
  * @author scotchfield
  * @covers Jetpack_PostImages::from_attachment
  * @since 3.2
  */
 public function test_from_attachment_is_correct_array()
 {
     $img_name = 'image.jpg';
     $img_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $img_name;
     $img_html = '<img src="' . $img_url . '"/>';
     $img_dimensions = array('width' => 250, 'height' => 250);
     $post_id = $this->factory->post->create(array('post_content' => $img_html));
     $attachment_id = $this->factory->attachment->create_object($img_name, $post_id, array('post_mime_type' => 'image/jpeg', 'post_type' => 'attachment'));
     wp_update_attachment_metadata($attachment_id, $img_dimensions);
     $images = Jetpack_PostImages::from_attachment($post_id);
     $this->assertEquals(count($images), 1);
     $this->assertEquals($images[0]['src'], $img_url);
 }
/**
 * Get an image from a post
 *
 * @uses Jetpack_PostImages::get_image( $post_id ) to get the source of an image in a post, apply_filters()
 *
 * @since 1.0
 *
 * @return string $the_image the image source
 */
function colorposts_get_post_image()
{
    $post_id = get_the_ID();
    if (class_exists('Jetpack_PostImages')) {
        $the_image = Jetpack_PostImages::get_image($post_id);
        if (!empty($the_image['src'])) {
            $the_image = $the_image['src'];
        } else {
            $the_image = apply_filters('jetpack_open_graph_image_default', "http://wordpress.com/i/blank.jpg");
        }
    }
    $the_image = apply_filters('colorposts_image_output', $the_image);
    return esc_url($the_image);
}
Beispiel #3
0
function wpcom_amp_add_image_to_metadata($metadata, $post)
{
    if (!class_exists('Jetpack_PostImages')) {
        return wpcom_amp_add_fallback_image_to_metadata($metadata);
    }
    $image = Jetpack_PostImages::get_image($post->ID, array('fallback_to_avatars' => true, 'avatar_size' => 200, 'from_thumbnail' => false, 'from_attachment' => false));
    if (empty($image)) {
        return wpcom_amp_add_fallback_image_to_metadata($metadata);
    }
    if (!isset($image['src_width'])) {
        $dimensions = wpcom_amp_getimagesize($image['src']);
        if ($dimensions) {
            $image['src_width'] = $dimensions[0];
            $image['src_height'] = $dimensions[1];
        }
    }
    $metadata['image'] = array('@type' => 'ImageObject', 'url' => $image['src'], 'width' => $image['src_width'], 'height' => $image['src_height']);
    return $metadata;
}
 /**
  *
  * @param string $html Some markup, possibly containing image tags
  * @param array $images_already_extracted (just an array of image URLs without query strings, no special structure), used for de-duplication
  * @return array Image URLs extracted from the HTML, stripped of query params and de-duped
  */
 public static function get_images_from_html($html, $images_already_extracted)
 {
     $image_list = $images_already_extracted;
     $from_html = Jetpack_PostImages::from_html($html);
     if (!empty($from_html)) {
         $srcs = wp_list_pluck($from_html, 'src');
         foreach ($srcs as $image_url) {
             if (($src = parse_url($image_url)) && isset($src['scheme'], $src['host'], $src['path'])) {
                 // Rebuild the URL without the query string
                 $queryless = $src['scheme'] . '://' . $src['host'] . $src['path'];
             } elseif ($length = strpos($image_url, '?')) {
                 // If parse_url() didn't work, strip off theh query string the old fashioned way
                 $queryless = substr($image_url, 0, $length);
             } else {
                 // Failing that, there was no spoon! Err ... query string!
                 $queryless = $image_url;
             }
             if (!in_array($queryless, $image_list)) {
                 $image_list[] = $queryless;
             }
         }
     }
     return $image_list;
 }
Beispiel #5
0
    function widget($args, $instance)
    {
        $title = isset($instance['title']) ? $instance['title'] : false;
        if (false === $title) {
            $title = $this->default_title;
        }
        $title = apply_filters('widget_title', $title);
        $count = isset($instance['count']) ? (int) $instance['count'] : false;
        if ($count < 1 || 20 < $count) {
            $count = 10;
        }
        if (isset($instance['display']) && in_array($instance['display'], array('grid', 'list', 'text'))) {
            $display = $instance['display'];
        } else {
            $display = 'text';
        }
        if ('text' != $display) {
            $get_image_options = array('fallback_to_avatars' => true, 'gravatar_default' => apply_filters('jetpack_static_url', is_ssl() ? 'https' : 'http' . '://en.wordpress.com/i/logo/white-gray-80.png'));
            if ('grid' == $display) {
                if ($count % 2 != 0) {
                    $count++;
                }
                $get_image_options['avatar_size'] = 200;
            } else {
                $get_image_options['avatar_size'] = 40;
            }
            $get_image_options = apply_filters('jetpack_top_posts_widget_image_options', $get_image_options);
        }
        $posts = $this->get_by_views($count);
        if (!$posts) {
            $posts = $this->get_fallback_posts();
        }
        echo $args['before_widget'];
        if (!empty($title)) {
            echo $args['before_title'] . $title . $args['after_title'];
        }
        if (!$posts) {
            if (current_user_can('edit_theme_options')) {
                echo '<p>' . sprintf(__('There are no posts to display. <a href="%s">Want more traffic?</a>', 'jetpack'), 'http://en.support.wordpress.com/getting-more-site-traffic/') . '</p>';
            }
            echo $args['after_widget'];
            return;
        }
        switch ($display) {
            case 'list':
            case 'grid':
                wp_enqueue_style('widget-grid-and-list');
                foreach ($posts as &$post) {
                    $image = Jetpack_PostImages::get_image($post['post_id']);
                    $post['image'] = $image['src'];
                    if ('blavatar' != $image['from'] && 'gravatar' != $image['from']) {
                        $size = (int) $get_image_options['avatar_size'];
                        $post['image'] = jetpack_photon_url($post['image'], array('resize' => "{$size},{$size}"));
                    }
                }
                unset($post);
                if ('grid' == $display) {
                    echo "<div class='widgets-grid-layout no-grav'>\n";
                    foreach ($posts as $post) {
                        ?>
					<div class="widget-grid-view-image">
						<a href="<?php 
                        echo esc_url($post['permalink']);
                        ?>
" title="<?php 
                        echo esc_attr(wp_kses($post['title'], array()));
                        ?>
" class="bump-view" data-bump-view="tp"><img src="<?php 
                        echo esc_url($post['image']);
                        ?>
" /></a>
					</div>

				<?php 
                    }
                    echo "</div>\n";
                } else {
                    echo "<ul class='widgets-list-layout no-grav'>\n";
                    foreach ($posts as $post) {
                        ?>
					<li>
						<img src="<?php 
                        echo esc_url($post['image']);
                        ?>
" class='widgets-list-layout-blavatar' />
						<div class="widgets-list-layout-links"><a href="<?php 
                        echo esc_url($post['permalink']);
                        ?>
" class="bump-view" data-bump-view="tp"><?php 
                        echo esc_html(wp_kses($post['title'], array()));
                        ?>
</a></div>
					</li>
				<?php 
                    }
                    echo "</ul>\n";
                }
                break;
            default:
                echo '<ul>';
                foreach ($posts as $post) {
                    echo '<li><a href="' . esc_url($post['permalink']) . '" class="bump-view" data-bump-view="tp">' . esc_html($post['title']) . "</a></li>\n";
                }
                echo '</ul>';
        }
        echo $args['after_widget'];
    }
 static function twitter_cards_tags($og_tags)
 {
     global $post;
     if (post_password_required()) {
         return $og_tags;
     }
     if (apply_filters('jetpack_disable_twitter_cards', false)) {
         return $og_tags;
     }
     /*
      * These tags apply to any page (home, archives, etc)
      */
     $site_tag = apply_filters('jetpack_twitter_cards_site_tag', self::site_tag());
     $og_tags['twitter:site'] = self::sanitize_twitter_user($site_tag);
     if (!is_singular() || !empty($og_tags['twitter:card'])) {
         return $og_tags;
     }
     /*
      * The following tags only apply to single pages.
      */
     $card_type = 'summary';
     // Try to give priority to featured images
     if (class_exists('Jetpack_PostImages')) {
         $featured = Jetpack_PostImages::from_thumbnail($post->ID, 240, 240);
         if (!empty($featured) && count($featured) > 0) {
             if ((int) $featured[0]['src_width'] >= 280 && (int) $featured[0]['src_height'] >= 150) {
                 $card_type = 'summary_large_image';
                 $og_tags['twitter:image:src'] = add_query_arg('w', 640, $featured[0]['src']);
             } else {
                 $og_tags['twitter:image'] = add_query_arg('w', 240, $featured[0]['src']);
             }
         }
     }
     // Only proceed with media analysis if a featured image has not superseded it already.
     if (empty($og_tags['twitter:image']) && empty($og_tags['twitter:image:src'])) {
         if (!class_exists('Jetpack_Media_Summary') && defined('IS_WPCOM') && IS_WPCOM) {
             include WP_CONTENT_DIR . '/lib/class.wpcom-media-summary.php';
         }
         // Test again, class should already be auto-loaded in Jetpack.
         // If not, skip extra media analysis and stick with a summary card
         if (class_exists('Jetpack_Media_Summary')) {
             $extract = Jetpack_Media_Summary::get($post->ID);
             if ('gallery' == $extract['type']) {
                 list($og_tags, $card_type) = self::twitter_cards_define_type_based_on_image_count($og_tags, $extract);
             } else {
                 if ('video' == $extract['type']) {
                     // Leave as summary, but with large pict of poster frame (we know those comply to Twitter's size requirements)
                     $card_type = 'summary_large_image';
                     $og_tags['twitter:image:src'] = add_query_arg('w', 640, $extract['image']);
                 } else {
                     list($og_tags, $card_type) = self::twitter_cards_define_type_based_on_image_count($og_tags, $extract);
                 }
             }
         }
     }
     $og_tags['twitter:card'] = $card_type;
     // If we have information on the author/creator, then include that as well
     if (!empty($post) && !empty($post->post_author)) {
         $handle = apply_filters('jetpack_sharing_twitter_via', '', $post->ID);
         if (!empty($handle) && 'wordpressdotcom' != $handle && 'jetpack' != $handle) {
             $og_tags['twitter:creator'] = self::sanitize_twitter_user($handle);
         }
     }
     // Make sure we have a description for Twitter, their validator isn't happy without some content (single space not valid).
     if (!isset($og_tags['og:description']) || '' == trim($og_tags['og:description']) || __('Visit the post for more.', 'jetpack') == $og_tags['og:description']) {
         // empty( trim( $og_tags['og:description'] ) ) isn't valid php
         $has_creator = !empty($og_tags['twitter:creator']) && '@wordpressdotcom' != $og_tags['twitter:creator'] ? true : false;
         if ('photo' == $card_type) {
             $og_tags['twitter:description'] = $has_creator ? sprintf(__('Photo post by %s.', 'jetpack'), $og_tags['twitter:creator']) : __('Photo post.', 'jetpack');
         } else {
             if (!empty($extract) && 'video' == $extract['type']) {
                 // use $extract['type'] since $card_type is 'summary' for video posts
                 $og_tags['twitter:description'] = $has_creator ? sprintf(__('Video post by %s.', 'jetpack'), $og_tags['twitter:creator']) : __('Video post.', 'jetpack');
             } else {
                 if ('gallery' == $card_type) {
                     $og_tags['twitter:description'] = $has_creator ? sprintf(__('Gallery post by %s.', 'jetpack'), $og_tags['twitter:creator']) : __('Gallery post.', 'jetpack');
                 } else {
                     $og_tags['twitter:description'] = $has_creator ? sprintf(__('Post by %s.', 'jetpack'), $og_tags['twitter:creator']) : __('Visit the post for more.', 'jetpack');
                 }
             }
         }
     }
     return $og_tags;
 }
Beispiel #7
0
/**
 * Prints the news XML sitemap conforming to the Sitemaps.org protocol.
 * Outputs an XML list of up to 1000 posts published in the last 2 days.
 *
 * @module sitemaps
 *
 * @link http://sitemaps.org/protocol.php Sitemaps.org protocol.
 */
function jetpack_print_news_sitemap()
{
    $xml = get_transient('jetpack_news_sitemap');
    if ($xml) {
        header('Content-Type: application/xml');
        echo $xml;
        die;
    }
    global $wpdb, $post;
    /**
     * Filter post types to be included in news sitemap.
     *
     * @module sitemaps
     *
     * @since 3.9.0
     *
     * @param array $post_types Array with post types to include in news sitemap.
     */
    $post_types = apply_filters('jetpack_sitemap_news_sitemap_post_types', array('post'));
    if (empty($post_types)) {
        return;
    }
    $post_types_in = array();
    foreach ($post_types as $post_type) {
        $post_types_in[] = $wpdb->prepare('%s', $post_type);
    }
    $post_types_in_string = implode(', ', $post_types_in);
    /**
     * Filter limit of entries to include in news sitemap.
     *
     * @module sitemaps
     *
     * @since 3.9.0
     *
     * @param int $count Number of entries to include in news sitemap.
     */
    $limit = apply_filters('jetpack_sitemap_news_sitemap_count', 1000);
    $cur_datetime = current_time('mysql', true);
    $query = $wpdb->prepare("\n\t\tSELECT p.ID, p.post_title, p.post_type, p.post_date, p.post_name, p.post_date_gmt, GROUP_CONCAT(t.name SEPARATOR ', ') AS keywords\n\t\tFROM\n\t\t\t{$wpdb->posts} AS p LEFT JOIN {$wpdb->term_relationships} AS r ON p.ID = r.object_id\n\t\t\tLEFT JOIN {$wpdb->term_taxonomy} AS tt ON r.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = 'post_tag'\n\t\t\tLEFT JOIN {$wpdb->terms} AS t ON tt.term_id = t.term_id\n\t\tWHERE\n\t\t\tpost_status='publish' AND post_type IN ( {$post_types_in_string} ) AND post_date_gmt > (%s - INTERVAL 2 DAY)\n\t\tGROUP BY p.ID\n\t\tORDER BY p.post_date_gmt DESC LIMIT %d", $cur_datetime, $limit);
    // URL to XSLT
    $xsl = get_option('permalink_structure') ? home_url('news-sitemap.xsl') : home_url('/?jetpack-news-sitemap-xsl=true');
    // Unless it's zh-cn for Simplified Chinese or zh-tw for Traditional Chinese,
    // trim national variety so an ISO 639 language code as required by Google.
    $language_code = strtolower(get_locale());
    if (in_array($language_code, array('zh_tw', 'zh_cn'))) {
        $language_code = str_replace('_', '-', $language_code);
    } else {
        $language_code = preg_replace('/(_.*)$/i', '', $language_code);
    }
    header('Content-Type: application/xml');
    ob_start();
    echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
    echo '<?xml-stylesheet type="text/xsl" href="' . esc_url($xsl) . '"?>' . "\n";
    echo '<!-- generator="jetpack-' . JETPACK__VERSION . '" -->' . "\n";
    ?>
	<!-- generator="jetpack" -->
	<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
	        xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
	        xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"
	        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
		>
		<?php 
    $posts = $wpdb->get_results($query);
    foreach ($posts as $post) {
        setup_postdata($post);
        /**
         * Filter condition to allow skipping specific posts in news sitemap.
         *
         * @module sitemaps
         *
         * @since 3.9.0
         *
         * @param bool $skip Current boolean. False by default, so no post is skipped.
         * @param WP_POST $post Current post object.
         */
        if (apply_filters('jetpack_sitemap_news_skip_post', false, $post)) {
            continue;
        }
        $GLOBALS['post'] = $post;
        $url = array();
        $url['loc'] = get_permalink($post->ID);
        $news = array();
        $news['news:publication']['news:name'] = get_bloginfo_rss('name');
        $news['news:publication']['news:language'] = $language_code;
        $news['news:publication_date'] = jetpack_w3cdate_from_mysql($post->post_date_gmt);
        $news['news:title'] = get_the_title_rss();
        if ($post->keywords) {
            $news['news:keywords'] = html_entity_decode(ent2ncr($post->keywords), ENT_HTML5);
        }
        $url['news:news'] = $news;
        // Add image to sitemap
        $post_thumbnail = Jetpack_PostImages::get_image($post->ID);
        if (isset($post_thumbnail['src'])) {
            $url['image:image'] = array('image:loc' => esc_url($post_thumbnail['src']));
        }
        /**
         * Filter associative array with data to build <url> node and its descendants for current post in news sitemap.
         *
         * @module sitemaps
         *
         * @since 3.9.0
         *
         * @param array $url Data to build parent and children nodes for current post.
         * @param int $post_id Current post ID.
         */
        $url = apply_filters('jetpack_sitemap_news_sitemap_item', $url, $post);
        if (empty($url)) {
            continue;
        }
        jetpack_print_sitemap_item($url);
    }
    wp_reset_postdata();
    ?>
	</urlset>
	<?php 
    $xml = ob_get_contents();
    ob_end_clean();
    if (!empty($xml)) {
        set_transient('jetpack_news_sitemap', $xml, DAY_IN_SECONDS);
        echo $xml;
    }
    die;
}
function jetpack_og_get_image($width = 200, $height = 200, $max_images = 4)
{
    // Facebook requires thumbnails to be a minimum of 200x200
    $image = '';
    if (is_singular() && !is_home() && !is_front_page()) {
        global $post;
        $image = '';
        // Attempt to find something good for this post using our generalized PostImages code
        if (class_exists('Jetpack_PostImages')) {
            $post_images = Jetpack_PostImages::get_images($post->ID, array('width' => $width, 'height' => $height));
            if ($post_images && !is_wp_error($post_images)) {
                $image = array();
                foreach ((array) $post_images as $post_image) {
                    $image[] = $post_image['src'];
                }
            }
        }
    } else {
        if (is_author()) {
            $author = get_queried_object();
            if (function_exists('get_avatar_url')) {
                $avatar = get_avatar_url($author->user_email, $width);
                if (!empty($avatar)) {
                    if (is_array($avatar)) {
                        $image = $avatar[0];
                    } else {
                        $image = $avatar;
                    }
                }
            } else {
                $has_filter = has_filter('pre_option_show_avatars', '__return_true');
                if (!$has_filter) {
                    add_filter('pre_option_show_avatars', '__return_true');
                }
                $avatar = get_avatar($author->user_email, $width);
                if (!$has_filter) {
                    remove_filter('pre_option_show_avatars', '__return_true');
                }
                if (!empty($avatar) && !is_wp_error($avatar)) {
                    if (preg_match('/src=["\']([^"\']+)["\']/', $avatar, $matches)) {
                    }
                    $image = wp_specialchars_decode($matches[1], ENT_QUOTES);
                }
            }
        }
    }
    // Fallback to Blavatar if available
    if (function_exists('blavatar_domain')) {
        $blavatar_domain = blavatar_domain(site_url());
        if (empty($image) && blavatar_exists($blavatar_domain)) {
            $image = blavatar_url($blavatar_domain, 'img', $width);
        }
    }
    return $image;
}
Beispiel #9
0
/**
 * Get one image from a specified post in the following order:
 * Featured Image, first attached image, first image from the_content HTML
 * @param int $id, The post ID to check
 * @param string $size, The image size to return, defaults to 'post-thumbnail'
 * @return string $thumb, Thumbnail image with markup
 */
function spun_get_image($id, $size = 'home-post')
{
    $thumb = '';
    if ('' != get_the_post_thumbnail($id)) {
        $thumb = get_the_post_thumbnail($id, $size, array('title' => esc_attr(strip_tags(get_the_title()))));
    } else {
        $args = array('post_type' => 'attachment', 'fields' => 'ids', 'numberposts' => 1, 'post_status' => null, 'post_mime_type' => 'image', 'post_parent' => $id);
        $first_attachment = get_posts($args);
        if ($first_attachment) {
            /* Get the first image attachment */
            foreach ($first_attachment as $attachment) {
                $thumb = wp_get_attachment_image($attachment, $size, false, array('title' => esc_attr(strip_tags(get_the_title()))));
            }
        } elseif (class_exists('Jetpack_PostImages')) {
            /* Get the first image directly from HTML content */
            $getimage = new Jetpack_PostImages();
            $image = $getimage->from_html($id);
            if ($image) {
                $thumb = '<img src="' . $image[0]['src'] . '" title="' . esc_attr(strip_tags(get_the_title())) . '" class="attachment-' . $size . ' wp-post-image" />';
            }
        }
    }
    return $thumb;
}
function enhanced_og_has_featured_image($post_id)
{
    $featured = Jetpack_PostImages::from_thumbnail($post_id, 200, 200);
    if (!empty($featured) && count($featured) > 0) {
        return true;
    }
    return false;
}
function jetpack_og_get_image($width = 200, $height = 200, $max_images = 4)
{
    // Facebook requires thumbnails to be a minimum of 200x200
    $image = '';
    if (is_singular() && !is_home()) {
        global $post;
        $image = '';
        // Attempt to find something good for this post using our generalized PostImages code
        if (class_exists('Jetpack_PostImages')) {
            $post_images = Jetpack_PostImages::get_images($post->ID, array('width' => $width, 'height' => $height));
            if ($post_images && !is_wp_error($post_images)) {
                $image = array();
                foreach ((array) $post_images as $post_image) {
                    $image[] = $post_image['src'];
                }
            }
        }
    } else {
        if (is_author()) {
            $author = get_queried_object();
            if (function_exists('get_avatar_url')) {
                // Prefer the core function get_avatar_url() if available, WP 4.2+
                $image = get_avatar_url($author->user_email, array('size' => $width));
            } else {
                $has_filter = has_filter('pre_option_show_avatars', '__return_true');
                if (!$has_filter) {
                    add_filter('pre_option_show_avatars', '__return_true');
                }
                $avatar = get_avatar($author->user_email, $width);
                if (!$has_filter) {
                    remove_filter('pre_option_show_avatars', '__return_true');
                }
                if (!empty($avatar) && !is_wp_error($avatar)) {
                    if (preg_match('/src=["\']([^"\']+)["\']/', $avatar, $matches)) {
                    }
                    $image = wp_specialchars_decode($matches[1], ENT_QUOTES);
                }
            }
        }
    }
    if (empty($image)) {
        $image = array();
    } else {
        if (!is_array($image)) {
            $image = array($image);
        }
    }
    // First fall back, blavatar
    if (empty($image) && function_exists('blavatar_domain')) {
        $blavatar_domain = blavatar_domain(site_url());
        if (blavatar_exists($blavatar_domain)) {
            $image[] = blavatar_url($blavatar_domain, 'img', $width);
        }
    }
    // Second fall back, Site Logo
    if (empty($image) && (function_exists('jetpack_has_site_logo') && jetpack_has_site_logo())) {
        $image[] = jetpack_get_site_logo('url');
    }
    // Third fall back, Site Icon
    if (empty($image) && (function_exists('jetpack_has_site_icon') && jetpack_has_site_icon())) {
        $image[] = jetpack_site_icon_url(null, '512');
    }
    // Fourth fall back, blank image
    if (empty($image)) {
        $image[] = apply_filters('jetpack_open_graph_image_default', 'https://s0.wp.com/i/blank.jpg');
    }
    return $image;
}
 /**
  * Retrieve the image URL for a post.
  *
  * @since 3.1.0
  *
  * @see Cedaro_Theme_PostMedia::get_image()
  * @todo Refactor to remove dependence on Cedaro_Theme_PostMedia::get_image()
  *
  * @param int|WP_Post  $post Optional. Post ID or object. Defaults to the current post.
  * @param string|array $size Optional. The size of the image to return. Defaults to large.
  * @return string      Image URL.
  */
 public function get_image_url($post = 0, $size = 'large')
 {
     $post = get_post($post);
     $url = $this->get_image_url_from_cache($post, $size);
     if (null !== $url) {
         return apply_filters($this->theme->prefix . '_post_image_url', $url, $post);
     }
     // Check for a featured image first.
     if (has_post_thumbnail($post->ID)) {
         $data = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), $size);
         $url = $data[0];
     } elseif (class_exists('Jetpack_PostImages') && ($data = Jetpack_PostImages::get_image($post->ID))) {
         $url = $data['src'];
     } elseif (!class_exists('Jetpack_PostImages')) {
         // Check the post content for an image.
         $html = $this->get_image_in_content($post->post_content);
         if (!empty($html) && preg_match('/src=[\'"]([^\'"]+)/', $html, $matches)) {
             $url = $matches[1];
         } else {
             // Check the post's attachments.
             $images = get_posts(array('post_type' => 'attachment', 'post_parent' => $post->ID, 'post_mime_type' => 'image', 'posts_per_page' => 1, 'fields' => 'ids', 'orderby' => 'menu_order', 'order' => 'asc'));
             if (count($images)) {
                 $data = wp_get_attachment_image_src($images[0], $size);
                 $url = $data[0];
             }
         }
     }
     $this->cache_image_url($post, $size, $url);
     return apply_filters($this->theme->prefix . '_post_image_url', $url, $post);
 }
Beispiel #13
0
 public function get_post_image($content)
 {
     $image = '';
     if (class_exists('Jetpack_PostImages')) {
         global $post;
         $img = Jetpack_PostImages::from_html($post->ID);
         if (!empty($img['src'])) {
             return $img['src'];
         }
     }
     if (function_exists('has_post_thumbnail') && has_post_thumbnail()) {
         $thumb_id = get_post_thumbnail_id();
         $thumb = wp_get_attachment_image_src($thumb_id, 'full');
         // This shouldn't be necessary, since has_post_thumbnail() is true,
         // but... see http://wordpress.org/support/topic/jetpack-youtube-embeds
         if (!$thumb) {
             return '';
         }
         $image = remove_query_arg(array('w', 'h'), $thumb[0]);
     } else {
         if (preg_match_all('/<img (.+?)>/', $content, $matches)) {
             foreach ($matches[1] as $attrs) {
                 $media = $img = array();
                 foreach (wp_kses_hair($attrs, array('http', 'https')) as $attr) {
                     $img[$attr['name']] = $attr['value'];
                 }
                 if (!isset($img['src']) || 0 !== strpos($img['src'], 'http')) {
                     continue;
                 } else {
                     $image = htmlspecialchars_decode($img['src']);
                     break;
                 }
             }
         }
     }
     return $image;
 }
 public function get_image($post)
 {
     if (class_exists('Jetpack_PostImages')) {
         $image = Jetpack_PostImages::get_image($post->ID, array('fallback_to_avatars' => true));
         if (!empty($image)) {
             return $image['src'];
         }
     }
     /**
      * Filters the default image used by the Pinterest Pin It share button.
      *
      * @module sharedaddy
      *
      * @since 3.6.0
      *
      * @param string $url Default image URL.
      */
     return apply_filters('jetpack_sharing_pinterest_default_image', 'https://s0.wp.com/i/blank.jpg');
 }
Beispiel #15
0
 public function get_post_image($content)
 {
     $image = '';
     if (class_exists('Jetpack_PostImages')) {
         // Use the full stack of methods to find an image, except for HTML, which can cause loops
         $img = Jetpack_PostImages::get_image($content->ID);
         if (!empty($img['src'])) {
             return $img['src'];
         }
     }
     // If we have to fall back to the following, we only do a few basic image checks
     $content = $content->post_content;
     if (function_exists('has_post_thumbnail') && has_post_thumbnail()) {
         $thumb_id = get_post_thumbnail_id();
         $thumb = wp_get_attachment_image_src($thumb_id, 'full');
         // This shouldn't be necessary, since has_post_thumbnail() is true,
         // but... see http://wordpress.org/support/topic/jetpack-youtube-embeds
         if (!$thumb) {
             return '';
         }
         $image = remove_query_arg(array('w', 'h'), $thumb[0]);
     } else {
         if (preg_match_all('/<img (.+?)>/', $content, $matches)) {
             foreach ($matches[1] as $attrs) {
                 $media = $img = array();
                 foreach (wp_kses_hair($attrs, array('http', 'https')) as $attr) {
                     $img[$attr['name']] = $attr['value'];
                 }
                 if (!isset($img['src']) || 0 !== strpos($img['src'], 'http')) {
                     continue;
                 } else {
                     $image = htmlspecialchars_decode($img['src']);
                     break;
                 }
             }
         }
     }
     return $image;
 }
Beispiel #16
0
    function widget($args, $instance)
    {
        $title = isset($instance['title']) ? $instance['title'] : false;
        if (false === $title) {
            $title = $this->default_title;
        }
        /** This filter is documented in core/src/wp-includes/default-widgets.php */
        $title = apply_filters('widget_title', $title);
        $count = isset($instance['count']) ? (int) $instance['count'] : false;
        if ($count < 1 || 10 < $count) {
            $count = 10;
        }
        /**
         * Control the number of displayed posts.
         *
         * @since 3.3.0
         *
         * @param string $count Number of Posts displayed in the Top Posts widget. Default is 10.
         */
        $count = apply_filters('jetpack_top_posts_widget_count', $count);
        $types = isset($instance['types']) ? (array) $instance['types'] : array('post', 'page');
        if (isset($instance['display']) && in_array($instance['display'], array('grid', 'list', 'text'))) {
            $display = $instance['display'];
        } else {
            $display = 'text';
        }
        if ('text' != $display) {
            $get_image_options = array('fallback_to_avatars' => true, 'gravatar_default' => apply_filters('jetpack_static_url', set_url_scheme('http://en.wordpress.com/i/logo/white-gray-80.png')));
            if ('grid' == $display) {
                $get_image_options['avatar_size'] = 200;
            } else {
                $get_image_options['avatar_size'] = 40;
            }
            /**
             * Top Posts Widget Image options.
             *
             * @since 1.8.0
             *
             * @param array $get_image_options {
             * Array of Image options.
             * @type bool true Should we default to Gravatars when no image is found? Default is true.
             * @type string $gravatar_default Default Image URL if no Gravatar is found.
             * @type int $avatar_size Default Image size.
             * }
             */
            $get_image_options = apply_filters('jetpack_top_posts_widget_image_options', $get_image_options);
        }
        $posts = $this->get_by_views($count);
        // Filter the returned posts. Remove all posts that do not match the chosen Post Types.
        if (isset($types)) {
            foreach ($posts as $k => $post) {
                if (!in_array($post['post_type'], $types)) {
                    unset($posts[$k]);
                }
            }
        }
        if (!$posts) {
            $posts = $this->get_fallback_posts();
        }
        echo $args['before_widget'];
        if (!empty($title)) {
            echo $args['before_title'] . $title . $args['after_title'];
        }
        if (!$posts) {
            if (current_user_can('edit_theme_options')) {
                echo '<p>' . sprintf(__('There are no posts to display. <a href="%s">Want more traffic?</a>', 'jetpack'), 'http://en.support.wordpress.com/getting-more-site-traffic/') . '</p>';
            }
            echo $args['after_widget'];
            return;
        }
        switch ($display) {
            case 'list':
            case 'grid':
                wp_enqueue_style('widget-grid-and-list');
                foreach ($posts as &$post) {
                    $image = Jetpack_PostImages::get_image($post['post_id'], array('fallback_to_avatars' => true));
                    $post['image'] = $image['src'];
                    if ('blavatar' != $image['from'] && 'gravatar' != $image['from']) {
                        $size = (int) $get_image_options['avatar_size'];
                        $post['image'] = jetpack_photon_url($post['image'], array('resize' => "{$size},{$size}"));
                    }
                }
                unset($post);
                if ('grid' == $display) {
                    echo "<div class='widgets-grid-layout no-grav'>\n";
                    foreach ($posts as $post) {
                        ?>
					<div class="widget-grid-view-image">
						<?php 
                        /**
                         * Fires before each Top Post result, inside <li>.
                         *
                         * @since 3.2.0
                         *
                         * @param string $post['post_id'] Post ID.
                         */
                        do_action('jetpack_widget_top_posts_before_post', $post['post_id']);
                        ?>
						<a href="<?php 
                        echo esc_url($post['permalink']);
                        ?>
" title="<?php 
                        echo esc_attr(wp_kses($post['title'], array()));
                        ?>
" class="bump-view" data-bump-view="tp">
							<img src="<?php 
                        echo esc_url($post['image']);
                        ?>
" alt="<?php 
                        echo esc_attr(wp_kses($post['title'], array()));
                        ?>
" data-pin-nopin="true" />
						</a>
						<?php 
                        /**
                         * Fires after each Top Post result, inside <li>.
                         *
                         * @since 3.2.0
                         *
                         * @param string $post['post_id'] Post ID.
                         */
                        do_action('jetpack_widget_top_posts_after_post', $post['post_id']);
                        ?>
					</div>
				<?php 
                    }
                    echo "</div>\n";
                } else {
                    echo "<ul class='widgets-list-layout no-grav'>\n";
                    foreach ($posts as $post) {
                        ?>
					<li>
						<?php 
                        /** This action is documented in modules/widgets/top-posts.php */
                        do_action('jetpack_widget_top_posts_before_post', $post['post_id']);
                        ?>
						<a href="<?php 
                        echo esc_url($post['permalink']);
                        ?>
" title="<?php 
                        echo esc_attr(wp_kses($post['title'], array()));
                        ?>
" class="bump-view" data-bump-view="tp">
							<img src="<?php 
                        echo esc_url($post['image']);
                        ?>
" class='widgets-list-layout-blavatar' alt="<?php 
                        echo esc_attr(wp_kses($post['title'], array()));
                        ?>
" data-pin-nopin="true" />
						</a>
						<div class="widgets-list-layout-links">
							<a href="<?php 
                        echo esc_url($post['permalink']);
                        ?>
" class="bump-view" data-bump-view="tp">
								<?php 
                        echo esc_html(wp_kses($post['title'], array()));
                        ?>
							</a>
						</div>
						<?php 
                        /** This action is documented in modules/widgets/top-posts.php */
                        do_action('jetpack_widget_top_posts_after_post', $post['post_id']);
                        ?>
					</li>
				<?php 
                    }
                    echo "</ul>\n";
                }
                break;
            default:
                echo '<ul>';
                foreach ($posts as $post) {
                    ?>
				<li>
					<?php 
                    /** This action is documented in modules/widgets/top-posts.php */
                    do_action('jetpack_widget_top_posts_before_post', $post['post_id']);
                    ?>
					<a href="<?php 
                    echo esc_url($post['permalink']);
                    ?>
" class="bump-view" data-bump-view="tp">
						<?php 
                    echo esc_html(wp_kses($post['title'], array()));
                    ?>
					</a>
					<?php 
                    /** This action is documented in modules/widgets/top-posts.php */
                    do_action('jetpack_widget_top_posts_after_post', $post['post_id']);
                    ?>
				</li>
			<?php 
                }
                echo '</ul>';
        }
        echo $args['after_widget'];
    }
 /**
  * Generates the thumbnail image to be used for the post. Uses the
  * image as returned by Jetpack_PostImages::get_image()
  *
  * @param int $post_id
  * @uses self::get_options, apply_filters, Jetpack_PostImages::get_image, Jetpack_PostImages::fit_image_url
  * @return string
  */
 protected function _generate_related_post_image_params($post_id)
 {
     $options = $this->get_options();
     $image_params = array('src' => '', 'width' => 0, 'height' => 0);
     if (!$options['show_thumbnails']) {
         return $image_params;
     }
     $thumbnail_size = apply_filters('jetpack_relatedposts_filter_thumbnail_size', array('width' => 350, 'height' => 200));
     if (!is_array($thumbnail_size)) {
         $thumbnail_size = array('width' => (int) $thumbnail_size, 'height' => (int) $thumbnail_size);
     }
     // Try to get post image
     if (class_exists('Jetpack_PostImages')) {
         $img_url = '';
         $post_image = Jetpack_PostImages::get_image($post_id, $thumbnail_size);
         if (is_array($post_image)) {
             $img_url = $post_image['src'];
         } elseif (class_exists('Jetpack_Media_Summary')) {
             $media = Jetpack_Media_Summary::get($post_id);
             if (is_array($media) && !empty($media['image'])) {
                 $img_url = $media['image'];
             }
         }
         if (!empty($img_url)) {
             $image_params['width'] = $thumbnail_size['width'];
             $image_params['height'] = $thumbnail_size['height'];
             $image_params['src'] = Jetpack_PostImages::fit_image_url($img_url, $thumbnail_size['width'], $thumbnail_size['height']);
         }
     }
     return $image_params;
 }
function jetpack_og_get_image($width = 200, $height = 200, $max_images = 4)
{
    // Facebook requires thumbnails to be a minimum of 200x200
    $image = '';
    if (is_singular() && !is_home()) {
        global $post;
        $image = '';
        // Grab obvious image if $post is an attachment page for an image
        if (is_attachment($post->ID) && 'image' == substr($post->post_mime_type, 0, 5)) {
            $image = wp_get_attachment_url($post->ID);
        }
        // Attempt to find something good for this post using our generalized PostImages code
        if (!$image && class_exists('Jetpack_PostImages')) {
            $post_images = Jetpack_PostImages::get_images($post->ID, array('width' => $width, 'height' => $height));
            if ($post_images && !is_wp_error($post_images)) {
                $image = array();
                foreach ((array) $post_images as $post_image) {
                    $image['src'] = $post_image['src'];
                    if (isset($post_image['src_width'], $post_image['src_height'])) {
                        $image['width'] = $post_image['src_width'];
                        $image['height'] = $post_image['src_height'];
                    }
                }
            }
        }
    } else {
        if (is_author()) {
            $author = get_queried_object();
            if (function_exists('get_avatar_url')) {
                // Prefer the core function get_avatar_url() if available, WP 4.2+
                $image['src'] = get_avatar_url($author->user_email, array('size' => $width));
            } else {
                $has_filter = has_filter('pre_option_show_avatars', '__return_true');
                if (!$has_filter) {
                    add_filter('pre_option_show_avatars', '__return_true');
                }
                $avatar = get_avatar($author->user_email, $width);
                if (!$has_filter) {
                    remove_filter('pre_option_show_avatars', '__return_true');
                }
                if (!empty($avatar) && !is_wp_error($avatar)) {
                    if (preg_match('/src=["\']([^"\']+)["\']/', $avatar, $matches)) {
                    }
                    $image['src'] = wp_specialchars_decode($matches[1], ENT_QUOTES);
                }
            }
        }
    }
    if (empty($image)) {
        $image = array();
    } else {
        if (!is_array($image)) {
            $image = array('src' => $image);
        }
    }
    // First fall back, blavatar
    if (empty($image) && function_exists('blavatar_domain')) {
        $blavatar_domain = blavatar_domain(site_url());
        if (blavatar_exists($blavatar_domain)) {
            $image['src'] = blavatar_url($blavatar_domain, 'img', $width, false, true);
            $image['width'] = $width;
            $image['height'] = $height;
        }
    }
    // Second fall back, Site Logo
    if (empty($image) && (function_exists('jetpack_has_site_logo') && jetpack_has_site_logo())) {
        $image['src'] = jetpack_get_site_logo('url');
        $image_dimensions = jetpack_get_site_logo_dimensions();
        if (!empty($image_dimensions)) {
            $image['width'] = $image_dimensions['width'];
            $image['height'] = $image_dimensions['height'];
        }
    }
    // Third fall back, Site Icon
    if (empty($image) && (function_exists('jetpack_has_site_icon') && jetpack_has_site_icon())) {
        $image['src'] = jetpack_site_icon_url(null, '512');
        $image['width'] = '512';
        $image['height'] = '512';
    }
    // Fourth fall back, Core Site Icon. Added in WP 4.3.
    if (empty($image) && (function_exists('has_site_icon') && has_site_icon())) {
        $image['src'] = get_site_icon_url(null, '512');
    }
    // Finally fall back, blank image
    if (empty($image)) {
        /**
         * Filter the default Open Graph Image tag, used when no Image can be found in a post.
         *
         * @since 3.0.0
         *
         * @param string $str Default Image URL.
         */
        $image['src'] = apply_filters('jetpack_open_graph_image_default', 'https://s0.wp.com/i/blank.jpg');
    }
    return $image;
}
Beispiel #19
0
/**
 * Get one image from a specified post in the following order:
 * Featured Image, first attached image, first image from the_content HTML
 *
 * @param int $id, The post ID to check
 * @param string        $size   The image size to return, defaults to 'featured-home-big'.
 * @param string|array  $attr   Optional. Query string or array of attributes.
 * @return string       $thumb  Thumbnail image with markup.
 */
function boardwalk_get_image($post_id = null, $size = 'post-thumbnail', $attr = '')
{
    $post_id = null === $post_id ? get_the_ID() : $post_id;
    if ('' != get_the_post_thumbnail($post_id)) {
        return get_the_post_thumbnail($post_id, $size, $attr);
    }
    $attached_images = get_attached_media('image');
    if (!empty($attached_images)) {
        $first_attached_image = array_shift($attached_images);
        return wp_get_attachment_image($first_attached_image->ID, $size, false, $attr);
    }
    if (class_exists('Jetpack_PostImages')) {
        global $_wp_additional_image_sizes;
        $args = array('from_thumbnail' => false, 'from_slideshow' => true, 'from_gallery' => true, 'from_attachment' => false);
        $image = Jetpack_PostImages::get_image($post_id, $args);
        if (!empty($image)) {
            $image['width'] = '';
            $image['height'] = '';
            if (array_key_exists($size, $_wp_additional_image_sizes)) {
                $image['width'] = $_wp_additional_image_sizes[$size]['width'];
                $image['height'] = $_wp_additional_image_sizes[$size]['height'];
            }
            $image_src = Jetpack_PostImages::fit_image_url($image['src'], $image['width'], $image['height']);
            return '<img src="' . esc_url($image_src) . '" title="' . esc_attr(strip_tags(get_the_title())) . '" class="attachment-' . esc_attr($size) . ' wp-post-image" />';
        }
    }
    return false;
}