Jake Vanderwerf
2026-08-25 3d25ad7de4fdbb3eba2cde2a62422e6be5d79a0a
inc/rest/routes/SEORoutes.php
@@ -1,15 +1,15 @@
<?php
namespace JVBase\rest\routes;
use JVBase\base\SchemaHelper;
use JVBase\Instance;
use JVBase\managers\Cache;
use JVBase\managers\SEO\render\SchemaOutput;
use JVBase\registrar\Registrar;
use JVBase\rest\Rest;
use JVBase\managers\Cache;
use JVBase\managers\SEO\ConfigManager;
use JVBase\managers\SEO\SchemaBuilder;
use JVBase\rest\Route;
use WP_REST_Request;
use WP_REST_Response;
use WP_Error;
if (!defined('ABSPATH')) {
   exit;
@@ -23,12 +23,14 @@
 */
class SEORoutes extends Rest
{
   use Instance;
// protected SchemaBuilder $registry;
   public function __construct()
   {
      $this->cacheName = 'schema';
      parent::__construct();
      $this->cache = Cache::for($this->cacheName, null, $this->cacheTtl);
//    $this->registry = SchemaBuilder::getInstance();
   }
@@ -106,7 +108,7 @@
   {
      $errors = [];
      $config = ConfigManager::for($context);
      $config = SchemaHelper::getInstance()::for($context);
      $result = $config->updateConfig($data);
      if (is_wp_error($result)) {
@@ -131,7 +133,7 @@
    */
   protected function saveTypeSettings(string $type, array $data): WP_REST_Response
   {
      $config = ConfigManager::for($type);
      $config = SchemaHelper::getInstance();
      // Separate meta and schema data if needed
      $meta = $data['meta'] ?? [];
@@ -141,23 +143,17 @@
      // Save meta configuration
      if (!empty($meta)) {
         $metaResult = $config->updateMeta($meta);
         if (is_wp_error($metaResult)) {
            $errors['meta'] = [
               'message' => $metaResult->get_error_message(),
               'errors' => $metaResult->get_error_data()
            ];
         $metaResult =  $config->updateMeta($type, $meta);
         if (!$metaResult) {
            $errors[] = 'meta';
         }
      }
      // Save schema configuration
      if (!empty($schema)) {
         $schemaResult = $config->updateConfig($schema);
         if (is_wp_error($schemaResult)) {
            $errors['schema'] = [
               'message' => $schemaResult->get_error_message(),
               'errors' => $schemaResult->get_error_data()
            ];
         $schemaResult = $config->updateSchema($type, $schema);
         if (!$schemaResult) {
            $errors[] = 'schema';
         }
      }
@@ -179,23 +175,11 @@
      $context = $request->get_param('context');
      $data = $request->get_json_params();
      $resetMeta = $data['resetMeta'] ?? false;
      $resetSchema = $data['resetSchema'] ?? false;
      $config = ConfigManager::for($context);
      // Reset based on what was requested
      if ($resetMeta) {
         $config->resetMeta();
      }
      if ($resetSchema) {
         $config->resetConfig();
      }
      if (!$resetMeta && !$resetSchema) {
         // Default: reset everything
         $config->resetAll();
      $type = $data['type']??false;
      $format = $data['resetMeta'] ? 'meta' : ($data['resetSchema'] ? 'schema' : ($data['resetArchive'] ? 'archive' : false));
      $hasIt = $type && $format;
      if ($hasIt) {
         SchemaHelper::reset($type, $format);
      }
      // Invalidate cache
@@ -204,8 +188,7 @@
      return $this->success([
         'status' => 'completed',
         'message' => 'Reset to defaults successfully',
         'meta' => $config->meta(),
         'schema' => $config->schema()
         'result' => $hasIt ? SchemaHelper::getConfig($type, $format) : [],
      ]);
   }
@@ -215,25 +198,42 @@
   protected function previewSchema(WP_REST_Request $request): WP_REST_Response
   {
      $data = $request->get_json_params();
      $schemaType = $data['type'] ?? 'Thing';
      // Get field definitions from registry
      $fieldDefinitions = $this->registry->getMetaConfigForType($schemaType);
      //Whether it is the Schema, Meta, Archive, or Reference schema
      $format = $data['format']??false;
      if (!$format) {
         return $this->error('Required field "Format" missing');
      }
      //WebSite, Organization, or any registered post type/taxonomy
      $type = $data['type']??false;
      if (!$type) {
         return $this->error('Required field "Type" missing');
      }
      $class = $data['class']??false;
      if (!$class) {
         return $this->error('Required "Schema Class" field missing (Ex: Thing, LocalBusiness, etc)');
      }
      // Build schema with actual form values
      $schema = [
         '@type' => $schemaType,
         '@id'   => get_home_url() . '/#' . strtolower($schemaType),
         '@type' => $class,
      ];
      $theClass = SchemaHelper::classFromConfig($schema);
      // Add fields with their actual values
      foreach ($data['fields'] ?? [] as $fieldName => $value) {
         if (empty($value)) {
            continue;
         }
         $schema[$fieldName] = $value;
         $method = 'set'.ucfirst($fieldName);
         if (!method_exists($theClass, $method)) {
            error_log('Attempted field not found in class '.$class.': '.$fieldName);
            continue;
      }
         $theClass->$method($value);
      }
      $schema = $theClass->outputSchema();
      return $this->success([
         'schema' => $schema
@@ -243,11 +243,14 @@
   /**
    * Get fields for a schema type
    * Used for dynamic type switching
    * TODO
    */
   public function getFields(WP_REST_Request $request): WP_REST_Response
   {
      $type = $request->get_param('type');
      $class = SchemaHelper::classFromConfig(['type' => $type]);
      // Get Meta field definitions from registry
      $fields = $this->registry->getMetaConfigForType($type);