Completed
Branch BUG/fatal-with-paypal-debug-li... (3a6198)
by
unknown
09:03 queued 40s
created

EEH_Schema::addressLocality()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
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
            'event_attendance_mode' => '',
35
            'event_status' => '',
36
            'currency' => '',
37
            'event_tickets' => array(),
38
            'venue_name' => '',
39
            'venue_url' => '',
40
            'venue_locality' => '',
41
            'venue_region' => '',
42
            'venue_address' => '',
43
            'event_image' => '',
44
        );
45
        $template_args['event_permalink'] = $event->get_permalink();
46
        $template_args['event_name'] = $event->name();
47
        $template_args['event_description'] = wp_strip_all_tags($event->short_description(200));
48
        // clone datetime so that date formats don't override those for the original datetime
49
        $primary_datetime = clone $event->primary_datetime();
50
        $template_args['event_start'] = $primary_datetime->start_date(DateTime::ATOM);
51
        $template_args['event_end'] = $primary_datetime->end_date(DateTime::ATOM);
52
        unset($primary_datetime);
53
        switch ($event->status()) {
54
            case EEM_Event::cancelled:
55
                $event_status = 'EventCancelled';
56
                break;
57
            case EEM_Event::postponed:
58
                $event_status = 'EventPostponed';
59
                break;
60
            default:
61
                $event_status = 'EventScheduled';
62
        }
63
        $template_args['event_attendance_mode'] = 'OfflineEventAttendanceMode';
64
        $template_args['event_status'] = '"https://schema.org/' . $event_status . '"';
65
        $template_args['currency'] = EE_Registry::instance()->CFG->currency->code;
66
        foreach ($event->tickets() as $original_ticket) {
67
            // clone tickets so that date formats don't override those for the original ticket
68
            $ticket= clone $original_ticket;
69
            $ID = $ticket->ID();
70
            $template_args['event_tickets'][ $ID ]['start_date'] = $ticket->start_date(DateTime::ATOM, null);
71
            $template_args['event_tickets'][ $ID ]['end_date'] = $ticket->end_date(DateTime::ATOM, null);
72
            $template_args['event_tickets'][ $ID ]['price'] = number_format(
73
                $ticket->price(),
74
                EE_Registry::instance()->CFG->currency->dec_plc,
75
                EE_Registry::instance()->CFG->currency->dec_mrk,
76
                ''
77
            );
78
            switch ($ticket->ticket_status()) {
79
                case 'TKO':
80
                    $availability = 'InStock';
81
                    break;
82
                case 'TKS':
83
                    $availability = 'SoldOut';
84
                    break;
85
                default:
86
                    $availability = null;
87
                    break;
88
            }
89
            $template_args['event_tickets'][ $ID ]['availability'] = $availability;
90
            unset($ticket);
91
        }
92
        $VNU_ID = espresso_venue_id();
93
        if (! empty($VNU_ID) && ! espresso_is_venue_private($VNU_ID)) {
94
            $venue = EEH_Venue_View::get_venue($VNU_ID);
95
            $template_args['venue_name'] = get_the_title($VNU_ID);
96
            $template_args['venue_url'] = get_permalink($VNU_ID);
97
            $template_args['venue_locality'] = $venue->city();
98
            $template_args['venue_region'] = $venue->state_name();
99
            $template_args['venue_address'] = $venue->address();
100
            if ($venue->virtual_url() !== '') {
101
                $template_args['event_attendance_mode'] = 'OnlineEventAttendanceMode';
102
            }
103
            if ($venue->virtual_url() !== '' && $venue->address() !== '') {
104
                $template_args['event_attendance_mode'] = 'MixedEventAttendanceMode';
105
            }
106
        }
107
        $template_args['event_image'] = $event->feature_image_url();
108
        $template_args = apply_filters(
109
            'FHEE__EEH_Schema__add_json_linked_data_for_event__template_args',
110
            $template_args,
111
            $event,
112
            $VNU_ID
113
        );
114
        extract($template_args, EXTR_OVERWRITE);
115
        include EE_TEMPLATES . 'json_linked_data_for_event.template.php';
116
    }
117
118
119
    /**
120
     *    location
121
     *    The location of the event, organization or action.
122
     *    Should include the Venue name AND schema formatted address info
123
     *
124
     * @access public
125
     * @param string $location
126
     * @return string
127
     */
128
    public static function location($location = null)
129
    {
130
        return ! empty($location) ? '<div itemprop="location" itemscope itemtype="http://schema.org/Place">'
131
                                      . $location
132
                                      . '</div>' : '';
133
    }
134
135
136
137
    /**
138
     *    name
139
     *    The name of the Event or Venue.
140
     *
141
     * @access public
142
     * @param string $name
143
     * @return string
144
     */
145
    public static function name($name = null)
146
    {
147
        return ! empty($name) ? '<span itemprop="name">' . $name . '</span>' : '';
148
    }
149
150
151
152
    /**
153
     *    streetAddress
154
     *    The street address. For example, 1600 Amphitheatre Pkwy.
155
     *
156
     * @access public
157
     * @param EEI_Address $obj_with_address
158
     * @return string
159
     */
160
    public static function streetAddress(EEI_Address $obj_with_address = null)
161
    {
162
        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...
163
            ? '<span itemprop="streetAddress">' . $obj_with_address->address() . '</span>' : '';
164
    }
165
166
167
168
    /**
169
     *    postOfficeBoxNumber
170
     *    The post office box number for PO box addresses.
171
     *
172
     * @access public
173
     * @param EEI_Address $obj_with_address
174
     * @return string
175
     */
176
    public static function postOfficeBoxNumber(EEI_Address $obj_with_address = null)
177
    {
178
        // regex check for some form of PO Box or P.O. Box, etc, etc, etc
179
        if (preg_match(
180
            "/^\s*((P(OST)?.?\s*(O(FF(ICE)?)?)?.?\s+(B(IN|OX))?)|B(IN|OX))/i",
181
            $obj_with_address->address2()
182
        ) ) {
183
            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...
184
                ? '<span itemprop="postOfficeBoxNumber">' . $obj_with_address->address2() . '</span>' : '';
185
        } else {
186
            return $obj_with_address->address2();
187
        }
188
    }
189
190
191
192
    /**
193
     *    addressLocality
194
     *    The locality (city, town, etc). For example, Mountain View.
195
     *
196
     * @access public
197
     * @param EEI_Address $obj_with_address
198
     * @return string
199
     */
200
    public static function addressLocality(EEI_Address $obj_with_address = null)
201
    {
202
        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...
203
            ? '<span itemprop="addressLocality">' . $obj_with_address->city() . '</span>' : '';
204
    }
205
206
207
208
    /**
209
     *    addressRegion
210
     *    The region (state, province, etc). For example, CA.
211
     *
212
     * @access public
213
     * @param EEI_Address $obj_with_address
214
     * @return string
215
     */
216
    public static function addressRegion(EEI_Address $obj_with_address = null)
217
    {
218
        $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...
219
        if (! empty($state)) {
220
            return '<span itemprop="addressRegion">' . $state . '</span>';
221
        } else {
222
            return '';
223
        }
224
    }
225
226
227
228
    /**
229
     *    addressCountry
230
     *    The country. For example, USA. You can also provide the two-letter ISO 3166-1 alpha-2 country code.
231
     *
232
     * @access public
233
     * @param EEI_Address $obj_with_address
234
     * @return string
235
     */
236
    public static function addressCountry(EEI_Address $obj_with_address = null)
237
    {
238
        $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...
239
        if (! empty($country)) {
240
            return '<span itemprop="addressCountry">' . $country . '</span>';
241
        } else {
242
            return '';
243
        }
244
    }
245
246
247
248
    /**
249
     *    postalCode
250
     *    The postal code. For example, 94043.
251
     *
252
     * @access public
253
     * @param EEI_Address $obj_with_address
254
     * @return string
255
     */
256
    public static function postalCode(EEI_Address $obj_with_address = null)
257
    {
258
        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...
259
                                                                                      . $obj_with_address->zip()
260
                                                                                      . '</span>' : '';
261
    }
262
263
264
265
    /**
266
     *    telephone
267
     *    The telephone number.
268
     *
269
     * @access public
270
     * @param string $phone_nmbr
271
     * @return string
272
     */
273
    public static function telephone($phone_nmbr = null)
274
    {
275
        return $phone_nmbr !== null && $phone_nmbr !== '' ? '<span itemprop="telephone">' . $phone_nmbr . '</span>'
276
            : '';
277
    }
278
279
280
281
    /**
282
     *    URL
283
     *    URL of the item as a clickable link
284
     *
285
     * @access public
286
     * @param string $url        - the URL that the link will resolve to
287
     * @param string $text       - the text that will be used for the visible link
288
     * @param array  $attributes - array of additional link attributes in  attribute_name => value pairs. ie: array( 'title' => 'click here', 'class' => 'link-class' )
289
     * @return string (link)
290
     */
291
    public static function url($url = null, $text = null, $attributes = array())
292
    {
293
        // Check the URL includes a scheme
294
        $parsed_url = parse_url($url);
295
        if (empty($parsed_url['scheme'])) {
296
            $url = 'http://' . ltrim($url, '/');
297
        }
298
299
        $atts = '';
300
        foreach ($attributes as $attribute => $value) {
301
            $atts .= ' ' . $attribute . '="' . $value . '"';
302
        }
303
        $text = $text !== null && $text !== '' ? $text : $url;
304
        return $url !== null && $url !== '' ? '<a itemprop="url" href="' . $url . '"' . $atts . '>' . $text . '</a>'
305
            : '';
306
    }
307
}
308