Completed
Branch Gutenberg/event-attendees-bloc... (e27df5)
by
unknown
42:51 queued 28:10
created

EEH_Schema   A

Complexity

Total Complexity 36

Size/Duplication

Total Lines 263
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 263
rs 9.52
c 0
b 0
f 0
wmc 36
lcom 0
cbo 7

11 Methods

Rating   Name   Duplication   Size   Complexity  
B add_json_linked_data_for_event() 0 62 5
A location() 0 6 2
A name() 0 4 2
A streetAddress() 0 5 3
A postOfficeBoxNumber() 0 13 4
A addressLocality() 0 5 3
A addressRegion() 0 9 2
A addressCountry() 0 9 2
A postalCode() 0 6 3
A telephone() 0 5 3
B url() 0 16 7
1
<?php
2
3
/**
4
 * Class EEH_Schema
5
 * This class is a collection of static methods for applying schema.org formatting to passed items
6
 *
7
 * @package       Event Espresso
8
 * @subpackage    /core/helpers/
9
 * @author        Brent Christensen
10
 */
11
class EEH_Schema
12
{
13
14
15
    /**
16
     * generates JSON-based linked data for an event
17
     *
18
     * @param EE_Event $event
19
     * @throws EE_Error
20
     */
21
    public static function add_json_linked_data_for_event(EE_Event $event)
22
    {
23
        // Check we have a valid datetime for the event
24
        if (! $event->primary_datetime() instanceof EE_Datetime) {
25
            return;
26
        }
27
28
        $template_args = array(
29
            'event_permalink' => '',
30
            'event_name' => '',
31
            'event_description' => '',
32
            'event_start' => '',
33
            'event_end' => '',
34
            'currency' => '',
35
            'event_tickets' => array(),
36
            'venue_name' => '',
37
            'venue_url' => '',
38
            'venue_locality' => '',
39
            'venue_region' => '',
40
            'event_image' => '',
41
        );
42
        $template_args['event_permalink'] = $event->get_permalink();
43
        $template_args['event_name'] = $event->name();
44
        $template_args['event_description'] = wp_strip_all_tags($event->short_description(200));
45
        // clone datetime so that date formats don't override those for the original datetime
46
        $primary_datetime = clone $event->primary_datetime();
47
        $template_args['event_start'] = $primary_datetime->start_date(DateTime::ATOM);
48
        $template_args['event_end'] = $primary_datetime->end_date(DateTime::ATOM);
49
        unset($primary_datetime);
50
        $template_args['currency'] = EE_Registry::instance()->CFG->currency->code;
51
        foreach ($event->tickets() as $original_ticket) {
52
            // clone tickets so that date formats don't override those for the original ticket
53
            $ticket= clone $original_ticket;
54
            $ID = $ticket->ID();
55
            $template_args['event_tickets'][ $ID ]['start_date'] = $ticket->start_date(DateTime::ATOM, null);
56
            $template_args['event_tickets'][ $ID ]['end_date'] = $ticket->end_date(DateTime::ATOM, null);
57
            $template_args['event_tickets'][ $ID ]['price'] = number_format(
58
                $ticket->price(),
59
                EE_Registry::instance()->CFG->currency->dec_plc,
60
                EE_Registry::instance()->CFG->currency->dec_mrk,
61
                EE_Registry::instance()->CFG->currency->thsnds
62
            );
63
            unset($ticket);
64
        }
65
        $VNU_ID = espresso_venue_id();
66
        if (! empty($VNU_ID) && ! espresso_is_venue_private($VNU_ID)) {
67
            $venue = EEH_Venue_View::get_venue($VNU_ID);
68
            $template_args['venue_name'] = get_the_title($VNU_ID);
69
            $template_args['venue_url'] = get_permalink($VNU_ID);
70
            $template_args['venue_locality'] = $venue->city();
71
            $template_args['venue_region'] = $venue->state_name();
72
        }
73
        $template_args['event_image'] = $event->feature_image_url();
74
        $template_args = apply_filters(
75
            'FHEE__EEH_Schema__add_json_linked_data_for_event__template_args',
76
            $template_args,
77
            $event,
78
            $VNU_ID
79
        );
80
        extract($template_args, EXTR_OVERWRITE);
81
        include EE_TEMPLATES . 'json_linked_data_for_event.template.php';
82
    }
83
84
85
    /**
86
     *    location
87
     *    The location of the event, organization or action.
88
     *    Should include the Venue name AND schema formatted address info
89
     *
90
     * @access public
91
     * @param string $location
92
     * @return string
93
     */
94
    public static function location($location = null)
95
    {
96
        return ! empty($location) ? '<div itemprop="location" itemscope itemtype="http://schema.org/Place">'
97
                                      . $location
98
                                      . '</div>' : '';
99
    }
100
101
102
103
    /**
104
     *    name
105
     *    The name of the Event or Venue.
106
     *
107
     * @access public
108
     * @param string $name
109
     * @return string
110
     */
111
    public static function name($name = null)
112
    {
113
        return ! empty($name) ? '<span itemprop="name">' . $name . '</span>' : '';
114
    }
115
116
117
118
    /**
119
     *    streetAddress
120
     *    The street address. For example, 1600 Amphitheatre Pkwy.
121
     *
122
     * @access public
123
     * @param EEI_Address $obj_with_address
124
     * @return string
125
     */
126
    public static function streetAddress(EEI_Address $obj_with_address = null)
127
    {
128
        return $obj_with_address->address() !== null && $obj_with_address->address() !== ''
0 ignored issues
show
Bug introduced by
It seems like $obj_with_address is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
129
            ? '<span itemprop="streetAddress">' . $obj_with_address->address() . '</span>' : '';
130
    }
131
132
133
134
    /**
135
     *    postOfficeBoxNumber
136
     *    The post office box number for PO box addresses.
137
     *
138
     * @access public
139
     * @param EEI_Address $obj_with_address
140
     * @return string
141
     */
142
    public static function postOfficeBoxNumber(EEI_Address $obj_with_address = null)
143
    {
144
        // regex check for some form of PO Box or P.O. Box, etc, etc, etc
145
        if (preg_match(
146
            "/^\s*((P(OST)?.?\s*(O(FF(ICE)?)?)?.?\s+(B(IN|OX))?)|B(IN|OX))/i",
147
            $obj_with_address->address2()
148
        ) ) {
149
            return $obj_with_address->address2() !== null && $obj_with_address->address2() !== ''
0 ignored issues
show
Bug introduced by
It seems like $obj_with_address is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
150
                ? '<span itemprop="postOfficeBoxNumber">' . $obj_with_address->address2() . '</span>' : '';
151
        } else {
152
            return $obj_with_address->address2();
153
        }
154
    }
155
156
157
158
    /**
159
     *    addressLocality
160
     *    The locality (city, town, etc). For example, Mountain View.
161
     *
162
     * @access public
163
     * @param EEI_Address $obj_with_address
164
     * @return string
165
     */
166
    public static function addressLocality(EEI_Address $obj_with_address = null)
167
    {
168
        return $obj_with_address->city() !== null && $obj_with_address->city() !== ''
0 ignored issues
show
Bug introduced by
It seems like $obj_with_address is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
169
            ? '<span itemprop="addressLocality">' . $obj_with_address->city() . '</span>' : '';
170
    }
171
172
173
174
    /**
175
     *    addressRegion
176
     *    The region (state, province, etc). For example, CA.
177
     *
178
     * @access public
179
     * @param EEI_Address $obj_with_address
180
     * @return string
181
     */
182
    public static function addressRegion(EEI_Address $obj_with_address = null)
183
    {
184
        $state = $obj_with_address->state_name();
0 ignored issues
show
Bug introduced by
It seems like $obj_with_address is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
185
        if (! empty($state)) {
186
            return '<span itemprop="addressRegion">' . $state . '</span>';
187
        } else {
188
            return '';
189
        }
190
    }
191
192
193
194
    /**
195
     *    addressCountry
196
     *    The country. For example, USA. You can also provide the two-letter ISO 3166-1 alpha-2 country code.
197
     *
198
     * @access public
199
     * @param EEI_Address $obj_with_address
200
     * @return string
201
     */
202
    public static function addressCountry(EEI_Address $obj_with_address = null)
203
    {
204
        $country = $obj_with_address->country_name();
0 ignored issues
show
Bug introduced by
It seems like $obj_with_address is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
205
        if (! empty($country)) {
206
            return '<span itemprop="addressCountry">' . $country . '</span>';
207
        } else {
208
            return '';
209
        }
210
    }
211
212
213
214
    /**
215
     *    postalCode
216
     *    The postal code. For example, 94043.
217
     *
218
     * @access public
219
     * @param EEI_Address $obj_with_address
220
     * @return string
221
     */
222
    public static function postalCode(EEI_Address $obj_with_address = null)
223
    {
224
        return $obj_with_address->zip() !== null && $obj_with_address->zip() !== '' ? '<span itemprop="postalCode">'
0 ignored issues
show
Bug introduced by
It seems like $obj_with_address is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
225
                                                                                      . $obj_with_address->zip()
226
                                                                                      . '</span>' : '';
227
    }
228
229
230
231
    /**
232
     *    telephone
233
     *    The telephone number.
234
     *
235
     * @access public
236
     * @param string $phone_nmbr
237
     * @return string
238
     */
239
    public static function telephone($phone_nmbr = null)
240
    {
241
        return $phone_nmbr !== null && $phone_nmbr !== '' ? '<span itemprop="telephone">' . $phone_nmbr . '</span>'
242
            : '';
243
    }
244
245
246
247
    /**
248
     *    URL
249
     *    URL of the item as a clickable link
250
     *
251
     * @access public
252
     * @param string $url        - the URL that the link will resolve to
253
     * @param string $text       - the text that will be used for the visible link
254
     * @param array  $attributes - array of additional link attributes in  attribute_name => value pairs. ie: array( 'title' => 'click here', 'class' => 'link-class' )
255
     * @return string (link)
256
     */
257
    public static function url($url = null, $text = null, $attributes = array())
258
    {
259
        // Check the URL includes a scheme
260
        $parsed_url = parse_url($url);
261
        if (empty($parsed_url['scheme'])) {
262
            $url = 'http://' . ltrim($url, '/');
263
        }
264
265
        $atts = '';
266
        foreach ($attributes as $attribute => $value) {
267
            $atts .= ' ' . $attribute . '="' . $value . '"';
268
        }
269
        $text = $text !== null && $text !== '' ? $text : $url;
270
        return $url !== null && $url !== '' ? '<a itemprop="url" href="' . $url . '"' . $atts . '>' . $text . '</a>'
271
            : '';
272
    }
273
}
274