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 EE_Front_Controller 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 EE_Front_Controller, and based on these observations, apply Extract Interface, too.
| 1 | <?php if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
||
| 22 | final class EE_Front_Controller |
||
| 23 | { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * $_template_path |
||
| 27 | * @var string $_template_path |
||
| 28 | * @access public |
||
| 29 | */ |
||
| 30 | private $_template_path; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * $_template |
||
| 34 | * @var string $_template |
||
| 35 | * @access public |
||
| 36 | */ |
||
| 37 | private $_template; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @type EE_Registry $Registry |
||
| 41 | * @access protected |
||
| 42 | */ |
||
| 43 | protected $Registry; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @type EE_Request_Handler $Request_Handler |
||
| 47 | * @access protected |
||
| 48 | */ |
||
| 49 | protected $Request_Handler; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @type EE_Module_Request_Router $Module_Request_Router |
||
| 53 | * @access protected |
||
| 54 | */ |
||
| 55 | protected $Module_Request_Router; |
||
| 56 | |||
| 57 | |||
| 58 | /** |
||
| 59 | * class constructor |
||
| 60 | * should fire after shortcode, module, addon, or other plugin's default priority init phases have run |
||
| 61 | * |
||
| 62 | * @access public |
||
| 63 | * @param \EE_Registry $Registry |
||
| 64 | * @param \EE_Request_Handler $Request_Handler |
||
| 65 | * @param \EE_Module_Request_Router $Module_Request_Router |
||
| 66 | */ |
||
| 67 | public function __construct( |
||
| 109 | |||
| 110 | |||
| 111 | /** |
||
| 112 | * @return EE_Request_Handler |
||
| 113 | */ |
||
| 114 | public function Request_Handler() |
||
| 115 | { |
||
| 116 | return $this->Request_Handler; |
||
| 117 | } |
||
| 118 | |||
| 119 | |||
| 120 | /** |
||
| 121 | * @return EE_Module_Request_Router |
||
| 122 | */ |
||
| 123 | public function Module_Request_Router() |
||
| 124 | { |
||
| 125 | return $this->Module_Request_Router; |
||
| 126 | } |
||
| 127 | |||
| 128 | |||
| 129 | |||
| 130 | |||
| 131 | |||
| 132 | /*********************************************** INIT ACTION HOOK ***********************************************/ |
||
| 133 | |||
| 134 | |||
| 135 | /** |
||
| 136 | * load_espresso_template_tags - if current theme is an espresso theme, or uses ee theme template parts, then |
||
| 137 | * load it's functions.php file ( if not already loaded ) |
||
| 138 | * |
||
| 139 | * @return void |
||
| 140 | */ |
||
| 141 | public function load_espresso_template_tags() |
||
| 142 | { |
||
| 143 | if (is_readable(EE_PUBLIC . 'template_tags.php')) { |
||
| 144 | require_once(EE_PUBLIC . 'template_tags.php'); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | |||
| 149 | /** |
||
| 150 | * filter_wp_comments |
||
| 151 | * This simply makes sure that any "private" EE CPTs do not have their comments show up in any wp comment |
||
| 152 | * widgets/queries done on frontend |
||
| 153 | * |
||
| 154 | * @param array $clauses array of comment clauses setup by WP_Comment_Query |
||
| 155 | * @return array array of comment clauses with modifications. |
||
| 156 | */ |
||
| 157 | public function filter_wp_comments($clauses) |
||
| 158 | { |
||
| 159 | global $wpdb; |
||
| 160 | if (strpos($clauses['join'], $wpdb->posts) !== false) { |
||
| 161 | $cpts = EE_Register_CPTs::get_private_CPTs(); |
||
| 162 | foreach ($cpts as $cpt => $details) { |
||
| 163 | $clauses['where'] .= $wpdb->prepare(" AND $wpdb->posts.post_type != %s", $cpt); |
||
| 164 | } |
||
| 165 | } |
||
| 166 | return $clauses; |
||
| 167 | } |
||
| 168 | |||
| 169 | |||
| 170 | /** |
||
| 171 | * employ_CPT_Strategy |
||
| 172 | * |
||
| 173 | * @access public |
||
| 174 | * @return void |
||
| 175 | */ |
||
| 176 | public function employ_CPT_Strategy() |
||
| 177 | { |
||
| 178 | if (apply_filters('FHEE__EE_Front_Controller__employ_CPT_Strategy', true)) { |
||
| 179 | $this->Registry->load_core('CPT_Strategy'); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | |||
| 184 | /** |
||
| 185 | * this just makes sure that if the site is using ssl that we force that for any admin ajax calls from frontend |
||
| 186 | * |
||
| 187 | * @param string $url incoming url |
||
| 188 | * @return string final assembled url |
||
| 189 | */ |
||
| 190 | public function maybe_force_admin_ajax_ssl($url) |
||
| 191 | { |
||
| 192 | if (is_ssl() && preg_match('/admin-ajax.php/', $url)) { |
||
| 193 | $url = str_replace('http://', 'https://', $url); |
||
| 194 | } |
||
| 195 | return $url; |
||
| 196 | } |
||
| 197 | |||
| 198 | |||
| 199 | |||
| 200 | |||
| 201 | |||
| 202 | |||
| 203 | /*********************************************** WP_LOADED ACTION HOOK ***********************************************/ |
||
| 204 | |||
| 205 | |||
| 206 | /** |
||
| 207 | * wp_loaded - should fire after shortcode, module, addon, or other plugin's have been registered and their |
||
| 208 | * default priority init phases have run |
||
| 209 | * |
||
| 210 | * @access public |
||
| 211 | * @return void |
||
| 212 | */ |
||
| 213 | public function wp_loaded() |
||
| 214 | { |
||
| 215 | } |
||
| 216 | |||
| 217 | |||
| 218 | |||
| 219 | |||
| 220 | |||
| 221 | /*********************************************** PARSE_REQUEST HOOK ***********************************************/ |
||
| 222 | /** |
||
| 223 | * _get_request |
||
| 224 | * |
||
| 225 | * @access public |
||
| 226 | * @param WP $WP |
||
| 227 | * @return void |
||
| 228 | */ |
||
| 229 | public function get_request(WP $WP) |
||
| 230 | { |
||
| 231 | do_action('AHEE__EE_Front_Controller__get_request__start'); |
||
| 232 | $this->Request_Handler->parse_request($WP); |
||
| 233 | do_action('AHEE__EE_Front_Controller__get_request__complete'); |
||
| 234 | } |
||
| 235 | |||
| 236 | |||
| 237 | /** |
||
| 238 | * _initialize_shortcodes - calls init method on shortcodes that have been determined to be in the_content for |
||
| 239 | * the currently requested page |
||
| 240 | * |
||
| 241 | * @access public |
||
| 242 | * @param WP $WP |
||
| 243 | * @return void |
||
| 244 | */ |
||
| 245 | public function _initialize_shortcodes(WP $WP) |
||
| 246 | { |
||
| 247 | do_action('AHEE__EE_Front_Controller__initialize_shortcodes__begin', $WP, $this); |
||
| 248 | $this->Request_Handler->set_request_vars($WP); |
||
| 249 | // grab post_name from request |
||
| 250 | $current_post = apply_filters('FHEE__EE_Front_Controller__initialize_shortcodes__current_post_name', |
||
| 251 | $this->Request_Handler->get('post_name')); |
||
| 252 | $show_on_front = get_option('show_on_front'); |
||
| 253 | // if it's not set, then check if frontpage is blog |
||
| 254 | View Code Duplication | if (empty($current_post)) { |
|
| 255 | // yup.. this is the posts page, prepare to load all shortcode modules |
||
| 256 | $current_post = 'posts'; |
||
| 257 | // unless.. |
||
| 258 | if ($show_on_front === 'page') { |
||
| 259 | // some other page is set as the homepage |
||
| 260 | $page_on_front = get_option('page_on_front'); |
||
| 261 | if ($page_on_front) { |
||
| 262 | // k now we need to find the post_name for this page |
||
| 263 | global $wpdb; |
||
| 264 | $page_on_front = $wpdb->get_var( |
||
| 265 | $wpdb->prepare( |
||
| 266 | "SELECT post_name from $wpdb->posts WHERE post_type='page' AND post_status='publish' AND ID=%d", |
||
| 267 | $page_on_front |
||
| 268 | ) |
||
| 269 | ); |
||
| 270 | // set the current post slug to what it actually is |
||
| 271 | $current_post = $page_on_front ? $page_on_front : $current_post; |
||
| 272 | } |
||
| 273 | } |
||
| 274 | } |
||
| 275 | // where are posts being displayed ? |
||
| 276 | $page_for_posts = EE_Config::get_page_for_posts(); |
||
| 277 | // in case $current_post is hierarchical like: /parent-page/current-page |
||
| 278 | $current_post = basename($current_post); |
||
| 279 | // are we on a category page? |
||
| 280 | $term_exists = is_array(term_exists($current_post, 'category')) || array_key_exists('category_name', |
||
| 281 | $WP->query_vars); |
||
| 282 | // make sure shortcodes are set |
||
| 283 | if (isset($this->Registry->CFG->core->post_shortcodes)) { |
||
| 284 | if ( ! isset($this->Registry->CFG->core->post_shortcodes[$page_for_posts])) { |
||
| 285 | $this->Registry->CFG->core->post_shortcodes[$page_for_posts] = array(); |
||
| 286 | } |
||
| 287 | // cycle thru all posts with shortcodes set |
||
| 288 | foreach ($this->Registry->CFG->core->post_shortcodes as $post_name => $post_shortcodes) { |
||
| 289 | // filter shortcodes so |
||
| 290 | $post_shortcodes = apply_filters('FHEE__Front_Controller__initialize_shortcodes__post_shortcodes', |
||
| 291 | $post_shortcodes); |
||
| 292 | // now cycle thru shortcodes |
||
| 293 | foreach ($post_shortcodes as $shortcode_class => $post_id) { |
||
| 294 | // are we on this page, or on the blog page, or an EE CPT category page ? |
||
| 295 | if ($current_post === $post_name || $term_exists) { |
||
| 296 | // maybe init the shortcode |
||
| 297 | $this->initialize_shortcode_if_active_on_page( |
||
| 298 | $shortcode_class, |
||
| 299 | $current_post, |
||
| 300 | $page_for_posts, |
||
| 301 | $post_id, |
||
| 302 | $term_exists, |
||
| 303 | $WP |
||
| 304 | ); |
||
| 305 | // if this is NOT the "Posts page" and we have a valid entry |
||
| 306 | // for the "Posts page" in our tracked post_shortcodes array |
||
| 307 | // but the shortcode is not being tracked for this page |
||
| 308 | } else if ( |
||
| 309 | $post_name !== $page_for_posts |
||
| 310 | && isset($this->Registry->CFG->core->post_shortcodes[$page_for_posts]) |
||
| 311 | && ! isset($this->Registry->CFG->core->post_shortcodes[$page_for_posts][$shortcode_class]) |
||
| 312 | ) { |
||
| 313 | // then remove the "fallback" shortcode processor |
||
| 314 | remove_shortcode($shortcode_class); |
||
| 315 | } |
||
| 316 | } |
||
| 317 | } |
||
| 318 | } |
||
| 319 | do_action('AHEE__EE_Front_Controller__initialize_shortcodes__end', $this); |
||
| 320 | } |
||
| 321 | |||
| 322 | |||
| 323 | /** |
||
| 324 | * @param string $shortcode_class |
||
| 325 | * @param string $current_post |
||
| 326 | * @param string $page_for_posts |
||
| 327 | * @param int $post_id |
||
| 328 | * @param bool $term_exists |
||
| 329 | * @param WP $WP |
||
| 330 | */ |
||
| 331 | protected function initialize_shortcode_if_active_on_page( |
||
| 394 | |||
| 395 | |||
| 396 | /** |
||
| 397 | * pre_get_posts - basically a module factory for instantiating modules and selecting the final view template |
||
| 398 | * |
||
| 399 | * @access public |
||
| 400 | * @param WP_Query $WP_Query |
||
| 401 | * @return void |
||
| 402 | */ |
||
| 403 | public function pre_get_posts($WP_Query) |
||
| 425 | |||
| 426 | |||
| 427 | |||
| 428 | |||
| 429 | |||
| 430 | /*********************************************** WP HOOK ***********************************************/ |
||
| 431 | |||
| 432 | |||
| 433 | /** |
||
| 434 | * wp - basically last chance to do stuff before headers sent |
||
| 435 | * |
||
| 436 | * @access public |
||
| 437 | * @return void |
||
| 438 | */ |
||
| 439 | public function wp() |
||
| 442 | |||
| 443 | |||
| 444 | |||
| 445 | /*********************************************** WP_ENQUEUE_SCRIPTS && WP_HEAD HOOK ***********************************************/ |
||
| 446 | |||
| 447 | |||
| 448 | /** |
||
| 449 | * wp_enqueue_scripts |
||
| 450 | * |
||
| 451 | * @access public |
||
| 452 | * @return void |
||
| 453 | */ |
||
| 454 | public function wp_enqueue_scripts() |
||
| 573 | |||
| 574 | |||
| 575 | /** |
||
| 576 | * header_meta_tag |
||
| 577 | * |
||
| 578 | * @access public |
||
| 579 | * @return void |
||
| 580 | */ |
||
| 581 | public function header_meta_tag() |
||
| 603 | |||
| 604 | |||
| 605 | |||
| 606 | |||
| 607 | /*********************************************** THE_CONTENT FILTER HOOK ***********************************************/ |
||
| 608 | /** |
||
| 609 | * the_content |
||
| 610 | * |
||
| 611 | * @access public |
||
| 612 | * @param $the_content |
||
| 613 | * @return string |
||
| 614 | */ |
||
| 615 | // public function the_content( $the_content ) { |
||
| 616 | // // nothing gets loaded at this point unless other systems turn this hookpoint on by using: add_filter( 'FHEE_run_EE_the_content', '__return_true' ); |
||
| 617 | // if ( apply_filters( 'FHEE_run_EE_the_content', FALSE ) ) { |
||
| 618 | // } |
||
| 619 | // return $the_content; |
||
| 620 | // } |
||
| 621 | |||
| 622 | |||
| 623 | /*********************************************** WP_FOOTER ***********************************************/ |
||
| 624 | |||
| 625 | |||
| 626 | /** |
||
| 627 | * display_errors |
||
| 628 | * |
||
| 629 | * @access public |
||
| 630 | * @return string |
||
| 631 | */ |
||
| 632 | public function display_errors() |
||
| 650 | |||
| 651 | |||
| 652 | |||
| 653 | |||
| 654 | |||
| 655 | /*********************************************** UTILITIES ***********************************************/ |
||
| 656 | /** |
||
| 657 | * template_include |
||
| 658 | * |
||
| 659 | * @access public |
||
| 660 | * @param string $template_include_path |
||
| 661 | * @return string |
||
| 662 | */ |
||
| 663 | public function template_include($template_include_path = null) |
||
| 674 | |||
| 675 | |||
| 676 | /** |
||
| 677 | * get_selected_template |
||
| 678 | * |
||
| 679 | * @access public |
||
| 680 | * @param bool $with_path |
||
| 681 | * @return string |
||
| 682 | */ |
||
| 683 | public function get_selected_template($with_path = false) |
||
| 687 | |||
| 688 | |||
| 689 | } |
||
| 690 | // End of file EE_Front_Controller.core.php |
||
| 692 |