Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like EEH_Activation often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EEH_Activation, and based on these observations, apply Extract Interface, too.
1 | <?php if ( ! defined( 'EVENT_ESPRESSO_VERSION')) {exit('No direct script access allowed');} |
||
9 | class EEH_Activation { |
||
10 | |||
11 | /** |
||
12 | * constant used to indicate a cron task is no longer in use |
||
13 | */ |
||
14 | const cron_task_no_longer_in_use = 'no_longer_in_use'; |
||
15 | |||
16 | /** |
||
17 | * option name that will indicate whether or not we still |
||
18 | * need to create EE's folders in the uploads directory |
||
19 | * (because if EE was installed without file system access, |
||
20 | * we need to request credentials before we can create them) |
||
21 | */ |
||
22 | const upload_directories_incomplete_option_name = 'ee_upload_directories_incomplete'; |
||
23 | |||
24 | /** |
||
25 | * WP_User->ID |
||
26 | * |
||
27 | * @var int |
||
28 | */ |
||
29 | private static $_default_creator_id; |
||
30 | |||
31 | /** |
||
32 | * indicates whether or not we've already verified core's default data during this request, |
||
33 | * because after migrations are done, any addons activated while in maintenance mode |
||
34 | * will want to setup their own default data, and they might hook into core's default data |
||
35 | * and trigger core to setup its default data. In which case they might all ask for core to init its default data. |
||
36 | * This prevents doing that for EVERY single addon. |
||
37 | * @var boolean |
||
38 | */ |
||
39 | protected static $_initialized_db_content_already_in_this_request = false; |
||
40 | |||
41 | /** |
||
42 | * @var \EventEspresso\core\services\database\TableAnalysis $table_analysis |
||
43 | */ |
||
44 | private static $table_analysis; |
||
45 | |||
46 | /** |
||
47 | * @var \EventEspresso\core\services\database\TableManager $table_manager |
||
48 | */ |
||
49 | private static $table_manager; |
||
50 | |||
51 | |||
52 | |||
53 | /** |
||
54 | * @return \EventEspresso\core\services\database\TableAnalysis |
||
55 | */ |
||
56 | public static function getTableAnalysis() { |
||
57 | if ( ! self::$table_analysis instanceof \EventEspresso\core\services\database\TableAnalysis ) { |
||
58 | self::$table_analysis = EE_Registry::instance()->create( 'TableAnalysis', array(), true ); |
||
59 | } |
||
60 | return self::$table_analysis; |
||
61 | } |
||
62 | |||
63 | |||
64 | |||
65 | /** |
||
66 | * @return \EventEspresso\core\services\database\TableManager |
||
67 | */ |
||
68 | public static function getTableManager() { |
||
69 | if ( ! self::$table_manager instanceof \EventEspresso\core\services\database\TableManager ) { |
||
70 | self::$table_manager = EE_Registry::instance()->create( 'TableManager', array(), true ); |
||
71 | } |
||
72 | return self::$table_manager; |
||
73 | } |
||
74 | |||
75 | |||
76 | |||
77 | /** |
||
78 | * _ensure_table_name_has_prefix |
||
79 | * @deprecated instead use TableAnalysis::ensureTableNameHasPrefix() |
||
80 | * @access public |
||
81 | * @static |
||
82 | * @param $table_name |
||
83 | * @return string |
||
84 | */ |
||
85 | public static function ensure_table_name_has_prefix( $table_name ) { |
||
86 | return \EEH_Activation::getTableAnalysis()->ensureTableNameHasPrefix( $table_name ); |
||
87 | } |
||
88 | |||
89 | |||
90 | /** |
||
91 | * system_initialization |
||
92 | * ensures the EE configuration settings are loaded with at least default options set |
||
93 | * and that all critical EE pages have been generated with the appropriate shortcodes in place |
||
94 | * |
||
95 | * @access public |
||
96 | * @static |
||
97 | * @return void |
||
98 | */ |
||
99 | public static function system_initialization() { |
||
100 | EEH_Activation::reset_and_update_config(); |
||
101 | //which is fired BEFORE activation of plugin anyways |
||
102 | EEH_Activation::verify_default_pages_exist(); |
||
103 | } |
||
104 | |||
105 | |||
106 | |||
107 | /** |
||
108 | * Sets the database schema and creates folders. This should |
||
109 | * be called on plugin activation and reactivation |
||
110 | * |
||
111 | * @return boolean success, whether the database and folders are setup properly |
||
112 | * @throws \EE_Error |
||
113 | */ |
||
114 | public static function initialize_db_and_folders(){ |
||
115 | $good_filesystem = EEH_Activation::create_upload_directories(); |
||
116 | $good_db = EEH_Activation::create_database_tables(); |
||
117 | return $good_filesystem && $good_db; |
||
118 | } |
||
119 | |||
120 | |||
121 | |||
122 | /** |
||
123 | * assuming we have an up-to-date database schema, this will populate it |
||
124 | * with default and initial data. This should be called |
||
125 | * upon activation of a new plugin, reactivation, and at the end |
||
126 | * of running migration scripts |
||
127 | * |
||
128 | * @throws \EE_Error |
||
129 | */ |
||
130 | public static function initialize_db_content(){ |
||
131 | //let's avoid doing all this logic repeatedly, especially when addons are requesting it |
||
132 | if( EEH_Activation::$_initialized_db_content_already_in_this_request ) { |
||
133 | return; |
||
134 | } |
||
135 | EEH_Activation::$_initialized_db_content_already_in_this_request = true; |
||
136 | |||
137 | EEH_Activation::initialize_system_questions(); |
||
138 | EEH_Activation::insert_default_status_codes(); |
||
139 | EEH_Activation::generate_default_message_templates(); |
||
140 | EEH_Activation::create_no_ticket_prices_array(); |
||
141 | EE_Registry::instance()->CAP->init_caps(); |
||
142 | |||
143 | EEH_Activation::validate_messages_system(); |
||
144 | EEH_Activation::insert_default_payment_methods(); |
||
145 | //in case we've |
||
146 | EEH_Activation::remove_cron_tasks(); |
||
147 | EEH_Activation::create_cron_tasks(); |
||
148 | // remove all TXN locks since that is being done via extra meta now |
||
149 | delete_option( 'ee_locked_transactions' ); |
||
150 | //also, check for CAF default db content |
||
151 | do_action( 'AHEE__EEH_Activation__initialize_db_content' ); |
||
152 | //also: EEM_Gateways::load_all_gateways() outputs a lot of success messages |
||
153 | //which users really won't care about on initial activation |
||
154 | EE_Error::overwrite_success(); |
||
155 | } |
||
156 | |||
157 | |||
158 | |||
159 | |||
160 | /** |
||
161 | * Returns an array of cron tasks. Array values are the actions fired by the cron tasks (the "hooks"), |
||
162 | * values are the frequency (the "recurrence"). See http://codex.wordpress.org/Function_Reference/wp_schedule_event |
||
163 | * If the cron task should NO longer be used, it should have a value of EEH_Activation::cron_task_no_longer_in_use |
||
164 | * (null) |
||
165 | * |
||
166 | * @param string $which_to_include can be 'current' (ones that are currently in use), |
||
167 | * 'old' (only returns ones that should no longer be used),or 'all', |
||
168 | * @return array |
||
169 | * @throws \EE_Error |
||
170 | */ |
||
171 | public static function get_cron_tasks( $which_to_include ) { |
||
172 | $cron_tasks = apply_filters( |
||
173 | 'FHEE__EEH_Activation__get_cron_tasks', |
||
174 | array( |
||
175 | 'AHEE__EE_Cron_Tasks__clean_up_junk_transactions' => 'hourly', |
||
176 | // 'AHEE__EE_Cron_Tasks__finalize_abandoned_transactions' => EEH_Activation::cron_task_no_longer_in_use, actually this is still in use |
||
177 | 'AHEE__EE_Cron_Tasks__update_transaction_with_payment' => EEH_Activation::cron_task_no_longer_in_use, //there may have been a bug which prevented from these cron tasks from getting unscheduled, so we might want to remove these for a few updates |
||
178 | ) |
||
179 | ); |
||
180 | if ( $which_to_include === 'old' ) { |
||
181 | $cron_tasks = array_filter( |
||
182 | $cron_tasks, |
||
183 | function ( $value ) { |
||
184 | return $value === EEH_Activation::cron_task_no_longer_in_use; |
||
185 | } |
||
186 | ); |
||
187 | } elseif ( $which_to_include === 'current' ) { |
||
188 | $cron_tasks = array_filter( $cron_tasks ); |
||
189 | } elseif ( WP_DEBUG && $which_to_include !== 'all' ) { |
||
190 | throw new EE_Error( |
||
191 | sprintf( |
||
192 | __( |
||
193 | 'Invalid argument of "%1$s" passed to EEH_Activation::get_cron_tasks. Valid values are "all", "old" and "current".', |
||
194 | 'event_espresso' |
||
195 | ), |
||
196 | $which_to_include |
||
197 | ) |
||
198 | ); |
||
199 | } |
||
200 | return $cron_tasks; |
||
201 | } |
||
202 | |||
203 | |||
204 | |||
205 | /** |
||
206 | * Ensure cron tasks are setup (the removal of crons should be done by remove_crons()) |
||
207 | * |
||
208 | * @throws \EE_Error |
||
209 | */ |
||
210 | public static function create_cron_tasks() { |
||
211 | |||
212 | foreach( EEH_Activation::get_cron_tasks( 'current' ) as $hook_name => $frequency ) { |
||
213 | if( ! wp_next_scheduled( $hook_name ) ) { |
||
214 | wp_schedule_event( time(), $frequency, $hook_name ); |
||
215 | } |
||
216 | } |
||
217 | |||
218 | } |
||
219 | |||
220 | |||
221 | |||
222 | /** |
||
223 | * Remove the currently-existing and now-removed cron tasks. |
||
224 | * |
||
225 | * @param boolean $remove_all whether to only remove the old ones, or remove absolutely ALL the EE ones |
||
226 | * @throws \EE_Error |
||
227 | */ |
||
228 | public static function remove_cron_tasks( $remove_all = true ) { |
||
229 | $cron_tasks_to_remove = $remove_all ? 'all' : 'old'; |
||
230 | $crons = _get_cron_array(); |
||
231 | $crons = is_array( $crons ) ? $crons : array(); |
||
232 | /* reminder of what $crons look like: |
||
233 | * Top-level keys are timestamps, and their values are arrays. |
||
234 | * The 2nd level arrays have keys with each of the cron task hook names to run at that time |
||
235 | * and their values are arrays. |
||
236 | * The 3rd level level arrays are keys which are hashes of the cron task's arguments, |
||
237 | * and their values are the UN-hashed arguments |
||
238 | * eg |
||
239 | * array (size=13) |
||
240 | * 1429903276 => |
||
241 | * array (size=1) |
||
242 | * 'AHEE__EE_Cron_Tasks__update_transaction_with_payment' => |
||
243 | * array (size=1) |
||
244 | * '561299d6e42c8e079285870ade0e47e6' => |
||
245 | * array (size=2) |
||
246 | * ... |
||
247 | * ... |
||
248 | */ |
||
249 | $ee_cron_tasks_to_remove = EEH_Activation::get_cron_tasks( $cron_tasks_to_remove ); |
||
250 | foreach ( $crons as $timestamp => $hooks_to_fire_at_time ) { |
||
251 | if ( is_array( $hooks_to_fire_at_time ) ) { |
||
252 | foreach ( $hooks_to_fire_at_time as $hook_name => $hook_actions ) { |
||
253 | if ( isset( $ee_cron_tasks_to_remove[ $hook_name ] ) |
||
254 | && is_array( $ee_cron_tasks_to_remove[ $hook_name ] ) |
||
255 | ) { |
||
256 | unset( $crons[ $timestamp ][ $hook_name ] ); |
||
257 | } |
||
258 | } |
||
259 | //also take care of any empty cron timestamps. |
||
260 | if ( empty( $hooks_to_fire_at_time ) ) { |
||
261 | unset( $crons[ $timestamp ] ); |
||
262 | } |
||
263 | } |
||
264 | } |
||
265 | _set_cron_array( $crons ); |
||
266 | } |
||
267 | |||
268 | |||
269 | |||
270 | /** |
||
271 | * CPT_initialization |
||
272 | * registers all EE CPTs ( Custom Post Types ) then flushes rewrite rules so that all endpoints exist |
||
273 | * |
||
274 | * @access public |
||
275 | * @static |
||
276 | * @return void |
||
277 | */ |
||
278 | public static function CPT_initialization() { |
||
279 | // register Custom Post Types |
||
280 | EE_Registry::instance()->load_core( 'Register_CPTs' ); |
||
281 | flush_rewrite_rules(); |
||
282 | } |
||
283 | |||
284 | |||
285 | |||
286 | /** |
||
287 | * reset_and_update_config |
||
288 | * |
||
289 | * The following code was moved over from EE_Config so that it will no longer run on every request. |
||
290 | * If there is old calendar config data saved, then it will get converted on activation. |
||
291 | * This was basically a DMS before we had DMS's, and will get removed after a few more versions. |
||
292 | * |
||
293 | * @access public |
||
294 | * @static |
||
295 | * @return void |
||
296 | */ |
||
297 | public static function reset_and_update_config() { |
||
298 | do_action( 'AHEE__EE_Config___load_core_config__start', array( 'EEH_Activation', 'load_calendar_config' ) ); |
||
299 | add_filter( 'FHEE__EE_Config___load_core_config__config_settings', array( 'EEH_Activation', 'migrate_old_config_data' ), 10, 3 ); |
||
300 | //EE_Config::reset(); |
||
301 | } |
||
302 | |||
303 | |||
304 | /** |
||
305 | * load_calendar_config |
||
306 | * |
||
307 | * @access public |
||
308 | * @return void |
||
309 | */ |
||
310 | public static function load_calendar_config() { |
||
311 | // grab array of all plugin folders and loop thru it |
||
312 | $plugins = glob( WP_PLUGIN_DIR . DS . '*', GLOB_ONLYDIR ); |
||
313 | if ( empty( $plugins ) ) { |
||
314 | return; |
||
315 | } |
||
316 | foreach ( $plugins as $plugin_path ) { |
||
317 | // grab plugin folder name from path |
||
318 | $plugin = basename( $plugin_path ); |
||
319 | // drill down to Espresso plugins |
||
320 | // then to calendar related plugins |
||
321 | if ( |
||
322 | strpos( $plugin, 'espresso' ) !== FALSE |
||
323 | || strpos( $plugin, 'Espresso' ) !== FALSE |
||
324 | || strpos( $plugin, 'ee4' ) !== FALSE |
||
325 | || strpos( $plugin, 'EE4' ) !== FALSE |
||
326 | || strpos( $plugin, 'calendar' ) !== false |
||
327 | ) { |
||
328 | // this is what we are looking for |
||
329 | $calendar_config = $plugin_path . DS . 'EE_Calendar_Config.php'; |
||
330 | // does it exist in this folder ? |
||
331 | if ( is_readable( $calendar_config )) { |
||
332 | // YEAH! let's load it |
||
333 | require_once( $calendar_config ); |
||
334 | } |
||
335 | } |
||
336 | } |
||
337 | } |
||
338 | |||
339 | |||
340 | /** |
||
341 | * _migrate_old_config_data |
||
342 | * |
||
343 | * @access public |
||
344 | * @param array|stdClass $settings |
||
345 | * @param string $config |
||
346 | * @param \EE_Config $EE_Config |
||
347 | * @return \stdClass |
||
348 | */ |
||
349 | public static function migrate_old_config_data( $settings = array(), $config = '', EE_Config $EE_Config ) { |
||
350 | $convert_from_array = array( 'addons' ); |
||
351 | // in case old settings were saved as an array |
||
352 | if ( is_array( $settings ) && in_array( $config, $convert_from_array )) { |
||
353 | // convert existing settings to an object |
||
354 | $config_array = $settings; |
||
355 | $settings = new stdClass(); |
||
356 | foreach ( $config_array as $key => $value ){ |
||
357 | if ( $key === 'calendar' && class_exists( 'EE_Calendar_Config' )) { |
||
358 | $EE_Config->set_config( 'addons', 'EE_Calendar', 'EE_Calendar_Config', $value ); |
||
359 | } else { |
||
360 | $settings->{$key} = $value; |
||
361 | } |
||
362 | } |
||
363 | add_filter( 'FHEE__EE_Config___load_core_config__update_espresso_config', '__return_true' ); |
||
364 | } |
||
365 | return $settings; |
||
|
|||
366 | } |
||
367 | |||
368 | |||
369 | |||
370 | /** |
||
371 | * deactivate_event_espresso |
||
372 | * |
||
373 | * @access public |
||
374 | * @static |
||
375 | * @return void |
||
376 | */ |
||
377 | public static function deactivate_event_espresso() { |
||
378 | // check permissions |
||
379 | if ( current_user_can( 'activate_plugins' )) { |
||
380 | deactivate_plugins( EE_PLUGIN_BASENAME, TRUE ); |
||
381 | } |
||
382 | } |
||
383 | |||
384 | |||
385 | |||
386 | |||
387 | |||
388 | /** |
||
389 | * verify_default_pages_exist |
||
390 | * |
||
391 | * @access public |
||
392 | * @static |
||
393 | * @return void |
||
394 | */ |
||
395 | public static function verify_default_pages_exist() { |
||
396 | |||
397 | $critical_page_problem = FALSE; |
||
398 | |||
399 | $critical_pages = array( |
||
400 | array( |
||
401 | 'id' =>'reg_page_id', |
||
402 | 'name' => __( 'Registration Checkout', 'event_espresso' ), |
||
403 | 'post' => NULL, |
||
404 | 'code' => 'ESPRESSO_CHECKOUT' |
||
405 | ), |
||
406 | array( |
||
407 | 'id' => 'txn_page_id', |
||
408 | 'name' => __( 'Transactions', 'event_espresso' ), |
||
409 | 'post' => NULL, |
||
410 | 'code' => 'ESPRESSO_TXN_PAGE' |
||
411 | ), |
||
412 | array( |
||
413 | 'id' => 'thank_you_page_id', |
||
414 | 'name' => __( 'Thank You', 'event_espresso' ), |
||
415 | 'post' => NULL, |
||
416 | 'code' => 'ESPRESSO_THANK_YOU' |
||
417 | ), |
||
418 | array( |
||
419 | 'id' => 'cancel_page_id', |
||
420 | 'name' => __( 'Registration Cancelled', 'event_espresso' ), |
||
421 | 'post' => NULL, |
||
422 | 'code' => 'ESPRESSO_CANCELLED' |
||
423 | ), |
||
424 | ); |
||
425 | |||
426 | $EE_Core_Config = EE_Registry::instance()->CFG->core; |
||
427 | |||
428 | foreach ( $critical_pages as $critical_page ) { |
||
429 | // is critical page ID set in config ? |
||
430 | if ( $EE_Core_Config->{$critical_page[ 'id' ]} !== FALSE ) { |
||
431 | // attempt to find post by ID |
||
432 | $critical_page['post'] = get_post( $EE_Core_Config->{$critical_page[ 'id' ]} ); |
||
433 | } |
||
434 | // no dice? |
||
435 | if ( $critical_page['post'] === null ) { |
||
436 | // attempt to find post by title |
||
437 | $critical_page['post'] = self::get_page_by_ee_shortcode( $critical_page['code'] ); |
||
438 | // still nothing? |
||
439 | if ( $critical_page['post'] === null ) { |
||
440 | $critical_page = EEH_Activation::create_critical_page( $critical_page ); |
||
441 | // REALLY? Still nothing ??!?!? |
||
442 | if ( $critical_page['post'] === null ) { |
||
443 | $msg = __( 'The Event Espresso critical page configuration settings could not be updated.', 'event_espresso' ); |
||
444 | EE_Error::add_error( $msg, __FILE__, __FUNCTION__, __LINE__ ); |
||
445 | break; |
||
446 | } |
||
447 | } |
||
448 | } |
||
449 | // track post_shortcodes |
||
450 | if ( $critical_page['post'] ) { |
||
451 | EEH_Activation::_track_critical_page_post_shortcodes( $critical_page ); |
||
452 | } |
||
453 | // check that Post ID matches critical page ID in config |
||
454 | if ( |
||
455 | isset( $critical_page['post']->ID ) |
||
456 | && $critical_page['post']->ID !== $EE_Core_Config->{$critical_page[ 'id' ]} |
||
457 | ) { |
||
458 | //update Config with post ID |
||
459 | $EE_Core_Config->{$critical_page[ 'id' ]} = $critical_page['post']->ID; |
||
460 | if ( ! EE_Config::instance()->update_espresso_config( FALSE, FALSE ) ) { |
||
461 | $msg = __( 'The Event Espresso critical page configuration settings could not be updated.', 'event_espresso' ); |
||
462 | EE_Error::add_error( $msg, __FILE__, __FUNCTION__, __LINE__ ); |
||
463 | } |
||
464 | } |
||
465 | |||
466 | $critical_page_problem = |
||
467 | ! isset( $critical_page['post']->post_status ) |
||
468 | || $critical_page['post']->post_status !== 'publish' |
||
469 | || strpos( $critical_page['post']->post_content, $critical_page['code'] ) === FALSE |
||
470 | ? TRUE |
||
471 | : $critical_page_problem; |
||
472 | |||
473 | } |
||
474 | |||
475 | if ( $critical_page_problem ) { |
||
476 | $msg = sprintf( |
||
477 | __('A potential issue has been detected with one or more of your Event Espresso pages. Go to %s to view your Event Espresso pages.', 'event_espresso' ), |
||
478 | '<a href="' . admin_url('admin.php?page=espresso_general_settings&action=critical_pages') . '">' . __('Event Espresso Critical Pages Settings', 'event_espresso') . '</a>' |
||
479 | ); |
||
480 | EE_Error::add_persistent_admin_notice( 'critical_page_problem', $msg ); |
||
481 | } |
||
482 | if ( EE_Error::has_notices() ) { |
||
483 | EE_Error::get_notices( FALSE, TRUE, TRUE ); |
||
484 | } |
||
485 | } |
||
486 | |||
487 | /** |
||
488 | * Returns the first post which uses the specified shortcode |
||
489 | * @param string $ee_shortcode usually one of the critical pages shortcodes, eg |
||
490 | * ESPRESSO_THANK_YOU. So we will search fora post with the content "[ESPRESSO_THANK_YOU" |
||
491 | * (we don't search for the closing shortcode bracket because they might have added |
||
492 | * parameter to the shortcode |
||
493 | * @return WP_Post or NULl |
||
494 | */ |
||
495 | public static function get_page_by_ee_shortcode($ee_shortcode){ |
||
496 | global $wpdb; |
||
497 | $shortcode_and_opening_bracket = '['.$ee_shortcode; |
||
498 | $post_id = $wpdb->get_var("SELECT ID FROM {$wpdb->posts} WHERE post_content LIKE '%$shortcode_and_opening_bracket%' LIMIT 1"); |
||
499 | if($post_id){ |
||
500 | return get_post($post_id); |
||
501 | }else{ |
||
502 | return NULL; |
||
503 | } |
||
504 | |||
505 | // return $post_id; |
||
506 | } |
||
507 | |||
508 | |||
509 | |||
510 | /** |
||
511 | * This function generates a post for critical espresso pages |
||
512 | * |
||
513 | * @access public |
||
514 | * @static |
||
515 | * @param array $critical_page |
||
516 | * @return array |
||
517 | */ |
||
518 | public static function create_critical_page( $critical_page ) { |
||
519 | |||
520 | $post_args = array( |
||
521 | 'post_title' => $critical_page['name'], |
||
522 | 'post_status' => 'publish', |
||
523 | 'post_type' => 'page', |
||
524 | 'comment_status' => 'closed', |
||
525 | 'post_content' => '[' . $critical_page['code'] . ']' |
||
526 | ); |
||
527 | |||
528 | $post_id = wp_insert_post( $post_args ); |
||
529 | if ( ! $post_id ) { |
||
530 | $msg = sprintf( |
||
531 | __( 'The Event Espresso critical page entitled "%s" could not be created.', 'event_espresso' ), |
||
532 | $critical_page['name'] |
||
533 | ); |
||
534 | EE_Error::add_error( $msg, __FILE__, __FUNCTION__, __LINE__ ); |
||
535 | return $critical_page; |
||
536 | } |
||
537 | // get newly created post's details |
||
538 | if ( ! $critical_page['post'] = get_post( $post_id )) { |
||
539 | $msg = sprintf( |
||
540 | __( 'The Event Espresso critical page entitled "%s" could not be retrieved.', 'event_espresso' ), |
||
541 | $critical_page['name'] |
||
542 | ); |
||
543 | EE_Error::add_error( $msg, __FILE__, __FUNCTION__, __LINE__ ); |
||
544 | } |
||
545 | |||
546 | return $critical_page; |
||
547 | |||
548 | } |
||
549 | |||
550 | |||
551 | |||
552 | |||
553 | |||
554 | /** |
||
555 | * This function adds a critical page's shortcode to the post_shortcodes array |
||
556 | * |
||
557 | * @access private |
||
558 | * @static |
||
559 | * @param array $critical_page |
||
560 | * @return void |
||
561 | */ |
||
562 | private static function _track_critical_page_post_shortcodes( $critical_page = array() ) { |
||
563 | // check the goods |
||
564 | View Code Duplication | if ( ! $critical_page['post'] instanceof WP_Post ) { |
|
565 | $msg = sprintf( |
||
566 | __( 'The Event Espresso critical page shortcode for the page %s can not be tracked because it is not a WP_Post object.', 'event_espresso' ), |
||
567 | $critical_page['name'] |
||
568 | ); |
||
569 | EE_Error::add_error( $msg, __FILE__, __FUNCTION__, __LINE__ ); |
||
570 | return; |
||
571 | } |
||
572 | $EE_Core_Config = EE_Registry::instance()->CFG->core; |
||
573 | // map shortcode to post |
||
574 | $EE_Core_Config->post_shortcodes[ $critical_page['post']->post_name ][ $critical_page['code'] ] = $critical_page['post']->ID; |
||
575 | // and make sure it's NOT added to the WP "Posts Page" |
||
576 | // name of the WP Posts Page |
||
577 | $posts_page = EE_Config::get_page_for_posts(); |
||
578 | if ( isset( $EE_Core_Config->post_shortcodes[ $posts_page ] )) { |
||
579 | unset( $EE_Core_Config->post_shortcodes[ $posts_page ][ $critical_page['code'] ] ); |
||
580 | } |
||
581 | if ( $posts_page !== 'posts' && isset( $EE_Core_Config->post_shortcodes['posts'] )) { |
||
582 | unset( $EE_Core_Config->post_shortcodes['posts'][ $critical_page['code'] ] ); |
||
583 | } |
||
584 | // update post_shortcode CFG |
||
585 | if ( ! EE_Config::instance()->update_espresso_config( FALSE, FALSE )) { |
||
586 | $msg = sprintf( |
||
587 | __( 'The Event Espresso critical page shortcode for the %s page could not be configured properly.', 'event_espresso' ), |
||
588 | $critical_page['name'] |
||
589 | ); |
||
590 | EE_Error::add_error( $msg, __FILE__, __FUNCTION__, __LINE__ ); |
||
591 | } |
||
592 | } |
||
593 | |||
594 | |||
595 | |||
596 | /** |
||
597 | * Tries to find the oldest admin for this site. If there are no admins for this site then return NULL. |
||
598 | * The role being used to check is filterable. |
||
599 | * |
||
600 | * @since 4.6.0 |
||
601 | * @global WPDB $wpdb |
||
602 | * |
||
603 | * @return mixed null|int WP_user ID or NULL |
||
604 | */ |
||
605 | public static function get_default_creator_id() { |
||
606 | global $wpdb; |
||
607 | |||
608 | if ( ! empty( self::$_default_creator_id ) ) { |
||
609 | return self::$_default_creator_id; |
||
610 | }/**/ |
||
611 | |||
612 | $role_to_check = apply_filters( 'FHEE__EEH_Activation__get_default_creator_id__role_to_check', 'administrator' ); |
||
613 | |||
614 | //let's allow pre_filtering for early exits by alternative methods for getting id. We check for truthy result and if so then exit early. |
||
615 | $pre_filtered_id = apply_filters( 'FHEE__EEH_Activation__get_default_creator_id__pre_filtered_id', false, $role_to_check ); |
||
616 | if ( $pre_filtered_id !== false ) { |
||
617 | return (int) $pre_filtered_id; |
||
618 | } |
||
619 | |||
620 | $capabilities_key = \EEH_Activation::getTableAnalysis()->ensureTableNameHasPrefix( 'capabilities' ); |
||
621 | $query = $wpdb->prepare( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$capabilities_key' AND meta_value LIKE %s ORDER BY user_id ASC LIMIT 0,1", '%' . $role_to_check . '%' ); |
||
622 | $user_id = $wpdb->get_var( $query ); |
||
623 | $user_id = apply_filters( 'FHEE__EEH_Activation_Helper__get_default_creator_id__user_id', $user_id ); |
||
624 | if ( $user_id && (int)$user_id ) { |
||
625 | self::$_default_creator_id = (int)$user_id; |
||
626 | return self::$_default_creator_id; |
||
627 | } else { |
||
628 | return NULL; |
||
629 | } |
||
630 | } |
||
631 | |||
632 | |||
633 | |||
634 | |||
635 | |||
636 | /** |
||
637 | * used by EE and EE addons during plugin activation to create tables. |
||
638 | * Its a wrapper for EventEspresso\core\services\database\TableManager::createTable, |
||
639 | * but includes extra logic regarding activations. |
||
640 | * |
||
641 | * @access public |
||
642 | * @static |
||
643 | * @param string $table_name without the $wpdb->prefix |
||
644 | * @param string $sql SQL for creating the table (contents between brackets in an SQL create table query) |
||
645 | * @param string $engine like 'ENGINE=MyISAM' or 'ENGINE=InnoDB' |
||
646 | * @param boolean $drop_pre_existing_table set to TRUE when you want to make SURE the table is completely empty |
||
647 | * and new once this function is done (ie, you really do want to CREATE a table, and |
||
648 | * expect it to be empty once you're done) |
||
649 | * leave as FALSE when you just want to verify the table exists and matches this definition (and if it |
||
650 | * HAS data in it you want to leave it be) |
||
651 | * @return void |
||
652 | * @throws EE_Error if there are database errors |
||
653 | */ |
||
654 | public static function create_table( $table_name, $sql, $engine = 'ENGINE=MyISAM ', $drop_pre_existing_table = false ) { |
||
655 | if( apply_filters( 'FHEE__EEH_Activation__create_table__short_circuit', FALSE, $table_name, $sql ) ){ |
||
656 | return; |
||
657 | } |
||
658 | do_action( 'AHEE_log', __FILE__, __FUNCTION__, '' ); |
||
659 | if ( ! function_exists( 'dbDelta' )) { |
||
660 | require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
||
661 | } |
||
662 | $tableAnalysis = \EEH_Activation::getTableAnalysis(); |
||
663 | $wp_table_name = $tableAnalysis->ensureTableNameHasPrefix( $table_name ); |
||
664 | // do we need to first delete an existing version of this table ? |
||
665 | if ( $drop_pre_existing_table && $tableAnalysis->tableExists( $wp_table_name ) ){ |
||
666 | // ok, delete the table... but ONLY if it's empty |
||
667 | $deleted_safely = EEH_Activation::delete_db_table_if_empty( $wp_table_name ); |
||
668 | // table is NOT empty, are you SURE you want to delete this table ??? |
||
669 | if ( ! $deleted_safely && defined( 'EE_DROP_BAD_TABLES' ) && EE_DROP_BAD_TABLES ){ |
||
670 | \EEH_Activation::getTableManager()->dropTable( $wp_table_name ); |
||
671 | } else if ( ! $deleted_safely ) { |
||
672 | // so we should be more cautious rather than just dropping tables so easily |
||
673 | EE_Error::add_persistent_admin_notice( |
||
674 | 'bad_table_' . $wp_table_name . '_detected', |
||
675 | sprintf( __( 'Database table %1$s exists when it shouldn\'t, and may contain erroneous data. If you have previously restored your database from a backup that didn\'t remove the old tables, then we recommend adding %2$s to your %3$s file then restore to that backup again. This will clear out the invalid data from %1$s. Afterwards you should undo that change from your %3$s file. %4$sIf you cannot edit %3$s, you should remove the data from %1$s manually then restore to the backup again.', 'event_espresso' ), |
||
676 | $wp_table_name, |
||
677 | "<pre>define( 'EE_DROP_BAD_TABLES', TRUE );</pre>", |
||
678 | '<b>wp-config.php</b>', |
||
679 | '<br/>'), |
||
680 | TRUE ); |
||
681 | } |
||
682 | } |
||
683 | $engine = str_replace( 'ENGINE=', '', $engine ); |
||
684 | \EEH_Activation::getTableManager()->createTable( $table_name, $sql, $engine ); |
||
685 | } |
||
686 | |||
687 | |||
688 | |||
689 | /** |
||
690 | * add_column_if_it_doesn't_exist |
||
691 | * Checks if this column already exists on the specified table. Handy for addons which want to add a column |
||
692 | * |
||
693 | * @access public |
||
694 | * @static |
||
695 | * @deprecated instead use TableManager::addColumn() |
||
696 | * @param string $table_name (without "wp_", eg "esp_attendee" |
||
697 | * @param string $column_name |
||
698 | * @param string $column_info if your SQL were 'ALTER TABLE table_name ADD price VARCHAR(10)', this would be |
||
699 | * 'VARCHAR(10)' |
||
700 | * @return bool|int |
||
701 | */ |
||
702 | public static function add_column_if_it_doesnt_exist($table_name,$column_name,$column_info='INT UNSIGNED NOT NULL'){ |
||
703 | return \EEH_Activation::getTableManager()->addColumn( $table_name, $column_name, $column_info ); |
||
704 | } |
||
705 | |||
706 | |||
707 | |||
708 | |||
709 | /** |
||
710 | * get_fields_on_table |
||
711 | * Gets all the fields on the database table. |
||
712 | * |
||
713 | * @access public |
||
714 | * @deprecated instead use TableManager::getTableColumns() |
||
715 | * @static |
||
716 | * @param string $table_name, without prefixed $wpdb->prefix |
||
717 | * @return array of database column names |
||
718 | */ |
||
719 | public static function get_fields_on_table( $table_name = NULL ) { |
||
720 | return \EEH_Activation::getTableManager()->getTableColumns( $table_name ); |
||
721 | } |
||
722 | |||
723 | |||
724 | |||
725 | /** |
||
726 | * db_table_is_empty |
||
727 | * |
||
728 | * @access public\ |
||
729 | * @deprecated instead use TableAnalysis::tableIsEmpty() |
||
730 | * @static |
||
731 | * @param string $table_name |
||
732 | * @return bool |
||
733 | */ |
||
734 | public static function db_table_is_empty( $table_name ) { |
||
735 | return \EEH_Activation::getTableAnalysis()->tableIsEmpty( $table_name ); |
||
736 | } |
||
737 | |||
738 | |||
739 | |||
740 | /** |
||
741 | * delete_db_table_if_empty |
||
742 | * |
||
743 | * @access public |
||
744 | * @static |
||
745 | * @param string $table_name |
||
746 | * @return bool | int |
||
747 | */ |
||
748 | public static function delete_db_table_if_empty( $table_name ) { |
||
754 | |||
755 | |||
756 | |||
757 | /** |
||
758 | * delete_unused_db_table |
||
759 | * |
||
760 | * @access public |
||
761 | * @static |
||
762 | * @deprecated instead use TableManager::dropTable() |
||
763 | * @param string $table_name |
||
764 | * @return bool | int |
||
765 | */ |
||
766 | public static function delete_unused_db_table( $table_name ) { |
||
767 | return \EEH_Activation::getTableManager()->dropTable( $table_name ); |
||
768 | } |
||
769 | |||
770 | |||
771 | |||
772 | /** |
||
773 | * drop_index |
||
774 | * |
||
775 | * @access public |
||
776 | * @static |
||
777 | * @deprecated instead use TableManager::dropIndex() |
||
778 | * @param string $table_name |
||
779 | * @param string $index_name |
||
780 | * @return bool | int |
||
781 | */ |
||
782 | public static function drop_index( $table_name, $index_name ) { |
||
785 | |||
786 | |||
787 | |||
788 | /** |
||
789 | * create_database_tables |
||
790 | * |
||
791 | * @access public |
||
792 | * @static |
||
793 | * @throws EE_Error |
||
794 | * @return boolean success (whether database is setup properly or not) |
||
795 | */ |
||
796 | public static function create_database_tables() { |
||
822 | |||
823 | |||
824 | |||
825 | |||
826 | |||
827 | /** |
||
828 | * initialize_system_questions |
||
829 | * |
||
830 | * @access public |
||
831 | * @static |
||
832 | * @return void |
||
833 | */ |
||
834 | public static function initialize_system_questions() { |
||
1133 | |||
1134 | |||
1135 | |||
1136 | /** |
||
1137 | * Makes sure the default payment method (Invoice) is active. |
||
1138 | * This used to be done automatically as part of constructing the old gateways config |
||
1139 | * |
||
1140 | * @throws \EE_Error |
||
1141 | */ |
||
1142 | public static function insert_default_payment_methods(){ |
||
1150 | |||
1151 | /** |
||
1152 | * insert_default_status_codes |
||
1153 | * |
||
1154 | * @access public |
||
1155 | * @static |
||
1156 | * @return void |
||
1157 | */ |
||
1158 | public static function insert_default_status_codes() { |
||
1212 | |||
1213 | |||
1214 | |||
1215 | |||
1216 | |||
1217 | |||
1218 | |||
1219 | |||
1220 | |||
1221 | |||
1222 | |||
1223 | |||
1224 | |||
1225 | /** |
||
1226 | * create_upload_directories |
||
1227 | * Creates folders in the uploads directory to facilitate addons and templates |
||
1228 | * |
||
1229 | * @access public |
||
1230 | * @static |
||
1231 | * @return boolean success of verifying upload directories exist |
||
1232 | */ |
||
1233 | public static function create_upload_directories() { |
||
1267 | |||
1268 | /** |
||
1269 | * Whether the upload directories need to be fixed or not. |
||
1270 | * If EE is installed but filesystem access isn't initially available, |
||
1271 | * we need to get the user's filesystem credentials and THEN create them, |
||
1272 | * so there might be period of time when EE is installed but its |
||
1273 | * upload directories aren't available. This indicates such a state |
||
1274 | * @return boolean |
||
1275 | */ |
||
1276 | public static function upload_directories_incomplete() { |
||
1279 | |||
1280 | |||
1281 | |||
1282 | /** |
||
1283 | * generate_default_message_templates |
||
1284 | * |
||
1285 | * @static |
||
1286 | * @throws EE_Error |
||
1287 | * @return bool true means new templates were created. |
||
1288 | * false means no templates were created. |
||
1289 | * This is NOT an error flag. To check for errors you will want |
||
1290 | * to use either EE_Error or a try catch for an EE_Error exception. |
||
1291 | */ |
||
1292 | public static function generate_default_message_templates() { |
||
1315 | |||
1316 | |||
1317 | |||
1318 | /** |
||
1319 | * @param \EE_Message_Resource_Manager $message_resource_manager |
||
1320 | * @return array|bool |
||
1321 | * @throws \EE_Error |
||
1322 | */ |
||
1323 | protected static function _activate_new_message_types_for_active_messengers_and_generate_default_templates( |
||
1369 | |||
1370 | |||
1371 | |||
1372 | /** |
||
1373 | * This will activate and generate default messengers and default message types for those messengers. |
||
1374 | * |
||
1375 | * @param EE_message_Resource_Manager $message_resource_manager |
||
1376 | * @return array|bool True means there were default messengers and message type templates generated. |
||
1377 | * False means that there were no templates generated |
||
1378 | * (which could simply mean there are no default message types for a messenger). |
||
1379 | * @throws EE_Error |
||
1380 | */ |
||
1381 | protected static function _activate_and_generate_default_messengers_and_message_templates( |
||
1421 | |||
1422 | |||
1423 | |||
1424 | /** |
||
1425 | * This returns the default messengers to generate templates for on activation of EE. |
||
1426 | * It considers: |
||
1427 | * - whether a messenger is already active in the db. |
||
1428 | * - whether a messenger has been made active at any time in the past. |
||
1429 | * |
||
1430 | * @static |
||
1431 | * @param EE_Message_Resource_Manager $message_resource_manager |
||
1432 | * @return EE_messenger[] |
||
1433 | */ |
||
1434 | protected static function _get_default_messengers_to_generate_on_activation( |
||
1457 | |||
1458 | |||
1459 | |||
1460 | |||
1461 | |||
1462 | |||
1463 | /** |
||
1464 | * This simply validates active message types to ensure they actually match installed |
||
1465 | * message types. If there's a mismatch then we deactivate the message type and ensure all related db |
||
1466 | * rows are set inactive. |
||
1467 | * |
||
1468 | * Note: Messengers are no longer validated here as of 4.9.0 because they get validated automatically whenever |
||
1469 | * EE_Messenger_Resource_Manager is constructed. Message Types are a bit more resource heavy for validation so they |
||
1470 | * are still handled in here. |
||
1471 | * |
||
1472 | * @since 4.3.1 |
||
1473 | * |
||
1474 | * @return void |
||
1475 | */ |
||
1476 | public static function validate_messages_system() { |
||
1482 | |||
1483 | |||
1484 | |||
1485 | |||
1486 | /** |
||
1487 | * create_no_ticket_prices_array |
||
1488 | * |
||
1489 | * @access public |
||
1490 | * @static |
||
1491 | * @return void |
||
1492 | */ |
||
1493 | public static function create_no_ticket_prices_array(){ |
||
1501 | |||
1502 | |||
1503 | |||
1504 | /** |
||
1505 | * plugin_deactivation |
||
1506 | * |
||
1507 | * @access public |
||
1508 | * @static |
||
1509 | * @return void |
||
1510 | */ |
||
1511 | public static function plugin_deactivation() { |
||
1513 | |||
1514 | |||
1515 | |||
1516 | /** |
||
1517 | * Finds all our EE4 custom post types, and deletes them and their associated data |
||
1518 | * (like post meta or term relations) |
||
1519 | * |
||
1520 | * @global wpdb $wpdb |
||
1521 | * @throws \EE_Error |
||
1522 | */ |
||
1523 | public static function delete_all_espresso_cpt_data(){ |
||
1543 | |||
1544 | /** |
||
1545 | * Deletes all EE custom tables |
||
1546 | * |
||
1547 | * @return array |
||
1548 | */ |
||
1549 | public static function drop_espresso_tables() { |
||
1585 | |||
1586 | /** |
||
1587 | * Drops all the tables mentioned in a single MYSQL query. Double-checks |
||
1588 | * each table name provided has a wpdb prefix attached, and that it exists. |
||
1589 | * Returns the list actually deleted |
||
1590 | * @deprecated in 4.9.13. Instead use TableManager::dropTables() |
||
1591 | * @global WPDB $wpdb |
||
1592 | * @param array $table_names |
||
1593 | * @return array of table names which we deleted |
||
1594 | */ |
||
1595 | public static function drop_tables( $table_names ) { |
||
1598 | /** |
||
1599 | * plugin_uninstall |
||
1600 | * |
||
1601 | * @access public |
||
1602 | * @static |
||
1603 | * @param bool $remove_all |
||
1604 | * @return void |
||
1605 | */ |
||
1606 | public static function delete_all_espresso_tables_and_data( $remove_all = true ) { |
||
1690 | |||
1691 | /** |
||
1692 | * Gets the mysql error code from the last used query by wpdb |
||
1693 | * @return int mysql error code, see https://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html |
||
1694 | */ |
||
1695 | public static function last_wpdb_error_code() { |
||
1703 | |||
1704 | /** |
||
1705 | * Checks that the database table exists. Also works on temporary tables (for unit tests mostly). |
||
1706 | * @global wpdb $wpdb |
||
1707 | * @deprecated instead use TableAnalysis::tableExists() |
||
1708 | * @param string $table_name with or without $wpdb->prefix |
||
1709 | * @return boolean |
||
1710 | */ |
||
1711 | public static function table_exists( $table_name ){ |
||
1714 | |||
1715 | /** |
||
1716 | * Resets the cache on EEH_Activation |
||
1717 | */ |
||
1718 | public static function reset(){ |
||
1722 | } |
||
1723 | // End of file EEH_Activation.helper.php |
||
1725 |