Completed
Branch master (19a9be)
by
unknown
08:32
created
core/EE_Load_Textdomain.core.php 2 patches
Indentation   +91 added lines, -91 removed lines patch added patch discarded remove patch
@@ -13,105 +13,105 @@
 block discarded – undo
13 13
 class EE_Load_Textdomain extends EE_Base
14 14
 {
15 15
 
16
-    /**
17
-     * holds the current lang in WP
18
-     *
19
-     * @var string
20
-     */
21
-    private static $locale;
16
+	/**
17
+	 * holds the current lang in WP
18
+	 *
19
+	 * @var string
20
+	 */
21
+	private static $locale;
22 22
 
23 23
 
24
-    /**
25
-     * this takes care of retrieving a matching textdomain for event espresso for the current WPLANG from EE GitHub
26
-     * repo (if necessary) and then loading it for translations. should only be called in wp plugins_loaded callback
27
-     *
28
-     * @return void
29
-     * @throws EE_Error
30
-     * @throws InvalidArgumentException
31
-     * @throws ReflectionException
32
-     * @throws InvalidDataTypeException
33
-     * @throws InvalidInterfaceException
34
-     */
35
-    public static function load_textdomain()
36
-    {
37
-        EE_Load_Textdomain::loadTranslationsForLocale();
38
-        // now load the textdomain
39
-        if (!empty(EE_Load_Textdomain::$locale)) {
40
-            $github_mo_path = EE_LANGUAGES_SAFE_DIR . 'event_espresso-' . EE_Load_Textdomain::$locale . '.mo';
41
-            if (is_readable($github_mo_path)) {
42
-                load_plugin_textdomain('event_espresso', false, EE_LANGUAGES_SAFE_LOC);
43
-                return;
44
-            }
45
-            $glotpress_mo_path = EE_LANGUAGES_SAFE_DIR . 'event-espresso-4-' . EE_Load_Textdomain::$locale . '.mo';
46
-            if (is_readable($glotpress_mo_path)) {
47
-                load_textdomain('event_espresso', $glotpress_mo_path);
48
-                return;
49
-            }
50
-        }
51
-        load_plugin_textdomain('event_espresso', false, dirname(EE_PLUGIN_BASENAME) . '/languages/');
52
-    }
24
+	/**
25
+	 * this takes care of retrieving a matching textdomain for event espresso for the current WPLANG from EE GitHub
26
+	 * repo (if necessary) and then loading it for translations. should only be called in wp plugins_loaded callback
27
+	 *
28
+	 * @return void
29
+	 * @throws EE_Error
30
+	 * @throws InvalidArgumentException
31
+	 * @throws ReflectionException
32
+	 * @throws InvalidDataTypeException
33
+	 * @throws InvalidInterfaceException
34
+	 */
35
+	public static function load_textdomain()
36
+	{
37
+		EE_Load_Textdomain::loadTranslationsForLocale();
38
+		// now load the textdomain
39
+		if (!empty(EE_Load_Textdomain::$locale)) {
40
+			$github_mo_path = EE_LANGUAGES_SAFE_DIR . 'event_espresso-' . EE_Load_Textdomain::$locale . '.mo';
41
+			if (is_readable($github_mo_path)) {
42
+				load_plugin_textdomain('event_espresso', false, EE_LANGUAGES_SAFE_LOC);
43
+				return;
44
+			}
45
+			$glotpress_mo_path = EE_LANGUAGES_SAFE_DIR . 'event-espresso-4-' . EE_Load_Textdomain::$locale . '.mo';
46
+			if (is_readable($glotpress_mo_path)) {
47
+				load_textdomain('event_espresso', $glotpress_mo_path);
48
+				return;
49
+			}
50
+		}
51
+		load_plugin_textdomain('event_espresso', false, dirname(EE_PLUGIN_BASENAME) . '/languages/');
52
+	}
53 53
 
54 54
 
55
-    /**
56
-     * The purpose of this method is to sideload all of the lang files for EE, this includes the POT file and also the PO/MO files for the given WPLANG locale (if necessary).
57
-     *
58
-     * @access private
59
-     * @static
60
-     * @return void
61
-     * @throws EE_Error
62
-     * @throws InvalidArgumentException
63
-     * @throws ReflectionException
64
-     * @throws InvalidDataTypeException
65
-     * @throws InvalidInterfaceException
66
-     */
67
-    private static function loadTranslationsForLocale()
68
-    {
69
-        EE_Load_Textdomain::$locale = get_locale();
70
-        // can't download a language file if a language isn't set <taps temple>
71
-        if (empty(EE_Load_Textdomain::$locale)) {
72
-            return;
73
-        }
74
-        $language_check_option_name = 'ee_lang_check_' . EE_Load_Textdomain::$locale . '_' . EVENT_ESPRESSO_VERSION;
75
-        // check if language files has already been sideloaded
76
-        if (get_option($language_check_option_name)) {
77
-            return;
78
-        }
55
+	/**
56
+	 * The purpose of this method is to sideload all of the lang files for EE, this includes the POT file and also the PO/MO files for the given WPLANG locale (if necessary).
57
+	 *
58
+	 * @access private
59
+	 * @static
60
+	 * @return void
61
+	 * @throws EE_Error
62
+	 * @throws InvalidArgumentException
63
+	 * @throws ReflectionException
64
+	 * @throws InvalidDataTypeException
65
+	 * @throws InvalidInterfaceException
66
+	 */
67
+	private static function loadTranslationsForLocale()
68
+	{
69
+		EE_Load_Textdomain::$locale = get_locale();
70
+		// can't download a language file if a language isn't set <taps temple>
71
+		if (empty(EE_Load_Textdomain::$locale)) {
72
+			return;
73
+		}
74
+		$language_check_option_name = 'ee_lang_check_' . EE_Load_Textdomain::$locale . '_' . EVENT_ESPRESSO_VERSION;
75
+		// check if language files has already been sideloaded
76
+		if (get_option($language_check_option_name)) {
77
+			return;
78
+		}
79 79
 
80
-        $repo_base_URL = 'https://github.com/eventespresso/languages-ee4/blob/master/event_espresso';
80
+		$repo_base_URL = 'https://github.com/eventespresso/languages-ee4/blob/master/event_espresso';
81 81
 
82
-        // load sideloader and sideload the .POT file as this should always be included.
83
-        $sideloader_args = array(
84
-            '_upload_to'     => EE_PLUGIN_DIR_PATH . 'languages/',
85
-            '_download_from'   => $repo_base_URL .'.pot?raw=true',
86
-            '_new_file_name' => 'event_espresso.pot',
87
-        );
88
-        /** @var EEH_Sideloader $sideloader */
89
-        $sideloader = EE_Registry::instance()->load_helper('Sideloader', $sideloader_args, false);
90
-        // sideload the .POT file only for main site of the network, or if not running Multisite.
91
-        if (is_main_site()) {
92
-            $sideloader->sideload();
93
-        }
82
+		// load sideloader and sideload the .POT file as this should always be included.
83
+		$sideloader_args = array(
84
+			'_upload_to'     => EE_PLUGIN_DIR_PATH . 'languages/',
85
+			'_download_from'   => $repo_base_URL .'.pot?raw=true',
86
+			'_new_file_name' => 'event_espresso.pot',
87
+		);
88
+		/** @var EEH_Sideloader $sideloader */
89
+		$sideloader = EE_Registry::instance()->load_helper('Sideloader', $sideloader_args, false);
90
+		// sideload the .POT file only for main site of the network, or if not running Multisite.
91
+		if (is_main_site()) {
92
+			$sideloader->sideload();
93
+		}
94 94
 
95
-        // if locale is "en_US" then lets just get out, since Event Espresso core is already "en_US"
96
-        if (EE_Load_Textdomain::$locale === 'en_US') {
97
-            // but set option first else we'll forever be downloading the pot file
98
-            update_option($language_check_option_name, 1);
99
-            return;
100
-        }
101
-        $repo_locale_URL = $repo_base_URL . '-' . EE_Load_Textdomain::$locale;
102
-        $file_name_base = 'event_espresso-' . EE_Load_Textdomain::$locale;
95
+		// if locale is "en_US" then lets just get out, since Event Espresso core is already "en_US"
96
+		if (EE_Load_Textdomain::$locale === 'en_US') {
97
+			// but set option first else we'll forever be downloading the pot file
98
+			update_option($language_check_option_name, 1);
99
+			return;
100
+		}
101
+		$repo_locale_URL = $repo_base_URL . '-' . EE_Load_Textdomain::$locale;
102
+		$file_name_base = 'event_espresso-' . EE_Load_Textdomain::$locale;
103 103
 
104
-        // made it here so let's get the language files from the github repo, first the .mo file
105
-        $sideloader->set_download_from("{$repo_locale_URL}.mo?raw=true");
106
-        $sideloader->set_new_file_name("{$file_name_base}.mo");
107
-        $sideloader->sideload();
104
+		// made it here so let's get the language files from the github repo, first the .mo file
105
+		$sideloader->set_download_from("{$repo_locale_URL}.mo?raw=true");
106
+		$sideloader->set_new_file_name("{$file_name_base}.mo");
107
+		$sideloader->sideload();
108 108
 
109
-        // now the .po file:
110
-        $sideloader->set_download_from("{$repo_locale_URL}.po?raw=true");
111
-        $sideloader->set_new_file_name("{$file_name_base}.po");
112
-        $sideloader->sideload();
109
+		// now the .po file:
110
+		$sideloader->set_download_from("{$repo_locale_URL}.po?raw=true");
111
+		$sideloader->set_new_file_name("{$file_name_base}.po");
112
+		$sideloader->sideload();
113 113
 
114
-        // set option so the above only runs when EE updates.
115
-        update_option($language_check_option_name, 1);
116
-    }
114
+		// set option so the above only runs when EE updates.
115
+		update_option($language_check_option_name, 1);
116
+	}
117 117
 }
Please login to merge, or discard this patch.
Spacing   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -36,19 +36,19 @@  discard block
 block discarded – undo
36 36
     {
37 37
         EE_Load_Textdomain::loadTranslationsForLocale();
38 38
         // now load the textdomain
39
-        if (!empty(EE_Load_Textdomain::$locale)) {
40
-            $github_mo_path = EE_LANGUAGES_SAFE_DIR . 'event_espresso-' . EE_Load_Textdomain::$locale . '.mo';
39
+        if ( ! empty(EE_Load_Textdomain::$locale)) {
40
+            $github_mo_path = EE_LANGUAGES_SAFE_DIR.'event_espresso-'.EE_Load_Textdomain::$locale.'.mo';
41 41
             if (is_readable($github_mo_path)) {
42 42
                 load_plugin_textdomain('event_espresso', false, EE_LANGUAGES_SAFE_LOC);
43 43
                 return;
44 44
             }
45
-            $glotpress_mo_path = EE_LANGUAGES_SAFE_DIR . 'event-espresso-4-' . EE_Load_Textdomain::$locale . '.mo';
45
+            $glotpress_mo_path = EE_LANGUAGES_SAFE_DIR.'event-espresso-4-'.EE_Load_Textdomain::$locale.'.mo';
46 46
             if (is_readable($glotpress_mo_path)) {
47 47
                 load_textdomain('event_espresso', $glotpress_mo_path);
48 48
                 return;
49 49
             }
50 50
         }
51
-        load_plugin_textdomain('event_espresso', false, dirname(EE_PLUGIN_BASENAME) . '/languages/');
51
+        load_plugin_textdomain('event_espresso', false, dirname(EE_PLUGIN_BASENAME).'/languages/');
52 52
     }
53 53
 
54 54
 
@@ -71,7 +71,7 @@  discard block
 block discarded – undo
71 71
         if (empty(EE_Load_Textdomain::$locale)) {
72 72
             return;
73 73
         }
74
-        $language_check_option_name = 'ee_lang_check_' . EE_Load_Textdomain::$locale . '_' . EVENT_ESPRESSO_VERSION;
74
+        $language_check_option_name = 'ee_lang_check_'.EE_Load_Textdomain::$locale.'_'.EVENT_ESPRESSO_VERSION;
75 75
         // check if language files has already been sideloaded
76 76
         if (get_option($language_check_option_name)) {
77 77
             return;
@@ -81,8 +81,8 @@  discard block
 block discarded – undo
81 81
 
82 82
         // load sideloader and sideload the .POT file as this should always be included.
83 83
         $sideloader_args = array(
84
-            '_upload_to'     => EE_PLUGIN_DIR_PATH . 'languages/',
85
-            '_download_from'   => $repo_base_URL .'.pot?raw=true',
84
+            '_upload_to'     => EE_PLUGIN_DIR_PATH.'languages/',
85
+            '_download_from'   => $repo_base_URL.'.pot?raw=true',
86 86
             '_new_file_name' => 'event_espresso.pot',
87 87
         );
88 88
         /** @var EEH_Sideloader $sideloader */
@@ -98,8 +98,8 @@  discard block
 block discarded – undo
98 98
             update_option($language_check_option_name, 1);
99 99
             return;
100 100
         }
101
-        $repo_locale_URL = $repo_base_URL . '-' . EE_Load_Textdomain::$locale;
102
-        $file_name_base = 'event_espresso-' . EE_Load_Textdomain::$locale;
101
+        $repo_locale_URL = $repo_base_URL.'-'.EE_Load_Textdomain::$locale;
102
+        $file_name_base = 'event_espresso-'.EE_Load_Textdomain::$locale;
103 103
 
104 104
         // made it here so let's get the language files from the github repo, first the .mo file
105 105
         $sideloader->set_download_from("{$repo_locale_URL}.mo?raw=true");
Please login to merge, or discard this patch.
core/libraries/messages/EE_Messages_Scheduler.lib.php 2 patches
Indentation   +206 added lines, -206 removed lines patch added patch discarded remove patch
@@ -11,210 +11,210 @@
 block discarded – undo
11 11
 class EE_Messages_Scheduler extends EE_Base
12 12
 {
13 13
 
14
-    /**
15
-     * Number of seconds between batch sends/generates on the cron job.
16
-     * Defaults to 5 minutes in seconds.  If you want to change this interval, you can use the native WordPress
17
-     * `cron_schedules` filter and modify the existing custom `ee_message_cron` schedule interval added.
18
-     *
19
-     * @type int
20
-     */
21
-    const message_cron_schedule = 300;
22
-
23
-    /**
24
-     * Constructor
25
-     */
26
-    public function __construct()
27
-    {
28
-        // register tasks (and make sure only registered once).
29
-        if (! has_action('FHEE__EEH_Activation__get_cron_tasks', array($this, 'register_scheduled_tasks'))) {
30
-            add_action('FHEE__EEH_Activation__get_cron_tasks', array($this, 'register_scheduled_tasks'), 10);
31
-        }
32
-
33
-        // register callbacks for scheduled events (but make sure they are set only once).
34
-        if (! has_action(
35
-            'AHEE__EE_Messages_Scheduler__generation',
36
-            array('EE_Messages_Scheduler', 'batch_generation')
37
-        )) {
38
-            add_action('AHEE__EE_Messages_Scheduler__generation', array('EE_Messages_Scheduler', 'batch_generation'));
39
-            add_action('AHEE__EE_Messages_Scheduler__sending', array('EE_Messages_Scheduler', 'batch_sending'));
40
-            add_action('AHEE__EE_Messages_Scheduler__cleanup', array('EE_Messages_Scheduler', 'cleanup'));
41
-        }
42
-
43
-        // add custom schedules
44
-        add_filter('cron_schedules', array($this, 'custom_schedules'));
45
-    }
46
-
47
-
48
-    /**
49
-     * Add custom schedules for wp_cron
50
-     *
51
-     * @param $schedules
52
-     */
53
-    public function custom_schedules($schedules)
54
-    {
55
-        $schedules['ee_message_cron'] = array(
56
-            'interval' => self::message_cron_schedule,
57
-            'display'  => __(
58
-                'This is the cron time interval for EE Message schedules (defaults to once every 5 minutes)',
59
-                'event_espresso'
60
-            ),
61
-        );
62
-        return $schedules;
63
-    }
64
-
65
-
66
-    /**
67
-     * Callback for FHEE__EEH_Activation__get_cron_tasks that is used to retrieve scheduled Cron events to add and
68
-     * remove.
69
-     *
70
-     * @param array $tasks already existing scheduled tasks
71
-     * @return array
72
-     */
73
-    public function register_scheduled_tasks($tasks)
74
-    {
75
-        EE_Registry::instance()->load_helper('DTT_Helper');
76
-        $tasks['AHEE__EE_Messages_Scheduler__generation'] = 'ee_message_cron';
77
-        $tasks['AHEE__EE_Messages_Scheduler__sending']    = 'ee_message_cron';
78
-        $tasks['AHEE__EE_Messages_Scheduler__cleanup'] = array( EEH_DTT_Helper::tomorrow(), 'daily');
79
-        return $tasks;
80
-    }
81
-
82
-
83
-    /**
84
-     * This initiates a non-blocking separate request to execute on a scheduled task.
85
-     * Note: The EED_Messages module has the handlers for these requests.
86
-     *
87
-     * @param string $task The task the request is being generated for.
88
-     */
89
-    public static function initiate_scheduled_non_blocking_request($task)
90
-    {
91
-        if (apply_filters(
92
-            'EE_Messages_Scheduler__initiate_scheduled_non_blocking_request__do_separate_request',
93
-            true
94
-        )) {
95
-            $request_url  = add_query_arg(
96
-                array_merge(
97
-                    array('ee' => 'msg_cron_trigger'),
98
-                    EE_Messages_Scheduler::get_request_params($task)
99
-                ),
100
-                site_url()
101
-            );
102
-            $request_args = array(
103
-                'timeout'     => 300,
104
-                'blocking'    => (defined('DOING_CRON') && DOING_CRON) || (defined('DOING_AJAX') && DOING_AJAX) ? true : false,
105
-                'sslverify'   => false,
106
-                'redirection' => 10,
107
-            );
108
-            $response     = wp_remote_get($request_url, $request_args);
109
-            if (is_wp_error($response)) {
110
-                trigger_error($response->get_error_message());
111
-            }
112
-        } else {
113
-            EE_Messages_Scheduler::initiate_immediate_request_on_cron($task);
114
-        }
115
-    }
116
-
117
-
118
-    /**
119
-     * This returns
120
-     * the request params used for a scheduled message task request.
121
-     *
122
-     * @param string $task The task the request is for.
123
-     * @return array
124
-     */
125
-    public static function get_request_params($task)
126
-    {
127
-        // transient is used for flood control on msg_cron_trigger requests
128
-        $transient_key = 'ee_trans_' . uniqid($task);
129
-        set_transient($transient_key, 1, 5 * MINUTE_IN_SECONDS);
130
-        return array(
131
-            'type' => $task,
132
-            'key'  => $transient_key,
133
-        );
134
-    }
135
-
136
-
137
-    /**
138
-     * This is used to execute an immediate call to the run_cron task performed by EED_Messages
139
-     *
140
-     * @param string $task The task the request is being generated for.
141
-     */
142
-    public static function initiate_immediate_request_on_cron($task)
143
-    {
144
-        $request_args = EE_Messages_Scheduler::get_request_params($task);
145
-        // set those request args in the request so it gets picked up
146
-        foreach ($request_args as $request_key => $request_value) {
147
-            EE_Registry::instance()->REQ->set($request_key, $request_value);
148
-        }
149
-        EED_Messages::instance()->run_cron();
150
-    }
151
-
152
-
153
-    /**
154
-     * Callback for scheduled AHEE__EE_Messages_Scheduler__generation wp cron event
155
-     */
156
-    public static function batch_generation()
157
-    {
158
-        /**
159
-         * @see filter usage in EE_Messages_Queue::initiate_request_by_priority()
160
-         */
161
-        if (! apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false)
162
-            || ! EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request
163
-        ) {
164
-            EE_Messages_Scheduler::initiate_immediate_request_on_cron('generate');
165
-        }
166
-    }
167
-
168
-
169
-    /**
170
-     * Callback for scheduled AHEE__EE_Messages_Scheduler__sending
171
-     */
172
-    public static function batch_sending()
173
-    {
174
-        /**
175
-         * @see filter usage in EE_Messages_Queue::initiate_request_by_priority()
176
-         */
177
-        if (! apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false)
178
-            || ! EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request
179
-        ) {
180
-            EE_Messages_Scheduler::initiate_immediate_request_on_cron('send');
181
-        }
182
-    }
183
-
184
-
185
-    /**
186
-     * This is the callback for the `AHEE__EE_Messages_Scheduler__cleanup` scheduled event action.
187
-     * This runs once a day and if cleanup is active (set via messages settings), it will (by default) delete permanently
188
-     * from the database messages that have a MSG_modified date older than 30 days.
189
-     */
190
-    public static function cleanup()
191
-    {
192
-        // First, confirm that the generation and sending EE_Messages_Scheduler crons are
193
-        // set and reschedule them if they are not.
194
-        $message_crons_to_check = array(
195
-            'AHEE__EE_Messages_Scheduler__generation' => 'ee_message_cron',
196
-            'AHEE__EE_Messages_Scheduler__sending'    => 'ee_message_cron',
197
-        );
198
-        foreach ($message_crons_to_check as $hook_name => $frequency) {
199
-            if (! wp_next_scheduled($hook_name)) {
200
-                wp_schedule_event(time(), $frequency, $hook_name);
201
-            }
202
-        }
203
-
204
-        // check if user has cleanup turned on or if we're in maintenance mode.  If in maintenance mode we'll wait
205
-        // until the next scheduled event.
206
-        if (! EE_Registry::instance()->CFG->messages->delete_threshold
207
-            || ! EE_Maintenance_Mode::instance()->models_can_query()
208
-        ) {
209
-            return;
210
-        }
211
-
212
-        /**
213
-         * This filter switch allows other code (such as the EE_Worker_Queue add-on) to replace this with its own handling
214
-         * of deleting messages.
215
-         */
216
-        if (apply_filters('FHEE__EE_Messages_Scheduler__cleanup__handle_cleanup_on_cron', true)) {
217
-            EEM_Message::instance()->delete_old_messages(EE_Registry::instance()->CFG->messages->delete_threshold);
218
-        }
219
-    }
14
+	/**
15
+	 * Number of seconds between batch sends/generates on the cron job.
16
+	 * Defaults to 5 minutes in seconds.  If you want to change this interval, you can use the native WordPress
17
+	 * `cron_schedules` filter and modify the existing custom `ee_message_cron` schedule interval added.
18
+	 *
19
+	 * @type int
20
+	 */
21
+	const message_cron_schedule = 300;
22
+
23
+	/**
24
+	 * Constructor
25
+	 */
26
+	public function __construct()
27
+	{
28
+		// register tasks (and make sure only registered once).
29
+		if (! has_action('FHEE__EEH_Activation__get_cron_tasks', array($this, 'register_scheduled_tasks'))) {
30
+			add_action('FHEE__EEH_Activation__get_cron_tasks', array($this, 'register_scheduled_tasks'), 10);
31
+		}
32
+
33
+		// register callbacks for scheduled events (but make sure they are set only once).
34
+		if (! has_action(
35
+			'AHEE__EE_Messages_Scheduler__generation',
36
+			array('EE_Messages_Scheduler', 'batch_generation')
37
+		)) {
38
+			add_action('AHEE__EE_Messages_Scheduler__generation', array('EE_Messages_Scheduler', 'batch_generation'));
39
+			add_action('AHEE__EE_Messages_Scheduler__sending', array('EE_Messages_Scheduler', 'batch_sending'));
40
+			add_action('AHEE__EE_Messages_Scheduler__cleanup', array('EE_Messages_Scheduler', 'cleanup'));
41
+		}
42
+
43
+		// add custom schedules
44
+		add_filter('cron_schedules', array($this, 'custom_schedules'));
45
+	}
46
+
47
+
48
+	/**
49
+	 * Add custom schedules for wp_cron
50
+	 *
51
+	 * @param $schedules
52
+	 */
53
+	public function custom_schedules($schedules)
54
+	{
55
+		$schedules['ee_message_cron'] = array(
56
+			'interval' => self::message_cron_schedule,
57
+			'display'  => __(
58
+				'This is the cron time interval for EE Message schedules (defaults to once every 5 minutes)',
59
+				'event_espresso'
60
+			),
61
+		);
62
+		return $schedules;
63
+	}
64
+
65
+
66
+	/**
67
+	 * Callback for FHEE__EEH_Activation__get_cron_tasks that is used to retrieve scheduled Cron events to add and
68
+	 * remove.
69
+	 *
70
+	 * @param array $tasks already existing scheduled tasks
71
+	 * @return array
72
+	 */
73
+	public function register_scheduled_tasks($tasks)
74
+	{
75
+		EE_Registry::instance()->load_helper('DTT_Helper');
76
+		$tasks['AHEE__EE_Messages_Scheduler__generation'] = 'ee_message_cron';
77
+		$tasks['AHEE__EE_Messages_Scheduler__sending']    = 'ee_message_cron';
78
+		$tasks['AHEE__EE_Messages_Scheduler__cleanup'] = array( EEH_DTT_Helper::tomorrow(), 'daily');
79
+		return $tasks;
80
+	}
81
+
82
+
83
+	/**
84
+	 * This initiates a non-blocking separate request to execute on a scheduled task.
85
+	 * Note: The EED_Messages module has the handlers for these requests.
86
+	 *
87
+	 * @param string $task The task the request is being generated for.
88
+	 */
89
+	public static function initiate_scheduled_non_blocking_request($task)
90
+	{
91
+		if (apply_filters(
92
+			'EE_Messages_Scheduler__initiate_scheduled_non_blocking_request__do_separate_request',
93
+			true
94
+		)) {
95
+			$request_url  = add_query_arg(
96
+				array_merge(
97
+					array('ee' => 'msg_cron_trigger'),
98
+					EE_Messages_Scheduler::get_request_params($task)
99
+				),
100
+				site_url()
101
+			);
102
+			$request_args = array(
103
+				'timeout'     => 300,
104
+				'blocking'    => (defined('DOING_CRON') && DOING_CRON) || (defined('DOING_AJAX') && DOING_AJAX) ? true : false,
105
+				'sslverify'   => false,
106
+				'redirection' => 10,
107
+			);
108
+			$response     = wp_remote_get($request_url, $request_args);
109
+			if (is_wp_error($response)) {
110
+				trigger_error($response->get_error_message());
111
+			}
112
+		} else {
113
+			EE_Messages_Scheduler::initiate_immediate_request_on_cron($task);
114
+		}
115
+	}
116
+
117
+
118
+	/**
119
+	 * This returns
120
+	 * the request params used for a scheduled message task request.
121
+	 *
122
+	 * @param string $task The task the request is for.
123
+	 * @return array
124
+	 */
125
+	public static function get_request_params($task)
126
+	{
127
+		// transient is used for flood control on msg_cron_trigger requests
128
+		$transient_key = 'ee_trans_' . uniqid($task);
129
+		set_transient($transient_key, 1, 5 * MINUTE_IN_SECONDS);
130
+		return array(
131
+			'type' => $task,
132
+			'key'  => $transient_key,
133
+		);
134
+	}
135
+
136
+
137
+	/**
138
+	 * This is used to execute an immediate call to the run_cron task performed by EED_Messages
139
+	 *
140
+	 * @param string $task The task the request is being generated for.
141
+	 */
142
+	public static function initiate_immediate_request_on_cron($task)
143
+	{
144
+		$request_args = EE_Messages_Scheduler::get_request_params($task);
145
+		// set those request args in the request so it gets picked up
146
+		foreach ($request_args as $request_key => $request_value) {
147
+			EE_Registry::instance()->REQ->set($request_key, $request_value);
148
+		}
149
+		EED_Messages::instance()->run_cron();
150
+	}
151
+
152
+
153
+	/**
154
+	 * Callback for scheduled AHEE__EE_Messages_Scheduler__generation wp cron event
155
+	 */
156
+	public static function batch_generation()
157
+	{
158
+		/**
159
+		 * @see filter usage in EE_Messages_Queue::initiate_request_by_priority()
160
+		 */
161
+		if (! apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false)
162
+			|| ! EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request
163
+		) {
164
+			EE_Messages_Scheduler::initiate_immediate_request_on_cron('generate');
165
+		}
166
+	}
167
+
168
+
169
+	/**
170
+	 * Callback for scheduled AHEE__EE_Messages_Scheduler__sending
171
+	 */
172
+	public static function batch_sending()
173
+	{
174
+		/**
175
+		 * @see filter usage in EE_Messages_Queue::initiate_request_by_priority()
176
+		 */
177
+		if (! apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false)
178
+			|| ! EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request
179
+		) {
180
+			EE_Messages_Scheduler::initiate_immediate_request_on_cron('send');
181
+		}
182
+	}
183
+
184
+
185
+	/**
186
+	 * This is the callback for the `AHEE__EE_Messages_Scheduler__cleanup` scheduled event action.
187
+	 * This runs once a day and if cleanup is active (set via messages settings), it will (by default) delete permanently
188
+	 * from the database messages that have a MSG_modified date older than 30 days.
189
+	 */
190
+	public static function cleanup()
191
+	{
192
+		// First, confirm that the generation and sending EE_Messages_Scheduler crons are
193
+		// set and reschedule them if they are not.
194
+		$message_crons_to_check = array(
195
+			'AHEE__EE_Messages_Scheduler__generation' => 'ee_message_cron',
196
+			'AHEE__EE_Messages_Scheduler__sending'    => 'ee_message_cron',
197
+		);
198
+		foreach ($message_crons_to_check as $hook_name => $frequency) {
199
+			if (! wp_next_scheduled($hook_name)) {
200
+				wp_schedule_event(time(), $frequency, $hook_name);
201
+			}
202
+		}
203
+
204
+		// check if user has cleanup turned on or if we're in maintenance mode.  If in maintenance mode we'll wait
205
+		// until the next scheduled event.
206
+		if (! EE_Registry::instance()->CFG->messages->delete_threshold
207
+			|| ! EE_Maintenance_Mode::instance()->models_can_query()
208
+		) {
209
+			return;
210
+		}
211
+
212
+		/**
213
+		 * This filter switch allows other code (such as the EE_Worker_Queue add-on) to replace this with its own handling
214
+		 * of deleting messages.
215
+		 */
216
+		if (apply_filters('FHEE__EE_Messages_Scheduler__cleanup__handle_cleanup_on_cron', true)) {
217
+			EEM_Message::instance()->delete_old_messages(EE_Registry::instance()->CFG->messages->delete_threshold);
218
+		}
219
+	}
220 220
 }
Please login to merge, or discard this patch.
Spacing   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -26,12 +26,12 @@  discard block
 block discarded – undo
26 26
     public function __construct()
27 27
     {
28 28
         // register tasks (and make sure only registered once).
29
-        if (! has_action('FHEE__EEH_Activation__get_cron_tasks', array($this, 'register_scheduled_tasks'))) {
29
+        if ( ! has_action('FHEE__EEH_Activation__get_cron_tasks', array($this, 'register_scheduled_tasks'))) {
30 30
             add_action('FHEE__EEH_Activation__get_cron_tasks', array($this, 'register_scheduled_tasks'), 10);
31 31
         }
32 32
 
33 33
         // register callbacks for scheduled events (but make sure they are set only once).
34
-        if (! has_action(
34
+        if ( ! has_action(
35 35
             'AHEE__EE_Messages_Scheduler__generation',
36 36
             array('EE_Messages_Scheduler', 'batch_generation')
37 37
         )) {
@@ -75,7 +75,7 @@  discard block
 block discarded – undo
75 75
         EE_Registry::instance()->load_helper('DTT_Helper');
76 76
         $tasks['AHEE__EE_Messages_Scheduler__generation'] = 'ee_message_cron';
77 77
         $tasks['AHEE__EE_Messages_Scheduler__sending']    = 'ee_message_cron';
78
-        $tasks['AHEE__EE_Messages_Scheduler__cleanup'] = array( EEH_DTT_Helper::tomorrow(), 'daily');
78
+        $tasks['AHEE__EE_Messages_Scheduler__cleanup'] = array(EEH_DTT_Helper::tomorrow(), 'daily');
79 79
         return $tasks;
80 80
     }
81 81
 
@@ -92,7 +92,7 @@  discard block
 block discarded – undo
92 92
             'EE_Messages_Scheduler__initiate_scheduled_non_blocking_request__do_separate_request',
93 93
             true
94 94
         )) {
95
-            $request_url  = add_query_arg(
95
+            $request_url = add_query_arg(
96 96
                 array_merge(
97 97
                     array('ee' => 'msg_cron_trigger'),
98 98
                     EE_Messages_Scheduler::get_request_params($task)
@@ -105,7 +105,7 @@  discard block
 block discarded – undo
105 105
                 'sslverify'   => false,
106 106
                 'redirection' => 10,
107 107
             );
108
-            $response     = wp_remote_get($request_url, $request_args);
108
+            $response = wp_remote_get($request_url, $request_args);
109 109
             if (is_wp_error($response)) {
110 110
                 trigger_error($response->get_error_message());
111 111
             }
@@ -125,7 +125,7 @@  discard block
 block discarded – undo
125 125
     public static function get_request_params($task)
126 126
     {
127 127
         // transient is used for flood control on msg_cron_trigger requests
128
-        $transient_key = 'ee_trans_' . uniqid($task);
128
+        $transient_key = 'ee_trans_'.uniqid($task);
129 129
         set_transient($transient_key, 1, 5 * MINUTE_IN_SECONDS);
130 130
         return array(
131 131
             'type' => $task,
@@ -158,7 +158,7 @@  discard block
 block discarded – undo
158 158
         /**
159 159
          * @see filter usage in EE_Messages_Queue::initiate_request_by_priority()
160 160
          */
161
-        if (! apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false)
161
+        if ( ! apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false)
162 162
             || ! EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request
163 163
         ) {
164 164
             EE_Messages_Scheduler::initiate_immediate_request_on_cron('generate');
@@ -174,7 +174,7 @@  discard block
 block discarded – undo
174 174
         /**
175 175
          * @see filter usage in EE_Messages_Queue::initiate_request_by_priority()
176 176
          */
177
-        if (! apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false)
177
+        if ( ! apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false)
178 178
             || ! EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request
179 179
         ) {
180 180
             EE_Messages_Scheduler::initiate_immediate_request_on_cron('send');
@@ -196,14 +196,14 @@  discard block
 block discarded – undo
196 196
             'AHEE__EE_Messages_Scheduler__sending'    => 'ee_message_cron',
197 197
         );
198 198
         foreach ($message_crons_to_check as $hook_name => $frequency) {
199
-            if (! wp_next_scheduled($hook_name)) {
199
+            if ( ! wp_next_scheduled($hook_name)) {
200 200
                 wp_schedule_event(time(), $frequency, $hook_name);
201 201
             }
202 202
         }
203 203
 
204 204
         // check if user has cleanup turned on or if we're in maintenance mode.  If in maintenance mode we'll wait
205 205
         // until the next scheduled event.
206
-        if (! EE_Registry::instance()->CFG->messages->delete_threshold
206
+        if ( ! EE_Registry::instance()->CFG->messages->delete_threshold
207 207
             || ! EE_Maintenance_Mode::instance()->models_can_query()
208 208
         ) {
209 209
             return;
Please login to merge, or discard this patch.
espresso.php 1 patch
Indentation   +80 added lines, -80 removed lines patch added patch discarded remove patch
@@ -38,103 +38,103 @@
 block discarded – undo
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.9.rc.000');
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.9.rc.000');
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
 }
Please login to merge, or discard this patch.
core/helpers/EEH_Event_Query.helper.php 2 patches
Indentation   +672 added lines, -672 removed lines patch added patch discarded remove patch
@@ -15,676 +15,676 @@
 block discarded – undo
15 15
 class EEH_Event_Query
16 16
 {
17 17
 
18
-    /**
19
-     * Start Date
20
-     *
21
-     * @var $_event_query_month
22
-     */
23
-    protected static $_event_query_month;
24
-
25
-    /**
26
-     * Category
27
-     *
28
-     * @var $_event_query_category
29
-     */
30
-    protected static $_event_query_category;
31
-
32
-    /**
33
-     * whether to display expired events in the event list
34
-     *
35
-     * @var bool $_show_expired
36
-     */
37
-    protected static $_event_query_show_expired = false;
38
-
39
-    /**
40
-     * list of params for controlling how the query results are ordered
41
-     *
42
-     * @var array $_event_query_orderby
43
-     */
44
-    protected static $_event_query_orderby = array();
45
-
46
-    /**
47
-     * direction list is sorted
48
-     *
49
-     * @var string $_event_query_sort
50
-     */
51
-    protected static $_event_query_sort;
52
-
53
-    /**
54
-     * list of params used to build the query's various clauses
55
-     *
56
-     * @var $_query_params
57
-     */
58
-    protected static $_query_params = array();
59
-
60
-
61
-
62
-    /**
63
-     * @return void
64
-     */
65
-    public static function add_query_filters()
66
-    {
67
-        // add query filters
68
-        add_action('pre_get_posts', array('EEH_Event_Query', 'filter_query_parts'), 10, 1);
69
-    }
70
-
71
-
72
-
73
-    /**
74
-     * @param WP_Query $WP_Query
75
-     * @return bool
76
-     */
77
-    public static function apply_query_filters(WP_Query $WP_Query)
78
-    {
79
-        return (
80
-                   isset($WP_Query->query['post_type'])
81
-                   && $WP_Query->query['post_type'] === 'espresso_events'
82
-               )
83
-               || apply_filters('FHEE__EEH_Event_Query__apply_query_filters', false);
84
-    }
85
-
86
-
87
-    /**
88
-     * @param WP_Query $WP_Query
89
-     */
90
-    public static function filter_query_parts(WP_Query $WP_Query)
91
-    {
92
-        // ONLY add our filters if this isn't the main wp_query,
93
-        // because if this is the main wp_query we already have
94
-        // our cpt strategies take care of adding things in.
95
-        if ($WP_Query instanceof WP_Query && ! $WP_Query->is_main_query()) {
96
-            // build event list query
97
-            add_filter('posts_fields', array('EEH_Event_Query', 'posts_fields'), 10, 2);
98
-            add_filter('posts_join', array('EEH_Event_Query', 'posts_join'), 10, 2);
99
-            add_filter('posts_where', array('EEH_Event_Query', 'posts_where'), 10, 2);
100
-            add_filter('posts_orderby', array('EEH_Event_Query', 'posts_orderby'), 10, 2);
101
-            add_filter('posts_clauses_request', array('EEH_Event_Query', 'posts_clauses'), 10, 2);
102
-        }
103
-    }
104
-
105
-
106
-
107
-    /**
108
-     * @param string $month
109
-     * @param string $category
110
-     * @param bool   $show_expired
111
-     * @param 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                        = array();
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
-    /**
135
-     * what month should the event list display events for?
136
-     *
137
-     * @param string $month
138
-     * @return string
139
-     * @throws InvalidArgumentException
140
-     * @throws InvalidDataTypeException
141
-     * @throws InvalidInterfaceException
142
-     */
143
-    private static function _display_month($month = '')
144
-    {
145
-        return sanitize_text_field(EE_Registry::instance()->REQ->get('event_query_month', $month));
146
-    }
147
-
148
-
149
-
150
-    /**
151
-     * @param string $category
152
-     * @return string
153
-     * @throws InvalidArgumentException
154
-     * @throws InvalidDataTypeException
155
-     * @throws InvalidInterfaceException
156
-     */
157
-    private static function _event_category_slug($category = '')
158
-    {
159
-        return sanitize_text_field(EE_Registry::instance()->REQ->get('event_query_category', $category));
160
-    }
161
-
162
-
163
-
164
-    /**
165
-     * @param bool $show_expired
166
-     * @return bool
167
-     * @throws InvalidArgumentException
168
-     * @throws InvalidDataTypeException
169
-     * @throws InvalidInterfaceException
170
-     */
171
-    private static function _show_expired($show_expired = false)
172
-    {
173
-        // override default expired option if set via filter
174
-        return filter_var(
175
-            EE_Registry::instance()->REQ->get('event_query_show_expired', $show_expired),
176
-            FILTER_VALIDATE_BOOLEAN
177
-        );
178
-    }
179
-
180
-
181
-
182
-    /**
183
-     * @param    string $orderby
184
-     * @return array
185
-     * @throws InvalidArgumentException
186
-     * @throws InvalidDataTypeException
187
-     * @throws InvalidInterfaceException
188
-     */
189
-    private static function _orderby($orderby = 'start_date')
190
-    {
191
-        $event_query_orderby = EE_Registry::instance()->REQ->get('event_query_orderby', $orderby);
192
-        $event_query_orderby = is_array($event_query_orderby)
193
-            ? $event_query_orderby
194
-            : explode(',', $event_query_orderby);
195
-        $event_query_orderby = array_map('trim', $event_query_orderby);
196
-        $event_query_orderby = array_map('sanitize_text_field', $event_query_orderby);
197
-        return $event_query_orderby;
198
-    }
199
-
200
-
201
-
202
-    /**
203
-     * @param string $sort
204
-     * @return string
205
-     * @throws InvalidArgumentException
206
-     * @throws InvalidDataTypeException
207
-     * @throws InvalidInterfaceException
208
-     */
209
-    private static function _sort($sort = 'ASC')
210
-    {
211
-        $sort = EE_Registry::instance()->REQ->get('event_query_sort', $sort);
212
-        return in_array($sort, array('ASC', 'asc', 'DESC', 'desc'), true)
213
-            ? strtoupper($sort)
214
-            : 'ASC';
215
-    }
216
-
217
-
218
-
219
-    /**
220
-     * Filters the clauses for the WP_Query object
221
-     *
222
-     * @param array    $clauses array of clauses
223
-     * @param WP_Query $wp_query
224
-     * @return array   array of clauses
225
-     */
226
-    public static function posts_clauses($clauses, WP_Query $wp_query)
227
-    {
228
-        if (EEH_Event_Query::apply_query_filters($wp_query)) {
229
-            global $wpdb;
230
-            $clauses['groupby'] = $wpdb->posts . '.ID ';
231
-        }
232
-        return $clauses;
233
-    }
234
-
235
-
236
-
237
-    /**
238
-     * @param string   $SQL
239
-     * @param WP_Query $wp_query
240
-     * @return string
241
-     * @throws EE_Error
242
-     * @throws InvalidArgumentException
243
-     * @throws InvalidDataTypeException
244
-     * @throws InvalidInterfaceException
245
-     */
246
-    public static function posts_fields($SQL, WP_Query $wp_query)
247
-    {
248
-        if (EEH_Event_Query::apply_query_filters($wp_query)) {
249
-            // adds something like ", wp_esp_datetime.* " to WP Query SELECT statement
250
-            $SQL .= EEH_Event_Query::posts_fields_sql_for_orderby(EEH_Event_Query::$_event_query_orderby);
251
-        }
252
-        return $SQL;
253
-    }
254
-
255
-
256
-
257
-    /**
258
-     * @param array $orderby_params
259
-     * @return string
260
-     * @throws EE_Error
261
-     * @throws InvalidArgumentException
262
-     * @throws InvalidDataTypeException
263
-     * @throws InvalidInterfaceException
264
-     */
265
-    public static function posts_fields_sql_for_orderby(array $orderby_params = array())
266
-    {
267
-        $SQL = ', MIN( ' . EEM_Datetime::instance()->table() . '.DTT_EVT_start ) as event_start_date ';
268
-        foreach ($orderby_params as $orderby) {
269
-            switch ($orderby) {
270
-                case 'ticket_start':
271
-                    $SQL .= ', ' . EEM_Ticket::instance()->table() . '.TKT_start_date';
272
-                    break;
273
-                case 'ticket_end':
274
-                    $SQL .= ', ' . EEM_Ticket::instance()->table() . '.TKT_end_date';
275
-                    break;
276
-                case 'venue_title':
277
-                    $SQL .= ', Venue.post_title AS venue_title';
278
-                    break;
279
-                case 'city':
280
-                    $SQL .= ', ' . EEM_Venue::instance()->second_table() . '.VNU_city';
281
-                    break;
282
-                case 'state':
283
-                    $SQL .= ', ' . EEM_State::instance()->table() . '.STA_name';
284
-                    break;
285
-            }
286
-        }
287
-        return $SQL;
288
-    }
289
-
290
-
291
-
292
-    /**
293
-     * @param string   $SQL
294
-     * @param WP_Query $wp_query
295
-     * @return string
296
-     * @throws EE_Error
297
-     * @throws InvalidArgumentException
298
-     * @throws InvalidDataTypeException
299
-     * @throws InvalidInterfaceException
300
-     */
301
-    public static function posts_join($SQL = '', WP_Query $wp_query)
302
-    {
303
-        if (EEH_Event_Query::apply_query_filters($wp_query)) {
304
-            // Category
305
-            $SQL = EEH_Event_Query::posts_join_sql_for_show_expired($SQL, EEH_Event_Query::$_event_query_show_expired);
306
-            $SQL = EEH_Event_Query::posts_join_sql_for_terms($SQL, EEH_Event_Query::$_event_query_category);
307
-            $SQL = EEH_Event_Query::posts_join_for_orderby($SQL, EEH_Event_Query::$_event_query_orderby);
308
-        }
309
-        return $SQL;
310
-    }
311
-
312
-
313
-
314
-    /**
315
-     * @param string  $SQL
316
-     * @param boolean $show_expired if TRUE, then displayed past events
317
-     * @return string
318
-     * @throws EE_Error
319
-     * @throws InvalidArgumentException
320
-     * @throws InvalidDataTypeException
321
-     * @throws InvalidInterfaceException
322
-     */
323
-    public static function posts_join_sql_for_show_expired($SQL = '', $show_expired = false)
324
-    {
325
-        if (! $show_expired) {
326
-            $join = EEM_Event::instance()->table() . '.ID = ';
327
-            $join .= EEM_Datetime::instance()->table() . '.' . EEM_Event::instance()->primary_key_name();
328
-            // don't add if this is already in the SQL
329
-            if (strpos($SQL, $join) === false) {
330
-                $SQL .= ' INNER JOIN ' . EEM_Datetime::instance()->table() . ' ON ( ' . $join . ' ) ';
331
-            }
332
-        }
333
-        return $SQL;
334
-    }
335
-
336
-
337
-
338
-    /**
339
-     * @param string $SQL
340
-     * @param string $join_terms    pass TRUE or term string, doesn't really matter since this value doesn't really get
341
-     *                              used for anything yet
342
-     * @return string
343
-     */
344
-    public static function posts_join_sql_for_terms($SQL = '', $join_terms = '')
345
-    {
346
-        if (! empty($join_terms)) {
347
-            global $wpdb;
348
-            $SQL .= " LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)";
349
-            $SQL .= " LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)";
350
-            $SQL .= " LEFT JOIN $wpdb->terms ON ($wpdb->terms.term_id = $wpdb->term_taxonomy.term_id) ";
351
-        }
352
-        return $SQL;
353
-    }
354
-
355
-
356
-
357
-    /**
358
-     * usage:  $SQL .= EEH_Event_Query::posts_join_for_orderby( $orderby_params );
359
-     *
360
-     * @param    string $SQL
361
-     * @param    array  $orderby_params
362
-     * @return string
363
-     * @throws EE_Error
364
-     * @throws InvalidArgumentException
365
-     * @throws InvalidDataTypeException
366
-     * @throws InvalidInterfaceException
367
-     */
368
-    public static function posts_join_for_orderby($SQL = '', array $orderby_params = array())
369
-    {
370
-        foreach ($orderby_params as $orderby) {
371
-            switch ($orderby) {
372
-                case 'ticket_start':
373
-                case 'ticket_end':
374
-                    $SQL .= EEH_Event_Query::_posts_join_for_datetime(
375
-                        $SQL,
376
-                        EEM_Datetime_Ticket::instance()->table() . '.' . EEM_Datetime::instance()->primary_key_name()
377
-                    );
378
-                    $SQL .= ' LEFT JOIN ' . EEM_Ticket::instance()->table();
379
-                    $SQL .= ' ON (';
380
-                    $SQL .= EEM_Datetime_Ticket::instance()->table() . '.' . EEM_Ticket::instance()->primary_key_name();
381
-                    $SQL .= ' = ';
382
-                    $SQL .= EEM_Ticket::instance()->table() . '.' . EEM_Ticket::instance()->primary_key_name();
383
-                    $SQL .= ' )';
384
-                    break;
385
-                case 'venue_title':
386
-                case 'city':
387
-                    $SQL .= EEH_Event_Query::_posts_join_for_event_venue($SQL);
388
-                    break;
389
-                case 'state':
390
-                    $SQL .= EEH_Event_Query::_posts_join_for_event_venue($SQL);
391
-                    $SQL .= EEH_Event_Query::_posts_join_for_venue_state($SQL);
392
-                    break;
393
-                case 'start_date':
394
-                default:
395
-                    $SQL .= EEH_Event_Query::_posts_join_for_datetime($SQL, EEM_Event::instance()->table() . '.ID');
396
-                    break;
397
-            }
398
-        }
399
-        return $SQL;
400
-    }
401
-
402
-
403
-
404
-    /**
405
-     * @param string $SQL
406
-     * @param string $join
407
-     * @return string
408
-     * @throws EE_Error
409
-     * @throws InvalidArgumentException
410
-     * @throws InvalidDataTypeException
411
-     * @throws InvalidInterfaceException
412
-     */
413
-    protected static function _posts_join_for_datetime($SQL = '', $join = '')
414
-    {
415
-        if (! empty($join)) {
416
-            $join .= ' = ' . EEM_Datetime::instance()->table() . '.' . EEM_Event::instance()->primary_key_name();
417
-            if (strpos($SQL, $join) === false) {
418
-                return ' INNER JOIN ' . EEM_Datetime::instance()->table() . ' ON ( ' . $join . ' )';
419
-            }
420
-        }
421
-        return '';
422
-    }
423
-
424
-
425
-
426
-    /**
427
-     * @param string $SQL
428
-     * @return string
429
-     * @throws EE_Error
430
-     * @throws InvalidArgumentException
431
-     * @throws InvalidDataTypeException
432
-     * @throws InvalidInterfaceException
433
-     */
434
-    protected static function _posts_join_for_event_venue($SQL = '')
435
-    {
436
-        // Event Venue table name
437
-        $event_venue_table = EEM_Event_Venue::instance()->table();
438
-        // generate conditions for:  Event <=> Event Venue  JOIN clause
439
-        $event_to_event_venue_join = EEM_Event::instance()->table() . '.ID = ';
440
-        $event_to_event_venue_join .= $event_venue_table . '.' . EEM_Event::instance()->primary_key_name();
441
-        // don't add joins if they have already been added
442
-        if (strpos($SQL, $event_to_event_venue_join) === false) {
443
-            // Venue table name
444
-            $venue_table = EEM_Venue::instance()->table();
445
-            // Venue table pk
446
-            $venue_table_pk = EEM_Venue::instance()->primary_key_name();
447
-            // Venue Meta table name
448
-            $venue_meta_table = EEM_Venue::instance()->second_table();
449
-            // generate JOIN clause for: Event <=> Event Venue
450
-            $venue_SQL = " LEFT JOIN $event_venue_table ON ( $event_to_event_venue_join )";
451
-            // generate JOIN clause for: Event Venue <=> Venue
452
-            $venue_SQL .= " LEFT JOIN $venue_table as Venue ON ( $event_venue_table.$venue_table_pk = Venue.ID )";
453
-            // generate JOIN clause for: Venue <=> Venue Meta
454
-            $venue_SQL .= " LEFT JOIN $venue_meta_table ON ( Venue.ID = $venue_meta_table.$venue_table_pk )";
455
-            unset($event_venue_table, $event_to_event_venue_join, $venue_table, $venue_table_pk, $venue_meta_table);
456
-            return $venue_SQL;
457
-        }
458
-        unset($event_venue_table, $event_to_event_venue_join);
459
-        return '';
460
-    }
461
-
462
-
463
-
464
-    /**
465
-     * @param string $SQL
466
-     * @return string
467
-     * @throws EE_Error
468
-     * @throws InvalidArgumentException
469
-     * @throws InvalidDataTypeException
470
-     * @throws InvalidInterfaceException
471
-     */
472
-    protected static function _posts_join_for_venue_state($SQL = '')
473
-    {
474
-        // Venue Meta table name
475
-        $venue_meta_table = EEM_Venue::instance()->second_table();
476
-        // State table name
477
-        $state_table = EEM_State::instance()->table();
478
-        // State table pk
479
-        $state_table_pk = EEM_State::instance()->primary_key_name();
480
-        // verify vars
481
-        if ($venue_meta_table && $state_table && $state_table_pk) {
482
-            // like: wp_esp_venue_meta.STA_ID = wp_esp_state.STA_ID
483
-            $join = "$venue_meta_table.$state_table_pk = $state_table.$state_table_pk";
484
-            // don't add join if it has already been added
485
-            if (strpos($SQL, $join) === false) {
486
-                unset($state_table_pk, $venue_meta_table, $venue_table_pk);
487
-                return " LEFT JOIN $state_table ON ( $join )";
488
-            }
489
-        }
490
-        unset($join, $state_table, $state_table_pk, $venue_meta_table, $venue_table_pk);
491
-        return '';
492
-    }
493
-
494
-
495
-
496
-    /**
497
-     * @param string   $SQL
498
-     * @param WP_Query $wp_query
499
-     * @return string
500
-     * @throws EE_Error
501
-     * @throws InvalidArgumentException
502
-     * @throws InvalidDataTypeException
503
-     * @throws InvalidInterfaceException
504
-     */
505
-    public static function posts_where($SQL = '', WP_Query $wp_query)
506
-    {
507
-        if (EEH_Event_Query::apply_query_filters($wp_query)) {
508
-            // Show Expired ?
509
-            $SQL .= EEH_Event_Query::posts_where_sql_for_show_expired(EEH_Event_Query::$_event_query_show_expired);
510
-            // Category
511
-            $SQL .= EEH_Event_Query::posts_where_sql_for_event_category_slug(EEH_Event_Query::$_event_query_category);
512
-            // Start Date
513
-            $SQL .= EEH_Event_Query::posts_where_sql_for_event_list_month(EEH_Event_Query::$_event_query_month);
514
-        }
515
-        return $SQL;
516
-    }
517
-
518
-
519
-
520
-    /**
521
-     * @param    boolean $show_expired if TRUE, then displayed past events
522
-     * @return string
523
-     * @throws EE_Error
524
-     * @throws InvalidArgumentException
525
-     * @throws InvalidDataTypeException
526
-     * @throws InvalidInterfaceException
527
-     */
528
-    public static function posts_where_sql_for_show_expired($show_expired = false)
529
-    {
530
-        return ! $show_expired
531
-            ? ' AND ' . EEM_Datetime::instance()->table() . '.DTT_EVT_end > \'' . current_time('mysql', true) . '\' '
532
-            : '';
533
-    }
534
-
535
-
536
-
537
-    /**
538
-     * @param boolean $event_category_slug
539
-     * @return string
540
-     */
541
-    public static function posts_where_sql_for_event_category_slug($event_category_slug = null)
542
-    {
543
-        global $wpdb;
544
-        if (! empty($event_category_slug)) {
545
-            $event_category_slugs_array = array_map('trim', explode(',', $event_category_slug));
546
-            $event_category_slugs_prepare = implode(', ', array_fill(0, count($event_category_slugs_array), '%s'));
547
-            return $wpdb->prepare(" AND {$wpdb->terms}.slug IN ({$event_category_slugs_prepare}) ", $event_category_slugs_array);
548
-        }
549
-        return '';
550
-    }
551
-
552
-
553
-
554
-    /**
555
-     * @param boolean $month
556
-     * @return string
557
-     * @throws EE_Error
558
-     * @throws InvalidArgumentException
559
-     * @throws InvalidDataTypeException
560
-     * @throws InvalidInterfaceException
561
-     */
562
-    public static function posts_where_sql_for_event_list_month($month = null)
563
-    {
564
-        $SQL = '';
565
-        if (! empty($month)) {
566
-            $datetime_table = EEM_Datetime::instance()->table();
567
-            // event start date is LESS than the end of the month ( so nothing that doesn't start until next month )
568
-            $SQL = " AND {$datetime_table}.DTT_EVT_start <= '";
569
-            $SQL .= date('Y-m-t 23:59:59', \EEH_DTT_Helper::first_of_month_timestamp($month)) . "'";
570
-            // event end date is GREATER than the start of the month ( so nothing that ended before this month )
571
-            $SQL .= " AND {$datetime_table}.DTT_EVT_end >= '";
572
-            $SQL .= date('Y-m-01 0:0:00', \EEH_DTT_Helper::first_of_month_timestamp($month)) . "' ";
573
-        }
574
-        return $SQL;
575
-    }
576
-
577
-
578
-
579
-    /**
580
-     * @param string $SQL
581
-     * @param WP_Query $wp_query
582
-     * @return string
583
-     * @throws EE_Error
584
-     * @throws InvalidArgumentException
585
-     * @throws InvalidDataTypeException
586
-     * @throws InvalidInterfaceException
587
-     */
588
-    public static function posts_orderby($SQL = '', WP_Query $wp_query)
589
-    {
590
-        if (EEH_Event_Query::apply_query_filters($wp_query)) {
591
-            $SQL = EEH_Event_Query::posts_orderby_sql(
592
-                EEH_Event_Query::$_event_query_orderby,
593
-                EEH_Event_Query::$_event_query_sort
594
-            );
595
-        }
596
-        return $SQL;
597
-    }
598
-
599
-
600
-
601
-    /**
602
-     *    posts_orderby_sql
603
-     *    possible parameters:
604
-     *    ID
605
-     *    start_date
606
-     *    end_date
607
-     *    event_name
608
-     *    category_slug
609
-     *    ticket_start
610
-     *    ticket_end
611
-     *    venue_title
612
-     *    city
613
-     *    state
614
-     *    **IMPORTANT**
615
-     *    make sure to also send the $orderby_params array to the posts_join_for_orderby() method
616
-     *    or else some of the table references below will result in MySQL errors
617
-     *
618
-     * @param array  $orderby_params
619
-     * @param string $sort
620
-     * @return string
621
-     * @throws EE_Error
622
-     * @throws InvalidArgumentException
623
-     * @throws InvalidDataTypeException
624
-     * @throws InvalidInterfaceException
625
-     */
626
-    public static function posts_orderby_sql(array $orderby_params = array(), $sort = 'ASC')
627
-    {
628
-        global $wpdb;
629
-        $SQL     = '';
630
-        $counter = 0;
631
-        $sort    = in_array($sort, array('ASC', 'asc', 'DESC', 'desc'), true)
632
-            ? strtoupper($sort)
633
-            : 'ASC';
634
-        // make sure 'orderby' is set in query params
635
-        if (! isset(self::$_query_params['orderby'])) {
636
-            self::$_query_params['orderby'] = array();
637
-        }
638
-        // loop thru $orderby_params (type cast as array)
639
-        foreach ($orderby_params as $orderby) {
640
-            // check if we have already added this param
641
-            if (isset(self::$_query_params['orderby'][ $orderby ])) {
642
-                // if so then remove from the $orderby_params so that the count() method below is accurate
643
-                unset($orderby_params[ $orderby ]);
644
-                // then bump ahead to the next param
645
-                continue;
646
-            }
647
-            // this will ad a comma depending on whether this is the first or last param
648
-            $glue = $counter === 0 || $counter === count($orderby_params) ? ' ' : ', ';
649
-            // ok what's we dealing with?
650
-            switch ($orderby) {
651
-                case 'id':
652
-                case 'ID':
653
-                    $SQL .= $glue . $wpdb->posts . '.ID ' . $sort;
654
-                    break;
655
-                case 'end_date':
656
-                    $SQL .= $glue . EEM_Datetime::instance()->table() . '.DTT_EVT_end ' . $sort;
657
-                    break;
658
-                case 'event_name':
659
-                    $SQL .= $glue . $wpdb->posts . '.post_title ' . $sort;
660
-                    break;
661
-                case 'category_slug':
662
-                    $SQL .= $glue . $wpdb->terms . '.slug ' . $sort;
663
-                    break;
664
-                case 'ticket_start':
665
-                    $SQL .= $glue . EEM_Ticket::instance()->table() . '.TKT_start_date ' . $sort;
666
-                    break;
667
-                case 'ticket_end':
668
-                    $SQL .= $glue . EEM_Ticket::instance()->table() . '.TKT_end_date ' . $sort;
669
-                    break;
670
-                case 'venue_title':
671
-                    $SQL .= $glue . 'venue_title ' . $sort;
672
-                    break;
673
-                case 'city':
674
-                    $SQL .= $glue . EEM_Venue::instance()->second_table() . '.VNU_city ' . $sort;
675
-                    break;
676
-                case 'state':
677
-                    $SQL .= $glue . EEM_State::instance()->table() . '.STA_name ' . $sort;
678
-                    break;
679
-                case 'start_date':
680
-                default:
681
-                    $SQL .= $glue . ' event_start_date ' . $sort;
682
-                    break;
683
-            }
684
-            // add to array of orderby params that have been added
685
-            self::$_query_params['orderby'][ $orderby ] = true;
686
-            $counter++;
687
-        }
688
-        return $SQL;
689
-    }
18
+	/**
19
+	 * Start Date
20
+	 *
21
+	 * @var $_event_query_month
22
+	 */
23
+	protected static $_event_query_month;
24
+
25
+	/**
26
+	 * Category
27
+	 *
28
+	 * @var $_event_query_category
29
+	 */
30
+	protected static $_event_query_category;
31
+
32
+	/**
33
+	 * whether to display expired events in the event list
34
+	 *
35
+	 * @var bool $_show_expired
36
+	 */
37
+	protected static $_event_query_show_expired = false;
38
+
39
+	/**
40
+	 * list of params for controlling how the query results are ordered
41
+	 *
42
+	 * @var array $_event_query_orderby
43
+	 */
44
+	protected static $_event_query_orderby = array();
45
+
46
+	/**
47
+	 * direction list is sorted
48
+	 *
49
+	 * @var string $_event_query_sort
50
+	 */
51
+	protected static $_event_query_sort;
52
+
53
+	/**
54
+	 * list of params used to build the query's various clauses
55
+	 *
56
+	 * @var $_query_params
57
+	 */
58
+	protected static $_query_params = array();
59
+
60
+
61
+
62
+	/**
63
+	 * @return void
64
+	 */
65
+	public static function add_query_filters()
66
+	{
67
+		// add query filters
68
+		add_action('pre_get_posts', array('EEH_Event_Query', 'filter_query_parts'), 10, 1);
69
+	}
70
+
71
+
72
+
73
+	/**
74
+	 * @param WP_Query $WP_Query
75
+	 * @return bool
76
+	 */
77
+	public static function apply_query_filters(WP_Query $WP_Query)
78
+	{
79
+		return (
80
+				   isset($WP_Query->query['post_type'])
81
+				   && $WP_Query->query['post_type'] === 'espresso_events'
82
+			   )
83
+			   || apply_filters('FHEE__EEH_Event_Query__apply_query_filters', false);
84
+	}
85
+
86
+
87
+	/**
88
+	 * @param WP_Query $WP_Query
89
+	 */
90
+	public static function filter_query_parts(WP_Query $WP_Query)
91
+	{
92
+		// ONLY add our filters if this isn't the main wp_query,
93
+		// because if this is the main wp_query we already have
94
+		// our cpt strategies take care of adding things in.
95
+		if ($WP_Query instanceof WP_Query && ! $WP_Query->is_main_query()) {
96
+			// build event list query
97
+			add_filter('posts_fields', array('EEH_Event_Query', 'posts_fields'), 10, 2);
98
+			add_filter('posts_join', array('EEH_Event_Query', 'posts_join'), 10, 2);
99
+			add_filter('posts_where', array('EEH_Event_Query', 'posts_where'), 10, 2);
100
+			add_filter('posts_orderby', array('EEH_Event_Query', 'posts_orderby'), 10, 2);
101
+			add_filter('posts_clauses_request', array('EEH_Event_Query', 'posts_clauses'), 10, 2);
102
+		}
103
+	}
104
+
105
+
106
+
107
+	/**
108
+	 * @param string $month
109
+	 * @param string $category
110
+	 * @param bool   $show_expired
111
+	 * @param 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                        = array();
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
+	/**
135
+	 * what month should the event list display events for?
136
+	 *
137
+	 * @param string $month
138
+	 * @return string
139
+	 * @throws InvalidArgumentException
140
+	 * @throws InvalidDataTypeException
141
+	 * @throws InvalidInterfaceException
142
+	 */
143
+	private static function _display_month($month = '')
144
+	{
145
+		return sanitize_text_field(EE_Registry::instance()->REQ->get('event_query_month', $month));
146
+	}
147
+
148
+
149
+
150
+	/**
151
+	 * @param string $category
152
+	 * @return string
153
+	 * @throws InvalidArgumentException
154
+	 * @throws InvalidDataTypeException
155
+	 * @throws InvalidInterfaceException
156
+	 */
157
+	private static function _event_category_slug($category = '')
158
+	{
159
+		return sanitize_text_field(EE_Registry::instance()->REQ->get('event_query_category', $category));
160
+	}
161
+
162
+
163
+
164
+	/**
165
+	 * @param bool $show_expired
166
+	 * @return bool
167
+	 * @throws InvalidArgumentException
168
+	 * @throws InvalidDataTypeException
169
+	 * @throws InvalidInterfaceException
170
+	 */
171
+	private static function _show_expired($show_expired = false)
172
+	{
173
+		// override default expired option if set via filter
174
+		return filter_var(
175
+			EE_Registry::instance()->REQ->get('event_query_show_expired', $show_expired),
176
+			FILTER_VALIDATE_BOOLEAN
177
+		);
178
+	}
179
+
180
+
181
+
182
+	/**
183
+	 * @param    string $orderby
184
+	 * @return array
185
+	 * @throws InvalidArgumentException
186
+	 * @throws InvalidDataTypeException
187
+	 * @throws InvalidInterfaceException
188
+	 */
189
+	private static function _orderby($orderby = 'start_date')
190
+	{
191
+		$event_query_orderby = EE_Registry::instance()->REQ->get('event_query_orderby', $orderby);
192
+		$event_query_orderby = is_array($event_query_orderby)
193
+			? $event_query_orderby
194
+			: explode(',', $event_query_orderby);
195
+		$event_query_orderby = array_map('trim', $event_query_orderby);
196
+		$event_query_orderby = array_map('sanitize_text_field', $event_query_orderby);
197
+		return $event_query_orderby;
198
+	}
199
+
200
+
201
+
202
+	/**
203
+	 * @param string $sort
204
+	 * @return string
205
+	 * @throws InvalidArgumentException
206
+	 * @throws InvalidDataTypeException
207
+	 * @throws InvalidInterfaceException
208
+	 */
209
+	private static function _sort($sort = 'ASC')
210
+	{
211
+		$sort = EE_Registry::instance()->REQ->get('event_query_sort', $sort);
212
+		return in_array($sort, array('ASC', 'asc', 'DESC', 'desc'), true)
213
+			? strtoupper($sort)
214
+			: 'ASC';
215
+	}
216
+
217
+
218
+
219
+	/**
220
+	 * Filters the clauses for the WP_Query object
221
+	 *
222
+	 * @param array    $clauses array of clauses
223
+	 * @param WP_Query $wp_query
224
+	 * @return array   array of clauses
225
+	 */
226
+	public static function posts_clauses($clauses, WP_Query $wp_query)
227
+	{
228
+		if (EEH_Event_Query::apply_query_filters($wp_query)) {
229
+			global $wpdb;
230
+			$clauses['groupby'] = $wpdb->posts . '.ID ';
231
+		}
232
+		return $clauses;
233
+	}
234
+
235
+
236
+
237
+	/**
238
+	 * @param string   $SQL
239
+	 * @param WP_Query $wp_query
240
+	 * @return string
241
+	 * @throws EE_Error
242
+	 * @throws InvalidArgumentException
243
+	 * @throws InvalidDataTypeException
244
+	 * @throws InvalidInterfaceException
245
+	 */
246
+	public static function posts_fields($SQL, WP_Query $wp_query)
247
+	{
248
+		if (EEH_Event_Query::apply_query_filters($wp_query)) {
249
+			// adds something like ", wp_esp_datetime.* " to WP Query SELECT statement
250
+			$SQL .= EEH_Event_Query::posts_fields_sql_for_orderby(EEH_Event_Query::$_event_query_orderby);
251
+		}
252
+		return $SQL;
253
+	}
254
+
255
+
256
+
257
+	/**
258
+	 * @param array $orderby_params
259
+	 * @return string
260
+	 * @throws EE_Error
261
+	 * @throws InvalidArgumentException
262
+	 * @throws InvalidDataTypeException
263
+	 * @throws InvalidInterfaceException
264
+	 */
265
+	public static function posts_fields_sql_for_orderby(array $orderby_params = array())
266
+	{
267
+		$SQL = ', MIN( ' . EEM_Datetime::instance()->table() . '.DTT_EVT_start ) as event_start_date ';
268
+		foreach ($orderby_params as $orderby) {
269
+			switch ($orderby) {
270
+				case 'ticket_start':
271
+					$SQL .= ', ' . EEM_Ticket::instance()->table() . '.TKT_start_date';
272
+					break;
273
+				case 'ticket_end':
274
+					$SQL .= ', ' . EEM_Ticket::instance()->table() . '.TKT_end_date';
275
+					break;
276
+				case 'venue_title':
277
+					$SQL .= ', Venue.post_title AS venue_title';
278
+					break;
279
+				case 'city':
280
+					$SQL .= ', ' . EEM_Venue::instance()->second_table() . '.VNU_city';
281
+					break;
282
+				case 'state':
283
+					$SQL .= ', ' . EEM_State::instance()->table() . '.STA_name';
284
+					break;
285
+			}
286
+		}
287
+		return $SQL;
288
+	}
289
+
290
+
291
+
292
+	/**
293
+	 * @param string   $SQL
294
+	 * @param WP_Query $wp_query
295
+	 * @return string
296
+	 * @throws EE_Error
297
+	 * @throws InvalidArgumentException
298
+	 * @throws InvalidDataTypeException
299
+	 * @throws InvalidInterfaceException
300
+	 */
301
+	public static function posts_join($SQL = '', WP_Query $wp_query)
302
+	{
303
+		if (EEH_Event_Query::apply_query_filters($wp_query)) {
304
+			// Category
305
+			$SQL = EEH_Event_Query::posts_join_sql_for_show_expired($SQL, EEH_Event_Query::$_event_query_show_expired);
306
+			$SQL = EEH_Event_Query::posts_join_sql_for_terms($SQL, EEH_Event_Query::$_event_query_category);
307
+			$SQL = EEH_Event_Query::posts_join_for_orderby($SQL, EEH_Event_Query::$_event_query_orderby);
308
+		}
309
+		return $SQL;
310
+	}
311
+
312
+
313
+
314
+	/**
315
+	 * @param string  $SQL
316
+	 * @param boolean $show_expired if TRUE, then displayed past events
317
+	 * @return string
318
+	 * @throws EE_Error
319
+	 * @throws InvalidArgumentException
320
+	 * @throws InvalidDataTypeException
321
+	 * @throws InvalidInterfaceException
322
+	 */
323
+	public static function posts_join_sql_for_show_expired($SQL = '', $show_expired = false)
324
+	{
325
+		if (! $show_expired) {
326
+			$join = EEM_Event::instance()->table() . '.ID = ';
327
+			$join .= EEM_Datetime::instance()->table() . '.' . EEM_Event::instance()->primary_key_name();
328
+			// don't add if this is already in the SQL
329
+			if (strpos($SQL, $join) === false) {
330
+				$SQL .= ' INNER JOIN ' . EEM_Datetime::instance()->table() . ' ON ( ' . $join . ' ) ';
331
+			}
332
+		}
333
+		return $SQL;
334
+	}
335
+
336
+
337
+
338
+	/**
339
+	 * @param string $SQL
340
+	 * @param string $join_terms    pass TRUE or term string, doesn't really matter since this value doesn't really get
341
+	 *                              used for anything yet
342
+	 * @return string
343
+	 */
344
+	public static function posts_join_sql_for_terms($SQL = '', $join_terms = '')
345
+	{
346
+		if (! empty($join_terms)) {
347
+			global $wpdb;
348
+			$SQL .= " LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)";
349
+			$SQL .= " LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)";
350
+			$SQL .= " LEFT JOIN $wpdb->terms ON ($wpdb->terms.term_id = $wpdb->term_taxonomy.term_id) ";
351
+		}
352
+		return $SQL;
353
+	}
354
+
355
+
356
+
357
+	/**
358
+	 * usage:  $SQL .= EEH_Event_Query::posts_join_for_orderby( $orderby_params );
359
+	 *
360
+	 * @param    string $SQL
361
+	 * @param    array  $orderby_params
362
+	 * @return string
363
+	 * @throws EE_Error
364
+	 * @throws InvalidArgumentException
365
+	 * @throws InvalidDataTypeException
366
+	 * @throws InvalidInterfaceException
367
+	 */
368
+	public static function posts_join_for_orderby($SQL = '', array $orderby_params = array())
369
+	{
370
+		foreach ($orderby_params as $orderby) {
371
+			switch ($orderby) {
372
+				case 'ticket_start':
373
+				case 'ticket_end':
374
+					$SQL .= EEH_Event_Query::_posts_join_for_datetime(
375
+						$SQL,
376
+						EEM_Datetime_Ticket::instance()->table() . '.' . EEM_Datetime::instance()->primary_key_name()
377
+					);
378
+					$SQL .= ' LEFT JOIN ' . EEM_Ticket::instance()->table();
379
+					$SQL .= ' ON (';
380
+					$SQL .= EEM_Datetime_Ticket::instance()->table() . '.' . EEM_Ticket::instance()->primary_key_name();
381
+					$SQL .= ' = ';
382
+					$SQL .= EEM_Ticket::instance()->table() . '.' . EEM_Ticket::instance()->primary_key_name();
383
+					$SQL .= ' )';
384
+					break;
385
+				case 'venue_title':
386
+				case 'city':
387
+					$SQL .= EEH_Event_Query::_posts_join_for_event_venue($SQL);
388
+					break;
389
+				case 'state':
390
+					$SQL .= EEH_Event_Query::_posts_join_for_event_venue($SQL);
391
+					$SQL .= EEH_Event_Query::_posts_join_for_venue_state($SQL);
392
+					break;
393
+				case 'start_date':
394
+				default:
395
+					$SQL .= EEH_Event_Query::_posts_join_for_datetime($SQL, EEM_Event::instance()->table() . '.ID');
396
+					break;
397
+			}
398
+		}
399
+		return $SQL;
400
+	}
401
+
402
+
403
+
404
+	/**
405
+	 * @param string $SQL
406
+	 * @param string $join
407
+	 * @return string
408
+	 * @throws EE_Error
409
+	 * @throws InvalidArgumentException
410
+	 * @throws InvalidDataTypeException
411
+	 * @throws InvalidInterfaceException
412
+	 */
413
+	protected static function _posts_join_for_datetime($SQL = '', $join = '')
414
+	{
415
+		if (! empty($join)) {
416
+			$join .= ' = ' . EEM_Datetime::instance()->table() . '.' . EEM_Event::instance()->primary_key_name();
417
+			if (strpos($SQL, $join) === false) {
418
+				return ' INNER JOIN ' . EEM_Datetime::instance()->table() . ' ON ( ' . $join . ' )';
419
+			}
420
+		}
421
+		return '';
422
+	}
423
+
424
+
425
+
426
+	/**
427
+	 * @param string $SQL
428
+	 * @return string
429
+	 * @throws EE_Error
430
+	 * @throws InvalidArgumentException
431
+	 * @throws InvalidDataTypeException
432
+	 * @throws InvalidInterfaceException
433
+	 */
434
+	protected static function _posts_join_for_event_venue($SQL = '')
435
+	{
436
+		// Event Venue table name
437
+		$event_venue_table = EEM_Event_Venue::instance()->table();
438
+		// generate conditions for:  Event <=> Event Venue  JOIN clause
439
+		$event_to_event_venue_join = EEM_Event::instance()->table() . '.ID = ';
440
+		$event_to_event_venue_join .= $event_venue_table . '.' . EEM_Event::instance()->primary_key_name();
441
+		// don't add joins if they have already been added
442
+		if (strpos($SQL, $event_to_event_venue_join) === false) {
443
+			// Venue table name
444
+			$venue_table = EEM_Venue::instance()->table();
445
+			// Venue table pk
446
+			$venue_table_pk = EEM_Venue::instance()->primary_key_name();
447
+			// Venue Meta table name
448
+			$venue_meta_table = EEM_Venue::instance()->second_table();
449
+			// generate JOIN clause for: Event <=> Event Venue
450
+			$venue_SQL = " LEFT JOIN $event_venue_table ON ( $event_to_event_venue_join )";
451
+			// generate JOIN clause for: Event Venue <=> Venue
452
+			$venue_SQL .= " LEFT JOIN $venue_table as Venue ON ( $event_venue_table.$venue_table_pk = Venue.ID )";
453
+			// generate JOIN clause for: Venue <=> Venue Meta
454
+			$venue_SQL .= " LEFT JOIN $venue_meta_table ON ( Venue.ID = $venue_meta_table.$venue_table_pk )";
455
+			unset($event_venue_table, $event_to_event_venue_join, $venue_table, $venue_table_pk, $venue_meta_table);
456
+			return $venue_SQL;
457
+		}
458
+		unset($event_venue_table, $event_to_event_venue_join);
459
+		return '';
460
+	}
461
+
462
+
463
+
464
+	/**
465
+	 * @param string $SQL
466
+	 * @return string
467
+	 * @throws EE_Error
468
+	 * @throws InvalidArgumentException
469
+	 * @throws InvalidDataTypeException
470
+	 * @throws InvalidInterfaceException
471
+	 */
472
+	protected static function _posts_join_for_venue_state($SQL = '')
473
+	{
474
+		// Venue Meta table name
475
+		$venue_meta_table = EEM_Venue::instance()->second_table();
476
+		// State table name
477
+		$state_table = EEM_State::instance()->table();
478
+		// State table pk
479
+		$state_table_pk = EEM_State::instance()->primary_key_name();
480
+		// verify vars
481
+		if ($venue_meta_table && $state_table && $state_table_pk) {
482
+			// like: wp_esp_venue_meta.STA_ID = wp_esp_state.STA_ID
483
+			$join = "$venue_meta_table.$state_table_pk = $state_table.$state_table_pk";
484
+			// don't add join if it has already been added
485
+			if (strpos($SQL, $join) === false) {
486
+				unset($state_table_pk, $venue_meta_table, $venue_table_pk);
487
+				return " LEFT JOIN $state_table ON ( $join )";
488
+			}
489
+		}
490
+		unset($join, $state_table, $state_table_pk, $venue_meta_table, $venue_table_pk);
491
+		return '';
492
+	}
493
+
494
+
495
+
496
+	/**
497
+	 * @param string   $SQL
498
+	 * @param WP_Query $wp_query
499
+	 * @return string
500
+	 * @throws EE_Error
501
+	 * @throws InvalidArgumentException
502
+	 * @throws InvalidDataTypeException
503
+	 * @throws InvalidInterfaceException
504
+	 */
505
+	public static function posts_where($SQL = '', WP_Query $wp_query)
506
+	{
507
+		if (EEH_Event_Query::apply_query_filters($wp_query)) {
508
+			// Show Expired ?
509
+			$SQL .= EEH_Event_Query::posts_where_sql_for_show_expired(EEH_Event_Query::$_event_query_show_expired);
510
+			// Category
511
+			$SQL .= EEH_Event_Query::posts_where_sql_for_event_category_slug(EEH_Event_Query::$_event_query_category);
512
+			// Start Date
513
+			$SQL .= EEH_Event_Query::posts_where_sql_for_event_list_month(EEH_Event_Query::$_event_query_month);
514
+		}
515
+		return $SQL;
516
+	}
517
+
518
+
519
+
520
+	/**
521
+	 * @param    boolean $show_expired if TRUE, then displayed past events
522
+	 * @return string
523
+	 * @throws EE_Error
524
+	 * @throws InvalidArgumentException
525
+	 * @throws InvalidDataTypeException
526
+	 * @throws InvalidInterfaceException
527
+	 */
528
+	public static function posts_where_sql_for_show_expired($show_expired = false)
529
+	{
530
+		return ! $show_expired
531
+			? ' AND ' . EEM_Datetime::instance()->table() . '.DTT_EVT_end > \'' . current_time('mysql', true) . '\' '
532
+			: '';
533
+	}
534
+
535
+
536
+
537
+	/**
538
+	 * @param boolean $event_category_slug
539
+	 * @return string
540
+	 */
541
+	public static function posts_where_sql_for_event_category_slug($event_category_slug = null)
542
+	{
543
+		global $wpdb;
544
+		if (! empty($event_category_slug)) {
545
+			$event_category_slugs_array = array_map('trim', explode(',', $event_category_slug));
546
+			$event_category_slugs_prepare = implode(', ', array_fill(0, count($event_category_slugs_array), '%s'));
547
+			return $wpdb->prepare(" AND {$wpdb->terms}.slug IN ({$event_category_slugs_prepare}) ", $event_category_slugs_array);
548
+		}
549
+		return '';
550
+	}
551
+
552
+
553
+
554
+	/**
555
+	 * @param boolean $month
556
+	 * @return string
557
+	 * @throws EE_Error
558
+	 * @throws InvalidArgumentException
559
+	 * @throws InvalidDataTypeException
560
+	 * @throws InvalidInterfaceException
561
+	 */
562
+	public static function posts_where_sql_for_event_list_month($month = null)
563
+	{
564
+		$SQL = '';
565
+		if (! empty($month)) {
566
+			$datetime_table = EEM_Datetime::instance()->table();
567
+			// event start date is LESS than the end of the month ( so nothing that doesn't start until next month )
568
+			$SQL = " AND {$datetime_table}.DTT_EVT_start <= '";
569
+			$SQL .= date('Y-m-t 23:59:59', \EEH_DTT_Helper::first_of_month_timestamp($month)) . "'";
570
+			// event end date is GREATER than the start of the month ( so nothing that ended before this month )
571
+			$SQL .= " AND {$datetime_table}.DTT_EVT_end >= '";
572
+			$SQL .= date('Y-m-01 0:0:00', \EEH_DTT_Helper::first_of_month_timestamp($month)) . "' ";
573
+		}
574
+		return $SQL;
575
+	}
576
+
577
+
578
+
579
+	/**
580
+	 * @param string $SQL
581
+	 * @param WP_Query $wp_query
582
+	 * @return string
583
+	 * @throws EE_Error
584
+	 * @throws InvalidArgumentException
585
+	 * @throws InvalidDataTypeException
586
+	 * @throws InvalidInterfaceException
587
+	 */
588
+	public static function posts_orderby($SQL = '', WP_Query $wp_query)
589
+	{
590
+		if (EEH_Event_Query::apply_query_filters($wp_query)) {
591
+			$SQL = EEH_Event_Query::posts_orderby_sql(
592
+				EEH_Event_Query::$_event_query_orderby,
593
+				EEH_Event_Query::$_event_query_sort
594
+			);
595
+		}
596
+		return $SQL;
597
+	}
598
+
599
+
600
+
601
+	/**
602
+	 *    posts_orderby_sql
603
+	 *    possible parameters:
604
+	 *    ID
605
+	 *    start_date
606
+	 *    end_date
607
+	 *    event_name
608
+	 *    category_slug
609
+	 *    ticket_start
610
+	 *    ticket_end
611
+	 *    venue_title
612
+	 *    city
613
+	 *    state
614
+	 *    **IMPORTANT**
615
+	 *    make sure to also send the $orderby_params array to the posts_join_for_orderby() method
616
+	 *    or else some of the table references below will result in MySQL errors
617
+	 *
618
+	 * @param array  $orderby_params
619
+	 * @param string $sort
620
+	 * @return string
621
+	 * @throws EE_Error
622
+	 * @throws InvalidArgumentException
623
+	 * @throws InvalidDataTypeException
624
+	 * @throws InvalidInterfaceException
625
+	 */
626
+	public static function posts_orderby_sql(array $orderby_params = array(), $sort = 'ASC')
627
+	{
628
+		global $wpdb;
629
+		$SQL     = '';
630
+		$counter = 0;
631
+		$sort    = in_array($sort, array('ASC', 'asc', 'DESC', 'desc'), true)
632
+			? strtoupper($sort)
633
+			: 'ASC';
634
+		// make sure 'orderby' is set in query params
635
+		if (! isset(self::$_query_params['orderby'])) {
636
+			self::$_query_params['orderby'] = array();
637
+		}
638
+		// loop thru $orderby_params (type cast as array)
639
+		foreach ($orderby_params as $orderby) {
640
+			// check if we have already added this param
641
+			if (isset(self::$_query_params['orderby'][ $orderby ])) {
642
+				// if so then remove from the $orderby_params so that the count() method below is accurate
643
+				unset($orderby_params[ $orderby ]);
644
+				// then bump ahead to the next param
645
+				continue;
646
+			}
647
+			// this will ad a comma depending on whether this is the first or last param
648
+			$glue = $counter === 0 || $counter === count($orderby_params) ? ' ' : ', ';
649
+			// ok what's we dealing with?
650
+			switch ($orderby) {
651
+				case 'id':
652
+				case 'ID':
653
+					$SQL .= $glue . $wpdb->posts . '.ID ' . $sort;
654
+					break;
655
+				case 'end_date':
656
+					$SQL .= $glue . EEM_Datetime::instance()->table() . '.DTT_EVT_end ' . $sort;
657
+					break;
658
+				case 'event_name':
659
+					$SQL .= $glue . $wpdb->posts . '.post_title ' . $sort;
660
+					break;
661
+				case 'category_slug':
662
+					$SQL .= $glue . $wpdb->terms . '.slug ' . $sort;
663
+					break;
664
+				case 'ticket_start':
665
+					$SQL .= $glue . EEM_Ticket::instance()->table() . '.TKT_start_date ' . $sort;
666
+					break;
667
+				case 'ticket_end':
668
+					$SQL .= $glue . EEM_Ticket::instance()->table() . '.TKT_end_date ' . $sort;
669
+					break;
670
+				case 'venue_title':
671
+					$SQL .= $glue . 'venue_title ' . $sort;
672
+					break;
673
+				case 'city':
674
+					$SQL .= $glue . EEM_Venue::instance()->second_table() . '.VNU_city ' . $sort;
675
+					break;
676
+				case 'state':
677
+					$SQL .= $glue . EEM_State::instance()->table() . '.STA_name ' . $sort;
678
+					break;
679
+				case 'start_date':
680
+				default:
681
+					$SQL .= $glue . ' event_start_date ' . $sort;
682
+					break;
683
+			}
684
+			// add to array of orderby params that have been added
685
+			self::$_query_params['orderby'][ $orderby ] = true;
686
+			$counter++;
687
+		}
688
+		return $SQL;
689
+	}
690 690
 }
Please login to merge, or discard this patch.
Spacing   +40 added lines, -40 removed lines patch added patch discarded remove patch
@@ -227,7 +227,7 @@  discard block
 block discarded – undo
227 227
     {
228 228
         if (EEH_Event_Query::apply_query_filters($wp_query)) {
229 229
             global $wpdb;
230
-            $clauses['groupby'] = $wpdb->posts . '.ID ';
230
+            $clauses['groupby'] = $wpdb->posts.'.ID ';
231 231
         }
232 232
         return $clauses;
233 233
     }
@@ -264,23 +264,23 @@  discard block
 block discarded – undo
264 264
      */
265 265
     public static function posts_fields_sql_for_orderby(array $orderby_params = array())
266 266
     {
267
-        $SQL = ', MIN( ' . EEM_Datetime::instance()->table() . '.DTT_EVT_start ) as event_start_date ';
267
+        $SQL = ', MIN( '.EEM_Datetime::instance()->table().'.DTT_EVT_start ) as event_start_date ';
268 268
         foreach ($orderby_params as $orderby) {
269 269
             switch ($orderby) {
270 270
                 case 'ticket_start':
271
-                    $SQL .= ', ' . EEM_Ticket::instance()->table() . '.TKT_start_date';
271
+                    $SQL .= ', '.EEM_Ticket::instance()->table().'.TKT_start_date';
272 272
                     break;
273 273
                 case 'ticket_end':
274
-                    $SQL .= ', ' . EEM_Ticket::instance()->table() . '.TKT_end_date';
274
+                    $SQL .= ', '.EEM_Ticket::instance()->table().'.TKT_end_date';
275 275
                     break;
276 276
                 case 'venue_title':
277 277
                     $SQL .= ', Venue.post_title AS venue_title';
278 278
                     break;
279 279
                 case 'city':
280
-                    $SQL .= ', ' . EEM_Venue::instance()->second_table() . '.VNU_city';
280
+                    $SQL .= ', '.EEM_Venue::instance()->second_table().'.VNU_city';
281 281
                     break;
282 282
                 case 'state':
283
-                    $SQL .= ', ' . EEM_State::instance()->table() . '.STA_name';
283
+                    $SQL .= ', '.EEM_State::instance()->table().'.STA_name';
284 284
                     break;
285 285
             }
286 286
         }
@@ -322,12 +322,12 @@  discard block
 block discarded – undo
322 322
      */
323 323
     public static function posts_join_sql_for_show_expired($SQL = '', $show_expired = false)
324 324
     {
325
-        if (! $show_expired) {
326
-            $join = EEM_Event::instance()->table() . '.ID = ';
327
-            $join .= EEM_Datetime::instance()->table() . '.' . EEM_Event::instance()->primary_key_name();
325
+        if ( ! $show_expired) {
326
+            $join = EEM_Event::instance()->table().'.ID = ';
327
+            $join .= EEM_Datetime::instance()->table().'.'.EEM_Event::instance()->primary_key_name();
328 328
             // don't add if this is already in the SQL
329 329
             if (strpos($SQL, $join) === false) {
330
-                $SQL .= ' INNER JOIN ' . EEM_Datetime::instance()->table() . ' ON ( ' . $join . ' ) ';
330
+                $SQL .= ' INNER JOIN '.EEM_Datetime::instance()->table().' ON ( '.$join.' ) ';
331 331
             }
332 332
         }
333 333
         return $SQL;
@@ -343,7 +343,7 @@  discard block
 block discarded – undo
343 343
      */
344 344
     public static function posts_join_sql_for_terms($SQL = '', $join_terms = '')
345 345
     {
346
-        if (! empty($join_terms)) {
346
+        if ( ! empty($join_terms)) {
347 347
             global $wpdb;
348 348
             $SQL .= " LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)";
349 349
             $SQL .= " LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)";
@@ -373,13 +373,13 @@  discard block
 block discarded – undo
373 373
                 case 'ticket_end':
374 374
                     $SQL .= EEH_Event_Query::_posts_join_for_datetime(
375 375
                         $SQL,
376
-                        EEM_Datetime_Ticket::instance()->table() . '.' . EEM_Datetime::instance()->primary_key_name()
376
+                        EEM_Datetime_Ticket::instance()->table().'.'.EEM_Datetime::instance()->primary_key_name()
377 377
                     );
378
-                    $SQL .= ' LEFT JOIN ' . EEM_Ticket::instance()->table();
378
+                    $SQL .= ' LEFT JOIN '.EEM_Ticket::instance()->table();
379 379
                     $SQL .= ' ON (';
380
-                    $SQL .= EEM_Datetime_Ticket::instance()->table() . '.' . EEM_Ticket::instance()->primary_key_name();
380
+                    $SQL .= EEM_Datetime_Ticket::instance()->table().'.'.EEM_Ticket::instance()->primary_key_name();
381 381
                     $SQL .= ' = ';
382
-                    $SQL .= EEM_Ticket::instance()->table() . '.' . EEM_Ticket::instance()->primary_key_name();
382
+                    $SQL .= EEM_Ticket::instance()->table().'.'.EEM_Ticket::instance()->primary_key_name();
383 383
                     $SQL .= ' )';
384 384
                     break;
385 385
                 case 'venue_title':
@@ -392,7 +392,7 @@  discard block
 block discarded – undo
392 392
                     break;
393 393
                 case 'start_date':
394 394
                 default:
395
-                    $SQL .= EEH_Event_Query::_posts_join_for_datetime($SQL, EEM_Event::instance()->table() . '.ID');
395
+                    $SQL .= EEH_Event_Query::_posts_join_for_datetime($SQL, EEM_Event::instance()->table().'.ID');
396 396
                     break;
397 397
             }
398 398
         }
@@ -412,10 +412,10 @@  discard block
 block discarded – undo
412 412
      */
413 413
     protected static function _posts_join_for_datetime($SQL = '', $join = '')
414 414
     {
415
-        if (! empty($join)) {
416
-            $join .= ' = ' . EEM_Datetime::instance()->table() . '.' . EEM_Event::instance()->primary_key_name();
415
+        if ( ! empty($join)) {
416
+            $join .= ' = '.EEM_Datetime::instance()->table().'.'.EEM_Event::instance()->primary_key_name();
417 417
             if (strpos($SQL, $join) === false) {
418
-                return ' INNER JOIN ' . EEM_Datetime::instance()->table() . ' ON ( ' . $join . ' )';
418
+                return ' INNER JOIN '.EEM_Datetime::instance()->table().' ON ( '.$join.' )';
419 419
             }
420 420
         }
421 421
         return '';
@@ -436,8 +436,8 @@  discard block
 block discarded – undo
436 436
         // Event Venue table name
437 437
         $event_venue_table = EEM_Event_Venue::instance()->table();
438 438
         // generate conditions for:  Event <=> Event Venue  JOIN clause
439
-        $event_to_event_venue_join = EEM_Event::instance()->table() . '.ID = ';
440
-        $event_to_event_venue_join .= $event_venue_table . '.' . EEM_Event::instance()->primary_key_name();
439
+        $event_to_event_venue_join = EEM_Event::instance()->table().'.ID = ';
440
+        $event_to_event_venue_join .= $event_venue_table.'.'.EEM_Event::instance()->primary_key_name();
441 441
         // don't add joins if they have already been added
442 442
         if (strpos($SQL, $event_to_event_venue_join) === false) {
443 443
             // Venue table name
@@ -528,7 +528,7 @@  discard block
 block discarded – undo
528 528
     public static function posts_where_sql_for_show_expired($show_expired = false)
529 529
     {
530 530
         return ! $show_expired
531
-            ? ' AND ' . EEM_Datetime::instance()->table() . '.DTT_EVT_end > \'' . current_time('mysql', true) . '\' '
531
+            ? ' AND '.EEM_Datetime::instance()->table().'.DTT_EVT_end > \''.current_time('mysql', true).'\' '
532 532
             : '';
533 533
     }
534 534
 
@@ -541,7 +541,7 @@  discard block
 block discarded – undo
541 541
     public static function posts_where_sql_for_event_category_slug($event_category_slug = null)
542 542
     {
543 543
         global $wpdb;
544
-        if (! empty($event_category_slug)) {
544
+        if ( ! empty($event_category_slug)) {
545 545
             $event_category_slugs_array = array_map('trim', explode(',', $event_category_slug));
546 546
             $event_category_slugs_prepare = implode(', ', array_fill(0, count($event_category_slugs_array), '%s'));
547 547
             return $wpdb->prepare(" AND {$wpdb->terms}.slug IN ({$event_category_slugs_prepare}) ", $event_category_slugs_array);
@@ -562,14 +562,14 @@  discard block
 block discarded – undo
562 562
     public static function posts_where_sql_for_event_list_month($month = null)
563 563
     {
564 564
         $SQL = '';
565
-        if (! empty($month)) {
565
+        if ( ! empty($month)) {
566 566
             $datetime_table = EEM_Datetime::instance()->table();
567 567
             // event start date is LESS than the end of the month ( so nothing that doesn't start until next month )
568 568
             $SQL = " AND {$datetime_table}.DTT_EVT_start <= '";
569
-            $SQL .= date('Y-m-t 23:59:59', \EEH_DTT_Helper::first_of_month_timestamp($month)) . "'";
569
+            $SQL .= date('Y-m-t 23:59:59', \EEH_DTT_Helper::first_of_month_timestamp($month))."'";
570 570
             // event end date is GREATER than the start of the month ( so nothing that ended before this month )
571 571
             $SQL .= " AND {$datetime_table}.DTT_EVT_end >= '";
572
-            $SQL .= date('Y-m-01 0:0:00', \EEH_DTT_Helper::first_of_month_timestamp($month)) . "' ";
572
+            $SQL .= date('Y-m-01 0:0:00', \EEH_DTT_Helper::first_of_month_timestamp($month))."' ";
573 573
         }
574 574
         return $SQL;
575 575
     }
@@ -632,15 +632,15 @@  discard block
 block discarded – undo
632 632
             ? strtoupper($sort)
633 633
             : 'ASC';
634 634
         // make sure 'orderby' is set in query params
635
-        if (! isset(self::$_query_params['orderby'])) {
635
+        if ( ! isset(self::$_query_params['orderby'])) {
636 636
             self::$_query_params['orderby'] = array();
637 637
         }
638 638
         // loop thru $orderby_params (type cast as array)
639 639
         foreach ($orderby_params as $orderby) {
640 640
             // check if we have already added this param
641
-            if (isset(self::$_query_params['orderby'][ $orderby ])) {
641
+            if (isset(self::$_query_params['orderby'][$orderby])) {
642 642
                 // if so then remove from the $orderby_params so that the count() method below is accurate
643
-                unset($orderby_params[ $orderby ]);
643
+                unset($orderby_params[$orderby]);
644 644
                 // then bump ahead to the next param
645 645
                 continue;
646 646
             }
@@ -650,39 +650,39 @@  discard block
 block discarded – undo
650 650
             switch ($orderby) {
651 651
                 case 'id':
652 652
                 case 'ID':
653
-                    $SQL .= $glue . $wpdb->posts . '.ID ' . $sort;
653
+                    $SQL .= $glue.$wpdb->posts.'.ID '.$sort;
654 654
                     break;
655 655
                 case 'end_date':
656
-                    $SQL .= $glue . EEM_Datetime::instance()->table() . '.DTT_EVT_end ' . $sort;
656
+                    $SQL .= $glue.EEM_Datetime::instance()->table().'.DTT_EVT_end '.$sort;
657 657
                     break;
658 658
                 case 'event_name':
659
-                    $SQL .= $glue . $wpdb->posts . '.post_title ' . $sort;
659
+                    $SQL .= $glue.$wpdb->posts.'.post_title '.$sort;
660 660
                     break;
661 661
                 case 'category_slug':
662
-                    $SQL .= $glue . $wpdb->terms . '.slug ' . $sort;
662
+                    $SQL .= $glue.$wpdb->terms.'.slug '.$sort;
663 663
                     break;
664 664
                 case 'ticket_start':
665
-                    $SQL .= $glue . EEM_Ticket::instance()->table() . '.TKT_start_date ' . $sort;
665
+                    $SQL .= $glue.EEM_Ticket::instance()->table().'.TKT_start_date '.$sort;
666 666
                     break;
667 667
                 case 'ticket_end':
668
-                    $SQL .= $glue . EEM_Ticket::instance()->table() . '.TKT_end_date ' . $sort;
668
+                    $SQL .= $glue.EEM_Ticket::instance()->table().'.TKT_end_date '.$sort;
669 669
                     break;
670 670
                 case 'venue_title':
671
-                    $SQL .= $glue . 'venue_title ' . $sort;
671
+                    $SQL .= $glue.'venue_title '.$sort;
672 672
                     break;
673 673
                 case 'city':
674
-                    $SQL .= $glue . EEM_Venue::instance()->second_table() . '.VNU_city ' . $sort;
674
+                    $SQL .= $glue.EEM_Venue::instance()->second_table().'.VNU_city '.$sort;
675 675
                     break;
676 676
                 case 'state':
677
-                    $SQL .= $glue . EEM_State::instance()->table() . '.STA_name ' . $sort;
677
+                    $SQL .= $glue.EEM_State::instance()->table().'.STA_name '.$sort;
678 678
                     break;
679 679
                 case 'start_date':
680 680
                 default:
681
-                    $SQL .= $glue . ' event_start_date ' . $sort;
681
+                    $SQL .= $glue.' event_start_date '.$sort;
682 682
                     break;
683 683
             }
684 684
             // add to array of orderby params that have been added
685
-            self::$_query_params['orderby'][ $orderby ] = true;
685
+            self::$_query_params['orderby'][$orderby] = true;
686 686
             $counter++;
687 687
         }
688 688
         return $SQL;
Please login to merge, or discard this patch.