Completed
Branch FET-10896-improvements-to-floa... (000e49)
by
unknown
163:03 queued 150:10
created

EspressoEventAttendees::processShortcode()   D

Complexity

Conditions 10
Paths 4

Size

Total Lines 45
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 26
nc 4
nop 1
dl 0
loc 45
rs 4.8196
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\services\shortcodes\EspressoShortcode;
16
17
defined('EVENT_ESPRESSO_VERSION') || exit;
18
19
20
21
/**
22
 * Class EspressoEventAttendees
23
 * ESPRESSO_EVENT_ATTENDEES shortcode
24
 *
25
 * @package Event Espresso
26
 * @author  Darren Ethier
27
 * @since   4.9.26
28
 */
29
class EspressoEventAttendees extends EspressoShortcode
30
{
31
32
    private $query_params = array(
33
        0 => array()
34
    );
35
36
    private $template_args = array(
37
        'contacts'      => array(),
38
        'event'         => null,
39
        'datetime'      => null,
40
        'ticket'        => null,
41
    );
42
43
    /**
44
     * the actual shortcode tag that gets registered with WordPress
45
     *
46
     * @return string
47
     */
48
    public function getTag()
49
    {
50
        return 'ESPRESSO_EVENT_ATTENDEES';
51
    }
52
53
54
55
    /**
56
     * the time in seconds to cache the results of the processShortcode() method
57
     * 0 means the processShortcode() results will NOT be cached at all
58
     *
59
     * @return int
60
     */
61
    public function cacheExpiration()
62
    {
63
        return 0;
64
    }
65
66
67
68
    /**
69
     * a place for adding any initialization code that needs to run prior to wp_header().
70
     * this may be required for shortcodes that utilize a corresponding module,
71
     * and need to enqueue assets for that module
72
     *
73
     * @return void
74
     */
75
    public function initializeShortcode()
76
    {
77
        $this->shortcodeHasBeenInitialized();
78
    }
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
     *
87
     *  [ESPRESSO_EVENT_ATTENDEES event_id=123]
88
     *  - attendees for specific event.
89
     *
90
     *  [ESPRESSO_EVENT_ATTENDEES datetime_id=245]
91
     *  - attendees for a specific datetime.
92
     *
93
     *  [ESPRESSO_EVENT_ATTENDEES ticket_id=123]
94
     *  - attendees for a specific ticket.
95
     *
96
     *  [ESPRESSO_EVENT_ATTENDEES status=all]
97
     *  - specific registration status (use status id) or all for all attendees regardless of status.
98
     *  Note default is to only return approved attendees
99
     *
100
     *  [ESPRESSO_EVENT_ATTENDEES show_gravatar=true]
101
     *  - default is to not return gravatar.  Otherwise if this is set then return gravatar for email address given.
102
     *
103
     *  [ESPRESSO_EVENT_ATTENDEES display_on_archives=true]
104
     *  - default is to not display attendees list on archive pages.
105
     *
106
     * Note: because of the relationship between event_id, ticket_id, and datetime_id:
107
     * If more than one of those params is included, then preference is given to the following:
108
     *  - event_id is used whenever its present and any others are ignored.
109
     *  - if no event_id then datetime is used whenever its present and any others are ignored.
110
     *  - otherwise ticket_id is used if present.
111
     *
112
     * @param array $attributes
113
     * @return string
114
     * @throws EE_Error
115
     */
116
    public function processShortcode($attributes = array())
117
    {
118
        // grab attributes and merge with defaults
119
        $attributes = $this->getAttributes((array)$attributes);
120
        $archive = is_archive();
121
        $display_on_archives = filter_var($attributes['display_on_archives'], FILTER_VALIDATE_BOOLEAN);
122
        // don't display on archives unless 'display_on_archives' is true
123
        if($archive && ! $display_on_archives) {
124
            return '';
125
        }
126
        // add attributes to template args
127
        $this->template_args['show_gravatar'] = $attributes['show_gravatar'];
128
        // add required objects: event, datetime, and ticket
129
        $this->template_args['event'] = $this->getEventAndQueryParams($attributes);
130
        $this->template_args['datetime'] = $this->getDatetimeAndQueryParams($attributes);
131
        $this->template_args['ticket'] = $this->getTicketAndQueryParams($attributes);
132
133
        // if any of the above objects is invalid or missing,
134
        // then there was an invalid parameter or the shortcode was used incorrectly
135
        // so when WP_DEBUG is set and true, we'll show a message,
136
        // otherwise we'll just return an empty string.
137
         if (
138
            ! $this->template_args['event'] instanceof EE_Event
139
            || empty($this->query_params[0])
140
            || ($attributes['datetime_id'] && ! $this->template_args['datetime'] instanceof EE_Datetime)
141
            || ($attributes['ticket_id'] && ! $this->template_args['ticket'] instanceof EE_Ticket)
142
        ) {
143
            if (WP_DEBUG) {
144
                return '<div class="important-notice ee-attention">'
145
                       . esc_html__('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.',
146
                        'event_espresso')
147
                       . '</div>';
148
            }
149
             return '';
150
        }
151
        $this->setAdditionalQueryParams($attributes);
152
        //get contacts!
153
        $this->template_args['contacts'] = EEM_Attendee::instance()->get_all($this->query_params);
154
        //all set let's load up the template and return.
155
        return EEH_Template::locate_template(
156
            'loop-espresso_event_attendees.php',
157
            $this->template_args
158
        );
159
160
    }
161
162
163
164
    /**
165
     * merge incoming attributes with filtered defaults
166
     *
167
     * @param array $attributes
168
     * @return array
169
     */
170
    private function getAttributes(array $attributes)
171
    {
172
        return (array) apply_filters(
173
            'EES_Espresso_Event_Attendees__process_shortcode__default_shortcode_atts',
174
            $attributes + array(
175
                'event_id'            => null,
176
                'datetime_id'         => null,
177
                'ticket_id'           => null,
178
                'status'              => EEM_Registration::status_id_approved,
179
                'show_gravatar'       => false,
180
                'display_on_archives' => false,
181
            )
182
        );
183
    }
184
185
186
187
    /**
188
     * @param array $attributes
189
     * @return EE_Event|null
190
     * @throws EE_Error
191
     */
192
    private function getEventAndQueryParams(array $attributes){
193
        if ( ! empty($attributes['event_id'])) {
194
            $event = EEM_Event::instance()->get_one_by_ID($attributes['event_id']);
195
            if ($event instanceof EE_Event) {
196
                $this->query_params[0]['Registration.EVT_ID'] = $attributes['event_id'];
197
                return $event;
198
            }
199
        }
200
        if (is_espresso_event()) {
201
            $event = EEH_Event_View::get_event();
202
            if ($event instanceof EE_Event) {
203
                $this->query_params[0]['Registration.EVT_ID'] = $event->ID();
204
                return $event;
205
            }
206
        }
207
        // one last shot...
208
        // try getting the earliest active event
209
        $events = EEM_Event::instance()->get_active_events(array(
210
            'limit'    => 1,
211
            'order_by' => array('Datetime.DTT_EVT_start' => 'ASC')
212
        ));
213
        //  if none then get the next upcoming
214
        $events = empty($events)
215
            ? EEM_Event::instance()->get_upcoming_events(array(
216
                'limit'    => 1,
217
                'order_by' => array('Datetime.DTT_EVT_start' => 'ASC')
218
            ))
219
            : $events;
220
        $event = reset($events);
221
        if ($event instanceof EE_Event) {
222
            $this->query_params[0]['Registration.EVT_ID'] = $event->ID();
223
            return $event;
224
        }
225
        return null;
226
    }
227
228
229
230
    /**
231
     * @param array $attributes
232
     * @return EE_Datetime|null
233
     */
234
    private function getDatetimeAndQueryParams(array $attributes)
235
    {
236
        if ( ! empty($attributes['datetime_id'])) {
237
            $datetime = EEM_Datetime::instance()->get_one_by_ID($attributes['datetime_id']);
238
            if ($datetime instanceof EE_Datetime) {
239
                $this->query_params[0]['Registration.Ticket.Datetime.DTT_ID'] = $attributes['datetime_id'];
240
                $this->query_params['default_where_conditions'] = 'this_model_only';
241
                if ( ! $this->template_args['event'] instanceof EE_Event) {
242
                    $this->template_args['event'] = $datetime->event();
243
                }
244
                return $datetime;
245
            }
246
        }
247
        return null;
248
    }
249
250
251
252
    /**
253
     * @param array $attributes
254
     * @return \EE_Base_Class|null
255
     * @throws EE_Error
256
     */
257
    private function getTicketAndQueryParams(array $attributes)
258
    {
259
        if ( ! empty($attributes['ticket_id']) && empty($attributes['event_id']) && empty($attributes['datetime_id'])) {
260
            $ticket = EEM_Ticket::instance()->get_one_by_ID($attributes['ticket_id']);
261
            if ($ticket instanceof EE_Ticket) {
262
                $this->query_params[0]['Registration.TKT_ID'] = $attributes['ticket_id'];
263
                if ( ! $this->template_args['event'] instanceof EE_Event) {
264
                    $this->template_args['event'] = $ticket->first_datetime() instanceof EE_Datetime
265
                        ? $ticket->first_datetime()->event()
266
                        : null;
267
                }
268
                return $ticket;
269
            }
270
        }
271
        return null;
272
    }
273
274
275
276
    /**
277
     * @param array $attributes
278
     * @throws EE_Error
279
     */
280
    private function setAdditionalQueryParams(array $attributes)
281
    {
282
        $reg_status_array = EEM_Registration::reg_status_array();
283
        if ($attributes['status'] !== 'all' && isset($reg_status_array[$attributes['status']])) {
284
            $this->query_params[0]['Registration.STS_ID'] = $attributes['status'];
285
        }
286
        $this->query_params['group_by'] = array('ATT_ID');
287
        $this->query_params['order_by'] = (array) apply_filters(
288
            'FHEE__EES_Espresso_Event_Attendees__process_shortcode__order_by',
289
            array('ATT_lname' => 'ASC', 'ATT_fname' => 'ASC')
290
        );
291
    }
292
293
294
295
}
296
// End of file EspressoEventAttendees.php
297
// Location: EventEspresso\core\domain\entities\shortcodes/EspressoEventAttendees.php
298