<?php
|
namespace JVBase\utility;
|
|
use JVBase\managers\Cache;
|
use JVBase\managers\SEO\render\Thing\CreativeWork\MediaObject\VideoObject;
|
|
if (!defined('ABSPATH')) {
|
exit;
|
}
|
|
class Video
|
{
|
protected ?Cache $cache = null;
|
protected ?Cache $vidData = null;
|
|
public function __construct()
|
{
|
$this->cache = Cache::for('videos')->connect('post');
|
$this->vidData = Cache::for('vidData')->connect('post');
|
if (JVB_TESTING) {
|
$this->cache->flush();
|
$this->vidData->flush();
|
}
|
}
|
public static function get(int $ID, array $args = []):string
|
{
|
return (new self())->formatVideo($ID, $args);
|
}
|
public function formatVideo(int $ID, array $args = []):string
|
{
|
return $this->cache->remember(
|
$this->cache->generateKey(array_merge(['id' => $ID], $args)),
|
function() use ($ID, $args) {
|
$url = wp_get_attachment_url($ID);
|
$meta = wp_get_attachment_metadata($ID);
|
$attachmentPost = get_post($ID);
|
|
jvbDump($meta, 'video meta');
|
|
$title = ($attachmentPost && $attachmentPost->post_title !== '') ? $attachmentPost->post_title : '';
|
$caption = wp_get_attachment_caption($ID);
|
|
$video = new VideoObject();
|
$video->setName($title);
|
$video->setContentUrl($url);
|
|
if ($attachmentPost) {
|
$video->setUploadDate($attachmentPost->post_date);
|
}
|
|
if (isset($meta['height'])) {
|
$video->setHeight($meta['height'].'px');
|
}
|
if (isset($meta['width'])) {
|
$video->setWidth($meta['width'].'px');
|
}
|
if (isset($meta['mime_type'])) {
|
$video->setEncodingFormat($meta['mime_type']);
|
}
|
if (isset($meta['filesize'])) {
|
$video->setContentSize($meta['filesize']);
|
}
|
if (isset($meta['length'])) {
|
$video->setDuration($meta['length']);
|
}
|
if (!empty($caption)) {
|
$video->setDescription($caption);
|
}
|
|
$schema = sprintf(
|
'<script type="application/ld+json">%s</script>',
|
json_encode($video->outputSchema())
|
);
|
|
return sprintf(
|
'%s<video width="%s" height="%s" controls>
|
<source src="%s" type="%s">Your browser does not support the video tag
|
</video>',
|
$schema,
|
$meta['width'],
|
$meta['height'],
|
$url,
|
$meta['mime_type']
|
);
|
|
|
|
}
|
);
|
}
|
|
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 '';
|
}
|
|
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 Video)->getImageData($imgID);
|
}
|
|
}
|