@@ -74,9 +74,9 @@ discard block |
||
74 | 74 | public function statsCallback() |
75 | 75 | { |
76 | 76 | // returns a callback that can is used to retrieve the stats to send along to the pue server. |
77 | - return function () { |
|
77 | + return function() { |
|
78 | 78 | // we only send stats one a week, so let's see if our stat timestamp has expired. |
79 | - if (! $this->sendStats()) { |
|
79 | + if ( ! $this->sendStats()) { |
|
80 | 80 | return array(); |
81 | 81 | } |
82 | 82 | return $this->stats_gatherer->stats(); |
@@ -124,9 +124,9 @@ discard block |
||
124 | 124 | */ |
125 | 125 | public static function optinText($extra = true) |
126 | 126 | { |
127 | - if (! $extra) { |
|
127 | + if ( ! $extra) { |
|
128 | 128 | echo '<h2 class="ee-admin-settings-hdr" ' |
129 | - . (! $extra ? 'id="UXIP_settings"' : '') |
|
129 | + . ( ! $extra ? 'id="UXIP_settings"' : '') |
|
130 | 130 | . '>' |
131 | 131 | . esc_html__('User eXperience Improvement Program (UXIP)', 'event_espresso') |
132 | 132 | . EEH_Template::get_help_tab_link('organization_logo_info') |
@@ -157,7 +157,7 @@ discard block |
||
157 | 157 | ), |
158 | 158 | '<a href="https://eventespresso.com/about/user-experience-improvement-program-uxip/" target="_blank">', |
159 | 159 | '</a>', |
160 | - '<a href="' . $settings_url . '" target="_blank">', |
|
160 | + '<a href="'.$settings_url.'" target="_blank">', |
|
161 | 161 | '</a>' |
162 | 162 | ); |
163 | 163 | } |
@@ -171,14 +171,14 @@ discard block |
||
171 | 171 | { |
172 | 172 | wp_register_script( |
173 | 173 | 'ee-data-optin-js', |
174 | - EE_GLOBAL_ASSETS_URL . 'scripts/ee-data-optin.js', |
|
174 | + EE_GLOBAL_ASSETS_URL.'scripts/ee-data-optin.js', |
|
175 | 175 | array('jquery'), |
176 | 176 | EVENT_ESPRESSO_VERSION, |
177 | 177 | true |
178 | 178 | ); |
179 | 179 | wp_register_style( |
180 | 180 | 'ee-data-optin-css', |
181 | - EE_GLOBAL_ASSETS_URL . 'css/ee-data-optin.css', |
|
181 | + EE_GLOBAL_ASSETS_URL.'css/ee-data-optin.css', |
|
182 | 182 | array(), |
183 | 183 | EVENT_ESPRESSO_VERSION |
184 | 184 | ); |
@@ -197,7 +197,7 @@ discard block |
||
197 | 197 | $request = LoaderFactory::getLoader()->getShared(RequestInterface::class); |
198 | 198 | $nonce = $request->getRequestParam('nonce'); |
199 | 199 | // verify nonce |
200 | - if (! $nonce || ! wp_verify_nonce($nonce, 'ee-data-optin')) { |
|
200 | + if ( ! $nonce || ! wp_verify_nonce($nonce, 'ee-data-optin')) { |
|
201 | 201 | exit(); |
202 | 202 | } |
203 | 203 |
@@ -21,86 +21,86 @@ discard block |
||
21 | 21 | */ |
22 | 22 | class Stats |
23 | 23 | { |
24 | - const OPTIONS_KEY_EXPIRY_TIMESTAMP_FOR_SENDING_STATS = 'ee_uxip_stats_expiry'; |
|
25 | - |
|
26 | - /** |
|
27 | - * @var Config |
|
28 | - */ |
|
29 | - private $config; |
|
30 | - |
|
31 | - |
|
32 | - /** |
|
33 | - * @var StatsGatherer |
|
34 | - */ |
|
35 | - private $stats_gatherer; |
|
36 | - |
|
37 | - |
|
38 | - /** |
|
39 | - * @var EE_Maintenance_Mode |
|
40 | - */ |
|
41 | - private $maintenance_mode; |
|
42 | - |
|
43 | - public function __construct( |
|
44 | - Config $config, |
|
45 | - EE_Maintenance_Mode $maintenance_mode, |
|
46 | - StatsGatherer $stats_gatherer |
|
47 | - ) { |
|
48 | - $this->config = $config; |
|
49 | - $this->maintenance_mode = $maintenance_mode; |
|
50 | - $this->stats_gatherer = $stats_gatherer; |
|
51 | - $this->setUxipNotices(); |
|
52 | - } |
|
53 | - |
|
54 | - |
|
55 | - /** |
|
56 | - * Displays uxip opt-in notice if necessary. |
|
57 | - */ |
|
58 | - private function setUxipNotices() |
|
59 | - { |
|
60 | - if ($this->canDisplayNotices()) { |
|
61 | - add_action('admin_notices', array($this, 'optinNotice')); |
|
62 | - add_action('admin_enqueue_scripts', array($this, 'enqueueScripts')); |
|
63 | - add_action('wp_ajax_espresso_data_optin', array($this, 'ajaxHandler')); |
|
64 | - } |
|
65 | - } |
|
66 | - |
|
67 | - |
|
68 | - /** |
|
69 | - * This returns the callback that PluginUpdateEngineChecker will use for getting any extra stats to send. |
|
70 | - * |
|
71 | - * @return Closure |
|
72 | - */ |
|
73 | - public function statsCallback() |
|
74 | - { |
|
75 | - // returns a callback that can is used to retrieve the stats to send along to the pue server. |
|
76 | - return function () { |
|
77 | - // we only send stats one a week, so let's see if our stat timestamp has expired. |
|
78 | - if (! $this->sendStats()) { |
|
79 | - return array(); |
|
80 | - } |
|
81 | - return $this->stats_gatherer->stats(); |
|
82 | - }; |
|
83 | - } |
|
84 | - |
|
85 | - |
|
86 | - /** |
|
87 | - * Return whether notices can be displayed or not |
|
88 | - * |
|
89 | - * @return bool |
|
90 | - */ |
|
91 | - private function canDisplayNotices() |
|
92 | - { |
|
93 | - return ! $this->config->hasNotifiedForUxip() |
|
94 | - && $this->maintenance_mode->level() !== EE_Maintenance_Mode::level_2_complete_maintenance; |
|
95 | - } |
|
96 | - |
|
97 | - |
|
98 | - /** |
|
99 | - * Callback for the admin_notices hook that outputs the UXIP optin-in notice. |
|
100 | - */ |
|
101 | - public function optinNotice() |
|
102 | - { |
|
103 | - ?> |
|
24 | + const OPTIONS_KEY_EXPIRY_TIMESTAMP_FOR_SENDING_STATS = 'ee_uxip_stats_expiry'; |
|
25 | + |
|
26 | + /** |
|
27 | + * @var Config |
|
28 | + */ |
|
29 | + private $config; |
|
30 | + |
|
31 | + |
|
32 | + /** |
|
33 | + * @var StatsGatherer |
|
34 | + */ |
|
35 | + private $stats_gatherer; |
|
36 | + |
|
37 | + |
|
38 | + /** |
|
39 | + * @var EE_Maintenance_Mode |
|
40 | + */ |
|
41 | + private $maintenance_mode; |
|
42 | + |
|
43 | + public function __construct( |
|
44 | + Config $config, |
|
45 | + EE_Maintenance_Mode $maintenance_mode, |
|
46 | + StatsGatherer $stats_gatherer |
|
47 | + ) { |
|
48 | + $this->config = $config; |
|
49 | + $this->maintenance_mode = $maintenance_mode; |
|
50 | + $this->stats_gatherer = $stats_gatherer; |
|
51 | + $this->setUxipNotices(); |
|
52 | + } |
|
53 | + |
|
54 | + |
|
55 | + /** |
|
56 | + * Displays uxip opt-in notice if necessary. |
|
57 | + */ |
|
58 | + private function setUxipNotices() |
|
59 | + { |
|
60 | + if ($this->canDisplayNotices()) { |
|
61 | + add_action('admin_notices', array($this, 'optinNotice')); |
|
62 | + add_action('admin_enqueue_scripts', array($this, 'enqueueScripts')); |
|
63 | + add_action('wp_ajax_espresso_data_optin', array($this, 'ajaxHandler')); |
|
64 | + } |
|
65 | + } |
|
66 | + |
|
67 | + |
|
68 | + /** |
|
69 | + * This returns the callback that PluginUpdateEngineChecker will use for getting any extra stats to send. |
|
70 | + * |
|
71 | + * @return Closure |
|
72 | + */ |
|
73 | + public function statsCallback() |
|
74 | + { |
|
75 | + // returns a callback that can is used to retrieve the stats to send along to the pue server. |
|
76 | + return function () { |
|
77 | + // we only send stats one a week, so let's see if our stat timestamp has expired. |
|
78 | + if (! $this->sendStats()) { |
|
79 | + return array(); |
|
80 | + } |
|
81 | + return $this->stats_gatherer->stats(); |
|
82 | + }; |
|
83 | + } |
|
84 | + |
|
85 | + |
|
86 | + /** |
|
87 | + * Return whether notices can be displayed or not |
|
88 | + * |
|
89 | + * @return bool |
|
90 | + */ |
|
91 | + private function canDisplayNotices() |
|
92 | + { |
|
93 | + return ! $this->config->hasNotifiedForUxip() |
|
94 | + && $this->maintenance_mode->level() !== EE_Maintenance_Mode::level_2_complete_maintenance; |
|
95 | + } |
|
96 | + |
|
97 | + |
|
98 | + /** |
|
99 | + * Callback for the admin_notices hook that outputs the UXIP optin-in notice. |
|
100 | + */ |
|
101 | + public function optinNotice() |
|
102 | + { |
|
103 | + ?> |
|
104 | 104 | <div class="updated data-collect-optin" id="espresso-data-collect-optin-container"> |
105 | 105 | <div id="data-collect-optin-options-container"> |
106 | 106 | <span class="dashicons dashicons-admin-site"></span> |
@@ -113,128 +113,128 @@ discard block |
||
113 | 113 | </div> |
114 | 114 | </div> |
115 | 115 | <?php |
116 | - } |
|
117 | - |
|
118 | - |
|
119 | - /** |
|
120 | - * Retrieves the optin text (static so it can be used in multiple places as necessary). |
|
121 | - * |
|
122 | - * @param bool $extra |
|
123 | - */ |
|
124 | - public static function optinText($extra = true) |
|
125 | - { |
|
126 | - if (! $extra) { |
|
127 | - echo '<h2 class="ee-admin-settings-hdr" ' |
|
128 | - . (! $extra ? 'id="UXIP_settings"' : '') |
|
129 | - . '>' |
|
130 | - . esc_html__('User eXperience Improvement Program (UXIP)', 'event_espresso') |
|
131 | - . EEH_Template::get_help_tab_link('organization_logo_info') |
|
132 | - . '</h2>'; |
|
133 | - printf( |
|
134 | - esc_html__( |
|
135 | - '%1$sPlease help us make Event Espresso better and vote for your favorite features.%2$s The %3$sUser eXperience Improvement Program (UXIP)%4$s, has been created so when you use Event Espresso you are voting for the features and settings that are important to you. The UXIP helps us understand how you use our products and services, track problems and in what context. If you opt-out of the UXIP you essentially elect for us to disregard how you use Event Espresso as we build new features and make changes. Participation in the program is completely voluntary and it is disabled by default. The end results of the UXIP are software improvements to better meet your needs. The data we collect will never be sold, traded, or misused in any way. %5$sPlease see our %6$sPrivacy Policy%7$s for more information.', |
|
136 | - 'event_espresso' |
|
137 | - ), |
|
138 | - '<p><em>', |
|
139 | - '</em></p>', |
|
140 | - '<a href="https://eventespresso.com/about/user-experience-improvement-program-uxip/" target="_blank">', |
|
141 | - '</a>', |
|
142 | - '<br><br>', |
|
143 | - '<a href="https://eventespresso.com/about/privacy-policy/" target="_blank">', |
|
144 | - '</a>' |
|
145 | - ); |
|
146 | - } else { |
|
147 | - $settings_url = EEH_URL::add_query_args_and_nonce( |
|
148 | - array('action' => 'default'), |
|
149 | - admin_url('admin.php?page=espresso_general_settings') |
|
150 | - ); |
|
151 | - $settings_url .= '#UXIP_settings'; |
|
152 | - printf( |
|
153 | - esc_html__( |
|
154 | - 'The Event Espresso UXIP feature is not yet active on your site. For %1$smore info%2$s and to opt-in %3$sclick here%4$s.', |
|
155 | - 'event_espresso' |
|
156 | - ), |
|
157 | - '<a href="https://eventespresso.com/about/user-experience-improvement-program-uxip/" target="_blank">', |
|
158 | - '</a>', |
|
159 | - '<a href="' . $settings_url . '" target="_blank">', |
|
160 | - '</a>' |
|
161 | - ); |
|
162 | - } |
|
163 | - } |
|
164 | - |
|
165 | - |
|
166 | - /** |
|
167 | - * Callback for admin_enqueue_scripts that sets up the scripts and styles for the uxip notice |
|
168 | - */ |
|
169 | - public function enqueueScripts() |
|
170 | - { |
|
171 | - wp_register_script( |
|
172 | - 'ee-data-optin-js', |
|
173 | - EE_GLOBAL_ASSETS_URL . 'scripts/ee-data-optin.js', |
|
174 | - array('jquery'), |
|
175 | - EVENT_ESPRESSO_VERSION, |
|
176 | - true |
|
177 | - ); |
|
178 | - wp_register_style( |
|
179 | - 'ee-data-optin-css', |
|
180 | - EE_GLOBAL_ASSETS_URL . 'css/ee-data-optin.css', |
|
181 | - array(), |
|
182 | - EVENT_ESPRESSO_VERSION |
|
183 | - ); |
|
184 | - |
|
185 | - wp_enqueue_script('ee-data-optin-js'); |
|
186 | - wp_enqueue_style('ee-data-optin-css'); |
|
187 | - } |
|
188 | - |
|
189 | - |
|
190 | - /** |
|
191 | - * Callback for wp_ajax_espresso_data_optin that handles the ajax request |
|
192 | - */ |
|
193 | - public function ajaxHandler() |
|
194 | - { |
|
195 | - /** @var RequestInterface $request */ |
|
196 | - $request = LoaderFactory::getLoader()->getShared(RequestInterface::class); |
|
197 | - $nonce = $request->getRequestParam('nonce'); |
|
198 | - // verify nonce |
|
199 | - if (! $nonce || ! wp_verify_nonce($nonce, 'ee-data-optin')) { |
|
200 | - exit(); |
|
201 | - } |
|
202 | - |
|
203 | - // update has notified option |
|
204 | - $this->config->setHasNotifiedAboutUxip(); |
|
205 | - exit(); |
|
206 | - } |
|
207 | - |
|
208 | - |
|
209 | - /** |
|
210 | - * Used to determine whether additional stats are sent. |
|
211 | - */ |
|
212 | - private function sendStats() |
|
213 | - { |
|
214 | - return $this->config->isOptedInForUxip() |
|
215 | - && $this->maintenance_mode->level() !== EE_Maintenance_Mode::level_2_complete_maintenance |
|
216 | - && $this->statSendTimestampExpired(); |
|
217 | - } |
|
218 | - |
|
219 | - |
|
220 | - /** |
|
221 | - * Returns true when the timestamp used to track whether stats get sent (currently a weekly interval) is expired. |
|
222 | - * Returns false otherwise. |
|
223 | - * |
|
224 | - * @return bool |
|
225 | - */ |
|
226 | - private function statSendTimestampExpired() |
|
227 | - { |
|
228 | - $current_expiry = get_option(self::OPTIONS_KEY_EXPIRY_TIMESTAMP_FOR_SENDING_STATS, null); |
|
229 | - if ($current_expiry === null) { |
|
230 | - add_option(self::OPTIONS_KEY_EXPIRY_TIMESTAMP_FOR_SENDING_STATS, time() + WEEK_IN_SECONDS, '', 'no'); |
|
231 | - return true; |
|
232 | - } |
|
233 | - |
|
234 | - if (time() > (int) $current_expiry) { |
|
235 | - update_option(self::OPTIONS_KEY_EXPIRY_TIMESTAMP_FOR_SENDING_STATS, time() + WEEK_IN_SECONDS); |
|
236 | - return true; |
|
237 | - } |
|
238 | - return false; |
|
239 | - } |
|
116 | + } |
|
117 | + |
|
118 | + |
|
119 | + /** |
|
120 | + * Retrieves the optin text (static so it can be used in multiple places as necessary). |
|
121 | + * |
|
122 | + * @param bool $extra |
|
123 | + */ |
|
124 | + public static function optinText($extra = true) |
|
125 | + { |
|
126 | + if (! $extra) { |
|
127 | + echo '<h2 class="ee-admin-settings-hdr" ' |
|
128 | + . (! $extra ? 'id="UXIP_settings"' : '') |
|
129 | + . '>' |
|
130 | + . esc_html__('User eXperience Improvement Program (UXIP)', 'event_espresso') |
|
131 | + . EEH_Template::get_help_tab_link('organization_logo_info') |
|
132 | + . '</h2>'; |
|
133 | + printf( |
|
134 | + esc_html__( |
|
135 | + '%1$sPlease help us make Event Espresso better and vote for your favorite features.%2$s The %3$sUser eXperience Improvement Program (UXIP)%4$s, has been created so when you use Event Espresso you are voting for the features and settings that are important to you. The UXIP helps us understand how you use our products and services, track problems and in what context. If you opt-out of the UXIP you essentially elect for us to disregard how you use Event Espresso as we build new features and make changes. Participation in the program is completely voluntary and it is disabled by default. The end results of the UXIP are software improvements to better meet your needs. The data we collect will never be sold, traded, or misused in any way. %5$sPlease see our %6$sPrivacy Policy%7$s for more information.', |
|
136 | + 'event_espresso' |
|
137 | + ), |
|
138 | + '<p><em>', |
|
139 | + '</em></p>', |
|
140 | + '<a href="https://eventespresso.com/about/user-experience-improvement-program-uxip/" target="_blank">', |
|
141 | + '</a>', |
|
142 | + '<br><br>', |
|
143 | + '<a href="https://eventespresso.com/about/privacy-policy/" target="_blank">', |
|
144 | + '</a>' |
|
145 | + ); |
|
146 | + } else { |
|
147 | + $settings_url = EEH_URL::add_query_args_and_nonce( |
|
148 | + array('action' => 'default'), |
|
149 | + admin_url('admin.php?page=espresso_general_settings') |
|
150 | + ); |
|
151 | + $settings_url .= '#UXIP_settings'; |
|
152 | + printf( |
|
153 | + esc_html__( |
|
154 | + 'The Event Espresso UXIP feature is not yet active on your site. For %1$smore info%2$s and to opt-in %3$sclick here%4$s.', |
|
155 | + 'event_espresso' |
|
156 | + ), |
|
157 | + '<a href="https://eventespresso.com/about/user-experience-improvement-program-uxip/" target="_blank">', |
|
158 | + '</a>', |
|
159 | + '<a href="' . $settings_url . '" target="_blank">', |
|
160 | + '</a>' |
|
161 | + ); |
|
162 | + } |
|
163 | + } |
|
164 | + |
|
165 | + |
|
166 | + /** |
|
167 | + * Callback for admin_enqueue_scripts that sets up the scripts and styles for the uxip notice |
|
168 | + */ |
|
169 | + public function enqueueScripts() |
|
170 | + { |
|
171 | + wp_register_script( |
|
172 | + 'ee-data-optin-js', |
|
173 | + EE_GLOBAL_ASSETS_URL . 'scripts/ee-data-optin.js', |
|
174 | + array('jquery'), |
|
175 | + EVENT_ESPRESSO_VERSION, |
|
176 | + true |
|
177 | + ); |
|
178 | + wp_register_style( |
|
179 | + 'ee-data-optin-css', |
|
180 | + EE_GLOBAL_ASSETS_URL . 'css/ee-data-optin.css', |
|
181 | + array(), |
|
182 | + EVENT_ESPRESSO_VERSION |
|
183 | + ); |
|
184 | + |
|
185 | + wp_enqueue_script('ee-data-optin-js'); |
|
186 | + wp_enqueue_style('ee-data-optin-css'); |
|
187 | + } |
|
188 | + |
|
189 | + |
|
190 | + /** |
|
191 | + * Callback for wp_ajax_espresso_data_optin that handles the ajax request |
|
192 | + */ |
|
193 | + public function ajaxHandler() |
|
194 | + { |
|
195 | + /** @var RequestInterface $request */ |
|
196 | + $request = LoaderFactory::getLoader()->getShared(RequestInterface::class); |
|
197 | + $nonce = $request->getRequestParam('nonce'); |
|
198 | + // verify nonce |
|
199 | + if (! $nonce || ! wp_verify_nonce($nonce, 'ee-data-optin')) { |
|
200 | + exit(); |
|
201 | + } |
|
202 | + |
|
203 | + // update has notified option |
|
204 | + $this->config->setHasNotifiedAboutUxip(); |
|
205 | + exit(); |
|
206 | + } |
|
207 | + |
|
208 | + |
|
209 | + /** |
|
210 | + * Used to determine whether additional stats are sent. |
|
211 | + */ |
|
212 | + private function sendStats() |
|
213 | + { |
|
214 | + return $this->config->isOptedInForUxip() |
|
215 | + && $this->maintenance_mode->level() !== EE_Maintenance_Mode::level_2_complete_maintenance |
|
216 | + && $this->statSendTimestampExpired(); |
|
217 | + } |
|
218 | + |
|
219 | + |
|
220 | + /** |
|
221 | + * Returns true when the timestamp used to track whether stats get sent (currently a weekly interval) is expired. |
|
222 | + * Returns false otherwise. |
|
223 | + * |
|
224 | + * @return bool |
|
225 | + */ |
|
226 | + private function statSendTimestampExpired() |
|
227 | + { |
|
228 | + $current_expiry = get_option(self::OPTIONS_KEY_EXPIRY_TIMESTAMP_FOR_SENDING_STATS, null); |
|
229 | + if ($current_expiry === null) { |
|
230 | + add_option(self::OPTIONS_KEY_EXPIRY_TIMESTAMP_FOR_SENDING_STATS, time() + WEEK_IN_SECONDS, '', 'no'); |
|
231 | + return true; |
|
232 | + } |
|
233 | + |
|
234 | + if (time() > (int) $current_expiry) { |
|
235 | + update_option(self::OPTIONS_KEY_EXPIRY_TIMESTAMP_FOR_SENDING_STATS, time() + WEEK_IN_SECONDS); |
|
236 | + return true; |
|
237 | + } |
|
238 | + return false; |
|
239 | + } |
|
240 | 240 | } |
@@ -48,7 +48,7 @@ discard block |
||
48 | 48 | */ |
49 | 49 | public static function instance() |
50 | 50 | { |
51 | - if (! self::$_instance instanceof EE_Log) { |
|
51 | + if ( ! self::$_instance instanceof EE_Log) { |
|
52 | 52 | self::$_instance = new self(); |
53 | 53 | } |
54 | 54 | return self::$_instance; |
@@ -61,7 +61,7 @@ discard block |
||
61 | 61 | private function __construct() |
62 | 62 | { |
63 | 63 | |
64 | - if (! EE_Registry::instance()->CFG->admin->use_remote_logging) { |
|
64 | + if ( ! EE_Registry::instance()->CFG->admin->use_remote_logging) { |
|
65 | 65 | return; |
66 | 66 | } |
67 | 67 | |
@@ -105,14 +105,14 @@ discard block |
||
105 | 105 | */ |
106 | 106 | private function _format_message($file = '', $function = '', $message = '', $type = '') |
107 | 107 | { |
108 | - $msg = '----------------------------------------------------------------------------------------' . PHP_EOL; |
|
109 | - $msg .= '[' . current_time('mysql') . '] '; |
|
108 | + $msg = '----------------------------------------------------------------------------------------'.PHP_EOL; |
|
109 | + $msg .= '['.current_time('mysql').'] '; |
|
110 | 110 | $msg .= ! empty($file) ? basename($file) : ''; |
111 | 111 | $msg .= ! empty($file) && ! empty($function) ? ' -> ' : ''; |
112 | - $msg .= ! empty($function) ? $function . '()' : ''; |
|
112 | + $msg .= ! empty($function) ? $function.'()' : ''; |
|
113 | 113 | $msg .= PHP_EOL; |
114 | 114 | $type = ! empty($type) ? $type : 'log message'; |
115 | - $msg .= ! empty($message) ? "\t" . '[' . $type . '] ' . $message . PHP_EOL : ''; |
|
115 | + $msg .= ! empty($message) ? "\t".'['.$type.'] '.$message.PHP_EOL : ''; |
|
116 | 116 | return $msg; |
117 | 117 | } |
118 | 118 | |
@@ -164,18 +164,18 @@ discard block |
||
164 | 164 | |
165 | 165 | /** @var RequestInterface $request */ |
166 | 166 | $request = LoaderFactory::getLoader()->getShared(RequestInterface::class); |
167 | - $data = 'domain=' . $request->getServerParam('HTTP_HOST'); |
|
168 | - $data .= '&ip=' . $request->getServerParam('SERVER_ADDR'); |
|
169 | - $data .= '&server_type=' . $request->getServerParam('SERVER_SOFTWARE'); |
|
170 | - $data .= '&time=' . time(); |
|
171 | - $data .= '&remote_log=' . $this->_log; |
|
167 | + $data = 'domain='.$request->getServerParam('HTTP_HOST'); |
|
168 | + $data .= '&ip='.$request->getServerParam('SERVER_ADDR'); |
|
169 | + $data .= '&server_type='.$request->getServerParam('SERVER_SOFTWARE'); |
|
170 | + $data .= '&time='.time(); |
|
171 | + $data .= '&remote_log='.$this->_log; |
|
172 | 172 | $data .= '&action=save'; |
173 | 173 | |
174 | 174 | if (defined('EELOGGING_PASS')) { |
175 | - $data .= '&pass=' . EELOGGING_PASS; |
|
175 | + $data .= '&pass='.EELOGGING_PASS; |
|
176 | 176 | } |
177 | 177 | if (defined('EELOGGING_KEY')) { |
178 | - $data .= '&key=' . EELOGGING_KEY; |
|
178 | + $data .= '&key='.EELOGGING_KEY; |
|
179 | 179 | } |
180 | 180 | |
181 | 181 | $c = curl_init($this->_remote_logging_url); |
@@ -19,197 +19,197 @@ |
||
19 | 19 | */ |
20 | 20 | class EE_Log |
21 | 21 | { |
22 | - /** |
|
23 | - * @var string |
|
24 | - */ |
|
25 | - private $_log = ''; |
|
26 | - |
|
27 | - /** |
|
28 | - * Used for remote logging |
|
29 | - * |
|
30 | - * @var string |
|
31 | - */ |
|
32 | - private $_remote_logging_url = ''; |
|
33 | - |
|
34 | - /** |
|
35 | - * @var string |
|
36 | - */ |
|
37 | - private $_remote_log = ''; |
|
38 | - |
|
39 | - /** |
|
40 | - * @var EE_Log |
|
41 | - */ |
|
42 | - private static $_instance; |
|
43 | - |
|
44 | - |
|
45 | - /** |
|
46 | - * @return EE_Log |
|
47 | - */ |
|
48 | - public static function instance() |
|
49 | - { |
|
50 | - if (! self::$_instance instanceof EE_Log) { |
|
51 | - self::$_instance = new self(); |
|
52 | - } |
|
53 | - return self::$_instance; |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * @access private |
|
58 | - * @return EE_Log |
|
59 | - */ |
|
60 | - private function __construct() |
|
61 | - { |
|
62 | - |
|
63 | - if (! EE_Registry::instance()->CFG->admin->use_remote_logging) { |
|
64 | - return; |
|
65 | - } |
|
66 | - |
|
67 | - $this->_remote_logging_url = EE_Registry::instance()->CFG->admin->remote_logging_url; |
|
68 | - $this->_remote_log = ''; |
|
69 | - |
|
70 | - if (EE_Registry::instance()->CFG->admin->use_remote_logging) { |
|
71 | - add_action('shutdown', array($this, 'send_log'), 9999); |
|
72 | - } |
|
73 | - } |
|
74 | - |
|
75 | - |
|
76 | - /** |
|
77 | - * verify_filesystem |
|
78 | - * tests that the required files and folders exist and are writable |
|
79 | - * |
|
80 | - */ |
|
81 | - public function verify_filesystem() |
|
82 | - { |
|
83 | - $msg = esc_html__( |
|
84 | - 'The Local File Logging functionality was removed permanently. Remote Logging is recommended instead.', |
|
85 | - 'event_espresso' |
|
86 | - ); |
|
87 | - EE_Error::doing_it_wrong( |
|
88 | - __METHOD__, |
|
89 | - $msg, |
|
90 | - '4.10.1.p' |
|
91 | - ); |
|
92 | - } |
|
93 | - |
|
94 | - |
|
95 | - /** |
|
96 | - * _format_message |
|
97 | - * makes yer log entries look all purdy |
|
98 | - * |
|
99 | - * @param string $file |
|
100 | - * @param string $function |
|
101 | - * @param string $message |
|
102 | - * @param string $type |
|
103 | - * @return string |
|
104 | - */ |
|
105 | - private function _format_message($file = '', $function = '', $message = '', $type = '') |
|
106 | - { |
|
107 | - $msg = '----------------------------------------------------------------------------------------' . PHP_EOL; |
|
108 | - $msg .= '[' . current_time('mysql') . '] '; |
|
109 | - $msg .= ! empty($file) ? basename($file) : ''; |
|
110 | - $msg .= ! empty($file) && ! empty($function) ? ' -> ' : ''; |
|
111 | - $msg .= ! empty($function) ? $function . '()' : ''; |
|
112 | - $msg .= PHP_EOL; |
|
113 | - $type = ! empty($type) ? $type : 'log message'; |
|
114 | - $msg .= ! empty($message) ? "\t" . '[' . $type . '] ' . $message . PHP_EOL : ''; |
|
115 | - return $msg; |
|
116 | - } |
|
117 | - |
|
118 | - |
|
119 | - /** |
|
120 | - * log |
|
121 | - * adds content to the EE_Log->_log property which gets written to file during the WP 'shutdown' hookpoint via the |
|
122 | - * EE_Log::write_log() callback |
|
123 | - * |
|
124 | - * @param string $file |
|
125 | - * @param string $function |
|
126 | - * @param string $message |
|
127 | - * @param string $type |
|
128 | - */ |
|
129 | - public function log($file = '', $function = '', $message = '', $type = '') |
|
130 | - { |
|
131 | - $this->_log .= $this->_format_message($file, $function, $message, $type); |
|
132 | - } |
|
133 | - |
|
134 | - |
|
135 | - /** |
|
136 | - * write_log |
|
137 | - * appends the results of the 'AHEE_log' filter to the espresso log file |
|
138 | - */ |
|
139 | - public function write_log() |
|
140 | - { |
|
141 | - $msg = esc_html__( |
|
142 | - 'The Local File Logging functionality was removed permanently. Remote Logging is recommended instead.', |
|
143 | - 'event_espresso' |
|
144 | - ); |
|
145 | - EE_Error::doing_it_wrong( |
|
146 | - __METHOD__, |
|
147 | - $msg, |
|
148 | - '4.10.1.p' |
|
149 | - ); |
|
150 | - } |
|
151 | - |
|
152 | - |
|
153 | - /** |
|
154 | - * send_log |
|
155 | - * sends the espresso log to a remote URL via a PHP cURL request |
|
156 | - */ |
|
157 | - public function send_log() |
|
158 | - { |
|
159 | - |
|
160 | - if (empty($this->_remote_logging_url)) { |
|
161 | - return; |
|
162 | - } |
|
163 | - |
|
164 | - /** @var RequestInterface $request */ |
|
165 | - $request = LoaderFactory::getLoader()->getShared(RequestInterface::class); |
|
166 | - $data = 'domain=' . $request->getServerParam('HTTP_HOST'); |
|
167 | - $data .= '&ip=' . $request->getServerParam('SERVER_ADDR'); |
|
168 | - $data .= '&server_type=' . $request->getServerParam('SERVER_SOFTWARE'); |
|
169 | - $data .= '&time=' . time(); |
|
170 | - $data .= '&remote_log=' . $this->_log; |
|
171 | - $data .= '&action=save'; |
|
172 | - |
|
173 | - if (defined('EELOGGING_PASS')) { |
|
174 | - $data .= '&pass=' . EELOGGING_PASS; |
|
175 | - } |
|
176 | - if (defined('EELOGGING_KEY')) { |
|
177 | - $data .= '&key=' . EELOGGING_KEY; |
|
178 | - } |
|
179 | - |
|
180 | - $c = curl_init($this->_remote_logging_url); |
|
181 | - curl_setopt($c, CURLOPT_POST, true); |
|
182 | - curl_setopt($c, CURLOPT_POSTFIELDS, $data); |
|
183 | - curl_setopt($c, CURLOPT_RETURNTRANSFER, true); |
|
184 | - curl_exec($c); |
|
185 | - curl_close($c); |
|
186 | - } |
|
187 | - |
|
188 | - |
|
189 | - /** |
|
190 | - * write_debug |
|
191 | - * writes the contents of the current request's data to a log file. |
|
192 | - * previous entries are overwritten |
|
193 | - */ |
|
194 | - public function write_debug() |
|
195 | - { |
|
196 | - $msg = esc_html__( |
|
197 | - 'The Local File Logging functionality was removed permanently. Remote Logging is recommended instead.', |
|
198 | - 'event_espresso' |
|
199 | - ); |
|
200 | - EE_Error::doing_it_wrong( |
|
201 | - __METHOD__, |
|
202 | - $msg, |
|
203 | - '4.10.1.p' |
|
204 | - ); |
|
205 | - } |
|
206 | - |
|
207 | - |
|
208 | - /** |
|
209 | - * __clone |
|
210 | - */ |
|
211 | - public function __clone() |
|
212 | - { |
|
213 | - trigger_error(esc_html__('Clone is not allowed.', 'event_espresso'), E_USER_ERROR); |
|
214 | - } |
|
22 | + /** |
|
23 | + * @var string |
|
24 | + */ |
|
25 | + private $_log = ''; |
|
26 | + |
|
27 | + /** |
|
28 | + * Used for remote logging |
|
29 | + * |
|
30 | + * @var string |
|
31 | + */ |
|
32 | + private $_remote_logging_url = ''; |
|
33 | + |
|
34 | + /** |
|
35 | + * @var string |
|
36 | + */ |
|
37 | + private $_remote_log = ''; |
|
38 | + |
|
39 | + /** |
|
40 | + * @var EE_Log |
|
41 | + */ |
|
42 | + private static $_instance; |
|
43 | + |
|
44 | + |
|
45 | + /** |
|
46 | + * @return EE_Log |
|
47 | + */ |
|
48 | + public static function instance() |
|
49 | + { |
|
50 | + if (! self::$_instance instanceof EE_Log) { |
|
51 | + self::$_instance = new self(); |
|
52 | + } |
|
53 | + return self::$_instance; |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * @access private |
|
58 | + * @return EE_Log |
|
59 | + */ |
|
60 | + private function __construct() |
|
61 | + { |
|
62 | + |
|
63 | + if (! EE_Registry::instance()->CFG->admin->use_remote_logging) { |
|
64 | + return; |
|
65 | + } |
|
66 | + |
|
67 | + $this->_remote_logging_url = EE_Registry::instance()->CFG->admin->remote_logging_url; |
|
68 | + $this->_remote_log = ''; |
|
69 | + |
|
70 | + if (EE_Registry::instance()->CFG->admin->use_remote_logging) { |
|
71 | + add_action('shutdown', array($this, 'send_log'), 9999); |
|
72 | + } |
|
73 | + } |
|
74 | + |
|
75 | + |
|
76 | + /** |
|
77 | + * verify_filesystem |
|
78 | + * tests that the required files and folders exist and are writable |
|
79 | + * |
|
80 | + */ |
|
81 | + public function verify_filesystem() |
|
82 | + { |
|
83 | + $msg = esc_html__( |
|
84 | + 'The Local File Logging functionality was removed permanently. Remote Logging is recommended instead.', |
|
85 | + 'event_espresso' |
|
86 | + ); |
|
87 | + EE_Error::doing_it_wrong( |
|
88 | + __METHOD__, |
|
89 | + $msg, |
|
90 | + '4.10.1.p' |
|
91 | + ); |
|
92 | + } |
|
93 | + |
|
94 | + |
|
95 | + /** |
|
96 | + * _format_message |
|
97 | + * makes yer log entries look all purdy |
|
98 | + * |
|
99 | + * @param string $file |
|
100 | + * @param string $function |
|
101 | + * @param string $message |
|
102 | + * @param string $type |
|
103 | + * @return string |
|
104 | + */ |
|
105 | + private function _format_message($file = '', $function = '', $message = '', $type = '') |
|
106 | + { |
|
107 | + $msg = '----------------------------------------------------------------------------------------' . PHP_EOL; |
|
108 | + $msg .= '[' . current_time('mysql') . '] '; |
|
109 | + $msg .= ! empty($file) ? basename($file) : ''; |
|
110 | + $msg .= ! empty($file) && ! empty($function) ? ' -> ' : ''; |
|
111 | + $msg .= ! empty($function) ? $function . '()' : ''; |
|
112 | + $msg .= PHP_EOL; |
|
113 | + $type = ! empty($type) ? $type : 'log message'; |
|
114 | + $msg .= ! empty($message) ? "\t" . '[' . $type . '] ' . $message . PHP_EOL : ''; |
|
115 | + return $msg; |
|
116 | + } |
|
117 | + |
|
118 | + |
|
119 | + /** |
|
120 | + * log |
|
121 | + * adds content to the EE_Log->_log property which gets written to file during the WP 'shutdown' hookpoint via the |
|
122 | + * EE_Log::write_log() callback |
|
123 | + * |
|
124 | + * @param string $file |
|
125 | + * @param string $function |
|
126 | + * @param string $message |
|
127 | + * @param string $type |
|
128 | + */ |
|
129 | + public function log($file = '', $function = '', $message = '', $type = '') |
|
130 | + { |
|
131 | + $this->_log .= $this->_format_message($file, $function, $message, $type); |
|
132 | + } |
|
133 | + |
|
134 | + |
|
135 | + /** |
|
136 | + * write_log |
|
137 | + * appends the results of the 'AHEE_log' filter to the espresso log file |
|
138 | + */ |
|
139 | + public function write_log() |
|
140 | + { |
|
141 | + $msg = esc_html__( |
|
142 | + 'The Local File Logging functionality was removed permanently. Remote Logging is recommended instead.', |
|
143 | + 'event_espresso' |
|
144 | + ); |
|
145 | + EE_Error::doing_it_wrong( |
|
146 | + __METHOD__, |
|
147 | + $msg, |
|
148 | + '4.10.1.p' |
|
149 | + ); |
|
150 | + } |
|
151 | + |
|
152 | + |
|
153 | + /** |
|
154 | + * send_log |
|
155 | + * sends the espresso log to a remote URL via a PHP cURL request |
|
156 | + */ |
|
157 | + public function send_log() |
|
158 | + { |
|
159 | + |
|
160 | + if (empty($this->_remote_logging_url)) { |
|
161 | + return; |
|
162 | + } |
|
163 | + |
|
164 | + /** @var RequestInterface $request */ |
|
165 | + $request = LoaderFactory::getLoader()->getShared(RequestInterface::class); |
|
166 | + $data = 'domain=' . $request->getServerParam('HTTP_HOST'); |
|
167 | + $data .= '&ip=' . $request->getServerParam('SERVER_ADDR'); |
|
168 | + $data .= '&server_type=' . $request->getServerParam('SERVER_SOFTWARE'); |
|
169 | + $data .= '&time=' . time(); |
|
170 | + $data .= '&remote_log=' . $this->_log; |
|
171 | + $data .= '&action=save'; |
|
172 | + |
|
173 | + if (defined('EELOGGING_PASS')) { |
|
174 | + $data .= '&pass=' . EELOGGING_PASS; |
|
175 | + } |
|
176 | + if (defined('EELOGGING_KEY')) { |
|
177 | + $data .= '&key=' . EELOGGING_KEY; |
|
178 | + } |
|
179 | + |
|
180 | + $c = curl_init($this->_remote_logging_url); |
|
181 | + curl_setopt($c, CURLOPT_POST, true); |
|
182 | + curl_setopt($c, CURLOPT_POSTFIELDS, $data); |
|
183 | + curl_setopt($c, CURLOPT_RETURNTRANSFER, true); |
|
184 | + curl_exec($c); |
|
185 | + curl_close($c); |
|
186 | + } |
|
187 | + |
|
188 | + |
|
189 | + /** |
|
190 | + * write_debug |
|
191 | + * writes the contents of the current request's data to a log file. |
|
192 | + * previous entries are overwritten |
|
193 | + */ |
|
194 | + public function write_debug() |
|
195 | + { |
|
196 | + $msg = esc_html__( |
|
197 | + 'The Local File Logging functionality was removed permanently. Remote Logging is recommended instead.', |
|
198 | + 'event_espresso' |
|
199 | + ); |
|
200 | + EE_Error::doing_it_wrong( |
|
201 | + __METHOD__, |
|
202 | + $msg, |
|
203 | + '4.10.1.p' |
|
204 | + ); |
|
205 | + } |
|
206 | + |
|
207 | + |
|
208 | + /** |
|
209 | + * __clone |
|
210 | + */ |
|
211 | + public function __clone() |
|
212 | + { |
|
213 | + trigger_error(esc_html__('Clone is not allowed.', 'event_espresso'), E_USER_ERROR); |
|
214 | + } |
|
215 | 215 | } |
@@ -114,7 +114,7 @@ discard block |
||
114 | 114 | */ |
115 | 115 | public function set_question_form_input_meta($q_meta = []) |
116 | 116 | { |
117 | - $default_q_meta = [ |
|
117 | + $default_q_meta = [ |
|
118 | 118 | 'att_nmbr' => 1, |
119 | 119 | 'ticket_id' => '', |
120 | 120 | 'date' => '', |
@@ -160,13 +160,13 @@ discard block |
||
160 | 160 | */ |
161 | 161 | private function _set_input_name($qstn_id) |
162 | 162 | { |
163 | - if (! empty($qstn_id)) { |
|
163 | + if ( ! empty($qstn_id)) { |
|
164 | 164 | $ANS_ID = $this->get('ANS_ID'); |
165 | - $qstn_id = ! empty($ANS_ID) ? '[' . $qstn_id . '][' . $ANS_ID . ']' : '[' . $qstn_id . ']'; |
|
165 | + $qstn_id = ! empty($ANS_ID) ? '['.$qstn_id.']['.$ANS_ID.']' : '['.$qstn_id.']'; |
|
166 | 166 | } |
167 | 167 | $this->QST_input_name = $this->_QST_meta['append_qstn_id'] && ! empty($qstn_id) |
168 | - ? $this->_QST_meta['input_prefix'] . $this->_QST_meta['input_name'] . $qstn_id |
|
169 | - : $this->_QST_meta['input_prefix'] . $this->_QST_meta['input_name']; |
|
168 | + ? $this->_QST_meta['input_prefix'].$this->_QST_meta['input_name'].$qstn_id |
|
169 | + : $this->_QST_meta['input_prefix'].$this->_QST_meta['input_name']; |
|
170 | 170 | } |
171 | 171 | |
172 | 172 | |
@@ -181,7 +181,7 @@ discard block |
||
181 | 181 | */ |
182 | 182 | public function get($property = null) |
183 | 183 | { |
184 | - if (! empty($property)) { |
|
184 | + if ( ! empty($property)) { |
|
185 | 185 | if (EEM_Question::instance()->has_field($property)) { |
186 | 186 | return $this->_QST->get($property); |
187 | 187 | } elseif (EEM_Answer::instance()->has_field($property)) { |
@@ -207,7 +207,7 @@ discard block |
||
207 | 207 | { |
208 | 208 | // first try regular property exists method which works as expected in PHP 5.3+ |
209 | 209 | $prop = EEH_Class_Tools::has_property($classname, $property); |
210 | - if (! $prop) { |
|
210 | + if ( ! $prop) { |
|
211 | 211 | // use reflection for < PHP 5.3 as a double check when property is not found, possible due to access restriction |
212 | 212 | $reflector = new ReflectionClass($classname); |
213 | 213 | $prop = $reflector->hasProperty($property); |
@@ -231,7 +231,7 @@ discard block |
||
231 | 231 | ? $this->_QST_meta['input_id'] |
232 | 232 | : sanitize_key(strip_tags($this->_QST->get('QST_display_text'))); |
233 | 233 | $this->QST_input_id = $this->_QST_meta['append_qstn_id'] && ! empty($qstn_id) |
234 | - ? $input_id . '-' . $qstn_id |
|
234 | + ? $input_id.'-'.$qstn_id |
|
235 | 235 | : $input_id; |
236 | 236 | } |
237 | 237 | |
@@ -272,8 +272,8 @@ discard block |
||
272 | 272 | $date = $this->_QST_meta['date']; |
273 | 273 | $time = $this->_QST_meta['time']; |
274 | 274 | $price_id = $this->_QST_meta['price_id']; |
275 | - if (isset($this->form_data['qstn'][ $EVT_ID ][ $att_nmbr ][ $date ][ $time ][ $price_id ][ $qstn_id ])) { |
|
276 | - $answer = $this->form_data['qstn'][ $EVT_ID ][ $att_nmbr ][ $date ][ $time ][ $price_id ][ $qstn_id ]; |
|
275 | + if (isset($this->form_data['qstn'][$EVT_ID][$att_nmbr][$date][$time][$price_id][$qstn_id])) { |
|
276 | + $answer = $this->form_data['qstn'][$EVT_ID][$att_nmbr][$date][$time][$price_id][$qstn_id]; |
|
277 | 277 | $this->_ANS->set('ANS_value', $answer); |
278 | 278 | } |
279 | 279 | } |
@@ -292,42 +292,42 @@ discard block |
||
292 | 292 | */ |
293 | 293 | public static function generate_question_form_inputs_for_object($object = false, $input_types = []) |
294 | 294 | { |
295 | - if (! is_object($object)) { |
|
295 | + if ( ! is_object($object)) { |
|
296 | 296 | return []; |
297 | 297 | } |
298 | 298 | $inputs = []; |
299 | 299 | $fields = $object->get_model()->field_settings(false); |
300 | 300 | foreach ($fields as $field_ID => $field) { |
301 | 301 | if ($field instanceof EE_Model_Field_Base) { |
302 | - if (isset($input_types[ $field_ID ])) { |
|
302 | + if (isset($input_types[$field_ID])) { |
|
303 | 303 | // get saved value for field |
304 | 304 | $value = $object->get($field_ID); |
305 | 305 | // if no saved value, then use default |
306 | 306 | $value = $value !== null ? $value : $field->get_default_value(); |
307 | 307 | // determine question type |
308 | - $type = isset($input_types[ $field_ID ]) ? $input_types[ $field_ID ]['type'] : 'TEXT'; |
|
308 | + $type = isset($input_types[$field_ID]) ? $input_types[$field_ID]['type'] : 'TEXT'; |
|
309 | 309 | // input name |
310 | - $input_name = isset($input_types[ $field_ID ]) && isset($input_types[ $field_ID ]['input_name']) |
|
311 | - ? $input_types[ $field_ID ]['input_name'] . '[' . $field_ID . ']' |
|
310 | + $input_name = isset($input_types[$field_ID]) && isset($input_types[$field_ID]['input_name']) |
|
311 | + ? $input_types[$field_ID]['input_name'].'['.$field_ID.']' |
|
312 | 312 | : $field_ID; |
313 | 313 | // css class for input |
314 | - $class = isset($input_types[ $field_ID ]['class']) && ! empty($input_types[ $field_ID ]['class']) |
|
315 | - ? ' ' . $input_types[ $field_ID ]['class'] |
|
314 | + $class = isset($input_types[$field_ID]['class']) && ! empty($input_types[$field_ID]['class']) |
|
315 | + ? ' '.$input_types[$field_ID]['class'] |
|
316 | 316 | : ''; |
317 | 317 | // whether to apply htmlentities to answer |
318 | - $htmlentities = isset($input_types[ $field_ID ]['htmlentities']) |
|
319 | - ? $input_types[ $field_ID ]['htmlentities'] |
|
318 | + $htmlentities = isset($input_types[$field_ID]['htmlentities']) |
|
319 | + ? $input_types[$field_ID]['htmlentities'] |
|
320 | 320 | : true; |
321 | 321 | // whether to apply htmlentities to answer |
322 | - $label_b4 = isset($input_types[ $field_ID ]['label_b4']) |
|
323 | - ? $input_types[ $field_ID ]['label_b4'] |
|
322 | + $label_b4 = isset($input_types[$field_ID]['label_b4']) |
|
323 | + ? $input_types[$field_ID]['label_b4'] |
|
324 | 324 | : false; |
325 | 325 | // whether to apply htmlentities to answer |
326 | - $use_desc_4_label = isset($input_types[ $field_ID ]['use_desc_4_label']) |
|
327 | - ? $input_types[ $field_ID ]['use_desc_4_label'] |
|
326 | + $use_desc_4_label = isset($input_types[$field_ID]['use_desc_4_label']) |
|
327 | + ? $input_types[$field_ID]['use_desc_4_label'] |
|
328 | 328 | : false; |
329 | 329 | // whether input is disabled |
330 | - $disabled = isset($input_types[ $field_ID ]['disabled']) && $input_types[ $field_ID ]['disabled']; |
|
330 | + $disabled = isset($input_types[$field_ID]['disabled']) && $input_types[$field_ID]['disabled']; |
|
331 | 331 | |
332 | 332 | // create EE_Question_Form_Input object |
333 | 333 | $QFI = new EE_Question_Form_Input( |
@@ -347,9 +347,9 @@ discard block |
||
347 | 347 | ] |
348 | 348 | ), |
349 | 349 | [ |
350 | - 'input_id' => $field_ID . '-' . $object->ID(), |
|
350 | + 'input_id' => $field_ID.'-'.$object->ID(), |
|
351 | 351 | 'input_name' => $input_name, |
352 | - 'input_class' => $field_ID . $class, |
|
352 | + 'input_class' => $field_ID.$class, |
|
353 | 353 | 'input_prefix' => '', |
354 | 354 | 'append_qstn_id' => false, |
355 | 355 | 'htmlentities' => $htmlentities, |
@@ -360,10 +360,10 @@ discard block |
||
360 | 360 | // does question type have options ? |
361 | 361 | if ( |
362 | 362 | in_array($type, ['DROPDOWN', 'RADIO_BTN', 'CHECKBOX']) |
363 | - && isset($input_types[ $field_ID ]) |
|
364 | - && isset($input_types[ $field_ID ]['options']) |
|
363 | + && isset($input_types[$field_ID]) |
|
364 | + && isset($input_types[$field_ID]['options']) |
|
365 | 365 | ) { |
366 | - foreach ($input_types[ $field_ID ]['options'] as $option) { |
|
366 | + foreach ($input_types[$field_ID]['options'] as $option) { |
|
367 | 367 | $option = stripslashes_deep($option); |
368 | 368 | $option_id = ! empty($option['id']) ? $option['id'] : 0; |
369 | 369 | $QSO = EE_Question_Option::new_instance( |
@@ -381,7 +381,7 @@ discard block |
||
381 | 381 | if ($disabled || $field_ID == $object->get_model()->primary_key_name()) { |
382 | 382 | $QFI->set('QST_disabled', true); |
383 | 383 | } |
384 | - $inputs[ $field_ID ] = $QFI; |
|
384 | + $inputs[$field_ID] = $QFI; |
|
385 | 385 | } |
386 | 386 | } |
387 | 387 | } |
@@ -414,7 +414,7 @@ discard block |
||
414 | 414 | */ |
415 | 415 | public function set($property = null, $value = null) |
416 | 416 | { |
417 | - if (! empty($property)) { |
|
417 | + if ( ! empty($property)) { |
|
418 | 418 | if (EEM_Question::instance()->has_field($property)) { |
419 | 419 | $this->_QST->set($property, $value); |
420 | 420 | } elseif (EEM_Answer::instance()->has_field($property)) { |
@@ -460,6 +460,6 @@ discard block |
||
460 | 460 | */ |
461 | 461 | public function get_meta($key = false) |
462 | 462 | { |
463 | - return $key && isset($this->_QST_meta[ $key ]) ? $this->_QST_meta[ $key ] : false; |
|
463 | + return $key && isset($this->_QST_meta[$key]) ? $this->_QST_meta[$key] : false; |
|
464 | 464 | } |
465 | 465 | } |
@@ -15,450 +15,450 @@ |
||
15 | 15 | */ |
16 | 16 | class EE_Question_Form_Input |
17 | 17 | { |
18 | - /** |
|
19 | - * EE_Question object |
|
20 | - * |
|
21 | - * @access private |
|
22 | - * @var object |
|
23 | - */ |
|
24 | - private $_QST = null; |
|
25 | - |
|
26 | - /** |
|
27 | - * EE_Answer object |
|
28 | - * |
|
29 | - * @access private |
|
30 | - * @var object |
|
31 | - */ |
|
32 | - private $_ANS = null; |
|
33 | - |
|
34 | - /** |
|
35 | - * $_QST_meta |
|
36 | - * @access private |
|
37 | - * @var array |
|
38 | - */ |
|
39 | - private $_QST_meta = []; |
|
40 | - |
|
41 | - /** |
|
42 | - * $QST_input_name |
|
43 | - * @access private |
|
44 | - * @var string |
|
45 | - */ |
|
46 | - private $QST_input_name = ''; |
|
47 | - |
|
48 | - /** |
|
49 | - * $QST_input_id |
|
50 | - * @access private |
|
51 | - * @var string |
|
52 | - */ |
|
53 | - private $QST_input_id = ''; |
|
54 | - |
|
55 | - /** |
|
56 | - * $QST_input_class |
|
57 | - * @access private |
|
58 | - * @var string |
|
59 | - */ |
|
60 | - private $QST_input_class = ''; |
|
61 | - |
|
62 | - /** |
|
63 | - * @var bool $QST_disabled |
|
64 | - */ |
|
65 | - private $QST_disabled = false; |
|
66 | - |
|
67 | - /** |
|
68 | - * @var RequestInterface |
|
69 | - */ |
|
70 | - protected $request; |
|
71 | - |
|
72 | - /** |
|
73 | - * @var array |
|
74 | - */ |
|
75 | - protected $form_data; |
|
76 | - |
|
77 | - |
|
78 | - /** |
|
79 | - * constructor for questions |
|
80 | - * |
|
81 | - * @param EE_Question $QST EE_Question object |
|
82 | - * @param EE_Answer $ANS EE_Answer object |
|
83 | - * @param array $q_meta |
|
84 | - * @throws EE_Error |
|
85 | - * @throws ReflectionException |
|
86 | - */ |
|
87 | - public function __construct(EE_Question $QST = null, EE_Answer $ANS = null, $q_meta = []) |
|
88 | - { |
|
89 | - $this->request = LoaderFactory::getLoader()->getShared(RequestInterface::class); |
|
90 | - $this->form_data = $this->request->requestParams(); |
|
91 | - if (empty($QST) || empty($ANS)) { |
|
92 | - EE_Error::add_error( |
|
93 | - esc_html__('An error occurred. A valid EE_Question or EE_Answer object was not received.', 'event_espresso'), |
|
94 | - __FILE__, |
|
95 | - __FUNCTION__, |
|
96 | - __LINE__ |
|
97 | - ); |
|
98 | - return null; |
|
99 | - } |
|
100 | - $this->_QST = $QST; |
|
101 | - $this->_ANS = $ANS; |
|
102 | - $this->set_question_form_input_meta($q_meta); |
|
103 | - $this->set_question_form_input_init(); |
|
104 | - } |
|
105 | - |
|
106 | - |
|
107 | - /** |
|
108 | - * sets meta data for the question form input |
|
109 | - * |
|
110 | - * @access public |
|
111 | - * @param array $q_meta |
|
112 | - * @return void |
|
113 | - */ |
|
114 | - public function set_question_form_input_meta($q_meta = []) |
|
115 | - { |
|
116 | - $default_q_meta = [ |
|
117 | - 'att_nmbr' => 1, |
|
118 | - 'ticket_id' => '', |
|
119 | - 'date' => '', |
|
120 | - 'time' => '', |
|
121 | - 'input_name' => '', |
|
122 | - 'input_id' => '', |
|
123 | - 'input_class' => '', |
|
124 | - 'input_prefix' => 'qstn', |
|
125 | - 'append_qstn_id' => true, |
|
126 | - 'htmlentities' => true, |
|
127 | - 'allow_null' => false, |
|
128 | - ]; |
|
129 | - $this->_QST_meta = array_merge($default_q_meta, $q_meta); |
|
130 | - } |
|
131 | - |
|
132 | - |
|
133 | - /** |
|
134 | - * set_question_form_input_init |
|
135 | - * |
|
136 | - * @access public |
|
137 | - * @return void |
|
138 | - * @throws EE_Error |
|
139 | - * @throws ReflectionException |
|
140 | - */ |
|
141 | - public function set_question_form_input_init() |
|
142 | - { |
|
143 | - $qstn_id = $this->_QST->system_ID() ? $this->_QST->system_ID() : $this->_QST->ID(); |
|
144 | - $this->_set_input_name($qstn_id); |
|
145 | - $this->_set_input_id($qstn_id); |
|
146 | - $this->_set_input_class(); |
|
147 | - $this->set_question_form_input_answer($qstn_id); |
|
148 | - } |
|
149 | - |
|
150 | - |
|
151 | - /** |
|
152 | - * set_input_name |
|
153 | - * |
|
154 | - * @access private |
|
155 | - * @param $qstn_id |
|
156 | - * @return void |
|
157 | - * @throws EE_Error |
|
158 | - * @throws ReflectionException |
|
159 | - */ |
|
160 | - private function _set_input_name($qstn_id) |
|
161 | - { |
|
162 | - if (! empty($qstn_id)) { |
|
163 | - $ANS_ID = $this->get('ANS_ID'); |
|
164 | - $qstn_id = ! empty($ANS_ID) ? '[' . $qstn_id . '][' . $ANS_ID . ']' : '[' . $qstn_id . ']'; |
|
165 | - } |
|
166 | - $this->QST_input_name = $this->_QST_meta['append_qstn_id'] && ! empty($qstn_id) |
|
167 | - ? $this->_QST_meta['input_prefix'] . $this->_QST_meta['input_name'] . $qstn_id |
|
168 | - : $this->_QST_meta['input_prefix'] . $this->_QST_meta['input_name']; |
|
169 | - } |
|
170 | - |
|
171 | - |
|
172 | - /** |
|
173 | - * get property values for question form input |
|
174 | - * |
|
175 | - * @access public |
|
176 | - * @param string $property |
|
177 | - * @return mixed |
|
178 | - * @throws EE_Error |
|
179 | - * @throws ReflectionException |
|
180 | - */ |
|
181 | - public function get($property = null) |
|
182 | - { |
|
183 | - if (! empty($property)) { |
|
184 | - if (EEM_Question::instance()->has_field($property)) { |
|
185 | - return $this->_QST->get($property); |
|
186 | - } elseif (EEM_Answer::instance()->has_field($property)) { |
|
187 | - return $this->_ANS->get($property); |
|
188 | - } elseif ($this->_question_form_input_property_exists(__CLASS__, $property)) { |
|
189 | - return $this->{$property}; |
|
190 | - } |
|
191 | - } |
|
192 | - return null; |
|
193 | - } |
|
194 | - |
|
195 | - |
|
196 | - /** |
|
197 | - * _question_form_input_property_exists |
|
198 | - * |
|
199 | - * @access private |
|
200 | - * @param string $classname |
|
201 | - * @param string $property |
|
202 | - * @return boolean |
|
203 | - * @throws ReflectionException |
|
204 | - */ |
|
205 | - private function _question_form_input_property_exists($classname, $property) |
|
206 | - { |
|
207 | - // first try regular property exists method which works as expected in PHP 5.3+ |
|
208 | - $prop = EEH_Class_Tools::has_property($classname, $property); |
|
209 | - if (! $prop) { |
|
210 | - // use reflection for < PHP 5.3 as a double check when property is not found, possible due to access restriction |
|
211 | - $reflector = new ReflectionClass($classname); |
|
212 | - $prop = $reflector->hasProperty($property); |
|
213 | - } |
|
214 | - return $prop; |
|
215 | - } |
|
216 | - |
|
217 | - |
|
218 | - /** |
|
219 | - * set_input_id |
|
220 | - * |
|
221 | - * @access private |
|
222 | - * @param $qstn_id |
|
223 | - * @return void |
|
224 | - * @throws EE_Error |
|
225 | - * @throws ReflectionException |
|
226 | - */ |
|
227 | - private function _set_input_id($qstn_id) |
|
228 | - { |
|
229 | - $input_id = isset($this->_QST_meta['input_id']) && ! empty($this->_QST_meta['input_id']) |
|
230 | - ? $this->_QST_meta['input_id'] |
|
231 | - : sanitize_key(strip_tags($this->_QST->get('QST_display_text'))); |
|
232 | - $this->QST_input_id = $this->_QST_meta['append_qstn_id'] && ! empty($qstn_id) |
|
233 | - ? $input_id . '-' . $qstn_id |
|
234 | - : $input_id; |
|
235 | - } |
|
236 | - |
|
237 | - |
|
238 | - /** |
|
239 | - * set_input_class |
|
240 | - * |
|
241 | - * @access private |
|
242 | - * @return void |
|
243 | - */ |
|
244 | - private function _set_input_class() |
|
245 | - { |
|
246 | - $this->QST_input_class = isset($this->_QST_meta['input_class']) ? $this->_QST_meta['input_class'] : ''; |
|
247 | - } |
|
248 | - |
|
249 | - |
|
250 | - /** |
|
251 | - * set_question_form_input_answer |
|
252 | - * |
|
253 | - * @access public |
|
254 | - * @param mixed int | string $qstn_id |
|
255 | - * @return void |
|
256 | - * @throws EE_Error |
|
257 | - * @throws ReflectionException |
|
258 | - */ |
|
259 | - public function set_question_form_input_answer($qstn_id) |
|
260 | - { |
|
261 | - // check for answer in $this->form_data in case we are reprocessing a form after an error |
|
262 | - if ( |
|
263 | - isset($this->_QST_meta['EVT_ID']) |
|
264 | - && isset($this->_QST_meta['att_nmbr']) |
|
265 | - && isset($this->_QST_meta['date']) |
|
266 | - && isset($this->_QST_meta['time']) |
|
267 | - && isset($this->_QST_meta['price_id']) |
|
268 | - ) { |
|
269 | - $EVT_ID = $this->_QST_meta['EVT_ID']; |
|
270 | - $att_nmbr = $this->_QST_meta['att_nmbr']; |
|
271 | - $date = $this->_QST_meta['date']; |
|
272 | - $time = $this->_QST_meta['time']; |
|
273 | - $price_id = $this->_QST_meta['price_id']; |
|
274 | - if (isset($this->form_data['qstn'][ $EVT_ID ][ $att_nmbr ][ $date ][ $time ][ $price_id ][ $qstn_id ])) { |
|
275 | - $answer = $this->form_data['qstn'][ $EVT_ID ][ $att_nmbr ][ $date ][ $time ][ $price_id ][ $qstn_id ]; |
|
276 | - $this->_ANS->set('ANS_value', $answer); |
|
277 | - } |
|
278 | - } |
|
279 | - } |
|
280 | - |
|
281 | - |
|
282 | - /** |
|
283 | - * generate_question_form_inputs_for_object |
|
284 | - * |
|
285 | - * @access protected |
|
286 | - * @param bool|object $object $object |
|
287 | - * @param array $input_types |
|
288 | - * @return array |
|
289 | - * @throws EE_Error |
|
290 | - * @throws ReflectionException |
|
291 | - */ |
|
292 | - public static function generate_question_form_inputs_for_object($object = false, $input_types = []) |
|
293 | - { |
|
294 | - if (! is_object($object)) { |
|
295 | - return []; |
|
296 | - } |
|
297 | - $inputs = []; |
|
298 | - $fields = $object->get_model()->field_settings(false); |
|
299 | - foreach ($fields as $field_ID => $field) { |
|
300 | - if ($field instanceof EE_Model_Field_Base) { |
|
301 | - if (isset($input_types[ $field_ID ])) { |
|
302 | - // get saved value for field |
|
303 | - $value = $object->get($field_ID); |
|
304 | - // if no saved value, then use default |
|
305 | - $value = $value !== null ? $value : $field->get_default_value(); |
|
306 | - // determine question type |
|
307 | - $type = isset($input_types[ $field_ID ]) ? $input_types[ $field_ID ]['type'] : 'TEXT'; |
|
308 | - // input name |
|
309 | - $input_name = isset($input_types[ $field_ID ]) && isset($input_types[ $field_ID ]['input_name']) |
|
310 | - ? $input_types[ $field_ID ]['input_name'] . '[' . $field_ID . ']' |
|
311 | - : $field_ID; |
|
312 | - // css class for input |
|
313 | - $class = isset($input_types[ $field_ID ]['class']) && ! empty($input_types[ $field_ID ]['class']) |
|
314 | - ? ' ' . $input_types[ $field_ID ]['class'] |
|
315 | - : ''; |
|
316 | - // whether to apply htmlentities to answer |
|
317 | - $htmlentities = isset($input_types[ $field_ID ]['htmlentities']) |
|
318 | - ? $input_types[ $field_ID ]['htmlentities'] |
|
319 | - : true; |
|
320 | - // whether to apply htmlentities to answer |
|
321 | - $label_b4 = isset($input_types[ $field_ID ]['label_b4']) |
|
322 | - ? $input_types[ $field_ID ]['label_b4'] |
|
323 | - : false; |
|
324 | - // whether to apply htmlentities to answer |
|
325 | - $use_desc_4_label = isset($input_types[ $field_ID ]['use_desc_4_label']) |
|
326 | - ? $input_types[ $field_ID ]['use_desc_4_label'] |
|
327 | - : false; |
|
328 | - // whether input is disabled |
|
329 | - $disabled = isset($input_types[ $field_ID ]['disabled']) && $input_types[ $field_ID ]['disabled']; |
|
330 | - |
|
331 | - // create EE_Question_Form_Input object |
|
332 | - $QFI = new EE_Question_Form_Input( |
|
333 | - EE_Question::new_instance( |
|
334 | - [ |
|
335 | - 'QST_ID' => 0, |
|
336 | - 'QST_display_text' => $field->get_nicename(), |
|
337 | - 'QST_type' => $type, |
|
338 | - ] |
|
339 | - ), |
|
340 | - EE_Answer::new_instance( |
|
341 | - [ |
|
342 | - 'ANS_ID' => 0, |
|
343 | - 'QST_ID' => 0, |
|
344 | - 'REG_ID' => 0, |
|
345 | - 'ANS_value' => $value, |
|
346 | - ] |
|
347 | - ), |
|
348 | - [ |
|
349 | - 'input_id' => $field_ID . '-' . $object->ID(), |
|
350 | - 'input_name' => $input_name, |
|
351 | - 'input_class' => $field_ID . $class, |
|
352 | - 'input_prefix' => '', |
|
353 | - 'append_qstn_id' => false, |
|
354 | - 'htmlentities' => $htmlentities, |
|
355 | - 'label_b4' => $label_b4, |
|
356 | - 'use_desc_4_label' => $use_desc_4_label, |
|
357 | - ] |
|
358 | - ); |
|
359 | - // does question type have options ? |
|
360 | - if ( |
|
361 | - in_array($type, ['DROPDOWN', 'RADIO_BTN', 'CHECKBOX']) |
|
362 | - && isset($input_types[ $field_ID ]) |
|
363 | - && isset($input_types[ $field_ID ]['options']) |
|
364 | - ) { |
|
365 | - foreach ($input_types[ $field_ID ]['options'] as $option) { |
|
366 | - $option = stripslashes_deep($option); |
|
367 | - $option_id = ! empty($option['id']) ? $option['id'] : 0; |
|
368 | - $QSO = EE_Question_Option::new_instance( |
|
369 | - [ |
|
370 | - 'QSO_value' => (string) $option_id, |
|
371 | - 'QSO_desc' => $option['text'], |
|
372 | - 'QSO_deleted' => false, |
|
373 | - ] |
|
374 | - ); |
|
375 | - // all QST (and ANS) properties can be accessed indirectly thru QFI |
|
376 | - $QFI->add_temp_option($QSO); |
|
377 | - } |
|
378 | - } |
|
379 | - // we don't want ppl manually changing primary keys cuz that would just lead to total craziness man |
|
380 | - if ($disabled || $field_ID == $object->get_model()->primary_key_name()) { |
|
381 | - $QFI->set('QST_disabled', true); |
|
382 | - } |
|
383 | - $inputs[ $field_ID ] = $QFI; |
|
384 | - } |
|
385 | - } |
|
386 | - } |
|
387 | - return $inputs; |
|
388 | - } |
|
389 | - |
|
390 | - |
|
391 | - /** |
|
392 | - * add_temp_option |
|
393 | - * |
|
394 | - * @access public |
|
395 | - * @param EE_Question_Option $QSO EE_Question_Option |
|
396 | - * @return void |
|
397 | - */ |
|
398 | - public function add_temp_option(EE_Question_Option $QSO) |
|
399 | - { |
|
400 | - $this->_QST->add_temp_option($QSO); |
|
401 | - } |
|
402 | - |
|
403 | - |
|
404 | - /** |
|
405 | - * set property values for question form input |
|
406 | - * |
|
407 | - * @access public |
|
408 | - * @param string $property |
|
409 | - * @param mixed $value |
|
410 | - * @return void |
|
411 | - * @throws EE_Error |
|
412 | - * @throws ReflectionException |
|
413 | - */ |
|
414 | - public function set($property = null, $value = null) |
|
415 | - { |
|
416 | - if (! empty($property)) { |
|
417 | - if (EEM_Question::instance()->has_field($property)) { |
|
418 | - $this->_QST->set($property, $value); |
|
419 | - } elseif (EEM_Answer::instance()->has_field($property)) { |
|
420 | - $this->_ANS->set($property, $value); |
|
421 | - } elseif ($this->_question_form_input_property_exists(__CLASS__, $property)) { |
|
422 | - $this->{$property} = $value; |
|
423 | - } |
|
424 | - } |
|
425 | - } |
|
426 | - |
|
427 | - |
|
428 | - /** |
|
429 | - * _question_form_input_property_exists |
|
430 | - * |
|
431 | - * @access public |
|
432 | - * @param boolean $notDeletedOptionsOnly 1 |
|
433 | - * whether to return ALL options, or only the ones which have |
|
434 | - * not yet been deleted |
|
435 | - * @param string|array $selected_value_to_always_include , when retrieving options to an ANSWERED question, |
|
436 | - * we want to usually only show non-deleted options AND the |
|
437 | - * value that was selected for the answer, whether it was |
|
438 | - * trashed or not. |
|
439 | - * @return EE_Question_Option |
|
440 | - */ |
|
441 | - public function options($notDeletedOptionsOnly = true, $selected_value_to_always_include = null) |
|
442 | - { |
|
443 | - $temp_options = $this->_QST->temp_options(); |
|
444 | - return ! empty($temp_options) |
|
445 | - ? $temp_options |
|
446 | - : $this->_QST->options( |
|
447 | - $notDeletedOptionsOnly, |
|
448 | - $selected_value_to_always_include |
|
449 | - ); |
|
450 | - } |
|
451 | - |
|
452 | - |
|
453 | - /** |
|
454 | - * get_meta |
|
455 | - * |
|
456 | - * @access public |
|
457 | - * @param mixed $key |
|
458 | - * @return mixed |
|
459 | - */ |
|
460 | - public function get_meta($key = false) |
|
461 | - { |
|
462 | - return $key && isset($this->_QST_meta[ $key ]) ? $this->_QST_meta[ $key ] : false; |
|
463 | - } |
|
18 | + /** |
|
19 | + * EE_Question object |
|
20 | + * |
|
21 | + * @access private |
|
22 | + * @var object |
|
23 | + */ |
|
24 | + private $_QST = null; |
|
25 | + |
|
26 | + /** |
|
27 | + * EE_Answer object |
|
28 | + * |
|
29 | + * @access private |
|
30 | + * @var object |
|
31 | + */ |
|
32 | + private $_ANS = null; |
|
33 | + |
|
34 | + /** |
|
35 | + * $_QST_meta |
|
36 | + * @access private |
|
37 | + * @var array |
|
38 | + */ |
|
39 | + private $_QST_meta = []; |
|
40 | + |
|
41 | + /** |
|
42 | + * $QST_input_name |
|
43 | + * @access private |
|
44 | + * @var string |
|
45 | + */ |
|
46 | + private $QST_input_name = ''; |
|
47 | + |
|
48 | + /** |
|
49 | + * $QST_input_id |
|
50 | + * @access private |
|
51 | + * @var string |
|
52 | + */ |
|
53 | + private $QST_input_id = ''; |
|
54 | + |
|
55 | + /** |
|
56 | + * $QST_input_class |
|
57 | + * @access private |
|
58 | + * @var string |
|
59 | + */ |
|
60 | + private $QST_input_class = ''; |
|
61 | + |
|
62 | + /** |
|
63 | + * @var bool $QST_disabled |
|
64 | + */ |
|
65 | + private $QST_disabled = false; |
|
66 | + |
|
67 | + /** |
|
68 | + * @var RequestInterface |
|
69 | + */ |
|
70 | + protected $request; |
|
71 | + |
|
72 | + /** |
|
73 | + * @var array |
|
74 | + */ |
|
75 | + protected $form_data; |
|
76 | + |
|
77 | + |
|
78 | + /** |
|
79 | + * constructor for questions |
|
80 | + * |
|
81 | + * @param EE_Question $QST EE_Question object |
|
82 | + * @param EE_Answer $ANS EE_Answer object |
|
83 | + * @param array $q_meta |
|
84 | + * @throws EE_Error |
|
85 | + * @throws ReflectionException |
|
86 | + */ |
|
87 | + public function __construct(EE_Question $QST = null, EE_Answer $ANS = null, $q_meta = []) |
|
88 | + { |
|
89 | + $this->request = LoaderFactory::getLoader()->getShared(RequestInterface::class); |
|
90 | + $this->form_data = $this->request->requestParams(); |
|
91 | + if (empty($QST) || empty($ANS)) { |
|
92 | + EE_Error::add_error( |
|
93 | + esc_html__('An error occurred. A valid EE_Question or EE_Answer object was not received.', 'event_espresso'), |
|
94 | + __FILE__, |
|
95 | + __FUNCTION__, |
|
96 | + __LINE__ |
|
97 | + ); |
|
98 | + return null; |
|
99 | + } |
|
100 | + $this->_QST = $QST; |
|
101 | + $this->_ANS = $ANS; |
|
102 | + $this->set_question_form_input_meta($q_meta); |
|
103 | + $this->set_question_form_input_init(); |
|
104 | + } |
|
105 | + |
|
106 | + |
|
107 | + /** |
|
108 | + * sets meta data for the question form input |
|
109 | + * |
|
110 | + * @access public |
|
111 | + * @param array $q_meta |
|
112 | + * @return void |
|
113 | + */ |
|
114 | + public function set_question_form_input_meta($q_meta = []) |
|
115 | + { |
|
116 | + $default_q_meta = [ |
|
117 | + 'att_nmbr' => 1, |
|
118 | + 'ticket_id' => '', |
|
119 | + 'date' => '', |
|
120 | + 'time' => '', |
|
121 | + 'input_name' => '', |
|
122 | + 'input_id' => '', |
|
123 | + 'input_class' => '', |
|
124 | + 'input_prefix' => 'qstn', |
|
125 | + 'append_qstn_id' => true, |
|
126 | + 'htmlentities' => true, |
|
127 | + 'allow_null' => false, |
|
128 | + ]; |
|
129 | + $this->_QST_meta = array_merge($default_q_meta, $q_meta); |
|
130 | + } |
|
131 | + |
|
132 | + |
|
133 | + /** |
|
134 | + * set_question_form_input_init |
|
135 | + * |
|
136 | + * @access public |
|
137 | + * @return void |
|
138 | + * @throws EE_Error |
|
139 | + * @throws ReflectionException |
|
140 | + */ |
|
141 | + public function set_question_form_input_init() |
|
142 | + { |
|
143 | + $qstn_id = $this->_QST->system_ID() ? $this->_QST->system_ID() : $this->_QST->ID(); |
|
144 | + $this->_set_input_name($qstn_id); |
|
145 | + $this->_set_input_id($qstn_id); |
|
146 | + $this->_set_input_class(); |
|
147 | + $this->set_question_form_input_answer($qstn_id); |
|
148 | + } |
|
149 | + |
|
150 | + |
|
151 | + /** |
|
152 | + * set_input_name |
|
153 | + * |
|
154 | + * @access private |
|
155 | + * @param $qstn_id |
|
156 | + * @return void |
|
157 | + * @throws EE_Error |
|
158 | + * @throws ReflectionException |
|
159 | + */ |
|
160 | + private function _set_input_name($qstn_id) |
|
161 | + { |
|
162 | + if (! empty($qstn_id)) { |
|
163 | + $ANS_ID = $this->get('ANS_ID'); |
|
164 | + $qstn_id = ! empty($ANS_ID) ? '[' . $qstn_id . '][' . $ANS_ID . ']' : '[' . $qstn_id . ']'; |
|
165 | + } |
|
166 | + $this->QST_input_name = $this->_QST_meta['append_qstn_id'] && ! empty($qstn_id) |
|
167 | + ? $this->_QST_meta['input_prefix'] . $this->_QST_meta['input_name'] . $qstn_id |
|
168 | + : $this->_QST_meta['input_prefix'] . $this->_QST_meta['input_name']; |
|
169 | + } |
|
170 | + |
|
171 | + |
|
172 | + /** |
|
173 | + * get property values for question form input |
|
174 | + * |
|
175 | + * @access public |
|
176 | + * @param string $property |
|
177 | + * @return mixed |
|
178 | + * @throws EE_Error |
|
179 | + * @throws ReflectionException |
|
180 | + */ |
|
181 | + public function get($property = null) |
|
182 | + { |
|
183 | + if (! empty($property)) { |
|
184 | + if (EEM_Question::instance()->has_field($property)) { |
|
185 | + return $this->_QST->get($property); |
|
186 | + } elseif (EEM_Answer::instance()->has_field($property)) { |
|
187 | + return $this->_ANS->get($property); |
|
188 | + } elseif ($this->_question_form_input_property_exists(__CLASS__, $property)) { |
|
189 | + return $this->{$property}; |
|
190 | + } |
|
191 | + } |
|
192 | + return null; |
|
193 | + } |
|
194 | + |
|
195 | + |
|
196 | + /** |
|
197 | + * _question_form_input_property_exists |
|
198 | + * |
|
199 | + * @access private |
|
200 | + * @param string $classname |
|
201 | + * @param string $property |
|
202 | + * @return boolean |
|
203 | + * @throws ReflectionException |
|
204 | + */ |
|
205 | + private function _question_form_input_property_exists($classname, $property) |
|
206 | + { |
|
207 | + // first try regular property exists method which works as expected in PHP 5.3+ |
|
208 | + $prop = EEH_Class_Tools::has_property($classname, $property); |
|
209 | + if (! $prop) { |
|
210 | + // use reflection for < PHP 5.3 as a double check when property is not found, possible due to access restriction |
|
211 | + $reflector = new ReflectionClass($classname); |
|
212 | + $prop = $reflector->hasProperty($property); |
|
213 | + } |
|
214 | + return $prop; |
|
215 | + } |
|
216 | + |
|
217 | + |
|
218 | + /** |
|
219 | + * set_input_id |
|
220 | + * |
|
221 | + * @access private |
|
222 | + * @param $qstn_id |
|
223 | + * @return void |
|
224 | + * @throws EE_Error |
|
225 | + * @throws ReflectionException |
|
226 | + */ |
|
227 | + private function _set_input_id($qstn_id) |
|
228 | + { |
|
229 | + $input_id = isset($this->_QST_meta['input_id']) && ! empty($this->_QST_meta['input_id']) |
|
230 | + ? $this->_QST_meta['input_id'] |
|
231 | + : sanitize_key(strip_tags($this->_QST->get('QST_display_text'))); |
|
232 | + $this->QST_input_id = $this->_QST_meta['append_qstn_id'] && ! empty($qstn_id) |
|
233 | + ? $input_id . '-' . $qstn_id |
|
234 | + : $input_id; |
|
235 | + } |
|
236 | + |
|
237 | + |
|
238 | + /** |
|
239 | + * set_input_class |
|
240 | + * |
|
241 | + * @access private |
|
242 | + * @return void |
|
243 | + */ |
|
244 | + private function _set_input_class() |
|
245 | + { |
|
246 | + $this->QST_input_class = isset($this->_QST_meta['input_class']) ? $this->_QST_meta['input_class'] : ''; |
|
247 | + } |
|
248 | + |
|
249 | + |
|
250 | + /** |
|
251 | + * set_question_form_input_answer |
|
252 | + * |
|
253 | + * @access public |
|
254 | + * @param mixed int | string $qstn_id |
|
255 | + * @return void |
|
256 | + * @throws EE_Error |
|
257 | + * @throws ReflectionException |
|
258 | + */ |
|
259 | + public function set_question_form_input_answer($qstn_id) |
|
260 | + { |
|
261 | + // check for answer in $this->form_data in case we are reprocessing a form after an error |
|
262 | + if ( |
|
263 | + isset($this->_QST_meta['EVT_ID']) |
|
264 | + && isset($this->_QST_meta['att_nmbr']) |
|
265 | + && isset($this->_QST_meta['date']) |
|
266 | + && isset($this->_QST_meta['time']) |
|
267 | + && isset($this->_QST_meta['price_id']) |
|
268 | + ) { |
|
269 | + $EVT_ID = $this->_QST_meta['EVT_ID']; |
|
270 | + $att_nmbr = $this->_QST_meta['att_nmbr']; |
|
271 | + $date = $this->_QST_meta['date']; |
|
272 | + $time = $this->_QST_meta['time']; |
|
273 | + $price_id = $this->_QST_meta['price_id']; |
|
274 | + if (isset($this->form_data['qstn'][ $EVT_ID ][ $att_nmbr ][ $date ][ $time ][ $price_id ][ $qstn_id ])) { |
|
275 | + $answer = $this->form_data['qstn'][ $EVT_ID ][ $att_nmbr ][ $date ][ $time ][ $price_id ][ $qstn_id ]; |
|
276 | + $this->_ANS->set('ANS_value', $answer); |
|
277 | + } |
|
278 | + } |
|
279 | + } |
|
280 | + |
|
281 | + |
|
282 | + /** |
|
283 | + * generate_question_form_inputs_for_object |
|
284 | + * |
|
285 | + * @access protected |
|
286 | + * @param bool|object $object $object |
|
287 | + * @param array $input_types |
|
288 | + * @return array |
|
289 | + * @throws EE_Error |
|
290 | + * @throws ReflectionException |
|
291 | + */ |
|
292 | + public static function generate_question_form_inputs_for_object($object = false, $input_types = []) |
|
293 | + { |
|
294 | + if (! is_object($object)) { |
|
295 | + return []; |
|
296 | + } |
|
297 | + $inputs = []; |
|
298 | + $fields = $object->get_model()->field_settings(false); |
|
299 | + foreach ($fields as $field_ID => $field) { |
|
300 | + if ($field instanceof EE_Model_Field_Base) { |
|
301 | + if (isset($input_types[ $field_ID ])) { |
|
302 | + // get saved value for field |
|
303 | + $value = $object->get($field_ID); |
|
304 | + // if no saved value, then use default |
|
305 | + $value = $value !== null ? $value : $field->get_default_value(); |
|
306 | + // determine question type |
|
307 | + $type = isset($input_types[ $field_ID ]) ? $input_types[ $field_ID ]['type'] : 'TEXT'; |
|
308 | + // input name |
|
309 | + $input_name = isset($input_types[ $field_ID ]) && isset($input_types[ $field_ID ]['input_name']) |
|
310 | + ? $input_types[ $field_ID ]['input_name'] . '[' . $field_ID . ']' |
|
311 | + : $field_ID; |
|
312 | + // css class for input |
|
313 | + $class = isset($input_types[ $field_ID ]['class']) && ! empty($input_types[ $field_ID ]['class']) |
|
314 | + ? ' ' . $input_types[ $field_ID ]['class'] |
|
315 | + : ''; |
|
316 | + // whether to apply htmlentities to answer |
|
317 | + $htmlentities = isset($input_types[ $field_ID ]['htmlentities']) |
|
318 | + ? $input_types[ $field_ID ]['htmlentities'] |
|
319 | + : true; |
|
320 | + // whether to apply htmlentities to answer |
|
321 | + $label_b4 = isset($input_types[ $field_ID ]['label_b4']) |
|
322 | + ? $input_types[ $field_ID ]['label_b4'] |
|
323 | + : false; |
|
324 | + // whether to apply htmlentities to answer |
|
325 | + $use_desc_4_label = isset($input_types[ $field_ID ]['use_desc_4_label']) |
|
326 | + ? $input_types[ $field_ID ]['use_desc_4_label'] |
|
327 | + : false; |
|
328 | + // whether input is disabled |
|
329 | + $disabled = isset($input_types[ $field_ID ]['disabled']) && $input_types[ $field_ID ]['disabled']; |
|
330 | + |
|
331 | + // create EE_Question_Form_Input object |
|
332 | + $QFI = new EE_Question_Form_Input( |
|
333 | + EE_Question::new_instance( |
|
334 | + [ |
|
335 | + 'QST_ID' => 0, |
|
336 | + 'QST_display_text' => $field->get_nicename(), |
|
337 | + 'QST_type' => $type, |
|
338 | + ] |
|
339 | + ), |
|
340 | + EE_Answer::new_instance( |
|
341 | + [ |
|
342 | + 'ANS_ID' => 0, |
|
343 | + 'QST_ID' => 0, |
|
344 | + 'REG_ID' => 0, |
|
345 | + 'ANS_value' => $value, |
|
346 | + ] |
|
347 | + ), |
|
348 | + [ |
|
349 | + 'input_id' => $field_ID . '-' . $object->ID(), |
|
350 | + 'input_name' => $input_name, |
|
351 | + 'input_class' => $field_ID . $class, |
|
352 | + 'input_prefix' => '', |
|
353 | + 'append_qstn_id' => false, |
|
354 | + 'htmlentities' => $htmlentities, |
|
355 | + 'label_b4' => $label_b4, |
|
356 | + 'use_desc_4_label' => $use_desc_4_label, |
|
357 | + ] |
|
358 | + ); |
|
359 | + // does question type have options ? |
|
360 | + if ( |
|
361 | + in_array($type, ['DROPDOWN', 'RADIO_BTN', 'CHECKBOX']) |
|
362 | + && isset($input_types[ $field_ID ]) |
|
363 | + && isset($input_types[ $field_ID ]['options']) |
|
364 | + ) { |
|
365 | + foreach ($input_types[ $field_ID ]['options'] as $option) { |
|
366 | + $option = stripslashes_deep($option); |
|
367 | + $option_id = ! empty($option['id']) ? $option['id'] : 0; |
|
368 | + $QSO = EE_Question_Option::new_instance( |
|
369 | + [ |
|
370 | + 'QSO_value' => (string) $option_id, |
|
371 | + 'QSO_desc' => $option['text'], |
|
372 | + 'QSO_deleted' => false, |
|
373 | + ] |
|
374 | + ); |
|
375 | + // all QST (and ANS) properties can be accessed indirectly thru QFI |
|
376 | + $QFI->add_temp_option($QSO); |
|
377 | + } |
|
378 | + } |
|
379 | + // we don't want ppl manually changing primary keys cuz that would just lead to total craziness man |
|
380 | + if ($disabled || $field_ID == $object->get_model()->primary_key_name()) { |
|
381 | + $QFI->set('QST_disabled', true); |
|
382 | + } |
|
383 | + $inputs[ $field_ID ] = $QFI; |
|
384 | + } |
|
385 | + } |
|
386 | + } |
|
387 | + return $inputs; |
|
388 | + } |
|
389 | + |
|
390 | + |
|
391 | + /** |
|
392 | + * add_temp_option |
|
393 | + * |
|
394 | + * @access public |
|
395 | + * @param EE_Question_Option $QSO EE_Question_Option |
|
396 | + * @return void |
|
397 | + */ |
|
398 | + public function add_temp_option(EE_Question_Option $QSO) |
|
399 | + { |
|
400 | + $this->_QST->add_temp_option($QSO); |
|
401 | + } |
|
402 | + |
|
403 | + |
|
404 | + /** |
|
405 | + * set property values for question form input |
|
406 | + * |
|
407 | + * @access public |
|
408 | + * @param string $property |
|
409 | + * @param mixed $value |
|
410 | + * @return void |
|
411 | + * @throws EE_Error |
|
412 | + * @throws ReflectionException |
|
413 | + */ |
|
414 | + public function set($property = null, $value = null) |
|
415 | + { |
|
416 | + if (! empty($property)) { |
|
417 | + if (EEM_Question::instance()->has_field($property)) { |
|
418 | + $this->_QST->set($property, $value); |
|
419 | + } elseif (EEM_Answer::instance()->has_field($property)) { |
|
420 | + $this->_ANS->set($property, $value); |
|
421 | + } elseif ($this->_question_form_input_property_exists(__CLASS__, $property)) { |
|
422 | + $this->{$property} = $value; |
|
423 | + } |
|
424 | + } |
|
425 | + } |
|
426 | + |
|
427 | + |
|
428 | + /** |
|
429 | + * _question_form_input_property_exists |
|
430 | + * |
|
431 | + * @access public |
|
432 | + * @param boolean $notDeletedOptionsOnly 1 |
|
433 | + * whether to return ALL options, or only the ones which have |
|
434 | + * not yet been deleted |
|
435 | + * @param string|array $selected_value_to_always_include , when retrieving options to an ANSWERED question, |
|
436 | + * we want to usually only show non-deleted options AND the |
|
437 | + * value that was selected for the answer, whether it was |
|
438 | + * trashed or not. |
|
439 | + * @return EE_Question_Option |
|
440 | + */ |
|
441 | + public function options($notDeletedOptionsOnly = true, $selected_value_to_always_include = null) |
|
442 | + { |
|
443 | + $temp_options = $this->_QST->temp_options(); |
|
444 | + return ! empty($temp_options) |
|
445 | + ? $temp_options |
|
446 | + : $this->_QST->options( |
|
447 | + $notDeletedOptionsOnly, |
|
448 | + $selected_value_to_always_include |
|
449 | + ); |
|
450 | + } |
|
451 | + |
|
452 | + |
|
453 | + /** |
|
454 | + * get_meta |
|
455 | + * |
|
456 | + * @access public |
|
457 | + * @param mixed $key |
|
458 | + * @return mixed |
|
459 | + */ |
|
460 | + public function get_meta($key = false) |
|
461 | + { |
|
462 | + return $key && isset($this->_QST_meta[ $key ]) ? $this->_QST_meta[ $key ] : false; |
|
463 | + } |
|
464 | 464 | } |
@@ -154,19 +154,19 @@ |
||
154 | 154 | $sanitized_attributes = array(); |
155 | 155 | foreach ($attributes as $attribute => $value) { |
156 | 156 | $convert = $this->getAttributesMap(); |
157 | - if (isset($convert[ $attribute ])) { |
|
158 | - $sanitize = $convert[ $attribute ]; |
|
157 | + if (isset($convert[$attribute])) { |
|
158 | + $sanitize = $convert[$attribute]; |
|
159 | 159 | if ($sanitize === 'bool') { |
160 | - $sanitized_attributes[ $attribute ] = filter_var( |
|
160 | + $sanitized_attributes[$attribute] = filter_var( |
|
161 | 161 | $value, |
162 | 162 | FILTER_VALIDATE_BOOLEAN |
163 | 163 | ); |
164 | 164 | } else { |
165 | - $sanitized_attributes[ $attribute ] = $sanitize($value); |
|
165 | + $sanitized_attributes[$attribute] = $sanitize($value); |
|
166 | 166 | } |
167 | 167 | // don't pass along attributes with a 0 value |
168 | - if ($sanitized_attributes[ $attribute ] === 0) { |
|
169 | - unset($sanitized_attributes[ $attribute ]); |
|
168 | + if ($sanitized_attributes[$attribute] === 0) { |
|
169 | + unset($sanitized_attributes[$attribute]); |
|
170 | 170 | } |
171 | 171 | } |
172 | 172 | } |
@@ -20,172 +20,172 @@ |
||
20 | 20 | */ |
21 | 21 | class EventAttendees extends Block |
22 | 22 | { |
23 | - const BLOCK_TYPE = 'event-attendees'; |
|
23 | + const BLOCK_TYPE = 'event-attendees'; |
|
24 | 24 | |
25 | - /** |
|
26 | - * @var EventAttendeesBlockRenderer $renderer |
|
27 | - */ |
|
28 | - protected $renderer; |
|
25 | + /** |
|
26 | + * @var EventAttendeesBlockRenderer $renderer |
|
27 | + */ |
|
28 | + protected $renderer; |
|
29 | 29 | |
30 | 30 | |
31 | - /** |
|
32 | - * EventAttendees constructor. |
|
33 | - * |
|
34 | - * @param CoreBlocksAssetManager $block_asset_manager |
|
35 | - * @param RequestInterface $request |
|
36 | - * @param EventAttendeesBlockRenderer $renderer |
|
37 | - */ |
|
38 | - public function __construct( |
|
39 | - CoreBlocksAssetManager $block_asset_manager, |
|
40 | - RequestInterface $request, |
|
41 | - EventAttendeesBlockRenderer $renderer |
|
42 | - ) { |
|
43 | - parent::__construct($block_asset_manager, $request); |
|
44 | - $this->renderer = $renderer; |
|
45 | - } |
|
31 | + /** |
|
32 | + * EventAttendees constructor. |
|
33 | + * |
|
34 | + * @param CoreBlocksAssetManager $block_asset_manager |
|
35 | + * @param RequestInterface $request |
|
36 | + * @param EventAttendeesBlockRenderer $renderer |
|
37 | + */ |
|
38 | + public function __construct( |
|
39 | + CoreBlocksAssetManager $block_asset_manager, |
|
40 | + RequestInterface $request, |
|
41 | + EventAttendeesBlockRenderer $renderer |
|
42 | + ) { |
|
43 | + parent::__construct($block_asset_manager, $request); |
|
44 | + $this->renderer = $renderer; |
|
45 | + } |
|
46 | 46 | |
47 | 47 | |
48 | - /** |
|
49 | - * Perform any early setup required by the block |
|
50 | - * including setting the block type and supported post types |
|
51 | - * |
|
52 | - * @return void |
|
53 | - */ |
|
54 | - public function initialize() |
|
55 | - { |
|
56 | - $this->setBlockType(self::BLOCK_TYPE); |
|
57 | - $this->setSupportedRoutes( |
|
58 | - array( |
|
59 | - 'EventEspresso\core\domain\entities\route_match\specifications\admin\EspressoStandardPostTypeEditor', |
|
60 | - 'EventEspresso\core\domain\entities\route_match\specifications\admin\WordPressPostTypeEditor', |
|
61 | - 'EventEspresso\core\domain\entities\route_match\specifications\frontend\EspressoBlockRenderer', |
|
62 | - 'EventEspresso\core\domain\entities\route_match\specifications\frontend\AnyFrontendRequest' |
|
63 | - ) |
|
64 | - ); |
|
65 | - $EVT_ID = $this->request->getRequestParam('page') === 'espresso_events' |
|
66 | - ? $this->request->getRequestParam('post', 0, 'int') |
|
67 | - : 0; |
|
68 | - $this->setAttributes( |
|
69 | - array( |
|
70 | - 'eventId' => array( |
|
71 | - 'type' => 'number', |
|
72 | - 'default' => $EVT_ID, |
|
73 | - ), |
|
74 | - 'datetimeId' => array( |
|
75 | - 'type' => 'number', |
|
76 | - 'default' => 0, |
|
77 | - ), |
|
78 | - 'ticketId' => array( |
|
79 | - 'type' => 'number', |
|
80 | - 'default' => 0, |
|
81 | - ), |
|
82 | - 'status' => array( |
|
83 | - 'type' => 'string', |
|
84 | - 'default' => EEM_Registration::status_id_approved, |
|
85 | - ), |
|
86 | - 'limit' => array( |
|
87 | - 'type' => 'number', |
|
88 | - 'default' => 100, |
|
89 | - ), |
|
90 | - 'order' => array( |
|
91 | - 'type' => 'string', |
|
92 | - 'default' => 'ASC' |
|
93 | - ), |
|
94 | - 'orderBy' => array( |
|
95 | - 'type' => 'string', |
|
96 | - 'default' => 'lastThenFirstName', |
|
97 | - ), |
|
98 | - 'showGravatar' => array( |
|
99 | - 'type' => 'boolean', |
|
100 | - 'default' => false, |
|
101 | - ), |
|
102 | - 'avatarClass' => array( |
|
103 | - 'type' => 'string', |
|
104 | - 'default' => 'contact', |
|
105 | - ), |
|
106 | - 'avatarSize' => array( |
|
107 | - 'type' => 'number', |
|
108 | - 'default' => 24, |
|
109 | - ), |
|
110 | - 'displayOnArchives' => array( |
|
111 | - 'type' => 'boolean', |
|
112 | - 'default' => false, |
|
113 | - ), |
|
114 | - ) |
|
115 | - ); |
|
116 | - $this->setDynamic(); |
|
117 | - } |
|
48 | + /** |
|
49 | + * Perform any early setup required by the block |
|
50 | + * including setting the block type and supported post types |
|
51 | + * |
|
52 | + * @return void |
|
53 | + */ |
|
54 | + public function initialize() |
|
55 | + { |
|
56 | + $this->setBlockType(self::BLOCK_TYPE); |
|
57 | + $this->setSupportedRoutes( |
|
58 | + array( |
|
59 | + 'EventEspresso\core\domain\entities\route_match\specifications\admin\EspressoStandardPostTypeEditor', |
|
60 | + 'EventEspresso\core\domain\entities\route_match\specifications\admin\WordPressPostTypeEditor', |
|
61 | + 'EventEspresso\core\domain\entities\route_match\specifications\frontend\EspressoBlockRenderer', |
|
62 | + 'EventEspresso\core\domain\entities\route_match\specifications\frontend\AnyFrontendRequest' |
|
63 | + ) |
|
64 | + ); |
|
65 | + $EVT_ID = $this->request->getRequestParam('page') === 'espresso_events' |
|
66 | + ? $this->request->getRequestParam('post', 0, 'int') |
|
67 | + : 0; |
|
68 | + $this->setAttributes( |
|
69 | + array( |
|
70 | + 'eventId' => array( |
|
71 | + 'type' => 'number', |
|
72 | + 'default' => $EVT_ID, |
|
73 | + ), |
|
74 | + 'datetimeId' => array( |
|
75 | + 'type' => 'number', |
|
76 | + 'default' => 0, |
|
77 | + ), |
|
78 | + 'ticketId' => array( |
|
79 | + 'type' => 'number', |
|
80 | + 'default' => 0, |
|
81 | + ), |
|
82 | + 'status' => array( |
|
83 | + 'type' => 'string', |
|
84 | + 'default' => EEM_Registration::status_id_approved, |
|
85 | + ), |
|
86 | + 'limit' => array( |
|
87 | + 'type' => 'number', |
|
88 | + 'default' => 100, |
|
89 | + ), |
|
90 | + 'order' => array( |
|
91 | + 'type' => 'string', |
|
92 | + 'default' => 'ASC' |
|
93 | + ), |
|
94 | + 'orderBy' => array( |
|
95 | + 'type' => 'string', |
|
96 | + 'default' => 'lastThenFirstName', |
|
97 | + ), |
|
98 | + 'showGravatar' => array( |
|
99 | + 'type' => 'boolean', |
|
100 | + 'default' => false, |
|
101 | + ), |
|
102 | + 'avatarClass' => array( |
|
103 | + 'type' => 'string', |
|
104 | + 'default' => 'contact', |
|
105 | + ), |
|
106 | + 'avatarSize' => array( |
|
107 | + 'type' => 'number', |
|
108 | + 'default' => 24, |
|
109 | + ), |
|
110 | + 'displayOnArchives' => array( |
|
111 | + 'type' => 'boolean', |
|
112 | + 'default' => false, |
|
113 | + ), |
|
114 | + ) |
|
115 | + ); |
|
116 | + $this->setDynamic(); |
|
117 | + } |
|
118 | 118 | |
119 | 119 | |
120 | - /** |
|
121 | - * Returns an array where the key corresponds to the incoming attribute name from the WP block |
|
122 | - * and the value corresponds to the attribute name for the existing EspressoEventAttendees shortcode |
|
123 | - * |
|
124 | - * @since 4.9.71.p |
|
125 | - * @return array |
|
126 | - */ |
|
127 | - private function getAttributesMap() |
|
128 | - { |
|
129 | - return array( |
|
130 | - 'eventId' => 'absint', |
|
131 | - 'datetimeId' => 'absint', |
|
132 | - 'ticketId' => 'absint', |
|
133 | - 'status' => 'sanitize_text_field', |
|
134 | - 'limit' => 'intval', |
|
135 | - 'showGravatar' => 'bool', |
|
136 | - 'avatarClass' => 'sanitize_text_field', |
|
137 | - 'avatarSize' => 'absint', |
|
138 | - 'displayOnArchives' => 'bool', |
|
139 | - 'order' => 'sanitize_text_field', |
|
140 | - 'orderBy' => 'sanitize_text_field', |
|
141 | - ); |
|
142 | - } |
|
120 | + /** |
|
121 | + * Returns an array where the key corresponds to the incoming attribute name from the WP block |
|
122 | + * and the value corresponds to the attribute name for the existing EspressoEventAttendees shortcode |
|
123 | + * |
|
124 | + * @since 4.9.71.p |
|
125 | + * @return array |
|
126 | + */ |
|
127 | + private function getAttributesMap() |
|
128 | + { |
|
129 | + return array( |
|
130 | + 'eventId' => 'absint', |
|
131 | + 'datetimeId' => 'absint', |
|
132 | + 'ticketId' => 'absint', |
|
133 | + 'status' => 'sanitize_text_field', |
|
134 | + 'limit' => 'intval', |
|
135 | + 'showGravatar' => 'bool', |
|
136 | + 'avatarClass' => 'sanitize_text_field', |
|
137 | + 'avatarSize' => 'absint', |
|
138 | + 'displayOnArchives' => 'bool', |
|
139 | + 'order' => 'sanitize_text_field', |
|
140 | + 'orderBy' => 'sanitize_text_field', |
|
141 | + ); |
|
142 | + } |
|
143 | 143 | |
144 | 144 | |
145 | - /** |
|
146 | - * Sanitizes attributes. |
|
147 | - * |
|
148 | - * @param array $attributes |
|
149 | - * @return array |
|
150 | - */ |
|
151 | - private function sanitizeAttributes(array $attributes) |
|
152 | - { |
|
153 | - $sanitized_attributes = array(); |
|
154 | - foreach ($attributes as $attribute => $value) { |
|
155 | - $convert = $this->getAttributesMap(); |
|
156 | - if (isset($convert[ $attribute ])) { |
|
157 | - $sanitize = $convert[ $attribute ]; |
|
158 | - if ($sanitize === 'bool') { |
|
159 | - $sanitized_attributes[ $attribute ] = filter_var( |
|
160 | - $value, |
|
161 | - FILTER_VALIDATE_BOOLEAN |
|
162 | - ); |
|
163 | - } else { |
|
164 | - $sanitized_attributes[ $attribute ] = $sanitize($value); |
|
165 | - } |
|
166 | - // don't pass along attributes with a 0 value |
|
167 | - if ($sanitized_attributes[ $attribute ] === 0) { |
|
168 | - unset($sanitized_attributes[ $attribute ]); |
|
169 | - } |
|
170 | - } |
|
171 | - } |
|
172 | - return $attributes; |
|
173 | - } |
|
145 | + /** |
|
146 | + * Sanitizes attributes. |
|
147 | + * |
|
148 | + * @param array $attributes |
|
149 | + * @return array |
|
150 | + */ |
|
151 | + private function sanitizeAttributes(array $attributes) |
|
152 | + { |
|
153 | + $sanitized_attributes = array(); |
|
154 | + foreach ($attributes as $attribute => $value) { |
|
155 | + $convert = $this->getAttributesMap(); |
|
156 | + if (isset($convert[ $attribute ])) { |
|
157 | + $sanitize = $convert[ $attribute ]; |
|
158 | + if ($sanitize === 'bool') { |
|
159 | + $sanitized_attributes[ $attribute ] = filter_var( |
|
160 | + $value, |
|
161 | + FILTER_VALIDATE_BOOLEAN |
|
162 | + ); |
|
163 | + } else { |
|
164 | + $sanitized_attributes[ $attribute ] = $sanitize($value); |
|
165 | + } |
|
166 | + // don't pass along attributes with a 0 value |
|
167 | + if ($sanitized_attributes[ $attribute ] === 0) { |
|
168 | + unset($sanitized_attributes[ $attribute ]); |
|
169 | + } |
|
170 | + } |
|
171 | + } |
|
172 | + return $attributes; |
|
173 | + } |
|
174 | 174 | |
175 | 175 | |
176 | - /** |
|
177 | - * Returns the rendered HTML for the block |
|
178 | - * |
|
179 | - * @param array $attributes |
|
180 | - * @return string |
|
181 | - * @throws DomainException |
|
182 | - * @throws EE_Error |
|
183 | - */ |
|
184 | - public function renderBlock(array $attributes = array()) |
|
185 | - { |
|
186 | - $attributes = $this->sanitizeAttributes($attributes); |
|
187 | - return (is_archive() || is_front_page() || is_home()) && ! $attributes['displayOnArchives'] |
|
188 | - ? '' |
|
189 | - : $this->renderer->render($attributes); |
|
190 | - } |
|
176 | + /** |
|
177 | + * Returns the rendered HTML for the block |
|
178 | + * |
|
179 | + * @param array $attributes |
|
180 | + * @return string |
|
181 | + * @throws DomainException |
|
182 | + * @throws EE_Error |
|
183 | + */ |
|
184 | + public function renderBlock(array $attributes = array()) |
|
185 | + { |
|
186 | + $attributes = $this->sanitizeAttributes($attributes); |
|
187 | + return (is_archive() || is_front_page() || is_home()) && ! $attributes['displayOnArchives'] |
|
188 | + ? '' |
|
189 | + : $this->renderer->render($attributes); |
|
190 | + } |
|
191 | 191 | } |
@@ -17,85 +17,85 @@ |
||
17 | 17 | */ |
18 | 18 | class EE_Restriction_Generator_Default_Protected extends EE_Restriction_Generator_Base |
19 | 19 | { |
20 | - /** |
|
21 | - * Name of the field on this model (or a related model, including the model chain to it) |
|
22 | - * that is a boolean indicating whether or not a model object is considered "Default" or not |
|
23 | - * @var string |
|
24 | - */ |
|
25 | - protected $_default_field_name; |
|
20 | + /** |
|
21 | + * Name of the field on this model (or a related model, including the model chain to it) |
|
22 | + * that is a boolean indicating whether or not a model object is considered "Default" or not |
|
23 | + * @var string |
|
24 | + */ |
|
25 | + protected $_default_field_name; |
|
26 | 26 | |
27 | - /** |
|
28 | - * The model chain to follow to get to the event model, including the event model itself. |
|
29 | - * Eg 'Ticket.Datetime.Event' |
|
30 | - * @var string |
|
31 | - */ |
|
32 | - protected $_path_to_event_model; |
|
33 | - /** |
|
34 | - * |
|
35 | - * @param string $default_field_name the name of the field Name of the field on this model (or a related model, including the model chain to it) |
|
36 | - * that is a boolean indicating whether or not a model object is considered "Default" or not |
|
37 | - * @param string $path_to_event_model The model chain to follow to get to the event model, including the event model itself. |
|
38 | - * Eg 'Ticket.Datetime.Event' |
|
39 | - */ |
|
40 | - public function __construct($default_field_name, $path_to_event_model) |
|
41 | - { |
|
42 | - $this->_default_field_name = $default_field_name; |
|
43 | - if (substr($path_to_event_model, -1, 1) != '.') { |
|
44 | - $path_to_event_model .= '.'; |
|
45 | - } |
|
46 | - $this->_path_to_event_model = $path_to_event_model; |
|
47 | - } |
|
27 | + /** |
|
28 | + * The model chain to follow to get to the event model, including the event model itself. |
|
29 | + * Eg 'Ticket.Datetime.Event' |
|
30 | + * @var string |
|
31 | + */ |
|
32 | + protected $_path_to_event_model; |
|
33 | + /** |
|
34 | + * |
|
35 | + * @param string $default_field_name the name of the field Name of the field on this model (or a related model, including the model chain to it) |
|
36 | + * that is a boolean indicating whether or not a model object is considered "Default" or not |
|
37 | + * @param string $path_to_event_model The model chain to follow to get to the event model, including the event model itself. |
|
38 | + * Eg 'Ticket.Datetime.Event' |
|
39 | + */ |
|
40 | + public function __construct($default_field_name, $path_to_event_model) |
|
41 | + { |
|
42 | + $this->_default_field_name = $default_field_name; |
|
43 | + if (substr($path_to_event_model, -1, 1) != '.') { |
|
44 | + $path_to_event_model .= '.'; |
|
45 | + } |
|
46 | + $this->_path_to_event_model = $path_to_event_model; |
|
47 | + } |
|
48 | 48 | |
49 | 49 | |
50 | 50 | |
51 | - /** |
|
52 | - * |
|
53 | - * @return \EE_Default_Where_Conditions |
|
54 | - */ |
|
55 | - protected function _generate_restrictions() |
|
56 | - { |
|
57 | - // if there are no standard caps for this model, then for now all we know is |
|
58 | - // if they need the default cap to access this |
|
59 | - if (! $this->model()->cap_slug()) { |
|
60 | - return array( |
|
61 | - self::get_default_restrictions_cap() => new EE_Return_None_Where_Conditions() |
|
62 | - ); |
|
63 | - } |
|
51 | + /** |
|
52 | + * |
|
53 | + * @return \EE_Default_Where_Conditions |
|
54 | + */ |
|
55 | + protected function _generate_restrictions() |
|
56 | + { |
|
57 | + // if there are no standard caps for this model, then for now all we know is |
|
58 | + // if they need the default cap to access this |
|
59 | + if (! $this->model()->cap_slug()) { |
|
60 | + return array( |
|
61 | + self::get_default_restrictions_cap() => new EE_Return_None_Where_Conditions() |
|
62 | + ); |
|
63 | + } |
|
64 | 64 | |
65 | - $event_model = EEM_Event::instance(); |
|
65 | + $event_model = EEM_Event::instance(); |
|
66 | 66 | |
67 | - $restrictions = array( |
|
68 | - // first: basically access to non-defaults is essentially controlled by which events are accessible |
|
69 | - // if they don't have the basic event cap, they can't access ANY non-default items |
|
70 | - EE_Restriction_Generator_Base::get_cap_name($event_model, $this->action()) => new EE_Default_Where_Conditions(array( $this->_default_field_name => true )), |
|
71 | - // if they don't have the others event cap, they can't access others' non-default items |
|
72 | - EE_Restriction_Generator_Base::get_cap_name($event_model, $this->action() . '_others') => new EE_Default_Where_Conditions(array( |
|
73 | - 'OR*no_' . EE_Restriction_Generator_Base::get_cap_name($event_model, $this->action() . '_others') => array( |
|
74 | - $this->_path_to_event_model . 'EVT_wp_user' => EE_Default_Where_Conditions::current_user_placeholder ), |
|
75 | - $this->_default_field_name => true )), |
|
76 | - // if they have basic and others, but not private, they can't access others' private non-default items |
|
77 | - EE_Restriction_Generator_Base::get_cap_name($event_model, $this->action() . '_private') => new EE_Default_Where_Conditions(array( |
|
78 | - 'OR*no_' . EE_Restriction_Generator_Base::get_cap_name($event_model, $this->action() . '_private') => array( |
|
79 | - $this->_path_to_event_model . 'EVT_wp_user' => EE_Default_Where_Conditions::current_user_placeholder, |
|
80 | - $this->_path_to_event_model . 'status' => array( '!=', 'private' ), |
|
81 | - $this->_default_field_name => true ) )), |
|
82 | - // second: access to defaults is controlled by the defaulty capabilities |
|
83 | - // if they don't have the default capability, restrict access to only non-default items |
|
84 | - EE_Restriction_Generator_Base::get_cap_name($this->model(), $this->action() . '_default') => new EE_Default_Where_Conditions(array( $this->_default_field_name => false )), |
|
85 | - // if they don't have the "others" default capability, restrict access to only their default ones, and non-default ones |
|
86 | - ); |
|
87 | - if (EE_Restriction_Generator_Base::is_cap($this->model(), $this->action() . '_others_default')) { |
|
88 | - $restrictions[ EE_Restriction_Generator_Base::get_cap_name($this->model(), $this->action() . '_others_default') ] = new EE_Default_Where_Conditions(array( |
|
89 | - // if they don't have the others default cap, they can't access others default items (but they can access |
|
90 | - // their own default items, and non-default items) |
|
91 | - 'OR*no_' . EE_Restriction_Generator_Base::get_cap_name($this->model(), $this->action() . '_others_default') => array( |
|
92 | - 'AND' => array( |
|
93 | - $this->_path_to_event_model . 'EVT_wp_user' => EE_Default_Where_Conditions::current_user_placeholder, |
|
94 | - $this->_default_field_name => true |
|
95 | - ), |
|
96 | - $this->_default_field_name => false |
|
97 | - ) )); |
|
98 | - } |
|
99 | - return $restrictions; |
|
100 | - } |
|
67 | + $restrictions = array( |
|
68 | + // first: basically access to non-defaults is essentially controlled by which events are accessible |
|
69 | + // if they don't have the basic event cap, they can't access ANY non-default items |
|
70 | + EE_Restriction_Generator_Base::get_cap_name($event_model, $this->action()) => new EE_Default_Where_Conditions(array( $this->_default_field_name => true )), |
|
71 | + // if they don't have the others event cap, they can't access others' non-default items |
|
72 | + EE_Restriction_Generator_Base::get_cap_name($event_model, $this->action() . '_others') => new EE_Default_Where_Conditions(array( |
|
73 | + 'OR*no_' . EE_Restriction_Generator_Base::get_cap_name($event_model, $this->action() . '_others') => array( |
|
74 | + $this->_path_to_event_model . 'EVT_wp_user' => EE_Default_Where_Conditions::current_user_placeholder ), |
|
75 | + $this->_default_field_name => true )), |
|
76 | + // if they have basic and others, but not private, they can't access others' private non-default items |
|
77 | + EE_Restriction_Generator_Base::get_cap_name($event_model, $this->action() . '_private') => new EE_Default_Where_Conditions(array( |
|
78 | + 'OR*no_' . EE_Restriction_Generator_Base::get_cap_name($event_model, $this->action() . '_private') => array( |
|
79 | + $this->_path_to_event_model . 'EVT_wp_user' => EE_Default_Where_Conditions::current_user_placeholder, |
|
80 | + $this->_path_to_event_model . 'status' => array( '!=', 'private' ), |
|
81 | + $this->_default_field_name => true ) )), |
|
82 | + // second: access to defaults is controlled by the defaulty capabilities |
|
83 | + // if they don't have the default capability, restrict access to only non-default items |
|
84 | + EE_Restriction_Generator_Base::get_cap_name($this->model(), $this->action() . '_default') => new EE_Default_Where_Conditions(array( $this->_default_field_name => false )), |
|
85 | + // if they don't have the "others" default capability, restrict access to only their default ones, and non-default ones |
|
86 | + ); |
|
87 | + if (EE_Restriction_Generator_Base::is_cap($this->model(), $this->action() . '_others_default')) { |
|
88 | + $restrictions[ EE_Restriction_Generator_Base::get_cap_name($this->model(), $this->action() . '_others_default') ] = new EE_Default_Where_Conditions(array( |
|
89 | + // if they don't have the others default cap, they can't access others default items (but they can access |
|
90 | + // their own default items, and non-default items) |
|
91 | + 'OR*no_' . EE_Restriction_Generator_Base::get_cap_name($this->model(), $this->action() . '_others_default') => array( |
|
92 | + 'AND' => array( |
|
93 | + $this->_path_to_event_model . 'EVT_wp_user' => EE_Default_Where_Conditions::current_user_placeholder, |
|
94 | + $this->_default_field_name => true |
|
95 | + ), |
|
96 | + $this->_default_field_name => false |
|
97 | + ) )); |
|
98 | + } |
|
99 | + return $restrictions; |
|
100 | + } |
|
101 | 101 | } |
@@ -56,7 +56,7 @@ discard block |
||
56 | 56 | { |
57 | 57 | // if there are no standard caps for this model, then for now all we know is |
58 | 58 | // if they need the default cap to access this |
59 | - if (! $this->model()->cap_slug()) { |
|
59 | + if ( ! $this->model()->cap_slug()) { |
|
60 | 60 | return array( |
61 | 61 | self::get_default_restrictions_cap() => new EE_Return_None_Where_Conditions() |
62 | 62 | ); |
@@ -64,33 +64,33 @@ discard block |
||
64 | 64 | |
65 | 65 | $event_model = EEM_Event::instance(); |
66 | 66 | |
67 | - $restrictions = array( |
|
67 | + $restrictions = array( |
|
68 | 68 | // first: basically access to non-defaults is essentially controlled by which events are accessible |
69 | 69 | // if they don't have the basic event cap, they can't access ANY non-default items |
70 | - EE_Restriction_Generator_Base::get_cap_name($event_model, $this->action()) => new EE_Default_Where_Conditions(array( $this->_default_field_name => true )), |
|
70 | + EE_Restriction_Generator_Base::get_cap_name($event_model, $this->action()) => new EE_Default_Where_Conditions(array($this->_default_field_name => true)), |
|
71 | 71 | // if they don't have the others event cap, they can't access others' non-default items |
72 | - EE_Restriction_Generator_Base::get_cap_name($event_model, $this->action() . '_others') => new EE_Default_Where_Conditions(array( |
|
73 | - 'OR*no_' . EE_Restriction_Generator_Base::get_cap_name($event_model, $this->action() . '_others') => array( |
|
74 | - $this->_path_to_event_model . 'EVT_wp_user' => EE_Default_Where_Conditions::current_user_placeholder ), |
|
72 | + EE_Restriction_Generator_Base::get_cap_name($event_model, $this->action().'_others') => new EE_Default_Where_Conditions(array( |
|
73 | + 'OR*no_'.EE_Restriction_Generator_Base::get_cap_name($event_model, $this->action().'_others') => array( |
|
74 | + $this->_path_to_event_model.'EVT_wp_user' => EE_Default_Where_Conditions::current_user_placeholder ), |
|
75 | 75 | $this->_default_field_name => true )), |
76 | 76 | // if they have basic and others, but not private, they can't access others' private non-default items |
77 | - EE_Restriction_Generator_Base::get_cap_name($event_model, $this->action() . '_private') => new EE_Default_Where_Conditions(array( |
|
78 | - 'OR*no_' . EE_Restriction_Generator_Base::get_cap_name($event_model, $this->action() . '_private') => array( |
|
79 | - $this->_path_to_event_model . 'EVT_wp_user' => EE_Default_Where_Conditions::current_user_placeholder, |
|
80 | - $this->_path_to_event_model . 'status' => array( '!=', 'private' ), |
|
77 | + EE_Restriction_Generator_Base::get_cap_name($event_model, $this->action().'_private') => new EE_Default_Where_Conditions(array( |
|
78 | + 'OR*no_'.EE_Restriction_Generator_Base::get_cap_name($event_model, $this->action().'_private') => array( |
|
79 | + $this->_path_to_event_model.'EVT_wp_user' => EE_Default_Where_Conditions::current_user_placeholder, |
|
80 | + $this->_path_to_event_model.'status' => array('!=', 'private'), |
|
81 | 81 | $this->_default_field_name => true ) )), |
82 | 82 | // second: access to defaults is controlled by the defaulty capabilities |
83 | 83 | // if they don't have the default capability, restrict access to only non-default items |
84 | - EE_Restriction_Generator_Base::get_cap_name($this->model(), $this->action() . '_default') => new EE_Default_Where_Conditions(array( $this->_default_field_name => false )), |
|
84 | + EE_Restriction_Generator_Base::get_cap_name($this->model(), $this->action().'_default') => new EE_Default_Where_Conditions(array($this->_default_field_name => false)), |
|
85 | 85 | // if they don't have the "others" default capability, restrict access to only their default ones, and non-default ones |
86 | 86 | ); |
87 | - if (EE_Restriction_Generator_Base::is_cap($this->model(), $this->action() . '_others_default')) { |
|
88 | - $restrictions[ EE_Restriction_Generator_Base::get_cap_name($this->model(), $this->action() . '_others_default') ] = new EE_Default_Where_Conditions(array( |
|
87 | + if (EE_Restriction_Generator_Base::is_cap($this->model(), $this->action().'_others_default')) { |
|
88 | + $restrictions[EE_Restriction_Generator_Base::get_cap_name($this->model(), $this->action().'_others_default')] = new EE_Default_Where_Conditions(array( |
|
89 | 89 | // if they don't have the others default cap, they can't access others default items (but they can access |
90 | 90 | // their own default items, and non-default items) |
91 | - 'OR*no_' . EE_Restriction_Generator_Base::get_cap_name($this->model(), $this->action() . '_others_default') => array( |
|
91 | + 'OR*no_'.EE_Restriction_Generator_Base::get_cap_name($this->model(), $this->action().'_others_default') => array( |
|
92 | 92 | 'AND' => array( |
93 | - $this->_path_to_event_model . 'EVT_wp_user' => EE_Default_Where_Conditions::current_user_placeholder, |
|
93 | + $this->_path_to_event_model.'EVT_wp_user' => EE_Default_Where_Conditions::current_user_placeholder, |
|
94 | 94 | $this->_default_field_name => true |
95 | 95 | ), |
96 | 96 | $this->_default_field_name => false |
@@ -216,7 +216,7 @@ discard block |
||
216 | 216 | { |
217 | 217 | if (EEH_Event_Query::apply_query_filters($wp_query)) { |
218 | 218 | global $wpdb; |
219 | - $clauses['groupby'] = $wpdb->posts . '.ID '; |
|
219 | + $clauses['groupby'] = $wpdb->posts.'.ID '; |
|
220 | 220 | } |
221 | 221 | return $clauses; |
222 | 222 | } |
@@ -251,23 +251,23 @@ discard block |
||
251 | 251 | */ |
252 | 252 | public static function posts_fields_sql_for_orderby(array $orderby_params = []) |
253 | 253 | { |
254 | - $SQL = ', MIN( ' . EEM_Datetime::instance()->table() . '.DTT_EVT_start ) as event_start_date '; |
|
254 | + $SQL = ', MIN( '.EEM_Datetime::instance()->table().'.DTT_EVT_start ) as event_start_date '; |
|
255 | 255 | foreach ($orderby_params as $orderby) { |
256 | 256 | switch ($orderby) { |
257 | 257 | case 'ticket_start': |
258 | - $SQL .= ', ' . EEM_Ticket::instance()->table() . '.TKT_start_date'; |
|
258 | + $SQL .= ', '.EEM_Ticket::instance()->table().'.TKT_start_date'; |
|
259 | 259 | break; |
260 | 260 | case 'ticket_end': |
261 | - $SQL .= ', ' . EEM_Ticket::instance()->table() . '.TKT_end_date'; |
|
261 | + $SQL .= ', '.EEM_Ticket::instance()->table().'.TKT_end_date'; |
|
262 | 262 | break; |
263 | 263 | case 'venue_title': |
264 | 264 | $SQL .= ', Venue.post_title AS venue_title'; |
265 | 265 | break; |
266 | 266 | case 'city': |
267 | - $SQL .= ', ' . EEM_Venue::instance()->second_table() . '.VNU_city'; |
|
267 | + $SQL .= ', '.EEM_Venue::instance()->second_table().'.VNU_city'; |
|
268 | 268 | break; |
269 | 269 | case 'state': |
270 | - $SQL .= ', ' . EEM_State::instance()->table() . '.STA_name'; |
|
270 | + $SQL .= ', '.EEM_State::instance()->table().'.STA_name'; |
|
271 | 271 | break; |
272 | 272 | } |
273 | 273 | } |
@@ -307,12 +307,12 @@ discard block |
||
307 | 307 | */ |
308 | 308 | public static function posts_join_sql_for_show_expired($SQL = '', $show_expired = false) |
309 | 309 | { |
310 | - if (! $show_expired) { |
|
311 | - $join = EEM_Event::instance()->table() . '.ID = '; |
|
312 | - $join .= EEM_Datetime::instance()->table() . '.' . EEM_Event::instance()->primary_key_name(); |
|
310 | + if ( ! $show_expired) { |
|
311 | + $join = EEM_Event::instance()->table().'.ID = '; |
|
312 | + $join .= EEM_Datetime::instance()->table().'.'.EEM_Event::instance()->primary_key_name(); |
|
313 | 313 | // don't add if this is already in the SQL |
314 | 314 | if (strpos($SQL, $join) === false) { |
315 | - $SQL .= ' INNER JOIN ' . EEM_Datetime::instance()->table() . ' ON ( ' . $join . ' ) '; |
|
315 | + $SQL .= ' INNER JOIN '.EEM_Datetime::instance()->table().' ON ( '.$join.' ) '; |
|
316 | 316 | } |
317 | 317 | } |
318 | 318 | return $SQL; |
@@ -327,7 +327,7 @@ discard block |
||
327 | 327 | */ |
328 | 328 | public static function posts_join_sql_for_terms($SQL = '', $join_terms = '') |
329 | 329 | { |
330 | - if (! empty($join_terms)) { |
|
330 | + if ( ! empty($join_terms)) { |
|
331 | 331 | global $wpdb; |
332 | 332 | $SQL .= " LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)"; |
333 | 333 | $SQL .= " LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)"; |
@@ -356,13 +356,13 @@ discard block |
||
356 | 356 | case 'ticket_end': |
357 | 357 | $SQL .= EEH_Event_Query::_posts_join_for_datetime( |
358 | 358 | $SQL, |
359 | - EEM_Datetime_Ticket::instance()->table() . '.' . EEM_Datetime::instance()->primary_key_name() |
|
359 | + EEM_Datetime_Ticket::instance()->table().'.'.EEM_Datetime::instance()->primary_key_name() |
|
360 | 360 | ); |
361 | - $SQL .= ' LEFT JOIN ' . EEM_Ticket::instance()->table(); |
|
361 | + $SQL .= ' LEFT JOIN '.EEM_Ticket::instance()->table(); |
|
362 | 362 | $SQL .= ' ON ('; |
363 | - $SQL .= EEM_Datetime_Ticket::instance()->table() . '.' . EEM_Ticket::instance()->primary_key_name(); |
|
363 | + $SQL .= EEM_Datetime_Ticket::instance()->table().'.'.EEM_Ticket::instance()->primary_key_name(); |
|
364 | 364 | $SQL .= ' = '; |
365 | - $SQL .= EEM_Ticket::instance()->table() . '.' . EEM_Ticket::instance()->primary_key_name(); |
|
365 | + $SQL .= EEM_Ticket::instance()->table().'.'.EEM_Ticket::instance()->primary_key_name(); |
|
366 | 366 | $SQL .= ' )'; |
367 | 367 | break; |
368 | 368 | case 'venue_title': |
@@ -375,7 +375,7 @@ discard block |
||
375 | 375 | break; |
376 | 376 | case 'start_date': |
377 | 377 | default: |
378 | - $SQL .= EEH_Event_Query::_posts_join_for_datetime($SQL, EEM_Event::instance()->table() . '.ID'); |
|
378 | + $SQL .= EEH_Event_Query::_posts_join_for_datetime($SQL, EEM_Event::instance()->table().'.ID'); |
|
379 | 379 | break; |
380 | 380 | } |
381 | 381 | } |
@@ -394,10 +394,10 @@ discard block |
||
394 | 394 | */ |
395 | 395 | protected static function _posts_join_for_datetime($SQL = '', $join = '') |
396 | 396 | { |
397 | - if (! empty($join)) { |
|
398 | - $join .= ' = ' . EEM_Datetime::instance()->table() . '.' . EEM_Event::instance()->primary_key_name(); |
|
397 | + if ( ! empty($join)) { |
|
398 | + $join .= ' = '.EEM_Datetime::instance()->table().'.'.EEM_Event::instance()->primary_key_name(); |
|
399 | 399 | if (strpos($SQL, $join) === false) { |
400 | - return ' INNER JOIN ' . EEM_Datetime::instance()->table() . ' ON ( ' . $join . ' )'; |
|
400 | + return ' INNER JOIN '.EEM_Datetime::instance()->table().' ON ( '.$join.' )'; |
|
401 | 401 | } |
402 | 402 | } |
403 | 403 | return ''; |
@@ -417,8 +417,8 @@ discard block |
||
417 | 417 | // Event Venue table name |
418 | 418 | $event_venue_table = EEM_Event_Venue::instance()->table(); |
419 | 419 | // generate conditions for: Event <=> Event Venue JOIN clause |
420 | - $event_to_event_venue_join = EEM_Event::instance()->table() . '.ID = '; |
|
421 | - $event_to_event_venue_join .= $event_venue_table . '.' . EEM_Event::instance()->primary_key_name(); |
|
420 | + $event_to_event_venue_join = EEM_Event::instance()->table().'.ID = '; |
|
421 | + $event_to_event_venue_join .= $event_venue_table.'.'.EEM_Event::instance()->primary_key_name(); |
|
422 | 422 | // don't add joins if they have already been added |
423 | 423 | if (strpos($SQL, $event_to_event_venue_join) === false) { |
424 | 424 | // Venue table name |
@@ -506,7 +506,7 @@ discard block |
||
506 | 506 | public static function posts_where_sql_for_show_expired($show_expired = false) |
507 | 507 | { |
508 | 508 | return ! $show_expired |
509 | - ? ' AND ' . EEM_Datetime::instance()->table() . '.DTT_EVT_end > \'' . current_time('mysql', true) . '\' ' |
|
509 | + ? ' AND '.EEM_Datetime::instance()->table().'.DTT_EVT_end > \''.current_time('mysql', true).'\' ' |
|
510 | 510 | : ''; |
511 | 511 | } |
512 | 512 | |
@@ -518,7 +518,7 @@ discard block |
||
518 | 518 | public static function posts_where_sql_for_event_category_slug($event_category_slug = null) |
519 | 519 | { |
520 | 520 | global $wpdb; |
521 | - if (! empty($event_category_slug)) { |
|
521 | + if ( ! empty($event_category_slug)) { |
|
522 | 522 | $event_category_slugs_array = array_map('trim', explode(',', $event_category_slug)); |
523 | 523 | $event_category_slugs_prepare = implode(', ', array_fill(0, count($event_category_slugs_array), '%s')); |
524 | 524 | return $wpdb->prepare( |
@@ -541,14 +541,14 @@ discard block |
||
541 | 541 | public static function posts_where_sql_for_event_list_month($month = null) |
542 | 542 | { |
543 | 543 | $SQL = ''; |
544 | - if (! empty($month)) { |
|
544 | + if ( ! empty($month)) { |
|
545 | 545 | $datetime_table = EEM_Datetime::instance()->table(); |
546 | 546 | // event start date is LESS than the end of the month ( so nothing that doesn't start until next month ) |
547 | 547 | $SQL = " AND {$datetime_table}.DTT_EVT_start <= '"; |
548 | - $SQL .= date('Y-m-t 23:59:59', EEH_DTT_Helper::first_of_month_timestamp($month)) . "'"; |
|
548 | + $SQL .= date('Y-m-t 23:59:59', EEH_DTT_Helper::first_of_month_timestamp($month))."'"; |
|
549 | 549 | // event end date is GREATER than the start of the month ( so nothing that ended before this month ) |
550 | 550 | $SQL .= " AND {$datetime_table}.DTT_EVT_end >= '"; |
551 | - $SQL .= date('Y-m-01 0:0:00', EEH_DTT_Helper::first_of_month_timestamp($month)) . "' "; |
|
551 | + $SQL .= date('Y-m-01 0:0:00', EEH_DTT_Helper::first_of_month_timestamp($month))."' "; |
|
552 | 552 | } |
553 | 553 | return $SQL; |
554 | 554 | } |
@@ -609,15 +609,15 @@ discard block |
||
609 | 609 | ? strtoupper($sort) |
610 | 610 | : 'ASC'; |
611 | 611 | // make sure 'orderby' is set in query params |
612 | - if (! isset(self::$_query_params['orderby'])) { |
|
612 | + if ( ! isset(self::$_query_params['orderby'])) { |
|
613 | 613 | self::$_query_params['orderby'] = []; |
614 | 614 | } |
615 | 615 | // loop thru $orderby_params (type cast as array) |
616 | 616 | foreach ($orderby_params as $orderby) { |
617 | 617 | // check if we have already added this param |
618 | - if (isset(self::$_query_params['orderby'][ $orderby ])) { |
|
618 | + if (isset(self::$_query_params['orderby'][$orderby])) { |
|
619 | 619 | // if so then remove from the $orderby_params so that the count() method below is accurate |
620 | - unset($orderby_params[ $orderby ]); |
|
620 | + unset($orderby_params[$orderby]); |
|
621 | 621 | // then bump ahead to the next param |
622 | 622 | continue; |
623 | 623 | } |
@@ -627,39 +627,39 @@ discard block |
||
627 | 627 | switch ($orderby) { |
628 | 628 | case 'id': |
629 | 629 | case 'ID': |
630 | - $SQL .= $glue . $wpdb->posts . '.ID ' . $sort; |
|
630 | + $SQL .= $glue.$wpdb->posts.'.ID '.$sort; |
|
631 | 631 | break; |
632 | 632 | case 'end_date': |
633 | - $SQL .= $glue . EEM_Datetime::instance()->table() . '.DTT_EVT_end ' . $sort; |
|
633 | + $SQL .= $glue.EEM_Datetime::instance()->table().'.DTT_EVT_end '.$sort; |
|
634 | 634 | break; |
635 | 635 | case 'event_name': |
636 | - $SQL .= $glue . $wpdb->posts . '.post_title ' . $sort; |
|
636 | + $SQL .= $glue.$wpdb->posts.'.post_title '.$sort; |
|
637 | 637 | break; |
638 | 638 | case 'category_slug': |
639 | - $SQL .= $glue . $wpdb->terms . '.slug ' . $sort; |
|
639 | + $SQL .= $glue.$wpdb->terms.'.slug '.$sort; |
|
640 | 640 | break; |
641 | 641 | case 'ticket_start': |
642 | - $SQL .= $glue . EEM_Ticket::instance()->table() . '.TKT_start_date ' . $sort; |
|
642 | + $SQL .= $glue.EEM_Ticket::instance()->table().'.TKT_start_date '.$sort; |
|
643 | 643 | break; |
644 | 644 | case 'ticket_end': |
645 | - $SQL .= $glue . EEM_Ticket::instance()->table() . '.TKT_end_date ' . $sort; |
|
645 | + $SQL .= $glue.EEM_Ticket::instance()->table().'.TKT_end_date '.$sort; |
|
646 | 646 | break; |
647 | 647 | case 'venue_title': |
648 | - $SQL .= $glue . 'venue_title ' . $sort; |
|
648 | + $SQL .= $glue.'venue_title '.$sort; |
|
649 | 649 | break; |
650 | 650 | case 'city': |
651 | - $SQL .= $glue . EEM_Venue::instance()->second_table() . '.VNU_city ' . $sort; |
|
651 | + $SQL .= $glue.EEM_Venue::instance()->second_table().'.VNU_city '.$sort; |
|
652 | 652 | break; |
653 | 653 | case 'state': |
654 | - $SQL .= $glue . EEM_State::instance()->table() . '.STA_name ' . $sort; |
|
654 | + $SQL .= $glue.EEM_State::instance()->table().'.STA_name '.$sort; |
|
655 | 655 | break; |
656 | 656 | case 'start_date': |
657 | 657 | default: |
658 | - $SQL .= $glue . ' event_start_date ' . $sort; |
|
658 | + $SQL .= $glue.' event_start_date '.$sort; |
|
659 | 659 | break; |
660 | 660 | } |
661 | 661 | // add to array of orderby params that have been added |
662 | - self::$_query_params['orderby'][ $orderby ] = true; |
|
662 | + self::$_query_params['orderby'][$orderby] = true; |
|
663 | 663 | $counter++; |
664 | 664 | } |
665 | 665 | return $SQL; |
@@ -17,666 +17,666 @@ |
||
17 | 17 | */ |
18 | 18 | class EEH_Event_Query |
19 | 19 | { |
20 | - /** |
|
21 | - * Start Date |
|
22 | - * |
|
23 | - * @var $_event_query_month |
|
24 | - */ |
|
25 | - protected static $_event_query_month; |
|
26 | - |
|
27 | - /** |
|
28 | - * Category |
|
29 | - * |
|
30 | - * @var $_event_query_category |
|
31 | - */ |
|
32 | - protected static $_event_query_category; |
|
33 | - |
|
34 | - /** |
|
35 | - * whether to display expired events in the event list |
|
36 | - * |
|
37 | - * @var bool $_show_expired |
|
38 | - */ |
|
39 | - protected static $_event_query_show_expired = false; |
|
40 | - |
|
41 | - /** |
|
42 | - * list of params for controlling how the query results are ordered |
|
43 | - * |
|
44 | - * @var array $_event_query_orderby |
|
45 | - */ |
|
46 | - protected static $_event_query_orderby = []; |
|
47 | - |
|
48 | - /** |
|
49 | - * direction list is sorted |
|
50 | - * |
|
51 | - * @var string $_event_query_sort |
|
52 | - */ |
|
53 | - protected static $_event_query_sort; |
|
54 | - |
|
55 | - /** |
|
56 | - * list of params used to build the query's various clauses |
|
57 | - * |
|
58 | - * @var $_query_params |
|
59 | - */ |
|
60 | - protected static $_query_params = []; |
|
61 | - |
|
62 | - |
|
63 | - /** |
|
64 | - * @return void |
|
65 | - */ |
|
66 | - public static function add_query_filters() |
|
67 | - { |
|
68 | - // add query filters |
|
69 | - add_action('pre_get_posts', ['EEH_Event_Query', 'filter_query_parts'], 10, 1); |
|
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', ['EEH_Event_Query', 'posts_fields'], 10, 2); |
|
98 | - add_filter('posts_join', ['EEH_Event_Query', 'posts_join'], 10, 2); |
|
99 | - add_filter('posts_where', ['EEH_Event_Query', 'posts_where'], 10, 2); |
|
100 | - add_filter('posts_orderby', ['EEH_Event_Query', 'posts_orderby'], 10, 2); |
|
101 | - add_filter('posts_clauses_request', ['EEH_Event_Query', 'posts_clauses'], 10, 2); |
|
102 | - } |
|
103 | - } |
|
104 | - |
|
105 | - |
|
106 | - /** |
|
107 | - * @param string $month |
|
108 | - * @param string $category |
|
109 | - * @param bool $show_expired |
|
110 | - * @param array|string $orderby |
|
111 | - * @param string $sort |
|
112 | - * @throws InvalidArgumentException |
|
113 | - * @throws InvalidDataTypeException |
|
114 | - * @throws InvalidInterfaceException |
|
115 | - */ |
|
116 | - public static function set_query_params( |
|
117 | - $month = '', |
|
118 | - $category = '', |
|
119 | - $show_expired = false, |
|
120 | - $orderby = 'start_date', |
|
121 | - $sort = 'ASC' |
|
122 | - ) { |
|
123 | - self::$_query_params = []; |
|
124 | - EEH_Event_Query::$_event_query_month = EEH_Event_Query::_display_month($month); |
|
125 | - EEH_Event_Query::$_event_query_category = EEH_Event_Query::_event_category_slug($category); |
|
126 | - EEH_Event_Query::$_event_query_show_expired = EEH_Event_Query::_show_expired($show_expired); |
|
127 | - EEH_Event_Query::$_event_query_orderby = EEH_Event_Query::_orderby($orderby); |
|
128 | - EEH_Event_Query::$_event_query_sort = EEH_Event_Query::_sort($sort); |
|
129 | - } |
|
130 | - |
|
131 | - |
|
132 | - /** |
|
133 | - * what month should the event list display events for? |
|
134 | - * |
|
135 | - * @param string $month |
|
136 | - * @return string |
|
137 | - * @throws InvalidArgumentException |
|
138 | - * @throws InvalidDataTypeException |
|
139 | - * @throws InvalidInterfaceException |
|
140 | - */ |
|
141 | - private static function _display_month($month = '') |
|
142 | - { |
|
143 | - return self::getRequest()->getRequestParam('event_query_month', $month); |
|
144 | - } |
|
145 | - |
|
146 | - |
|
147 | - /** |
|
148 | - * @param string $category |
|
149 | - * @return string |
|
150 | - * @throws InvalidArgumentException |
|
151 | - * @throws InvalidDataTypeException |
|
152 | - * @throws InvalidInterfaceException |
|
153 | - */ |
|
154 | - private static function _event_category_slug($category = '') |
|
155 | - { |
|
156 | - return self::getRequest()->getRequestParam('event_query_category', $category); |
|
157 | - } |
|
158 | - |
|
159 | - |
|
160 | - /** |
|
161 | - * @param bool $show_expired |
|
162 | - * @return bool |
|
163 | - * @throws InvalidArgumentException |
|
164 | - * @throws InvalidDataTypeException |
|
165 | - * @throws InvalidInterfaceException |
|
166 | - */ |
|
167 | - private static function _show_expired($show_expired = false) |
|
168 | - { |
|
169 | - // override default expired option if set via filter |
|
170 | - return self::getRequest()->getRequestParam('event_query_show_expired', $show_expired, 'bool'); |
|
171 | - } |
|
172 | - |
|
173 | - |
|
174 | - /** |
|
175 | - * @param array|string $orderby |
|
176 | - * @return array |
|
177 | - * @throws InvalidArgumentException |
|
178 | - * @throws InvalidDataTypeException |
|
179 | - * @throws InvalidInterfaceException |
|
180 | - */ |
|
181 | - private static function _orderby($orderby = 'start_date') |
|
182 | - { |
|
183 | - $event_query_orderby = self::getRequest()->getRequestParam( |
|
184 | - 'event_query_orderby', |
|
185 | - (array) $orderby, |
|
186 | - DataType::STRING, |
|
187 | - true |
|
188 | - ); |
|
189 | - $event_query_orderby = is_array($event_query_orderby) |
|
190 | - ? $event_query_orderby |
|
191 | - : explode(',', $event_query_orderby); |
|
192 | - $event_query_orderby = array_map('trim', $event_query_orderby); |
|
193 | - return array_map('sanitize_text_field', $event_query_orderby); |
|
194 | - } |
|
195 | - |
|
196 | - |
|
197 | - /** |
|
198 | - * @param string $sort |
|
199 | - * @return string |
|
200 | - * @throws InvalidArgumentException |
|
201 | - * @throws InvalidDataTypeException |
|
202 | - * @throws InvalidInterfaceException |
|
203 | - */ |
|
204 | - private static function _sort($sort = 'ASC') |
|
205 | - { |
|
206 | - $sort = self::getRequest()->getRequestParam('event_query_sort', $sort); |
|
207 | - return in_array($sort, ['ASC', 'asc', 'DESC', 'desc'], true) |
|
208 | - ? strtoupper($sort) |
|
209 | - : 'ASC'; |
|
210 | - } |
|
211 | - |
|
212 | - |
|
213 | - /** |
|
214 | - * Filters the clauses for the WP_Query object |
|
215 | - * |
|
216 | - * @param array $clauses array of clauses |
|
217 | - * @param WP_Query $wp_query |
|
218 | - * @return array array of clauses |
|
219 | - */ |
|
220 | - public static function posts_clauses($clauses, WP_Query $wp_query) |
|
221 | - { |
|
222 | - if (EEH_Event_Query::apply_query_filters($wp_query)) { |
|
223 | - global $wpdb; |
|
224 | - $clauses['groupby'] = $wpdb->posts . '.ID '; |
|
225 | - } |
|
226 | - return $clauses; |
|
227 | - } |
|
228 | - |
|
229 | - |
|
230 | - /** |
|
231 | - * @param string $SQL |
|
232 | - * @param WP_Query $wp_query |
|
233 | - * @return string |
|
234 | - * @throws EE_Error |
|
235 | - * @throws InvalidArgumentException |
|
236 | - * @throws InvalidDataTypeException |
|
237 | - * @throws InvalidInterfaceException |
|
238 | - */ |
|
239 | - public static function posts_fields($SQL, WP_Query $wp_query) |
|
240 | - { |
|
241 | - if (EEH_Event_Query::apply_query_filters($wp_query)) { |
|
242 | - // adds something like ", wp_esp_datetime.* " to WP Query SELECT statement |
|
243 | - $SQL .= EEH_Event_Query::posts_fields_sql_for_orderby(EEH_Event_Query::$_event_query_orderby); |
|
244 | - } |
|
245 | - return $SQL; |
|
246 | - } |
|
247 | - |
|
248 | - |
|
249 | - /** |
|
250 | - * @param array $orderby_params |
|
251 | - * @return string |
|
252 | - * @throws EE_Error |
|
253 | - * @throws InvalidArgumentException |
|
254 | - * @throws InvalidDataTypeException |
|
255 | - * @throws InvalidInterfaceException |
|
256 | - */ |
|
257 | - public static function posts_fields_sql_for_orderby(array $orderby_params = []) |
|
258 | - { |
|
259 | - $SQL = ', MIN( ' . EEM_Datetime::instance()->table() . '.DTT_EVT_start ) as event_start_date '; |
|
260 | - foreach ($orderby_params as $orderby) { |
|
261 | - switch ($orderby) { |
|
262 | - case 'ticket_start': |
|
263 | - $SQL .= ', ' . EEM_Ticket::instance()->table() . '.TKT_start_date'; |
|
264 | - break; |
|
265 | - case 'ticket_end': |
|
266 | - $SQL .= ', ' . EEM_Ticket::instance()->table() . '.TKT_end_date'; |
|
267 | - break; |
|
268 | - case 'venue_title': |
|
269 | - $SQL .= ', Venue.post_title AS venue_title'; |
|
270 | - break; |
|
271 | - case 'city': |
|
272 | - $SQL .= ', ' . EEM_Venue::instance()->second_table() . '.VNU_city'; |
|
273 | - break; |
|
274 | - case 'state': |
|
275 | - $SQL .= ', ' . EEM_State::instance()->table() . '.STA_name'; |
|
276 | - break; |
|
277 | - } |
|
278 | - } |
|
279 | - return $SQL; |
|
280 | - } |
|
281 | - |
|
282 | - |
|
283 | - /** |
|
284 | - * @param string $SQL |
|
285 | - * @param WP_Query $wp_query |
|
286 | - * @return string |
|
287 | - * @throws EE_Error |
|
288 | - * @throws InvalidArgumentException |
|
289 | - * @throws InvalidDataTypeException |
|
290 | - * @throws InvalidInterfaceException |
|
291 | - */ |
|
292 | - public static function posts_join($SQL, WP_Query $wp_query) |
|
293 | - { |
|
294 | - if (EEH_Event_Query::apply_query_filters($wp_query)) { |
|
295 | - // Category |
|
296 | - $SQL = EEH_Event_Query::posts_join_sql_for_show_expired($SQL, EEH_Event_Query::$_event_query_show_expired); |
|
297 | - $SQL = EEH_Event_Query::posts_join_sql_for_terms($SQL, EEH_Event_Query::$_event_query_category); |
|
298 | - $SQL = EEH_Event_Query::posts_join_for_orderby($SQL, EEH_Event_Query::$_event_query_orderby); |
|
299 | - } |
|
300 | - return $SQL; |
|
301 | - } |
|
302 | - |
|
303 | - |
|
304 | - /** |
|
305 | - * @param string $SQL |
|
306 | - * @param boolean $show_expired if TRUE, then displayed past events |
|
307 | - * @return string |
|
308 | - * @throws EE_Error |
|
309 | - * @throws InvalidArgumentException |
|
310 | - * @throws InvalidDataTypeException |
|
311 | - * @throws InvalidInterfaceException |
|
312 | - */ |
|
313 | - public static function posts_join_sql_for_show_expired($SQL = '', $show_expired = false) |
|
314 | - { |
|
315 | - if (! $show_expired) { |
|
316 | - $join = EEM_Event::instance()->table() . '.ID = '; |
|
317 | - $join .= EEM_Datetime::instance()->table() . '.' . EEM_Event::instance()->primary_key_name(); |
|
318 | - // don't add if this is already in the SQL |
|
319 | - if (strpos($SQL, $join) === false) { |
|
320 | - $SQL .= ' INNER JOIN ' . EEM_Datetime::instance()->table() . ' ON ( ' . $join . ' ) '; |
|
321 | - } |
|
322 | - } |
|
323 | - return $SQL; |
|
324 | - } |
|
325 | - |
|
326 | - |
|
327 | - /** |
|
328 | - * @param string $SQL |
|
329 | - * @param string $join_terms pass TRUE or term string, doesn't really matter since this value doesn't really get |
|
330 | - * used for anything yet |
|
331 | - * @return string |
|
332 | - */ |
|
333 | - public static function posts_join_sql_for_terms($SQL = '', $join_terms = '') |
|
334 | - { |
|
335 | - if (! empty($join_terms)) { |
|
336 | - global $wpdb; |
|
337 | - $SQL .= " LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)"; |
|
338 | - $SQL .= " LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)"; |
|
339 | - $SQL .= " LEFT JOIN $wpdb->terms ON ($wpdb->terms.term_id = $wpdb->term_taxonomy.term_id) "; |
|
340 | - } |
|
341 | - return $SQL; |
|
342 | - } |
|
343 | - |
|
344 | - |
|
345 | - /** |
|
346 | - * usage: $SQL .= EEH_Event_Query::posts_join_for_orderby( $orderby_params ); |
|
347 | - * |
|
348 | - * @param string $SQL |
|
349 | - * @param array $orderby_params |
|
350 | - * @return string |
|
351 | - * @throws EE_Error |
|
352 | - * @throws InvalidArgumentException |
|
353 | - * @throws InvalidDataTypeException |
|
354 | - * @throws InvalidInterfaceException |
|
355 | - */ |
|
356 | - public static function posts_join_for_orderby($SQL = '', array $orderby_params = []) |
|
357 | - { |
|
358 | - foreach ($orderby_params as $orderby) { |
|
359 | - switch ($orderby) { |
|
360 | - case 'ticket_start': |
|
361 | - case 'ticket_end': |
|
362 | - $SQL .= EEH_Event_Query::_posts_join_for_datetime( |
|
363 | - $SQL, |
|
364 | - EEM_Datetime_Ticket::instance()->table() . '.' . EEM_Datetime::instance()->primary_key_name() |
|
365 | - ); |
|
366 | - $SQL .= ' LEFT JOIN ' . EEM_Ticket::instance()->table(); |
|
367 | - $SQL .= ' ON ('; |
|
368 | - $SQL .= EEM_Datetime_Ticket::instance()->table() . '.' . EEM_Ticket::instance()->primary_key_name(); |
|
369 | - $SQL .= ' = '; |
|
370 | - $SQL .= EEM_Ticket::instance()->table() . '.' . EEM_Ticket::instance()->primary_key_name(); |
|
371 | - $SQL .= ' )'; |
|
372 | - break; |
|
373 | - case 'venue_title': |
|
374 | - case 'city': |
|
375 | - $SQL .= EEH_Event_Query::_posts_join_for_event_venue($SQL); |
|
376 | - break; |
|
377 | - case 'state': |
|
378 | - $SQL .= EEH_Event_Query::_posts_join_for_event_venue($SQL); |
|
379 | - $SQL .= EEH_Event_Query::_posts_join_for_venue_state($SQL); |
|
380 | - break; |
|
381 | - case 'start_date': |
|
382 | - default: |
|
383 | - $SQL .= EEH_Event_Query::_posts_join_for_datetime($SQL, EEM_Event::instance()->table() . '.ID'); |
|
384 | - break; |
|
385 | - } |
|
386 | - } |
|
387 | - return $SQL; |
|
388 | - } |
|
389 | - |
|
390 | - |
|
391 | - /** |
|
392 | - * @param string $SQL |
|
393 | - * @param string $join |
|
394 | - * @return string |
|
395 | - * @throws EE_Error |
|
396 | - * @throws InvalidArgumentException |
|
397 | - * @throws InvalidDataTypeException |
|
398 | - * @throws InvalidInterfaceException |
|
399 | - */ |
|
400 | - protected static function _posts_join_for_datetime($SQL = '', $join = '') |
|
401 | - { |
|
402 | - if (! empty($join)) { |
|
403 | - $join .= ' = ' . EEM_Datetime::instance()->table() . '.' . EEM_Event::instance()->primary_key_name(); |
|
404 | - if (strpos($SQL, $join) === false) { |
|
405 | - return ' INNER JOIN ' . EEM_Datetime::instance()->table() . ' ON ( ' . $join . ' )'; |
|
406 | - } |
|
407 | - } |
|
408 | - return ''; |
|
409 | - } |
|
410 | - |
|
411 | - |
|
412 | - /** |
|
413 | - * @param string $SQL |
|
414 | - * @return string |
|
415 | - * @throws EE_Error |
|
416 | - * @throws InvalidArgumentException |
|
417 | - * @throws InvalidDataTypeException |
|
418 | - * @throws InvalidInterfaceException |
|
419 | - */ |
|
420 | - protected static function _posts_join_for_event_venue($SQL = '') |
|
421 | - { |
|
422 | - // Event Venue table name |
|
423 | - $event_venue_table = EEM_Event_Venue::instance()->table(); |
|
424 | - // generate conditions for: Event <=> Event Venue JOIN clause |
|
425 | - $event_to_event_venue_join = EEM_Event::instance()->table() . '.ID = '; |
|
426 | - $event_to_event_venue_join .= $event_venue_table . '.' . EEM_Event::instance()->primary_key_name(); |
|
427 | - // don't add joins if they have already been added |
|
428 | - if (strpos($SQL, $event_to_event_venue_join) === false) { |
|
429 | - // Venue table name |
|
430 | - $venue_table = EEM_Venue::instance()->table(); |
|
431 | - // Venue table pk |
|
432 | - $venue_table_pk = EEM_Venue::instance()->primary_key_name(); |
|
433 | - // Venue Meta table name |
|
434 | - $venue_meta_table = EEM_Venue::instance()->second_table(); |
|
435 | - // generate JOIN clause for: Event <=> Event Venue |
|
436 | - $venue_SQL = " LEFT JOIN $event_venue_table ON ( $event_to_event_venue_join )"; |
|
437 | - // generate JOIN clause for: Event Venue <=> Venue |
|
438 | - $venue_SQL .= " LEFT JOIN $venue_table as Venue ON ( $event_venue_table.$venue_table_pk = Venue.ID )"; |
|
439 | - // generate JOIN clause for: Venue <=> Venue Meta |
|
440 | - $venue_SQL .= " LEFT JOIN $venue_meta_table ON ( Venue.ID = $venue_meta_table.$venue_table_pk )"; |
|
441 | - unset($event_venue_table, $event_to_event_venue_join, $venue_table, $venue_table_pk, $venue_meta_table); |
|
442 | - return $venue_SQL; |
|
443 | - } |
|
444 | - unset($event_venue_table, $event_to_event_venue_join); |
|
445 | - return ''; |
|
446 | - } |
|
447 | - |
|
448 | - |
|
449 | - /** |
|
450 | - * @param string $SQL |
|
451 | - * @return string |
|
452 | - * @throws EE_Error |
|
453 | - * @throws InvalidArgumentException |
|
454 | - * @throws InvalidDataTypeException |
|
455 | - * @throws InvalidInterfaceException |
|
456 | - */ |
|
457 | - protected static function _posts_join_for_venue_state($SQL = '') |
|
458 | - { |
|
459 | - // Venue Meta table name |
|
460 | - $venue_meta_table = EEM_Venue::instance()->second_table(); |
|
461 | - // State table name |
|
462 | - $state_table = EEM_State::instance()->table(); |
|
463 | - // State table pk |
|
464 | - $state_table_pk = EEM_State::instance()->primary_key_name(); |
|
465 | - // verify vars |
|
466 | - if ($venue_meta_table && $state_table && $state_table_pk) { |
|
467 | - // like: wp_esp_venue_meta.STA_ID = wp_esp_state.STA_ID |
|
468 | - $join = "$venue_meta_table.$state_table_pk = $state_table.$state_table_pk"; |
|
469 | - // don't add join if it has already been added |
|
470 | - if (strpos($SQL, $join) === false) { |
|
471 | - unset($state_table_pk, $venue_meta_table, $venue_table_pk); |
|
472 | - return " LEFT JOIN $state_table ON ( $join )"; |
|
473 | - } |
|
474 | - } |
|
475 | - unset($join, $state_table, $state_table_pk, $venue_meta_table, $venue_table_pk); |
|
476 | - return ''; |
|
477 | - } |
|
478 | - |
|
479 | - |
|
480 | - /** |
|
481 | - * @param string $SQL |
|
482 | - * @param WP_Query $wp_query |
|
483 | - * @return string |
|
484 | - * @throws EE_Error |
|
485 | - * @throws InvalidArgumentException |
|
486 | - * @throws InvalidDataTypeException |
|
487 | - * @throws InvalidInterfaceException |
|
488 | - */ |
|
489 | - public static function posts_where($SQL, WP_Query $wp_query) |
|
490 | - { |
|
491 | - if (EEH_Event_Query::apply_query_filters($wp_query)) { |
|
492 | - // Show Expired ? |
|
493 | - $SQL .= EEH_Event_Query::posts_where_sql_for_show_expired(EEH_Event_Query::$_event_query_show_expired); |
|
494 | - // Category |
|
495 | - $SQL .= EEH_Event_Query::posts_where_sql_for_event_category_slug(EEH_Event_Query::$_event_query_category); |
|
496 | - // Start Date |
|
497 | - $SQL .= EEH_Event_Query::posts_where_sql_for_event_list_month(EEH_Event_Query::$_event_query_month); |
|
498 | - } |
|
499 | - return $SQL; |
|
500 | - } |
|
501 | - |
|
502 | - |
|
503 | - /** |
|
504 | - * @param boolean $show_expired if TRUE, then displayed past events |
|
505 | - * @return string |
|
506 | - * @throws EE_Error |
|
507 | - * @throws InvalidArgumentException |
|
508 | - * @throws InvalidDataTypeException |
|
509 | - * @throws InvalidInterfaceException |
|
510 | - */ |
|
511 | - public static function posts_where_sql_for_show_expired($show_expired = false) |
|
512 | - { |
|
513 | - return ! $show_expired |
|
514 | - ? ' AND ' . EEM_Datetime::instance()->table() . '.DTT_EVT_end > \'' . current_time('mysql', true) . '\' ' |
|
515 | - : ''; |
|
516 | - } |
|
517 | - |
|
518 | - |
|
519 | - /** |
|
520 | - * @param boolean $event_category_slug |
|
521 | - * @return string |
|
522 | - */ |
|
523 | - public static function posts_where_sql_for_event_category_slug($event_category_slug = null) |
|
524 | - { |
|
525 | - global $wpdb; |
|
526 | - if (! empty($event_category_slug)) { |
|
527 | - $event_category_slugs_array = array_map('trim', explode(',', $event_category_slug)); |
|
528 | - $event_category_slugs_prepare = implode(', ', array_fill(0, count($event_category_slugs_array), '%s')); |
|
529 | - return $wpdb->prepare( |
|
530 | - " AND {$wpdb->terms}.slug IN ({$event_category_slugs_prepare}) ", |
|
531 | - $event_category_slugs_array |
|
532 | - ); |
|
533 | - } |
|
534 | - return ''; |
|
535 | - } |
|
536 | - |
|
537 | - |
|
538 | - /** |
|
539 | - * @param boolean $month |
|
540 | - * @return string |
|
541 | - * @throws EE_Error |
|
542 | - * @throws InvalidArgumentException |
|
543 | - * @throws InvalidDataTypeException |
|
544 | - * @throws InvalidInterfaceException |
|
545 | - */ |
|
546 | - public static function posts_where_sql_for_event_list_month($month = null) |
|
547 | - { |
|
548 | - $SQL = ''; |
|
549 | - if (! empty($month)) { |
|
550 | - $datetime_table = EEM_Datetime::instance()->table(); |
|
551 | - // event start date is LESS than the end of the month ( so nothing that doesn't start until next month ) |
|
552 | - $SQL = " AND {$datetime_table}.DTT_EVT_start <= '"; |
|
553 | - $SQL .= date('Y-m-t 23:59:59', EEH_DTT_Helper::first_of_month_timestamp($month)) . "'"; |
|
554 | - // event end date is GREATER than the start of the month ( so nothing that ended before this month ) |
|
555 | - $SQL .= " AND {$datetime_table}.DTT_EVT_end >= '"; |
|
556 | - $SQL .= date('Y-m-01 0:0:00', EEH_DTT_Helper::first_of_month_timestamp($month)) . "' "; |
|
557 | - } |
|
558 | - return $SQL; |
|
559 | - } |
|
560 | - |
|
561 | - |
|
562 | - /** |
|
563 | - * @param string $SQL |
|
564 | - * @param WP_Query $wp_query |
|
565 | - * @return string |
|
566 | - * @throws EE_Error |
|
567 | - * @throws InvalidArgumentException |
|
568 | - * @throws InvalidDataTypeException |
|
569 | - * @throws InvalidInterfaceException |
|
570 | - */ |
|
571 | - public static function posts_orderby($SQL, WP_Query $wp_query) |
|
572 | - { |
|
573 | - if (EEH_Event_Query::apply_query_filters($wp_query)) { |
|
574 | - $SQL = EEH_Event_Query::posts_orderby_sql( |
|
575 | - EEH_Event_Query::$_event_query_orderby, |
|
576 | - EEH_Event_Query::$_event_query_sort |
|
577 | - ); |
|
578 | - } |
|
579 | - return $SQL; |
|
580 | - } |
|
581 | - |
|
582 | - |
|
583 | - /** |
|
584 | - * posts_orderby_sql |
|
585 | - * possible parameters: |
|
586 | - * ID |
|
587 | - * start_date |
|
588 | - * end_date |
|
589 | - * event_name |
|
590 | - * category_slug |
|
591 | - * ticket_start |
|
592 | - * ticket_end |
|
593 | - * venue_title |
|
594 | - * city |
|
595 | - * state |
|
596 | - * **IMPORTANT** |
|
597 | - * make sure to also send the $orderby_params array to the posts_join_for_orderby() method |
|
598 | - * or else some of the table references below will result in MySQL errors |
|
599 | - * |
|
600 | - * @param array $orderby_params |
|
601 | - * @param string $sort |
|
602 | - * @return string |
|
603 | - * @throws EE_Error |
|
604 | - * @throws InvalidArgumentException |
|
605 | - * @throws InvalidDataTypeException |
|
606 | - * @throws InvalidInterfaceException |
|
607 | - */ |
|
608 | - public static function posts_orderby_sql(array $orderby_params = [], $sort = 'ASC') |
|
609 | - { |
|
610 | - global $wpdb; |
|
611 | - $SQL = ''; |
|
612 | - $counter = 0; |
|
613 | - $sort = in_array($sort, ['ASC', 'asc', 'DESC', 'desc'], true) |
|
614 | - ? strtoupper($sort) |
|
615 | - : 'ASC'; |
|
616 | - // make sure 'orderby' is set in query params |
|
617 | - if (! isset(self::$_query_params['orderby'])) { |
|
618 | - self::$_query_params['orderby'] = []; |
|
619 | - } |
|
620 | - // loop thru $orderby_params (type cast as array) |
|
621 | - foreach ($orderby_params as $orderby) { |
|
622 | - // check if we have already added this param |
|
623 | - if (isset(self::$_query_params['orderby'][ $orderby ])) { |
|
624 | - // if so then remove from the $orderby_params so that the count() method below is accurate |
|
625 | - unset($orderby_params[ $orderby ]); |
|
626 | - // then bump ahead to the next param |
|
627 | - continue; |
|
628 | - } |
|
629 | - // this will ad a comma depending on whether this is the first or last param |
|
630 | - $glue = $counter === 0 || $counter === count($orderby_params) ? ' ' : ', '; |
|
631 | - // ok what's we dealing with? |
|
632 | - switch ($orderby) { |
|
633 | - case 'id': |
|
634 | - case 'ID': |
|
635 | - $SQL .= $glue . $wpdb->posts . '.ID ' . $sort; |
|
636 | - break; |
|
637 | - case 'end_date': |
|
638 | - $SQL .= $glue . EEM_Datetime::instance()->table() . '.DTT_EVT_end ' . $sort; |
|
639 | - break; |
|
640 | - case 'event_name': |
|
641 | - $SQL .= $glue . $wpdb->posts . '.post_title ' . $sort; |
|
642 | - break; |
|
643 | - case 'category_slug': |
|
644 | - $SQL .= $glue . $wpdb->terms . '.slug ' . $sort; |
|
645 | - break; |
|
646 | - case 'ticket_start': |
|
647 | - $SQL .= $glue . EEM_Ticket::instance()->table() . '.TKT_start_date ' . $sort; |
|
648 | - break; |
|
649 | - case 'ticket_end': |
|
650 | - $SQL .= $glue . EEM_Ticket::instance()->table() . '.TKT_end_date ' . $sort; |
|
651 | - break; |
|
652 | - case 'venue_title': |
|
653 | - $SQL .= $glue . 'venue_title ' . $sort; |
|
654 | - break; |
|
655 | - case 'city': |
|
656 | - $SQL .= $glue . EEM_Venue::instance()->second_table() . '.VNU_city ' . $sort; |
|
657 | - break; |
|
658 | - case 'state': |
|
659 | - $SQL .= $glue . EEM_State::instance()->table() . '.STA_name ' . $sort; |
|
660 | - break; |
|
661 | - case 'start_date': |
|
662 | - default: |
|
663 | - $SQL .= $glue . ' event_start_date ' . $sort; |
|
664 | - break; |
|
665 | - } |
|
666 | - // add to array of orderby params that have been added |
|
667 | - self::$_query_params['orderby'][ $orderby ] = true; |
|
668 | - $counter++; |
|
669 | - } |
|
670 | - return $SQL; |
|
671 | - } |
|
672 | - |
|
673 | - |
|
674 | - /** |
|
675 | - * @return RequestInterface |
|
676 | - * @since 4.10.14.p |
|
677 | - */ |
|
678 | - private static function getRequest() |
|
679 | - { |
|
680 | - return LoaderFactory::getLoader()->getShared(RequestInterface::class); |
|
681 | - } |
|
20 | + /** |
|
21 | + * Start Date |
|
22 | + * |
|
23 | + * @var $_event_query_month |
|
24 | + */ |
|
25 | + protected static $_event_query_month; |
|
26 | + |
|
27 | + /** |
|
28 | + * Category |
|
29 | + * |
|
30 | + * @var $_event_query_category |
|
31 | + */ |
|
32 | + protected static $_event_query_category; |
|
33 | + |
|
34 | + /** |
|
35 | + * whether to display expired events in the event list |
|
36 | + * |
|
37 | + * @var bool $_show_expired |
|
38 | + */ |
|
39 | + protected static $_event_query_show_expired = false; |
|
40 | + |
|
41 | + /** |
|
42 | + * list of params for controlling how the query results are ordered |
|
43 | + * |
|
44 | + * @var array $_event_query_orderby |
|
45 | + */ |
|
46 | + protected static $_event_query_orderby = []; |
|
47 | + |
|
48 | + /** |
|
49 | + * direction list is sorted |
|
50 | + * |
|
51 | + * @var string $_event_query_sort |
|
52 | + */ |
|
53 | + protected static $_event_query_sort; |
|
54 | + |
|
55 | + /** |
|
56 | + * list of params used to build the query's various clauses |
|
57 | + * |
|
58 | + * @var $_query_params |
|
59 | + */ |
|
60 | + protected static $_query_params = []; |
|
61 | + |
|
62 | + |
|
63 | + /** |
|
64 | + * @return void |
|
65 | + */ |
|
66 | + public static function add_query_filters() |
|
67 | + { |
|
68 | + // add query filters |
|
69 | + add_action('pre_get_posts', ['EEH_Event_Query', 'filter_query_parts'], 10, 1); |
|
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', ['EEH_Event_Query', 'posts_fields'], 10, 2); |
|
98 | + add_filter('posts_join', ['EEH_Event_Query', 'posts_join'], 10, 2); |
|
99 | + add_filter('posts_where', ['EEH_Event_Query', 'posts_where'], 10, 2); |
|
100 | + add_filter('posts_orderby', ['EEH_Event_Query', 'posts_orderby'], 10, 2); |
|
101 | + add_filter('posts_clauses_request', ['EEH_Event_Query', 'posts_clauses'], 10, 2); |
|
102 | + } |
|
103 | + } |
|
104 | + |
|
105 | + |
|
106 | + /** |
|
107 | + * @param string $month |
|
108 | + * @param string $category |
|
109 | + * @param bool $show_expired |
|
110 | + * @param array|string $orderby |
|
111 | + * @param string $sort |
|
112 | + * @throws InvalidArgumentException |
|
113 | + * @throws InvalidDataTypeException |
|
114 | + * @throws InvalidInterfaceException |
|
115 | + */ |
|
116 | + public static function set_query_params( |
|
117 | + $month = '', |
|
118 | + $category = '', |
|
119 | + $show_expired = false, |
|
120 | + $orderby = 'start_date', |
|
121 | + $sort = 'ASC' |
|
122 | + ) { |
|
123 | + self::$_query_params = []; |
|
124 | + EEH_Event_Query::$_event_query_month = EEH_Event_Query::_display_month($month); |
|
125 | + EEH_Event_Query::$_event_query_category = EEH_Event_Query::_event_category_slug($category); |
|
126 | + EEH_Event_Query::$_event_query_show_expired = EEH_Event_Query::_show_expired($show_expired); |
|
127 | + EEH_Event_Query::$_event_query_orderby = EEH_Event_Query::_orderby($orderby); |
|
128 | + EEH_Event_Query::$_event_query_sort = EEH_Event_Query::_sort($sort); |
|
129 | + } |
|
130 | + |
|
131 | + |
|
132 | + /** |
|
133 | + * what month should the event list display events for? |
|
134 | + * |
|
135 | + * @param string $month |
|
136 | + * @return string |
|
137 | + * @throws InvalidArgumentException |
|
138 | + * @throws InvalidDataTypeException |
|
139 | + * @throws InvalidInterfaceException |
|
140 | + */ |
|
141 | + private static function _display_month($month = '') |
|
142 | + { |
|
143 | + return self::getRequest()->getRequestParam('event_query_month', $month); |
|
144 | + } |
|
145 | + |
|
146 | + |
|
147 | + /** |
|
148 | + * @param string $category |
|
149 | + * @return string |
|
150 | + * @throws InvalidArgumentException |
|
151 | + * @throws InvalidDataTypeException |
|
152 | + * @throws InvalidInterfaceException |
|
153 | + */ |
|
154 | + private static function _event_category_slug($category = '') |
|
155 | + { |
|
156 | + return self::getRequest()->getRequestParam('event_query_category', $category); |
|
157 | + } |
|
158 | + |
|
159 | + |
|
160 | + /** |
|
161 | + * @param bool $show_expired |
|
162 | + * @return bool |
|
163 | + * @throws InvalidArgumentException |
|
164 | + * @throws InvalidDataTypeException |
|
165 | + * @throws InvalidInterfaceException |
|
166 | + */ |
|
167 | + private static function _show_expired($show_expired = false) |
|
168 | + { |
|
169 | + // override default expired option if set via filter |
|
170 | + return self::getRequest()->getRequestParam('event_query_show_expired', $show_expired, 'bool'); |
|
171 | + } |
|
172 | + |
|
173 | + |
|
174 | + /** |
|
175 | + * @param array|string $orderby |
|
176 | + * @return array |
|
177 | + * @throws InvalidArgumentException |
|
178 | + * @throws InvalidDataTypeException |
|
179 | + * @throws InvalidInterfaceException |
|
180 | + */ |
|
181 | + private static function _orderby($orderby = 'start_date') |
|
182 | + { |
|
183 | + $event_query_orderby = self::getRequest()->getRequestParam( |
|
184 | + 'event_query_orderby', |
|
185 | + (array) $orderby, |
|
186 | + DataType::STRING, |
|
187 | + true |
|
188 | + ); |
|
189 | + $event_query_orderby = is_array($event_query_orderby) |
|
190 | + ? $event_query_orderby |
|
191 | + : explode(',', $event_query_orderby); |
|
192 | + $event_query_orderby = array_map('trim', $event_query_orderby); |
|
193 | + return array_map('sanitize_text_field', $event_query_orderby); |
|
194 | + } |
|
195 | + |
|
196 | + |
|
197 | + /** |
|
198 | + * @param string $sort |
|
199 | + * @return string |
|
200 | + * @throws InvalidArgumentException |
|
201 | + * @throws InvalidDataTypeException |
|
202 | + * @throws InvalidInterfaceException |
|
203 | + */ |
|
204 | + private static function _sort($sort = 'ASC') |
|
205 | + { |
|
206 | + $sort = self::getRequest()->getRequestParam('event_query_sort', $sort); |
|
207 | + return in_array($sort, ['ASC', 'asc', 'DESC', 'desc'], true) |
|
208 | + ? strtoupper($sort) |
|
209 | + : 'ASC'; |
|
210 | + } |
|
211 | + |
|
212 | + |
|
213 | + /** |
|
214 | + * Filters the clauses for the WP_Query object |
|
215 | + * |
|
216 | + * @param array $clauses array of clauses |
|
217 | + * @param WP_Query $wp_query |
|
218 | + * @return array array of clauses |
|
219 | + */ |
|
220 | + public static function posts_clauses($clauses, WP_Query $wp_query) |
|
221 | + { |
|
222 | + if (EEH_Event_Query::apply_query_filters($wp_query)) { |
|
223 | + global $wpdb; |
|
224 | + $clauses['groupby'] = $wpdb->posts . '.ID '; |
|
225 | + } |
|
226 | + return $clauses; |
|
227 | + } |
|
228 | + |
|
229 | + |
|
230 | + /** |
|
231 | + * @param string $SQL |
|
232 | + * @param WP_Query $wp_query |
|
233 | + * @return string |
|
234 | + * @throws EE_Error |
|
235 | + * @throws InvalidArgumentException |
|
236 | + * @throws InvalidDataTypeException |
|
237 | + * @throws InvalidInterfaceException |
|
238 | + */ |
|
239 | + public static function posts_fields($SQL, WP_Query $wp_query) |
|
240 | + { |
|
241 | + if (EEH_Event_Query::apply_query_filters($wp_query)) { |
|
242 | + // adds something like ", wp_esp_datetime.* " to WP Query SELECT statement |
|
243 | + $SQL .= EEH_Event_Query::posts_fields_sql_for_orderby(EEH_Event_Query::$_event_query_orderby); |
|
244 | + } |
|
245 | + return $SQL; |
|
246 | + } |
|
247 | + |
|
248 | + |
|
249 | + /** |
|
250 | + * @param array $orderby_params |
|
251 | + * @return string |
|
252 | + * @throws EE_Error |
|
253 | + * @throws InvalidArgumentException |
|
254 | + * @throws InvalidDataTypeException |
|
255 | + * @throws InvalidInterfaceException |
|
256 | + */ |
|
257 | + public static function posts_fields_sql_for_orderby(array $orderby_params = []) |
|
258 | + { |
|
259 | + $SQL = ', MIN( ' . EEM_Datetime::instance()->table() . '.DTT_EVT_start ) as event_start_date '; |
|
260 | + foreach ($orderby_params as $orderby) { |
|
261 | + switch ($orderby) { |
|
262 | + case 'ticket_start': |
|
263 | + $SQL .= ', ' . EEM_Ticket::instance()->table() . '.TKT_start_date'; |
|
264 | + break; |
|
265 | + case 'ticket_end': |
|
266 | + $SQL .= ', ' . EEM_Ticket::instance()->table() . '.TKT_end_date'; |
|
267 | + break; |
|
268 | + case 'venue_title': |
|
269 | + $SQL .= ', Venue.post_title AS venue_title'; |
|
270 | + break; |
|
271 | + case 'city': |
|
272 | + $SQL .= ', ' . EEM_Venue::instance()->second_table() . '.VNU_city'; |
|
273 | + break; |
|
274 | + case 'state': |
|
275 | + $SQL .= ', ' . EEM_State::instance()->table() . '.STA_name'; |
|
276 | + break; |
|
277 | + } |
|
278 | + } |
|
279 | + return $SQL; |
|
280 | + } |
|
281 | + |
|
282 | + |
|
283 | + /** |
|
284 | + * @param string $SQL |
|
285 | + * @param WP_Query $wp_query |
|
286 | + * @return string |
|
287 | + * @throws EE_Error |
|
288 | + * @throws InvalidArgumentException |
|
289 | + * @throws InvalidDataTypeException |
|
290 | + * @throws InvalidInterfaceException |
|
291 | + */ |
|
292 | + public static function posts_join($SQL, WP_Query $wp_query) |
|
293 | + { |
|
294 | + if (EEH_Event_Query::apply_query_filters($wp_query)) { |
|
295 | + // Category |
|
296 | + $SQL = EEH_Event_Query::posts_join_sql_for_show_expired($SQL, EEH_Event_Query::$_event_query_show_expired); |
|
297 | + $SQL = EEH_Event_Query::posts_join_sql_for_terms($SQL, EEH_Event_Query::$_event_query_category); |
|
298 | + $SQL = EEH_Event_Query::posts_join_for_orderby($SQL, EEH_Event_Query::$_event_query_orderby); |
|
299 | + } |
|
300 | + return $SQL; |
|
301 | + } |
|
302 | + |
|
303 | + |
|
304 | + /** |
|
305 | + * @param string $SQL |
|
306 | + * @param boolean $show_expired if TRUE, then displayed past events |
|
307 | + * @return string |
|
308 | + * @throws EE_Error |
|
309 | + * @throws InvalidArgumentException |
|
310 | + * @throws InvalidDataTypeException |
|
311 | + * @throws InvalidInterfaceException |
|
312 | + */ |
|
313 | + public static function posts_join_sql_for_show_expired($SQL = '', $show_expired = false) |
|
314 | + { |
|
315 | + if (! $show_expired) { |
|
316 | + $join = EEM_Event::instance()->table() . '.ID = '; |
|
317 | + $join .= EEM_Datetime::instance()->table() . '.' . EEM_Event::instance()->primary_key_name(); |
|
318 | + // don't add if this is already in the SQL |
|
319 | + if (strpos($SQL, $join) === false) { |
|
320 | + $SQL .= ' INNER JOIN ' . EEM_Datetime::instance()->table() . ' ON ( ' . $join . ' ) '; |
|
321 | + } |
|
322 | + } |
|
323 | + return $SQL; |
|
324 | + } |
|
325 | + |
|
326 | + |
|
327 | + /** |
|
328 | + * @param string $SQL |
|
329 | + * @param string $join_terms pass TRUE or term string, doesn't really matter since this value doesn't really get |
|
330 | + * used for anything yet |
|
331 | + * @return string |
|
332 | + */ |
|
333 | + public static function posts_join_sql_for_terms($SQL = '', $join_terms = '') |
|
334 | + { |
|
335 | + if (! empty($join_terms)) { |
|
336 | + global $wpdb; |
|
337 | + $SQL .= " LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)"; |
|
338 | + $SQL .= " LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)"; |
|
339 | + $SQL .= " LEFT JOIN $wpdb->terms ON ($wpdb->terms.term_id = $wpdb->term_taxonomy.term_id) "; |
|
340 | + } |
|
341 | + return $SQL; |
|
342 | + } |
|
343 | + |
|
344 | + |
|
345 | + /** |
|
346 | + * usage: $SQL .= EEH_Event_Query::posts_join_for_orderby( $orderby_params ); |
|
347 | + * |
|
348 | + * @param string $SQL |
|
349 | + * @param array $orderby_params |
|
350 | + * @return string |
|
351 | + * @throws EE_Error |
|
352 | + * @throws InvalidArgumentException |
|
353 | + * @throws InvalidDataTypeException |
|
354 | + * @throws InvalidInterfaceException |
|
355 | + */ |
|
356 | + public static function posts_join_for_orderby($SQL = '', array $orderby_params = []) |
|
357 | + { |
|
358 | + foreach ($orderby_params as $orderby) { |
|
359 | + switch ($orderby) { |
|
360 | + case 'ticket_start': |
|
361 | + case 'ticket_end': |
|
362 | + $SQL .= EEH_Event_Query::_posts_join_for_datetime( |
|
363 | + $SQL, |
|
364 | + EEM_Datetime_Ticket::instance()->table() . '.' . EEM_Datetime::instance()->primary_key_name() |
|
365 | + ); |
|
366 | + $SQL .= ' LEFT JOIN ' . EEM_Ticket::instance()->table(); |
|
367 | + $SQL .= ' ON ('; |
|
368 | + $SQL .= EEM_Datetime_Ticket::instance()->table() . '.' . EEM_Ticket::instance()->primary_key_name(); |
|
369 | + $SQL .= ' = '; |
|
370 | + $SQL .= EEM_Ticket::instance()->table() . '.' . EEM_Ticket::instance()->primary_key_name(); |
|
371 | + $SQL .= ' )'; |
|
372 | + break; |
|
373 | + case 'venue_title': |
|
374 | + case 'city': |
|
375 | + $SQL .= EEH_Event_Query::_posts_join_for_event_venue($SQL); |
|
376 | + break; |
|
377 | + case 'state': |
|
378 | + $SQL .= EEH_Event_Query::_posts_join_for_event_venue($SQL); |
|
379 | + $SQL .= EEH_Event_Query::_posts_join_for_venue_state($SQL); |
|
380 | + break; |
|
381 | + case 'start_date': |
|
382 | + default: |
|
383 | + $SQL .= EEH_Event_Query::_posts_join_for_datetime($SQL, EEM_Event::instance()->table() . '.ID'); |
|
384 | + break; |
|
385 | + } |
|
386 | + } |
|
387 | + return $SQL; |
|
388 | + } |
|
389 | + |
|
390 | + |
|
391 | + /** |
|
392 | + * @param string $SQL |
|
393 | + * @param string $join |
|
394 | + * @return string |
|
395 | + * @throws EE_Error |
|
396 | + * @throws InvalidArgumentException |
|
397 | + * @throws InvalidDataTypeException |
|
398 | + * @throws InvalidInterfaceException |
|
399 | + */ |
|
400 | + protected static function _posts_join_for_datetime($SQL = '', $join = '') |
|
401 | + { |
|
402 | + if (! empty($join)) { |
|
403 | + $join .= ' = ' . EEM_Datetime::instance()->table() . '.' . EEM_Event::instance()->primary_key_name(); |
|
404 | + if (strpos($SQL, $join) === false) { |
|
405 | + return ' INNER JOIN ' . EEM_Datetime::instance()->table() . ' ON ( ' . $join . ' )'; |
|
406 | + } |
|
407 | + } |
|
408 | + return ''; |
|
409 | + } |
|
410 | + |
|
411 | + |
|
412 | + /** |
|
413 | + * @param string $SQL |
|
414 | + * @return string |
|
415 | + * @throws EE_Error |
|
416 | + * @throws InvalidArgumentException |
|
417 | + * @throws InvalidDataTypeException |
|
418 | + * @throws InvalidInterfaceException |
|
419 | + */ |
|
420 | + protected static function _posts_join_for_event_venue($SQL = '') |
|
421 | + { |
|
422 | + // Event Venue table name |
|
423 | + $event_venue_table = EEM_Event_Venue::instance()->table(); |
|
424 | + // generate conditions for: Event <=> Event Venue JOIN clause |
|
425 | + $event_to_event_venue_join = EEM_Event::instance()->table() . '.ID = '; |
|
426 | + $event_to_event_venue_join .= $event_venue_table . '.' . EEM_Event::instance()->primary_key_name(); |
|
427 | + // don't add joins if they have already been added |
|
428 | + if (strpos($SQL, $event_to_event_venue_join) === false) { |
|
429 | + // Venue table name |
|
430 | + $venue_table = EEM_Venue::instance()->table(); |
|
431 | + // Venue table pk |
|
432 | + $venue_table_pk = EEM_Venue::instance()->primary_key_name(); |
|
433 | + // Venue Meta table name |
|
434 | + $venue_meta_table = EEM_Venue::instance()->second_table(); |
|
435 | + // generate JOIN clause for: Event <=> Event Venue |
|
436 | + $venue_SQL = " LEFT JOIN $event_venue_table ON ( $event_to_event_venue_join )"; |
|
437 | + // generate JOIN clause for: Event Venue <=> Venue |
|
438 | + $venue_SQL .= " LEFT JOIN $venue_table as Venue ON ( $event_venue_table.$venue_table_pk = Venue.ID )"; |
|
439 | + // generate JOIN clause for: Venue <=> Venue Meta |
|
440 | + $venue_SQL .= " LEFT JOIN $venue_meta_table ON ( Venue.ID = $venue_meta_table.$venue_table_pk )"; |
|
441 | + unset($event_venue_table, $event_to_event_venue_join, $venue_table, $venue_table_pk, $venue_meta_table); |
|
442 | + return $venue_SQL; |
|
443 | + } |
|
444 | + unset($event_venue_table, $event_to_event_venue_join); |
|
445 | + return ''; |
|
446 | + } |
|
447 | + |
|
448 | + |
|
449 | + /** |
|
450 | + * @param string $SQL |
|
451 | + * @return string |
|
452 | + * @throws EE_Error |
|
453 | + * @throws InvalidArgumentException |
|
454 | + * @throws InvalidDataTypeException |
|
455 | + * @throws InvalidInterfaceException |
|
456 | + */ |
|
457 | + protected static function _posts_join_for_venue_state($SQL = '') |
|
458 | + { |
|
459 | + // Venue Meta table name |
|
460 | + $venue_meta_table = EEM_Venue::instance()->second_table(); |
|
461 | + // State table name |
|
462 | + $state_table = EEM_State::instance()->table(); |
|
463 | + // State table pk |
|
464 | + $state_table_pk = EEM_State::instance()->primary_key_name(); |
|
465 | + // verify vars |
|
466 | + if ($venue_meta_table && $state_table && $state_table_pk) { |
|
467 | + // like: wp_esp_venue_meta.STA_ID = wp_esp_state.STA_ID |
|
468 | + $join = "$venue_meta_table.$state_table_pk = $state_table.$state_table_pk"; |
|
469 | + // don't add join if it has already been added |
|
470 | + if (strpos($SQL, $join) === false) { |
|
471 | + unset($state_table_pk, $venue_meta_table, $venue_table_pk); |
|
472 | + return " LEFT JOIN $state_table ON ( $join )"; |
|
473 | + } |
|
474 | + } |
|
475 | + unset($join, $state_table, $state_table_pk, $venue_meta_table, $venue_table_pk); |
|
476 | + return ''; |
|
477 | + } |
|
478 | + |
|
479 | + |
|
480 | + /** |
|
481 | + * @param string $SQL |
|
482 | + * @param WP_Query $wp_query |
|
483 | + * @return string |
|
484 | + * @throws EE_Error |
|
485 | + * @throws InvalidArgumentException |
|
486 | + * @throws InvalidDataTypeException |
|
487 | + * @throws InvalidInterfaceException |
|
488 | + */ |
|
489 | + public static function posts_where($SQL, WP_Query $wp_query) |
|
490 | + { |
|
491 | + if (EEH_Event_Query::apply_query_filters($wp_query)) { |
|
492 | + // Show Expired ? |
|
493 | + $SQL .= EEH_Event_Query::posts_where_sql_for_show_expired(EEH_Event_Query::$_event_query_show_expired); |
|
494 | + // Category |
|
495 | + $SQL .= EEH_Event_Query::posts_where_sql_for_event_category_slug(EEH_Event_Query::$_event_query_category); |
|
496 | + // Start Date |
|
497 | + $SQL .= EEH_Event_Query::posts_where_sql_for_event_list_month(EEH_Event_Query::$_event_query_month); |
|
498 | + } |
|
499 | + return $SQL; |
|
500 | + } |
|
501 | + |
|
502 | + |
|
503 | + /** |
|
504 | + * @param boolean $show_expired if TRUE, then displayed past events |
|
505 | + * @return string |
|
506 | + * @throws EE_Error |
|
507 | + * @throws InvalidArgumentException |
|
508 | + * @throws InvalidDataTypeException |
|
509 | + * @throws InvalidInterfaceException |
|
510 | + */ |
|
511 | + public static function posts_where_sql_for_show_expired($show_expired = false) |
|
512 | + { |
|
513 | + return ! $show_expired |
|
514 | + ? ' AND ' . EEM_Datetime::instance()->table() . '.DTT_EVT_end > \'' . current_time('mysql', true) . '\' ' |
|
515 | + : ''; |
|
516 | + } |
|
517 | + |
|
518 | + |
|
519 | + /** |
|
520 | + * @param boolean $event_category_slug |
|
521 | + * @return string |
|
522 | + */ |
|
523 | + public static function posts_where_sql_for_event_category_slug($event_category_slug = null) |
|
524 | + { |
|
525 | + global $wpdb; |
|
526 | + if (! empty($event_category_slug)) { |
|
527 | + $event_category_slugs_array = array_map('trim', explode(',', $event_category_slug)); |
|
528 | + $event_category_slugs_prepare = implode(', ', array_fill(0, count($event_category_slugs_array), '%s')); |
|
529 | + return $wpdb->prepare( |
|
530 | + " AND {$wpdb->terms}.slug IN ({$event_category_slugs_prepare}) ", |
|
531 | + $event_category_slugs_array |
|
532 | + ); |
|
533 | + } |
|
534 | + return ''; |
|
535 | + } |
|
536 | + |
|
537 | + |
|
538 | + /** |
|
539 | + * @param boolean $month |
|
540 | + * @return string |
|
541 | + * @throws EE_Error |
|
542 | + * @throws InvalidArgumentException |
|
543 | + * @throws InvalidDataTypeException |
|
544 | + * @throws InvalidInterfaceException |
|
545 | + */ |
|
546 | + public static function posts_where_sql_for_event_list_month($month = null) |
|
547 | + { |
|
548 | + $SQL = ''; |
|
549 | + if (! empty($month)) { |
|
550 | + $datetime_table = EEM_Datetime::instance()->table(); |
|
551 | + // event start date is LESS than the end of the month ( so nothing that doesn't start until next month ) |
|
552 | + $SQL = " AND {$datetime_table}.DTT_EVT_start <= '"; |
|
553 | + $SQL .= date('Y-m-t 23:59:59', EEH_DTT_Helper::first_of_month_timestamp($month)) . "'"; |
|
554 | + // event end date is GREATER than the start of the month ( so nothing that ended before this month ) |
|
555 | + $SQL .= " AND {$datetime_table}.DTT_EVT_end >= '"; |
|
556 | + $SQL .= date('Y-m-01 0:0:00', EEH_DTT_Helper::first_of_month_timestamp($month)) . "' "; |
|
557 | + } |
|
558 | + return $SQL; |
|
559 | + } |
|
560 | + |
|
561 | + |
|
562 | + /** |
|
563 | + * @param string $SQL |
|
564 | + * @param WP_Query $wp_query |
|
565 | + * @return string |
|
566 | + * @throws EE_Error |
|
567 | + * @throws InvalidArgumentException |
|
568 | + * @throws InvalidDataTypeException |
|
569 | + * @throws InvalidInterfaceException |
|
570 | + */ |
|
571 | + public static function posts_orderby($SQL, WP_Query $wp_query) |
|
572 | + { |
|
573 | + if (EEH_Event_Query::apply_query_filters($wp_query)) { |
|
574 | + $SQL = EEH_Event_Query::posts_orderby_sql( |
|
575 | + EEH_Event_Query::$_event_query_orderby, |
|
576 | + EEH_Event_Query::$_event_query_sort |
|
577 | + ); |
|
578 | + } |
|
579 | + return $SQL; |
|
580 | + } |
|
581 | + |
|
582 | + |
|
583 | + /** |
|
584 | + * posts_orderby_sql |
|
585 | + * possible parameters: |
|
586 | + * ID |
|
587 | + * start_date |
|
588 | + * end_date |
|
589 | + * event_name |
|
590 | + * category_slug |
|
591 | + * ticket_start |
|
592 | + * ticket_end |
|
593 | + * venue_title |
|
594 | + * city |
|
595 | + * state |
|
596 | + * **IMPORTANT** |
|
597 | + * make sure to also send the $orderby_params array to the posts_join_for_orderby() method |
|
598 | + * or else some of the table references below will result in MySQL errors |
|
599 | + * |
|
600 | + * @param array $orderby_params |
|
601 | + * @param string $sort |
|
602 | + * @return string |
|
603 | + * @throws EE_Error |
|
604 | + * @throws InvalidArgumentException |
|
605 | + * @throws InvalidDataTypeException |
|
606 | + * @throws InvalidInterfaceException |
|
607 | + */ |
|
608 | + public static function posts_orderby_sql(array $orderby_params = [], $sort = 'ASC') |
|
609 | + { |
|
610 | + global $wpdb; |
|
611 | + $SQL = ''; |
|
612 | + $counter = 0; |
|
613 | + $sort = in_array($sort, ['ASC', 'asc', 'DESC', 'desc'], true) |
|
614 | + ? strtoupper($sort) |
|
615 | + : 'ASC'; |
|
616 | + // make sure 'orderby' is set in query params |
|
617 | + if (! isset(self::$_query_params['orderby'])) { |
|
618 | + self::$_query_params['orderby'] = []; |
|
619 | + } |
|
620 | + // loop thru $orderby_params (type cast as array) |
|
621 | + foreach ($orderby_params as $orderby) { |
|
622 | + // check if we have already added this param |
|
623 | + if (isset(self::$_query_params['orderby'][ $orderby ])) { |
|
624 | + // if so then remove from the $orderby_params so that the count() method below is accurate |
|
625 | + unset($orderby_params[ $orderby ]); |
|
626 | + // then bump ahead to the next param |
|
627 | + continue; |
|
628 | + } |
|
629 | + // this will ad a comma depending on whether this is the first or last param |
|
630 | + $glue = $counter === 0 || $counter === count($orderby_params) ? ' ' : ', '; |
|
631 | + // ok what's we dealing with? |
|
632 | + switch ($orderby) { |
|
633 | + case 'id': |
|
634 | + case 'ID': |
|
635 | + $SQL .= $glue . $wpdb->posts . '.ID ' . $sort; |
|
636 | + break; |
|
637 | + case 'end_date': |
|
638 | + $SQL .= $glue . EEM_Datetime::instance()->table() . '.DTT_EVT_end ' . $sort; |
|
639 | + break; |
|
640 | + case 'event_name': |
|
641 | + $SQL .= $glue . $wpdb->posts . '.post_title ' . $sort; |
|
642 | + break; |
|
643 | + case 'category_slug': |
|
644 | + $SQL .= $glue . $wpdb->terms . '.slug ' . $sort; |
|
645 | + break; |
|
646 | + case 'ticket_start': |
|
647 | + $SQL .= $glue . EEM_Ticket::instance()->table() . '.TKT_start_date ' . $sort; |
|
648 | + break; |
|
649 | + case 'ticket_end': |
|
650 | + $SQL .= $glue . EEM_Ticket::instance()->table() . '.TKT_end_date ' . $sort; |
|
651 | + break; |
|
652 | + case 'venue_title': |
|
653 | + $SQL .= $glue . 'venue_title ' . $sort; |
|
654 | + break; |
|
655 | + case 'city': |
|
656 | + $SQL .= $glue . EEM_Venue::instance()->second_table() . '.VNU_city ' . $sort; |
|
657 | + break; |
|
658 | + case 'state': |
|
659 | + $SQL .= $glue . EEM_State::instance()->table() . '.STA_name ' . $sort; |
|
660 | + break; |
|
661 | + case 'start_date': |
|
662 | + default: |
|
663 | + $SQL .= $glue . ' event_start_date ' . $sort; |
|
664 | + break; |
|
665 | + } |
|
666 | + // add to array of orderby params that have been added |
|
667 | + self::$_query_params['orderby'][ $orderby ] = true; |
|
668 | + $counter++; |
|
669 | + } |
|
670 | + return $SQL; |
|
671 | + } |
|
672 | + |
|
673 | + |
|
674 | + /** |
|
675 | + * @return RequestInterface |
|
676 | + * @since 4.10.14.p |
|
677 | + */ |
|
678 | + private static function getRequest() |
|
679 | + { |
|
680 | + return LoaderFactory::getLoader()->getShared(RequestInterface::class); |
|
681 | + } |
|
682 | 682 | } |
@@ -246,22 +246,22 @@ discard block |
||
246 | 246 | // but prior to the 'AHEE__EE_System__core_loaded_and_ready' hook |
247 | 247 | // (which currently fires on the init hook at priority 9), |
248 | 248 | // can be turned back OFF via: add_filter( 'FHEE_load_EE_Session', '__return_false' ); |
249 | - if (! apply_filters('FHEE_load_EE_Session', true)) { |
|
249 | + if ( ! apply_filters('FHEE_load_EE_Session', true)) { |
|
250 | 250 | return; |
251 | 251 | } |
252 | 252 | $this->session_start_handler = $session_start_handler; |
253 | 253 | $this->session_lifespan = $lifespan; |
254 | 254 | $this->request = $request; |
255 | - if (! defined('ESPRESSO_SESSION')) { |
|
255 | + if ( ! defined('ESPRESSO_SESSION')) { |
|
256 | 256 | define('ESPRESSO_SESSION', true); |
257 | 257 | } |
258 | 258 | // retrieve session options from db |
259 | 259 | $session_settings = (array) get_option(EE_Session::OPTION_NAME_SETTINGS, array()); |
260 | - if (! empty($session_settings)) { |
|
260 | + if ( ! empty($session_settings)) { |
|
261 | 261 | // cycle though existing session options |
262 | 262 | foreach ($session_settings as $var_name => $session_setting) { |
263 | 263 | // set values for class properties |
264 | - $var_name = '_' . $var_name; |
|
264 | + $var_name = '_'.$var_name; |
|
265 | 265 | $this->{$var_name} = $session_setting; |
266 | 266 | } |
267 | 267 | } |
@@ -322,7 +322,7 @@ discard block |
||
322 | 322 | public function open_session() |
323 | 323 | { |
324 | 324 | // check for existing session and retrieve it from db |
325 | - if (! $this->_espresso_session()) { |
|
325 | + if ( ! $this->_espresso_session()) { |
|
326 | 326 | // or just start a new one |
327 | 327 | $this->_create_espresso_session(); |
328 | 328 | } |
@@ -399,7 +399,7 @@ discard block |
||
399 | 399 | EE_Session::SAVE_STATE_CLEAN, |
400 | 400 | EE_Session::SAVE_STATE_DIRTY, |
401 | 401 | ]; |
402 | - if (! in_array($save_state, $valid_save_states, true)) { |
|
402 | + if ( ! in_array($save_state, $valid_save_states, true)) { |
|
403 | 403 | $save_state = EE_Session::SAVE_STATE_DIRTY; |
404 | 404 | } |
405 | 405 | $this->save_state = $save_state; |
@@ -417,9 +417,9 @@ discard block |
||
417 | 417 | // set some defaults |
418 | 418 | foreach ($this->_default_session_vars as $key => $default_var) { |
419 | 419 | if (is_array($default_var)) { |
420 | - $this->_session_data[ $key ] = array(); |
|
420 | + $this->_session_data[$key] = array(); |
|
421 | 421 | } else { |
422 | - $this->_session_data[ $key ] = ''; |
|
422 | + $this->_session_data[$key] = ''; |
|
423 | 423 | } |
424 | 424 | } |
425 | 425 | } |
@@ -555,8 +555,8 @@ discard block |
||
555 | 555 | $this->reset_checkout(); |
556 | 556 | $this->reset_transaction(); |
557 | 557 | } |
558 | - if (! empty($key)) { |
|
559 | - return isset($this->_session_data[ $key ]) ? $this->_session_data[ $key ] : null; |
|
558 | + if ( ! empty($key)) { |
|
559 | + return isset($this->_session_data[$key]) ? $this->_session_data[$key] : null; |
|
560 | 560 | } |
561 | 561 | return $this->_session_data; |
562 | 562 | } |
@@ -584,7 +584,7 @@ discard block |
||
584 | 584 | return false; |
585 | 585 | } |
586 | 586 | foreach ($data as $key => $value) { |
587 | - if (isset($this->_default_session_vars[ $key ])) { |
|
587 | + if (isset($this->_default_session_vars[$key])) { |
|
588 | 588 | EE_Error::add_error( |
589 | 589 | sprintf( |
590 | 590 | esc_html__( |
@@ -599,7 +599,7 @@ discard block |
||
599 | 599 | ); |
600 | 600 | return false; |
601 | 601 | } |
602 | - $this->_session_data[ $key ] = $value; |
|
602 | + $this->_session_data[$key] = $value; |
|
603 | 603 | $this->setSaveState(); |
604 | 604 | } |
605 | 605 | return true; |
@@ -630,7 +630,7 @@ discard block |
||
630 | 630 | $this->_user_agent = $this->request->userAgent(); |
631 | 631 | // now let's retrieve what's in the db |
632 | 632 | $session_data = $this->_retrieve_session_data(); |
633 | - if (! empty($session_data)) { |
|
633 | + if ( ! empty($session_data)) { |
|
634 | 634 | // get the current time in UTC |
635 | 635 | $this->_time = $this->_time !== null ? $this->_time : time(); |
636 | 636 | // and reset the session expiration |
@@ -641,7 +641,7 @@ discard block |
||
641 | 641 | // set initial site access time and the session expiration |
642 | 642 | $this->_set_init_access_and_expiration(); |
643 | 643 | // set referer |
644 | - $this->_session_data['pages_visited'][ $this->_session_data['init_access'] ] = esc_attr( |
|
644 | + $this->_session_data['pages_visited'][$this->_session_data['init_access']] = esc_attr( |
|
645 | 645 | $this->request->getServerParam('HTTP_REFERER') |
646 | 646 | ); |
647 | 647 | // no previous session = go back and create one (on top of the data above) |
@@ -679,7 +679,7 @@ discard block |
||
679 | 679 | */ |
680 | 680 | protected function _retrieve_session_data() |
681 | 681 | { |
682 | - $ssn_key = EE_Session::session_id_prefix . $this->_sid; |
|
682 | + $ssn_key = EE_Session::session_id_prefix.$this->_sid; |
|
683 | 683 | try { |
684 | 684 | // we're using WP's Transient API to store session data using the PHP session ID as the option name |
685 | 685 | $session_data = $this->cache_storage->get($ssn_key, false); |
@@ -688,7 +688,7 @@ discard block |
||
688 | 688 | } |
689 | 689 | if (apply_filters('FHEE__EE_Session___perform_session_id_hash_check', WP_DEBUG)) { |
690 | 690 | $hash_check = $this->cache_storage->get( |
691 | - EE_Session::hash_check_prefix . $this->_sid, |
|
691 | + EE_Session::hash_check_prefix.$this->_sid, |
|
692 | 692 | false |
693 | 693 | ); |
694 | 694 | if ($hash_check && $hash_check !== md5($session_data)) { |
@@ -698,7 +698,7 @@ discard block |
||
698 | 698 | 'The stored data for session %1$s failed to pass a hash check and therefore appears to be invalid.', |
699 | 699 | 'event_espresso' |
700 | 700 | ), |
701 | - EE_Session::session_id_prefix . $this->_sid |
|
701 | + EE_Session::session_id_prefix.$this->_sid |
|
702 | 702 | ), |
703 | 703 | __FILE__, |
704 | 704 | __FUNCTION__, |
@@ -712,17 +712,17 @@ discard block |
||
712 | 712 | $row = $wpdb->get_row( |
713 | 713 | $wpdb->prepare( |
714 | 714 | "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s LIMIT 1", |
715 | - '_transient_' . $ssn_key |
|
715 | + '_transient_'.$ssn_key |
|
716 | 716 | ) |
717 | 717 | ); |
718 | 718 | $session_data = is_object($row) ? $row->option_value : null; |
719 | 719 | if ($session_data) { |
720 | 720 | $session_data = preg_replace_callback( |
721 | 721 | '!s:(d+):"(.*?)";!', |
722 | - function ($match) { |
|
722 | + function($match) { |
|
723 | 723 | return $match[1] === strlen($match[2]) |
724 | 724 | ? $match[0] |
725 | - : 's:' . strlen($match[2]) . ':"' . $match[2] . '";'; |
|
725 | + : 's:'.strlen($match[2]).':"'.$match[2].'";'; |
|
726 | 726 | }, |
727 | 727 | $session_data |
728 | 728 | ); |
@@ -733,7 +733,7 @@ discard block |
||
733 | 733 | $session_data = $this->encryption instanceof EE_Encryption |
734 | 734 | ? $this->encryption->base64_string_decode($session_data) |
735 | 735 | : $session_data; |
736 | - if (! is_array($session_data)) { |
|
736 | + if ( ! is_array($session_data)) { |
|
737 | 737 | try { |
738 | 738 | $session_data = maybe_unserialize($session_data); |
739 | 739 | } catch (Exception $e) { |
@@ -747,21 +747,21 @@ discard block |
||
747 | 747 | . '</pre><br>' |
748 | 748 | . $this->find_serialize_error($session_data) |
749 | 749 | : ''; |
750 | - $this->cache_storage->delete(EE_Session::session_id_prefix . $this->_sid); |
|
750 | + $this->cache_storage->delete(EE_Session::session_id_prefix.$this->_sid); |
|
751 | 751 | throw new InvalidSessionDataException($msg, 0, $e); |
752 | 752 | } |
753 | 753 | } |
754 | 754 | // just a check to make sure the session array is indeed an array |
755 | - if (! is_array($session_data)) { |
|
755 | + if ( ! is_array($session_data)) { |
|
756 | 756 | // no?!?! then something is wrong |
757 | 757 | $msg = esc_html__( |
758 | 758 | 'The session data is missing, invalid, or corrupted.', |
759 | 759 | 'event_espresso' |
760 | 760 | ); |
761 | 761 | $msg .= WP_DEBUG |
762 | - ? '<br><pre>' . print_r($session_data, true) . '</pre><br>' . $this->find_serialize_error($session_data) |
|
762 | + ? '<br><pre>'.print_r($session_data, true).'</pre><br>'.$this->find_serialize_error($session_data) |
|
763 | 763 | : ''; |
764 | - $this->cache_storage->delete(EE_Session::session_id_prefix . $this->_sid); |
|
764 | + $this->cache_storage->delete(EE_Session::session_id_prefix.$this->_sid); |
|
765 | 765 | throw new InvalidSessionDataException($msg); |
766 | 766 | } |
767 | 767 | if (isset($session_data['transaction']) && absint($session_data['transaction']) !== 0) { |
@@ -787,7 +787,7 @@ discard block |
||
787 | 787 | // check if the SID was passed explicitly, otherwise get from session, then add salt and hash it to reduce length |
788 | 788 | $session_id = $this->request->requestParamIsSet('EESID') |
789 | 789 | ? $this->request->getRequestParam('EESID') |
790 | - : md5(session_id() . get_current_blog_id() . $this->_get_sid_salt()); |
|
790 | + : md5(session_id().get_current_blog_id().$this->_get_sid_salt()); |
|
791 | 791 | return apply_filters('FHEE__EE_Session___generate_session_id__session_id', $session_id); |
792 | 792 | } |
793 | 793 | |
@@ -889,19 +889,19 @@ discard block |
||
889 | 889 | $page_visit = $this->_get_page_visit(); |
890 | 890 | if ($page_visit) { |
891 | 891 | // set pages visited where the first will be the http referrer |
892 | - $this->_session_data['pages_visited'][ $this->_time ] = $page_visit; |
|
892 | + $this->_session_data['pages_visited'][$this->_time] = $page_visit; |
|
893 | 893 | // we'll only save the last 10 page visits. |
894 | 894 | $session_data['pages_visited'] = array_slice($this->_session_data['pages_visited'], -10); |
895 | 895 | } |
896 | 896 | break; |
897 | 897 | default: |
898 | 898 | // carry any other data over |
899 | - $session_data[ $key ] = $this->_session_data[ $key ]; |
|
899 | + $session_data[$key] = $this->_session_data[$key]; |
|
900 | 900 | } |
901 | 901 | } |
902 | 902 | $this->_session_data = $session_data; |
903 | 903 | // creating a new session does not require saving to the db just yet |
904 | - if (! $new_session) { |
|
904 | + if ( ! $new_session) { |
|
905 | 905 | // ready? let's save |
906 | 906 | if ($this->_save_session_to_db()) { |
907 | 907 | return true; |
@@ -979,7 +979,7 @@ discard block |
||
979 | 979 | } |
980 | 980 | $transaction = $this->transaction(); |
981 | 981 | if ($transaction instanceof EE_Transaction) { |
982 | - if (! $transaction->ID()) { |
|
982 | + if ( ! $transaction->ID()) { |
|
983 | 983 | $transaction->save(); |
984 | 984 | } |
985 | 985 | $this->_session_data['transaction'] = $transaction->ID(); |
@@ -993,14 +993,14 @@ discard block |
||
993 | 993 | // maybe save hash check |
994 | 994 | if (apply_filters('FHEE__EE_Session___perform_session_id_hash_check', WP_DEBUG)) { |
995 | 995 | $this->cache_storage->add( |
996 | - EE_Session::hash_check_prefix . $this->_sid, |
|
996 | + EE_Session::hash_check_prefix.$this->_sid, |
|
997 | 997 | md5($session_data), |
998 | 998 | $this->session_lifespan->inSeconds() |
999 | 999 | ); |
1000 | 1000 | } |
1001 | 1001 | // we're using the Transient API for storing session data, |
1002 | 1002 | $saved = $this->cache_storage->add( |
1003 | - EE_Session::session_id_prefix . $this->_sid, |
|
1003 | + EE_Session::session_id_prefix.$this->_sid, |
|
1004 | 1004 | $session_data, |
1005 | 1005 | $this->session_lifespan->inSeconds() |
1006 | 1006 | ); |
@@ -1015,7 +1015,7 @@ discard block |
||
1015 | 1015 | */ |
1016 | 1016 | public function _get_page_visit() |
1017 | 1017 | { |
1018 | - $page_visit = home_url('/') . 'wp-admin/admin-ajax.php'; |
|
1018 | + $page_visit = home_url('/').'wp-admin/admin-ajax.php'; |
|
1019 | 1019 | // check for request url |
1020 | 1020 | if ($this->request->serverParamIsSet('REQUEST_URI')) { |
1021 | 1021 | $page_id = '?'; |
@@ -1027,14 +1027,14 @@ discard block |
||
1027 | 1027 | // check for page_id in SERVER REQUEST |
1028 | 1028 | if ($this->request->requestParamIsSet('page_id')) { |
1029 | 1029 | // rebuild $e_reg without any of the extra parameters |
1030 | - $page_id .= 'page_id=' . $this->request->getRequestParam('page_id', 0, 'int') . '&'; |
|
1030 | + $page_id .= 'page_id='.$this->request->getRequestParam('page_id', 0, 'int').'&'; |
|
1031 | 1031 | } |
1032 | 1032 | // check for $e_reg in SERVER REQUEST |
1033 | 1033 | if ($this->request->requestParamIsSet('ee')) { |
1034 | 1034 | // rebuild $e_reg without any of the extra parameters |
1035 | - $e_reg = 'ee=' . $this->request->getRequestParam('ee'); |
|
1035 | + $e_reg = 'ee='.$this->request->getRequestParam('ee'); |
|
1036 | 1036 | } |
1037 | - $page_visit = esc_url(rtrim($http_host . $request_uri . $page_id . $e_reg, '?')); |
|
1037 | + $page_visit = esc_url(rtrim($http_host.$request_uri.$page_id.$e_reg, '?')); |
|
1038 | 1038 | } |
1039 | 1039 | return $page_visit !== home_url('/wp-admin/admin-ajax.php') ? $page_visit : ''; |
1040 | 1040 | } |
@@ -1071,7 +1071,7 @@ discard block |
||
1071 | 1071 | // <span style="color:#2EA2CC">' . __CLASS__ . '</span>::<span style="color:#E76700">' . __FUNCTION__ . '( ' . $class . '::' . $function . '() )</span><br/> |
1072 | 1072 | // <span style="font-size:9px;font-weight:normal;">' . __FILE__ . '</span> <b style="font-size:10px;"> ' . __LINE__ . ' </b> |
1073 | 1073 | // </h3>'; |
1074 | - do_action('AHEE_log', __FILE__, __FUNCTION__, 'session cleared by : ' . $class . '::' . $function . '()'); |
|
1074 | + do_action('AHEE_log', __FILE__, __FUNCTION__, 'session cleared by : '.$class.'::'.$function.'()'); |
|
1075 | 1075 | $this->reset_cart(); |
1076 | 1076 | $this->reset_checkout(); |
1077 | 1077 | $this->reset_transaction(); |
@@ -1094,7 +1094,7 @@ discard block |
||
1094 | 1094 | public function reset_data($data_to_reset = array(), $show_all_notices = false) |
1095 | 1095 | { |
1096 | 1096 | // if $data_to_reset is not in an array, then put it in one |
1097 | - if (! is_array($data_to_reset)) { |
|
1097 | + if ( ! is_array($data_to_reset)) { |
|
1098 | 1098 | $data_to_reset = array($data_to_reset); |
1099 | 1099 | } |
1100 | 1100 | // nothing ??? go home! |
@@ -1114,11 +1114,11 @@ discard block |
||
1114 | 1114 | // since $data_to_reset is an array, cycle through the values |
1115 | 1115 | foreach ($data_to_reset as $reset) { |
1116 | 1116 | // first check to make sure it is a valid session var |
1117 | - if (isset($this->_session_data[ $reset ])) { |
|
1117 | + if (isset($this->_session_data[$reset])) { |
|
1118 | 1118 | // then check to make sure it is not a default var |
1119 | - if (! array_key_exists($reset, $this->_default_session_vars)) { |
|
1119 | + if ( ! array_key_exists($reset, $this->_default_session_vars)) { |
|
1120 | 1120 | // remove session var |
1121 | - unset($this->_session_data[ $reset ]); |
|
1121 | + unset($this->_session_data[$reset]); |
|
1122 | 1122 | $this->setSaveState(); |
1123 | 1123 | if ($show_all_notices) { |
1124 | 1124 | EE_Error::add_success( |
@@ -1221,7 +1221,7 @@ discard block |
||
1221 | 1221 | // or use that for the new transient cleanup query limit |
1222 | 1222 | add_filter( |
1223 | 1223 | 'FHEE__TransientCacheStorage__clearExpiredTransients__limit', |
1224 | - function () use ($expired_session_transient_delete_query_limit) { |
|
1224 | + function() use ($expired_session_transient_delete_query_limit) { |
|
1225 | 1225 | return $expired_session_transient_delete_query_limit; |
1226 | 1226 | } |
1227 | 1227 | ); |
@@ -1239,7 +1239,7 @@ discard block |
||
1239 | 1239 | $error = '<pre>'; |
1240 | 1240 | $data2 = preg_replace_callback( |
1241 | 1241 | '!s:(\d+):"(.*?)";!', |
1242 | - function ($match) { |
|
1242 | + function($match) { |
|
1243 | 1243 | return ($match[1] === strlen($match[2])) |
1244 | 1244 | ? $match[0] |
1245 | 1245 | : 's:' |
@@ -1251,13 +1251,13 @@ discard block |
||
1251 | 1251 | $data1 |
1252 | 1252 | ); |
1253 | 1253 | $max = (strlen($data1) > strlen($data2)) ? strlen($data1) : strlen($data2); |
1254 | - $error .= $data1 . PHP_EOL; |
|
1255 | - $error .= $data2 . PHP_EOL; |
|
1254 | + $error .= $data1.PHP_EOL; |
|
1255 | + $error .= $data2.PHP_EOL; |
|
1256 | 1256 | for ($i = 0; $i < $max; $i++) { |
1257 | - if (@$data1[ $i ] !== @$data2[ $i ]) { |
|
1258 | - $error .= 'Difference ' . @$data1[ $i ] . ' != ' . @$data2[ $i ] . PHP_EOL; |
|
1259 | - $error .= "\t-> ORD number " . ord(@$data1[ $i ]) . ' != ' . ord(@$data2[ $i ]) . PHP_EOL; |
|
1260 | - $error .= "\t-> Line Number = $i" . PHP_EOL; |
|
1257 | + if (@$data1[$i] !== @$data2[$i]) { |
|
1258 | + $error .= 'Difference '.@$data1[$i].' != '.@$data2[$i].PHP_EOL; |
|
1259 | + $error .= "\t-> ORD number ".ord(@$data1[$i]).' != '.ord(@$data2[$i]).PHP_EOL; |
|
1260 | + $error .= "\t-> Line Number = $i".PHP_EOL; |
|
1261 | 1261 | $start = ($i - 20); |
1262 | 1262 | $start = ($start < 0) ? 0 : $start; |
1263 | 1263 | $length = 40; |
@@ -1272,7 +1272,7 @@ discard block |
||
1272 | 1272 | $error .= "\t-> Section Data1 = "; |
1273 | 1273 | $error .= substr_replace( |
1274 | 1274 | substr($data1, $start, $length), |
1275 | - "<b style=\"color:green\">{$data1[ $i ]}</b>", |
|
1275 | + "<b style=\"color:green\">{$data1[$i]}</b>", |
|
1276 | 1276 | $rpoint, |
1277 | 1277 | $rlength |
1278 | 1278 | ); |
@@ -1280,7 +1280,7 @@ discard block |
||
1280 | 1280 | $error .= "\t-> Section Data2 = "; |
1281 | 1281 | $error .= substr_replace( |
1282 | 1282 | substr($data2, $start, $length), |
1283 | - "<b style=\"color:red\">{$data2[ $i ]}</b>", |
|
1283 | + "<b style=\"color:red\">{$data2[$i]}</b>", |
|
1284 | 1284 | $rpoint, |
1285 | 1285 | $rlength |
1286 | 1286 | ); |
@@ -1311,7 +1311,7 @@ discard block |
||
1311 | 1311 | public function garbageCollection() |
1312 | 1312 | { |
1313 | 1313 | // only perform during regular requests if last garbage collection was over an hour ago |
1314 | - if (! (defined('DOING_AJAX') && DOING_AJAX) && (time() - HOUR_IN_SECONDS) >= $this->_last_gc) { |
|
1314 | + if ( ! (defined('DOING_AJAX') && DOING_AJAX) && (time() - HOUR_IN_SECONDS) >= $this->_last_gc) { |
|
1315 | 1315 | $this->_last_gc = time(); |
1316 | 1316 | $this->updateSessionSettings(array('last_gc' => $this->_last_gc)); |
1317 | 1317 | /** @type WPDB $wpdb */ |
@@ -1346,7 +1346,7 @@ discard block |
||
1346 | 1346 | // AND option_value < 1508368198 LIMIT 50 |
1347 | 1347 | $expired_sessions = $wpdb->get_col($SQL); |
1348 | 1348 | // valid results? |
1349 | - if (! $expired_sessions instanceof WP_Error && ! empty($expired_sessions)) { |
|
1349 | + if ( ! $expired_sessions instanceof WP_Error && ! empty($expired_sessions)) { |
|
1350 | 1350 | $this->cache_storage->deleteMany($expired_sessions, true); |
1351 | 1351 | } |
1352 | 1352 | } |
@@ -24,1330 +24,1330 @@ discard block |
||
24 | 24 | */ |
25 | 25 | class EE_Session implements SessionIdentifierInterface, ResettableInterface |
26 | 26 | { |
27 | - const session_id_prefix = 'ee_ssn_'; |
|
28 | - |
|
29 | - const hash_check_prefix = 'ee_shc_'; |
|
30 | - |
|
31 | - const OPTION_NAME_SETTINGS = 'ee_session_settings'; |
|
32 | - |
|
33 | - const STATUS_CLOSED = 0; |
|
34 | - |
|
35 | - const STATUS_OPEN = 1; |
|
36 | - |
|
37 | - const SAVE_STATE_CLEAN = 'clean'; |
|
38 | - const SAVE_STATE_DIRTY = 'dirty'; |
|
39 | - |
|
40 | - |
|
41 | - /** |
|
42 | - * instance of the EE_Session object |
|
43 | - * |
|
44 | - * @var EE_Session |
|
45 | - */ |
|
46 | - private static $_instance; |
|
47 | - |
|
48 | - /** |
|
49 | - * @var CacheStorageInterface $cache_storage |
|
50 | - */ |
|
51 | - protected $cache_storage; |
|
52 | - |
|
53 | - /** |
|
54 | - * @var EE_Encryption $encryption |
|
55 | - */ |
|
56 | - protected $encryption; |
|
57 | - |
|
58 | - /** |
|
59 | - * @var SessionStartHandler $session_start_handler |
|
60 | - */ |
|
61 | - protected $session_start_handler; |
|
62 | - |
|
63 | - /** |
|
64 | - * the session id |
|
65 | - * |
|
66 | - * @var string |
|
67 | - */ |
|
68 | - private $_sid; |
|
69 | - |
|
70 | - /** |
|
71 | - * session id salt |
|
72 | - * |
|
73 | - * @var string |
|
74 | - */ |
|
75 | - private $_sid_salt; |
|
76 | - |
|
77 | - /** |
|
78 | - * session data |
|
79 | - * |
|
80 | - * @var array |
|
81 | - */ |
|
82 | - private $_session_data = array(); |
|
83 | - |
|
84 | - /** |
|
85 | - * how long an EE session lasts |
|
86 | - * default session lifespan of 1 hour (for not so instant IPNs) |
|
87 | - * |
|
88 | - * @var SessionLifespan $session_lifespan |
|
89 | - */ |
|
90 | - private $session_lifespan; |
|
91 | - |
|
92 | - /** |
|
93 | - * session expiration time as Unix timestamp in GMT |
|
94 | - * |
|
95 | - * @var int |
|
96 | - */ |
|
97 | - private $_expiration; |
|
98 | - |
|
99 | - /** |
|
100 | - * whether or not session has expired at some point |
|
101 | - * |
|
102 | - * @var boolean |
|
103 | - */ |
|
104 | - private $_expired = false; |
|
105 | - |
|
106 | - /** |
|
107 | - * current time as Unix timestamp in GMT |
|
108 | - * |
|
109 | - * @var int |
|
110 | - */ |
|
111 | - private $_time; |
|
112 | - |
|
113 | - /** |
|
114 | - * whether to encrypt session data |
|
115 | - * |
|
116 | - * @var bool |
|
117 | - */ |
|
118 | - private $_use_encryption; |
|
119 | - |
|
120 | - /** |
|
121 | - * well... according to the server... |
|
122 | - * |
|
123 | - * @var null |
|
124 | - */ |
|
125 | - private $_user_agent; |
|
126 | - |
|
127 | - /** |
|
128 | - * do you really trust the server ? |
|
129 | - * |
|
130 | - * @var null |
|
131 | - */ |
|
132 | - private $_ip_address; |
|
133 | - |
|
134 | - /** |
|
135 | - * current WP user_id |
|
136 | - * |
|
137 | - * @var null |
|
138 | - */ |
|
139 | - private $_wp_user_id; |
|
140 | - |
|
141 | - /** |
|
142 | - * array for defining default session vars |
|
143 | - * |
|
144 | - * @var array |
|
145 | - */ |
|
146 | - private $_default_session_vars = array( |
|
147 | - 'id' => null, |
|
148 | - 'user_id' => null, |
|
149 | - 'ip_address' => null, |
|
150 | - 'user_agent' => null, |
|
151 | - 'init_access' => null, |
|
152 | - 'last_access' => null, |
|
153 | - 'expiration' => null, |
|
154 | - 'pages_visited' => array(), |
|
155 | - ); |
|
156 | - |
|
157 | - /** |
|
158 | - * timestamp for when last garbage collection cycle was performed |
|
159 | - * |
|
160 | - * @var int $_last_gc |
|
161 | - */ |
|
162 | - private $_last_gc; |
|
163 | - |
|
164 | - /** |
|
165 | - * @var RequestInterface $request |
|
166 | - */ |
|
167 | - protected $request; |
|
168 | - |
|
169 | - /** |
|
170 | - * whether session is active or not |
|
171 | - * |
|
172 | - * @var int $status |
|
173 | - */ |
|
174 | - private $status = EE_Session::STATUS_CLOSED; |
|
175 | - |
|
176 | - /** |
|
177 | - * whether session data has changed therefore requiring a session save |
|
178 | - * |
|
179 | - * @var string $save_state |
|
180 | - */ |
|
181 | - private $save_state = EE_Session::SAVE_STATE_CLEAN; |
|
182 | - |
|
183 | - |
|
184 | - /** |
|
185 | - * @singleton method used to instantiate class object |
|
186 | - * @param CacheStorageInterface $cache_storage |
|
187 | - * @param SessionLifespan|null $lifespan |
|
188 | - * @param RequestInterface $request |
|
189 | - * @param SessionStartHandler $session_start_handler |
|
190 | - * @param EE_Encryption $encryption |
|
191 | - * @return EE_Session |
|
192 | - * @throws InvalidArgumentException |
|
193 | - * @throws InvalidDataTypeException |
|
194 | - * @throws InvalidInterfaceException |
|
195 | - */ |
|
196 | - public static function instance( |
|
197 | - CacheStorageInterface $cache_storage = null, |
|
198 | - SessionLifespan $lifespan = null, |
|
199 | - RequestInterface $request = null, |
|
200 | - SessionStartHandler $session_start_handler = null, |
|
201 | - EE_Encryption $encryption = null |
|
202 | - ) { |
|
203 | - // check if class object is instantiated |
|
204 | - // session loading is turned ON by default, but prior to the init hook, can be turned back OFF via: |
|
205 | - // add_filter( 'FHEE_load_EE_Session', '__return_false' ); |
|
206 | - if ( |
|
207 | - ! self::$_instance instanceof EE_Session |
|
208 | - && $cache_storage instanceof CacheStorageInterface |
|
209 | - && $lifespan instanceof SessionLifespan |
|
210 | - && $request instanceof RequestInterface |
|
211 | - && $session_start_handler instanceof SessionStartHandler |
|
212 | - && apply_filters('FHEE_load_EE_Session', true) |
|
213 | - ) { |
|
214 | - self::$_instance = new self( |
|
215 | - $cache_storage, |
|
216 | - $lifespan, |
|
217 | - $request, |
|
218 | - $session_start_handler, |
|
219 | - $encryption |
|
220 | - ); |
|
221 | - } |
|
222 | - return self::$_instance; |
|
223 | - } |
|
224 | - |
|
225 | - |
|
226 | - /** |
|
227 | - * protected constructor to prevent direct creation |
|
228 | - * |
|
229 | - * @param CacheStorageInterface $cache_storage |
|
230 | - * @param SessionLifespan $lifespan |
|
231 | - * @param RequestInterface $request |
|
232 | - * @param SessionStartHandler $session_start_handler |
|
233 | - * @param EE_Encryption $encryption |
|
234 | - * @throws InvalidArgumentException |
|
235 | - * @throws InvalidDataTypeException |
|
236 | - * @throws InvalidInterfaceException |
|
237 | - */ |
|
238 | - protected function __construct( |
|
239 | - CacheStorageInterface $cache_storage, |
|
240 | - SessionLifespan $lifespan, |
|
241 | - RequestInterface $request, |
|
242 | - SessionStartHandler $session_start_handler, |
|
243 | - EE_Encryption $encryption = null |
|
244 | - ) { |
|
245 | - // session loading is turned ON by default, |
|
246 | - // but prior to the 'AHEE__EE_System__core_loaded_and_ready' hook |
|
247 | - // (which currently fires on the init hook at priority 9), |
|
248 | - // can be turned back OFF via: add_filter( 'FHEE_load_EE_Session', '__return_false' ); |
|
249 | - if (! apply_filters('FHEE_load_EE_Session', true)) { |
|
250 | - return; |
|
251 | - } |
|
252 | - $this->session_start_handler = $session_start_handler; |
|
253 | - $this->session_lifespan = $lifespan; |
|
254 | - $this->request = $request; |
|
255 | - if (! defined('ESPRESSO_SESSION')) { |
|
256 | - define('ESPRESSO_SESSION', true); |
|
257 | - } |
|
258 | - // retrieve session options from db |
|
259 | - $session_settings = (array) get_option(EE_Session::OPTION_NAME_SETTINGS, array()); |
|
260 | - if (! empty($session_settings)) { |
|
261 | - // cycle though existing session options |
|
262 | - foreach ($session_settings as $var_name => $session_setting) { |
|
263 | - // set values for class properties |
|
264 | - $var_name = '_' . $var_name; |
|
265 | - $this->{$var_name} = $session_setting; |
|
266 | - } |
|
267 | - } |
|
268 | - $this->cache_storage = $cache_storage; |
|
269 | - // are we using encryption? |
|
270 | - $this->_use_encryption = $encryption instanceof EE_Encryption |
|
271 | - && EE_Registry::instance()->CFG->admin->encode_session_data(); |
|
272 | - // encrypt data via: $this->encryption->encrypt(); |
|
273 | - $this->encryption = $encryption; |
|
274 | - // filter hook allows outside functions/classes/plugins to change default empty cart |
|
275 | - $extra_default_session_vars = apply_filters('FHEE__EE_Session__construct__extra_default_session_vars', array()); |
|
276 | - array_merge($this->_default_session_vars, $extra_default_session_vars); |
|
277 | - // apply default session vars |
|
278 | - $this->_set_defaults(); |
|
279 | - add_action('AHEE__EE_System__initialize', array($this, 'open_session')); |
|
280 | - // check request for 'clear_session' param |
|
281 | - add_action('AHEE__EE_Request_Handler__construct__complete', array($this, 'wp_loaded')); |
|
282 | - // once everything is all said and done, |
|
283 | - add_action('shutdown', array($this, 'update'), 100); |
|
284 | - add_action('shutdown', array($this, 'garbageCollection'), 1000); |
|
285 | - $this->configure_garbage_collection_filters(); |
|
286 | - } |
|
287 | - |
|
288 | - |
|
289 | - /** |
|
290 | - * @return bool |
|
291 | - * @throws InvalidArgumentException |
|
292 | - * @throws InvalidDataTypeException |
|
293 | - * @throws InvalidInterfaceException |
|
294 | - */ |
|
295 | - public static function isLoadedAndActive() |
|
296 | - { |
|
297 | - return did_action('AHEE__EE_System__core_loaded_and_ready') |
|
298 | - && EE_Session::instance() instanceof EE_Session |
|
299 | - && EE_Session::instance()->isActive(); |
|
300 | - } |
|
301 | - |
|
302 | - |
|
303 | - /** |
|
304 | - * @return bool |
|
305 | - */ |
|
306 | - public function isActive() |
|
307 | - { |
|
308 | - return $this->status === EE_Session::STATUS_OPEN; |
|
309 | - } |
|
310 | - |
|
311 | - |
|
312 | - /** |
|
313 | - * @return void |
|
314 | - * @throws EE_Error |
|
315 | - * @throws InvalidArgumentException |
|
316 | - * @throws InvalidDataTypeException |
|
317 | - * @throws InvalidInterfaceException |
|
318 | - * @throws InvalidSessionDataException |
|
319 | - * @throws RuntimeException |
|
320 | - * @throws ReflectionException |
|
321 | - */ |
|
322 | - public function open_session() |
|
323 | - { |
|
324 | - // check for existing session and retrieve it from db |
|
325 | - if (! $this->_espresso_session()) { |
|
326 | - // or just start a new one |
|
327 | - $this->_create_espresso_session(); |
|
328 | - } |
|
329 | - } |
|
330 | - |
|
331 | - |
|
332 | - /** |
|
333 | - * @return bool |
|
334 | - */ |
|
335 | - public function expired() |
|
336 | - { |
|
337 | - return $this->_expired; |
|
338 | - } |
|
339 | - |
|
340 | - |
|
341 | - /** |
|
342 | - * @return void |
|
343 | - */ |
|
344 | - public function reset_expired() |
|
345 | - { |
|
346 | - $this->_expired = false; |
|
347 | - } |
|
348 | - |
|
349 | - |
|
350 | - /** |
|
351 | - * @return int |
|
352 | - */ |
|
353 | - public function expiration() |
|
354 | - { |
|
355 | - return $this->_expiration; |
|
356 | - } |
|
357 | - |
|
358 | - |
|
359 | - /** |
|
360 | - * @return int |
|
361 | - */ |
|
362 | - public function extension() |
|
363 | - { |
|
364 | - return apply_filters('FHEE__EE_Session__extend_expiration__seconds_added', 10 * MINUTE_IN_SECONDS); |
|
365 | - } |
|
366 | - |
|
367 | - |
|
368 | - /** |
|
369 | - * @param int $time number of seconds to add to session expiration |
|
370 | - */ |
|
371 | - public function extend_expiration($time = 0) |
|
372 | - { |
|
373 | - $time = $time ? $time : $this->extension(); |
|
374 | - $this->_expiration += absint($time); |
|
375 | - } |
|
376 | - |
|
377 | - |
|
378 | - /** |
|
379 | - * @return int |
|
380 | - */ |
|
381 | - public function lifespan() |
|
382 | - { |
|
383 | - return $this->session_lifespan->inSeconds(); |
|
384 | - } |
|
385 | - |
|
386 | - |
|
387 | - /** |
|
388 | - * Marks whether the session data has been updated or not. |
|
389 | - * Valid options are: |
|
390 | - * EE_Session::SAVE_STATE_CLEAN - session data remains unchanged and updating is not necessary |
|
391 | - * EE_Session::SAVE_STATE_DIRTY - session data has changed since last save and needs to be updated |
|
392 | - * default value is EE_Session::SAVE_STATE_DIRTY |
|
393 | - * |
|
394 | - * @param string $save_state |
|
395 | - */ |
|
396 | - public function setSaveState($save_state = EE_Session::SAVE_STATE_DIRTY) |
|
397 | - { |
|
398 | - $valid_save_states = [ |
|
399 | - EE_Session::SAVE_STATE_CLEAN, |
|
400 | - EE_Session::SAVE_STATE_DIRTY, |
|
401 | - ]; |
|
402 | - if (! in_array($save_state, $valid_save_states, true)) { |
|
403 | - $save_state = EE_Session::SAVE_STATE_DIRTY; |
|
404 | - } |
|
405 | - $this->save_state = $save_state; |
|
406 | - } |
|
407 | - |
|
408 | - |
|
409 | - |
|
410 | - /** |
|
411 | - * This just sets some defaults for the _session data property |
|
412 | - * |
|
413 | - * @return void |
|
414 | - */ |
|
415 | - private function _set_defaults() |
|
416 | - { |
|
417 | - // set some defaults |
|
418 | - foreach ($this->_default_session_vars as $key => $default_var) { |
|
419 | - if (is_array($default_var)) { |
|
420 | - $this->_session_data[ $key ] = array(); |
|
421 | - } else { |
|
422 | - $this->_session_data[ $key ] = ''; |
|
423 | - } |
|
424 | - } |
|
425 | - } |
|
426 | - |
|
427 | - |
|
428 | - /** |
|
429 | - * @retrieve session data |
|
430 | - * @return string |
|
431 | - */ |
|
432 | - public function id() |
|
433 | - { |
|
434 | - return $this->_sid; |
|
435 | - } |
|
436 | - |
|
437 | - |
|
438 | - /** |
|
439 | - * @param \EE_Cart $cart |
|
440 | - * @return bool |
|
441 | - */ |
|
442 | - public function set_cart(EE_Cart $cart) |
|
443 | - { |
|
444 | - $this->_session_data['cart'] = $cart; |
|
445 | - $this->setSaveState(); |
|
446 | - return true; |
|
447 | - } |
|
448 | - |
|
449 | - |
|
450 | - /** |
|
451 | - * reset_cart |
|
452 | - */ |
|
453 | - public function reset_cart() |
|
454 | - { |
|
455 | - do_action('AHEE__EE_Session__reset_cart__before_reset', $this); |
|
456 | - $this->_session_data['cart'] = null; |
|
457 | - $this->setSaveState(); |
|
458 | - } |
|
459 | - |
|
460 | - |
|
461 | - /** |
|
462 | - * @return \EE_Cart |
|
463 | - */ |
|
464 | - public function cart() |
|
465 | - { |
|
466 | - return isset($this->_session_data['cart']) && $this->_session_data['cart'] instanceof EE_Cart |
|
467 | - ? $this->_session_data['cart'] |
|
468 | - : null; |
|
469 | - } |
|
470 | - |
|
471 | - |
|
472 | - /** |
|
473 | - * @param \EE_Checkout $checkout |
|
474 | - * @return bool |
|
475 | - */ |
|
476 | - public function set_checkout(EE_Checkout $checkout) |
|
477 | - { |
|
478 | - $this->_session_data['checkout'] = $checkout; |
|
479 | - $this->setSaveState(); |
|
480 | - return true; |
|
481 | - } |
|
482 | - |
|
483 | - |
|
484 | - /** |
|
485 | - * reset_checkout |
|
486 | - */ |
|
487 | - public function reset_checkout() |
|
488 | - { |
|
489 | - do_action('AHEE__EE_Session__reset_checkout__before_reset', $this); |
|
490 | - $this->_session_data['checkout'] = null; |
|
491 | - $this->setSaveState(); |
|
492 | - } |
|
493 | - |
|
494 | - |
|
495 | - /** |
|
496 | - * @return \EE_Checkout |
|
497 | - */ |
|
498 | - public function checkout() |
|
499 | - { |
|
500 | - return isset($this->_session_data['checkout']) && $this->_session_data['checkout'] instanceof EE_Checkout |
|
501 | - ? $this->_session_data['checkout'] |
|
502 | - : null; |
|
503 | - } |
|
504 | - |
|
505 | - |
|
506 | - /** |
|
507 | - * @param \EE_Transaction $transaction |
|
508 | - * @return bool |
|
509 | - * @throws EE_Error |
|
510 | - */ |
|
511 | - public function set_transaction(EE_Transaction $transaction) |
|
512 | - { |
|
513 | - // first remove the session from the transaction before we save the transaction in the session |
|
514 | - $transaction->set_txn_session_data(null); |
|
515 | - $this->_session_data['transaction'] = $transaction; |
|
516 | - $this->setSaveState(); |
|
517 | - return true; |
|
518 | - } |
|
519 | - |
|
520 | - |
|
521 | - /** |
|
522 | - * reset_transaction |
|
523 | - */ |
|
524 | - public function reset_transaction() |
|
525 | - { |
|
526 | - do_action('AHEE__EE_Session__reset_transaction__before_reset', $this); |
|
527 | - $this->_session_data['transaction'] = null; |
|
528 | - $this->setSaveState(); |
|
529 | - } |
|
530 | - |
|
531 | - |
|
532 | - /** |
|
533 | - * @return \EE_Transaction |
|
534 | - */ |
|
535 | - public function transaction() |
|
536 | - { |
|
537 | - return isset($this->_session_data['transaction']) |
|
538 | - && $this->_session_data['transaction'] instanceof EE_Transaction |
|
539 | - ? $this->_session_data['transaction'] |
|
540 | - : null; |
|
541 | - } |
|
542 | - |
|
543 | - |
|
544 | - /** |
|
545 | - * retrieve session data |
|
546 | - * |
|
547 | - * @param null $key |
|
548 | - * @param bool $reset_cache |
|
549 | - * @return array |
|
550 | - */ |
|
551 | - public function get_session_data($key = null, $reset_cache = false) |
|
552 | - { |
|
553 | - if ($reset_cache) { |
|
554 | - $this->reset_cart(); |
|
555 | - $this->reset_checkout(); |
|
556 | - $this->reset_transaction(); |
|
557 | - } |
|
558 | - if (! empty($key)) { |
|
559 | - return isset($this->_session_data[ $key ]) ? $this->_session_data[ $key ] : null; |
|
560 | - } |
|
561 | - return $this->_session_data; |
|
562 | - } |
|
563 | - |
|
564 | - |
|
565 | - /** |
|
566 | - * Returns TRUE on success, FALSE on fail |
|
567 | - * |
|
568 | - * @param array $data |
|
569 | - * @return bool |
|
570 | - */ |
|
571 | - public function set_session_data($data) |
|
572 | - { |
|
573 | - // nothing ??? bad data ??? go home! |
|
574 | - if (empty($data) || ! is_array($data)) { |
|
575 | - EE_Error::add_error( |
|
576 | - esc_html__( |
|
577 | - 'No session data or invalid session data was provided.', |
|
578 | - 'event_espresso' |
|
579 | - ), |
|
580 | - __FILE__, |
|
581 | - __FUNCTION__, |
|
582 | - __LINE__ |
|
583 | - ); |
|
584 | - return false; |
|
585 | - } |
|
586 | - foreach ($data as $key => $value) { |
|
587 | - if (isset($this->_default_session_vars[ $key ])) { |
|
588 | - EE_Error::add_error( |
|
589 | - sprintf( |
|
590 | - esc_html__( |
|
591 | - 'Sorry! %s is a default session datum and can not be reset.', |
|
592 | - 'event_espresso' |
|
593 | - ), |
|
594 | - $key |
|
595 | - ), |
|
596 | - __FILE__, |
|
597 | - __FUNCTION__, |
|
598 | - __LINE__ |
|
599 | - ); |
|
600 | - return false; |
|
601 | - } |
|
602 | - $this->_session_data[ $key ] = $value; |
|
603 | - $this->setSaveState(); |
|
604 | - } |
|
605 | - return true; |
|
606 | - } |
|
607 | - |
|
608 | - |
|
609 | - /** |
|
610 | - * @initiate session |
|
611 | - * @return bool TRUE on success, FALSE on fail |
|
612 | - * @throws EE_Error |
|
613 | - * @throws InvalidArgumentException |
|
614 | - * @throws InvalidDataTypeException |
|
615 | - * @throws InvalidInterfaceException |
|
616 | - * @throws InvalidSessionDataException |
|
617 | - * @throws RuntimeException |
|
618 | - * @throws ReflectionException |
|
619 | - */ |
|
620 | - private function _espresso_session() |
|
621 | - { |
|
622 | - do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
623 | - $this->session_start_handler->startSession(); |
|
624 | - $this->status = EE_Session::STATUS_OPEN; |
|
625 | - // get our modified session ID |
|
626 | - $this->_sid = $this->_generate_session_id(); |
|
627 | - // and the visitors IP |
|
628 | - $this->_ip_address = $this->request->ipAddress(); |
|
629 | - // set the "user agent" |
|
630 | - $this->_user_agent = $this->request->userAgent(); |
|
631 | - // now let's retrieve what's in the db |
|
632 | - $session_data = $this->_retrieve_session_data(); |
|
633 | - if (! empty($session_data)) { |
|
634 | - // get the current time in UTC |
|
635 | - $this->_time = $this->_time !== null ? $this->_time : time(); |
|
636 | - // and reset the session expiration |
|
637 | - $this->_expiration = isset($session_data['expiration']) |
|
638 | - ? $session_data['expiration'] |
|
639 | - : $this->_time + $this->session_lifespan->inSeconds(); |
|
640 | - } else { |
|
641 | - // set initial site access time and the session expiration |
|
642 | - $this->_set_init_access_and_expiration(); |
|
643 | - // set referer |
|
644 | - $this->_session_data['pages_visited'][ $this->_session_data['init_access'] ] = esc_attr( |
|
645 | - $this->request->getServerParam('HTTP_REFERER') |
|
646 | - ); |
|
647 | - // no previous session = go back and create one (on top of the data above) |
|
648 | - return false; |
|
649 | - } |
|
650 | - // now the user agent |
|
651 | - if ($session_data['user_agent'] !== $this->_user_agent) { |
|
652 | - return false; |
|
653 | - } |
|
654 | - // wait a minute... how old are you? |
|
655 | - if ($this->_time > $this->_expiration) { |
|
656 | - // yer too old fer me! |
|
657 | - $this->_expired = true; |
|
658 | - // wipe out everything that isn't a default session datum |
|
659 | - $this->clear_session(__CLASS__, __FUNCTION__); |
|
660 | - } |
|
661 | - // make event espresso session data available to plugin |
|
662 | - $this->_session_data = array_merge($this->_session_data, $session_data); |
|
663 | - return true; |
|
664 | - } |
|
665 | - |
|
666 | - |
|
667 | - /** |
|
668 | - * _get_session_data |
|
669 | - * Retrieves the session data, and attempts to correct any encoding issues that can occur due to improperly setup |
|
670 | - * databases |
|
671 | - * |
|
672 | - * @return array |
|
673 | - * @throws EE_Error |
|
674 | - * @throws InvalidArgumentException |
|
675 | - * @throws InvalidSessionDataException |
|
676 | - * @throws InvalidDataTypeException |
|
677 | - * @throws InvalidInterfaceException |
|
678 | - * @throws RuntimeException |
|
679 | - */ |
|
680 | - protected function _retrieve_session_data() |
|
681 | - { |
|
682 | - $ssn_key = EE_Session::session_id_prefix . $this->_sid; |
|
683 | - try { |
|
684 | - // we're using WP's Transient API to store session data using the PHP session ID as the option name |
|
685 | - $session_data = $this->cache_storage->get($ssn_key, false); |
|
686 | - if (empty($session_data)) { |
|
687 | - return array(); |
|
688 | - } |
|
689 | - if (apply_filters('FHEE__EE_Session___perform_session_id_hash_check', WP_DEBUG)) { |
|
690 | - $hash_check = $this->cache_storage->get( |
|
691 | - EE_Session::hash_check_prefix . $this->_sid, |
|
692 | - false |
|
693 | - ); |
|
694 | - if ($hash_check && $hash_check !== md5($session_data)) { |
|
695 | - EE_Error::add_error( |
|
696 | - sprintf( |
|
697 | - esc_html__( |
|
698 | - 'The stored data for session %1$s failed to pass a hash check and therefore appears to be invalid.', |
|
699 | - 'event_espresso' |
|
700 | - ), |
|
701 | - EE_Session::session_id_prefix . $this->_sid |
|
702 | - ), |
|
703 | - __FILE__, |
|
704 | - __FUNCTION__, |
|
705 | - __LINE__ |
|
706 | - ); |
|
707 | - } |
|
708 | - } |
|
709 | - } catch (Exception $e) { |
|
710 | - // let's just eat that error for now and attempt to correct any corrupted data |
|
711 | - global $wpdb; |
|
712 | - $row = $wpdb->get_row( |
|
713 | - $wpdb->prepare( |
|
714 | - "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s LIMIT 1", |
|
715 | - '_transient_' . $ssn_key |
|
716 | - ) |
|
717 | - ); |
|
718 | - $session_data = is_object($row) ? $row->option_value : null; |
|
719 | - if ($session_data) { |
|
720 | - $session_data = preg_replace_callback( |
|
721 | - '!s:(d+):"(.*?)";!', |
|
722 | - function ($match) { |
|
723 | - return $match[1] === strlen($match[2]) |
|
724 | - ? $match[0] |
|
725 | - : 's:' . strlen($match[2]) . ':"' . $match[2] . '";'; |
|
726 | - }, |
|
727 | - $session_data |
|
728 | - ); |
|
729 | - } |
|
730 | - $session_data = maybe_unserialize($session_data); |
|
731 | - } |
|
732 | - // in case the data is encoded... try to decode it |
|
733 | - $session_data = $this->encryption instanceof EE_Encryption |
|
734 | - ? $this->encryption->base64_string_decode($session_data) |
|
735 | - : $session_data; |
|
736 | - if (! is_array($session_data)) { |
|
737 | - try { |
|
738 | - $session_data = maybe_unserialize($session_data); |
|
739 | - } catch (Exception $e) { |
|
740 | - $msg = esc_html__( |
|
741 | - 'An error occurred while attempting to unserialize the session data.', |
|
742 | - 'event_espresso' |
|
743 | - ); |
|
744 | - $msg .= WP_DEBUG |
|
745 | - ? '<br><pre>' |
|
746 | - . print_r($session_data, true) |
|
747 | - . '</pre><br>' |
|
748 | - . $this->find_serialize_error($session_data) |
|
749 | - : ''; |
|
750 | - $this->cache_storage->delete(EE_Session::session_id_prefix . $this->_sid); |
|
751 | - throw new InvalidSessionDataException($msg, 0, $e); |
|
752 | - } |
|
753 | - } |
|
754 | - // just a check to make sure the session array is indeed an array |
|
755 | - if (! is_array($session_data)) { |
|
756 | - // no?!?! then something is wrong |
|
757 | - $msg = esc_html__( |
|
758 | - 'The session data is missing, invalid, or corrupted.', |
|
759 | - 'event_espresso' |
|
760 | - ); |
|
761 | - $msg .= WP_DEBUG |
|
762 | - ? '<br><pre>' . print_r($session_data, true) . '</pre><br>' . $this->find_serialize_error($session_data) |
|
763 | - : ''; |
|
764 | - $this->cache_storage->delete(EE_Session::session_id_prefix . $this->_sid); |
|
765 | - throw new InvalidSessionDataException($msg); |
|
766 | - } |
|
767 | - if (isset($session_data['transaction']) && absint($session_data['transaction']) !== 0) { |
|
768 | - $session_data['transaction'] = EEM_Transaction::instance()->get_one_by_ID( |
|
769 | - $session_data['transaction'] |
|
770 | - ); |
|
771 | - } |
|
772 | - return $session_data; |
|
773 | - } |
|
774 | - |
|
775 | - |
|
776 | - /** |
|
777 | - * _generate_session_id |
|
778 | - * Retrieves the PHP session id either directly from the PHP session, |
|
779 | - * or from the request array if it was passed in from an AJAX request. |
|
780 | - * The session id is then salted and hashed (mmm sounds tasty) |
|
781 | - * so that it can be safely used as a request param |
|
782 | - * |
|
783 | - * @return string |
|
784 | - */ |
|
785 | - protected function _generate_session_id() |
|
786 | - { |
|
787 | - // check if the SID was passed explicitly, otherwise get from session, then add salt and hash it to reduce length |
|
788 | - $session_id = $this->request->requestParamIsSet('EESID') |
|
789 | - ? $this->request->getRequestParam('EESID') |
|
790 | - : md5(session_id() . get_current_blog_id() . $this->_get_sid_salt()); |
|
791 | - return apply_filters('FHEE__EE_Session___generate_session_id__session_id', $session_id); |
|
792 | - } |
|
793 | - |
|
794 | - |
|
795 | - /** |
|
796 | - * _get_sid_salt |
|
797 | - * |
|
798 | - * @return string |
|
799 | - */ |
|
800 | - protected function _get_sid_salt() |
|
801 | - { |
|
802 | - // was session id salt already saved to db ? |
|
803 | - if (empty($this->_sid_salt)) { |
|
804 | - // no? then maybe use WP defined constant |
|
805 | - if (defined('AUTH_SALT')) { |
|
806 | - $this->_sid_salt = AUTH_SALT; |
|
807 | - } |
|
808 | - // if salt doesn't exist or is too short |
|
809 | - if (strlen($this->_sid_salt) < 32) { |
|
810 | - // create a new one |
|
811 | - $this->_sid_salt = wp_generate_password(64); |
|
812 | - } |
|
813 | - // and save it as a permanent session setting |
|
814 | - $this->updateSessionSettings(array('sid_salt' => $this->_sid_salt)); |
|
815 | - } |
|
816 | - return $this->_sid_salt; |
|
817 | - } |
|
818 | - |
|
819 | - |
|
820 | - /** |
|
821 | - * _set_init_access_and_expiration |
|
822 | - * |
|
823 | - * @return void |
|
824 | - */ |
|
825 | - protected function _set_init_access_and_expiration() |
|
826 | - { |
|
827 | - $this->_time = time(); |
|
828 | - $this->_expiration = $this->_time + $this->session_lifespan->inSeconds(); |
|
829 | - // set initial site access time |
|
830 | - $this->_session_data['init_access'] = $this->_time; |
|
831 | - // and the session expiration |
|
832 | - $this->_session_data['expiration'] = $this->_expiration; |
|
833 | - } |
|
834 | - |
|
835 | - |
|
836 | - /** |
|
837 | - * @update session data prior to saving to the db |
|
838 | - * @param bool $new_session |
|
839 | - * @return bool TRUE on success, FALSE on fail |
|
840 | - * @throws EE_Error |
|
841 | - * @throws InvalidArgumentException |
|
842 | - * @throws InvalidDataTypeException |
|
843 | - * @throws InvalidInterfaceException |
|
844 | - * @throws ReflectionException |
|
845 | - */ |
|
846 | - public function update($new_session = false) |
|
847 | - { |
|
848 | - $this->_session_data = is_array($this->_session_data) && isset($this->_session_data['id']) |
|
849 | - ? $this->_session_data |
|
850 | - : array(); |
|
851 | - if (empty($this->_session_data)) { |
|
852 | - $this->_set_defaults(); |
|
853 | - } |
|
854 | - $session_data = array(); |
|
855 | - foreach ($this->_session_data as $key => $value) { |
|
856 | - switch ($key) { |
|
857 | - case 'id': |
|
858 | - // session ID |
|
859 | - $session_data['id'] = $this->_sid; |
|
860 | - break; |
|
861 | - case 'ip_address': |
|
862 | - // visitor ip address |
|
863 | - $session_data['ip_address'] = $this->request->ipAddress(); |
|
864 | - break; |
|
865 | - case 'user_agent': |
|
866 | - // visitor user_agent |
|
867 | - $session_data['user_agent'] = $this->_user_agent; |
|
868 | - break; |
|
869 | - case 'init_access': |
|
870 | - $session_data['init_access'] = absint($value); |
|
871 | - break; |
|
872 | - case 'last_access': |
|
873 | - // current access time |
|
874 | - $session_data['last_access'] = $this->_time; |
|
875 | - break; |
|
876 | - case 'expiration': |
|
877 | - // when the session expires |
|
878 | - $session_data['expiration'] = ! empty($this->_expiration) |
|
879 | - ? $this->_expiration |
|
880 | - : $session_data['init_access'] + $this->session_lifespan->inSeconds(); |
|
881 | - break; |
|
882 | - case 'user_id': |
|
883 | - // current user if logged in |
|
884 | - $session_data['user_id'] = $this->_wp_user_id(); |
|
885 | - break; |
|
886 | - case 'pages_visited': |
|
887 | - $page_visit = $this->_get_page_visit(); |
|
888 | - if ($page_visit) { |
|
889 | - // set pages visited where the first will be the http referrer |
|
890 | - $this->_session_data['pages_visited'][ $this->_time ] = $page_visit; |
|
891 | - // we'll only save the last 10 page visits. |
|
892 | - $session_data['pages_visited'] = array_slice($this->_session_data['pages_visited'], -10); |
|
893 | - } |
|
894 | - break; |
|
895 | - default: |
|
896 | - // carry any other data over |
|
897 | - $session_data[ $key ] = $this->_session_data[ $key ]; |
|
898 | - } |
|
899 | - } |
|
900 | - $this->_session_data = $session_data; |
|
901 | - // creating a new session does not require saving to the db just yet |
|
902 | - if (! $new_session) { |
|
903 | - // ready? let's save |
|
904 | - if ($this->_save_session_to_db()) { |
|
905 | - return true; |
|
906 | - } |
|
907 | - return false; |
|
908 | - } |
|
909 | - // meh, why not? |
|
910 | - return true; |
|
911 | - } |
|
912 | - |
|
913 | - |
|
914 | - /** |
|
915 | - * @create session data array |
|
916 | - * @throws EE_Error |
|
917 | - * @throws InvalidArgumentException |
|
918 | - * @throws InvalidDataTypeException |
|
919 | - * @throws InvalidInterfaceException |
|
920 | - * @throws ReflectionException |
|
921 | - */ |
|
922 | - private function _create_espresso_session() |
|
923 | - { |
|
924 | - do_action('AHEE_log', __CLASS__, __FUNCTION__, ''); |
|
925 | - // use the update function for now with $new_session arg set to TRUE |
|
926 | - $this->update(true); |
|
927 | - } |
|
928 | - |
|
929 | - /** |
|
930 | - * Detects if there is anything worth saving in the session (eg the cart is a good one, notices are pretty good |
|
931 | - * too). This is used when determining if we want to save the session or not. |
|
932 | - * @since 4.9.67.p |
|
933 | - * @return bool |
|
934 | - */ |
|
935 | - private function sessionHasStuffWorthSaving() |
|
936 | - { |
|
937 | - return $this->save_state === EE_Session::SAVE_STATE_DIRTY |
|
938 | - // we may want to eventually remove the following |
|
939 | - // on the assumption that the above check is enough |
|
940 | - || $this->cart() instanceof EE_Cart |
|
941 | - || ( |
|
942 | - isset($this->_session_data['ee_notices']) |
|
943 | - && ( |
|
944 | - ! empty($this->_session_data['ee_notices']['attention']) |
|
945 | - || ! empty($this->_session_data['ee_notices']['errors']) |
|
946 | - || ! empty($this->_session_data['ee_notices']['success']) |
|
947 | - ) |
|
948 | - ); |
|
949 | - } |
|
950 | - |
|
951 | - |
|
952 | - /** |
|
953 | - * _save_session_to_db |
|
954 | - * |
|
955 | - * @param bool $clear_session |
|
956 | - * @return bool |
|
957 | - * @throws EE_Error |
|
958 | - * @throws InvalidArgumentException |
|
959 | - * @throws InvalidDataTypeException |
|
960 | - * @throws InvalidInterfaceException |
|
961 | - * @throws ReflectionException |
|
962 | - */ |
|
963 | - private function _save_session_to_db($clear_session = false) |
|
964 | - { |
|
965 | - // don't save sessions for crawlers |
|
966 | - // and unless we're deleting the session data, don't save anything if there isn't a cart |
|
967 | - if ( |
|
968 | - $this->request->isBot() |
|
969 | - || ( |
|
970 | - ! $clear_session |
|
971 | - && ! $this->sessionHasStuffWorthSaving() |
|
972 | - && apply_filters('FHEE__EE_Session___save_session_to_db__abort_session_save', true) |
|
973 | - ) |
|
974 | - ) { |
|
975 | - return false; |
|
976 | - } |
|
977 | - $transaction = $this->transaction(); |
|
978 | - if ($transaction instanceof EE_Transaction) { |
|
979 | - if (! $transaction->ID()) { |
|
980 | - $transaction->save(); |
|
981 | - } |
|
982 | - $this->_session_data['transaction'] = $transaction->ID(); |
|
983 | - } |
|
984 | - // then serialize all of our session data |
|
985 | - $session_data = serialize($this->_session_data); |
|
986 | - // do we need to also encode it to avoid corrupted data when saved to the db? |
|
987 | - $session_data = $this->_use_encryption |
|
988 | - ? $this->encryption->base64_string_encode($session_data) |
|
989 | - : $session_data; |
|
990 | - // maybe save hash check |
|
991 | - if (apply_filters('FHEE__EE_Session___perform_session_id_hash_check', WP_DEBUG)) { |
|
992 | - $this->cache_storage->add( |
|
993 | - EE_Session::hash_check_prefix . $this->_sid, |
|
994 | - md5($session_data), |
|
995 | - $this->session_lifespan->inSeconds() |
|
996 | - ); |
|
997 | - } |
|
998 | - // we're using the Transient API for storing session data, |
|
999 | - $saved = $this->cache_storage->add( |
|
1000 | - EE_Session::session_id_prefix . $this->_sid, |
|
1001 | - $session_data, |
|
1002 | - $this->session_lifespan->inSeconds() |
|
1003 | - ); |
|
1004 | - $this->setSaveState(EE_Session::SAVE_STATE_CLEAN); |
|
1005 | - return $saved; |
|
1006 | - } |
|
1007 | - |
|
1008 | - |
|
1009 | - /** |
|
1010 | - * @get the full page request the visitor is accessing |
|
1011 | - * @return string |
|
1012 | - */ |
|
1013 | - public function _get_page_visit() |
|
1014 | - { |
|
1015 | - $page_visit = home_url('/') . 'wp-admin/admin-ajax.php'; |
|
1016 | - // check for request url |
|
1017 | - if ($this->request->serverParamIsSet('REQUEST_URI')) { |
|
1018 | - $page_id = '?'; |
|
1019 | - $e_reg = ''; |
|
1020 | - $request_uri = $this->request->getServerParam('REQUEST_URI'); |
|
1021 | - $ru_bits = explode('?', $request_uri); |
|
1022 | - $request_uri = $ru_bits[0]; |
|
1023 | - $http_host = $this->request->getServerParam('HTTP_HOST'); |
|
1024 | - // check for page_id in SERVER REQUEST |
|
1025 | - if ($this->request->requestParamIsSet('page_id')) { |
|
1026 | - // rebuild $e_reg without any of the extra parameters |
|
1027 | - $page_id .= 'page_id=' . $this->request->getRequestParam('page_id', 0, 'int') . '&'; |
|
1028 | - } |
|
1029 | - // check for $e_reg in SERVER REQUEST |
|
1030 | - if ($this->request->requestParamIsSet('ee')) { |
|
1031 | - // rebuild $e_reg without any of the extra parameters |
|
1032 | - $e_reg = 'ee=' . $this->request->getRequestParam('ee'); |
|
1033 | - } |
|
1034 | - $page_visit = esc_url(rtrim($http_host . $request_uri . $page_id . $e_reg, '?')); |
|
1035 | - } |
|
1036 | - return $page_visit !== home_url('/wp-admin/admin-ajax.php') ? $page_visit : ''; |
|
1037 | - } |
|
1038 | - |
|
1039 | - |
|
1040 | - /** |
|
1041 | - * @the current wp user id |
|
1042 | - * @return int |
|
1043 | - */ |
|
1044 | - public function _wp_user_id() |
|
1045 | - { |
|
1046 | - // if I need to explain the following lines of code, then you shouldn't be looking at this! |
|
1047 | - $this->_wp_user_id = get_current_user_id(); |
|
1048 | - return $this->_wp_user_id; |
|
1049 | - } |
|
1050 | - |
|
1051 | - |
|
1052 | - /** |
|
1053 | - * Clear EE_Session data |
|
1054 | - * |
|
1055 | - * @param string $class |
|
1056 | - * @param string $function |
|
1057 | - * @return void |
|
1058 | - * @throws EE_Error |
|
1059 | - * @throws InvalidArgumentException |
|
1060 | - * @throws InvalidDataTypeException |
|
1061 | - * @throws InvalidInterfaceException |
|
1062 | - * @throws ReflectionException |
|
1063 | - */ |
|
1064 | - public function clear_session($class = '', $function = '') |
|
1065 | - { |
|
27 | + const session_id_prefix = 'ee_ssn_'; |
|
28 | + |
|
29 | + const hash_check_prefix = 'ee_shc_'; |
|
30 | + |
|
31 | + const OPTION_NAME_SETTINGS = 'ee_session_settings'; |
|
32 | + |
|
33 | + const STATUS_CLOSED = 0; |
|
34 | + |
|
35 | + const STATUS_OPEN = 1; |
|
36 | + |
|
37 | + const SAVE_STATE_CLEAN = 'clean'; |
|
38 | + const SAVE_STATE_DIRTY = 'dirty'; |
|
39 | + |
|
40 | + |
|
41 | + /** |
|
42 | + * instance of the EE_Session object |
|
43 | + * |
|
44 | + * @var EE_Session |
|
45 | + */ |
|
46 | + private static $_instance; |
|
47 | + |
|
48 | + /** |
|
49 | + * @var CacheStorageInterface $cache_storage |
|
50 | + */ |
|
51 | + protected $cache_storage; |
|
52 | + |
|
53 | + /** |
|
54 | + * @var EE_Encryption $encryption |
|
55 | + */ |
|
56 | + protected $encryption; |
|
57 | + |
|
58 | + /** |
|
59 | + * @var SessionStartHandler $session_start_handler |
|
60 | + */ |
|
61 | + protected $session_start_handler; |
|
62 | + |
|
63 | + /** |
|
64 | + * the session id |
|
65 | + * |
|
66 | + * @var string |
|
67 | + */ |
|
68 | + private $_sid; |
|
69 | + |
|
70 | + /** |
|
71 | + * session id salt |
|
72 | + * |
|
73 | + * @var string |
|
74 | + */ |
|
75 | + private $_sid_salt; |
|
76 | + |
|
77 | + /** |
|
78 | + * session data |
|
79 | + * |
|
80 | + * @var array |
|
81 | + */ |
|
82 | + private $_session_data = array(); |
|
83 | + |
|
84 | + /** |
|
85 | + * how long an EE session lasts |
|
86 | + * default session lifespan of 1 hour (for not so instant IPNs) |
|
87 | + * |
|
88 | + * @var SessionLifespan $session_lifespan |
|
89 | + */ |
|
90 | + private $session_lifespan; |
|
91 | + |
|
92 | + /** |
|
93 | + * session expiration time as Unix timestamp in GMT |
|
94 | + * |
|
95 | + * @var int |
|
96 | + */ |
|
97 | + private $_expiration; |
|
98 | + |
|
99 | + /** |
|
100 | + * whether or not session has expired at some point |
|
101 | + * |
|
102 | + * @var boolean |
|
103 | + */ |
|
104 | + private $_expired = false; |
|
105 | + |
|
106 | + /** |
|
107 | + * current time as Unix timestamp in GMT |
|
108 | + * |
|
109 | + * @var int |
|
110 | + */ |
|
111 | + private $_time; |
|
112 | + |
|
113 | + /** |
|
114 | + * whether to encrypt session data |
|
115 | + * |
|
116 | + * @var bool |
|
117 | + */ |
|
118 | + private $_use_encryption; |
|
119 | + |
|
120 | + /** |
|
121 | + * well... according to the server... |
|
122 | + * |
|
123 | + * @var null |
|
124 | + */ |
|
125 | + private $_user_agent; |
|
126 | + |
|
127 | + /** |
|
128 | + * do you really trust the server ? |
|
129 | + * |
|
130 | + * @var null |
|
131 | + */ |
|
132 | + private $_ip_address; |
|
133 | + |
|
134 | + /** |
|
135 | + * current WP user_id |
|
136 | + * |
|
137 | + * @var null |
|
138 | + */ |
|
139 | + private $_wp_user_id; |
|
140 | + |
|
141 | + /** |
|
142 | + * array for defining default session vars |
|
143 | + * |
|
144 | + * @var array |
|
145 | + */ |
|
146 | + private $_default_session_vars = array( |
|
147 | + 'id' => null, |
|
148 | + 'user_id' => null, |
|
149 | + 'ip_address' => null, |
|
150 | + 'user_agent' => null, |
|
151 | + 'init_access' => null, |
|
152 | + 'last_access' => null, |
|
153 | + 'expiration' => null, |
|
154 | + 'pages_visited' => array(), |
|
155 | + ); |
|
156 | + |
|
157 | + /** |
|
158 | + * timestamp for when last garbage collection cycle was performed |
|
159 | + * |
|
160 | + * @var int $_last_gc |
|
161 | + */ |
|
162 | + private $_last_gc; |
|
163 | + |
|
164 | + /** |
|
165 | + * @var RequestInterface $request |
|
166 | + */ |
|
167 | + protected $request; |
|
168 | + |
|
169 | + /** |
|
170 | + * whether session is active or not |
|
171 | + * |
|
172 | + * @var int $status |
|
173 | + */ |
|
174 | + private $status = EE_Session::STATUS_CLOSED; |
|
175 | + |
|
176 | + /** |
|
177 | + * whether session data has changed therefore requiring a session save |
|
178 | + * |
|
179 | + * @var string $save_state |
|
180 | + */ |
|
181 | + private $save_state = EE_Session::SAVE_STATE_CLEAN; |
|
182 | + |
|
183 | + |
|
184 | + /** |
|
185 | + * @singleton method used to instantiate class object |
|
186 | + * @param CacheStorageInterface $cache_storage |
|
187 | + * @param SessionLifespan|null $lifespan |
|
188 | + * @param RequestInterface $request |
|
189 | + * @param SessionStartHandler $session_start_handler |
|
190 | + * @param EE_Encryption $encryption |
|
191 | + * @return EE_Session |
|
192 | + * @throws InvalidArgumentException |
|
193 | + * @throws InvalidDataTypeException |
|
194 | + * @throws InvalidInterfaceException |
|
195 | + */ |
|
196 | + public static function instance( |
|
197 | + CacheStorageInterface $cache_storage = null, |
|
198 | + SessionLifespan $lifespan = null, |
|
199 | + RequestInterface $request = null, |
|
200 | + SessionStartHandler $session_start_handler = null, |
|
201 | + EE_Encryption $encryption = null |
|
202 | + ) { |
|
203 | + // check if class object is instantiated |
|
204 | + // session loading is turned ON by default, but prior to the init hook, can be turned back OFF via: |
|
205 | + // add_filter( 'FHEE_load_EE_Session', '__return_false' ); |
|
206 | + if ( |
|
207 | + ! self::$_instance instanceof EE_Session |
|
208 | + && $cache_storage instanceof CacheStorageInterface |
|
209 | + && $lifespan instanceof SessionLifespan |
|
210 | + && $request instanceof RequestInterface |
|
211 | + && $session_start_handler instanceof SessionStartHandler |
|
212 | + && apply_filters('FHEE_load_EE_Session', true) |
|
213 | + ) { |
|
214 | + self::$_instance = new self( |
|
215 | + $cache_storage, |
|
216 | + $lifespan, |
|
217 | + $request, |
|
218 | + $session_start_handler, |
|
219 | + $encryption |
|
220 | + ); |
|
221 | + } |
|
222 | + return self::$_instance; |
|
223 | + } |
|
224 | + |
|
225 | + |
|
226 | + /** |
|
227 | + * protected constructor to prevent direct creation |
|
228 | + * |
|
229 | + * @param CacheStorageInterface $cache_storage |
|
230 | + * @param SessionLifespan $lifespan |
|
231 | + * @param RequestInterface $request |
|
232 | + * @param SessionStartHandler $session_start_handler |
|
233 | + * @param EE_Encryption $encryption |
|
234 | + * @throws InvalidArgumentException |
|
235 | + * @throws InvalidDataTypeException |
|
236 | + * @throws InvalidInterfaceException |
|
237 | + */ |
|
238 | + protected function __construct( |
|
239 | + CacheStorageInterface $cache_storage, |
|
240 | + SessionLifespan $lifespan, |
|
241 | + RequestInterface $request, |
|
242 | + SessionStartHandler $session_start_handler, |
|
243 | + EE_Encryption $encryption = null |
|
244 | + ) { |
|
245 | + // session loading is turned ON by default, |
|
246 | + // but prior to the 'AHEE__EE_System__core_loaded_and_ready' hook |
|
247 | + // (which currently fires on the init hook at priority 9), |
|
248 | + // can be turned back OFF via: add_filter( 'FHEE_load_EE_Session', '__return_false' ); |
|
249 | + if (! apply_filters('FHEE_load_EE_Session', true)) { |
|
250 | + return; |
|
251 | + } |
|
252 | + $this->session_start_handler = $session_start_handler; |
|
253 | + $this->session_lifespan = $lifespan; |
|
254 | + $this->request = $request; |
|
255 | + if (! defined('ESPRESSO_SESSION')) { |
|
256 | + define('ESPRESSO_SESSION', true); |
|
257 | + } |
|
258 | + // retrieve session options from db |
|
259 | + $session_settings = (array) get_option(EE_Session::OPTION_NAME_SETTINGS, array()); |
|
260 | + if (! empty($session_settings)) { |
|
261 | + // cycle though existing session options |
|
262 | + foreach ($session_settings as $var_name => $session_setting) { |
|
263 | + // set values for class properties |
|
264 | + $var_name = '_' . $var_name; |
|
265 | + $this->{$var_name} = $session_setting; |
|
266 | + } |
|
267 | + } |
|
268 | + $this->cache_storage = $cache_storage; |
|
269 | + // are we using encryption? |
|
270 | + $this->_use_encryption = $encryption instanceof EE_Encryption |
|
271 | + && EE_Registry::instance()->CFG->admin->encode_session_data(); |
|
272 | + // encrypt data via: $this->encryption->encrypt(); |
|
273 | + $this->encryption = $encryption; |
|
274 | + // filter hook allows outside functions/classes/plugins to change default empty cart |
|
275 | + $extra_default_session_vars = apply_filters('FHEE__EE_Session__construct__extra_default_session_vars', array()); |
|
276 | + array_merge($this->_default_session_vars, $extra_default_session_vars); |
|
277 | + // apply default session vars |
|
278 | + $this->_set_defaults(); |
|
279 | + add_action('AHEE__EE_System__initialize', array($this, 'open_session')); |
|
280 | + // check request for 'clear_session' param |
|
281 | + add_action('AHEE__EE_Request_Handler__construct__complete', array($this, 'wp_loaded')); |
|
282 | + // once everything is all said and done, |
|
283 | + add_action('shutdown', array($this, 'update'), 100); |
|
284 | + add_action('shutdown', array($this, 'garbageCollection'), 1000); |
|
285 | + $this->configure_garbage_collection_filters(); |
|
286 | + } |
|
287 | + |
|
288 | + |
|
289 | + /** |
|
290 | + * @return bool |
|
291 | + * @throws InvalidArgumentException |
|
292 | + * @throws InvalidDataTypeException |
|
293 | + * @throws InvalidInterfaceException |
|
294 | + */ |
|
295 | + public static function isLoadedAndActive() |
|
296 | + { |
|
297 | + return did_action('AHEE__EE_System__core_loaded_and_ready') |
|
298 | + && EE_Session::instance() instanceof EE_Session |
|
299 | + && EE_Session::instance()->isActive(); |
|
300 | + } |
|
301 | + |
|
302 | + |
|
303 | + /** |
|
304 | + * @return bool |
|
305 | + */ |
|
306 | + public function isActive() |
|
307 | + { |
|
308 | + return $this->status === EE_Session::STATUS_OPEN; |
|
309 | + } |
|
310 | + |
|
311 | + |
|
312 | + /** |
|
313 | + * @return void |
|
314 | + * @throws EE_Error |
|
315 | + * @throws InvalidArgumentException |
|
316 | + * @throws InvalidDataTypeException |
|
317 | + * @throws InvalidInterfaceException |
|
318 | + * @throws InvalidSessionDataException |
|
319 | + * @throws RuntimeException |
|
320 | + * @throws ReflectionException |
|
321 | + */ |
|
322 | + public function open_session() |
|
323 | + { |
|
324 | + // check for existing session and retrieve it from db |
|
325 | + if (! $this->_espresso_session()) { |
|
326 | + // or just start a new one |
|
327 | + $this->_create_espresso_session(); |
|
328 | + } |
|
329 | + } |
|
330 | + |
|
331 | + |
|
332 | + /** |
|
333 | + * @return bool |
|
334 | + */ |
|
335 | + public function expired() |
|
336 | + { |
|
337 | + return $this->_expired; |
|
338 | + } |
|
339 | + |
|
340 | + |
|
341 | + /** |
|
342 | + * @return void |
|
343 | + */ |
|
344 | + public function reset_expired() |
|
345 | + { |
|
346 | + $this->_expired = false; |
|
347 | + } |
|
348 | + |
|
349 | + |
|
350 | + /** |
|
351 | + * @return int |
|
352 | + */ |
|
353 | + public function expiration() |
|
354 | + { |
|
355 | + return $this->_expiration; |
|
356 | + } |
|
357 | + |
|
358 | + |
|
359 | + /** |
|
360 | + * @return int |
|
361 | + */ |
|
362 | + public function extension() |
|
363 | + { |
|
364 | + return apply_filters('FHEE__EE_Session__extend_expiration__seconds_added', 10 * MINUTE_IN_SECONDS); |
|
365 | + } |
|
366 | + |
|
367 | + |
|
368 | + /** |
|
369 | + * @param int $time number of seconds to add to session expiration |
|
370 | + */ |
|
371 | + public function extend_expiration($time = 0) |
|
372 | + { |
|
373 | + $time = $time ? $time : $this->extension(); |
|
374 | + $this->_expiration += absint($time); |
|
375 | + } |
|
376 | + |
|
377 | + |
|
378 | + /** |
|
379 | + * @return int |
|
380 | + */ |
|
381 | + public function lifespan() |
|
382 | + { |
|
383 | + return $this->session_lifespan->inSeconds(); |
|
384 | + } |
|
385 | + |
|
386 | + |
|
387 | + /** |
|
388 | + * Marks whether the session data has been updated or not. |
|
389 | + * Valid options are: |
|
390 | + * EE_Session::SAVE_STATE_CLEAN - session data remains unchanged and updating is not necessary |
|
391 | + * EE_Session::SAVE_STATE_DIRTY - session data has changed since last save and needs to be updated |
|
392 | + * default value is EE_Session::SAVE_STATE_DIRTY |
|
393 | + * |
|
394 | + * @param string $save_state |
|
395 | + */ |
|
396 | + public function setSaveState($save_state = EE_Session::SAVE_STATE_DIRTY) |
|
397 | + { |
|
398 | + $valid_save_states = [ |
|
399 | + EE_Session::SAVE_STATE_CLEAN, |
|
400 | + EE_Session::SAVE_STATE_DIRTY, |
|
401 | + ]; |
|
402 | + if (! in_array($save_state, $valid_save_states, true)) { |
|
403 | + $save_state = EE_Session::SAVE_STATE_DIRTY; |
|
404 | + } |
|
405 | + $this->save_state = $save_state; |
|
406 | + } |
|
407 | + |
|
408 | + |
|
409 | + |
|
410 | + /** |
|
411 | + * This just sets some defaults for the _session data property |
|
412 | + * |
|
413 | + * @return void |
|
414 | + */ |
|
415 | + private function _set_defaults() |
|
416 | + { |
|
417 | + // set some defaults |
|
418 | + foreach ($this->_default_session_vars as $key => $default_var) { |
|
419 | + if (is_array($default_var)) { |
|
420 | + $this->_session_data[ $key ] = array(); |
|
421 | + } else { |
|
422 | + $this->_session_data[ $key ] = ''; |
|
423 | + } |
|
424 | + } |
|
425 | + } |
|
426 | + |
|
427 | + |
|
428 | + /** |
|
429 | + * @retrieve session data |
|
430 | + * @return string |
|
431 | + */ |
|
432 | + public function id() |
|
433 | + { |
|
434 | + return $this->_sid; |
|
435 | + } |
|
436 | + |
|
437 | + |
|
438 | + /** |
|
439 | + * @param \EE_Cart $cart |
|
440 | + * @return bool |
|
441 | + */ |
|
442 | + public function set_cart(EE_Cart $cart) |
|
443 | + { |
|
444 | + $this->_session_data['cart'] = $cart; |
|
445 | + $this->setSaveState(); |
|
446 | + return true; |
|
447 | + } |
|
448 | + |
|
449 | + |
|
450 | + /** |
|
451 | + * reset_cart |
|
452 | + */ |
|
453 | + public function reset_cart() |
|
454 | + { |
|
455 | + do_action('AHEE__EE_Session__reset_cart__before_reset', $this); |
|
456 | + $this->_session_data['cart'] = null; |
|
457 | + $this->setSaveState(); |
|
458 | + } |
|
459 | + |
|
460 | + |
|
461 | + /** |
|
462 | + * @return \EE_Cart |
|
463 | + */ |
|
464 | + public function cart() |
|
465 | + { |
|
466 | + return isset($this->_session_data['cart']) && $this->_session_data['cart'] instanceof EE_Cart |
|
467 | + ? $this->_session_data['cart'] |
|
468 | + : null; |
|
469 | + } |
|
470 | + |
|
471 | + |
|
472 | + /** |
|
473 | + * @param \EE_Checkout $checkout |
|
474 | + * @return bool |
|
475 | + */ |
|
476 | + public function set_checkout(EE_Checkout $checkout) |
|
477 | + { |
|
478 | + $this->_session_data['checkout'] = $checkout; |
|
479 | + $this->setSaveState(); |
|
480 | + return true; |
|
481 | + } |
|
482 | + |
|
483 | + |
|
484 | + /** |
|
485 | + * reset_checkout |
|
486 | + */ |
|
487 | + public function reset_checkout() |
|
488 | + { |
|
489 | + do_action('AHEE__EE_Session__reset_checkout__before_reset', $this); |
|
490 | + $this->_session_data['checkout'] = null; |
|
491 | + $this->setSaveState(); |
|
492 | + } |
|
493 | + |
|
494 | + |
|
495 | + /** |
|
496 | + * @return \EE_Checkout |
|
497 | + */ |
|
498 | + public function checkout() |
|
499 | + { |
|
500 | + return isset($this->_session_data['checkout']) && $this->_session_data['checkout'] instanceof EE_Checkout |
|
501 | + ? $this->_session_data['checkout'] |
|
502 | + : null; |
|
503 | + } |
|
504 | + |
|
505 | + |
|
506 | + /** |
|
507 | + * @param \EE_Transaction $transaction |
|
508 | + * @return bool |
|
509 | + * @throws EE_Error |
|
510 | + */ |
|
511 | + public function set_transaction(EE_Transaction $transaction) |
|
512 | + { |
|
513 | + // first remove the session from the transaction before we save the transaction in the session |
|
514 | + $transaction->set_txn_session_data(null); |
|
515 | + $this->_session_data['transaction'] = $transaction; |
|
516 | + $this->setSaveState(); |
|
517 | + return true; |
|
518 | + } |
|
519 | + |
|
520 | + |
|
521 | + /** |
|
522 | + * reset_transaction |
|
523 | + */ |
|
524 | + public function reset_transaction() |
|
525 | + { |
|
526 | + do_action('AHEE__EE_Session__reset_transaction__before_reset', $this); |
|
527 | + $this->_session_data['transaction'] = null; |
|
528 | + $this->setSaveState(); |
|
529 | + } |
|
530 | + |
|
531 | + |
|
532 | + /** |
|
533 | + * @return \EE_Transaction |
|
534 | + */ |
|
535 | + public function transaction() |
|
536 | + { |
|
537 | + return isset($this->_session_data['transaction']) |
|
538 | + && $this->_session_data['transaction'] instanceof EE_Transaction |
|
539 | + ? $this->_session_data['transaction'] |
|
540 | + : null; |
|
541 | + } |
|
542 | + |
|
543 | + |
|
544 | + /** |
|
545 | + * retrieve session data |
|
546 | + * |
|
547 | + * @param null $key |
|
548 | + * @param bool $reset_cache |
|
549 | + * @return array |
|
550 | + */ |
|
551 | + public function get_session_data($key = null, $reset_cache = false) |
|
552 | + { |
|
553 | + if ($reset_cache) { |
|
554 | + $this->reset_cart(); |
|
555 | + $this->reset_checkout(); |
|
556 | + $this->reset_transaction(); |
|
557 | + } |
|
558 | + if (! empty($key)) { |
|
559 | + return isset($this->_session_data[ $key ]) ? $this->_session_data[ $key ] : null; |
|
560 | + } |
|
561 | + return $this->_session_data; |
|
562 | + } |
|
563 | + |
|
564 | + |
|
565 | + /** |
|
566 | + * Returns TRUE on success, FALSE on fail |
|
567 | + * |
|
568 | + * @param array $data |
|
569 | + * @return bool |
|
570 | + */ |
|
571 | + public function set_session_data($data) |
|
572 | + { |
|
573 | + // nothing ??? bad data ??? go home! |
|
574 | + if (empty($data) || ! is_array($data)) { |
|
575 | + EE_Error::add_error( |
|
576 | + esc_html__( |
|
577 | + 'No session data or invalid session data was provided.', |
|
578 | + 'event_espresso' |
|
579 | + ), |
|
580 | + __FILE__, |
|
581 | + __FUNCTION__, |
|
582 | + __LINE__ |
|
583 | + ); |
|
584 | + return false; |
|
585 | + } |
|
586 | + foreach ($data as $key => $value) { |
|
587 | + if (isset($this->_default_session_vars[ $key ])) { |
|
588 | + EE_Error::add_error( |
|
589 | + sprintf( |
|
590 | + esc_html__( |
|
591 | + 'Sorry! %s is a default session datum and can not be reset.', |
|
592 | + 'event_espresso' |
|
593 | + ), |
|
594 | + $key |
|
595 | + ), |
|
596 | + __FILE__, |
|
597 | + __FUNCTION__, |
|
598 | + __LINE__ |
|
599 | + ); |
|
600 | + return false; |
|
601 | + } |
|
602 | + $this->_session_data[ $key ] = $value; |
|
603 | + $this->setSaveState(); |
|
604 | + } |
|
605 | + return true; |
|
606 | + } |
|
607 | + |
|
608 | + |
|
609 | + /** |
|
610 | + * @initiate session |
|
611 | + * @return bool TRUE on success, FALSE on fail |
|
612 | + * @throws EE_Error |
|
613 | + * @throws InvalidArgumentException |
|
614 | + * @throws InvalidDataTypeException |
|
615 | + * @throws InvalidInterfaceException |
|
616 | + * @throws InvalidSessionDataException |
|
617 | + * @throws RuntimeException |
|
618 | + * @throws ReflectionException |
|
619 | + */ |
|
620 | + private function _espresso_session() |
|
621 | + { |
|
622 | + do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
623 | + $this->session_start_handler->startSession(); |
|
624 | + $this->status = EE_Session::STATUS_OPEN; |
|
625 | + // get our modified session ID |
|
626 | + $this->_sid = $this->_generate_session_id(); |
|
627 | + // and the visitors IP |
|
628 | + $this->_ip_address = $this->request->ipAddress(); |
|
629 | + // set the "user agent" |
|
630 | + $this->_user_agent = $this->request->userAgent(); |
|
631 | + // now let's retrieve what's in the db |
|
632 | + $session_data = $this->_retrieve_session_data(); |
|
633 | + if (! empty($session_data)) { |
|
634 | + // get the current time in UTC |
|
635 | + $this->_time = $this->_time !== null ? $this->_time : time(); |
|
636 | + // and reset the session expiration |
|
637 | + $this->_expiration = isset($session_data['expiration']) |
|
638 | + ? $session_data['expiration'] |
|
639 | + : $this->_time + $this->session_lifespan->inSeconds(); |
|
640 | + } else { |
|
641 | + // set initial site access time and the session expiration |
|
642 | + $this->_set_init_access_and_expiration(); |
|
643 | + // set referer |
|
644 | + $this->_session_data['pages_visited'][ $this->_session_data['init_access'] ] = esc_attr( |
|
645 | + $this->request->getServerParam('HTTP_REFERER') |
|
646 | + ); |
|
647 | + // no previous session = go back and create one (on top of the data above) |
|
648 | + return false; |
|
649 | + } |
|
650 | + // now the user agent |
|
651 | + if ($session_data['user_agent'] !== $this->_user_agent) { |
|
652 | + return false; |
|
653 | + } |
|
654 | + // wait a minute... how old are you? |
|
655 | + if ($this->_time > $this->_expiration) { |
|
656 | + // yer too old fer me! |
|
657 | + $this->_expired = true; |
|
658 | + // wipe out everything that isn't a default session datum |
|
659 | + $this->clear_session(__CLASS__, __FUNCTION__); |
|
660 | + } |
|
661 | + // make event espresso session data available to plugin |
|
662 | + $this->_session_data = array_merge($this->_session_data, $session_data); |
|
663 | + return true; |
|
664 | + } |
|
665 | + |
|
666 | + |
|
667 | + /** |
|
668 | + * _get_session_data |
|
669 | + * Retrieves the session data, and attempts to correct any encoding issues that can occur due to improperly setup |
|
670 | + * databases |
|
671 | + * |
|
672 | + * @return array |
|
673 | + * @throws EE_Error |
|
674 | + * @throws InvalidArgumentException |
|
675 | + * @throws InvalidSessionDataException |
|
676 | + * @throws InvalidDataTypeException |
|
677 | + * @throws InvalidInterfaceException |
|
678 | + * @throws RuntimeException |
|
679 | + */ |
|
680 | + protected function _retrieve_session_data() |
|
681 | + { |
|
682 | + $ssn_key = EE_Session::session_id_prefix . $this->_sid; |
|
683 | + try { |
|
684 | + // we're using WP's Transient API to store session data using the PHP session ID as the option name |
|
685 | + $session_data = $this->cache_storage->get($ssn_key, false); |
|
686 | + if (empty($session_data)) { |
|
687 | + return array(); |
|
688 | + } |
|
689 | + if (apply_filters('FHEE__EE_Session___perform_session_id_hash_check', WP_DEBUG)) { |
|
690 | + $hash_check = $this->cache_storage->get( |
|
691 | + EE_Session::hash_check_prefix . $this->_sid, |
|
692 | + false |
|
693 | + ); |
|
694 | + if ($hash_check && $hash_check !== md5($session_data)) { |
|
695 | + EE_Error::add_error( |
|
696 | + sprintf( |
|
697 | + esc_html__( |
|
698 | + 'The stored data for session %1$s failed to pass a hash check and therefore appears to be invalid.', |
|
699 | + 'event_espresso' |
|
700 | + ), |
|
701 | + EE_Session::session_id_prefix . $this->_sid |
|
702 | + ), |
|
703 | + __FILE__, |
|
704 | + __FUNCTION__, |
|
705 | + __LINE__ |
|
706 | + ); |
|
707 | + } |
|
708 | + } |
|
709 | + } catch (Exception $e) { |
|
710 | + // let's just eat that error for now and attempt to correct any corrupted data |
|
711 | + global $wpdb; |
|
712 | + $row = $wpdb->get_row( |
|
713 | + $wpdb->prepare( |
|
714 | + "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s LIMIT 1", |
|
715 | + '_transient_' . $ssn_key |
|
716 | + ) |
|
717 | + ); |
|
718 | + $session_data = is_object($row) ? $row->option_value : null; |
|
719 | + if ($session_data) { |
|
720 | + $session_data = preg_replace_callback( |
|
721 | + '!s:(d+):"(.*?)";!', |
|
722 | + function ($match) { |
|
723 | + return $match[1] === strlen($match[2]) |
|
724 | + ? $match[0] |
|
725 | + : 's:' . strlen($match[2]) . ':"' . $match[2] . '";'; |
|
726 | + }, |
|
727 | + $session_data |
|
728 | + ); |
|
729 | + } |
|
730 | + $session_data = maybe_unserialize($session_data); |
|
731 | + } |
|
732 | + // in case the data is encoded... try to decode it |
|
733 | + $session_data = $this->encryption instanceof EE_Encryption |
|
734 | + ? $this->encryption->base64_string_decode($session_data) |
|
735 | + : $session_data; |
|
736 | + if (! is_array($session_data)) { |
|
737 | + try { |
|
738 | + $session_data = maybe_unserialize($session_data); |
|
739 | + } catch (Exception $e) { |
|
740 | + $msg = esc_html__( |
|
741 | + 'An error occurred while attempting to unserialize the session data.', |
|
742 | + 'event_espresso' |
|
743 | + ); |
|
744 | + $msg .= WP_DEBUG |
|
745 | + ? '<br><pre>' |
|
746 | + . print_r($session_data, true) |
|
747 | + . '</pre><br>' |
|
748 | + . $this->find_serialize_error($session_data) |
|
749 | + : ''; |
|
750 | + $this->cache_storage->delete(EE_Session::session_id_prefix . $this->_sid); |
|
751 | + throw new InvalidSessionDataException($msg, 0, $e); |
|
752 | + } |
|
753 | + } |
|
754 | + // just a check to make sure the session array is indeed an array |
|
755 | + if (! is_array($session_data)) { |
|
756 | + // no?!?! then something is wrong |
|
757 | + $msg = esc_html__( |
|
758 | + 'The session data is missing, invalid, or corrupted.', |
|
759 | + 'event_espresso' |
|
760 | + ); |
|
761 | + $msg .= WP_DEBUG |
|
762 | + ? '<br><pre>' . print_r($session_data, true) . '</pre><br>' . $this->find_serialize_error($session_data) |
|
763 | + : ''; |
|
764 | + $this->cache_storage->delete(EE_Session::session_id_prefix . $this->_sid); |
|
765 | + throw new InvalidSessionDataException($msg); |
|
766 | + } |
|
767 | + if (isset($session_data['transaction']) && absint($session_data['transaction']) !== 0) { |
|
768 | + $session_data['transaction'] = EEM_Transaction::instance()->get_one_by_ID( |
|
769 | + $session_data['transaction'] |
|
770 | + ); |
|
771 | + } |
|
772 | + return $session_data; |
|
773 | + } |
|
774 | + |
|
775 | + |
|
776 | + /** |
|
777 | + * _generate_session_id |
|
778 | + * Retrieves the PHP session id either directly from the PHP session, |
|
779 | + * or from the request array if it was passed in from an AJAX request. |
|
780 | + * The session id is then salted and hashed (mmm sounds tasty) |
|
781 | + * so that it can be safely used as a request param |
|
782 | + * |
|
783 | + * @return string |
|
784 | + */ |
|
785 | + protected function _generate_session_id() |
|
786 | + { |
|
787 | + // check if the SID was passed explicitly, otherwise get from session, then add salt and hash it to reduce length |
|
788 | + $session_id = $this->request->requestParamIsSet('EESID') |
|
789 | + ? $this->request->getRequestParam('EESID') |
|
790 | + : md5(session_id() . get_current_blog_id() . $this->_get_sid_salt()); |
|
791 | + return apply_filters('FHEE__EE_Session___generate_session_id__session_id', $session_id); |
|
792 | + } |
|
793 | + |
|
794 | + |
|
795 | + /** |
|
796 | + * _get_sid_salt |
|
797 | + * |
|
798 | + * @return string |
|
799 | + */ |
|
800 | + protected function _get_sid_salt() |
|
801 | + { |
|
802 | + // was session id salt already saved to db ? |
|
803 | + if (empty($this->_sid_salt)) { |
|
804 | + // no? then maybe use WP defined constant |
|
805 | + if (defined('AUTH_SALT')) { |
|
806 | + $this->_sid_salt = AUTH_SALT; |
|
807 | + } |
|
808 | + // if salt doesn't exist or is too short |
|
809 | + if (strlen($this->_sid_salt) < 32) { |
|
810 | + // create a new one |
|
811 | + $this->_sid_salt = wp_generate_password(64); |
|
812 | + } |
|
813 | + // and save it as a permanent session setting |
|
814 | + $this->updateSessionSettings(array('sid_salt' => $this->_sid_salt)); |
|
815 | + } |
|
816 | + return $this->_sid_salt; |
|
817 | + } |
|
818 | + |
|
819 | + |
|
820 | + /** |
|
821 | + * _set_init_access_and_expiration |
|
822 | + * |
|
823 | + * @return void |
|
824 | + */ |
|
825 | + protected function _set_init_access_and_expiration() |
|
826 | + { |
|
827 | + $this->_time = time(); |
|
828 | + $this->_expiration = $this->_time + $this->session_lifespan->inSeconds(); |
|
829 | + // set initial site access time |
|
830 | + $this->_session_data['init_access'] = $this->_time; |
|
831 | + // and the session expiration |
|
832 | + $this->_session_data['expiration'] = $this->_expiration; |
|
833 | + } |
|
834 | + |
|
835 | + |
|
836 | + /** |
|
837 | + * @update session data prior to saving to the db |
|
838 | + * @param bool $new_session |
|
839 | + * @return bool TRUE on success, FALSE on fail |
|
840 | + * @throws EE_Error |
|
841 | + * @throws InvalidArgumentException |
|
842 | + * @throws InvalidDataTypeException |
|
843 | + * @throws InvalidInterfaceException |
|
844 | + * @throws ReflectionException |
|
845 | + */ |
|
846 | + public function update($new_session = false) |
|
847 | + { |
|
848 | + $this->_session_data = is_array($this->_session_data) && isset($this->_session_data['id']) |
|
849 | + ? $this->_session_data |
|
850 | + : array(); |
|
851 | + if (empty($this->_session_data)) { |
|
852 | + $this->_set_defaults(); |
|
853 | + } |
|
854 | + $session_data = array(); |
|
855 | + foreach ($this->_session_data as $key => $value) { |
|
856 | + switch ($key) { |
|
857 | + case 'id': |
|
858 | + // session ID |
|
859 | + $session_data['id'] = $this->_sid; |
|
860 | + break; |
|
861 | + case 'ip_address': |
|
862 | + // visitor ip address |
|
863 | + $session_data['ip_address'] = $this->request->ipAddress(); |
|
864 | + break; |
|
865 | + case 'user_agent': |
|
866 | + // visitor user_agent |
|
867 | + $session_data['user_agent'] = $this->_user_agent; |
|
868 | + break; |
|
869 | + case 'init_access': |
|
870 | + $session_data['init_access'] = absint($value); |
|
871 | + break; |
|
872 | + case 'last_access': |
|
873 | + // current access time |
|
874 | + $session_data['last_access'] = $this->_time; |
|
875 | + break; |
|
876 | + case 'expiration': |
|
877 | + // when the session expires |
|
878 | + $session_data['expiration'] = ! empty($this->_expiration) |
|
879 | + ? $this->_expiration |
|
880 | + : $session_data['init_access'] + $this->session_lifespan->inSeconds(); |
|
881 | + break; |
|
882 | + case 'user_id': |
|
883 | + // current user if logged in |
|
884 | + $session_data['user_id'] = $this->_wp_user_id(); |
|
885 | + break; |
|
886 | + case 'pages_visited': |
|
887 | + $page_visit = $this->_get_page_visit(); |
|
888 | + if ($page_visit) { |
|
889 | + // set pages visited where the first will be the http referrer |
|
890 | + $this->_session_data['pages_visited'][ $this->_time ] = $page_visit; |
|
891 | + // we'll only save the last 10 page visits. |
|
892 | + $session_data['pages_visited'] = array_slice($this->_session_data['pages_visited'], -10); |
|
893 | + } |
|
894 | + break; |
|
895 | + default: |
|
896 | + // carry any other data over |
|
897 | + $session_data[ $key ] = $this->_session_data[ $key ]; |
|
898 | + } |
|
899 | + } |
|
900 | + $this->_session_data = $session_data; |
|
901 | + // creating a new session does not require saving to the db just yet |
|
902 | + if (! $new_session) { |
|
903 | + // ready? let's save |
|
904 | + if ($this->_save_session_to_db()) { |
|
905 | + return true; |
|
906 | + } |
|
907 | + return false; |
|
908 | + } |
|
909 | + // meh, why not? |
|
910 | + return true; |
|
911 | + } |
|
912 | + |
|
913 | + |
|
914 | + /** |
|
915 | + * @create session data array |
|
916 | + * @throws EE_Error |
|
917 | + * @throws InvalidArgumentException |
|
918 | + * @throws InvalidDataTypeException |
|
919 | + * @throws InvalidInterfaceException |
|
920 | + * @throws ReflectionException |
|
921 | + */ |
|
922 | + private function _create_espresso_session() |
|
923 | + { |
|
924 | + do_action('AHEE_log', __CLASS__, __FUNCTION__, ''); |
|
925 | + // use the update function for now with $new_session arg set to TRUE |
|
926 | + $this->update(true); |
|
927 | + } |
|
928 | + |
|
929 | + /** |
|
930 | + * Detects if there is anything worth saving in the session (eg the cart is a good one, notices are pretty good |
|
931 | + * too). This is used when determining if we want to save the session or not. |
|
932 | + * @since 4.9.67.p |
|
933 | + * @return bool |
|
934 | + */ |
|
935 | + private function sessionHasStuffWorthSaving() |
|
936 | + { |
|
937 | + return $this->save_state === EE_Session::SAVE_STATE_DIRTY |
|
938 | + // we may want to eventually remove the following |
|
939 | + // on the assumption that the above check is enough |
|
940 | + || $this->cart() instanceof EE_Cart |
|
941 | + || ( |
|
942 | + isset($this->_session_data['ee_notices']) |
|
943 | + && ( |
|
944 | + ! empty($this->_session_data['ee_notices']['attention']) |
|
945 | + || ! empty($this->_session_data['ee_notices']['errors']) |
|
946 | + || ! empty($this->_session_data['ee_notices']['success']) |
|
947 | + ) |
|
948 | + ); |
|
949 | + } |
|
950 | + |
|
951 | + |
|
952 | + /** |
|
953 | + * _save_session_to_db |
|
954 | + * |
|
955 | + * @param bool $clear_session |
|
956 | + * @return bool |
|
957 | + * @throws EE_Error |
|
958 | + * @throws InvalidArgumentException |
|
959 | + * @throws InvalidDataTypeException |
|
960 | + * @throws InvalidInterfaceException |
|
961 | + * @throws ReflectionException |
|
962 | + */ |
|
963 | + private function _save_session_to_db($clear_session = false) |
|
964 | + { |
|
965 | + // don't save sessions for crawlers |
|
966 | + // and unless we're deleting the session data, don't save anything if there isn't a cart |
|
967 | + if ( |
|
968 | + $this->request->isBot() |
|
969 | + || ( |
|
970 | + ! $clear_session |
|
971 | + && ! $this->sessionHasStuffWorthSaving() |
|
972 | + && apply_filters('FHEE__EE_Session___save_session_to_db__abort_session_save', true) |
|
973 | + ) |
|
974 | + ) { |
|
975 | + return false; |
|
976 | + } |
|
977 | + $transaction = $this->transaction(); |
|
978 | + if ($transaction instanceof EE_Transaction) { |
|
979 | + if (! $transaction->ID()) { |
|
980 | + $transaction->save(); |
|
981 | + } |
|
982 | + $this->_session_data['transaction'] = $transaction->ID(); |
|
983 | + } |
|
984 | + // then serialize all of our session data |
|
985 | + $session_data = serialize($this->_session_data); |
|
986 | + // do we need to also encode it to avoid corrupted data when saved to the db? |
|
987 | + $session_data = $this->_use_encryption |
|
988 | + ? $this->encryption->base64_string_encode($session_data) |
|
989 | + : $session_data; |
|
990 | + // maybe save hash check |
|
991 | + if (apply_filters('FHEE__EE_Session___perform_session_id_hash_check', WP_DEBUG)) { |
|
992 | + $this->cache_storage->add( |
|
993 | + EE_Session::hash_check_prefix . $this->_sid, |
|
994 | + md5($session_data), |
|
995 | + $this->session_lifespan->inSeconds() |
|
996 | + ); |
|
997 | + } |
|
998 | + // we're using the Transient API for storing session data, |
|
999 | + $saved = $this->cache_storage->add( |
|
1000 | + EE_Session::session_id_prefix . $this->_sid, |
|
1001 | + $session_data, |
|
1002 | + $this->session_lifespan->inSeconds() |
|
1003 | + ); |
|
1004 | + $this->setSaveState(EE_Session::SAVE_STATE_CLEAN); |
|
1005 | + return $saved; |
|
1006 | + } |
|
1007 | + |
|
1008 | + |
|
1009 | + /** |
|
1010 | + * @get the full page request the visitor is accessing |
|
1011 | + * @return string |
|
1012 | + */ |
|
1013 | + public function _get_page_visit() |
|
1014 | + { |
|
1015 | + $page_visit = home_url('/') . 'wp-admin/admin-ajax.php'; |
|
1016 | + // check for request url |
|
1017 | + if ($this->request->serverParamIsSet('REQUEST_URI')) { |
|
1018 | + $page_id = '?'; |
|
1019 | + $e_reg = ''; |
|
1020 | + $request_uri = $this->request->getServerParam('REQUEST_URI'); |
|
1021 | + $ru_bits = explode('?', $request_uri); |
|
1022 | + $request_uri = $ru_bits[0]; |
|
1023 | + $http_host = $this->request->getServerParam('HTTP_HOST'); |
|
1024 | + // check for page_id in SERVER REQUEST |
|
1025 | + if ($this->request->requestParamIsSet('page_id')) { |
|
1026 | + // rebuild $e_reg without any of the extra parameters |
|
1027 | + $page_id .= 'page_id=' . $this->request->getRequestParam('page_id', 0, 'int') . '&'; |
|
1028 | + } |
|
1029 | + // check for $e_reg in SERVER REQUEST |
|
1030 | + if ($this->request->requestParamIsSet('ee')) { |
|
1031 | + // rebuild $e_reg without any of the extra parameters |
|
1032 | + $e_reg = 'ee=' . $this->request->getRequestParam('ee'); |
|
1033 | + } |
|
1034 | + $page_visit = esc_url(rtrim($http_host . $request_uri . $page_id . $e_reg, '?')); |
|
1035 | + } |
|
1036 | + return $page_visit !== home_url('/wp-admin/admin-ajax.php') ? $page_visit : ''; |
|
1037 | + } |
|
1038 | + |
|
1039 | + |
|
1040 | + /** |
|
1041 | + * @the current wp user id |
|
1042 | + * @return int |
|
1043 | + */ |
|
1044 | + public function _wp_user_id() |
|
1045 | + { |
|
1046 | + // if I need to explain the following lines of code, then you shouldn't be looking at this! |
|
1047 | + $this->_wp_user_id = get_current_user_id(); |
|
1048 | + return $this->_wp_user_id; |
|
1049 | + } |
|
1050 | + |
|
1051 | + |
|
1052 | + /** |
|
1053 | + * Clear EE_Session data |
|
1054 | + * |
|
1055 | + * @param string $class |
|
1056 | + * @param string $function |
|
1057 | + * @return void |
|
1058 | + * @throws EE_Error |
|
1059 | + * @throws InvalidArgumentException |
|
1060 | + * @throws InvalidDataTypeException |
|
1061 | + * @throws InvalidInterfaceException |
|
1062 | + * @throws ReflectionException |
|
1063 | + */ |
|
1064 | + public function clear_session($class = '', $function = '') |
|
1065 | + { |
|
1066 | 1066 | // echo ' |
1067 | 1067 | // <h3 style="color:#999;line-height:.9em;"> |
1068 | 1068 | // <span style="color:#2EA2CC">' . __CLASS__ . '</span>::<span style="color:#E76700">' . __FUNCTION__ . '( ' . $class . '::' . $function . '() )</span><br/> |
1069 | 1069 | // <span style="font-size:9px;font-weight:normal;">' . __FILE__ . '</span> <b style="font-size:10px;"> ' . __LINE__ . ' </b> |
1070 | 1070 | // </h3>'; |
1071 | - do_action('AHEE_log', __FILE__, __FUNCTION__, 'session cleared by : ' . $class . '::' . $function . '()'); |
|
1072 | - $this->reset_cart(); |
|
1073 | - $this->reset_checkout(); |
|
1074 | - $this->reset_transaction(); |
|
1075 | - // wipe out everything that isn't a default session datum |
|
1076 | - $this->reset_data(array_keys($this->_session_data)); |
|
1077 | - // reset initial site access time and the session expiration |
|
1078 | - $this->_set_init_access_and_expiration(); |
|
1079 | - $this->setSaveState(); |
|
1080 | - $this->_save_session_to_db(true); |
|
1081 | - } |
|
1082 | - |
|
1083 | - |
|
1084 | - /** |
|
1085 | - * resets all non-default session vars. Returns TRUE on success, FALSE on fail |
|
1086 | - * |
|
1087 | - * @param array|mixed $data_to_reset |
|
1088 | - * @param bool $show_all_notices |
|
1089 | - * @return bool |
|
1090 | - */ |
|
1091 | - public function reset_data($data_to_reset = array(), $show_all_notices = false) |
|
1092 | - { |
|
1093 | - // if $data_to_reset is not in an array, then put it in one |
|
1094 | - if (! is_array($data_to_reset)) { |
|
1095 | - $data_to_reset = array($data_to_reset); |
|
1096 | - } |
|
1097 | - // nothing ??? go home! |
|
1098 | - if (empty($data_to_reset)) { |
|
1099 | - EE_Error::add_error( |
|
1100 | - esc_html__( |
|
1101 | - 'No session data could be reset, because no session var name was provided.', |
|
1102 | - 'event_espresso' |
|
1103 | - ), |
|
1104 | - __FILE__, |
|
1105 | - __FUNCTION__, |
|
1106 | - __LINE__ |
|
1107 | - ); |
|
1108 | - return false; |
|
1109 | - } |
|
1110 | - $return_value = true; |
|
1111 | - // since $data_to_reset is an array, cycle through the values |
|
1112 | - foreach ($data_to_reset as $reset) { |
|
1113 | - // first check to make sure it is a valid session var |
|
1114 | - if (isset($this->_session_data[ $reset ])) { |
|
1115 | - // then check to make sure it is not a default var |
|
1116 | - if (! array_key_exists($reset, $this->_default_session_vars)) { |
|
1117 | - // remove session var |
|
1118 | - unset($this->_session_data[ $reset ]); |
|
1119 | - $this->setSaveState(); |
|
1120 | - if ($show_all_notices) { |
|
1121 | - EE_Error::add_success( |
|
1122 | - sprintf( |
|
1123 | - esc_html__('The session variable %s was removed.', 'event_espresso'), |
|
1124 | - $reset |
|
1125 | - ), |
|
1126 | - __FILE__, |
|
1127 | - __FUNCTION__, |
|
1128 | - __LINE__ |
|
1129 | - ); |
|
1130 | - } |
|
1131 | - } else { |
|
1132 | - // yeeeeeeeeerrrrrrrrrrr OUT !!!! |
|
1133 | - if ($show_all_notices) { |
|
1134 | - EE_Error::add_error( |
|
1135 | - sprintf( |
|
1136 | - esc_html__( |
|
1137 | - 'Sorry! %s is a default session datum and can not be reset.', |
|
1138 | - 'event_espresso' |
|
1139 | - ), |
|
1140 | - $reset |
|
1141 | - ), |
|
1142 | - __FILE__, |
|
1143 | - __FUNCTION__, |
|
1144 | - __LINE__ |
|
1145 | - ); |
|
1146 | - } |
|
1147 | - $return_value = false; |
|
1148 | - } |
|
1149 | - } elseif ($show_all_notices) { |
|
1150 | - // oops! that session var does not exist! |
|
1151 | - EE_Error::add_error( |
|
1152 | - sprintf( |
|
1153 | - esc_html__( |
|
1154 | - 'The session item provided, %s, is invalid or does not exist.', |
|
1155 | - 'event_espresso' |
|
1156 | - ), |
|
1157 | - $reset |
|
1158 | - ), |
|
1159 | - __FILE__, |
|
1160 | - __FUNCTION__, |
|
1161 | - __LINE__ |
|
1162 | - ); |
|
1163 | - $return_value = false; |
|
1164 | - } |
|
1165 | - } // end of foreach |
|
1166 | - return $return_value; |
|
1167 | - } |
|
1168 | - |
|
1169 | - |
|
1170 | - /** |
|
1171 | - * wp_loaded |
|
1172 | - * |
|
1173 | - * @throws EE_Error |
|
1174 | - * @throws InvalidDataTypeException |
|
1175 | - * @throws InvalidInterfaceException |
|
1176 | - * @throws InvalidArgumentException |
|
1177 | - * @throws ReflectionException |
|
1178 | - */ |
|
1179 | - public function wp_loaded() |
|
1180 | - { |
|
1181 | - if ($this->request->requestParamIsSet('clear_session')) { |
|
1182 | - $this->clear_session(__CLASS__, __FUNCTION__); |
|
1183 | - } |
|
1184 | - } |
|
1185 | - |
|
1186 | - |
|
1187 | - /** |
|
1188 | - * Used to reset the entire object (for tests). |
|
1189 | - * |
|
1190 | - * @since 4.3.0 |
|
1191 | - * @throws EE_Error |
|
1192 | - * @throws InvalidDataTypeException |
|
1193 | - * @throws InvalidInterfaceException |
|
1194 | - * @throws InvalidArgumentException |
|
1195 | - * @throws ReflectionException |
|
1196 | - */ |
|
1197 | - public static function reset() |
|
1198 | - { |
|
1199 | - $session = EE_Session::instance(); |
|
1200 | - if ($session instanceof EE_Session) { |
|
1201 | - $session->clear_session(); |
|
1202 | - } |
|
1203 | - return $session; |
|
1204 | - } |
|
1205 | - |
|
1206 | - |
|
1207 | - /** |
|
1208 | - * Used to reset the entire object (for tests). |
|
1209 | - * |
|
1210 | - * @since 4.3.0 |
|
1211 | - * @throws EE_Error |
|
1212 | - * @throws InvalidDataTypeException |
|
1213 | - * @throws InvalidInterfaceException |
|
1214 | - * @throws InvalidArgumentException |
|
1215 | - * @throws ReflectionException |
|
1216 | - */ |
|
1217 | - public function reset_instance() |
|
1218 | - { |
|
1219 | - $this->clear_session(); |
|
1220 | - self::$_instance = null; |
|
1221 | - } |
|
1222 | - |
|
1223 | - |
|
1224 | - public function configure_garbage_collection_filters() |
|
1225 | - { |
|
1226 | - // run old filter we had for controlling session cleanup |
|
1227 | - $expired_session_transient_delete_query_limit = absint( |
|
1228 | - apply_filters( |
|
1229 | - 'FHEE__EE_Session__garbage_collection___expired_session_transient_delete_query_limit', |
|
1230 | - 50 |
|
1231 | - ) |
|
1232 | - ); |
|
1233 | - // is there a value? or one that is different than the default 50 records? |
|
1234 | - if ($expired_session_transient_delete_query_limit === 0) { |
|
1235 | - // hook into TransientCacheStorage in case Session cleanup was turned off |
|
1236 | - add_filter('FHEE__TransientCacheStorage__transient_cleanup_schedule', '__return_zero'); |
|
1237 | - } elseif ($expired_session_transient_delete_query_limit !== 50) { |
|
1238 | - // or use that for the new transient cleanup query limit |
|
1239 | - add_filter( |
|
1240 | - 'FHEE__TransientCacheStorage__clearExpiredTransients__limit', |
|
1241 | - function () use ($expired_session_transient_delete_query_limit) { |
|
1242 | - return $expired_session_transient_delete_query_limit; |
|
1243 | - } |
|
1244 | - ); |
|
1245 | - } |
|
1246 | - } |
|
1247 | - |
|
1248 | - |
|
1249 | - /** |
|
1250 | - * @see http://stackoverflow.com/questions/10152904/unserialize-function-unserialize-error-at-offset/21389439#10152996 |
|
1251 | - * @param $data1 |
|
1252 | - * @return string |
|
1253 | - */ |
|
1254 | - private function find_serialize_error($data1) |
|
1255 | - { |
|
1256 | - $error = '<pre>'; |
|
1257 | - $data2 = preg_replace_callback( |
|
1258 | - '!s:(\d+):"(.*?)";!', |
|
1259 | - function ($match) { |
|
1260 | - return ($match[1] === strlen($match[2])) |
|
1261 | - ? $match[0] |
|
1262 | - : 's:' |
|
1263 | - . strlen($match[2]) |
|
1264 | - . ':"' |
|
1265 | - . $match[2] |
|
1266 | - . '";'; |
|
1267 | - }, |
|
1268 | - $data1 |
|
1269 | - ); |
|
1270 | - $max = (strlen($data1) > strlen($data2)) ? strlen($data1) : strlen($data2); |
|
1271 | - $error .= $data1 . PHP_EOL; |
|
1272 | - $error .= $data2 . PHP_EOL; |
|
1273 | - for ($i = 0; $i < $max; $i++) { |
|
1274 | - if (@$data1[ $i ] !== @$data2[ $i ]) { |
|
1275 | - $error .= 'Difference ' . @$data1[ $i ] . ' != ' . @$data2[ $i ] . PHP_EOL; |
|
1276 | - $error .= "\t-> ORD number " . ord(@$data1[ $i ]) . ' != ' . ord(@$data2[ $i ]) . PHP_EOL; |
|
1277 | - $error .= "\t-> Line Number = $i" . PHP_EOL; |
|
1278 | - $start = ($i - 20); |
|
1279 | - $start = ($start < 0) ? 0 : $start; |
|
1280 | - $length = 40; |
|
1281 | - $point = $max - $i; |
|
1282 | - if ($point < 20) { |
|
1283 | - $rlength = 1; |
|
1284 | - $rpoint = -$point; |
|
1285 | - } else { |
|
1286 | - $rpoint = $length - 20; |
|
1287 | - $rlength = 1; |
|
1288 | - } |
|
1289 | - $error .= "\t-> Section Data1 = "; |
|
1290 | - $error .= substr_replace( |
|
1291 | - substr($data1, $start, $length), |
|
1292 | - "<b style=\"color:green\">{$data1[ $i ]}</b>", |
|
1293 | - $rpoint, |
|
1294 | - $rlength |
|
1295 | - ); |
|
1296 | - $error .= PHP_EOL; |
|
1297 | - $error .= "\t-> Section Data2 = "; |
|
1298 | - $error .= substr_replace( |
|
1299 | - substr($data2, $start, $length), |
|
1300 | - "<b style=\"color:red\">{$data2[ $i ]}</b>", |
|
1301 | - $rpoint, |
|
1302 | - $rlength |
|
1303 | - ); |
|
1304 | - $error .= PHP_EOL; |
|
1305 | - } |
|
1306 | - } |
|
1307 | - $error .= '</pre>'; |
|
1308 | - return $error; |
|
1309 | - } |
|
1310 | - |
|
1311 | - |
|
1312 | - /** |
|
1313 | - * Saves an array of settings used for configuring aspects of session behaviour |
|
1314 | - * |
|
1315 | - * @param array $updated_settings |
|
1316 | - */ |
|
1317 | - private function updateSessionSettings(array $updated_settings = array()) |
|
1318 | - { |
|
1319 | - // add existing settings, but only if not included in incoming $updated_settings array |
|
1320 | - $updated_settings += get_option(EE_Session::OPTION_NAME_SETTINGS, array()); |
|
1321 | - update_option(EE_Session::OPTION_NAME_SETTINGS, $updated_settings); |
|
1322 | - } |
|
1323 | - |
|
1324 | - |
|
1325 | - /** |
|
1326 | - * garbage_collection |
|
1327 | - */ |
|
1328 | - public function garbageCollection() |
|
1329 | - { |
|
1330 | - // only perform during regular requests if last garbage collection was over an hour ago |
|
1331 | - if (! (defined('DOING_AJAX') && DOING_AJAX) && (time() - HOUR_IN_SECONDS) >= $this->_last_gc) { |
|
1332 | - $this->_last_gc = time(); |
|
1333 | - $this->updateSessionSettings(array('last_gc' => $this->_last_gc)); |
|
1334 | - /** @type WPDB $wpdb */ |
|
1335 | - global $wpdb; |
|
1336 | - // filter the query limit. Set to 0 to turn off garbage collection |
|
1337 | - $expired_session_transient_delete_query_limit = absint( |
|
1338 | - apply_filters( |
|
1339 | - 'FHEE__EE_Session__garbage_collection___expired_session_transient_delete_query_limit', |
|
1340 | - 50 |
|
1341 | - ) |
|
1342 | - ); |
|
1343 | - // non-zero LIMIT means take out the trash |
|
1344 | - if ($expired_session_transient_delete_query_limit) { |
|
1345 | - $session_key = str_replace('_', '\_', EE_Session::session_id_prefix); |
|
1346 | - $hash_check_key = str_replace('_', '\_', EE_Session::hash_check_prefix); |
|
1347 | - // since transient expiration timestamps are set in the future, we can compare against NOW |
|
1348 | - // but we only want to pick up any trash that's been around for more than a day |
|
1349 | - $expiration = time() - DAY_IN_SECONDS; |
|
1350 | - $SQL = " |
|
1071 | + do_action('AHEE_log', __FILE__, __FUNCTION__, 'session cleared by : ' . $class . '::' . $function . '()'); |
|
1072 | + $this->reset_cart(); |
|
1073 | + $this->reset_checkout(); |
|
1074 | + $this->reset_transaction(); |
|
1075 | + // wipe out everything that isn't a default session datum |
|
1076 | + $this->reset_data(array_keys($this->_session_data)); |
|
1077 | + // reset initial site access time and the session expiration |
|
1078 | + $this->_set_init_access_and_expiration(); |
|
1079 | + $this->setSaveState(); |
|
1080 | + $this->_save_session_to_db(true); |
|
1081 | + } |
|
1082 | + |
|
1083 | + |
|
1084 | + /** |
|
1085 | + * resets all non-default session vars. Returns TRUE on success, FALSE on fail |
|
1086 | + * |
|
1087 | + * @param array|mixed $data_to_reset |
|
1088 | + * @param bool $show_all_notices |
|
1089 | + * @return bool |
|
1090 | + */ |
|
1091 | + public function reset_data($data_to_reset = array(), $show_all_notices = false) |
|
1092 | + { |
|
1093 | + // if $data_to_reset is not in an array, then put it in one |
|
1094 | + if (! is_array($data_to_reset)) { |
|
1095 | + $data_to_reset = array($data_to_reset); |
|
1096 | + } |
|
1097 | + // nothing ??? go home! |
|
1098 | + if (empty($data_to_reset)) { |
|
1099 | + EE_Error::add_error( |
|
1100 | + esc_html__( |
|
1101 | + 'No session data could be reset, because no session var name was provided.', |
|
1102 | + 'event_espresso' |
|
1103 | + ), |
|
1104 | + __FILE__, |
|
1105 | + __FUNCTION__, |
|
1106 | + __LINE__ |
|
1107 | + ); |
|
1108 | + return false; |
|
1109 | + } |
|
1110 | + $return_value = true; |
|
1111 | + // since $data_to_reset is an array, cycle through the values |
|
1112 | + foreach ($data_to_reset as $reset) { |
|
1113 | + // first check to make sure it is a valid session var |
|
1114 | + if (isset($this->_session_data[ $reset ])) { |
|
1115 | + // then check to make sure it is not a default var |
|
1116 | + if (! array_key_exists($reset, $this->_default_session_vars)) { |
|
1117 | + // remove session var |
|
1118 | + unset($this->_session_data[ $reset ]); |
|
1119 | + $this->setSaveState(); |
|
1120 | + if ($show_all_notices) { |
|
1121 | + EE_Error::add_success( |
|
1122 | + sprintf( |
|
1123 | + esc_html__('The session variable %s was removed.', 'event_espresso'), |
|
1124 | + $reset |
|
1125 | + ), |
|
1126 | + __FILE__, |
|
1127 | + __FUNCTION__, |
|
1128 | + __LINE__ |
|
1129 | + ); |
|
1130 | + } |
|
1131 | + } else { |
|
1132 | + // yeeeeeeeeerrrrrrrrrrr OUT !!!! |
|
1133 | + if ($show_all_notices) { |
|
1134 | + EE_Error::add_error( |
|
1135 | + sprintf( |
|
1136 | + esc_html__( |
|
1137 | + 'Sorry! %s is a default session datum and can not be reset.', |
|
1138 | + 'event_espresso' |
|
1139 | + ), |
|
1140 | + $reset |
|
1141 | + ), |
|
1142 | + __FILE__, |
|
1143 | + __FUNCTION__, |
|
1144 | + __LINE__ |
|
1145 | + ); |
|
1146 | + } |
|
1147 | + $return_value = false; |
|
1148 | + } |
|
1149 | + } elseif ($show_all_notices) { |
|
1150 | + // oops! that session var does not exist! |
|
1151 | + EE_Error::add_error( |
|
1152 | + sprintf( |
|
1153 | + esc_html__( |
|
1154 | + 'The session item provided, %s, is invalid or does not exist.', |
|
1155 | + 'event_espresso' |
|
1156 | + ), |
|
1157 | + $reset |
|
1158 | + ), |
|
1159 | + __FILE__, |
|
1160 | + __FUNCTION__, |
|
1161 | + __LINE__ |
|
1162 | + ); |
|
1163 | + $return_value = false; |
|
1164 | + } |
|
1165 | + } // end of foreach |
|
1166 | + return $return_value; |
|
1167 | + } |
|
1168 | + |
|
1169 | + |
|
1170 | + /** |
|
1171 | + * wp_loaded |
|
1172 | + * |
|
1173 | + * @throws EE_Error |
|
1174 | + * @throws InvalidDataTypeException |
|
1175 | + * @throws InvalidInterfaceException |
|
1176 | + * @throws InvalidArgumentException |
|
1177 | + * @throws ReflectionException |
|
1178 | + */ |
|
1179 | + public function wp_loaded() |
|
1180 | + { |
|
1181 | + if ($this->request->requestParamIsSet('clear_session')) { |
|
1182 | + $this->clear_session(__CLASS__, __FUNCTION__); |
|
1183 | + } |
|
1184 | + } |
|
1185 | + |
|
1186 | + |
|
1187 | + /** |
|
1188 | + * Used to reset the entire object (for tests). |
|
1189 | + * |
|
1190 | + * @since 4.3.0 |
|
1191 | + * @throws EE_Error |
|
1192 | + * @throws InvalidDataTypeException |
|
1193 | + * @throws InvalidInterfaceException |
|
1194 | + * @throws InvalidArgumentException |
|
1195 | + * @throws ReflectionException |
|
1196 | + */ |
|
1197 | + public static function reset() |
|
1198 | + { |
|
1199 | + $session = EE_Session::instance(); |
|
1200 | + if ($session instanceof EE_Session) { |
|
1201 | + $session->clear_session(); |
|
1202 | + } |
|
1203 | + return $session; |
|
1204 | + } |
|
1205 | + |
|
1206 | + |
|
1207 | + /** |
|
1208 | + * Used to reset the entire object (for tests). |
|
1209 | + * |
|
1210 | + * @since 4.3.0 |
|
1211 | + * @throws EE_Error |
|
1212 | + * @throws InvalidDataTypeException |
|
1213 | + * @throws InvalidInterfaceException |
|
1214 | + * @throws InvalidArgumentException |
|
1215 | + * @throws ReflectionException |
|
1216 | + */ |
|
1217 | + public function reset_instance() |
|
1218 | + { |
|
1219 | + $this->clear_session(); |
|
1220 | + self::$_instance = null; |
|
1221 | + } |
|
1222 | + |
|
1223 | + |
|
1224 | + public function configure_garbage_collection_filters() |
|
1225 | + { |
|
1226 | + // run old filter we had for controlling session cleanup |
|
1227 | + $expired_session_transient_delete_query_limit = absint( |
|
1228 | + apply_filters( |
|
1229 | + 'FHEE__EE_Session__garbage_collection___expired_session_transient_delete_query_limit', |
|
1230 | + 50 |
|
1231 | + ) |
|
1232 | + ); |
|
1233 | + // is there a value? or one that is different than the default 50 records? |
|
1234 | + if ($expired_session_transient_delete_query_limit === 0) { |
|
1235 | + // hook into TransientCacheStorage in case Session cleanup was turned off |
|
1236 | + add_filter('FHEE__TransientCacheStorage__transient_cleanup_schedule', '__return_zero'); |
|
1237 | + } elseif ($expired_session_transient_delete_query_limit !== 50) { |
|
1238 | + // or use that for the new transient cleanup query limit |
|
1239 | + add_filter( |
|
1240 | + 'FHEE__TransientCacheStorage__clearExpiredTransients__limit', |
|
1241 | + function () use ($expired_session_transient_delete_query_limit) { |
|
1242 | + return $expired_session_transient_delete_query_limit; |
|
1243 | + } |
|
1244 | + ); |
|
1245 | + } |
|
1246 | + } |
|
1247 | + |
|
1248 | + |
|
1249 | + /** |
|
1250 | + * @see http://stackoverflow.com/questions/10152904/unserialize-function-unserialize-error-at-offset/21389439#10152996 |
|
1251 | + * @param $data1 |
|
1252 | + * @return string |
|
1253 | + */ |
|
1254 | + private function find_serialize_error($data1) |
|
1255 | + { |
|
1256 | + $error = '<pre>'; |
|
1257 | + $data2 = preg_replace_callback( |
|
1258 | + '!s:(\d+):"(.*?)";!', |
|
1259 | + function ($match) { |
|
1260 | + return ($match[1] === strlen($match[2])) |
|
1261 | + ? $match[0] |
|
1262 | + : 's:' |
|
1263 | + . strlen($match[2]) |
|
1264 | + . ':"' |
|
1265 | + . $match[2] |
|
1266 | + . '";'; |
|
1267 | + }, |
|
1268 | + $data1 |
|
1269 | + ); |
|
1270 | + $max = (strlen($data1) > strlen($data2)) ? strlen($data1) : strlen($data2); |
|
1271 | + $error .= $data1 . PHP_EOL; |
|
1272 | + $error .= $data2 . PHP_EOL; |
|
1273 | + for ($i = 0; $i < $max; $i++) { |
|
1274 | + if (@$data1[ $i ] !== @$data2[ $i ]) { |
|
1275 | + $error .= 'Difference ' . @$data1[ $i ] . ' != ' . @$data2[ $i ] . PHP_EOL; |
|
1276 | + $error .= "\t-> ORD number " . ord(@$data1[ $i ]) . ' != ' . ord(@$data2[ $i ]) . PHP_EOL; |
|
1277 | + $error .= "\t-> Line Number = $i" . PHP_EOL; |
|
1278 | + $start = ($i - 20); |
|
1279 | + $start = ($start < 0) ? 0 : $start; |
|
1280 | + $length = 40; |
|
1281 | + $point = $max - $i; |
|
1282 | + if ($point < 20) { |
|
1283 | + $rlength = 1; |
|
1284 | + $rpoint = -$point; |
|
1285 | + } else { |
|
1286 | + $rpoint = $length - 20; |
|
1287 | + $rlength = 1; |
|
1288 | + } |
|
1289 | + $error .= "\t-> Section Data1 = "; |
|
1290 | + $error .= substr_replace( |
|
1291 | + substr($data1, $start, $length), |
|
1292 | + "<b style=\"color:green\">{$data1[ $i ]}</b>", |
|
1293 | + $rpoint, |
|
1294 | + $rlength |
|
1295 | + ); |
|
1296 | + $error .= PHP_EOL; |
|
1297 | + $error .= "\t-> Section Data2 = "; |
|
1298 | + $error .= substr_replace( |
|
1299 | + substr($data2, $start, $length), |
|
1300 | + "<b style=\"color:red\">{$data2[ $i ]}</b>", |
|
1301 | + $rpoint, |
|
1302 | + $rlength |
|
1303 | + ); |
|
1304 | + $error .= PHP_EOL; |
|
1305 | + } |
|
1306 | + } |
|
1307 | + $error .= '</pre>'; |
|
1308 | + return $error; |
|
1309 | + } |
|
1310 | + |
|
1311 | + |
|
1312 | + /** |
|
1313 | + * Saves an array of settings used for configuring aspects of session behaviour |
|
1314 | + * |
|
1315 | + * @param array $updated_settings |
|
1316 | + */ |
|
1317 | + private function updateSessionSettings(array $updated_settings = array()) |
|
1318 | + { |
|
1319 | + // add existing settings, but only if not included in incoming $updated_settings array |
|
1320 | + $updated_settings += get_option(EE_Session::OPTION_NAME_SETTINGS, array()); |
|
1321 | + update_option(EE_Session::OPTION_NAME_SETTINGS, $updated_settings); |
|
1322 | + } |
|
1323 | + |
|
1324 | + |
|
1325 | + /** |
|
1326 | + * garbage_collection |
|
1327 | + */ |
|
1328 | + public function garbageCollection() |
|
1329 | + { |
|
1330 | + // only perform during regular requests if last garbage collection was over an hour ago |
|
1331 | + if (! (defined('DOING_AJAX') && DOING_AJAX) && (time() - HOUR_IN_SECONDS) >= $this->_last_gc) { |
|
1332 | + $this->_last_gc = time(); |
|
1333 | + $this->updateSessionSettings(array('last_gc' => $this->_last_gc)); |
|
1334 | + /** @type WPDB $wpdb */ |
|
1335 | + global $wpdb; |
|
1336 | + // filter the query limit. Set to 0 to turn off garbage collection |
|
1337 | + $expired_session_transient_delete_query_limit = absint( |
|
1338 | + apply_filters( |
|
1339 | + 'FHEE__EE_Session__garbage_collection___expired_session_transient_delete_query_limit', |
|
1340 | + 50 |
|
1341 | + ) |
|
1342 | + ); |
|
1343 | + // non-zero LIMIT means take out the trash |
|
1344 | + if ($expired_session_transient_delete_query_limit) { |
|
1345 | + $session_key = str_replace('_', '\_', EE_Session::session_id_prefix); |
|
1346 | + $hash_check_key = str_replace('_', '\_', EE_Session::hash_check_prefix); |
|
1347 | + // since transient expiration timestamps are set in the future, we can compare against NOW |
|
1348 | + // but we only want to pick up any trash that's been around for more than a day |
|
1349 | + $expiration = time() - DAY_IN_SECONDS; |
|
1350 | + $SQL = " |
|
1351 | 1351 | SELECT option_name |
1352 | 1352 | FROM {$wpdb->options} |
1353 | 1353 | WHERE |
@@ -1356,17 +1356,17 @@ discard block |
||
1356 | 1356 | AND option_value < {$expiration} |
1357 | 1357 | LIMIT {$expired_session_transient_delete_query_limit} |
1358 | 1358 | "; |
1359 | - // produces something like: |
|
1360 | - // SELECT option_name FROM wp_options |
|
1361 | - // WHERE ( option_name LIKE '\_transient\_timeout\_ee\_ssn\_%' |
|
1362 | - // OR option_name LIKE '\_transient\_timeout\_ee\_shc\_%' ) |
|
1363 | - // AND option_value < 1508368198 LIMIT 50 |
|
1364 | - $expired_sessions = $wpdb->get_col($SQL); |
|
1365 | - // valid results? |
|
1366 | - if (! $expired_sessions instanceof WP_Error && ! empty($expired_sessions)) { |
|
1367 | - $this->cache_storage->deleteMany($expired_sessions, true); |
|
1368 | - } |
|
1369 | - } |
|
1370 | - } |
|
1371 | - } |
|
1359 | + // produces something like: |
|
1360 | + // SELECT option_name FROM wp_options |
|
1361 | + // WHERE ( option_name LIKE '\_transient\_timeout\_ee\_ssn\_%' |
|
1362 | + // OR option_name LIKE '\_transient\_timeout\_ee\_shc\_%' ) |
|
1363 | + // AND option_value < 1508368198 LIMIT 50 |
|
1364 | + $expired_sessions = $wpdb->get_col($SQL); |
|
1365 | + // valid results? |
|
1366 | + if (! $expired_sessions instanceof WP_Error && ! empty($expired_sessions)) { |
|
1367 | + $this->cache_storage->deleteMany($expired_sessions, true); |
|
1368 | + } |
|
1369 | + } |
|
1370 | + } |
|
1371 | + } |
|
1372 | 1372 | } |
@@ -22,206 +22,206 @@ |
||
22 | 22 | */ |
23 | 23 | class ModelObjNode extends BaseNode |
24 | 24 | { |
25 | - /** |
|
26 | - * @var int|string |
|
27 | - */ |
|
28 | - protected $id; |
|
25 | + /** |
|
26 | + * @var int|string |
|
27 | + */ |
|
28 | + protected $id; |
|
29 | 29 | |
30 | - /** |
|
31 | - * @var EEM_Base |
|
32 | - */ |
|
33 | - protected $model; |
|
30 | + /** |
|
31 | + * @var EEM_Base |
|
32 | + */ |
|
33 | + protected $model; |
|
34 | 34 | |
35 | - /** |
|
36 | - * @var RelationNode[] |
|
37 | - */ |
|
38 | - protected $nodes; |
|
35 | + /** |
|
36 | + * @var RelationNode[] |
|
37 | + */ |
|
38 | + protected $nodes; |
|
39 | 39 | |
40 | - /** |
|
41 | - * We don't pass the model objects because this needs to serialize to something tiny for effiency. |
|
42 | - * @param $model_obj_id |
|
43 | - * @param EEM_Base $model |
|
44 | - * @param array $dont_traverse_models array of model names we DON'T want to traverse. |
|
45 | - */ |
|
46 | - public function __construct($model_obj_id, EEM_Base $model, array $dont_traverse_models = []) |
|
47 | - { |
|
48 | - $this->id = $model_obj_id; |
|
49 | - $this->model = $model; |
|
50 | - $this->dont_traverse_models = $dont_traverse_models; |
|
51 | - } |
|
40 | + /** |
|
41 | + * We don't pass the model objects because this needs to serialize to something tiny for effiency. |
|
42 | + * @param $model_obj_id |
|
43 | + * @param EEM_Base $model |
|
44 | + * @param array $dont_traverse_models array of model names we DON'T want to traverse. |
|
45 | + */ |
|
46 | + public function __construct($model_obj_id, EEM_Base $model, array $dont_traverse_models = []) |
|
47 | + { |
|
48 | + $this->id = $model_obj_id; |
|
49 | + $this->model = $model; |
|
50 | + $this->dont_traverse_models = $dont_traverse_models; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Creates a relation node for each relation of this model's relations. |
|
55 | - * Does NOT call `discover` on them yet though. |
|
56 | - * @since 4.10.12.p |
|
57 | - * @throws \EE_Error |
|
58 | - * @throws InvalidDataTypeException |
|
59 | - * @throws InvalidInterfaceException |
|
60 | - * @throws InvalidArgumentException |
|
61 | - * @throws ReflectionException |
|
62 | - */ |
|
63 | - protected function discover() |
|
64 | - { |
|
65 | - $this->nodes = []; |
|
66 | - foreach ($this->model->relation_settings() as $relationName => $relation) { |
|
67 | - // Make sure this isn't one of the models we were told to not traverse into. |
|
68 | - if (in_array($relationName, $this->dont_traverse_models)) { |
|
69 | - continue; |
|
70 | - } |
|
71 | - if ($relation instanceof EE_Has_Many_Relation) { |
|
72 | - $this->nodes[ $relationName ] = new RelationNode( |
|
73 | - $this->id, |
|
74 | - $this->model, |
|
75 | - $relation->get_other_model(), |
|
76 | - $this->dont_traverse_models |
|
77 | - ); |
|
78 | - } elseif ( |
|
79 | - $relation instanceof EE_HABTM_Relation && |
|
80 | - ! in_array( |
|
81 | - $relation->get_join_model()->get_this_model_name(), |
|
82 | - $this->dont_traverse_models |
|
83 | - ) |
|
84 | - ) { |
|
85 | - $this->nodes[ $relation->get_join_model()->get_this_model_name() ] = new RelationNode( |
|
86 | - $this->id, |
|
87 | - $this->model, |
|
88 | - $relation->get_join_model(), |
|
89 | - $this->dont_traverse_models |
|
90 | - ); |
|
91 | - } |
|
92 | - } |
|
93 | - ksort($this->nodes); |
|
94 | - } |
|
53 | + /** |
|
54 | + * Creates a relation node for each relation of this model's relations. |
|
55 | + * Does NOT call `discover` on them yet though. |
|
56 | + * @since 4.10.12.p |
|
57 | + * @throws \EE_Error |
|
58 | + * @throws InvalidDataTypeException |
|
59 | + * @throws InvalidInterfaceException |
|
60 | + * @throws InvalidArgumentException |
|
61 | + * @throws ReflectionException |
|
62 | + */ |
|
63 | + protected function discover() |
|
64 | + { |
|
65 | + $this->nodes = []; |
|
66 | + foreach ($this->model->relation_settings() as $relationName => $relation) { |
|
67 | + // Make sure this isn't one of the models we were told to not traverse into. |
|
68 | + if (in_array($relationName, $this->dont_traverse_models)) { |
|
69 | + continue; |
|
70 | + } |
|
71 | + if ($relation instanceof EE_Has_Many_Relation) { |
|
72 | + $this->nodes[ $relationName ] = new RelationNode( |
|
73 | + $this->id, |
|
74 | + $this->model, |
|
75 | + $relation->get_other_model(), |
|
76 | + $this->dont_traverse_models |
|
77 | + ); |
|
78 | + } elseif ( |
|
79 | + $relation instanceof EE_HABTM_Relation && |
|
80 | + ! in_array( |
|
81 | + $relation->get_join_model()->get_this_model_name(), |
|
82 | + $this->dont_traverse_models |
|
83 | + ) |
|
84 | + ) { |
|
85 | + $this->nodes[ $relation->get_join_model()->get_this_model_name() ] = new RelationNode( |
|
86 | + $this->id, |
|
87 | + $this->model, |
|
88 | + $relation->get_join_model(), |
|
89 | + $this->dont_traverse_models |
|
90 | + ); |
|
91 | + } |
|
92 | + } |
|
93 | + ksort($this->nodes); |
|
94 | + } |
|
95 | 95 | |
96 | 96 | |
97 | - /** |
|
98 | - * Whether this item has already been initialized |
|
99 | - */ |
|
100 | - protected function isDiscovered() |
|
101 | - { |
|
102 | - return $this->nodes !== null && is_array($this->nodes); |
|
103 | - } |
|
97 | + /** |
|
98 | + * Whether this item has already been initialized |
|
99 | + */ |
|
100 | + protected function isDiscovered() |
|
101 | + { |
|
102 | + return $this->nodes !== null && is_array($this->nodes); |
|
103 | + } |
|
104 | 104 | |
105 | - /** |
|
106 | - * @since 4.10.12.p |
|
107 | - * @return boolean |
|
108 | - */ |
|
109 | - public function isComplete() |
|
110 | - { |
|
111 | - if ($this->complete === null) { |
|
112 | - $this->complete = false; |
|
113 | - } |
|
114 | - return $this->complete; |
|
115 | - } |
|
105 | + /** |
|
106 | + * @since 4.10.12.p |
|
107 | + * @return boolean |
|
108 | + */ |
|
109 | + public function isComplete() |
|
110 | + { |
|
111 | + if ($this->complete === null) { |
|
112 | + $this->complete = false; |
|
113 | + } |
|
114 | + return $this->complete; |
|
115 | + } |
|
116 | 116 | |
117 | - /** |
|
118 | - * Triggers working on each child relation node that has work to do. |
|
119 | - * @since 4.10.12.p |
|
120 | - * @param $model_objects_to_identify |
|
121 | - * @return int units of work done |
|
122 | - */ |
|
123 | - protected function work($model_objects_to_identify) |
|
124 | - { |
|
125 | - $num_identified = 0; |
|
126 | - // Begin assuming we'll finish all the work on this node and its children... |
|
127 | - $this->complete = true; |
|
128 | - foreach ($this->nodes as $model_name => $relation_node) { |
|
129 | - $num_identified += $relation_node->visit($model_objects_to_identify - $num_identified); |
|
130 | - // To save on space when serializing, only bother keeping a record of relation nodes that actually found |
|
131 | - // related model objects. |
|
132 | - if ($relation_node->isComplete() && $relation_node->countSubNodes() === 0) { |
|
133 | - unset($this->nodes[ $model_name ]); |
|
134 | - } |
|
135 | - if ($num_identified >= $model_objects_to_identify) { |
|
136 | - // ...but admit we're wrong if the work exceeded the budget. |
|
137 | - $this->complete = false; |
|
138 | - break; |
|
139 | - } |
|
140 | - } |
|
141 | - return $num_identified; |
|
142 | - } |
|
117 | + /** |
|
118 | + * Triggers working on each child relation node that has work to do. |
|
119 | + * @since 4.10.12.p |
|
120 | + * @param $model_objects_to_identify |
|
121 | + * @return int units of work done |
|
122 | + */ |
|
123 | + protected function work($model_objects_to_identify) |
|
124 | + { |
|
125 | + $num_identified = 0; |
|
126 | + // Begin assuming we'll finish all the work on this node and its children... |
|
127 | + $this->complete = true; |
|
128 | + foreach ($this->nodes as $model_name => $relation_node) { |
|
129 | + $num_identified += $relation_node->visit($model_objects_to_identify - $num_identified); |
|
130 | + // To save on space when serializing, only bother keeping a record of relation nodes that actually found |
|
131 | + // related model objects. |
|
132 | + if ($relation_node->isComplete() && $relation_node->countSubNodes() === 0) { |
|
133 | + unset($this->nodes[ $model_name ]); |
|
134 | + } |
|
135 | + if ($num_identified >= $model_objects_to_identify) { |
|
136 | + // ...but admit we're wrong if the work exceeded the budget. |
|
137 | + $this->complete = false; |
|
138 | + break; |
|
139 | + } |
|
140 | + } |
|
141 | + return $num_identified; |
|
142 | + } |
|
143 | 143 | |
144 | - /** |
|
145 | - * @since 4.10.12.p |
|
146 | - * @return array |
|
147 | - * @throws \EE_Error |
|
148 | - * @throws InvalidDataTypeException |
|
149 | - * @throws InvalidInterfaceException |
|
150 | - * @throws InvalidArgumentException |
|
151 | - * @throws ReflectionException |
|
152 | - */ |
|
153 | - public function toArray() |
|
154 | - { |
|
155 | - $tree = [ |
|
156 | - 'id' => $this->id, |
|
157 | - 'complete' => $this->isComplete(), |
|
158 | - 'rels' => [] |
|
159 | - ]; |
|
160 | - if ($this->nodes === null) { |
|
161 | - $tree['rels'] = null; |
|
162 | - } else { |
|
163 | - foreach ($this->nodes as $relation_name => $relation_node) { |
|
164 | - $tree['rels'][ $relation_name ] = $relation_node->toArray(); |
|
165 | - } |
|
166 | - } |
|
167 | - return $tree; |
|
168 | - } |
|
144 | + /** |
|
145 | + * @since 4.10.12.p |
|
146 | + * @return array |
|
147 | + * @throws \EE_Error |
|
148 | + * @throws InvalidDataTypeException |
|
149 | + * @throws InvalidInterfaceException |
|
150 | + * @throws InvalidArgumentException |
|
151 | + * @throws ReflectionException |
|
152 | + */ |
|
153 | + public function toArray() |
|
154 | + { |
|
155 | + $tree = [ |
|
156 | + 'id' => $this->id, |
|
157 | + 'complete' => $this->isComplete(), |
|
158 | + 'rels' => [] |
|
159 | + ]; |
|
160 | + if ($this->nodes === null) { |
|
161 | + $tree['rels'] = null; |
|
162 | + } else { |
|
163 | + foreach ($this->nodes as $relation_name => $relation_node) { |
|
164 | + $tree['rels'][ $relation_name ] = $relation_node->toArray(); |
|
165 | + } |
|
166 | + } |
|
167 | + return $tree; |
|
168 | + } |
|
169 | 169 | |
170 | - /** |
|
171 | - * @since 4.10.12.p |
|
172 | - * @return array|mixed |
|
173 | - * @throws InvalidArgumentException |
|
174 | - * @throws InvalidDataTypeException |
|
175 | - * @throws InvalidInterfaceException |
|
176 | - * @throws ReflectionException |
|
177 | - * @throws \EE_Error |
|
178 | - */ |
|
179 | - public function getIds() |
|
180 | - { |
|
181 | - $ids = [ |
|
182 | - $this->model->get_this_model_name() => [ |
|
183 | - $this->id => $this->id |
|
184 | - ] |
|
185 | - ]; |
|
186 | - if ($this->nodes && is_array($this->nodes)) { |
|
187 | - foreach ($this->nodes as $relation_node) { |
|
188 | - $ids = array_replace_recursive($ids, $relation_node->getIds()); |
|
189 | - } |
|
190 | - } |
|
191 | - return $ids; |
|
192 | - } |
|
170 | + /** |
|
171 | + * @since 4.10.12.p |
|
172 | + * @return array|mixed |
|
173 | + * @throws InvalidArgumentException |
|
174 | + * @throws InvalidDataTypeException |
|
175 | + * @throws InvalidInterfaceException |
|
176 | + * @throws ReflectionException |
|
177 | + * @throws \EE_Error |
|
178 | + */ |
|
179 | + public function getIds() |
|
180 | + { |
|
181 | + $ids = [ |
|
182 | + $this->model->get_this_model_name() => [ |
|
183 | + $this->id => $this->id |
|
184 | + ] |
|
185 | + ]; |
|
186 | + if ($this->nodes && is_array($this->nodes)) { |
|
187 | + foreach ($this->nodes as $relation_node) { |
|
188 | + $ids = array_replace_recursive($ids, $relation_node->getIds()); |
|
189 | + } |
|
190 | + } |
|
191 | + return $ids; |
|
192 | + } |
|
193 | 193 | |
194 | - /** |
|
195 | - * Don't serialize the models. Just record their names on some dynamic properties. |
|
196 | - * @since 4.10.12.p |
|
197 | - */ |
|
198 | - public function __sleep() |
|
199 | - { |
|
200 | - $this->m = $this->model->get_this_model_name(); |
|
201 | - return array_merge( |
|
202 | - [ |
|
203 | - 'm', |
|
204 | - 'id', |
|
205 | - 'nodes', |
|
206 | - ], |
|
207 | - parent::__sleep() |
|
208 | - ); |
|
209 | - } |
|
194 | + /** |
|
195 | + * Don't serialize the models. Just record their names on some dynamic properties. |
|
196 | + * @since 4.10.12.p |
|
197 | + */ |
|
198 | + public function __sleep() |
|
199 | + { |
|
200 | + $this->m = $this->model->get_this_model_name(); |
|
201 | + return array_merge( |
|
202 | + [ |
|
203 | + 'm', |
|
204 | + 'id', |
|
205 | + 'nodes', |
|
206 | + ], |
|
207 | + parent::__sleep() |
|
208 | + ); |
|
209 | + } |
|
210 | 210 | |
211 | - /** |
|
212 | - * Use the dynamic properties to instantiate the models we use. |
|
213 | - * @since 4.10.12.p |
|
214 | - * @throws EE_Error |
|
215 | - * @throws InvalidArgumentException |
|
216 | - * @throws InvalidDataTypeException |
|
217 | - * @throws InvalidInterfaceException |
|
218 | - * @throws ReflectionException |
|
219 | - */ |
|
220 | - public function __wakeup() |
|
221 | - { |
|
222 | - $this->model = EE_Registry::instance()->load_model($this->m); |
|
223 | - parent::__wakeup(); |
|
224 | - } |
|
211 | + /** |
|
212 | + * Use the dynamic properties to instantiate the models we use. |
|
213 | + * @since 4.10.12.p |
|
214 | + * @throws EE_Error |
|
215 | + * @throws InvalidArgumentException |
|
216 | + * @throws InvalidDataTypeException |
|
217 | + * @throws InvalidInterfaceException |
|
218 | + * @throws ReflectionException |
|
219 | + */ |
|
220 | + public function __wakeup() |
|
221 | + { |
|
222 | + $this->model = EE_Registry::instance()->load_model($this->m); |
|
223 | + parent::__wakeup(); |
|
224 | + } |
|
225 | 225 | } |
226 | 226 | // End of file Visitor.php |
227 | 227 | // Location: EventEspresso\core\services\orm\tree_traversal/Visitor.php |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | continue; |
70 | 70 | } |
71 | 71 | if ($relation instanceof EE_Has_Many_Relation) { |
72 | - $this->nodes[ $relationName ] = new RelationNode( |
|
72 | + $this->nodes[$relationName] = new RelationNode( |
|
73 | 73 | $this->id, |
74 | 74 | $this->model, |
75 | 75 | $relation->get_other_model(), |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | $this->dont_traverse_models |
83 | 83 | ) |
84 | 84 | ) { |
85 | - $this->nodes[ $relation->get_join_model()->get_this_model_name() ] = new RelationNode( |
|
85 | + $this->nodes[$relation->get_join_model()->get_this_model_name()] = new RelationNode( |
|
86 | 86 | $this->id, |
87 | 87 | $this->model, |
88 | 88 | $relation->get_join_model(), |
@@ -130,7 +130,7 @@ discard block |
||
130 | 130 | // To save on space when serializing, only bother keeping a record of relation nodes that actually found |
131 | 131 | // related model objects. |
132 | 132 | if ($relation_node->isComplete() && $relation_node->countSubNodes() === 0) { |
133 | - unset($this->nodes[ $model_name ]); |
|
133 | + unset($this->nodes[$model_name]); |
|
134 | 134 | } |
135 | 135 | if ($num_identified >= $model_objects_to_identify) { |
136 | 136 | // ...but admit we're wrong if the work exceeded the budget. |
@@ -161,7 +161,7 @@ discard block |
||
161 | 161 | $tree['rels'] = null; |
162 | 162 | } else { |
163 | 163 | foreach ($this->nodes as $relation_name => $relation_node) { |
164 | - $tree['rels'][ $relation_name ] = $relation_node->toArray(); |
|
164 | + $tree['rels'][$relation_name] = $relation_node->toArray(); |
|
165 | 165 | } |
166 | 166 | } |
167 | 167 | return $tree; |
@@ -68,11 +68,11 @@ discard block |
||
68 | 68 | } |
69 | 69 | |
70 | 70 | // make sure we don't register twice |
71 | - if (isset(self::$_registry[ $identifier ])) { |
|
71 | + if (isset(self::$_registry[$identifier])) { |
|
72 | 72 | return; |
73 | 73 | } |
74 | 74 | // check correct loading |
75 | - if (! did_action('AHEE__EE_System__load_espresso_addons') || did_action('AHEE__EE_Admin__loaded')) { |
|
75 | + if ( ! did_action('AHEE__EE_System__load_espresso_addons') || did_action('AHEE__EE_Admin__loaded')) { |
|
76 | 76 | EE_Error::doing_it_wrong( |
77 | 77 | __METHOD__, |
78 | 78 | sprintf( |
@@ -89,30 +89,30 @@ discard block |
||
89 | 89 | ); |
90 | 90 | } |
91 | 91 | |
92 | - self::$_registry[ $identifier ] = $setup_args; |
|
93 | - self::$_extensions[ $identifier ] = []; |
|
92 | + self::$_registry[$identifier] = $setup_args; |
|
93 | + self::$_extensions[$identifier] = []; |
|
94 | 94 | |
95 | 95 | if (isset($setup_args['model_extension_paths'])) { |
96 | - require_once(EE_LIBRARIES . 'plugin_api/db/EEME_Base.lib.php'); |
|
96 | + require_once(EE_LIBRARIES.'plugin_api/db/EEME_Base.lib.php'); |
|
97 | 97 | $class_to_filepath_map = EEH_File::get_contents_of_folders($setup_args['model_extension_paths']); |
98 | 98 | // remove all files that are not PHP |
99 | 99 | foreach ($class_to_filepath_map as $class => $path) { |
100 | 100 | if (substr($path, strlen($path) - 3) !== 'php') { |
101 | - unset($class_to_filepath_map[ $class ]); |
|
101 | + unset($class_to_filepath_map[$class]); |
|
102 | 102 | } |
103 | 103 | } |
104 | 104 | EEH_Autoloader::register_autoloader($class_to_filepath_map); |
105 | 105 | foreach (array_keys($class_to_filepath_map) as $classname) { |
106 | - self::$_extensions[ $identifier ]['models'][ $classname ] = new $classname(); |
|
106 | + self::$_extensions[$identifier]['models'][$classname] = new $classname(); |
|
107 | 107 | } |
108 | 108 | unset($setup_args['model_extension_paths']); |
109 | 109 | } |
110 | 110 | if (isset($setup_args['class_extension_paths'])) { |
111 | - require_once(EE_LIBRARIES . 'plugin_api/db/EEE_Base_Class.lib.php'); |
|
111 | + require_once(EE_LIBRARIES.'plugin_api/db/EEE_Base_Class.lib.php'); |
|
112 | 112 | $class_to_filepath_map = EEH_File::get_contents_of_folders($setup_args['class_extension_paths']); |
113 | 113 | EEH_Autoloader::register_autoloader($class_to_filepath_map); |
114 | 114 | foreach (array_keys($class_to_filepath_map) as $classname) { |
115 | - self::$_extensions[ $identifier ]['classes'][ $classname ] = new $classname(); |
|
115 | + self::$_extensions[$identifier]['classes'][$classname] = new $classname(); |
|
116 | 116 | } |
117 | 117 | unset($setup_args['class_extension_paths']); |
118 | 118 | } |
@@ -131,9 +131,9 @@ discard block |
||
131 | 131 | */ |
132 | 132 | public static function deregister($identifier = '') |
133 | 133 | { |
134 | - if (isset(self::$_registry[ $identifier ])) { |
|
135 | - unset(self::$_registry[ $identifier ]); |
|
136 | - foreach (self::$_extensions[ $identifier ] as $extension_of_type) { |
|
134 | + if (isset(self::$_registry[$identifier])) { |
|
135 | + unset(self::$_registry[$identifier]); |
|
136 | + foreach (self::$_extensions[$identifier] as $extension_of_type) { |
|
137 | 137 | foreach ($extension_of_type as $extension) { |
138 | 138 | $extension->deregister(); |
139 | 139 | } |
@@ -11,132 +11,132 @@ |
||
11 | 11 | */ |
12 | 12 | class EE_Register_Model_Extensions implements EEI_Plugin_API |
13 | 13 | { |
14 | - protected static $_registry; |
|
14 | + protected static $_registry; |
|
15 | 15 | |
16 | - protected static $_extensions = []; |
|
16 | + protected static $_extensions = []; |
|
17 | 17 | |
18 | 18 | |
19 | - /** |
|
20 | - * register method for setting up model extensions |
|
21 | - * |
|
22 | - * @param string $identifier unique id for the extensions being setup |
|
23 | - * @param array $setup_args { |
|
24 | - * @return void |
|
25 | - * @throws EE_Error |
|
26 | - * @type array $model_extension_paths array of folders containing DB model extensions, where each file follows |
|
27 | - * the models naming convention, which is: |
|
28 | - * EEME_{your_plugin_slug}_model_name_extended}.model_ext.php. |
|
29 | - * Where {your_plugin_slug} is really anything you want (but something having |
|
30 | - * to do with your addon, like 'Calendar' or '3D_View') and |
|
31 | - * model_name_extended} is the model extended. |
|
32 | - * The class contained in teh file should extend |
|
33 | - * EEME_Base_{model_name_extended}.model_ext.php. |
|
34 | - * Where {your_plugin_slug} is really anything you want (but something |
|
35 | - * having to do with your addon, like 'Calendar' or '3D_View') and |
|
36 | - * {model_name_extended} is the model extended. The class contained in teh |
|
37 | - * file should extend EEME_Base |
|
38 | - * @type array $class_extension_paths array of folders containing DB class extensions, where each file follows |
|
39 | - * the model class extension naming convention, which is: |
|
40 | - * EEE_{your_plugin_slug}_model_name_extended}.class_ext.php. |
|
41 | - * Where {your_plugin_slug} is something like 'Calendar','MailChimp',etc, |
|
42 | - * and model_name_extended} is the name of the model extended, eg |
|
43 | - * 'Attendee','Event',etc. |
|
44 | - * The class contained in the file should extend EEE_Base_Class |
|
45 | - * ._{model_name_extended}.class_ext.php. |
|
46 | - * Where {your_plugin_slug} is something like 'Calendar','MailChimp',etc, |
|
47 | - * and {model_name_extended} is the name of the model extended, eg |
|
48 | - * 'Attendee','Event',etc. The class contained in the file should extend |
|
49 | - * EEE_Base_Class. |
|
50 | - * } |
|
51 | - * |
|
52 | - */ |
|
53 | - public static function register($identifier = '', array $setup_args = []) |
|
54 | - { |
|
55 | - // required fields MUST be present, so let's make sure they are. |
|
56 | - if ( |
|
57 | - empty($identifier) |
|
58 | - || ! is_array($setup_args) |
|
59 | - || (empty($setup_args['model_extension_paths']) && empty($setup_args['class_extension_paths'])) |
|
60 | - ) { |
|
61 | - throw new EE_Error( |
|
62 | - esc_html__( |
|
63 | - 'In order to register Model extensions with EE_Register_Model_Extensions::register(), you must include a "model_id" (a unique identifier for this set of models), and an array containing the following keys: "model_extension_paths" (an array of full server paths to folders that contain model extensions), and "class_extension_paths" (an array of full server paths to folders that contain class extensions)', |
|
64 | - 'event_espresso' |
|
65 | - ) |
|
66 | - ); |
|
67 | - } |
|
19 | + /** |
|
20 | + * register method for setting up model extensions |
|
21 | + * |
|
22 | + * @param string $identifier unique id for the extensions being setup |
|
23 | + * @param array $setup_args { |
|
24 | + * @return void |
|
25 | + * @throws EE_Error |
|
26 | + * @type array $model_extension_paths array of folders containing DB model extensions, where each file follows |
|
27 | + * the models naming convention, which is: |
|
28 | + * EEME_{your_plugin_slug}_model_name_extended}.model_ext.php. |
|
29 | + * Where {your_plugin_slug} is really anything you want (but something having |
|
30 | + * to do with your addon, like 'Calendar' or '3D_View') and |
|
31 | + * model_name_extended} is the model extended. |
|
32 | + * The class contained in teh file should extend |
|
33 | + * EEME_Base_{model_name_extended}.model_ext.php. |
|
34 | + * Where {your_plugin_slug} is really anything you want (but something |
|
35 | + * having to do with your addon, like 'Calendar' or '3D_View') and |
|
36 | + * {model_name_extended} is the model extended. The class contained in teh |
|
37 | + * file should extend EEME_Base |
|
38 | + * @type array $class_extension_paths array of folders containing DB class extensions, where each file follows |
|
39 | + * the model class extension naming convention, which is: |
|
40 | + * EEE_{your_plugin_slug}_model_name_extended}.class_ext.php. |
|
41 | + * Where {your_plugin_slug} is something like 'Calendar','MailChimp',etc, |
|
42 | + * and model_name_extended} is the name of the model extended, eg |
|
43 | + * 'Attendee','Event',etc. |
|
44 | + * The class contained in the file should extend EEE_Base_Class |
|
45 | + * ._{model_name_extended}.class_ext.php. |
|
46 | + * Where {your_plugin_slug} is something like 'Calendar','MailChimp',etc, |
|
47 | + * and {model_name_extended} is the name of the model extended, eg |
|
48 | + * 'Attendee','Event',etc. The class contained in the file should extend |
|
49 | + * EEE_Base_Class. |
|
50 | + * } |
|
51 | + * |
|
52 | + */ |
|
53 | + public static function register($identifier = '', array $setup_args = []) |
|
54 | + { |
|
55 | + // required fields MUST be present, so let's make sure they are. |
|
56 | + if ( |
|
57 | + empty($identifier) |
|
58 | + || ! is_array($setup_args) |
|
59 | + || (empty($setup_args['model_extension_paths']) && empty($setup_args['class_extension_paths'])) |
|
60 | + ) { |
|
61 | + throw new EE_Error( |
|
62 | + esc_html__( |
|
63 | + 'In order to register Model extensions with EE_Register_Model_Extensions::register(), you must include a "model_id" (a unique identifier for this set of models), and an array containing the following keys: "model_extension_paths" (an array of full server paths to folders that contain model extensions), and "class_extension_paths" (an array of full server paths to folders that contain class extensions)', |
|
64 | + 'event_espresso' |
|
65 | + ) |
|
66 | + ); |
|
67 | + } |
|
68 | 68 | |
69 | - // make sure we don't register twice |
|
70 | - if (isset(self::$_registry[ $identifier ])) { |
|
71 | - return; |
|
72 | - } |
|
73 | - // check correct loading |
|
74 | - if (! did_action('AHEE__EE_System__load_espresso_addons') || did_action('AHEE__EE_Admin__loaded')) { |
|
75 | - EE_Error::doing_it_wrong( |
|
76 | - __METHOD__, |
|
77 | - sprintf( |
|
78 | - esc_html__( |
|
79 | - 'An attempt was made to register "%1$s" as a Model extension has failed because it was not registered at the correct time. Please use the "AHEE__EE_System__load_espresso_addons" hook to register models.%2$s Hook Status: %2$s "AHEE__EE_System__load_espresso_addons" : %3$s %2$s "AHEE__EE_Admin__loaded" : %4$s%2$s', |
|
80 | - 'event_espresso' |
|
81 | - ), |
|
82 | - $identifier, |
|
83 | - '<br />', |
|
84 | - did_action('AHEE__EE_System__load_espresso_addons') ? 'action done' : 'action NOT done', |
|
85 | - did_action('AHEE__EE_Admin__loaded') ? 'action done' : 'action NOT done' |
|
86 | - ), |
|
87 | - '4.3' |
|
88 | - ); |
|
89 | - } |
|
69 | + // make sure we don't register twice |
|
70 | + if (isset(self::$_registry[ $identifier ])) { |
|
71 | + return; |
|
72 | + } |
|
73 | + // check correct loading |
|
74 | + if (! did_action('AHEE__EE_System__load_espresso_addons') || did_action('AHEE__EE_Admin__loaded')) { |
|
75 | + EE_Error::doing_it_wrong( |
|
76 | + __METHOD__, |
|
77 | + sprintf( |
|
78 | + esc_html__( |
|
79 | + 'An attempt was made to register "%1$s" as a Model extension has failed because it was not registered at the correct time. Please use the "AHEE__EE_System__load_espresso_addons" hook to register models.%2$s Hook Status: %2$s "AHEE__EE_System__load_espresso_addons" : %3$s %2$s "AHEE__EE_Admin__loaded" : %4$s%2$s', |
|
80 | + 'event_espresso' |
|
81 | + ), |
|
82 | + $identifier, |
|
83 | + '<br />', |
|
84 | + did_action('AHEE__EE_System__load_espresso_addons') ? 'action done' : 'action NOT done', |
|
85 | + did_action('AHEE__EE_Admin__loaded') ? 'action done' : 'action NOT done' |
|
86 | + ), |
|
87 | + '4.3' |
|
88 | + ); |
|
89 | + } |
|
90 | 90 | |
91 | - self::$_registry[ $identifier ] = $setup_args; |
|
92 | - self::$_extensions[ $identifier ] = []; |
|
91 | + self::$_registry[ $identifier ] = $setup_args; |
|
92 | + self::$_extensions[ $identifier ] = []; |
|
93 | 93 | |
94 | - if (isset($setup_args['model_extension_paths'])) { |
|
95 | - require_once(EE_LIBRARIES . 'plugin_api/db/EEME_Base.lib.php'); |
|
96 | - $class_to_filepath_map = EEH_File::get_contents_of_folders($setup_args['model_extension_paths']); |
|
97 | - // remove all files that are not PHP |
|
98 | - foreach ($class_to_filepath_map as $class => $path) { |
|
99 | - if (substr($path, strlen($path) - 3) !== 'php') { |
|
100 | - unset($class_to_filepath_map[ $class ]); |
|
101 | - } |
|
102 | - } |
|
103 | - EEH_Autoloader::register_autoloader($class_to_filepath_map); |
|
104 | - foreach (array_keys($class_to_filepath_map) as $classname) { |
|
105 | - self::$_extensions[ $identifier ]['models'][ $classname ] = new $classname(); |
|
106 | - } |
|
107 | - unset($setup_args['model_extension_paths']); |
|
108 | - } |
|
109 | - if (isset($setup_args['class_extension_paths'])) { |
|
110 | - require_once(EE_LIBRARIES . 'plugin_api/db/EEE_Base_Class.lib.php'); |
|
111 | - $class_to_filepath_map = EEH_File::get_contents_of_folders($setup_args['class_extension_paths']); |
|
112 | - EEH_Autoloader::register_autoloader($class_to_filepath_map); |
|
113 | - foreach (array_keys($class_to_filepath_map) as $classname) { |
|
114 | - self::$_extensions[ $identifier ]['classes'][ $classname ] = new $classname(); |
|
115 | - } |
|
116 | - unset($setup_args['class_extension_paths']); |
|
117 | - } |
|
118 | - foreach ($setup_args as $unknown_key => $unknown_config) { |
|
119 | - throw new EE_Error( |
|
120 | - sprintf(esc_html__("The key '%s' is not a known key for registering a model", "event_espresso"), $unknown_key) |
|
121 | - ); |
|
122 | - } |
|
123 | - } |
|
94 | + if (isset($setup_args['model_extension_paths'])) { |
|
95 | + require_once(EE_LIBRARIES . 'plugin_api/db/EEME_Base.lib.php'); |
|
96 | + $class_to_filepath_map = EEH_File::get_contents_of_folders($setup_args['model_extension_paths']); |
|
97 | + // remove all files that are not PHP |
|
98 | + foreach ($class_to_filepath_map as $class => $path) { |
|
99 | + if (substr($path, strlen($path) - 3) !== 'php') { |
|
100 | + unset($class_to_filepath_map[ $class ]); |
|
101 | + } |
|
102 | + } |
|
103 | + EEH_Autoloader::register_autoloader($class_to_filepath_map); |
|
104 | + foreach (array_keys($class_to_filepath_map) as $classname) { |
|
105 | + self::$_extensions[ $identifier ]['models'][ $classname ] = new $classname(); |
|
106 | + } |
|
107 | + unset($setup_args['model_extension_paths']); |
|
108 | + } |
|
109 | + if (isset($setup_args['class_extension_paths'])) { |
|
110 | + require_once(EE_LIBRARIES . 'plugin_api/db/EEE_Base_Class.lib.php'); |
|
111 | + $class_to_filepath_map = EEH_File::get_contents_of_folders($setup_args['class_extension_paths']); |
|
112 | + EEH_Autoloader::register_autoloader($class_to_filepath_map); |
|
113 | + foreach (array_keys($class_to_filepath_map) as $classname) { |
|
114 | + self::$_extensions[ $identifier ]['classes'][ $classname ] = new $classname(); |
|
115 | + } |
|
116 | + unset($setup_args['class_extension_paths']); |
|
117 | + } |
|
118 | + foreach ($setup_args as $unknown_key => $unknown_config) { |
|
119 | + throw new EE_Error( |
|
120 | + sprintf(esc_html__("The key '%s' is not a known key for registering a model", "event_espresso"), $unknown_key) |
|
121 | + ); |
|
122 | + } |
|
123 | + } |
|
124 | 124 | |
125 | 125 | |
126 | - /** |
|
127 | - * deregister |
|
128 | - * |
|
129 | - * @param string $identifier |
|
130 | - */ |
|
131 | - public static function deregister($identifier = '') |
|
132 | - { |
|
133 | - if (isset(self::$_registry[ $identifier ])) { |
|
134 | - unset(self::$_registry[ $identifier ]); |
|
135 | - foreach (self::$_extensions[ $identifier ] as $extension_of_type) { |
|
136 | - foreach ($extension_of_type as $extension) { |
|
137 | - $extension->deregister(); |
|
138 | - } |
|
139 | - } |
|
140 | - } |
|
141 | - } |
|
126 | + /** |
|
127 | + * deregister |
|
128 | + * |
|
129 | + * @param string $identifier |
|
130 | + */ |
|
131 | + public static function deregister($identifier = '') |
|
132 | + { |
|
133 | + if (isset(self::$_registry[ $identifier ])) { |
|
134 | + unset(self::$_registry[ $identifier ]); |
|
135 | + foreach (self::$_extensions[ $identifier ] as $extension_of_type) { |
|
136 | + foreach ($extension_of_type as $extension) { |
|
137 | + $extension->deregister(); |
|
138 | + } |
|
139 | + } |
|
140 | + } |
|
141 | + } |
|
142 | 142 | } |