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_Template 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_Template, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
59 | class EEH_Template |
||
60 | { |
||
61 | |||
62 | private static $_espresso_themes = array(); |
||
63 | |||
64 | |||
65 | /** |
||
66 | * is_espresso_theme - returns TRUE or FALSE on whether the currently active WP theme is an espresso theme |
||
67 | * |
||
68 | * @return boolean |
||
69 | */ |
||
70 | public static function is_espresso_theme() |
||
74 | |||
75 | /** |
||
76 | * load_espresso_theme_functions - if current theme is an espresso theme, or uses ee theme template parts, then |
||
77 | * load it's functions.php file ( if not already loaded ) |
||
78 | * |
||
79 | * @return void |
||
80 | */ |
||
81 | public static function load_espresso_theme_functions() |
||
89 | |||
90 | |||
91 | /** |
||
92 | * get_espresso_themes - returns an array of Espresso Child themes located in the /templates/ directory |
||
93 | * |
||
94 | * @return array |
||
95 | */ |
||
96 | public static function get_espresso_themes() |
||
113 | |||
114 | |||
115 | /** |
||
116 | * EEH_Template::get_template_part |
||
117 | * basically a copy of the WordPress get_template_part() function but uses EEH_Template::locate_template() instead, |
||
118 | * and doesn't add base versions of files so not a very useful function at all except that it adds familiarity PLUS |
||
119 | * filtering based off of the entire template part name |
||
120 | * |
||
121 | * @param string $slug The slug name for the generic template. |
||
122 | * @param string $name The name of the specialised template. |
||
123 | * @param array $template_args |
||
124 | * @param bool $return_string |
||
125 | * @return string the html output for the formatted money value |
||
126 | */ |
||
127 | public static function get_template_part( |
||
144 | |||
145 | |||
146 | /** |
||
147 | * locate_template |
||
148 | * locate a template file by looking in the following places, in the following order: |
||
149 | * <server path up to>/wp-content/themes/<current active WordPress theme>/ |
||
150 | * <assumed full absolute server path> |
||
151 | * <server path up to>/wp-content/uploads/espresso/templates/<current EE theme>/ |
||
152 | * <server path up to>/wp-content/uploads/espresso/templates/ |
||
153 | * <server path up to>/wp-content/plugins/<EE4 folder>/public/<current EE theme>/ |
||
154 | * <server path up to>/wp-content/plugins/<EE4 folder>/core/templates/<current EE theme>/ |
||
155 | * <server path up to>/wp-content/plugins/<EE4 folder>/ |
||
156 | * as soon as the template is found in one of these locations, it will be returned or loaded |
||
157 | * Example: |
||
158 | * You are using the WordPress Twenty Sixteen theme, |
||
159 | * and you want to customize the "some-event.template.php" template, |
||
160 | * which is located in the "/relative/path/to/" folder relative to the main EE plugin folder. |
||
161 | * Assuming WP is installed on your server in the "/home/public_html/" folder, |
||
162 | * EEH_Template::locate_template() will look at the following paths in order until the template is found: |
||
163 | * /home/public_html/wp-content/themes/twentysixteen/some-event.template.php |
||
164 | * /relative/path/to/some-event.template.php |
||
165 | * /home/public_html/wp-content/uploads/espresso/templates/Espresso_Arabica_2014/relative/path/to/some-event.template.php |
||
166 | * /home/public_html/wp-content/uploads/espresso/templates/relative/path/to/some-event.template.php |
||
167 | * /home/public_html/wp-content/plugins/event-espresso-core-reg/public/Espresso_Arabica_2014/relative/path/to/some-event.template.php |
||
168 | * /home/public_html/wp-content/plugins/event-espresso-core-reg/core/templates/Espresso_Arabica_2014/relative/path/to/some-event.template.php |
||
169 | * /home/public_html/wp-content/plugins/event-espresso-core-reg/relative/path/to/some-event.template.php |
||
170 | * Had you passed an absolute path to your template that was in some other location, |
||
171 | * ie: "/absolute/path/to/some-event.template.php" |
||
172 | * then the search would have been : |
||
173 | * /home/public_html/wp-content/themes/twentysixteen/some-event.template.php |
||
174 | * /absolute/path/to/some-event.template.php |
||
175 | * and stopped there upon finding it in the second location |
||
176 | * |
||
177 | * @param array|string $templates array of template file names including extension (or just a single string) |
||
178 | * @param array $template_args an array of arguments to be extracted for use in the template |
||
179 | * @param boolean $load whether to pass the located template path on to the |
||
180 | * EEH_Template::display_template() method or simply return it |
||
181 | * @param boolean $return_string whether to send output immediately to screen, or capture and return as a |
||
182 | * string |
||
183 | * @param boolean $check_if_custom If TRUE, this flags this method to return boolean for whether this will |
||
184 | * generate a custom template or not. Used in places where you don't actually |
||
185 | * load the template, you just want to know if there's a custom version of it. |
||
186 | * @return mixed |
||
187 | */ |
||
188 | public static function locate_template( |
||
303 | |||
304 | |||
305 | /** |
||
306 | * _find_common_base_path |
||
307 | * given two paths, this determines if there is a common base path between the two |
||
308 | * |
||
309 | * @param array $paths |
||
310 | * @return string |
||
311 | */ |
||
312 | protected static function _find_common_base_path($paths) |
||
329 | |||
330 | |||
331 | /** |
||
332 | * load and display a template |
||
333 | * |
||
334 | * @param bool|string $template_path server path to the file to be loaded, including file name and extension |
||
335 | * @param array $template_args an array of arguments to be extracted for use in the template |
||
336 | * @param boolean $return_string whether to send output immediately to screen, or capture and return as a string |
||
337 | * @param bool $throw_exceptions if set to true, will throw an exception if the template is either |
||
338 | * not found or is not readable |
||
339 | * @return mixed string |
||
340 | * @throws \DomainException |
||
341 | */ |
||
342 | public static function display_template( |
||
391 | |||
392 | |||
393 | /** |
||
394 | * get_object_css_class - attempts to generate a css class based on the type of EE object passed |
||
395 | * |
||
396 | * @param EE_Base_Class $object the EE object the css class is being generated for |
||
397 | * @param string $prefix added to the beginning of the generated class |
||
398 | * @param string $suffix added to the end of the generated class |
||
399 | * @return string |
||
400 | */ |
||
401 | public static function get_object_css_class($object = null, $prefix = '', $suffix = '') |
||
424 | |||
425 | |||
426 | |||
427 | /** |
||
428 | * EEH_Template::format_currency |
||
429 | * This helper takes a raw float value and formats it according to the default config country currency settings, or |
||
430 | * the country currency settings from the supplied country ISO code |
||
431 | * |
||
432 | * @param float $amount raw money value |
||
433 | * @param boolean $return_raw whether to return the formatted float value only with no currency sign or code |
||
434 | * @param boolean $display_code whether to display the country code (USD). Default = TRUE |
||
435 | * @param string $CNT_ISO 2 letter ISO code for a country |
||
436 | * @param string $cur_code_span_class |
||
437 | * @return string the html output for the formatted money value |
||
438 | * @throws \EE_Error |
||
439 | */ |
||
440 | public static function format_currency( |
||
441 | $amount = null, |
||
442 | $return_raw = false, |
||
443 | $display_code = true, |
||
444 | $CNT_ISO = '', |
||
445 | $cur_code_span_class = 'currency-code' |
||
446 | ) { |
||
447 | // ensure amount was received |
||
448 | View Code Duplication | if ($amount === null) { |
|
449 | $msg = __('In order to format currency, an amount needs to be passed.', 'event_espresso'); |
||
450 | EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
||
451 | return ''; |
||
452 | } |
||
453 | //ensure amount is float |
||
454 | $amount = apply_filters('FHEE__EEH_Template__format_currency__raw_amount', (float)$amount); |
||
455 | $CNT_ISO = apply_filters('FHEE__EEH_Template__format_currency__CNT_ISO', $CNT_ISO, $amount); |
||
456 | // filter raw amount (allows 0.00 to be changed to "free" for example) |
||
457 | $amount_formatted = apply_filters('FHEE__EEH_Template__format_currency__amount', $amount, $return_raw); |
||
458 | // still a number or was amount converted to a string like "free" ? |
||
459 | if (is_float($amount_formatted)) { |
||
460 | // was a country ISO code passed ? if so generate currency config object for that country |
||
461 | $mny = $CNT_ISO !== '' ? new EE_Currency_Config($CNT_ISO) : null; |
||
462 | // verify results |
||
463 | View Code Duplication | if ( ! $mny instanceof EE_Currency_Config) { |
|
464 | // set default config country currency settings |
||
465 | $mny = EE_Registry::instance()->CFG->currency instanceof EE_Currency_Config |
||
466 | ? EE_Registry::instance()->CFG->currency |
||
467 | : new EE_Currency_Config(); |
||
468 | } |
||
469 | // format float |
||
470 | $amount_formatted = number_format($amount, $mny->dec_plc, $mny->dec_mrk, $mny->thsnds); |
||
471 | // add formatting ? |
||
472 | if ( ! $return_raw) { |
||
473 | // add currency sign |
||
474 | if ($mny->sign_b4) { |
||
475 | if ($amount >= 0) { |
||
476 | $amount_formatted = $mny->sign . $amount_formatted; |
||
477 | } else { |
||
478 | $amount_formatted = '-' . $mny->sign . str_replace('-', '', $amount_formatted); |
||
479 | } |
||
480 | |||
481 | } else { |
||
482 | $amount_formatted = $amount_formatted . $mny->sign; |
||
483 | } |
||
484 | |||
485 | // filter to allow global setting of display_code |
||
486 | $display_code = apply_filters('FHEE__EEH_Template__format_currency__display_code', $display_code); |
||
487 | |||
488 | // add currency code ? |
||
489 | $amount_formatted = $display_code ? $amount_formatted . ' <span class="' . $cur_code_span_class . '">(' . $mny->code . ')</span>' : $amount_formatted; |
||
490 | } |
||
491 | // filter results |
||
492 | $amount_formatted = apply_filters('FHEE__EEH_Template__format_currency__amount_formatted', |
||
493 | $amount_formatted, $mny, $return_raw); |
||
494 | } |
||
495 | // clean up vars |
||
496 | unset($mny); |
||
497 | // return formatted currency amount |
||
498 | return $amount_formatted; |
||
499 | } |
||
500 | |||
501 | |||
502 | /** |
||
503 | * This function is used for outputting the localized label for a given status id in the schema requested (and |
||
504 | * possibly plural). The intended use of this function is only for cases where wanting a label outside of a |
||
505 | * related status model or model object (i.e. in documentation etc.) |
||
506 | * |
||
507 | * @param string $status_id Status ID matching a registered status in the esp_status table. If there is no |
||
508 | * match, then 'Unknown' will be returned. |
||
509 | * @param boolean $plural Whether to return plural or not |
||
510 | * @param string $schema 'UPPER', 'lower', or 'Sentence' |
||
511 | * @return string The localized label for the status id. |
||
512 | */ |
||
513 | public static function pretty_status($status_id, $plural = false, $schema = 'upper') |
||
521 | |||
522 | |||
523 | /** |
||
524 | * This helper just returns a button or link for the given parameters |
||
525 | * |
||
526 | * @param string $url the url for the link, note that `esc_url` will be called on it |
||
527 | * @param string $label What is the label you want displayed for the button |
||
528 | * @param string $class what class is used for the button (defaults to 'button-primary') |
||
529 | * @param string $icon |
||
530 | * @param string $title |
||
531 | * @return string the html output for the button |
||
532 | */ |
||
533 | public static function get_button_or_link($url, $label, $class = 'button-primary', $icon = '', $title = '') |
||
551 | |||
552 | |||
553 | /** |
||
554 | * This returns a generated link that will load the related help tab on admin pages. |
||
555 | * |
||
556 | * @param string $help_tab_id the id for the connected help tab |
||
557 | * @param bool|string $page The page identifier for the page the help tab is on |
||
558 | * @param bool|string $action The action (route) for the admin page the help tab is on. |
||
559 | * @param bool|string $icon_style (optional) include css class for the style you want to use for the help icon. |
||
560 | * @param bool|string $help_text (optional) send help text you want to use for the link if default not to be used |
||
561 | * @return string generated link |
||
562 | */ |
||
563 | public static function get_help_tab_link( |
||
588 | |||
589 | |||
590 | /** |
||
591 | * This helper generates the html structure for the jquery joyride plugin with the given params. |
||
592 | * |
||
593 | * @link http://zurb.com/playground/jquery-joyride-feature-tour-plugin |
||
594 | * @see EE_Admin_Page->_stop_callback() for the construct expected for the $stops param. |
||
595 | * @param EE_Help_Tour |
||
596 | * @return string html |
||
597 | */ |
||
598 | public static function help_tour_stops_generator(EE_Help_Tour $tour) |
||
637 | |||
638 | |||
639 | /** |
||
640 | * This is a helper method to generate a status legend for a given status array. |
||
641 | * Note this will only work if the incoming statuses have a key in the EEM_Status->localized_status() methods |
||
642 | * status_array. |
||
643 | * |
||
644 | * @param array $status_array array of statuses that will make up the legend. In format: |
||
645 | * array( |
||
646 | * 'status_item' => 'status_name' |
||
647 | * ) |
||
648 | * @param string $active_status This is used to indicate what the active status is IF that is to be highlighted in |
||
649 | * the legend. |
||
650 | * @throws EE_Error |
||
651 | * @return string html structure for status. |
||
652 | */ |
||
653 | public static function status_legend($status_array, $active_status = '') |
||
683 | |||
684 | |||
685 | /** |
||
686 | * Gets HTML for laying out a deeply-nested array (and objects) in a format |
||
687 | * that's nice for presenting in the wp admin |
||
688 | * |
||
689 | * @param mixed $data |
||
690 | * @return string |
||
691 | */ |
||
692 | public static function layout_array_as_table($data) |
||
737 | |||
738 | |||
739 | /** |
||
740 | * wrapper for self::get_paging_html() that simply echos the generated paging html |
||
741 | * |
||
742 | * @since 4.4.0 |
||
743 | * @see self:get_paging_html() for argument docs. |
||
744 | * @param $total_items |
||
745 | * @param $current |
||
746 | * @param $per_page |
||
747 | * @param $url |
||
748 | * @param bool $show_num_field |
||
749 | * @param string $paged_arg_name |
||
750 | * @param array $items_label |
||
751 | * @return string |
||
752 | */ |
||
753 | public static function paging_html( |
||
765 | |||
766 | |||
767 | /** |
||
768 | * A method for generating paging similar to WP_List_Table |
||
769 | * |
||
770 | * @since 4.4.0 |
||
771 | * @see wp-admin/includes/class-wp-list-table.php WP_List_Table::pagination() |
||
772 | * @param integer $total_items How many total items there are to page. |
||
773 | * @param integer $current What the current page is. |
||
774 | * @param integer $per_page How many items per page. |
||
775 | * @param string $url What the base url for page links is. |
||
776 | * @param boolean $show_num_field Whether to show the input for changing page number. |
||
777 | * @param string $paged_arg_name The name of the key for the paged query argument. |
||
778 | * @param array $items_label An array of singular/plural values for the items label: |
||
779 | * array( |
||
780 | * 'single' => 'item', |
||
781 | * 'plural' => 'items' |
||
782 | * ) |
||
783 | * @return string |
||
784 | */ |
||
785 | public static function get_paging_html( |
||
903 | |||
904 | |||
905 | /** |
||
906 | * @param string $wrap_class |
||
907 | * @param string $wrap_id |
||
908 | * @return string |
||
909 | */ |
||
910 | public static function powered_by_event_espresso($wrap_class = '', $wrap_id = '', array $query_args = array()) |
||
957 | |||
958 | |||
959 | } //end EEH_Template class |
||
960 | |||
994 | } |