Completed
Branch FET-10416-autoload-b4-bootstra... (42a01c)
by
unknown
13:17
created

EspressoEvents::processShortcode()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 1
dl 0
loc 31
rs 8.8571
c 0
b 0
f 0
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 0;
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
        $this->shortcodeHasBeenInitialized();
73
    }
74
75
76
77
    /**
78
     * callback that runs when the shortcode is encountered in post content.
79
     * IMPORTANT !!!
80
     * remember that shortcode content should be RETURNED and NOT echoed out
81
     *
82
     * @param array $attributes
83
     * @return string
84
     */
85
    public function processShortcode($attributes = array())
86
    {
87
        // grab attributes and merge with defaults
88
        $attributes = $this->getAttributes($attributes);
89
        // make sure we use the_excerpt()
90
        add_filter('FHEE__EES_Espresso_Events__process_shortcode__true', '__return_true');
91
        // apply query filters
92
        add_filter('FHEE__EEH_Event_Query__apply_query_filters', '__return_true');
93
        // run the query
94
        global $wp_query;
95
        // yes we have to overwrite the main wp query, but it's ok...
96
        // we're going to reset it again below, so everything will be Hunky Dory (amazing album)
97
        $wp_query = new EventListQuery($attributes);
98
        // check what template is loaded and load filters accordingly
99
        EED_Events_Archive::instance()->template_include('loop-espresso_events.php');
100
        // load our template
101
        $event_list = EEH_Template::locate_template(
102
            'loop-espresso_events.php',
103
            array(),
104
            true,
105
            true
106
        );
107
        // now reset the query and post data
108
        wp_reset_query();
109
        wp_reset_postdata();
110
        EED_Events_Archive::remove_all_events_archive_filters();
111
        // remove query filters
112
        remove_filter('FHEE__EEH_Event_Query__apply_query_filters', '__return_true');
113
        // pull our content from the output buffer and return it
114
        return $event_list;
115
    }
116
117
118
119
    /**
120
     * merge incoming attributes with filtered defaults
121
     *
122
     * @param array $attributes
123
     * @return array
124
     */
125
    private function getAttributes(array $attributes)
126
    {
127
        return array_merge(
128
            (array)apply_filters(
129
                'EES_Espresso_Events__process_shortcode__default_espresso_events_shortcode_atts',
130
                array(
131
                    'title'         => '',
132
                    'limit'         => 10,
133
                    'css_class'     => '',
134
                    'show_expired'  => false,
135
                    'month'         => '',
136
                    'category_slug' => '',
137
                    'order_by'      => 'start_date',
138
                    'sort'          => 'ASC',
139
                    'show_title'    => true,
140
                )
141
            ),
142
            $attributes
143
        );
144
    }
145
146
147
148
    /**
149
     * array for defining custom attribute sanitization callbacks,
150
     * where keys match keys in your attributes array,
151
     * and values represent the sanitization function you wish to be applied to that attribute.
152
     * So for example, if you had an integer attribute named "event_id"
153
     * that you wanted to be sanitized using absint(),
154
     * then you would pass the following for your $custom_sanitization array:
155
     *      array('event_id' => 'absint')
156
     *
157
     * @return array
158
     */
159
    protected function customAttributeSanitizationMap()
160
    {
161
        // the following get sanitized/whitelisted in EEH_Event_Query
162
        return array(
163
            'category_slug' => 'skip_sanitization',
164
            'show_expired'  => 'skip_sanitization',
165
            'order_by'      => 'skip_sanitization',
166
            'month'         => 'skip_sanitization',
167
            'sort'          => 'skip_sanitization',
168
        );
169
    }
170
171
172
173
}
174
// End of file EspressoEvents.php
175
// Location: EventEspresso\core\domain\entities\shortcodes/EspressoEvents.php
176