| | |
| | | <?php |
| | | namespace JVBase\utility; |
| | | |
| | | use JVBase\managers\CacheManager; |
| | | use JVBase\managers\Cache; |
| | | |
| | | if (!defined('ABSPATH')) { |
| | | exit; |
| | |
| | | */ |
| | | class Image |
| | | { |
| | | protected ?CacheManager $cache = null; |
| | | protected ?Cache $cache = null; |
| | | protected ?Cache $imgData = null; |
| | | |
| | | public function __construct() |
| | | { |
| | | $this->cache = CacheManager::for('images')->connectTo('post', 'attachment'); |
| | | $this->cache = Cache::for('images')->connect('post'); |
| | | $this->imgData = Cache::for('imageData')->connect('post'); |
| | | if (JVB_TESTING) { |
| | | $this->cache->clear(); |
| | | $this->cache->flush(); |
| | | $this->imgData->flush(); |
| | | } |
| | | } |
| | | |
| | | public function formatImage(int $ID, string $start = 'tiny', string $replace = 'large', bool $addLink = true, ?string $postSlug = null):string |
| | | { |
| | | $return = $this->cache->remember( |
| | | ['ID' => $ID, 'start' => $start, 'replace' => $replace], |
| | | $this->cache->generateKey(['ID' => $ID, 'start' => $start, 'replace' => $replace]), |
| | | function() use ($ID, $start, $replace) { |
| | | // Define size order for progressive enhancement |
| | | $sizeOrder = ['tiny', 'directory-preview', 'thumbnail', 'medium', 'large', 'full']; |
| | |
| | | |
| | | if ($addLink) { |
| | | if (!$postSlug) { |
| | | global $post; |
| | | $postSlug = $post->post_name; |
| | | if (is_singular()) { |
| | | global $post; |
| | | $postSlug = $post->post_name; |
| | | }else if (is_tax()) { |
| | | $tax = get_queried_object(); |
| | | $postSlug = jvbNoBase($tax->taxonomy); |
| | | }elseif (is_post_type_archive()) { |
| | | $obj = get_queried_object(); |
| | | $postSlug = jvbNoBase($obj->post_type); |
| | | } |
| | | |
| | | } |
| | | $full = wp_get_attachment_image_src($ID, 'full'); |
| | | |
| | |
| | | $postSlug, |
| | | $postSlug, |
| | | $imgSlug, |
| | | $full[0] |
| | | $full[0]??'' |
| | | ); |
| | | |
| | | // Add gallery attributes to img tag |
| | |
| | | } |
| | | return ''; |
| | | } |
| | | |
| | | public function getImageData(int $imgID):array |
| | | { |
| | | return $this->imgData->remember( |
| | | $imgID, |
| | | function() use ($imgID) { |
| | | if (!wp_get_attachment_image($imgID, 'tiny')) { |
| | | return []; |
| | | } |
| | | return [ |
| | | 'tiny' => wp_get_attachment_image_src($imgID, 'tiny')[0], |
| | | 'small' => wp_get_attachment_image_src($imgID, 'medium')[0], |
| | | 'medium' => wp_get_attachment_image_src($imgID, 'large')[0], |
| | | 'large' => wp_get_attachment_image_src($imgID, 'full')[0], |
| | | 'image-alt-text'=> get_post_meta($imgID, '_wp_attachment_image_alt', true), |
| | | 'image-title' => get_the_title($imgID), |
| | | 'image-caption' => get_the_excerpt($imgID), |
| | | ]; |
| | | } |
| | | ); |
| | | } |
| | | |
| | | public static function getData(int $imgID):array |
| | | { |
| | | return (new Image)->getImageData($imgID); |
| | | } |
| | | |
| | | public static function gallery(string $ids, ?string $type = null):string |
| | | { |
| | | $ids = explode(',', $ids); |
| | | $inner = implode('', array_map(function ($ID) { |
| | | $caption = wp_get_attachment_caption($ID); |
| | | if (!empty($caption)) { |
| | | $caption = sprintf('<figcaption>%s</figcaption', $caption); |
| | | } |
| | | return sprintf( |
| | | '<li><figure>%s%s</figure></li>', |
| | | self::image($ID), |
| | | $caption |
| | | ); |
| | | }, $ids)); |
| | | return sprintf( |
| | | '<ul class="gallery%s">%s</ul>', |
| | | $type? ' '.$type : '', |
| | | $inner |
| | | ); |
| | | } |
| | | |
| | | public static function image(int $id, string $start = 'tiny', string $replace = 'large', bool $addLink = true, ?string $postSlug = null):string |
| | | { |
| | | return (new Image)->formatImage($id, $start, $replace, $addLink, $postSlug); |
| | | } |
| | | |
| | | } |