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

EspressoEvents   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 136
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 4

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getTag() 0 4 1
A cacheExpiration() 0 4 1
A initializeShortcode() 0 4 1
B processShortcode() 0 26 1
A getAttributes() 0 20 1
A customAttributeSanitizationMap() 0 11 1
1
<?php
2
namespace EventEspresso\core\domain\entities\shortcodes;
3
4
use EED_Events_Archive;
5
use EEH_Template;
6
use EventEspresso\core\domain\services\wp_queries\EventListQuery;
7
use EventEspresso\core\services\shortcodes\EspressoShortcode;
8
9
defined('EVENT_ESPRESSO_VERSION') || exit;
10
11
12
13
/**
14
 * Class EspressoEvents
15
 * processes the [ESPRESSO_EVENTS] shortcode and returns a list of events
16
 * usage:
17
 *    [ESPRESSO_EVENTS]
18
 *    [ESPRESSO_EVENTS title="My Super Event"]
19
 *    [ESPRESSO_EVENTS limit=5]
20
 *    [ESPRESSO_EVENTS css_class="my-custom-class"]
21
 *    [ESPRESSO_EVENTS month="April 2014"]
22
 *    [ESPRESSO_EVENTS show_expired=true]
23
 *    [ESPRESSO_EVENTS category_slug="free-events"]
24
 *    [ESPRESSO_EVENTS order_by="start_date,id"]
25
 *    [ESPRESSO_EVENTS sort="ASC"]
26
 *    [ESPRESSO_EVENTS show_title=true]
27
 *
28
 * @package       Event Espresso
29
 * @author        Brent Christensen
30
 * @since         4.9.26
31
 */
32
class EspressoEvents extends EspressoShortcode
33
{
34
35
36
37
    /**
38
     * the actual shortcode tag that gets registered with WordPress
39
     *
40
     * @return string
41
     */
42
    public function getTag()
43
    {
44
        return 'ESPRESSO_EVENTS';
45
    }
46
47
48
49
    /**
50
     * the time in seconds to cache the results of the processShortcode() method
51
     * 0 means the processShortcode() results will NOT be cached at all
52
     *
53
     * @return int
54
     */
55
    public function cacheExpiration()
56
    {
57
        return MINUTE_IN_SECONDS * 15;
58
    }
59
60
61
62
    /**
63
     * a place for adding any initialization code that needs to run prior to wp_header().
64
     * this may be required for shortcodes that utilize a corresponding module,
65
     * and need to enqueue assets for that module
66
     *
67
     * @return void
68
     */
69
    public function initializeShortcode()
70
    {
71
        EED_Events_Archive::instance()->event_list();
72
    }
73
74
75
76
    /**
77
     * callback that runs when the shortcode is encountered in post content.
78
     * IMPORTANT !!!
79
     * remember that shortcode content should be RETURNED and NOT echoed out
80
     *
81
     * @param array $attributes
82
     * @return string
83
     */
84
    public function processShortcode($attributes = array())
85
    {
86
        // grab attributes and merge with defaults
87
        $attributes = $this->getAttributes($attributes);
88
        // make sure we use the_excerpt()
89
        add_filter('FHEE__EES_Espresso_Events__process_shortcode__true', '__return_true');
90
        // apply query filters
91
        add_filter('FHEE__EEH_Event_Query__apply_query_filters', '__return_true');
92
        // run the query
93
        global $wp_query;
94
        // yes we have to overwrite the main wp query, but it's ok...
95
        // we're going to reset it again below, so everything will be Hunky Dory (amazing album)
96
        $wp_query = new EventListQuery($attributes);
97
        // check what template is loaded and load filters accordingly
98
        EED_Events_Archive::instance()->template_include('loop-espresso_events.php');
99
        // load our template
100
        $event_list = EEH_Template::locate_template('loop-espresso_events.php', array(), true, true);
101
        // now reset the query and postdata
102
        wp_reset_query();
103
        wp_reset_postdata();
104
        EED_Events_Archive::remove_all_events_archive_filters();
105
        // remove query filters
106
        remove_filter('FHEE__EEH_Event_Query__apply_query_filters', '__return_true');
107
        // pull our content from the output buffer and return it
108
        return $event_list;
109
    }
110
111
112
113
    /**
114
     * merge incoming attributes with filtered defaults
115
     *
116
     * @param array $attributes
117
     * @return array
118
     */
119
    private function getAttributes(array $attributes)
120
    {
121
        return array_merge(
122
            (array)apply_filters(
123
                'EES_Espresso_Events__process_shortcode__default_espresso_events_shortcode_atts',
124
                array(
125
                    'title'         => '',
126
                    'limit'         => 10,
127
                    'css_class'     => '',
128
                    'show_expired'  => false,
129
                    'month'         => '',
130
                    'category_slug' => '',
131
                    'order_by'      => 'start_date',
132
                    'sort'          => 'ASC',
133
                    'show_title'    => true,
134
                )
135
            ),
136
            $attributes
137
        );
138
    }
139
140
141
142
    /**
143
     * array for defining custom attribute sanitization callbacks,
144
     * where keys match keys in your attributes array,
145
     * and values represent the sanitization function you wish to be applied to that attribute.
146
     * So for example, if you had an integer attribute named "event_id"
147
     * that you wanted to be sanitized using absint(),
148
     * then you would pass the following for your $custom_sanitization array:
149
     *      array('event_id' => 'absint')
150
     *
151
     * @return array
152
     */
153
    protected function customAttributeSanitizationMap()
154
    {
155
        // the following get sanitized/whitelisted in EEH_Event_Query
156
        return array(
157
            'category_slug' => 'skip_sanitization',
158
            'show_expired'  => 'skip_sanitization',
159
            'order_by'      => 'skip_sanitization',
160
            'month'         => 'skip_sanitization',
161
            'sort'          => 'skip_sanitization',
162
        );
163
    }
164
165
166
167
}
168
// End of file EspressoEvents.php
169
// Location: EventEspresso\core\domain\entities\shortcodes/EspressoEvents.php