<?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|array The opening hours of a certain place.
|
*/
|
protected OpeningHoursSpecification|array $openingHoursSpecification;
|
|
public function getOpeningHoursSpecification():OpeningHoursSpecification|array|null
|
{
|
return $this->openingHoursSpecification??null;
|
}
|
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 setOpeningHoursSpecificationField(Fields $fields):void
|
{
|
$fields->addField('openingHours', [
|
'type' => 'repeater',
|
'label' => __('Opening Hours', 'jvb'),
|
'fields' => [
|
'dayOfWeek' => [
|
'type' => 'set',
|
'label' => __('Day(s) of Week', 'jvb'),
|
'options' => [
|
'Mo' => 'Monday',
|
'Tu' => 'Tuesday',
|
'We' => 'Wednesday',
|
'Th' => 'Thursday',
|
'Fr' => 'Friday',
|
'Sa' => 'Saturday',
|
'Su' => 'Sunday'
|
],
|
'required' => true
|
],
|
'opens' => [
|
'type' => 'time',
|
'label' => __('Opens at', 'jvb'),
|
'required' => true
|
],
|
'closes' => [
|
'type' => 'time',
|
'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);
|
}
|
}
|
}
|
}
|