/** * Get attachment images * * @since 1.0 * * @param int $id * @param string $size * @param string $format * @return array */ public static function getAttachmentImages($id, $size, $format = 'array') { // Meta data if (($metadata = wp_get_attachment_metadata($id)) === false || !isset($metadata['sizes'][$size])) { return $format == 'html' ? '' : array(); } $file = explode('/', $metadata['file']); $metadata['sizes']['full'] = array('file' => array_pop($file), 'width' => $metadata['width'], 'height' => $metadata['height']); // Current $current = $metadata['sizes'][$size]; $current['ratio'] = $current['width'] / $current['height']; $current['crop'] = isset($GLOBALS['_wp_additional_image_sizes'][$size]) ? $GLOBALS['_wp_additional_image_sizes'][$size]['crop'] : null; // Images $images = array(); foreach ($metadata['sizes'] as $_size => $size) { if ($size['width'] > $current['width'] && ($current['crop'] === false && isset($GLOBALS['_wp_additional_image_sizes'][$_size]) && !$GLOBALS['_wp_additional_image_sizes'][$_size]['crop'] || abs($size['width'] / $size['height'] - $current['ratio']) <= 0.015)) { list($images[$size['width']]) = wp_get_attachment_image_src($id, $_size); } } // Sort ksort($images); // Keys prefix $images = \Drone\Func::arrayKeysMap(function ($s) { return 'data-image' . $s; }, $images); // Output switch ($format) { case 'html': return \Drone\Func::arraySerialize($images, 'html'); default: return $images; } }
protected function getSlider() { // Data $data = array_map(function ($e) { return is_bool($e) || $e === '' ? Func::boolToString($e) : $e; }, $this->so_('slider')->toArray()); $data = Func::arrayKeysMap(function ($k) { return 'slider-' . $k; }, $data); // HTML $html = HTML::ul()->id('gallery-' . ++$this->instance_id)->class('slider')->data($data); foreach ($this->getAttachments() as $attachment) { $li = $html->addNew('li'); // Caption $caption = HTML::make(); if ($this->so('captions') && ($excerpt = trim($attachment->post_excerpt))) { $caption->addNew('h3')->add(wptexturize($excerpt)); } // Hyperlink and image if ($attachment->post_content && preg_match('#<iframe.*?>\\s*</iframe>#i', $attachment->post_content, $matches)) { $iframe = preg_replace_callback('#src="(?P<url>(https?:)?//www.youtube.com/embed/[-_a-z0-9]+)\\??(?P<get>.*?)"#i', function ($m) { return sprintf('src="%s?wmode=opaque&enablejsapi=1%s"', $m['url'], isset($m['get']) && $m['get'] ? '&' . $m['get'] : ''); }, $attachment->post_content); $li->addNew('div')->class('embed')->add($iframe); } else { if ($url = $this->getAttachmentLinkURI($attachment)) { $li->addNew('a')->attr(\Everything::getImageAttrs('a', $this->so_('image')->toArray()))->data('fancybox-group', $html->id)->data('fancybox-title', $attachment->post_excerpt)->href($url)->add(wp_get_attachment_image($attachment->ID, 'auto', false, array('title' => $caption->html()))); } else { $li->addNew('div')->attr(\Everything::getImageAttrs('div', $this->so_('image')->toArray()))->add(wp_get_attachment_image($attachment->ID, 'auto', false, array('title' => $caption->html()))); } } } return $html; }