|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
if ( ! defined( 'EVENT_ESPRESSO_VERSION' ) ) { |
|
4
|
|
|
exit( 'No direct script access allowed' ); |
|
5
|
|
|
} |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class EEH_Schema |
|
11
|
|
|
* This class is a collection of static methods for applying schema.org formatting to passed items |
|
12
|
|
|
* |
|
13
|
|
|
* @package Event Espresso |
|
14
|
|
|
* @subpackage /core/helpers/ |
|
15
|
|
|
* @author Brent Christensen |
|
16
|
|
|
*/ |
|
17
|
|
|
class EEH_Schema { |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* generates JSON-based linked data for an event |
|
22
|
|
|
* |
|
23
|
|
|
* @param \EE_Event $event |
|
24
|
|
|
*/ |
|
25
|
|
|
public static function add_json_linked_data_for_event(\EE_Event $event) |
|
26
|
|
|
{ |
|
27
|
|
|
//Check we have a valid datetime for the event |
|
28
|
|
|
if(! $event->primary_datetime() instanceof EE_Datetime) { |
|
29
|
|
|
return; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
$template_args = array( |
|
33
|
|
|
'event_permalink' => '', |
|
34
|
|
|
'event_name' => '', |
|
35
|
|
|
'event_description' => '', |
|
36
|
|
|
'event_start' => '', |
|
37
|
|
|
'event_end' => '', |
|
38
|
|
|
'currency' => '', |
|
39
|
|
|
'event_tickets' => array(), |
|
40
|
|
|
'venue_name' => '', |
|
41
|
|
|
'venue_url' => '', |
|
42
|
|
|
'venue_locality' => '', |
|
43
|
|
|
'venue_region' => '', |
|
44
|
|
|
'event_image' => '', |
|
45
|
|
|
); |
|
46
|
|
|
$template_args['event_permalink'] = $event->get_permalink(); |
|
47
|
|
|
$template_args['event_name'] = $event->name(); |
|
48
|
|
|
$template_args['event_description'] = wp_strip_all_tags($event->short_description(200)); |
|
49
|
|
|
$template_args['event_start'] = $event->primary_datetime()->start_date(DateTime::ATOM); |
|
50
|
|
|
$template_args['event_end'] = $event->primary_datetime()->end_date(DateTime::ATOM); |
|
51
|
|
|
$template_args['currency'] = EE_Registry::instance()->CFG->currency->code; |
|
52
|
|
|
foreach ($event->tickets() as $ticket) { |
|
53
|
|
|
$ID = $ticket->ID(); |
|
54
|
|
|
$template_args['event_tickets'][$ID]['start_date'] = $ticket->start_date(DateTime::ATOM, null); |
|
55
|
|
|
$template_args['event_tickets'][$ID]['end_date'] = $ticket->end_date(DateTime::ATOM, null); |
|
56
|
|
|
$template_args['event_tickets'][$ID]['price'] = number_format( |
|
57
|
|
|
$ticket->price(), |
|
58
|
|
|
EE_Registry::instance()->CFG->currency->dec_plc, |
|
59
|
|
|
EE_Registry::instance()->CFG->currency->dec_mrk, |
|
60
|
|
|
EE_Registry::instance()->CFG->currency->thsnds |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
$VNU_ID = espresso_venue_id(); |
|
64
|
|
|
if ( ! empty($VNU_ID) && ! espresso_is_venue_private($VNU_ID)) { |
|
65
|
|
|
$venue = EEH_Venue_View::get_venue($VNU_ID); |
|
66
|
|
|
$template_args['venue_name'] = get_the_title($VNU_ID); |
|
67
|
|
|
$template_args['venue_url'] = get_permalink($VNU_ID); |
|
68
|
|
|
$template_args['venue_locality'] = $venue->city(); |
|
69
|
|
|
$template_args['venue_region'] = $venue->state_name(); |
|
70
|
|
|
} |
|
71
|
|
|
$template_args['event_image'] = $event->feature_image_url(); |
|
72
|
|
|
$template_args = apply_filters( |
|
73
|
|
|
'FHEE__EEH_Schema__add_json_linked_data_for_event__template_args', |
|
74
|
|
|
$template_args, |
|
75
|
|
|
$event, |
|
76
|
|
|
$VNU_ID |
|
77
|
|
|
); |
|
78
|
|
|
extract($template_args, EXTR_OVERWRITE); |
|
79
|
|
|
include EE_TEMPLATES . 'json_linked_data_for_event.template.php'; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* location |
|
85
|
|
|
* The location of the event, organization or action. |
|
86
|
|
|
* Should include the Venue name AND schema formatted address info |
|
87
|
|
|
* |
|
88
|
|
|
* @access public |
|
89
|
|
|
* @param string $location |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
|
|
public static function location( $location = null ) { |
|
93
|
|
|
return ! empty( $location ) ? '<div itemprop="location" itemscope itemtype="http://schema.org/Place">' |
|
94
|
|
|
. $location |
|
95
|
|
|
. '</div>' : ''; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* name |
|
102
|
|
|
* The name of the Event or Venue. |
|
103
|
|
|
* |
|
104
|
|
|
* @access public |
|
105
|
|
|
* @param string $name |
|
106
|
|
|
* @return string |
|
107
|
|
|
*/ |
|
108
|
|
|
public static function name( $name = null ) { |
|
109
|
|
|
return ! empty( $name ) ? '<span itemprop="name">' . $name . '</span>' : ''; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* streetAddress |
|
116
|
|
|
* The street address. For example, 1600 Amphitheatre Pkwy. |
|
117
|
|
|
* |
|
118
|
|
|
* @access public |
|
119
|
|
|
* @param EEI_Address $obj_with_address |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
|
|
public static function streetAddress( EEI_Address $obj_with_address = null ) { |
|
123
|
|
|
return $obj_with_address->address() !== null && $obj_with_address->address() !== '' |
|
|
|
|
|
|
124
|
|
|
? '<span itemprop="streetAddress">' . $obj_with_address->address() . '</span>' : ''; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* postOfficeBoxNumber |
|
131
|
|
|
* The post office box number for PO box addresses. |
|
132
|
|
|
* |
|
133
|
|
|
* @access public |
|
134
|
|
|
* @param EEI_Address $obj_with_address |
|
135
|
|
|
* @return string |
|
136
|
|
|
*/ |
|
137
|
|
|
public static function postOfficeBoxNumber( EEI_Address $obj_with_address = null ) { |
|
138
|
|
|
// regex check for some form of PO Box or P.O. Box, etc, etc, etc |
|
139
|
|
|
if ( preg_match( |
|
140
|
|
|
"/^\s*((P(OST)?.?\s*(O(FF(ICE)?)?)?.?\s+(B(IN|OX))?)|B(IN|OX))/i", |
|
141
|
|
|
$obj_with_address->address2() |
|
142
|
|
|
) ) { |
|
143
|
|
|
return $obj_with_address->address2() !== null && $obj_with_address->address2() !== '' |
|
|
|
|
|
|
144
|
|
|
? '<span itemprop="postOfficeBoxNumber">' . $obj_with_address->address2() . '</span>' : ''; |
|
145
|
|
|
} else { |
|
146
|
|
|
return $obj_with_address->address2(); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* addressLocality |
|
154
|
|
|
* The locality (city, town, etc). For example, Mountain View. |
|
155
|
|
|
* |
|
156
|
|
|
* @access public |
|
157
|
|
|
* @param EEI_Address $obj_with_address |
|
158
|
|
|
* @return string |
|
159
|
|
|
*/ |
|
160
|
|
|
public static function addressLocality( EEI_Address $obj_with_address = null ) { |
|
161
|
|
|
return $obj_with_address->city() !== null && $obj_with_address->city() !== '' |
|
|
|
|
|
|
162
|
|
|
? '<span itemprop="addressLocality">' . $obj_with_address->city() . '</span>' : ''; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* addressRegion |
|
169
|
|
|
* The region (state, province, etc). For example, CA. |
|
170
|
|
|
* |
|
171
|
|
|
* @access public |
|
172
|
|
|
* @param EEI_Address $obj_with_address |
|
173
|
|
|
* @return string |
|
174
|
|
|
*/ |
|
175
|
|
|
public static function addressRegion( EEI_Address $obj_with_address = null ) { |
|
176
|
|
|
$state = $obj_with_address->state_name(); |
|
|
|
|
|
|
177
|
|
|
if ( ! empty( $state ) ) { |
|
178
|
|
|
return '<span itemprop="addressRegion">' . $state . '</span>'; |
|
179
|
|
|
} else { |
|
180
|
|
|
return ''; |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
|
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* addressCountry |
|
188
|
|
|
* The country. For example, USA. You can also provide the two-letter ISO 3166-1 alpha-2 country code. |
|
189
|
|
|
* |
|
190
|
|
|
* @access public |
|
191
|
|
|
* @param EEI_Address $obj_with_address |
|
192
|
|
|
* @return string |
|
193
|
|
|
*/ |
|
194
|
|
|
public static function addressCountry( EEI_Address $obj_with_address = null ) { |
|
195
|
|
|
$country = $obj_with_address->country_name(); |
|
|
|
|
|
|
196
|
|
|
if ( ! empty( $country ) ) { |
|
197
|
|
|
return '<span itemprop="addressCountry">' . $country . '</span>'; |
|
198
|
|
|
} else { |
|
199
|
|
|
return ''; |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
|
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* postalCode |
|
207
|
|
|
* The postal code. For example, 94043. |
|
208
|
|
|
* |
|
209
|
|
|
* @access public |
|
210
|
|
|
* @param EEI_Address $obj_with_address |
|
211
|
|
|
* @return string |
|
212
|
|
|
*/ |
|
213
|
|
|
public static function postalCode( EEI_Address $obj_with_address = null ) { |
|
214
|
|
|
return $obj_with_address->zip() !== null && $obj_with_address->zip() !== '' ? '<span itemprop="postalCode">' |
|
|
|
|
|
|
215
|
|
|
. $obj_with_address->zip() |
|
216
|
|
|
. '</span>' : ''; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
|
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* telephone |
|
223
|
|
|
* The telephone number. |
|
224
|
|
|
* |
|
225
|
|
|
* @access public |
|
226
|
|
|
* @param string $phone_nmbr |
|
227
|
|
|
* @return string |
|
228
|
|
|
*/ |
|
229
|
|
|
public static function telephone( $phone_nmbr = null ) { |
|
230
|
|
|
return $phone_nmbr !== null && $phone_nmbr !== '' ? '<span itemprop="telephone">' . $phone_nmbr . '</span>' |
|
231
|
|
|
: ''; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
|
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* URL |
|
238
|
|
|
* URL of the item as a clickable link |
|
239
|
|
|
* |
|
240
|
|
|
* @access public |
|
241
|
|
|
* @param string $url - the URL that the link will resolve to |
|
242
|
|
|
* @param string $text - the text that will be used for the visible link |
|
243
|
|
|
* @param array $attributes - array of additional link attributes in attribute_name => value pairs. ie: array( 'title' => 'click here', 'class' => 'link-class' ) |
|
244
|
|
|
* @return string (link) |
|
245
|
|
|
*/ |
|
246
|
|
|
public static function url( $url = null, $text = null, $attributes = array() ) { |
|
247
|
|
|
//Check the URL includes a scheme |
|
248
|
|
|
$parsed_url = parse_url($url); |
|
249
|
|
|
if ( empty($parsed_url['scheme']) ) { |
|
250
|
|
|
$url = 'http://' . ltrim($url, '/'); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
$atts = ''; |
|
254
|
|
|
foreach ( $attributes as $attribute => $value ) { |
|
255
|
|
|
$atts .= ' ' . $attribute . '="' . $value . '"'; |
|
256
|
|
|
} |
|
257
|
|
|
$text = $text !== null && $text !== '' ? $text : $url; |
|
258
|
|
|
return $url !== null && $url !== '' ? '<a itemprop="url" href="' . $url . '"' . $atts . '>' . $text . '</a>' |
|
259
|
|
|
: ''; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
|
|
263
|
|
|
|
|
264
|
|
|
} |
|
265
|
|
|
// End of file EEH_Schema.helper.php |
|
266
|
|
|
// Location: /EEH_Schema.helper.php |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: