get_images() static public method

Get an array containing a collection of possible images for this post, stopping once we hit a method that returns something useful.
static public get_images ( integer $post_id, array $args = [] ) : Array
$post_id integer
$args array Optional args, see defaults list for details
return Array containing images that would be good for representing this post
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;
}
Ejemplo n.º 2
0
/**
 * Print an XML sitemap conforming to the Sitemaps.org protocol.
 * Outputs an XML list of up to the latest 1000 posts.
 *
 * @module sitemaps
 *
 * @link http://sitemaps.org/protocol.php Sitemaps.org protocol.
 */
function jetpack_print_sitemap()
{
    global $wpdb, $post;
    $xml = get_transient('jetpack_sitemap');
    if ($xml) {
        header('Content-Type: ' . jetpack_sitemap_content_type(), true);
        echo $xml;
        die;
    }
    // Compatibility with PHP 5.3 and older
    if (!defined('ENT_XML1')) {
        define('ENT_XML1', 16);
    }
    /**
     * Filter the post types that will be included in sitemap.
     *
     * @module sitemaps
     *
     * @since 3.9.0
     *
     * @param array $post_types Array of post types.
     */
    $post_types = apply_filters('jetpack_sitemap_post_types', array('post', 'page'));
    $post_types_in = array();
    foreach ((array) $post_types as $post_type) {
        $post_types_in[] = $wpdb->prepare('%s', $post_type);
    }
    $post_types_in = join(",", $post_types_in);
    // use direct query instead because get_posts was acting too heavy for our needs
    //$posts = get_posts( array( 'numberposts'=>1000, 'post_type'=>$post_types, 'post_status'=>'published' ) );
    $posts = $wpdb->get_results("SELECT ID, post_type, post_modified_gmt, comment_count FROM {$wpdb->posts} WHERE post_status='publish' AND post_type IN ({$post_types_in}) ORDER BY post_modified_gmt DESC LIMIT 1000");
    if (empty($posts)) {
        status_header(404);
    }
    header('Content-Type: ' . jetpack_sitemap_content_type());
    $initstr = jetpack_sitemap_initstr(get_bloginfo('charset'));
    $tree = simplexml_load_string($initstr);
    // If we did not get a valid string, force UTF-8 and try again.
    if (false === $tree) {
        $initstr = jetpack_sitemap_initstr('UTF-8');
        $tree = simplexml_load_string($initstr);
    }
    unset($initstr);
    $latest_mod = '';
    foreach ($posts as $post) {
        setup_postdata($post);
        /**
         * Filter condition to allow skipping specific posts in 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_skip_post', false, $post)) {
            continue;
        }
        $post_latest_mod = null;
        $url = array('loc' => esc_url(get_permalink($post->ID)));
        // If this post is configured to be the site home, skip since it's added separately later
        if (untrailingslashit(get_permalink($post->ID)) == untrailingslashit(get_option('home'))) {
            continue;
        }
        // Mobile node specified in http://support.google.com/webmasters/bin/answer.py?hl=en&answer=34648
        $url['mobile:mobile'] = '';
        // Image node specified in http://support.google.com/webmasters/bin/answer.py?hl=en&answer=178636
        // These attachments were produced with batch SQL earlier in the script
        if (!post_password_required($post->ID)) {
            $media = array();
            $methods = array('from_thumbnail' => false, 'from_slideshow' => false, 'from_gallery' => false, 'from_attachment' => false, 'from_html' => false);
            foreach ($methods as $method => $value) {
                $methods[$method] = true;
                $images_collected = Jetpack_PostImages::get_images($post->ID, $methods);
                if (is_array($images_collected)) {
                    $media = array_merge($media, $images_collected);
                }
                $methods[$method] = false;
            }
            $images = array();
            foreach ($media as $item) {
                if (!isset($item['type']) || 'image' != $item['type']) {
                    continue;
                }
                $one_image = array();
                if (isset($item['src'])) {
                    $one_image['image:loc'] = esc_url($item['src']);
                    $one_image['image:title'] = sanitize_title_with_dashes($name = pathinfo($item['src'], PATHINFO_FILENAME));
                }
                $images[] = $one_image;
            }
            if (!empty($images)) {
                $url['image:image'] = $images;
            }
        }
        if ($post->post_modified_gmt && $post->post_modified_gmt != '0000-00-00 00:00:00') {
            $post_latest_mod = $post->post_modified_gmt;
        }
        if ($post->comment_count > 0) {
            // last modified based on last comment
            $latest_comment_datetime = jetpack_get_approved_comments_max_datetime($post->ID);
            if (!empty($latest_comment_datetime)) {
                if (is_null($post_latest_mod) || $latest_comment_datetime > $post_latest_mod) {
                    $post_latest_mod = $latest_comment_datetime;
                }
            }
            unset($latest_comment_datetime);
        }
        if (!empty($post_latest_mod)) {
            $latest_mod = max($latest_mod, $post_latest_mod);
            $url['lastmod'] = jetpack_w3cdate_from_mysql($post_latest_mod);
        }
        unset($post_latest_mod);
        if ($post->post_type == 'page') {
            $url['changefreq'] = 'weekly';
            $url['priority'] = '0.6';
            // set page priority above default priority of 0.5
        } else {
            $url['changefreq'] = 'monthly';
        }
        /**
         * Filter associative array with data to build <url> node and its descendants for current post.
         *
         * @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_node = apply_filters('jetpack_sitemap_url', $url, $post->ID);
        jetpack_sitemap_array_to_simplexml(array('url' => $url_node), $tree);
        unset($url);
    }
    wp_reset_postdata();
    $blog_home = array('loc' => esc_url(get_option('home')), 'changefreq' => 'daily', 'priority' => '1.0');
    if (!empty($latest_mod)) {
        $blog_home['lastmod'] = jetpack_w3cdate_from_mysql($latest_mod);
        header('Last-Modified:' . mysql2date('D, d M Y H:i:s', $latest_mod, 0) . ' GMT');
    }
    /**
     * Filter associative array with data to build <url> node and its descendants for site home.
     *
     * @module sitemaps
     *
     * @since 3.9.0
     *
     * @param array $blog_home Data to build parent and children nodes for site home.
     */
    $url_node = apply_filters('jetpack_sitemap_url_home', $blog_home);
    jetpack_sitemap_array_to_simplexml(array('url' => $url_node), $tree);
    unset($blog_home);
    /**
     * Filter data before rendering it as XML.
     *
     * @module sitemaps
     *
     * @since 3.9.0
     *
     * @param SimpleXMLElement $tree Data tree for sitemap.
     * @param string $latest_mod Date of last modification.
     */
    $tree = apply_filters('jetpack_print_sitemap', $tree, $latest_mod);
    $xml = $tree->asXML();
    unset($tree);
    if (!empty($xml)) {
        set_transient('jetpack_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;
}
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;
}