Completed
Branch BUG-10738-inconsistency-in-ses... (cda363)
by
unknown
13:38 queued 12s
created

EspressoEventAttendees::validateEntities()   D

Complexity

Conditions 9
Paths 2

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 17
nc 2
nop 1
dl 0
loc 25
rs 4.909
c 0
b 0
f 0
1
<?php
2
namespace EventEspresso\core\domain\entities\shortcodes;
3
4
use EE_Datetime;
5
use EE_Error;
6
use EE_Event;
7
use EE_Ticket;
8
use EEH_Event_View;
9
use EEH_Template;
10
use EEM_Attendee;
11
use EEM_Datetime;
12
use EEM_Event;
13
use EEM_Registration;
14
use EEM_Ticket;
15
use EventEspresso\core\exceptions\EntityNotFoundException;
16
use EventEspresso\core\services\shortcodes\EspressoShortcode;
17
18
defined('EVENT_ESPRESSO_VERSION') || exit;
19
20
21
22
/**
23
 * Class EspressoEventAttendees
24
 * ESPRESSO_EVENT_ATTENDEES shortcode
25
 *
26
 * @package Event Espresso
27
 * @author  Darren Ethier
28
 * @since   4.9.26
29
 */
30
class EspressoEventAttendees extends EspressoShortcode
31
{
32
33
    private $query_params = array(
34
        0 => array()
35
    );
36
37
    private $template_args = array(
38
        'contacts'      => array(),
39
        'event'         => null,
40
        'datetime'      => null,
41
        'ticket'        => null,
42
    );
43
44
    /**
45
     * the actual shortcode tag that gets registered with WordPress
46
     *
47
     * @return string
48
     */
49
    public function getTag()
50
    {
51
        return 'ESPRESSO_EVENT_ATTENDEES';
52
    }
53
54
55
56
    /**
57
     * the time in seconds to cache the results of the processShortcode() method
58
     * 0 means the processShortcode() results will NOT be cached at all
59
     *
60
     * @return int
61
     */
62
    public function cacheExpiration()
63
    {
64
        return 0;
65
    }
66
67
68
69
    /**
70
     * a place for adding any initialization code that needs to run prior to wp_header().
71
     * this may be required for shortcodes that utilize a corresponding module,
72
     * and need to enqueue assets for that module
73
     *
74
     * @return void
75
     */
76
    public function initializeShortcode()
77
    {
78
        $this->shortcodeHasBeenInitialized();
79
    }
80
81
82
    /**
83
     * process_shortcode - ESPRESSO_EVENT_ATTENDEES - Returns a list of attendees to an event.
84
     *  [ESPRESSO_EVENT_ATTENDEES]
85
     *  - defaults to attendees for earliest active event, or earliest upcoming event.
86
     *  [ESPRESSO_EVENT_ATTENDEES event_id=123]
87
     *  - attendees for specific event.
88
     *  [ESPRESSO_EVENT_ATTENDEES datetime_id=245]
89
     *  - attendees for a specific datetime.
90
     *  [ESPRESSO_EVENT_ATTENDEES ticket_id=123]
91
     *  - attendees for a specific ticket.
92
     *  [ESPRESSO_EVENT_ATTENDEES status=all]
93
     *  - specific registration status (use status id) or all for all attendees regardless of status.
94
     *  Note default is to only return approved attendees
95
     *  [ESPRESSO_EVENT_ATTENDEES show_gravatar=true]
96
     *  - default is to not return gravatar.  Otherwise if this is set then return gravatar for email address given.
97
     *  [ESPRESSO_EVENT_ATTENDEES display_on_archives=true]
98
     *  - default is to not display attendees list on archive pages.
99
     * Note: because of the relationship between event_id, ticket_id, and datetime_id:
100
     * If more than one of those params is included, then preference is given to the following:
101
     *  - event_id is used whenever its present and any others are ignored.
102
     *  - if no event_id then datetime is used whenever its present and any others are ignored.
103
     *  - otherwise ticket_id is used if present.
104
     *
105
     * @param array $attributes
106
     * @return string
107
     * @throws EE_Error
108
     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
109
     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
110
     * @throws \InvalidArgumentException
111
     */
112
    public function processShortcode($attributes = array())
113
    {
114
        // grab attributes and merge with defaults
115
        $attributes = $this->getAttributes((array)$attributes);
116
        $archive = is_archive();
117
        $display_on_archives = filter_var($attributes['display_on_archives'], FILTER_VALIDATE_BOOLEAN);
118
        // don't display on archives unless 'display_on_archives' is true
119
        if ($archive && ! $display_on_archives) {
120
            return '';
121
        }
122
123
        try {
124
            $this->setBaseTemplateArguments($attributes);
125
            $this->validateEntities($attributes);
126
            $this->setBaseQueryParams();
127
        } catch (EntityNotFoundException $e) {
128
            if (WP_DEBUG) {
129
                return '<div class="important-notice ee-error">'
130
                       . $e->getMessage()
131
                       . '</div>';
132
            }
133
            return '';
134
        }
135
        $this->setAdditionalQueryParams($attributes);
136
        //get contacts!
137
        $this->template_args['contacts'] = EEM_Attendee::instance()->get_all($this->query_params);
138
        //all set let's load up the template and return.
139
        return EEH_Template::locate_template(
140
            'loop-espresso_event_attendees.php',
141
            $this->template_args
142
        );
143
    }
144
145
146
147
    /**
148
     * merge incoming attributes with filtered defaults
149
     *
150
     * @param array $attributes
151
     * @return array
152
     */
153
    private function getAttributes(array $attributes)
154
    {
155
        return (array) apply_filters(
156
            'EES_Espresso_Event_Attendees__process_shortcode__default_shortcode_atts',
157
            $attributes + array(
158
                'event_id'            => null,
159
                'datetime_id'         => null,
160
                'ticket_id'           => null,
161
                'status'              => EEM_Registration::status_id_approved,
162
                'show_gravatar'       => false,
163
                'display_on_archives' => false,
164
            )
165
        );
166
    }
167
168
169
    /**
170
     * Set all the base template arguments from the incoming attributes.
171
     *
172
     * * Note: because of the relationship between event_id, ticket_id, and datetime_id:
173
     * If more than one of those params is included, then preference is given to the following:
174
     *  - event_id is used whenever its present and any others are ignored.
175
     *  - if no event_id then datetime is used whenever its present and any others are ignored.
176
     *  - otherwise ticket_id is used if present.
177
     *
178
     * @param array $attributes
179
     * @throws EE_Error
180
     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
181
     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
182
     * @throws \InvalidArgumentException
183
     */
184
    private function setBaseTemplateArguments(array $attributes)
185
    {
186
        $this->template_args['show_gravatar'] = $attributes['show_gravatar'];
187
        $this->template_args['event'] = $this->getEvent($attributes);
188
        $this->template_args['datetime'] = empty($attributes['event_id'])
189
            ? $this->getDatetime($attributes)
190
            : null;
191
        $this->template_args['ticket'] = empty($attributes['datetime_id']) && empty($attributes['event_id'])
192
            ? $this->getTicket($attributes)
193
            : null;
194
    }
195
196
197
    /**
198
     * Validates the presence of entities for the given attribute values.
199
     * @param array $attributes
200
     * @throws EntityNotFoundException
201
     */
202
    private function validateEntities(array $attributes)
203
    {
204
        if (! $this->template_args['event'] instanceof EE_Event
205
            || (
206
                empty($attributes['event_id'])
207
                && $attributes['datetime_id']
208
                && ! $this->template_args['datetime'] instanceof EE_Datetime
209
            )
210
            || (
211
                empty($attributes['event_id'])
212
                && empty($attributes['datetime_id'])
213
                && $attributes['ticket_id']
214
                && ! $this->template_args['ticket'] instanceof EE_Ticket
215
            )
216
        ) {
217
            throw new EntityNotFoundException(
218
                '',
219
                '',
220
                esc_html__(
221
                    'The [ESPRESSO_EVENT_ATTENDEES] shortcode has been used incorrectly.  Please double check the arguments you used for any typos.  In the case of ID type arguments, its possible the given ID does not correspond to existing data in the database.',
222
                    'event_espresso'
223
                )
224
            );
225
        }
226
    }
227
228
229
    /**
230
     * Sets the query params for the base query elements.
231
     */
232
    private function setBaseQueryParams()
233
    {
234
        switch (true) {
235
            case $this->template_args['datetime'] instanceof EE_Datetime:
236
                $this->query_params = array(
237
                    0 => array(
238
                        'Registration.Ticket.Datetime.DTT_ID' => $this->template_args['datetime']->ID()
239
                    ),
240
                    'default_where_conditions' => 'this_model_only'
241
                );
242
                break;
243
            case $this->template_args['ticket'] instanceof EE_Ticket:
244
                $this->query_params[0] = array(
245
                    'Registration.TKT_ID' => $this->template_args['ticket']->ID()
246
                );
247
                break;
248
            case $this->template_args['event'] instanceof EE_Event:
249
                $this->query_params[0] = array(
250
                    'Registration.EVT_ID' => $this->template_args['event']->ID()
251
                );
252
                break;
253
        }
254
    }
255
256
257
    /**
258
     * @param array $attributes
259
     * @return EE_Event|null
260
     * @throws EE_Error
261
     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
262
     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
263
     * @throws \InvalidArgumentException
264
     */
265
    private function getEvent(array $attributes)
266
    {
267
        switch (true) {
268
            case ! empty($attributes['event_id']):
269
                $event = EEM_Event::instance()->get_one_by_ID($attributes['event_id']);
270
                break;
271
            case ! empty($attributes['datetime_id']):
272
                $event = EEM_Event::instance()->get_one(array(
273
                    array(
274
                        'Datetime.DTT_ID' => $attributes['datetime_id']
275
                    )
276
                ));
277
                break;
278
            case ! empty($attributes['ticket_id']):
279
                $event = EEM_Event::instance()->get_one(array(
280
                    array(
281
                        'Datetime.Ticket.TKT_ID' => $attributes['ticket_id']
282
                    )
283
                ));
284
                break;
285
            case is_espresso_event():
286
                $event = EEH_Event_View::get_event();
287
                break;
288
            default:
289
                // one last shot...
290
                // try getting the earliest active event
291
                $events = EEM_Event::instance()->get_active_events(array(
292
                    'limit'    => 1,
293
                    'order_by' => array('Datetime.DTT_EVT_start' => 'ASC')
294
                ));
295
                //  if none then get the next upcoming
296
                $events = empty($events)
297
                    ? EEM_Event::instance()->get_upcoming_events(array(
298
                        'limit'    => 1,
299
                        'order_by' => array('Datetime.DTT_EVT_start' => 'ASC')
300
                    ))
301
                    : $events;
302
                $event = reset($events);
303
        }
304
305
        return $event instanceof EE_Event ? $event : null;
306
    }
307
308
309
    /**
310
     * @param array         $attributes
311
     * @return EE_Datetime|null
312
     * @throws EE_Error
313
     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
314
     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
315
     * @throws \InvalidArgumentException
316
     */
317 View Code Duplication
    private function getDatetime(array $attributes)
318
    {
319
        if (! empty($attributes['datetime_id'])) {
320
            $datetime = EEM_Datetime::instance()->get_one_by_ID($attributes['datetime_id']);
321
            if ($datetime instanceof EE_Datetime) {
322
                return $datetime;
323
            }
324
        }
325
        return null;
326
    }
327
328
329
    /**
330
     * @param array $attributes
331
     * @return \EE_Base_Class|EE_Ticket|null
332
     * @throws EE_Error
333
     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
334
     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
335
     * @throws \InvalidArgumentException
336
     */
337 View Code Duplication
    private function getTicket(array $attributes)
338
    {
339
        if (! empty($attributes['ticket_id'])) {
340
            $ticket = EEM_Ticket::instance()->get_one_by_ID($attributes['ticket_id']);
341
            if ($ticket instanceof EE_Ticket) {
342
                return $ticket;
343
            }
344
        }
345
        return null;
346
    }
347
348
349
350
    /**
351
     * @param array $attributes
352
     * @throws EE_Error
353
     */
354
    private function setAdditionalQueryParams(array $attributes)
355
    {
356
        $reg_status_array = EEM_Registration::reg_status_array();
357
        if ($attributes['status'] !== 'all' && isset($reg_status_array[$attributes['status']])) {
358
            $this->query_params[0]['Registration.STS_ID'] = $attributes['status'];
359
        }
360
        $this->query_params['group_by'] = array('ATT_ID');
361
        $this->query_params['order_by'] = (array) apply_filters(
362
            'FHEE__EES_Espresso_Event_Attendees__process_shortcode__order_by',
363
            array('ATT_lname' => 'ASC', 'ATT_fname' => 'ASC')
364
        );
365
    }
366
367
368
369
}
370
// End of file EspressoEventAttendees.php
371
// Location: EventEspresso\core\domain\entities\shortcodes/EspressoEventAttendees.php
372