Jetpack_PostImages::get_images PHP Метод

get_images() статический публичный Метод

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
Результат Array containing images that would be good for representing this post
    static function get_images($post_id, $args = array())
    {
        // Figure out which image to attach to this post.
        $media = false;
        /**
         * Filters the array of images that would be good for a specific post.
         * This filter is applied before options ($args) filter the original array.
         *
         * @since 2.0.0
         *
         * @param array $media Array of images that would be good for a specific post.
         * @param int $post_id Post ID.
         * @param array $args Array of options to get images.
         */
        $media = apply_filters('jetpack_images_pre_get_images', $media, $post_id, $args);
        if ($media) {
            return $media;
        }
        $defaults = array('width' => 200, 'height' => 200, 'fallback_to_avatars' => false, 'avatar_size' => 96, 'gravatar_default' => false, 'from_thumbnail' => true, 'from_slideshow' => true, 'from_gallery' => true, 'from_attachment' => true, 'from_html' => true, 'html_content' => '');
        $args = wp_parse_args($args, $defaults);
        $media = false;
        if ($args['from_thumbnail']) {
            $media = self::from_thumbnail($post_id, $args['width'], $args['height']);
        }
        if (!$media && $args['from_slideshow']) {
            $media = self::from_slideshow($post_id, $args['width'], $args['height']);
        }
        if (!$media && $args['from_gallery']) {
            $media = self::from_gallery($post_id);
        }
        if (!$media && $args['from_attachment']) {
            $media = self::from_attachment($post_id, $args['width'], $args['height']);
        }
        if (!$media && $args['from_html']) {
            if (empty($args['html_content'])) {
                $media = self::from_html($post_id);
            } else {
                $media = self::from_html($args['html_content']);
            }
            // If html_content is provided, use that
        }
        if (!$media && $args['fallback_to_avatars']) {
            $media = self::from_blavatar($post_id, $args['avatar_size']);
            if (!$media) {
                $media = self::from_gravatar($post_id, $args['avatar_size'], $args['gravatar_default']);
            }
        }
        /**
         * Filters the array of images that would be good for a specific post.
         * This filter is applied after options ($args) filter the original array.
         *
         * @since 2.0.0
         *
         * @param array $media Array of images that would be good for a specific post.
         * @param int $post_id Post ID.
         * @param array $args Array of options to get images.
         */
        return apply_filters('jetpack_images_get_images', $media, $post_id, $args);
    }

Usage Example

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;
}
All Usage Examples Of Jetpack_PostImages::get_images