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 |
||
62 | class EEH_Template { |
||
63 | |||
64 | private static $_espresso_themes = array(); |
||
65 | |||
66 | |||
67 | /** |
||
68 | * is_espresso_theme - returns TRUE or FALSE on whether the currently active WP theme is an espresso theme |
||
69 | * |
||
70 | * @return boolean |
||
71 | */ |
||
72 | public static function is_espresso_theme() { |
||
75 | |||
76 | /** |
||
77 | * load_espresso_theme_functions - if current theme is an espresso theme, or uses ee theme template parts, then load it's functions.php file ( if not already loaded ) |
||
78 | * |
||
79 | * @return void |
||
80 | */ |
||
81 | public static function load_espresso_theme_functions() { |
||
88 | |||
89 | |||
90 | /** |
||
91 | * get_espresso_themes - returns an array of Espresso Child themes located in the /templates/ directory |
||
92 | * |
||
93 | * @return array |
||
94 | */ |
||
95 | public static function get_espresso_themes() { |
||
111 | |||
112 | |||
113 | |||
114 | /** |
||
115 | * EEH_Template::get_template_part |
||
116 | * basically a copy of the WordPress get_template_part() function but uses EEH_Template::locate_template() instead, and doesn't add base versions of files |
||
117 | * so not a very useful function at all except that it adds familiarity PLUS filtering based off of the entire template part name |
||
118 | * |
||
119 | * @param string $slug The slug name for the generic template. |
||
120 | * @param string $name The name of the specialised template. |
||
121 | * @param array $template_args |
||
122 | * @param bool $return_string |
||
123 | * @return string the html output for the formatted money value |
||
124 | */ |
||
125 | public static function get_template_part( $slug = NULL, $name = NULL, $template_args = array(), $return_string = FALSE ) { |
||
137 | |||
138 | |||
139 | |||
140 | /** |
||
141 | * locate_template |
||
142 | * |
||
143 | * locate a template file by looking in the following places, in the following order: |
||
144 | * <server path up to>/wp-content/themes/<current active WordPress theme>/ |
||
145 | * <assumed full absolute server path> |
||
146 | * <server path up to>/wp-content/uploads/espresso/templates/<current EE theme>/ |
||
147 | * <server path up to>/wp-content/uploads/espresso/templates/ |
||
148 | * <server path up to>/wp-content/plugins/<EE4 folder>/public/<current EE theme>/ |
||
149 | * <server path up to>/wp-content/plugins/<EE4 folder>/core/templates/<current EE theme>/ |
||
150 | * <server path up to>/wp-content/plugins/<EE4 folder>/ |
||
151 | * as soon as the template is found in one of these locations, it will be returned or loaded |
||
152 | * |
||
153 | * Example: |
||
154 | * You are using the WordPress Twenty Sixteen theme, |
||
155 | * and you want to customize the "some-event.template.php" template, |
||
156 | * which is located in the "/relative/path/to/" folder relative to the main EE plugin folder. |
||
157 | * Assuming WP is installed on your server in the "/home/public_html/" folder, |
||
158 | * EEH_Template::locate_template() will look at the following paths in order until the template is found: |
||
159 | * |
||
160 | * /home/public_html/wp-content/themes/twentysixteen/some-event.template.php |
||
161 | * /relative/path/to/some-event.template.php |
||
162 | * /home/public_html/wp-content/uploads/espresso/templates/Espresso_Arabica_2014/relative/path/to/some-event.template.php |
||
163 | * /home/public_html/wp-content/uploads/espresso/templates/relative/path/to/some-event.template.php |
||
164 | * /home/public_html/wp-content/plugins/event-espresso-core-reg/public/Espresso_Arabica_2014/relative/path/to/some-event.template.php |
||
165 | * /home/public_html/wp-content/plugins/event-espresso-core-reg/core/templates/Espresso_Arabica_2014/relative/path/to/some-event.template.php |
||
166 | * /home/public_html/wp-content/plugins/event-espresso-core-reg/relative/path/to/some-event.template.php |
||
167 | * |
||
168 | * Had you passed an absolute path to your template that was in some other location, |
||
169 | * ie: "/absolute/path/to/some-event.template.php" |
||
170 | * then the search would have been : |
||
171 | * |
||
172 | * /home/public_html/wp-content/themes/twentysixteen/some-event.template.php |
||
173 | * /absolute/path/to/some-event.template.php |
||
174 | * |
||
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 EEH_Template::display_template() method or simply return it |
||
180 | * @param boolean $return_string whether to send output immediately to screen, or capture and return as a string |
||
181 | * @param boolean $check_if_custom If TRUE, this flags this method to return boolean for whether this will generate a custom template or not. |
||
182 | * Used in places where you don't actually load the template, you just want to know if there's a custom version of it. |
||
183 | * @return mixed |
||
184 | */ |
||
185 | public static function locate_template( $templates = array(), $template_args = array(), $load = TRUE, $return_string = TRUE, $check_if_custom = FALSE ) { |
||
186 | // first use WP locate_template to check for template in the current theme folder |
||
187 | $template_path = locate_template( $templates ); |
||
188 | |||
189 | if ( $check_if_custom && !empty( $template_path ) ) |
||
190 | return TRUE; |
||
191 | |||
192 | // not in the theme |
||
193 | if ( empty( $template_path )) { |
||
194 | // not even a template to look for ? |
||
195 | if ( empty( $templates )) { |
||
196 | // get post_type |
||
197 | $post_type = EE_Registry::instance()->REQ->get( 'post_type' ); |
||
198 | // get array of EE Custom Post Types |
||
199 | $EE_CPTs = EE_Register_CPTs::get_CPTs(); |
||
200 | // build template name based on request |
||
201 | if ( isset( $EE_CPTs[ $post_type ] )) { |
||
202 | $archive_or_single = is_archive() ? 'archive' : ''; |
||
203 | $archive_or_single = is_single() ? 'single' : $archive_or_single; |
||
204 | $templates = $archive_or_single . '-' . $post_type . '.php'; |
||
205 | } |
||
206 | } |
||
207 | // currently active EE template theme |
||
208 | $current_theme = EE_Config::get_current_theme(); |
||
209 | |||
210 | // array of paths to folders that may contain templates |
||
211 | $template_folder_paths = array( |
||
212 | // first check the /wp-content/uploads/espresso/templates/(current EE theme)/ folder for an EE theme template file |
||
213 | EVENT_ESPRESSO_TEMPLATE_DIR . $current_theme, |
||
214 | // then in the root of the /wp-content/uploads/espresso/templates/ folder |
||
215 | EVENT_ESPRESSO_TEMPLATE_DIR |
||
216 | ); |
||
217 | |||
218 | //add core plugin folders for checking only if we're not $check_if_custom |
||
219 | if ( ! $check_if_custom ) { |
||
220 | $core_paths = array( |
||
221 | // in the /wp-content/plugins/(EE4 folder)/public/(current EE theme)/ folder within the plugin |
||
222 | EE_PUBLIC . $current_theme, |
||
223 | // in the /wp-content/plugins/(EE4 folder)/core/templates/(current EE theme)/ folder within the plugin |
||
224 | EE_TEMPLATES . $current_theme, |
||
225 | // or maybe relative from the plugin root: /wp-content/plugins/(EE4 folder)/ |
||
226 | EE_PLUGIN_DIR_PATH |
||
227 | ); |
||
228 | $template_folder_paths = array_merge( $template_folder_paths, $core_paths ); |
||
229 | } |
||
230 | |||
231 | // now filter that array |
||
232 | $template_folder_paths = apply_filters( 'FHEE__EEH_Template__locate_template__template_folder_paths', $template_folder_paths ); |
||
233 | $templates = is_array( $templates ) ? $templates : array( $templates ); |
||
234 | $template_folder_paths = is_array( $template_folder_paths ) ? $template_folder_paths : array( $template_folder_paths ); |
||
235 | // array to hold all possible template paths |
||
236 | $full_template_paths = array(); |
||
237 | |||
238 | // loop through $templates |
||
239 | foreach ( $templates as $template ) { |
||
240 | // normalize directory separators |
||
241 | $template = EEH_File::standardise_directory_separators( $template ); |
||
242 | $file_name = basename( $template ); |
||
243 | $template_path_minus_file_name = substr( $template, 0, ( strlen( $file_name ) * -1 ) ); |
||
244 | // while looping through all template folder paths |
||
245 | foreach ( $template_folder_paths as $template_folder_path ) { |
||
246 | // normalize directory separators |
||
247 | $template_folder_path = EEH_File::standardise_directory_separators( $template_folder_path ); |
||
248 | // determine if any common base path exists between the two paths |
||
249 | $common_base_path = EEH_Template::_find_common_base_path( |
||
250 | array( $template_folder_path, $template_path_minus_file_name ) |
||
251 | ); |
||
252 | if ( $common_base_path !== '' ) { |
||
253 | // both paths have a common base, so just tack the filename onto our search path |
||
254 | $resolved_path = EEH_File::end_with_directory_separator( $template_folder_path ) . $file_name; |
||
255 | } else { |
||
256 | // no common base path, so let's just concatenate |
||
257 | $resolved_path = EEH_File::end_with_directory_separator( $template_folder_path ) . $template; |
||
258 | } |
||
259 | // build up our template locations array by adding our resolved paths |
||
260 | $full_template_paths[] = $resolved_path; |
||
261 | } |
||
262 | // if $template is an absolute path, then we'll tack it onto the start of our array so that it gets searched first |
||
263 | array_unshift( $full_template_paths, $template ); |
||
264 | // path to the directory of the current theme: /wp-content/themes/(current WP theme)/ |
||
265 | array_unshift( $full_template_paths, get_stylesheet_directory() . DS . $file_name ); |
||
266 | } |
||
267 | // filter final array of full template paths |
||
268 | $full_template_paths = apply_filters( 'FHEE__EEH_Template__locate_template__full_template_paths', $full_template_paths, $file_name ); |
||
|
|||
269 | // now loop through our final array of template location paths and check each location |
||
270 | foreach ( (array)$full_template_paths as $full_template_path ) { |
||
271 | if ( is_readable( $full_template_path )) { |
||
272 | $template_path = str_replace( array( '\\', '/' ), DIRECTORY_SEPARATOR, $full_template_path ); |
||
273 | // hook that can be used to display the full template path that will be used |
||
274 | do_action( 'AHEE__EEH_Template__locate_template__full_template_path', $template_path ); |
||
275 | break; |
||
276 | } |
||
277 | } |
||
278 | } |
||
279 | // if we got it and you want to see it... |
||
280 | if ( $template_path && $load && ! $check_if_custom ) { |
||
281 | if ( $return_string ) { |
||
282 | return EEH_Template::display_template( $template_path, $template_args, TRUE ); |
||
283 | } else { |
||
284 | EEH_Template::display_template( $template_path, $template_args, FALSE ); |
||
285 | } |
||
286 | } |
||
287 | return $check_if_custom && ! empty( $template_path ) ? TRUE : $template_path; |
||
288 | } |
||
289 | |||
290 | |||
291 | |||
292 | /** |
||
293 | * _find_common_base_path |
||
294 | * |
||
295 | * given two paths, this determines if there is a common base path between the two |
||
296 | * |
||
297 | * @param array $paths |
||
298 | * @return string |
||
299 | */ |
||
300 | protected static function _find_common_base_path( $paths ) { |
||
316 | |||
317 | |||
318 | |||
319 | /** |
||
320 | * load and display a template |
||
321 | * @param bool|string $template_path server path to the file to be loaded, including file name and extension |
||
322 | * @param array $template_args an array of arguments to be extracted for use in the template |
||
323 | * @param boolean $return_string whether to send output immediately to screen, or capture and return as a string |
||
324 | * @return mixed string |
||
325 | */ |
||
326 | public static function display_template( $template_path = FALSE, $template_args = array(), $return_string = FALSE ) { |
||
360 | |||
361 | |||
362 | |||
363 | |||
364 | |||
365 | /** |
||
366 | * get_object_css_class - attempts to generate a css class based on the type of EE object passed |
||
367 | * |
||
368 | * |
||
369 | * @param EE_Base_Class $object the EE object the css class is being generated for |
||
370 | * @param string $prefix added to the beginning of the generated class |
||
371 | * @param string $suffix added to the end of the generated class |
||
372 | * @return string |
||
373 | */ |
||
374 | public static function get_object_css_class( $object = NULL, $prefix = '', $suffix = '' ) { |
||
396 | |||
397 | |||
398 | |||
399 | /** |
||
400 | * EEH_Template::format_currency |
||
401 | * This helper takes a raw float value and formats it according to the default config country currency settings, or the country currency settings from the supplied country ISO code |
||
402 | * |
||
403 | * @param float $amount raw money value |
||
404 | * @param boolean $return_raw whether to return the formatted float value only with no currency sign or code |
||
405 | * @param boolean $display_code whether to display the country code (USD). Default = TRUE |
||
406 | * @param string $CNT_ISO 2 letter ISO code for a country |
||
407 | * @param string $cur_code_span_class |
||
408 | * @return string the html output for the formatted money value |
||
409 | */ |
||
410 | public static function format_currency( $amount = NULL, $return_raw = FALSE, $display_code = TRUE, $CNT_ISO = '', $cur_code_span_class = 'currency-code' ) { |
||
411 | // ensure amount was received |
||
412 | if ( is_null( $amount ) ) { |
||
413 | $msg = __( 'In order to format currency, an amount needs to be passed.', 'event_espresso' ); |
||
414 | EE_Error::add_error( $msg, __FILE__, __FUNCTION__, __LINE__ ); |
||
415 | return ''; |
||
416 | } |
||
417 | //ensure amount is float |
||
418 | $amount = apply_filters( 'FHEE__EEH_Template__format_currency__raw_amount', (float) $amount ); |
||
419 | $CNT_ISO = apply_filters( 'FHEE__EEH_Template__format_currency__CNT_ISO', $CNT_ISO, $amount ); |
||
420 | // filter raw amount (allows 0.00 to be changed to "free" for example) |
||
421 | $amount_formatted = apply_filters( 'FHEE__EEH_Template__format_currency__amount', $amount, $return_raw ); |
||
422 | // still a number or was amount converted to a string like "free" ? |
||
423 | if ( is_float( $amount_formatted )) { |
||
424 | // was a country ISO code passed ? if so generate currency config object for that country |
||
425 | $mny = $CNT_ISO !== '' ? new EE_Currency_Config( $CNT_ISO ) : NULL; |
||
426 | // verify results |
||
427 | View Code Duplication | if ( ! $mny instanceof EE_Currency_Config ) { |
|
428 | // set default config country currency settings |
||
429 | $mny = EE_Registry::instance()->CFG->currency instanceof EE_Currency_Config ? EE_Registry::instance()->CFG->currency : new EE_Currency_Config(); |
||
430 | } |
||
431 | // format float |
||
432 | $amount_formatted = number_format( $amount, $mny->dec_plc, $mny->dec_mrk, $mny->thsnds ); |
||
433 | // add formatting ? |
||
434 | if ( ! $return_raw ) { |
||
435 | // add currency sign |
||
436 | if( $mny->sign_b4 ){ |
||
437 | if( $amount >= 0 ){ |
||
438 | $amount_formatted = $mny->sign . $amount_formatted; |
||
439 | }else{ |
||
440 | $amount_formatted = '-' . $mny->sign . str_replace( '-', '', $amount_formatted ); |
||
441 | } |
||
442 | |||
443 | }else{ |
||
444 | $amount_formatted = $amount_formatted . $mny->sign; |
||
445 | } |
||
446 | |||
447 | // filter to allow global setting of display_code |
||
448 | $display_code = apply_filters( 'FHEE__EEH_Template__format_currency__display_code', $display_code ); |
||
449 | |||
450 | // add currency code ? |
||
451 | $amount_formatted = $display_code ? $amount_formatted . ' <span class="' . $cur_code_span_class . '">(' . $mny->code . ')</span>' : $amount_formatted; |
||
452 | } |
||
453 | // filter results |
||
454 | $amount_formatted = apply_filters( 'FHEE__EEH_Template__format_currency__amount_formatted', $amount_formatted, $mny, $return_raw ); |
||
455 | } |
||
456 | // clean up vars |
||
457 | unset( $mny ); |
||
458 | // return formatted currency amount |
||
459 | return $amount_formatted; |
||
460 | } |
||
461 | |||
462 | |||
463 | |||
464 | |||
465 | /** |
||
466 | * This function is used for outputting the localized label for a given status id in the schema requested (and possibly plural). The intended use of this function is only for cases where wanting a label outside of a related status model or model object (i.e. in documentation etc.) |
||
467 | * @param string $status_id Status ID matching a registered status in the esp_status table. If there is no match, then 'Unknown' will be returned. |
||
468 | * @param boolean $plural Whether to return plural or not |
||
469 | * @param string $schema 'UPPER', 'lower', or 'Sentence' |
||
470 | * @return string The localized label for the status id. |
||
471 | */ |
||
472 | public static function pretty_status( $status_id, $plural = FALSE, $schema = 'upper' ) { |
||
478 | |||
479 | |||
480 | |||
481 | /** |
||
482 | * This helper just returns a button or link for the given parameters |
||
483 | * |
||
484 | * @param string $url the url for the link |
||
485 | * @param string $label What is the label you want displayed for the button |
||
486 | * @param string $class what class is used for the button (defaults to 'button-primary') |
||
487 | * @param string $icon |
||
488 | * @param string $title |
||
489 | * @return string the html output for the button |
||
490 | */ |
||
491 | public static function get_button_or_link( $url, $label, $class = 'button-primary', $icon = '', $title = '' ) { |
||
508 | |||
509 | |||
510 | |||
511 | /** |
||
512 | * This returns a generated link that will load the related help tab on admin pages. |
||
513 | * |
||
514 | * |
||
515 | * @param string $help_tab_id the id for the connected help tab |
||
516 | * @param bool|string $page The page identifier for the page the help tab is on |
||
517 | * @param bool|string $action The action (route) for the admin page the help tab is on. |
||
518 | * @param bool|string $icon_style (optional) include css class for the style you want to use for the help icon. |
||
519 | * @param bool|string $help_text (optional) send help text you want to use for the link if default not to be used |
||
520 | * @return string generated link |
||
521 | */ |
||
522 | public static function get_help_tab_link( $help_tab_id, $page = FALSE, $action = FALSE, $icon_style = FALSE, $help_text = FALSE ) { |
||
538 | |||
539 | |||
540 | |||
541 | /** |
||
542 | * This helper generates the html structure for the jquery joyride plugin with the given params. |
||
543 | * @link http://zurb.com/playground/jquery-joyride-feature-tour-plugin |
||
544 | * @see EE_Admin_Page->_stop_callback() for the construct expected for the $stops param. |
||
545 | * @param EE_Help_Tour |
||
546 | * @return string html |
||
547 | */ |
||
548 | public static function help_tour_stops_generator( EE_Help_Tour $tour ) { |
||
586 | |||
587 | |||
588 | |||
589 | /** |
||
590 | * This is a helper method to generate a status legend for a given status array. |
||
591 | * Note this will only work if the incoming statuses have a key in the EEM_Status->localized_status() methods status_array. |
||
592 | * |
||
593 | * @param array $status_array array of statuses that will make up the legend. In format: |
||
594 | * array( |
||
595 | * 'status_item' => 'status_name' |
||
596 | * ) |
||
597 | * @param string $active_status This is used to indicate what the active status is IF that is to be highlighted in the legend. |
||
598 | * @throws EE_Error |
||
599 | * @return string html structure for status. |
||
600 | */ |
||
601 | public static function status_legend( $status_array, $active_status = '' ) { |
||
628 | |||
629 | |||
630 | |||
631 | /** |
||
632 | * Gets HTML for laying out a deeply-nested array (and objects) in a format |
||
633 | * that's nice for presenting in the wp admin |
||
634 | * @param mixed $data |
||
635 | * @return string |
||
636 | */ |
||
637 | public static function layout_array_as_table($data) { |
||
681 | |||
682 | |||
683 | |||
684 | /** |
||
685 | * wrapper for self::get_paging_html() that simply echos the generated paging html |
||
686 | * |
||
687 | * @since 4.4.0 |
||
688 | * @see self:get_paging_html() for argument docs. |
||
689 | * @param $total_items |
||
690 | * @param $current |
||
691 | * @param $per_page |
||
692 | * @param $url |
||
693 | * @param bool $show_num_field |
||
694 | * @param string $paged_arg_name |
||
695 | * @param array $items_label |
||
696 | * |
||
697 | * @return string |
||
698 | */ |
||
699 | public static function paging_html( $total_items, $current, $per_page, $url, $show_num_field = TRUE, $paged_arg_name = 'paged', $items_label = array() ) { |
||
702 | |||
703 | |||
704 | |||
705 | /** |
||
706 | * A method for generating paging similar to WP_List_Table |
||
707 | * |
||
708 | * @since 4.4.0 |
||
709 | * @see wp-admin/includes/class-wp-list-table.php WP_List_Table::pagination() |
||
710 | * |
||
711 | * @param integer $total_items How many total items there are to page. |
||
712 | * @param integer $current What the current page is. |
||
713 | * @param integer $per_page How many items per page. |
||
714 | * @param string $url What the base url for page links is. |
||
715 | * @param boolean $show_num_field Whether to show the input for changing page number. |
||
716 | * @param string $paged_arg_name The name of the key for the paged query argument. |
||
717 | * @param array $items_label An array of singular/plural values for the items label: |
||
718 | * array( |
||
719 | * 'single' => 'item', |
||
720 | * 'plural' => 'items' |
||
721 | * ) |
||
722 | * @return string |
||
723 | */ |
||
724 | public static function get_paging_html( $total_items, $current, $per_page, $url, $show_num_field = TRUE, $paged_arg_name = 'paged', $items_label = array() ) { |
||
832 | |||
833 | |||
834 | |||
835 | /** |
||
836 | * @param string $wrap_class |
||
837 | * @param string $wrap_id |
||
838 | * @return string |
||
839 | */ |
||
840 | public static function powered_by_event_espresso( $wrap_class = '', $wrap_id = '' ) { |
||
875 | |||
876 | |||
877 | |||
878 | } //end EEH_Template class |
||
879 | |||
920 | } |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: