PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 1.5.21
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v1.5.21
2.4.0 2.3.0 2.2.5 2.2.0 2.1.2 2.1.1 trunk 1.10.0 1.10.01 1.10.02 1.5.0 1.5.01 1.5.02 1.5.1 1.5.10 1.5.20 1.5.21 1.5.22 1.5.23 1.5.24 1.5.25 1.6.0 1.7.0 1.7.1 1.7.2 All 33 releases
fluent-booking / vendor / wpfluent / caldav / src / Clients / CommonTrait.php

CommonTrait.php in Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution 1.5.21, at vendor/wpfluent/caldav/src/Clients/CommonTrait.php

141 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentBooking\Package\CalDav\Clients;
4
5 use DateTime;
6 use DateTimeZone;
7 use DOMXPath;
8 use DOMDocument;
9 use FluentBooking\Package\CalDav\ICal\ICal;
10
11 trait CommonTrait
12 {
13 protected $options = [
14 'format_date_times' => false,
15 'date_time_format' => null,
16 'timezone' => null,
17 ];
18
19
20 protected $datetimeFields = [
21 'dtstart',
22 'dtend',
23 'dtstamp',
24 'created',
25 'last_modified'
26 ];
27
28 public function formatDateTimes($events)
29 {
30 $tz = $this->getTimezone();
31
32 $format = $this->getDateTimeFormat();
33
34 foreach ($events as $event) {
35
36 foreach ($this->datetimeFields as $prop) {
37
38 $dt = new DateTime($event->{$prop}, $tz);
39
40 $event->{$prop} = $dt->format($format);
41 }
42 }
43
44 return $events;
45 }
46
47 protected function getTimezone()
48 {
49 if ($tz = $this->getOption('timezone')) {
50 return new DateTimeZone($tz);
51 }
52
53 return wp_timezone();
54 }
55
56 protected function getDateTimeFormat()
57 {
58 if ($format = $this->getOption('date_time_format')) {
59 return $format;
60 }
61
62 return get_option('date_format');
63 }
64
65 public function setOptions($options = [])
66 {
67 foreach ($options as $key => $value) {
68 $this->options[$key] = $value;
69 }
70 }
71
72 public function getOptions($options = [])
73 {
74 return $this->options;
75 }
76
77 public function setOption($key, $value)
78 {
79 $this->options[$key] = $value;
80 }
81
82 public function getOption($key, $default = null)
83 {
84 if (isset($this->options[$key])) {
85 return $this->options[$key];
86 }
87
88 return $default;
89 }
90
91 protected function extractReportinformation($content)
92 {
93 $doc = new DOMDocument();
94
95 $doc->loadXML($content);
96
97 $xpath = new DOMXPath($doc);
98
99 $xpath->registerNamespace('d', 'DAV:');
100
101 $xpath->registerNamespace('cal', 'urn:ietf:params:xml:ns:caldav');
102
103 $xpath->registerNamespace('caldav', 'urn:ietf:params:xml:ns:caldav');
104
105 if ($href = $xpath->query('//d:href')->item(0)) {
106 $href = $href->nodeValue;
107 }
108
109 if ($status = $xpath->query('//d:status')->item(0)) {
110 $status = $status->nodeValue;
111 }
112
113 if ($etag = $xpath->query('//d:getetag')->item(0)) {
114 $etag = $etag->nodeValue;
115 }
116
117 $events = [];
118
119 foreach ($xpath->query('//cal:calendar-data') as $cal) {
120 $events = array_merge(
121 $events, $this->extractSingleEvent($cal->nodeValue)
122 );
123 }
124
125 if ($this->getOption('format_date_times')) {
126 $events = $this->formatDateTimes($events);
127 }
128
129 return compact('href', 'etag', 'status', 'events');
130 }
131
132 protected function extractSingleEvent($content)
133 {
134 $ical = new ICal($content, [
135 'defaultTimeZone' => 'UTC'
136 ]);
137
138 return $ical->events();
139 }
140 }
141