WPSEO_Sitemap_Image_Parser::get_images PHP Method

get_images() public method

Get set of image data sets for the given post.
public get_images ( object $post ) : array
$post object Post object to get images for.
return array
    public function get_images($post)
    {
        $images = array();
        if (!is_object($post)) {
            return $images;
        }
        $thumbnail_id = get_post_thumbnail_id($post->ID);
        if ($thumbnail_id) {
            $src = $this->get_absolute_url($this->image_url($thumbnail_id));
            $alt = get_post_meta($thumbnail_id, '_wp_attachment_image_alt', true);
            $title = get_post_field('post_title', $thumbnail_id);
            $images[] = $this->get_image_item($post, $src, $title, $alt);
        }
        $unfiltered_images = $this->parse_html_images($post->post_content);
        foreach ($unfiltered_images as $image) {
            $images[] = $this->get_image_item($post, $image['src'], $image['title'], $image['alt']);
        }
        foreach ($this->parse_galleries($post->post_content, $post->ID) as $attachment) {
            $src = $this->get_absolute_url($this->image_url($attachment->ID));
            $alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
            $images[] = $this->get_image_item($post, $src, $attachment->post_title, $alt);
        }
        if ('attachment' === $post->post_type && wp_attachment_is_image($post)) {
            $src = $this->get_absolute_url($this->image_url($post->ID));
            $alt = get_post_meta($post->ID, '_wp_attachment_image_alt', true);
            $images[] = $this->get_image_item($post, $src, $post->post_title, $alt);
        }
        foreach ($images as $key => $image) {
            if (empty($image['src'])) {
                unset($images[$key]);
            }
        }
        /**
         * Filter images to be included for the post in XML sitemap.
         *
         * @param array $images  Array of image items.
         * @param int   $post_id ID of the post.
         */
        $images = apply_filters('wpseo_sitemap_urlimages', $images, $post->ID);
        return $images;
    }

Usage Example

 /**
  * Produce array of URL parts for given post object.
  *
  * @param object $post Post object to get URL parts for.
  *
  * @return array|bool
  */
 protected function get_url($post)
 {
     $url = array();
     /**
      * Filter the URL Yoast SEO uses in the XML sitemap.
      *
      * Note that only absolute local URLs are allowed as the check after this removes external URLs.
      *
      * @param string $url  URL to use in the XML sitemap
      * @param object $post Post object for the URL.
      */
     $url['loc'] = apply_filters('wpseo_xml_sitemap_post_url', get_permalink($post), $post);
     /**
      * Do not include external URLs.
      *
      * @see https://wordpress.org/plugins/page-links-to/ can rewrite permalinks to external URLs.
      */
     if (false === strpos($url['loc'], $this->get_home_url())) {
         return false;
     }
     $modified = max($post->post_modified_gmt, $post->post_date_gmt);
     if ($modified !== '0000-00-00 00:00:00') {
         $url['mod'] = $modified;
     }
     $url['chf'] = WPSEO_Sitemaps::filter_frequency($post->post_type . '_single', 'weekly', $url['loc']);
     $canonical = WPSEO_Meta::get_value('canonical', $post->ID);
     if ($canonical !== '' && $canonical !== $url['loc']) {
         /*
         Let's assume that if a canonical is set for this page and it's different from
            the URL of this post, that page is either already in the XML sitemap OR is on
            an external site, either way, we shouldn't include it here.
         */
         return false;
     }
     unset($canonical);
     if ($this->options['trailingslash'] === true && $post->post_type !== 'post') {
         $url['loc'] = trailingslashit($url['loc']);
     }
     $url['pri'] = $this->calculate_priority($post);
     $url['images'] = $this->image_parser->get_images($post);
     return $url;
 }