<?php
|
namespace JVBase\utility;
|
|
use JVBase\managers\CacheManager;
|
|
if (!defined('ABSPATH')) {
|
exit;
|
}
|
/**
|
* Validates configuration arrays for content types, taxonomies, and user roles
|
* Catches errors early in the registration process
|
*/
|
class Image
|
{
|
protected ?CacheManager $cache = null;
|
|
public function __construct()
|
{
|
$this->cache = CacheManager::for('images')->connectTo('post', 'attachment');
|
}
|
|
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],
|
function() use ($ID, $start, $replace) {
|
$img = wp_get_attachment_image_src($ID, $start);
|
if (!$img) {
|
return'';
|
}
|
$img = $img[0];
|
|
$data = $this->getGallerySizes($ID, $replace);
|
|
|
|
$alt = get_post_meta($ID, '_wp_attachment_image_alt', true);
|
$alt = ($alt=='')? '' : ' alt="'.$alt.'" ';
|
return '<img width="100%" height="auto" src="'.$img.'"'.$alt.$data.' loading="lazy" decoding="async">';
|
}
|
);
|
|
$aOpen = $aClose = '';
|
if ($addLink) {
|
if (!$postSlug) {
|
global $post;
|
$postSlug = $post->post_name;
|
}
|
|
$imgPost = get_post($ID);
|
if (!$imgPost) {
|
return $return;
|
}
|
$imgSlug = $imgPost->post_name;
|
$aOpen = '<a class="open-gallery" target="_blank" rel="nofollow" data-opens="gallery-'.$postSlug.'" data-focus="'.$postSlug.'-'.$imgSlug.'">';
|
$aClose = '</a>';
|
}
|
return $aOpen.$return.$aClose;
|
}
|
|
public function getGallerySizes(int $ID, string $replace):string
|
{
|
if (!wp_get_attachment_image_src($ID)) {
|
return '';
|
}
|
if (!has_image_size($replace)) {
|
$replace = 'large';
|
}
|
|
if ($replace == 'large') {
|
return 'data-small="' .
|
wp_get_attachment_image_src($ID)[0] . '" data-medium="' .
|
wp_get_attachment_image_src($ID, 'large')[0] . '" data-full="' .
|
wp_get_attachment_image_src($ID, 'full')[0] . '"';
|
} elseif ($replace == 'medium') {
|
return 'data-small="'.
|
wp_get_attachment_image_src($ID, 'large')[0].'" data-medium="'.
|
wp_get_attachment_image_src($ID, 'large')[0].'" data-full="'.
|
wp_get_attachment_image_src($ID, 'large')[0].'"';
|
} elseif ($replace == 'thumbnail') {
|
return 'data-small="'.
|
wp_get_attachment_image_src($ID, 'medium')[0].'" data-medium="'.
|
wp_get_attachment_image_src($ID, 'medium')[0].'" data-full="'.
|
wp_get_attachment_image_src($ID, 'medium')[0].'"';
|
}
|
return '';
|
}
|
}
|