Jake Vanderwerf
4 hours ago 56a9a1ccf764ff7a6af8f8a2292cb07443cb4aa7
inc/registrar/config/seo/Resolver.php
@@ -4,6 +4,7 @@
use JVBase\managers\Cache;
use JVBase\managers\SEO\render\Thing\CreativeWork\MediaObject\ImageObject;
use JVBase\managers\SEO\render\Thing\Intangible\Quantity\Distance;
use JVBase\managers\SEO\render\Thing\Intangible\StructuredValue\QuantitativeValue;
use JVBase\meta\Meta;
use JVBase\registrar\Registrar;
@@ -16,6 +17,13 @@
   public static function resolve(string $template, ?Meta $meta): string
   {
      if (!$meta) {
         if (is_singular()){
            $meta = Meta::forPost(get_the_ID());
         }else if (is_tax()) {
            $meta = Meta::forTerm(get_queried_object()->term_id);
         }
      }
      self::$meta = $meta;
      return preg_replace_callback(
         '/\{\{([^}]+)}}/',
@@ -29,9 +37,17 @@
      switch ($variable) {
         case 'CREATOR':
            return JVB()->seo()->getCreator();
         case 'name':
            if (!$meta && is_post_type_archive()) {
               $obj = get_queried_object();
               $registrar = Registrar::getInstance($obj->name);
               return $registrar ? $registrar->getPlural() : $obj->label;
            }
            break;
      }
      if (str_contains($variable, '.')) {
      if (str_contains($variable, '.') && $meta) {
         return self::resolveRelation($variable, $meta);
      }
      if ($meta && $variable === 'post_permalink') {
@@ -45,6 +61,10 @@
         error_log('[SEO]Meta Resolver. Could not find meta configuration for variable: '.$variable);
         return '';
      }
      if ($variable === 'post_content') {
         return self::resolvePostContent($variable, $meta);
      }
      return match($config['type']) {
         'upload', 'image', 'gallery' => self::resolveImage($variable, $meta),
         'selector', 'taxonomy', 'user', 'post' => self::resolveObject($variable, $meta),
@@ -58,6 +78,7 @@
      $relation = array_shift($parts);
      $field = implode('.', $parts);
      //We need to:
         // 1) Get the id of the item we're fetching (meta value of the $relation)
      if ($relation === 'registrar') {
@@ -69,32 +90,48 @@
         }
      }
      if (!$meta) {
         return'';
      }
      $ID = $meta->get($relation);
      if (!$ID || $ID === '') {
      if (empty($ID)) {
         return '';
      }
      $IDs = explode(',',$ID);
         // 2) Create new Meta for that relation
      $config = $meta->config($relation);
      if (!$config) {
         return '';
      }
      $type = false;
      if ($config['type'] === 'taxonomy' || array_key_exists('taxonomy', $config)) {
         $type = 'taxonomy';
      } elseif ($config['type'] === 'user'){
         $type = 'term';
      } elseif ($config['type'] === 'user' || array_key_exists('user', $config)){
         $type = 'user';
      } elseif ($config['type'] === 'post'){
      } elseif ($config['type'] === 'post' || array_key_exists('post', $config)){
         $type = 'post';
      }
      if (!$type) {
         error_log('[SEO]Meta Resolver. Could not find type for relation: '.$relation.': '.$field);
         return '';
      }
      $newMeta = new Meta($ID, $type);
      $results = [];
      foreach ($IDs as $ID) {
         $newMeta = new Meta((int)$ID, $type);
         // 3) Pass to resolver
      return self::resolve($field, $newMeta);
         $results[] =  self::resolveVariable($field, $newMeta);
      }
      return jvbCommaList($results);
   }
   protected static function resolvePostContent(string $variable, ?Meta $meta):string{
      return wp_strip_all_tags(str_replace("\n", '', $meta->get('post_content')));
   }
   protected static function resolveImage(string $variable, ?Meta $meta, bool $returnID = false): string
   {
      $imgID = $meta->get($variable);
@@ -133,7 +170,11 @@
         return $checkType;
      }
      error_log('[SEO]Resolver - No method found for '.$property.' with value: '.print_r($value, true).'. Defaulting to base Resolver');
      $ignore = ['description', 'name'];
      if (JVB_TESTING && !in_array($property, $ignore)) {
         error_log('[SEO]Resolver - No method found for '.$property.' with value: '.print_r($value, true).'. Defaulting to base Resolver');
      }
      return self::resolve($value, $meta);
   }
@@ -171,11 +212,13 @@
            function () use ($imgID, $img) {
               $imageObject = new ImageObject();
               $imageObject->setContentUrl($img[0]);
               $width = new Distance();
               $width->setName($img[1].' px');
               $width = new QuantitativeValue();
               $width->setValue($img[1]);
               $width->setUnitText('px');
               $imageObject->setWidth($width);
               $height = new Distance();
               $height->setName($img[2].' px');
               $height = new QuantitativeValue();
               $height->setValue($img[2]);
               $width->setUnitText('px');
               $imageObject->setHeight($height);
               $image_path = get_attached_file($imgID);