Jake Vanderwerf
2026-05-03 9bbeea742424837fb58207d88e10dbca0b2cae04
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?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);
            }
        }
    }
}