From f4be611c51473359e6d41780f0313c446079e9d3 Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Tue, 09 Jun 2026 15:19:24 +0000
Subject: [PATCH] =Switched the /base/options.php to the same pattern as Site.php: a class based approached rather than a filter. Updated Meta.php to play along with the defined fields from there in Meta::forOptions. Had to change openingHoursSpecificationsTrait.php to not use the translater functions __('text','textdomain') for now, as we load before init.
---
base/options.php | 140 ++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 139 insertions(+), 1 deletions(-)
diff --git a/base/options.php b/base/options.php
index 70f5fb1..50f0979 100644
--- a/base/options.php
+++ b/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()
+ );
+ }
+}
--
Gitblit v1.10.0