Jake Vanderwerf
8 days ago 3b83905603d44b1a08f8b2b36a605808ce686ad6
inc/blocks/VideoCoverBlock.php
@@ -1,6 +1,8 @@
<?php
namespace JVBase\blocks;
use JVBase\managers\SEO\render\Thing\CreativeWork\MediaObject\VideoObject;
if (!defined('ABSPATH')) {
   exit;
}
@@ -14,7 +16,7 @@
class VideoCoverBlock
{
   protected static ?VideoCoverBlock $instance = null;
   protected string $path = JVB_DIR . '/build/video-cover';
   protected string $path = JVB_DIR . '/build/video';
   public static function getInstance(): VideoCoverBlock
   {
@@ -41,23 +43,30 @@
    */
   public function render($attributes, $content): string
   {
      // Extract attributes with defaults
      $poster_id = $attributes['posterId'] ?? 0;
      $video_sources = $attributes['videoSources'] ?? [];
      $mobile_sources = $attributes['mobileSources'] ?? [];
      $css_class = $attributes['className'] ?? '';
      $fade_class = $attributes['fadeEffect'] ?? false ? 'fade' : '';
      $overlay_opacity = $attributes['overlayOpacity'] ?? 0;
      $content_alignment = $attributes['contentAlignment'] ?? 'center';
      $min_height = $attributes['minHeight'] ?? 0;
      //Get date of current post
      global $post;
      $date = date('c',strtotime($post->post_date));
      $title = $attributes['title'] ?? $post->post_title;
      $description = $attributes['description'] ?? $post->post_excerpt;
      // If no video sources, return empty
      if (empty($video_sources)) {
         return '';
      }
      $video_id = $video_sources[0]['id'] ?? 0;
      $video_post = $video_id ? get_post($video_id) : null;
      $title = $video_post->post_title ?? $post->post_title;
      $description = $video_post->post_content ?? $post->post_excerpt;
      // Get poster URL
@@ -66,59 +75,70 @@
      // Build video tag
      $classes = trim("video-cover {$fade_class} {$css_class}");
      $html = '<section class="'.esc_attr($classes).'">
      <script type="application/ld+json">
        {
            "@context": "https://schema.org/",
            "@type": "VideoObject",
            "name": "'.$title.'",
            "thumbnailUrl": "'.$poster_url.'",
            "contentUrl": "'.$video_sources[0]['url'].'",
            "description": "'.$description.'",
            "uploadDate": "'.$date.'"
        }
    </script>
    <div class="wrap">
        <div class="video-container">';
      $html .= '<video';
      $html .= ' muted loop playsinline autoplay';
      $video = new VideoObject();
      $video->setName($title);
      $video->setThumbnailUrl($poster_url);
      $video->setContentUrl($video_sources[0]['url']);
      $video->setDescription($description);
      $video->setUploadDate($date);
      if ($poster_url) {
         $html .= ' poster="' . esc_url($poster_url) . '"';
      }
      $html = sprintf(
         '<section class="%s">
      <script type="application/ld+json">%s</script>
    <div class="wrap abs edges">
        <div class="video-container">',
         esc_attr($classes),
      json_encode($video->outputSchema())
   );
      $html .= '>';
      $poster = $poster_url ? sprintf(
         ' poster="%s"',
         esc_url($poster_url)
      ) : '';
      // Add mobile sources first (lower resolution)
      foreach ($mobile_sources as $source) {
         if (!empty($source['url']) && !empty($source['mime'])) {
            $html .= '<source';
            $html .= ' src="' . esc_url($source['url']) . '"';
            $html .= ' type="' . esc_attr($source['mime']) . '"';
            $html .= ' media="(max-width: 767px)"';
            $html .= '>';
         }
      }
      $html .= sprintf(
         '<video muted loop playsinline autoplay%s fetch-priority="high">',
         $poster
      );
      // Add desktop sources
      $html .= ' fetch-priority="high">';
      foreach ($video_sources as $source) {
         if (!empty($source['url']) && !empty($source['mime'])) {
            $html .= '<source';
            $html .= ' src="' . esc_url($source['url']) . '"';
            $html .= ' type="' . esc_attr($source['mime']) . '"';
            // Add media query for desktop if mobile sources exist
            if (!empty($mobile_sources)) {
               $html .= ' media="(min-width: 768px)"';
            }
            $html .= '>';
            $html .= sprintf(
               '<source data-src="%s" type="%s">',
               esc_url($source['url']),
               esc_attr($source['mime'])
            );
         }
      }
      $html .= '</video>';
      $html .= '</div></div><div class="inner-wrap"></div></section>';
      $inner_content = $this->extractInnerContent($content);
      $html .= sprintf(
         '</div></div><div class="inner-wrap">%s</div></section>',
         $inner_content
      );
      return $html;
   }
   /**
    * Extract inner content from the saved block content
    * Removes the wrapper div and returns just the inner blocks HTML
    */
   protected function extractInnerContent(string $content): string
   {
      if (empty($content)) {
         return '';
      }
      // Remove the placeholder wrapper div
      $content = preg_replace('/<div[^>]*class="[^"]*video-cover-wrapper-placeholder[^"]*"[^>]*>/', '', $content, 1);
      $content = preg_replace('/<\/div>\s*$/', '', $content, 1);
      return trim($content);
   }
}