Completed
Branch FET-10486-add-timestamp-checki... (611b15)
by
unknown
105:07 queued 90:18
created

getDatetimeAndQueryParams()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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