@@ -38,103 +38,103 @@ |
||
38 | 38 | * @since 4.0 |
39 | 39 | */ |
40 | 40 | if (function_exists('espresso_version')) { |
41 | - if (! function_exists('espresso_duplicate_plugin_error')) { |
|
42 | - /** |
|
43 | - * espresso_duplicate_plugin_error |
|
44 | - * displays if more than one version of EE is activated at the same time |
|
45 | - */ |
|
46 | - function espresso_duplicate_plugin_error() |
|
47 | - { |
|
48 | - ?> |
|
41 | + if (! function_exists('espresso_duplicate_plugin_error')) { |
|
42 | + /** |
|
43 | + * espresso_duplicate_plugin_error |
|
44 | + * displays if more than one version of EE is activated at the same time |
|
45 | + */ |
|
46 | + function espresso_duplicate_plugin_error() |
|
47 | + { |
|
48 | + ?> |
|
49 | 49 | <div class="error"> |
50 | 50 | <p> |
51 | 51 | <?php |
52 | - echo esc_html__( |
|
53 | - 'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.', |
|
54 | - 'event_espresso' |
|
55 | - ); ?> |
|
52 | + echo esc_html__( |
|
53 | + 'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.', |
|
54 | + 'event_espresso' |
|
55 | + ); ?> |
|
56 | 56 | </p> |
57 | 57 | </div> |
58 | 58 | <?php |
59 | - espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
60 | - } |
|
61 | - } |
|
62 | - add_action('admin_notices', 'espresso_duplicate_plugin_error', 1); |
|
59 | + espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
60 | + } |
|
61 | + } |
|
62 | + add_action('admin_notices', 'espresso_duplicate_plugin_error', 1); |
|
63 | 63 | } else { |
64 | - define('EE_MIN_PHP_VER_REQUIRED', '5.6.2'); |
|
65 | - if (! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) { |
|
66 | - /** |
|
67 | - * espresso_minimum_php_version_error |
|
68 | - * |
|
69 | - * @return void |
|
70 | - */ |
|
71 | - function espresso_minimum_php_version_error() |
|
72 | - { |
|
73 | - ?> |
|
64 | + define('EE_MIN_PHP_VER_REQUIRED', '5.6.2'); |
|
65 | + if (! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) { |
|
66 | + /** |
|
67 | + * espresso_minimum_php_version_error |
|
68 | + * |
|
69 | + * @return void |
|
70 | + */ |
|
71 | + function espresso_minimum_php_version_error() |
|
72 | + { |
|
73 | + ?> |
|
74 | 74 | <div class="error"> |
75 | 75 | <p> |
76 | 76 | <?php |
77 | - printf( |
|
78 | - esc_html__( |
|
79 | - 'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.', |
|
80 | - 'event_espresso' |
|
81 | - ), |
|
82 | - EE_MIN_PHP_VER_REQUIRED, |
|
83 | - PHP_VERSION, |
|
84 | - '<br/>', |
|
85 | - '<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>' |
|
86 | - ); |
|
87 | - ?> |
|
77 | + printf( |
|
78 | + esc_html__( |
|
79 | + 'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.', |
|
80 | + 'event_espresso' |
|
81 | + ), |
|
82 | + EE_MIN_PHP_VER_REQUIRED, |
|
83 | + PHP_VERSION, |
|
84 | + '<br/>', |
|
85 | + '<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>' |
|
86 | + ); |
|
87 | + ?> |
|
88 | 88 | </p> |
89 | 89 | </div> |
90 | 90 | <?php |
91 | - espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
92 | - } |
|
91 | + espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
92 | + } |
|
93 | 93 | |
94 | - add_action('admin_notices', 'espresso_minimum_php_version_error', 1); |
|
95 | - } else { |
|
96 | - define('EVENT_ESPRESSO_MAIN_FILE', __FILE__); |
|
97 | - /** |
|
98 | - * espresso_version |
|
99 | - * Returns the plugin version |
|
100 | - * |
|
101 | - * @return string |
|
102 | - */ |
|
103 | - function espresso_version() |
|
104 | - { |
|
105 | - return apply_filters('FHEE__espresso__espresso_version', '4.10.30.rc.018'); |
|
106 | - } |
|
94 | + add_action('admin_notices', 'espresso_minimum_php_version_error', 1); |
|
95 | + } else { |
|
96 | + define('EVENT_ESPRESSO_MAIN_FILE', __FILE__); |
|
97 | + /** |
|
98 | + * espresso_version |
|
99 | + * Returns the plugin version |
|
100 | + * |
|
101 | + * @return string |
|
102 | + */ |
|
103 | + function espresso_version() |
|
104 | + { |
|
105 | + return apply_filters('FHEE__espresso__espresso_version', '4.10.30.rc.018'); |
|
106 | + } |
|
107 | 107 | |
108 | - /** |
|
109 | - * espresso_plugin_activation |
|
110 | - * adds a wp-option to indicate that EE has been activated via the WP admin plugins page |
|
111 | - */ |
|
112 | - function espresso_plugin_activation() |
|
113 | - { |
|
114 | - update_option('ee_espresso_activation', true); |
|
115 | - } |
|
108 | + /** |
|
109 | + * espresso_plugin_activation |
|
110 | + * adds a wp-option to indicate that EE has been activated via the WP admin plugins page |
|
111 | + */ |
|
112 | + function espresso_plugin_activation() |
|
113 | + { |
|
114 | + update_option('ee_espresso_activation', true); |
|
115 | + } |
|
116 | 116 | |
117 | - register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation'); |
|
117 | + register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation'); |
|
118 | 118 | |
119 | - require_once __DIR__ . '/core/bootstrap_espresso.php'; |
|
120 | - bootstrap_espresso(); |
|
121 | - } |
|
119 | + require_once __DIR__ . '/core/bootstrap_espresso.php'; |
|
120 | + bootstrap_espresso(); |
|
121 | + } |
|
122 | 122 | } |
123 | 123 | if (! function_exists('espresso_deactivate_plugin')) { |
124 | - /** |
|
125 | - * deactivate_plugin |
|
126 | - * usage: espresso_deactivate_plugin( plugin_basename( __FILE__ )); |
|
127 | - * |
|
128 | - * @access public |
|
129 | - * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file |
|
130 | - * @return void |
|
131 | - */ |
|
132 | - function espresso_deactivate_plugin($plugin_basename = '') |
|
133 | - { |
|
134 | - if (! function_exists('deactivate_plugins')) { |
|
135 | - require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
|
136 | - } |
|
137 | - unset($_GET['activate'], $_REQUEST['activate']); |
|
138 | - deactivate_plugins($plugin_basename); |
|
139 | - } |
|
124 | + /** |
|
125 | + * deactivate_plugin |
|
126 | + * usage: espresso_deactivate_plugin( plugin_basename( __FILE__ )); |
|
127 | + * |
|
128 | + * @access public |
|
129 | + * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file |
|
130 | + * @return void |
|
131 | + */ |
|
132 | + function espresso_deactivate_plugin($plugin_basename = '') |
|
133 | + { |
|
134 | + if (! function_exists('deactivate_plugins')) { |
|
135 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
|
136 | + } |
|
137 | + unset($_GET['activate'], $_REQUEST['activate']); |
|
138 | + deactivate_plugins($plugin_basename); |
|
139 | + } |
|
140 | 140 | } |
141 | 141 | \ No newline at end of file |
@@ -18,666 +18,666 @@ |
||
18 | 18 | class EEH_Event_Query |
19 | 19 | { |
20 | 20 | |
21 | - /** |
|
22 | - * Start Date |
|
23 | - * |
|
24 | - * @var $_event_query_month |
|
25 | - */ |
|
26 | - protected static $_event_query_month; |
|
27 | - |
|
28 | - /** |
|
29 | - * Category |
|
30 | - * |
|
31 | - * @var $_event_query_category |
|
32 | - */ |
|
33 | - protected static $_event_query_category; |
|
34 | - |
|
35 | - /** |
|
36 | - * whether to display expired events in the event list |
|
37 | - * |
|
38 | - * @var bool $_show_expired |
|
39 | - */ |
|
40 | - protected static $_event_query_show_expired = false; |
|
41 | - |
|
42 | - /** |
|
43 | - * list of params for controlling how the query results are ordered |
|
44 | - * |
|
45 | - * @var array $_event_query_orderby |
|
46 | - */ |
|
47 | - protected static $_event_query_orderby = []; |
|
48 | - |
|
49 | - /** |
|
50 | - * direction list is sorted |
|
51 | - * |
|
52 | - * @var string $_event_query_sort |
|
53 | - */ |
|
54 | - protected static $_event_query_sort; |
|
55 | - |
|
56 | - /** |
|
57 | - * list of params used to build the query's various clauses |
|
58 | - * |
|
59 | - * @var $_query_params |
|
60 | - */ |
|
61 | - protected static $_query_params = []; |
|
62 | - |
|
63 | - |
|
64 | - /** |
|
65 | - * @return void |
|
66 | - */ |
|
67 | - public static function add_query_filters() |
|
68 | - { |
|
69 | - // add query filters |
|
70 | - add_action('pre_get_posts', ['EEH_Event_Query', 'filter_query_parts'], 10, 1); |
|
71 | - } |
|
72 | - |
|
73 | - |
|
74 | - /** |
|
75 | - * @param WP_Query $WP_Query |
|
76 | - * @return bool |
|
77 | - */ |
|
78 | - public static function apply_query_filters(WP_Query $WP_Query) |
|
79 | - { |
|
80 | - return ( |
|
81 | - isset($WP_Query->query['post_type']) |
|
82 | - && $WP_Query->query['post_type'] === 'espresso_events' |
|
83 | - ) |
|
84 | - || apply_filters('FHEE__EEH_Event_Query__apply_query_filters', false); |
|
85 | - } |
|
86 | - |
|
87 | - |
|
88 | - /** |
|
89 | - * @param WP_Query $WP_Query |
|
90 | - */ |
|
91 | - public static function filter_query_parts(WP_Query $WP_Query) |
|
92 | - { |
|
93 | - // ONLY add our filters if this isn't the main wp_query, |
|
94 | - // because if this is the main wp_query we already have |
|
95 | - // our cpt strategies take care of adding things in. |
|
96 | - if ($WP_Query instanceof WP_Query && ! $WP_Query->is_main_query()) { |
|
97 | - // build event list query |
|
98 | - add_filter('posts_fields', ['EEH_Event_Query', 'posts_fields'], 10, 2); |
|
99 | - add_filter('posts_join', ['EEH_Event_Query', 'posts_join'], 10, 2); |
|
100 | - add_filter('posts_where', ['EEH_Event_Query', 'posts_where'], 10, 2); |
|
101 | - add_filter('posts_orderby', ['EEH_Event_Query', 'posts_orderby'], 10, 2); |
|
102 | - add_filter('posts_clauses_request', ['EEH_Event_Query', 'posts_clauses'], 10, 2); |
|
103 | - } |
|
104 | - } |
|
105 | - |
|
106 | - |
|
107 | - /** |
|
108 | - * @param string $month |
|
109 | - * @param string $category |
|
110 | - * @param bool $show_expired |
|
111 | - * @param array|string $orderby |
|
112 | - * @param string $sort |
|
113 | - * @throws InvalidArgumentException |
|
114 | - * @throws InvalidDataTypeException |
|
115 | - * @throws InvalidInterfaceException |
|
116 | - */ |
|
117 | - public static function set_query_params( |
|
118 | - $month = '', |
|
119 | - $category = '', |
|
120 | - $show_expired = false, |
|
121 | - $orderby = 'start_date', |
|
122 | - $sort = 'ASC' |
|
123 | - ) { |
|
124 | - self::$_query_params = []; |
|
125 | - EEH_Event_Query::$_event_query_month = EEH_Event_Query::_display_month($month); |
|
126 | - EEH_Event_Query::$_event_query_category = EEH_Event_Query::_event_category_slug($category); |
|
127 | - EEH_Event_Query::$_event_query_show_expired = EEH_Event_Query::_show_expired($show_expired); |
|
128 | - EEH_Event_Query::$_event_query_orderby = EEH_Event_Query::_orderby($orderby); |
|
129 | - EEH_Event_Query::$_event_query_sort = EEH_Event_Query::_sort($sort); |
|
130 | - } |
|
131 | - |
|
132 | - |
|
133 | - /** |
|
134 | - * what month should the event list display events for? |
|
135 | - * |
|
136 | - * @param string $month |
|
137 | - * @return string |
|
138 | - * @throws InvalidArgumentException |
|
139 | - * @throws InvalidDataTypeException |
|
140 | - * @throws InvalidInterfaceException |
|
141 | - */ |
|
142 | - private static function _display_month($month = '') |
|
143 | - { |
|
144 | - return self::getRequest()->getRequestParam('event_query_month', $month); |
|
145 | - } |
|
146 | - |
|
147 | - |
|
148 | - /** |
|
149 | - * @param string $category |
|
150 | - * @return string |
|
151 | - * @throws InvalidArgumentException |
|
152 | - * @throws InvalidDataTypeException |
|
153 | - * @throws InvalidInterfaceException |
|
154 | - */ |
|
155 | - private static function _event_category_slug($category = '') |
|
156 | - { |
|
157 | - return self::getRequest()->getRequestParam('event_query_category', $category); |
|
158 | - } |
|
159 | - |
|
160 | - |
|
161 | - /** |
|
162 | - * @param bool $show_expired |
|
163 | - * @return bool |
|
164 | - * @throws InvalidArgumentException |
|
165 | - * @throws InvalidDataTypeException |
|
166 | - * @throws InvalidInterfaceException |
|
167 | - */ |
|
168 | - private static function _show_expired($show_expired = false) |
|
169 | - { |
|
170 | - // override default expired option if set via filter |
|
171 | - return self::getRequest()->getRequestParam('event_query_show_expired', $show_expired, 'bool'); |
|
172 | - } |
|
173 | - |
|
174 | - |
|
175 | - /** |
|
176 | - * @param array|string $orderby |
|
177 | - * @return array |
|
178 | - * @throws InvalidArgumentException |
|
179 | - * @throws InvalidDataTypeException |
|
180 | - * @throws InvalidInterfaceException |
|
181 | - */ |
|
182 | - private static function _orderby($orderby = 'start_date') |
|
183 | - { |
|
184 | - $event_query_orderby = self::getRequest()->getRequestParam( |
|
185 | - 'event_query_orderby', |
|
186 | - (array) $orderby, |
|
187 | - DataType::STRING, |
|
188 | - true |
|
189 | - ); |
|
190 | - $event_query_orderby = is_array($event_query_orderby) |
|
191 | - ? $event_query_orderby |
|
192 | - : explode(',', $event_query_orderby); |
|
193 | - $event_query_orderby = array_map('trim', $event_query_orderby); |
|
194 | - return array_map('sanitize_text_field', $event_query_orderby); |
|
195 | - } |
|
196 | - |
|
197 | - |
|
198 | - /** |
|
199 | - * @param string $sort |
|
200 | - * @return string |
|
201 | - * @throws InvalidArgumentException |
|
202 | - * @throws InvalidDataTypeException |
|
203 | - * @throws InvalidInterfaceException |
|
204 | - */ |
|
205 | - private static function _sort($sort = 'ASC') |
|
206 | - { |
|
207 | - $sort = self::getRequest()->getRequestParam('event_query_sort', $sort); |
|
208 | - return in_array($sort, ['ASC', 'asc', 'DESC', 'desc'], true) |
|
209 | - ? strtoupper($sort) |
|
210 | - : 'ASC'; |
|
211 | - } |
|
212 | - |
|
213 | - |
|
214 | - /** |
|
215 | - * Filters the clauses for the WP_Query object |
|
216 | - * |
|
217 | - * @param array $clauses array of clauses |
|
218 | - * @param WP_Query $wp_query |
|
219 | - * @return array array of clauses |
|
220 | - */ |
|
221 | - public static function posts_clauses($clauses, WP_Query $wp_query) |
|
222 | - { |
|
223 | - if (EEH_Event_Query::apply_query_filters($wp_query)) { |
|
224 | - global $wpdb; |
|
225 | - $clauses['groupby'] = $wpdb->posts . '.ID '; |
|
226 | - } |
|
227 | - return $clauses; |
|
228 | - } |
|
229 | - |
|
230 | - |
|
231 | - /** |
|
232 | - * @param string $SQL |
|
233 | - * @param WP_Query $wp_query |
|
234 | - * @return string |
|
235 | - * @throws EE_Error |
|
236 | - * @throws InvalidArgumentException |
|
237 | - * @throws InvalidDataTypeException |
|
238 | - * @throws InvalidInterfaceException |
|
239 | - */ |
|
240 | - public static function posts_fields($SQL, WP_Query $wp_query) |
|
241 | - { |
|
242 | - if (EEH_Event_Query::apply_query_filters($wp_query)) { |
|
243 | - // adds something like ", wp_esp_datetime.* " to WP Query SELECT statement |
|
244 | - $SQL .= EEH_Event_Query::posts_fields_sql_for_orderby(EEH_Event_Query::$_event_query_orderby); |
|
245 | - } |
|
246 | - return $SQL; |
|
247 | - } |
|
248 | - |
|
249 | - |
|
250 | - /** |
|
251 | - * @param array $orderby_params |
|
252 | - * @return string |
|
253 | - * @throws EE_Error |
|
254 | - * @throws InvalidArgumentException |
|
255 | - * @throws InvalidDataTypeException |
|
256 | - * @throws InvalidInterfaceException |
|
257 | - */ |
|
258 | - public static function posts_fields_sql_for_orderby(array $orderby_params = []) |
|
259 | - { |
|
260 | - $SQL = ', MIN( ' . EEM_Datetime::instance()->table() . '.DTT_EVT_start ) as event_start_date '; |
|
261 | - foreach ($orderby_params as $orderby) { |
|
262 | - switch ($orderby) { |
|
263 | - case 'ticket_start': |
|
264 | - $SQL .= ', ' . EEM_Ticket::instance()->table() . '.TKT_start_date'; |
|
265 | - break; |
|
266 | - case 'ticket_end': |
|
267 | - $SQL .= ', ' . EEM_Ticket::instance()->table() . '.TKT_end_date'; |
|
268 | - break; |
|
269 | - case 'venue_title': |
|
270 | - $SQL .= ', Venue.post_title AS venue_title'; |
|
271 | - break; |
|
272 | - case 'city': |
|
273 | - $SQL .= ', ' . EEM_Venue::instance()->second_table() . '.VNU_city'; |
|
274 | - break; |
|
275 | - case 'state': |
|
276 | - $SQL .= ', ' . EEM_State::instance()->table() . '.STA_name'; |
|
277 | - break; |
|
278 | - } |
|
279 | - } |
|
280 | - return $SQL; |
|
281 | - } |
|
282 | - |
|
283 | - |
|
284 | - /** |
|
285 | - * @param string $SQL |
|
286 | - * @param WP_Query $wp_query |
|
287 | - * @return string |
|
288 | - * @throws EE_Error |
|
289 | - * @throws InvalidArgumentException |
|
290 | - * @throws InvalidDataTypeException |
|
291 | - * @throws InvalidInterfaceException |
|
292 | - */ |
|
293 | - public static function posts_join($SQL, WP_Query $wp_query) |
|
294 | - { |
|
295 | - if (EEH_Event_Query::apply_query_filters($wp_query)) { |
|
296 | - // Category |
|
297 | - $SQL = EEH_Event_Query::posts_join_sql_for_show_expired($SQL, EEH_Event_Query::$_event_query_show_expired); |
|
298 | - $SQL = EEH_Event_Query::posts_join_sql_for_terms($SQL, EEH_Event_Query::$_event_query_category); |
|
299 | - $SQL = EEH_Event_Query::posts_join_for_orderby($SQL, EEH_Event_Query::$_event_query_orderby); |
|
300 | - } |
|
301 | - return $SQL; |
|
302 | - } |
|
303 | - |
|
304 | - |
|
305 | - /** |
|
306 | - * @param string $SQL |
|
307 | - * @param boolean $show_expired if TRUE, then displayed past events |
|
308 | - * @return string |
|
309 | - * @throws EE_Error |
|
310 | - * @throws InvalidArgumentException |
|
311 | - * @throws InvalidDataTypeException |
|
312 | - * @throws InvalidInterfaceException |
|
313 | - */ |
|
314 | - public static function posts_join_sql_for_show_expired($SQL = '', $show_expired = false) |
|
315 | - { |
|
316 | - if (! $show_expired) { |
|
317 | - $join = EEM_Event::instance()->table() . '.ID = '; |
|
318 | - $join .= EEM_Datetime::instance()->table() . '.' . EEM_Event::instance()->primary_key_name(); |
|
319 | - // don't add if this is already in the SQL |
|
320 | - if (strpos($SQL, $join) === false) { |
|
321 | - $SQL .= ' INNER JOIN ' . EEM_Datetime::instance()->table() . ' ON ( ' . $join . ' ) '; |
|
322 | - } |
|
323 | - } |
|
324 | - return $SQL; |
|
325 | - } |
|
326 | - |
|
327 | - |
|
328 | - /** |
|
329 | - * @param string $SQL |
|
330 | - * @param string $join_terms pass TRUE or term string, doesn't really matter since this value doesn't really get |
|
331 | - * used for anything yet |
|
332 | - * @return string |
|
333 | - */ |
|
334 | - public static function posts_join_sql_for_terms($SQL = '', $join_terms = '') |
|
335 | - { |
|
336 | - if (! empty($join_terms)) { |
|
337 | - global $wpdb; |
|
338 | - $SQL .= " LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)"; |
|
339 | - $SQL .= " LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)"; |
|
340 | - $SQL .= " LEFT JOIN $wpdb->terms ON ($wpdb->terms.term_id = $wpdb->term_taxonomy.term_id) "; |
|
341 | - } |
|
342 | - return $SQL; |
|
343 | - } |
|
344 | - |
|
345 | - |
|
346 | - /** |
|
347 | - * usage: $SQL .= EEH_Event_Query::posts_join_for_orderby( $orderby_params ); |
|
348 | - * |
|
349 | - * @param string $SQL |
|
350 | - * @param array $orderby_params |
|
351 | - * @return string |
|
352 | - * @throws EE_Error |
|
353 | - * @throws InvalidArgumentException |
|
354 | - * @throws InvalidDataTypeException |
|
355 | - * @throws InvalidInterfaceException |
|
356 | - */ |
|
357 | - public static function posts_join_for_orderby($SQL = '', array $orderby_params = []) |
|
358 | - { |
|
359 | - foreach ($orderby_params as $orderby) { |
|
360 | - switch ($orderby) { |
|
361 | - case 'ticket_start': |
|
362 | - case 'ticket_end': |
|
363 | - $SQL .= EEH_Event_Query::_posts_join_for_datetime( |
|
364 | - $SQL, |
|
365 | - EEM_Datetime_Ticket::instance()->table() . '.' . EEM_Datetime::instance()->primary_key_name() |
|
366 | - ); |
|
367 | - $SQL .= ' LEFT JOIN ' . EEM_Ticket::instance()->table(); |
|
368 | - $SQL .= ' ON ('; |
|
369 | - $SQL .= EEM_Datetime_Ticket::instance()->table() . '.' . EEM_Ticket::instance()->primary_key_name(); |
|
370 | - $SQL .= ' = '; |
|
371 | - $SQL .= EEM_Ticket::instance()->table() . '.' . EEM_Ticket::instance()->primary_key_name(); |
|
372 | - $SQL .= ' )'; |
|
373 | - break; |
|
374 | - case 'venue_title': |
|
375 | - case 'city': |
|
376 | - $SQL .= EEH_Event_Query::_posts_join_for_event_venue($SQL); |
|
377 | - break; |
|
378 | - case 'state': |
|
379 | - $SQL .= EEH_Event_Query::_posts_join_for_event_venue($SQL); |
|
380 | - $SQL .= EEH_Event_Query::_posts_join_for_venue_state($SQL); |
|
381 | - break; |
|
382 | - case 'start_date': |
|
383 | - default: |
|
384 | - $SQL .= EEH_Event_Query::_posts_join_for_datetime($SQL, EEM_Event::instance()->table() . '.ID'); |
|
385 | - break; |
|
386 | - } |
|
387 | - } |
|
388 | - return $SQL; |
|
389 | - } |
|
390 | - |
|
391 | - |
|
392 | - /** |
|
393 | - * @param string $SQL |
|
394 | - * @param string $join |
|
395 | - * @return string |
|
396 | - * @throws EE_Error |
|
397 | - * @throws InvalidArgumentException |
|
398 | - * @throws InvalidDataTypeException |
|
399 | - * @throws InvalidInterfaceException |
|
400 | - */ |
|
401 | - protected static function _posts_join_for_datetime($SQL = '', $join = '') |
|
402 | - { |
|
403 | - if (! empty($join)) { |
|
404 | - $join .= ' = ' . EEM_Datetime::instance()->table() . '.' . EEM_Event::instance()->primary_key_name(); |
|
405 | - if (strpos($SQL, $join) === false) { |
|
406 | - return ' INNER JOIN ' . EEM_Datetime::instance()->table() . ' ON ( ' . $join . ' )'; |
|
407 | - } |
|
408 | - } |
|
409 | - return ''; |
|
410 | - } |
|
411 | - |
|
412 | - |
|
413 | - /** |
|
414 | - * @param string $SQL |
|
415 | - * @return string |
|
416 | - * @throws EE_Error |
|
417 | - * @throws InvalidArgumentException |
|
418 | - * @throws InvalidDataTypeException |
|
419 | - * @throws InvalidInterfaceException |
|
420 | - */ |
|
421 | - protected static function _posts_join_for_event_venue($SQL = '') |
|
422 | - { |
|
423 | - // Event Venue table name |
|
424 | - $event_venue_table = EEM_Event_Venue::instance()->table(); |
|
425 | - // generate conditions for: Event <=> Event Venue JOIN clause |
|
426 | - $event_to_event_venue_join = EEM_Event::instance()->table() . '.ID = '; |
|
427 | - $event_to_event_venue_join .= $event_venue_table . '.' . EEM_Event::instance()->primary_key_name(); |
|
428 | - // don't add joins if they have already been added |
|
429 | - if (strpos($SQL, $event_to_event_venue_join) === false) { |
|
430 | - // Venue table name |
|
431 | - $venue_table = EEM_Venue::instance()->table(); |
|
432 | - // Venue table pk |
|
433 | - $venue_table_pk = EEM_Venue::instance()->primary_key_name(); |
|
434 | - // Venue Meta table name |
|
435 | - $venue_meta_table = EEM_Venue::instance()->second_table(); |
|
436 | - // generate JOIN clause for: Event <=> Event Venue |
|
437 | - $venue_SQL = " LEFT JOIN $event_venue_table ON ( $event_to_event_venue_join )"; |
|
438 | - // generate JOIN clause for: Event Venue <=> Venue |
|
439 | - $venue_SQL .= " LEFT JOIN $venue_table as Venue ON ( $event_venue_table.$venue_table_pk = Venue.ID )"; |
|
440 | - // generate JOIN clause for: Venue <=> Venue Meta |
|
441 | - $venue_SQL .= " LEFT JOIN $venue_meta_table ON ( Venue.ID = $venue_meta_table.$venue_table_pk )"; |
|
442 | - unset($event_venue_table, $event_to_event_venue_join, $venue_table, $venue_table_pk, $venue_meta_table); |
|
443 | - return $venue_SQL; |
|
444 | - } |
|
445 | - unset($event_venue_table, $event_to_event_venue_join); |
|
446 | - return ''; |
|
447 | - } |
|
448 | - |
|
449 | - |
|
450 | - /** |
|
451 | - * @param string $SQL |
|
452 | - * @return string |
|
453 | - * @throws EE_Error |
|
454 | - * @throws InvalidArgumentException |
|
455 | - * @throws InvalidDataTypeException |
|
456 | - * @throws InvalidInterfaceException |
|
457 | - */ |
|
458 | - protected static function _posts_join_for_venue_state($SQL = '') |
|
459 | - { |
|
460 | - // Venue Meta table name |
|
461 | - $venue_meta_table = EEM_Venue::instance()->second_table(); |
|
462 | - // State table name |
|
463 | - $state_table = EEM_State::instance()->table(); |
|
464 | - // State table pk |
|
465 | - $state_table_pk = EEM_State::instance()->primary_key_name(); |
|
466 | - // verify vars |
|
467 | - if ($venue_meta_table && $state_table && $state_table_pk) { |
|
468 | - // like: wp_esp_venue_meta.STA_ID = wp_esp_state.STA_ID |
|
469 | - $join = "$venue_meta_table.$state_table_pk = $state_table.$state_table_pk"; |
|
470 | - // don't add join if it has already been added |
|
471 | - if (strpos($SQL, $join) === false) { |
|
472 | - unset($state_table_pk, $venue_meta_table, $venue_table_pk); |
|
473 | - return " LEFT JOIN $state_table ON ( $join )"; |
|
474 | - } |
|
475 | - } |
|
476 | - unset($join, $state_table, $state_table_pk, $venue_meta_table, $venue_table_pk); |
|
477 | - return ''; |
|
478 | - } |
|
479 | - |
|
480 | - |
|
481 | - /** |
|
482 | - * @param string $SQL |
|
483 | - * @param WP_Query $wp_query |
|
484 | - * @return string |
|
485 | - * @throws EE_Error |
|
486 | - * @throws InvalidArgumentException |
|
487 | - * @throws InvalidDataTypeException |
|
488 | - * @throws InvalidInterfaceException |
|
489 | - */ |
|
490 | - public static function posts_where($SQL, WP_Query $wp_query) |
|
491 | - { |
|
492 | - if (EEH_Event_Query::apply_query_filters($wp_query)) { |
|
493 | - // Show Expired ? |
|
494 | - $SQL .= EEH_Event_Query::posts_where_sql_for_show_expired(EEH_Event_Query::$_event_query_show_expired); |
|
495 | - // Category |
|
496 | - $SQL .= EEH_Event_Query::posts_where_sql_for_event_category_slug(EEH_Event_Query::$_event_query_category); |
|
497 | - // Start Date |
|
498 | - $SQL .= EEH_Event_Query::posts_where_sql_for_event_list_month(EEH_Event_Query::$_event_query_month); |
|
499 | - } |
|
500 | - return $SQL; |
|
501 | - } |
|
502 | - |
|
503 | - |
|
504 | - /** |
|
505 | - * @param boolean $show_expired if TRUE, then displayed past events |
|
506 | - * @return string |
|
507 | - * @throws EE_Error |
|
508 | - * @throws InvalidArgumentException |
|
509 | - * @throws InvalidDataTypeException |
|
510 | - * @throws InvalidInterfaceException |
|
511 | - */ |
|
512 | - public static function posts_where_sql_for_show_expired($show_expired = false) |
|
513 | - { |
|
514 | - return ! $show_expired |
|
515 | - ? ' AND ' . EEM_Datetime::instance()->table() . '.DTT_EVT_end > \'' . current_time('mysql', true) . '\' ' |
|
516 | - : ''; |
|
517 | - } |
|
518 | - |
|
519 | - |
|
520 | - /** |
|
521 | - * @param boolean $event_category_slug |
|
522 | - * @return string |
|
523 | - */ |
|
524 | - public static function posts_where_sql_for_event_category_slug($event_category_slug = null) |
|
525 | - { |
|
526 | - global $wpdb; |
|
527 | - if (! empty($event_category_slug)) { |
|
528 | - $event_category_slugs_array = array_map('trim', explode(',', $event_category_slug)); |
|
529 | - $event_category_slugs_prepare = implode(', ', array_fill(0, count($event_category_slugs_array), '%s')); |
|
530 | - return $wpdb->prepare( |
|
531 | - " AND {$wpdb->terms}.slug IN ({$event_category_slugs_prepare}) ", |
|
532 | - $event_category_slugs_array |
|
533 | - ); |
|
534 | - } |
|
535 | - return ''; |
|
536 | - } |
|
537 | - |
|
538 | - |
|
539 | - /** |
|
540 | - * @param boolean $month |
|
541 | - * @return string |
|
542 | - * @throws EE_Error |
|
543 | - * @throws InvalidArgumentException |
|
544 | - * @throws InvalidDataTypeException |
|
545 | - * @throws InvalidInterfaceException |
|
546 | - */ |
|
547 | - public static function posts_where_sql_for_event_list_month($month = null) |
|
548 | - { |
|
549 | - $SQL = ''; |
|
550 | - if (! empty($month)) { |
|
551 | - $datetime_table = EEM_Datetime::instance()->table(); |
|
552 | - // event start date is LESS than the end of the month ( so nothing that doesn't start until next month ) |
|
553 | - $SQL = " AND {$datetime_table}.DTT_EVT_start <= '"; |
|
554 | - $SQL .= date('Y-m-t 23:59:59', EEH_DTT_Helper::first_of_month_timestamp($month)) . "'"; |
|
555 | - // event end date is GREATER than the start of the month ( so nothing that ended before this month ) |
|
556 | - $SQL .= " AND {$datetime_table}.DTT_EVT_end >= '"; |
|
557 | - $SQL .= date('Y-m-01 0:0:00', EEH_DTT_Helper::first_of_month_timestamp($month)) . "' "; |
|
558 | - } |
|
559 | - return $SQL; |
|
560 | - } |
|
561 | - |
|
562 | - |
|
563 | - /** |
|
564 | - * @param string $SQL |
|
565 | - * @param WP_Query $wp_query |
|
566 | - * @return string |
|
567 | - * @throws EE_Error |
|
568 | - * @throws InvalidArgumentException |
|
569 | - * @throws InvalidDataTypeException |
|
570 | - * @throws InvalidInterfaceException |
|
571 | - */ |
|
572 | - public static function posts_orderby($SQL, WP_Query $wp_query) |
|
573 | - { |
|
574 | - if (EEH_Event_Query::apply_query_filters($wp_query)) { |
|
575 | - $SQL = EEH_Event_Query::posts_orderby_sql( |
|
576 | - EEH_Event_Query::$_event_query_orderby, |
|
577 | - EEH_Event_Query::$_event_query_sort |
|
578 | - ); |
|
579 | - } |
|
580 | - return $SQL; |
|
581 | - } |
|
582 | - |
|
583 | - |
|
584 | - /** |
|
585 | - * posts_orderby_sql |
|
586 | - * possible parameters: |
|
587 | - * ID |
|
588 | - * start_date |
|
589 | - * end_date |
|
590 | - * event_name |
|
591 | - * category_slug |
|
592 | - * ticket_start |
|
593 | - * ticket_end |
|
594 | - * venue_title |
|
595 | - * city |
|
596 | - * state |
|
597 | - * **IMPORTANT** |
|
598 | - * make sure to also send the $orderby_params array to the posts_join_for_orderby() method |
|
599 | - * or else some of the table references below will result in MySQL errors |
|
600 | - * |
|
601 | - * @param array $orderby_params |
|
602 | - * @param string $sort |
|
603 | - * @return string |
|
604 | - * @throws EE_Error |
|
605 | - * @throws InvalidArgumentException |
|
606 | - * @throws InvalidDataTypeException |
|
607 | - * @throws InvalidInterfaceException |
|
608 | - */ |
|
609 | - public static function posts_orderby_sql(array $orderby_params = [], $sort = 'ASC') |
|
610 | - { |
|
611 | - global $wpdb; |
|
612 | - $SQL = ''; |
|
613 | - $counter = 0; |
|
614 | - $sort = in_array($sort, ['ASC', 'asc', 'DESC', 'desc'], true) |
|
615 | - ? strtoupper($sort) |
|
616 | - : 'ASC'; |
|
617 | - // make sure 'orderby' is set in query params |
|
618 | - if (! isset(self::$_query_params['orderby'])) { |
|
619 | - self::$_query_params['orderby'] = []; |
|
620 | - } |
|
621 | - // loop thru $orderby_params (type cast as array) |
|
622 | - foreach ($orderby_params as $orderby) { |
|
623 | - // check if we have already added this param |
|
624 | - if (isset(self::$_query_params['orderby'][ $orderby ])) { |
|
625 | - // if so then remove from the $orderby_params so that the count() method below is accurate |
|
626 | - unset($orderby_params[ $orderby ]); |
|
627 | - // then bump ahead to the next param |
|
628 | - continue; |
|
629 | - } |
|
630 | - // this will ad a comma depending on whether this is the first or last param |
|
631 | - $glue = $counter === 0 || $counter === count($orderby_params) ? ' ' : ', '; |
|
632 | - // ok what's we dealing with? |
|
633 | - switch ($orderby) { |
|
634 | - case 'id': |
|
635 | - case 'ID': |
|
636 | - $SQL .= $glue . $wpdb->posts . '.ID ' . $sort; |
|
637 | - break; |
|
638 | - case 'end_date': |
|
639 | - $SQL .= $glue . EEM_Datetime::instance()->table() . '.DTT_EVT_end ' . $sort; |
|
640 | - break; |
|
641 | - case 'event_name': |
|
642 | - $SQL .= $glue . $wpdb->posts . '.post_title ' . $sort; |
|
643 | - break; |
|
644 | - case 'category_slug': |
|
645 | - $SQL .= $glue . $wpdb->terms . '.slug ' . $sort; |
|
646 | - break; |
|
647 | - case 'ticket_start': |
|
648 | - $SQL .= $glue . EEM_Ticket::instance()->table() . '.TKT_start_date ' . $sort; |
|
649 | - break; |
|
650 | - case 'ticket_end': |
|
651 | - $SQL .= $glue . EEM_Ticket::instance()->table() . '.TKT_end_date ' . $sort; |
|
652 | - break; |
|
653 | - case 'venue_title': |
|
654 | - $SQL .= $glue . 'venue_title ' . $sort; |
|
655 | - break; |
|
656 | - case 'city': |
|
657 | - $SQL .= $glue . EEM_Venue::instance()->second_table() . '.VNU_city ' . $sort; |
|
658 | - break; |
|
659 | - case 'state': |
|
660 | - $SQL .= $glue . EEM_State::instance()->table() . '.STA_name ' . $sort; |
|
661 | - break; |
|
662 | - case 'start_date': |
|
663 | - default: |
|
664 | - $SQL .= $glue . ' event_start_date ' . $sort; |
|
665 | - break; |
|
666 | - } |
|
667 | - // add to array of orderby params that have been added |
|
668 | - self::$_query_params['orderby'][ $orderby ] = true; |
|
669 | - $counter++; |
|
670 | - } |
|
671 | - return $SQL; |
|
672 | - } |
|
673 | - |
|
674 | - |
|
675 | - /** |
|
676 | - * @return RequestInterface |
|
677 | - * @since 4.10.14.p |
|
678 | - */ |
|
679 | - private static function getRequest() |
|
680 | - { |
|
681 | - return LoaderFactory::getLoader()->getShared(RequestInterface::class); |
|
682 | - } |
|
21 | + /** |
|
22 | + * Start Date |
|
23 | + * |
|
24 | + * @var $_event_query_month |
|
25 | + */ |
|
26 | + protected static $_event_query_month; |
|
27 | + |
|
28 | + /** |
|
29 | + * Category |
|
30 | + * |
|
31 | + * @var $_event_query_category |
|
32 | + */ |
|
33 | + protected static $_event_query_category; |
|
34 | + |
|
35 | + /** |
|
36 | + * whether to display expired events in the event list |
|
37 | + * |
|
38 | + * @var bool $_show_expired |
|
39 | + */ |
|
40 | + protected static $_event_query_show_expired = false; |
|
41 | + |
|
42 | + /** |
|
43 | + * list of params for controlling how the query results are ordered |
|
44 | + * |
|
45 | + * @var array $_event_query_orderby |
|
46 | + */ |
|
47 | + protected static $_event_query_orderby = []; |
|
48 | + |
|
49 | + /** |
|
50 | + * direction list is sorted |
|
51 | + * |
|
52 | + * @var string $_event_query_sort |
|
53 | + */ |
|
54 | + protected static $_event_query_sort; |
|
55 | + |
|
56 | + /** |
|
57 | + * list of params used to build the query's various clauses |
|
58 | + * |
|
59 | + * @var $_query_params |
|
60 | + */ |
|
61 | + protected static $_query_params = []; |
|
62 | + |
|
63 | + |
|
64 | + /** |
|
65 | + * @return void |
|
66 | + */ |
|
67 | + public static function add_query_filters() |
|
68 | + { |
|
69 | + // add query filters |
|
70 | + add_action('pre_get_posts', ['EEH_Event_Query', 'filter_query_parts'], 10, 1); |
|
71 | + } |
|
72 | + |
|
73 | + |
|
74 | + /** |
|
75 | + * @param WP_Query $WP_Query |
|
76 | + * @return bool |
|
77 | + */ |
|
78 | + public static function apply_query_filters(WP_Query $WP_Query) |
|
79 | + { |
|
80 | + return ( |
|
81 | + isset($WP_Query->query['post_type']) |
|
82 | + && $WP_Query->query['post_type'] === 'espresso_events' |
|
83 | + ) |
|
84 | + || apply_filters('FHEE__EEH_Event_Query__apply_query_filters', false); |
|
85 | + } |
|
86 | + |
|
87 | + |
|
88 | + /** |
|
89 | + * @param WP_Query $WP_Query |
|
90 | + */ |
|
91 | + public static function filter_query_parts(WP_Query $WP_Query) |
|
92 | + { |
|
93 | + // ONLY add our filters if this isn't the main wp_query, |
|
94 | + // because if this is the main wp_query we already have |
|
95 | + // our cpt strategies take care of adding things in. |
|
96 | + if ($WP_Query instanceof WP_Query && ! $WP_Query->is_main_query()) { |
|
97 | + // build event list query |
|
98 | + add_filter('posts_fields', ['EEH_Event_Query', 'posts_fields'], 10, 2); |
|
99 | + add_filter('posts_join', ['EEH_Event_Query', 'posts_join'], 10, 2); |
|
100 | + add_filter('posts_where', ['EEH_Event_Query', 'posts_where'], 10, 2); |
|
101 | + add_filter('posts_orderby', ['EEH_Event_Query', 'posts_orderby'], 10, 2); |
|
102 | + add_filter('posts_clauses_request', ['EEH_Event_Query', 'posts_clauses'], 10, 2); |
|
103 | + } |
|
104 | + } |
|
105 | + |
|
106 | + |
|
107 | + /** |
|
108 | + * @param string $month |
|
109 | + * @param string $category |
|
110 | + * @param bool $show_expired |
|
111 | + * @param array|string $orderby |
|
112 | + * @param string $sort |
|
113 | + * @throws InvalidArgumentException |
|
114 | + * @throws InvalidDataTypeException |
|
115 | + * @throws InvalidInterfaceException |
|
116 | + */ |
|
117 | + public static function set_query_params( |
|
118 | + $month = '', |
|
119 | + $category = '', |
|
120 | + $show_expired = false, |
|
121 | + $orderby = 'start_date', |
|
122 | + $sort = 'ASC' |
|
123 | + ) { |
|
124 | + self::$_query_params = []; |
|
125 | + EEH_Event_Query::$_event_query_month = EEH_Event_Query::_display_month($month); |
|
126 | + EEH_Event_Query::$_event_query_category = EEH_Event_Query::_event_category_slug($category); |
|
127 | + EEH_Event_Query::$_event_query_show_expired = EEH_Event_Query::_show_expired($show_expired); |
|
128 | + EEH_Event_Query::$_event_query_orderby = EEH_Event_Query::_orderby($orderby); |
|
129 | + EEH_Event_Query::$_event_query_sort = EEH_Event_Query::_sort($sort); |
|
130 | + } |
|
131 | + |
|
132 | + |
|
133 | + /** |
|
134 | + * what month should the event list display events for? |
|
135 | + * |
|
136 | + * @param string $month |
|
137 | + * @return string |
|
138 | + * @throws InvalidArgumentException |
|
139 | + * @throws InvalidDataTypeException |
|
140 | + * @throws InvalidInterfaceException |
|
141 | + */ |
|
142 | + private static function _display_month($month = '') |
|
143 | + { |
|
144 | + return self::getRequest()->getRequestParam('event_query_month', $month); |
|
145 | + } |
|
146 | + |
|
147 | + |
|
148 | + /** |
|
149 | + * @param string $category |
|
150 | + * @return string |
|
151 | + * @throws InvalidArgumentException |
|
152 | + * @throws InvalidDataTypeException |
|
153 | + * @throws InvalidInterfaceException |
|
154 | + */ |
|
155 | + private static function _event_category_slug($category = '') |
|
156 | + { |
|
157 | + return self::getRequest()->getRequestParam('event_query_category', $category); |
|
158 | + } |
|
159 | + |
|
160 | + |
|
161 | + /** |
|
162 | + * @param bool $show_expired |
|
163 | + * @return bool |
|
164 | + * @throws InvalidArgumentException |
|
165 | + * @throws InvalidDataTypeException |
|
166 | + * @throws InvalidInterfaceException |
|
167 | + */ |
|
168 | + private static function _show_expired($show_expired = false) |
|
169 | + { |
|
170 | + // override default expired option if set via filter |
|
171 | + return self::getRequest()->getRequestParam('event_query_show_expired', $show_expired, 'bool'); |
|
172 | + } |
|
173 | + |
|
174 | + |
|
175 | + /** |
|
176 | + * @param array|string $orderby |
|
177 | + * @return array |
|
178 | + * @throws InvalidArgumentException |
|
179 | + * @throws InvalidDataTypeException |
|
180 | + * @throws InvalidInterfaceException |
|
181 | + */ |
|
182 | + private static function _orderby($orderby = 'start_date') |
|
183 | + { |
|
184 | + $event_query_orderby = self::getRequest()->getRequestParam( |
|
185 | + 'event_query_orderby', |
|
186 | + (array) $orderby, |
|
187 | + DataType::STRING, |
|
188 | + true |
|
189 | + ); |
|
190 | + $event_query_orderby = is_array($event_query_orderby) |
|
191 | + ? $event_query_orderby |
|
192 | + : explode(',', $event_query_orderby); |
|
193 | + $event_query_orderby = array_map('trim', $event_query_orderby); |
|
194 | + return array_map('sanitize_text_field', $event_query_orderby); |
|
195 | + } |
|
196 | + |
|
197 | + |
|
198 | + /** |
|
199 | + * @param string $sort |
|
200 | + * @return string |
|
201 | + * @throws InvalidArgumentException |
|
202 | + * @throws InvalidDataTypeException |
|
203 | + * @throws InvalidInterfaceException |
|
204 | + */ |
|
205 | + private static function _sort($sort = 'ASC') |
|
206 | + { |
|
207 | + $sort = self::getRequest()->getRequestParam('event_query_sort', $sort); |
|
208 | + return in_array($sort, ['ASC', 'asc', 'DESC', 'desc'], true) |
|
209 | + ? strtoupper($sort) |
|
210 | + : 'ASC'; |
|
211 | + } |
|
212 | + |
|
213 | + |
|
214 | + /** |
|
215 | + * Filters the clauses for the WP_Query object |
|
216 | + * |
|
217 | + * @param array $clauses array of clauses |
|
218 | + * @param WP_Query $wp_query |
|
219 | + * @return array array of clauses |
|
220 | + */ |
|
221 | + public static function posts_clauses($clauses, WP_Query $wp_query) |
|
222 | + { |
|
223 | + if (EEH_Event_Query::apply_query_filters($wp_query)) { |
|
224 | + global $wpdb; |
|
225 | + $clauses['groupby'] = $wpdb->posts . '.ID '; |
|
226 | + } |
|
227 | + return $clauses; |
|
228 | + } |
|
229 | + |
|
230 | + |
|
231 | + /** |
|
232 | + * @param string $SQL |
|
233 | + * @param WP_Query $wp_query |
|
234 | + * @return string |
|
235 | + * @throws EE_Error |
|
236 | + * @throws InvalidArgumentException |
|
237 | + * @throws InvalidDataTypeException |
|
238 | + * @throws InvalidInterfaceException |
|
239 | + */ |
|
240 | + public static function posts_fields($SQL, WP_Query $wp_query) |
|
241 | + { |
|
242 | + if (EEH_Event_Query::apply_query_filters($wp_query)) { |
|
243 | + // adds something like ", wp_esp_datetime.* " to WP Query SELECT statement |
|
244 | + $SQL .= EEH_Event_Query::posts_fields_sql_for_orderby(EEH_Event_Query::$_event_query_orderby); |
|
245 | + } |
|
246 | + return $SQL; |
|
247 | + } |
|
248 | + |
|
249 | + |
|
250 | + /** |
|
251 | + * @param array $orderby_params |
|
252 | + * @return string |
|
253 | + * @throws EE_Error |
|
254 | + * @throws InvalidArgumentException |
|
255 | + * @throws InvalidDataTypeException |
|
256 | + * @throws InvalidInterfaceException |
|
257 | + */ |
|
258 | + public static function posts_fields_sql_for_orderby(array $orderby_params = []) |
|
259 | + { |
|
260 | + $SQL = ', MIN( ' . EEM_Datetime::instance()->table() . '.DTT_EVT_start ) as event_start_date '; |
|
261 | + foreach ($orderby_params as $orderby) { |
|
262 | + switch ($orderby) { |
|
263 | + case 'ticket_start': |
|
264 | + $SQL .= ', ' . EEM_Ticket::instance()->table() . '.TKT_start_date'; |
|
265 | + break; |
|
266 | + case 'ticket_end': |
|
267 | + $SQL .= ', ' . EEM_Ticket::instance()->table() . '.TKT_end_date'; |
|
268 | + break; |
|
269 | + case 'venue_title': |
|
270 | + $SQL .= ', Venue.post_title AS venue_title'; |
|
271 | + break; |
|
272 | + case 'city': |
|
273 | + $SQL .= ', ' . EEM_Venue::instance()->second_table() . '.VNU_city'; |
|
274 | + break; |
|
275 | + case 'state': |
|
276 | + $SQL .= ', ' . EEM_State::instance()->table() . '.STA_name'; |
|
277 | + break; |
|
278 | + } |
|
279 | + } |
|
280 | + return $SQL; |
|
281 | + } |
|
282 | + |
|
283 | + |
|
284 | + /** |
|
285 | + * @param string $SQL |
|
286 | + * @param WP_Query $wp_query |
|
287 | + * @return string |
|
288 | + * @throws EE_Error |
|
289 | + * @throws InvalidArgumentException |
|
290 | + * @throws InvalidDataTypeException |
|
291 | + * @throws InvalidInterfaceException |
|
292 | + */ |
|
293 | + public static function posts_join($SQL, WP_Query $wp_query) |
|
294 | + { |
|
295 | + if (EEH_Event_Query::apply_query_filters($wp_query)) { |
|
296 | + // Category |
|
297 | + $SQL = EEH_Event_Query::posts_join_sql_for_show_expired($SQL, EEH_Event_Query::$_event_query_show_expired); |
|
298 | + $SQL = EEH_Event_Query::posts_join_sql_for_terms($SQL, EEH_Event_Query::$_event_query_category); |
|
299 | + $SQL = EEH_Event_Query::posts_join_for_orderby($SQL, EEH_Event_Query::$_event_query_orderby); |
|
300 | + } |
|
301 | + return $SQL; |
|
302 | + } |
|
303 | + |
|
304 | + |
|
305 | + /** |
|
306 | + * @param string $SQL |
|
307 | + * @param boolean $show_expired if TRUE, then displayed past events |
|
308 | + * @return string |
|
309 | + * @throws EE_Error |
|
310 | + * @throws InvalidArgumentException |
|
311 | + * @throws InvalidDataTypeException |
|
312 | + * @throws InvalidInterfaceException |
|
313 | + */ |
|
314 | + public static function posts_join_sql_for_show_expired($SQL = '', $show_expired = false) |
|
315 | + { |
|
316 | + if (! $show_expired) { |
|
317 | + $join = EEM_Event::instance()->table() . '.ID = '; |
|
318 | + $join .= EEM_Datetime::instance()->table() . '.' . EEM_Event::instance()->primary_key_name(); |
|
319 | + // don't add if this is already in the SQL |
|
320 | + if (strpos($SQL, $join) === false) { |
|
321 | + $SQL .= ' INNER JOIN ' . EEM_Datetime::instance()->table() . ' ON ( ' . $join . ' ) '; |
|
322 | + } |
|
323 | + } |
|
324 | + return $SQL; |
|
325 | + } |
|
326 | + |
|
327 | + |
|
328 | + /** |
|
329 | + * @param string $SQL |
|
330 | + * @param string $join_terms pass TRUE or term string, doesn't really matter since this value doesn't really get |
|
331 | + * used for anything yet |
|
332 | + * @return string |
|
333 | + */ |
|
334 | + public static function posts_join_sql_for_terms($SQL = '', $join_terms = '') |
|
335 | + { |
|
336 | + if (! empty($join_terms)) { |
|
337 | + global $wpdb; |
|
338 | + $SQL .= " LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)"; |
|
339 | + $SQL .= " LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)"; |
|
340 | + $SQL .= " LEFT JOIN $wpdb->terms ON ($wpdb->terms.term_id = $wpdb->term_taxonomy.term_id) "; |
|
341 | + } |
|
342 | + return $SQL; |
|
343 | + } |
|
344 | + |
|
345 | + |
|
346 | + /** |
|
347 | + * usage: $SQL .= EEH_Event_Query::posts_join_for_orderby( $orderby_params ); |
|
348 | + * |
|
349 | + * @param string $SQL |
|
350 | + * @param array $orderby_params |
|
351 | + * @return string |
|
352 | + * @throws EE_Error |
|
353 | + * @throws InvalidArgumentException |
|
354 | + * @throws InvalidDataTypeException |
|
355 | + * @throws InvalidInterfaceException |
|
356 | + */ |
|
357 | + public static function posts_join_for_orderby($SQL = '', array $orderby_params = []) |
|
358 | + { |
|
359 | + foreach ($orderby_params as $orderby) { |
|
360 | + switch ($orderby) { |
|
361 | + case 'ticket_start': |
|
362 | + case 'ticket_end': |
|
363 | + $SQL .= EEH_Event_Query::_posts_join_for_datetime( |
|
364 | + $SQL, |
|
365 | + EEM_Datetime_Ticket::instance()->table() . '.' . EEM_Datetime::instance()->primary_key_name() |
|
366 | + ); |
|
367 | + $SQL .= ' LEFT JOIN ' . EEM_Ticket::instance()->table(); |
|
368 | + $SQL .= ' ON ('; |
|
369 | + $SQL .= EEM_Datetime_Ticket::instance()->table() . '.' . EEM_Ticket::instance()->primary_key_name(); |
|
370 | + $SQL .= ' = '; |
|
371 | + $SQL .= EEM_Ticket::instance()->table() . '.' . EEM_Ticket::instance()->primary_key_name(); |
|
372 | + $SQL .= ' )'; |
|
373 | + break; |
|
374 | + case 'venue_title': |
|
375 | + case 'city': |
|
376 | + $SQL .= EEH_Event_Query::_posts_join_for_event_venue($SQL); |
|
377 | + break; |
|
378 | + case 'state': |
|
379 | + $SQL .= EEH_Event_Query::_posts_join_for_event_venue($SQL); |
|
380 | + $SQL .= EEH_Event_Query::_posts_join_for_venue_state($SQL); |
|
381 | + break; |
|
382 | + case 'start_date': |
|
383 | + default: |
|
384 | + $SQL .= EEH_Event_Query::_posts_join_for_datetime($SQL, EEM_Event::instance()->table() . '.ID'); |
|
385 | + break; |
|
386 | + } |
|
387 | + } |
|
388 | + return $SQL; |
|
389 | + } |
|
390 | + |
|
391 | + |
|
392 | + /** |
|
393 | + * @param string $SQL |
|
394 | + * @param string $join |
|
395 | + * @return string |
|
396 | + * @throws EE_Error |
|
397 | + * @throws InvalidArgumentException |
|
398 | + * @throws InvalidDataTypeException |
|
399 | + * @throws InvalidInterfaceException |
|
400 | + */ |
|
401 | + protected static function _posts_join_for_datetime($SQL = '', $join = '') |
|
402 | + { |
|
403 | + if (! empty($join)) { |
|
404 | + $join .= ' = ' . EEM_Datetime::instance()->table() . '.' . EEM_Event::instance()->primary_key_name(); |
|
405 | + if (strpos($SQL, $join) === false) { |
|
406 | + return ' INNER JOIN ' . EEM_Datetime::instance()->table() . ' ON ( ' . $join . ' )'; |
|
407 | + } |
|
408 | + } |
|
409 | + return ''; |
|
410 | + } |
|
411 | + |
|
412 | + |
|
413 | + /** |
|
414 | + * @param string $SQL |
|
415 | + * @return string |
|
416 | + * @throws EE_Error |
|
417 | + * @throws InvalidArgumentException |
|
418 | + * @throws InvalidDataTypeException |
|
419 | + * @throws InvalidInterfaceException |
|
420 | + */ |
|
421 | + protected static function _posts_join_for_event_venue($SQL = '') |
|
422 | + { |
|
423 | + // Event Venue table name |
|
424 | + $event_venue_table = EEM_Event_Venue::instance()->table(); |
|
425 | + // generate conditions for: Event <=> Event Venue JOIN clause |
|
426 | + $event_to_event_venue_join = EEM_Event::instance()->table() . '.ID = '; |
|
427 | + $event_to_event_venue_join .= $event_venue_table . '.' . EEM_Event::instance()->primary_key_name(); |
|
428 | + // don't add joins if they have already been added |
|
429 | + if (strpos($SQL, $event_to_event_venue_join) === false) { |
|
430 | + // Venue table name |
|
431 | + $venue_table = EEM_Venue::instance()->table(); |
|
432 | + // Venue table pk |
|
433 | + $venue_table_pk = EEM_Venue::instance()->primary_key_name(); |
|
434 | + // Venue Meta table name |
|
435 | + $venue_meta_table = EEM_Venue::instance()->second_table(); |
|
436 | + // generate JOIN clause for: Event <=> Event Venue |
|
437 | + $venue_SQL = " LEFT JOIN $event_venue_table ON ( $event_to_event_venue_join )"; |
|
438 | + // generate JOIN clause for: Event Venue <=> Venue |
|
439 | + $venue_SQL .= " LEFT JOIN $venue_table as Venue ON ( $event_venue_table.$venue_table_pk = Venue.ID )"; |
|
440 | + // generate JOIN clause for: Venue <=> Venue Meta |
|
441 | + $venue_SQL .= " LEFT JOIN $venue_meta_table ON ( Venue.ID = $venue_meta_table.$venue_table_pk )"; |
|
442 | + unset($event_venue_table, $event_to_event_venue_join, $venue_table, $venue_table_pk, $venue_meta_table); |
|
443 | + return $venue_SQL; |
|
444 | + } |
|
445 | + unset($event_venue_table, $event_to_event_venue_join); |
|
446 | + return ''; |
|
447 | + } |
|
448 | + |
|
449 | + |
|
450 | + /** |
|
451 | + * @param string $SQL |
|
452 | + * @return string |
|
453 | + * @throws EE_Error |
|
454 | + * @throws InvalidArgumentException |
|
455 | + * @throws InvalidDataTypeException |
|
456 | + * @throws InvalidInterfaceException |
|
457 | + */ |
|
458 | + protected static function _posts_join_for_venue_state($SQL = '') |
|
459 | + { |
|
460 | + // Venue Meta table name |
|
461 | + $venue_meta_table = EEM_Venue::instance()->second_table(); |
|
462 | + // State table name |
|
463 | + $state_table = EEM_State::instance()->table(); |
|
464 | + // State table pk |
|
465 | + $state_table_pk = EEM_State::instance()->primary_key_name(); |
|
466 | + // verify vars |
|
467 | + if ($venue_meta_table && $state_table && $state_table_pk) { |
|
468 | + // like: wp_esp_venue_meta.STA_ID = wp_esp_state.STA_ID |
|
469 | + $join = "$venue_meta_table.$state_table_pk = $state_table.$state_table_pk"; |
|
470 | + // don't add join if it has already been added |
|
471 | + if (strpos($SQL, $join) === false) { |
|
472 | + unset($state_table_pk, $venue_meta_table, $venue_table_pk); |
|
473 | + return " LEFT JOIN $state_table ON ( $join )"; |
|
474 | + } |
|
475 | + } |
|
476 | + unset($join, $state_table, $state_table_pk, $venue_meta_table, $venue_table_pk); |
|
477 | + return ''; |
|
478 | + } |
|
479 | + |
|
480 | + |
|
481 | + /** |
|
482 | + * @param string $SQL |
|
483 | + * @param WP_Query $wp_query |
|
484 | + * @return string |
|
485 | + * @throws EE_Error |
|
486 | + * @throws InvalidArgumentException |
|
487 | + * @throws InvalidDataTypeException |
|
488 | + * @throws InvalidInterfaceException |
|
489 | + */ |
|
490 | + public static function posts_where($SQL, WP_Query $wp_query) |
|
491 | + { |
|
492 | + if (EEH_Event_Query::apply_query_filters($wp_query)) { |
|
493 | + // Show Expired ? |
|
494 | + $SQL .= EEH_Event_Query::posts_where_sql_for_show_expired(EEH_Event_Query::$_event_query_show_expired); |
|
495 | + // Category |
|
496 | + $SQL .= EEH_Event_Query::posts_where_sql_for_event_category_slug(EEH_Event_Query::$_event_query_category); |
|
497 | + // Start Date |
|
498 | + $SQL .= EEH_Event_Query::posts_where_sql_for_event_list_month(EEH_Event_Query::$_event_query_month); |
|
499 | + } |
|
500 | + return $SQL; |
|
501 | + } |
|
502 | + |
|
503 | + |
|
504 | + /** |
|
505 | + * @param boolean $show_expired if TRUE, then displayed past events |
|
506 | + * @return string |
|
507 | + * @throws EE_Error |
|
508 | + * @throws InvalidArgumentException |
|
509 | + * @throws InvalidDataTypeException |
|
510 | + * @throws InvalidInterfaceException |
|
511 | + */ |
|
512 | + public static function posts_where_sql_for_show_expired($show_expired = false) |
|
513 | + { |
|
514 | + return ! $show_expired |
|
515 | + ? ' AND ' . EEM_Datetime::instance()->table() . '.DTT_EVT_end > \'' . current_time('mysql', true) . '\' ' |
|
516 | + : ''; |
|
517 | + } |
|
518 | + |
|
519 | + |
|
520 | + /** |
|
521 | + * @param boolean $event_category_slug |
|
522 | + * @return string |
|
523 | + */ |
|
524 | + public static function posts_where_sql_for_event_category_slug($event_category_slug = null) |
|
525 | + { |
|
526 | + global $wpdb; |
|
527 | + if (! empty($event_category_slug)) { |
|
528 | + $event_category_slugs_array = array_map('trim', explode(',', $event_category_slug)); |
|
529 | + $event_category_slugs_prepare = implode(', ', array_fill(0, count($event_category_slugs_array), '%s')); |
|
530 | + return $wpdb->prepare( |
|
531 | + " AND {$wpdb->terms}.slug IN ({$event_category_slugs_prepare}) ", |
|
532 | + $event_category_slugs_array |
|
533 | + ); |
|
534 | + } |
|
535 | + return ''; |
|
536 | + } |
|
537 | + |
|
538 | + |
|
539 | + /** |
|
540 | + * @param boolean $month |
|
541 | + * @return string |
|
542 | + * @throws EE_Error |
|
543 | + * @throws InvalidArgumentException |
|
544 | + * @throws InvalidDataTypeException |
|
545 | + * @throws InvalidInterfaceException |
|
546 | + */ |
|
547 | + public static function posts_where_sql_for_event_list_month($month = null) |
|
548 | + { |
|
549 | + $SQL = ''; |
|
550 | + if (! empty($month)) { |
|
551 | + $datetime_table = EEM_Datetime::instance()->table(); |
|
552 | + // event start date is LESS than the end of the month ( so nothing that doesn't start until next month ) |
|
553 | + $SQL = " AND {$datetime_table}.DTT_EVT_start <= '"; |
|
554 | + $SQL .= date('Y-m-t 23:59:59', EEH_DTT_Helper::first_of_month_timestamp($month)) . "'"; |
|
555 | + // event end date is GREATER than the start of the month ( so nothing that ended before this month ) |
|
556 | + $SQL .= " AND {$datetime_table}.DTT_EVT_end >= '"; |
|
557 | + $SQL .= date('Y-m-01 0:0:00', EEH_DTT_Helper::first_of_month_timestamp($month)) . "' "; |
|
558 | + } |
|
559 | + return $SQL; |
|
560 | + } |
|
561 | + |
|
562 | + |
|
563 | + /** |
|
564 | + * @param string $SQL |
|
565 | + * @param WP_Query $wp_query |
|
566 | + * @return string |
|
567 | + * @throws EE_Error |
|
568 | + * @throws InvalidArgumentException |
|
569 | + * @throws InvalidDataTypeException |
|
570 | + * @throws InvalidInterfaceException |
|
571 | + */ |
|
572 | + public static function posts_orderby($SQL, WP_Query $wp_query) |
|
573 | + { |
|
574 | + if (EEH_Event_Query::apply_query_filters($wp_query)) { |
|
575 | + $SQL = EEH_Event_Query::posts_orderby_sql( |
|
576 | + EEH_Event_Query::$_event_query_orderby, |
|
577 | + EEH_Event_Query::$_event_query_sort |
|
578 | + ); |
|
579 | + } |
|
580 | + return $SQL; |
|
581 | + } |
|
582 | + |
|
583 | + |
|
584 | + /** |
|
585 | + * posts_orderby_sql |
|
586 | + * possible parameters: |
|
587 | + * ID |
|
588 | + * start_date |
|
589 | + * end_date |
|
590 | + * event_name |
|
591 | + * category_slug |
|
592 | + * ticket_start |
|
593 | + * ticket_end |
|
594 | + * venue_title |
|
595 | + * city |
|
596 | + * state |
|
597 | + * **IMPORTANT** |
|
598 | + * make sure to also send the $orderby_params array to the posts_join_for_orderby() method |
|
599 | + * or else some of the table references below will result in MySQL errors |
|
600 | + * |
|
601 | + * @param array $orderby_params |
|
602 | + * @param string $sort |
|
603 | + * @return string |
|
604 | + * @throws EE_Error |
|
605 | + * @throws InvalidArgumentException |
|
606 | + * @throws InvalidDataTypeException |
|
607 | + * @throws InvalidInterfaceException |
|
608 | + */ |
|
609 | + public static function posts_orderby_sql(array $orderby_params = [], $sort = 'ASC') |
|
610 | + { |
|
611 | + global $wpdb; |
|
612 | + $SQL = ''; |
|
613 | + $counter = 0; |
|
614 | + $sort = in_array($sort, ['ASC', 'asc', 'DESC', 'desc'], true) |
|
615 | + ? strtoupper($sort) |
|
616 | + : 'ASC'; |
|
617 | + // make sure 'orderby' is set in query params |
|
618 | + if (! isset(self::$_query_params['orderby'])) { |
|
619 | + self::$_query_params['orderby'] = []; |
|
620 | + } |
|
621 | + // loop thru $orderby_params (type cast as array) |
|
622 | + foreach ($orderby_params as $orderby) { |
|
623 | + // check if we have already added this param |
|
624 | + if (isset(self::$_query_params['orderby'][ $orderby ])) { |
|
625 | + // if so then remove from the $orderby_params so that the count() method below is accurate |
|
626 | + unset($orderby_params[ $orderby ]); |
|
627 | + // then bump ahead to the next param |
|
628 | + continue; |
|
629 | + } |
|
630 | + // this will ad a comma depending on whether this is the first or last param |
|
631 | + $glue = $counter === 0 || $counter === count($orderby_params) ? ' ' : ', '; |
|
632 | + // ok what's we dealing with? |
|
633 | + switch ($orderby) { |
|
634 | + case 'id': |
|
635 | + case 'ID': |
|
636 | + $SQL .= $glue . $wpdb->posts . '.ID ' . $sort; |
|
637 | + break; |
|
638 | + case 'end_date': |
|
639 | + $SQL .= $glue . EEM_Datetime::instance()->table() . '.DTT_EVT_end ' . $sort; |
|
640 | + break; |
|
641 | + case 'event_name': |
|
642 | + $SQL .= $glue . $wpdb->posts . '.post_title ' . $sort; |
|
643 | + break; |
|
644 | + case 'category_slug': |
|
645 | + $SQL .= $glue . $wpdb->terms . '.slug ' . $sort; |
|
646 | + break; |
|
647 | + case 'ticket_start': |
|
648 | + $SQL .= $glue . EEM_Ticket::instance()->table() . '.TKT_start_date ' . $sort; |
|
649 | + break; |
|
650 | + case 'ticket_end': |
|
651 | + $SQL .= $glue . EEM_Ticket::instance()->table() . '.TKT_end_date ' . $sort; |
|
652 | + break; |
|
653 | + case 'venue_title': |
|
654 | + $SQL .= $glue . 'venue_title ' . $sort; |
|
655 | + break; |
|
656 | + case 'city': |
|
657 | + $SQL .= $glue . EEM_Venue::instance()->second_table() . '.VNU_city ' . $sort; |
|
658 | + break; |
|
659 | + case 'state': |
|
660 | + $SQL .= $glue . EEM_State::instance()->table() . '.STA_name ' . $sort; |
|
661 | + break; |
|
662 | + case 'start_date': |
|
663 | + default: |
|
664 | + $SQL .= $glue . ' event_start_date ' . $sort; |
|
665 | + break; |
|
666 | + } |
|
667 | + // add to array of orderby params that have been added |
|
668 | + self::$_query_params['orderby'][ $orderby ] = true; |
|
669 | + $counter++; |
|
670 | + } |
|
671 | + return $SQL; |
|
672 | + } |
|
673 | + |
|
674 | + |
|
675 | + /** |
|
676 | + * @return RequestInterface |
|
677 | + * @since 4.10.14.p |
|
678 | + */ |
|
679 | + private static function getRequest() |
|
680 | + { |
|
681 | + return LoaderFactory::getLoader()->getShared(RequestInterface::class); |
|
682 | + } |
|
683 | 683 | } |