Jetpack_PostImages::from_slideshow PHP Method

from_slideshow() static public method

If a slideshow is embedded within a post, then parse out the images involved and return them
static public from_slideshow ( $post_id, $width = 200, $height = 200 )
    static function from_slideshow($post_id, $width = 200, $height = 200)
    {
        $images = array();
        $post = get_post($post_id);
        if (!$post) {
            return $images;
        }
        if (!empty($post->post_password)) {
            return $images;
        }
        if (false === has_shortcode($post->post_content, 'slideshow')) {
            return $images;
            // no slideshow - bail
        }
        $permalink = get_permalink($post->ID);
        // Mechanic: Somebody set us up the bomb
        $old_post = $GLOBALS['post'];
        $GLOBALS['post'] = $post;
        $old_shortcodes = $GLOBALS['shortcode_tags'];
        $GLOBALS['shortcode_tags'] = array('slideshow' => $old_shortcodes['slideshow']);
        // Find all the slideshows
        preg_match_all('/' . get_shortcode_regex() . '/sx', $post->post_content, $slideshow_matches, PREG_SET_ORDER);
        ob_start();
        // The slideshow shortcode handler calls wp_print_scripts and wp_print_styles... not too happy about that
        foreach ($slideshow_matches as $slideshow_match) {
            $slideshow = do_shortcode_tag($slideshow_match);
            if (false === ($pos = stripos($slideshow, 'jetpack-slideshow'))) {
                // must be something wrong - or we changed the output format in which case none of the following will work
                continue;
            }
            $start = strpos($slideshow, '[', $pos);
            $end = strpos($slideshow, ']', $start);
            $post_images = json_decode(wp_specialchars_decode(str_replace("'", '"', substr($slideshow, $start, $end - $start + 1)), ENT_QUOTES));
            // parse via JSON
            foreach ($post_images as $post_image) {
                if (!($post_image_id = absint($post_image->id))) {
                    continue;
                }
                $meta = wp_get_attachment_metadata($post_image_id);
                // Must be larger than 200x200 (or user-specified)
                if (!isset($meta['width']) || $meta['width'] < $width) {
                    continue;
                }
                if (!isset($meta['height']) || $meta['height'] < $height) {
                    continue;
                }
                $url = wp_get_attachment_url($post_image_id);
                $images[] = array('type' => 'image', 'from' => 'slideshow', 'src' => $url, 'src_width' => $meta['width'], 'src_height' => $meta['height'], 'href' => $permalink);
            }
        }
        ob_end_clean();
        // Operator: Main screen turn on
        $GLOBALS['shortcode_tags'] = $old_shortcodes;
        $GLOBALS['post'] = $old_post;
        return $images;
    }

Usage Example

 /**
  * @author scotchfield
  * @covers Jetpack_PostImages::from_slideshow
  * @since 3.2
  */
 public function test_from_slideshow_is_array()
 {
     require_once plugin_dir_path(realpath(dirname(__FILE__) . '/../../modules/shortcodes/slideshow.php')) . 'slideshow.php';
     $slideshow = new Jetpack_Slideshow_Shortcode();
     $post_id = $this->factory->post->create(array('post_content' => '[slideshow]'));
     $images = Jetpack_PostImages::from_slideshow($post_id);
     $this->assertInternalType('array', $images);
 }
All Usage Examples Of Jetpack_PostImages::from_slideshow