| | |
| | | <?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; |
| | |
| | | */ |
| | | 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(); |
| | | } |
| | | |
| | |
| | | { |
| | | $errors = []; |
| | | |
| | | $config = ConfigManager::for($context); |
| | | $config = SchemaHelper::getInstance()::for($context); |
| | | $result = $config->updateConfig($data); |
| | | |
| | | if (is_wp_error($result)) { |
| | |
| | | */ |
| | | 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'] ?? []; |
| | |
| | | |
| | | // 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'; |
| | | } |
| | | } |
| | | |
| | |
| | | $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 |
| | |
| | | return $this->success([ |
| | | 'status' => 'completed', |
| | | 'message' => 'Reset to defaults successfully', |
| | | 'meta' => $config->meta(), |
| | | 'schema' => $config->schema() |
| | | 'result' => $hasIt ? SchemaHelper::getConfig($type, $format) : [], |
| | | ]); |
| | | } |
| | | |
| | |
| | | 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 |
| | |
| | | /** |
| | | * 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); |
| | | |