| | |
| | | <?php |
| | | namespace JVBase\integrations; |
| | | |
| | | use JVBase\base\Options; |
| | | use JVBase\meta\Form; |
| | | use JVBase\meta\Meta; |
| | | use JVBase\registrar\Registrar; |
| | | use JVBase\ui\Tabs; |
| | | |
| | | if (!defined('ABSPATH')) { |
| | | exit; |
| | | } |
| | |
| | | * Handles the getting and setting of default values for service fields. |
| | | */ |
| | | trait SyncDefaults { |
| | | use _Base; |
| | | protected array $allContent; |
| | | protected array $enabledTaxonomies; |
| | | protected array $enabledContent; |
| | | protected array $enabledUsers; |
| | | public function enabledContentTypes():array |
| | | { |
| | | //Build an array of post type/taxonomy slugs => integration content type |
| | | if (!isset($this->allContent)) { |
| | | $content = Registrar::withIntegration($this->service_name); |
| | | $this->allContent = []; |
| | | foreach ($content as $c) { |
| | | $registrar = Registrar::getInstance($c); |
| | | if ($registrar){ |
| | | $this->allContent[$c] = $registrar->getIntegration($this->service_name)->getContentType(); |
| | | } |
| | | } |
| | | } |
| | | return $this->allContent; |
| | | } |
| | | public function enabledContent():array |
| | | { |
| | | if (!isset($this->enabledContent)) { |
| | | $content = Registrar::withIntegration($this->service_name, 'post'); |
| | | $this->enabledContent = []; |
| | | foreach ($content as $c) { |
| | | $registrar = Registrar::getInstance($c); |
| | | if ($registrar){ |
| | | $this->enabledContent[$c] = $registrar->getIntegration($this->service_name)->getContentType(); |
| | | } |
| | | } |
| | | } |
| | | return $this->enabledContent; |
| | | } |
| | | public function enabledTaxonomies():array |
| | | { |
| | | if (!isset($this->enabledTaxonomies)) { |
| | | $content = Registrar::withIntegration($this->service_name, 'term'); |
| | | $this->enabledTaxonomies = []; |
| | | foreach ($content as $c) { |
| | | $registrar = Registrar::getInstance($c); |
| | | if ($registrar){ |
| | | $this->enabledTaxonomies[$c] = $registrar->getIntegration($this->service_name)->getContentType(); |
| | | } |
| | | } |
| | | } |
| | | return $this->enabledTaxonomies; |
| | | } |
| | | public function enabledUsers():array |
| | | { |
| | | if (!isset($this->enabledUsers)) { |
| | | $content = Registrar::withIntegration($this->service_name, 'user'); |
| | | $this->enabledUsers = []; |
| | | foreach ($content as $c) { |
| | | $registrar = Registrar::getInstance($c); |
| | | if ($registrar){ |
| | | $this->enabledUsers[$c] = $registrar->getIntegration($this->service_name)->getContentType(); |
| | | } |
| | | } |
| | | } |
| | | return $this->enabledUsers; |
| | | } |
| | | |
| | | public function renderDefaults():void |
| | | { |
| | | $tabs = new Tabs(); |
| | | |
| | | $types = [ |
| | | 'enabledContent' => [ |
| | | 'title' => 'Content', |
| | | 'slug' => 'content', |
| | | 'icon' => 'note-blank', |
| | | 'description' => 'Set default values for the meta for different content types here.' |
| | | ], |
| | | 'enabledTaxonomies' => [ |
| | | 'title' => 'Taxonomies', |
| | | 'slug' => 'taxonomies', |
| | | 'icon' => 'folder', |
| | | 'description' => 'Set default values for the meta for different taxonomies here.' |
| | | ], |
| | | 'enabledUsers' => [ |
| | | 'title' => 'Users', |
| | | 'slug' => 'users', |
| | | 'icon' => 'user', |
| | | 'description' => 'Set default values for the meta for different user types here.' |
| | | ] |
| | | ]; |
| | | |
| | | foreach ($types as $method => $options) { |
| | | $registered = $this->$method(); |
| | | if (empty($registered)) { |
| | | continue; |
| | | } |
| | | $role = !is_null($this->userID) ? jvbUserRole($this->userID) : ''; |
| | | $userRegistrar = null; |
| | | if (!empty($role)) { |
| | | $userRegistrar = Registrar::getInstance($role); |
| | | } |
| | | $allowed = is_null($this->userID) ? |
| | | array_keys($registered) : |
| | | array_filter(array_keys($registered), function ($type) use ($userRegistrar) { |
| | | return in_array($type, $userRegistrar->getCreatable()); |
| | | }); |
| | | if (empty($allowed)) { |
| | | continue; |
| | | } |
| | | |
| | | $tab = $tabs->addTab($options['slug']) |
| | | ->icon($options['icon']) |
| | | ->title($options['title']) |
| | | ->description($options['description']) |
| | | ->content($this->getContentTypeContent($allowed)); |
| | | } |
| | | $tabs->render(false); |
| | | } |
| | | public function getContentTypeContent(array $content):string |
| | | { |
| | | $tabs = new Tabs(); |
| | | foreach ($content as $c) { |
| | | $registrar = Registrar::getInstance($c); |
| | | $additionalFields = $registrar->getIntegrationFields($this->service_name); |
| | | $fields = $additionalFields->getIntegrationFields(); |
| | | if (empty($fields)) { |
| | | continue; |
| | | } |
| | | $form = ''; |
| | | foreach ($fields as $name => $config) { |
| | | $form .= Form::render( |
| | | $this->getFieldName($name, $c), |
| | | $this->getDefaultValue($name, $c, $this->userID), |
| | | $config |
| | | ); |
| | | } |
| | | |
| | | $tab = $tabs->addTab($c) |
| | | ->icon($registrar->getIcon()) |
| | | ->title($registrar->getPlural()) |
| | | ->description('Set default values for the meta for '.$registrar->getPlural().' here.') |
| | | ->content($form); |
| | | } |
| | | return $tabs->render(); |
| | | } |
| | | |
| | | protected function getFieldName(string $fieldName, string $type):string |
| | | { |
| | | return $type.'_defaults_'.$fieldName; |
| | | } |
| | | |
| | | public function getDefaultValue(string $fieldName, string $type, ?int $userID = null):mixed |
| | | { |
| | | $name = $this->getFieldName($fieldName, $type); |
| | | if (is_null($userID)) { |
| | | return Options::get($name); |
| | | } else { |
| | | return Meta::forUser($userID)->get($name); |
| | | } |
| | | } |
| | | public function setDefaultMetaValue(string $fieldName, mixed $value, string $type, ?int $userID = null):void |
| | | { |
| | | $name = $this->getFieldName($fieldName, $type); |
| | | if (is_null($userID)) { |
| | | Options::set($name, $value); |
| | | } else { |
| | | Meta::forUser($userID)->set($name, $value); |
| | | } |
| | | } |
| | | } |