Jetpack_PostImages::from_gallery PHP Method

    static function from_gallery($post_id)
    {
        $images = array();
        $post = get_post($post_id);
        if (!$post) {
            return $images;
        }
        if (!empty($post->post_password)) {
            return $images;
        }
        $permalink = get_permalink($post->ID);
        $galleries = get_post_galleries($post->ID, false);
        foreach ($galleries as $gallery) {
            if (isset($gallery['type']) && 'slideshow' === $gallery['type'] && !empty($gallery['ids'])) {
                $image_ids = explode(',', $gallery['ids']);
                $image_size = isset($gallery['size']) ? $gallery['size'] : 'thumbnail';
                foreach ($image_ids as $image_id) {
                    $image = wp_get_attachment_image_src($image_id, $image_size);
                    if (!empty($image[0])) {
                        list($raw_src) = explode('?', $image[0]);
                        // pull off any Query string (?w=250)
                        $raw_src = wp_specialchars_decode($raw_src);
                        // rawify it
                        $raw_src = esc_url_raw($raw_src);
                        // clean it
                        $images[] = array('type' => 'image', 'from' => 'gallery', 'src' => $raw_src, 'href' => $permalink);
                    }
                }
            } elseif (!empty($gallery['src'])) {
                foreach ($gallery['src'] as $src) {
                    list($raw_src) = explode('?', $src);
                    // pull off any Query string (?w=250)
                    $raw_src = wp_specialchars_decode($raw_src);
                    // rawify it
                    $raw_src = esc_url_raw($raw_src);
                    // clean it
                    $images[] = array('type' => 'image', 'from' => 'gallery', 'src' => $raw_src, 'href' => $permalink);
                }
            }
        }
        return $images;
    }

Usage Example

Example #1
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);
     return Jetpack_Media_Meta_Extractor::build_image_struct($image_list);
 }
All Usage Examples Of Jetpack_PostImages::from_gallery