From 9bbeea742424837fb58207d88e10dbca0b2cae04 Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Sun, 03 May 2026 22:04:17 +0000
Subject: [PATCH] =SEO Field registration and formatting
---
inc/managers/SEO/render/Traits/_Properties/openingHoursSpecificationTrait.php | 96 +++++++++++++++++++++++++++++++++++++++++------
1 files changed, 83 insertions(+), 13 deletions(-)
diff --git a/inc/managers/SEO/render/Traits/_Properties/openingHoursSpecificationTrait.php b/inc/managers/SEO/render/Traits/_Properties/openingHoursSpecificationTrait.php
index 94f601c..594ae51 100644
--- a/inc/managers/SEO/render/Traits/_Properties/openingHoursSpecificationTrait.php
+++ b/inc/managers/SEO/render/Traits/_Properties/openingHoursSpecificationTrait.php
@@ -1,35 +1,57 @@
<?php
namespace JVBase\managers\SEO\render\Traits\_Properties;
+use JVBase\base\SchemaHelper;
use JVBase\managers\SEO\render\Thing\Intangible\StructuredValue\OpeningHoursSpecification;
+use JVBase\managers\SEO\render\Traits\_Helpers\arrayHelper;
+use JVBase\meta\Meta;
+use JVBase\registrar\Fields;
if (!defined('ABSPATH')) {
exit;
}
trait openingHoursSpecificationTrait {
+ use arrayHelper;
/**
- * @var OpeningHoursSpecification The opening hours of a certain place.
+ * @var OpeningHoursSpecification|array The opening hours of a certain place.
*/
- protected OpeningHoursSpecification $openingHoursSpecification;
+ protected OpeningHoursSpecification|array $openingHoursSpecification;
- public function getOpeningHoursSpecification():?OpeningHoursSpecification
+ public function getOpeningHoursSpecification():OpeningHoursSpecification|array|null
{
return $this->openingHoursSpecification??null;
}
- public function setOpeningHoursSpecification(OpeningHoursSpecification $openingHoursSpecification):void
+ public function setOpeningHoursSpecification(OpeningHoursSpecification|array $openingHoursSpecification):void
{
+ if (is_array($openingHoursSpecification)) {
+ if (array_key_exists('dayOfWeek', $openingHoursSpecification)) {
+ if (!array_key_exists('type', $openingHoursSpecification)) {
+ $openingHoursSpecification['type'] = 'JVBase\managers\SEO\render\Thing\Intangible\StructuredValue\OpeningHoursSpecification';
+ }
+ $openingHoursSpecification = SchemaHelper::classFromConfig($openingHoursSpecification);
+ } else {
+ $out = [];
+ foreach ($openingHoursSpecification as $hours) {
+ if (!array_key_exists('type', $hours)){
+ $hours['type'] = 'JVBase\managers\SEO\render\Thing\Intangible\StructuredValue\OpeningHoursSpecification';
+ }
+ $out[] = SchemaHelper::classFromConfig($hours);
+ }
+ $openingHoursSpecification = $out;
+ }
+ }
$this->openingHoursSpecification = $openingHoursSpecification;
}
- public function getOpeningHoursSpecificationFieldConfig():array
+ public function setOpeningHoursSpecificationField(Fields $fields):void
{
- return [
+ $fields->addField('openingHours', [
'type' => 'repeater',
- 'label' => 'Opening Hours',
+ 'label' => __('Opening Hours', 'jvb'),
'fields' => [
'dayOfWeek' => [
- 'type' => 'radio',
- 'label' => 'Day(s) of Week',
+ 'type' => 'set',
+ 'label' => __('Day(s) of Week', 'jvb'),
'options' => [
'Mo' => 'Monday',
'Tu' => 'Tuesday',
@@ -38,17 +60,65 @@
'Fr' => 'Friday',
'Sa' => 'Saturday',
'Su' => 'Sunday'
- ]
+ ],
+ 'required' => true
],
'opens' => [
'type' => 'time',
- 'label' => 'Opens At',
+ 'label' => __('Opens at', 'jvb'),
+ 'required' => true
],
'closes' => [
'type' => 'time',
- 'label' => 'Closes At',
+ 'label' => __('Closes at', 'jvb'),
+ 'required' => true
]
]
- ];
+ ]);
+ $fields->addField('by_appointment', [
+ 'type' => 'true_false',
+ 'label' => __('By Appointment Only', 'jvb'),
+ ]);
+ $fields->addField('allow_walkins', [
+ 'type' => 'true_false',
+ 'label' => __('Walk Ins Welcome', 'jvb')
+ ]);
+ }
+ public function formatOpeningHoursSpecificationField(Meta $meta):void
+ {
+ $openingHours = $meta->get('openingHours');
+
+ if (!empty($openingHours)) {
+ $used = [
+ 'Mo' => false,
+ 'Tu' => false,
+ 'We' => false,
+ 'Th' => false,
+ 'Fr' => false,
+ 'Sa' => false,
+ 'Su' => false,
+ ];
+ $hours = [];
+ foreach ($openingHours as $row) {
+ $days = array_filter(explode(',', $row['dayOfWeek']),
+ function ($d) use ($used) {
+ if ($used[$d] === false) {
+ $used[$d] = true;
+ return true;
+ }
+ return false;
+ });
+ if (empty($days)) {
+ continue;
+ }
+ $opens = new OpeningHoursSpecification();
+ $opens->setDayOfWeek($days);
+ $opens->setOpens($row['opens']);
+ $opens->setCloses($row['closes']);
+ }
+ if (!empty($hours)){
+ $this->setOpeningHoursSpecification($hours);
+ }
+ }
}
}
--
Gitblit v1.10.0