Jake Vanderwerf
22 hours ago f4be611c51473359e6d41780f0313c446079e9d3
base/options.php
@@ -1,7 +1,145 @@
<?php
namespace JVBase\base;
use JVBase\managers\Cache;
use JVBase\meta\Form;
use JVBase\meta\Meta;
use JVBase\registrar\Fields;
/**
 * Custom options for the site can be set here. They will need to be handled by the child plugin, but you can make use of the MetaManager.php this way
 * Custom options for the site can be set here. They will need to be handled by the child plugin, but you can make use of the Meta.php this way
 */
$options = apply_filters('jvb_options', []);
define('JVB_OPTIONS', $options);
class Options {
   protected static Options $instance;
   protected array $resetFields = [];
   protected string $resetInterval = 'daily';
   protected Fields $fields;
   protected array $sections = [];
   protected static array $values = [];
   private function __construct(){
      $this->setFields();
      if (!empty($this->resetFields)) {
         add_action('jvbDailyReset', [$this, 'resetOptions']);
      }
   }
   public static function getInstance():Options {
      if (!isset(self::$instance)) {
         self::$instance = new self();
         do_action('jvb_define_options');
      }
      return self::$instance;
   }
   public function setFields():void
   {
      $this->fields = new Fields('options');
      if (Site::has('limit_hours')) {
         $this->fields->addField(
            'today_hours',
            [
               'type'   => 'group',
               'label'  => 'Today\'s Hours',
               'fields' => [
                  'time_start'   => [
                     'type'   => 'time',
                     'label'  => 'Open',
                  ],
                  'time_end'  => [
                     'type'   => 'time',
                     'label'  => 'Closed',
                  ]
               ]
            ]
         );
         $this->fields->addField(
            'open_to_public',
            [
               'type'   => 'true_false',
               'label'  => 'Open to Public?'
            ]
         );
         if (Site::hasAnyIntegration(['facebook','instagram'])) {
            $this->fields->addField(
               'post_to_social',
               [
                  'type'   => 'true_false',
                  'label'  => 'Post to Socials?',
               ]
            );
         }
      }
      if (Site::hasIntegration('gmb') || Site::has('hours')) {
         $this->fields->addCommon('hours');
      }
   }
   public function fields():Fields
   {
      return $this->fields;
   }
   public function resetOptions():void
   {
      if (empty($this->resetFields)) {
         return;
      }
      foreach ($this->resetFields as $field) {
         self::delete($field);
      }
      Cache::for('options')->flush();
   }
   public static function get(string $fieldName):mixed
   {
      if (!array_key_exists($fieldName, self::$values)) {
         $meta = Meta::forOptions();
         self::$values[$fieldName] = $meta->get($fieldName);
      }
      return self::$values[$fieldName];
   }
   public static function delete(string $fieldName):void
   {
      if (array_key_exists($fieldName, self::$values)) {
         unset(self::$values[$fieldName]);
      }
      $meta = Meta::forOptions();
      $meta->delete($fieldName);
   }
   public static function set(string $fieldName, mixed $value):void
   {
      $meta = Meta::forOptions();
      $meta->set($fieldName, $value);
      self::$values = $value;
   }
   public static function render(array $fieldNames, array $options = [], bool $output = false):string
   {
      $meta = Meta::forOptions();
      $result = Form::renderFormFrom(
         $meta,
         'options',
         $options,
         $fieldNames
      );
      if ($output) {
         echo $result;
      }
      return $result;
   }
   public function getFields():array
   {
      return array_map(function ($field) {
         return $field->getConfig();
      },
         $this->fields->getFields()
      );
   }
}