Jake Vanderwerf
10 hours ago f16cb88a3218ac7bb32e43f0e0a2542d35c7a01b
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
125
126
127
128
129
<?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
    {
        $days = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'];
        $dayFields = [];
        foreach ($days as $day) {
            $slug = strtolower($day);
 
            $dayFields[$slug] = [
                'type'  => 'group',
                'label' => $day,
                'fields'    => [
                    'isOpen'    => [
                        'type'  => 'true_false',
                        'label' => 'Open',
                    ],
                    'opens' => [
                        'type'  => 'time',
                        'label' => 'Opens at',
                        'required'  => true
                    ],
                    'closes'    => [
                        'type'  => 'time',
                        'label' => 'Closes at',
                        'required'  => true
                    ]
                ],
                'section'   => 'hours',
            ];
        }
        $fields->addField('openingHours', [
            'type'  => 'group',
            'label' => 'Opening Hours',
            'fields'    => $dayFields,
            'section'   => 'hours',
        ]);
        $fields->addField('by_appointment', [
            'type'  => 'true_false',
            'label' => 'By Appointment Only',
            'section'   => 'hours',
        ]);
        $fields->addField('allow_walkins', [
            'type'  => 'true_false',
            'label' => 'Walk Ins Welcome',
            'section'   => 'hours',
        ]);
    }
    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);
            }
        }
    }
}