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 Give_HTML_Elements 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 Give_HTML_Elements, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class Give_HTML_Elements { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Donations Dropdown |
||
| 28 | * |
||
| 29 | * Renders an HTML Dropdown of all the donations. |
||
| 30 | * |
||
| 31 | * @since 1.0 |
||
| 32 | * @access public |
||
| 33 | * |
||
| 34 | * @param array $args Arguments for the dropdown. |
||
| 35 | * |
||
| 36 | * @return string Donations dropdown. |
||
| 37 | */ |
||
| 38 | public function donations_dropdown( $args = array() ) { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Give Forms Dropdown |
||
| 92 | * |
||
| 93 | * Renders an HTML Dropdown of all the Give Forms. |
||
| 94 | * |
||
| 95 | * @since 1.0 |
||
| 96 | * @access public |
||
| 97 | * |
||
| 98 | * @param array $args Arguments for the dropdown. |
||
| 99 | * |
||
| 100 | * @return string Give forms dropdown. |
||
| 101 | */ |
||
| 102 | public function forms_dropdown( $args = array() ) { |
||
| 103 | |||
| 104 | $defaults = array( |
||
| 105 | 'name' => 'forms', |
||
| 106 | 'id' => 'forms', |
||
| 107 | 'class' => '', |
||
| 108 | 'multiple' => false, |
||
| 109 | 'selected' => 0, |
||
| 110 | 'chosen' => false, |
||
| 111 | 'number' => 30, |
||
| 112 | 'placeholder' => esc_attr__( 'Select a Donation Form', 'give' ), |
||
| 113 | 'data' => array( |
||
| 114 | 'search-type' => 'form', |
||
| 115 | ), |
||
| 116 | ); |
||
| 117 | |||
| 118 | $args = wp_parse_args( $args, $defaults ); |
||
| 119 | |||
| 120 | $forms = get_posts( array( |
||
| 121 | 'post_type' => 'give_forms', |
||
| 122 | 'orderby' => 'title', |
||
| 123 | 'order' => 'ASC', |
||
| 124 | 'posts_per_page' => $args['number'], |
||
| 125 | ) ); |
||
| 126 | |||
| 127 | $options = array(); |
||
| 128 | |||
| 129 | // Ensure the selected. |
||
| 130 | if ( false !== $args['selected'] && $args['selected'] !== 0 ) { |
||
| 131 | $options[ $args['selected'] ] = get_the_title( $args['selected'] ); |
||
| 132 | } |
||
| 133 | |||
| 134 | if ( $forms ) { |
||
| 135 | $options[0] = $args['placeholder']; |
||
| 136 | View Code Duplication | foreach ( $forms as $form ) { |
|
| 137 | $form_title = empty( $form->post_title ) ? sprintf( __( 'Untitled (#%s)', 'give' ), $form->ID ) : $form->post_title; |
||
| 138 | $options[ absint( $form->ID ) ] = esc_html( $form_title ); |
||
| 139 | } |
||
| 140 | } else { |
||
| 141 | $options[0] = esc_html__( 'No forms found.', 'give' ); |
||
| 142 | } |
||
| 143 | |||
| 144 | $output = $this->select( array( |
||
| 145 | 'name' => $args['name'], |
||
| 146 | 'selected' => $args['selected'], |
||
| 147 | 'id' => $args['id'], |
||
| 148 | 'class' => $args['class'], |
||
| 149 | 'options' => $options, |
||
| 150 | 'chosen' => $args['chosen'], |
||
| 151 | 'multiple' => $args['multiple'], |
||
| 152 | 'placeholder' => $args['placeholder'], |
||
| 153 | 'show_option_all' => false, |
||
| 154 | 'show_option_none' => false, |
||
| 155 | 'data' => $args['data'], |
||
| 156 | ) ); |
||
| 157 | |||
| 158 | return $output; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Donors Dropdown |
||
| 163 | * |
||
| 164 | * Renders an HTML Dropdown of all customers. |
||
| 165 | * |
||
| 166 | * @since 1.0 |
||
| 167 | * @access public |
||
| 168 | * |
||
| 169 | * @param array $args Arguments for the dropdown. |
||
| 170 | * |
||
| 171 | * @return string Donors dropdown. |
||
| 172 | */ |
||
| 173 | public function donor_dropdown( $args = array() ) { |
||
| 174 | |||
| 175 | $defaults = array( |
||
| 176 | 'name' => 'donors', |
||
| 177 | 'id' => 'donors', |
||
| 178 | 'class' => '', |
||
| 179 | 'multiple' => false, |
||
| 180 | 'selected' => 0, |
||
| 181 | 'chosen' => true, |
||
| 182 | 'placeholder' => esc_attr__( 'Select a Donor', 'give' ), |
||
| 183 | 'number' => 30, |
||
| 184 | 'data' => array( |
||
| 185 | 'search-type' => 'donor', |
||
| 186 | ), |
||
| 187 | ); |
||
| 188 | |||
| 189 | $args = wp_parse_args( $args, $defaults ); |
||
| 190 | |||
| 191 | $donors = Give()->donors->get_donors( array( |
||
| 192 | 'number' => $args['number'] |
||
| 193 | ) ); |
||
| 194 | |||
| 195 | $options = array(); |
||
| 196 | |||
| 197 | View Code Duplication | if ( $donors ) { |
|
| 198 | $options[0] = esc_html__( 'No donor attached', 'give' ); |
||
| 199 | foreach ( $donors as $donor ) { |
||
| 200 | $options[ absint( $donor->id ) ] = esc_html( $donor->name . ' (' . $donor->email . ')' ); |
||
| 201 | } |
||
| 202 | } else { |
||
| 203 | $options[0] = esc_html__( 'No donors found.', 'give' ); |
||
| 204 | } |
||
| 205 | |||
| 206 | if ( ! empty( $args['selected'] ) ) { |
||
| 207 | |||
| 208 | // If a selected customer has been specified, we need to ensure it's in the initial list of customers displayed. |
||
| 209 | if ( ! array_key_exists( $args['selected'], $options ) ) { |
||
| 210 | |||
| 211 | $donor = new Give_Donor( $args['selected'] ); |
||
| 212 | |||
| 213 | if ( $donor ) { |
||
| 214 | |||
| 215 | $options[ absint( $args['selected'] ) ] = esc_html( $donor->name . ' (' . $donor->email . ')' ); |
||
| 216 | |||
| 217 | } |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | $output = $this->select( array( |
||
| 222 | 'name' => $args['name'], |
||
| 223 | 'selected' => $args['selected'], |
||
| 224 | 'id' => $args['id'], |
||
| 225 | 'class' => $args['class'] . ' give-customer-select', |
||
| 226 | 'options' => $options, |
||
| 227 | 'multiple' => $args['multiple'], |
||
| 228 | 'chosen' => $args['chosen'], |
||
| 229 | 'show_option_all' => false, |
||
| 230 | 'show_option_none' => false, |
||
| 231 | 'data' => $args['data'], |
||
| 232 | ) ); |
||
| 233 | |||
| 234 | return $output; |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Categories Dropdown |
||
| 239 | * |
||
| 240 | * Renders an HTML Dropdown of all the Categories. |
||
| 241 | * |
||
| 242 | * @since 1.0 |
||
| 243 | * @access public |
||
| 244 | * |
||
| 245 | * @param string $name Name attribute of the dropdown. Default is 'give_forms_categories'. |
||
| 246 | * @param int $selected Category to select automatically. Default is 0. |
||
| 247 | * @param array $args Select box options. |
||
| 248 | * |
||
| 249 | * @return string Categories dropdown. |
||
| 250 | */ |
||
| 251 | View Code Duplication | public function category_dropdown( $name = 'give_forms_categories', $selected = 0, $args = array() ) { |
|
| 252 | $categories = get_terms( 'give_forms_category', apply_filters( 'give_forms_category_dropdown', array() ) ); |
||
| 253 | $options = array(); |
||
| 254 | |||
| 255 | foreach ( $categories as $category ) { |
||
| 256 | $options[ absint( $category->term_id ) ] = esc_html( $category->name ); |
||
| 257 | } |
||
| 258 | |||
| 259 | $output = $this->select( wp_parse_args( $args, array( |
||
| 260 | 'name' => $name, |
||
| 261 | 'selected' => $selected, |
||
| 262 | 'options' => $options, |
||
| 263 | 'show_option_all' => esc_html__( 'All Categories', 'give' ), |
||
| 264 | 'show_option_none' => false, |
||
| 265 | ) ) ); |
||
| 266 | |||
| 267 | return $output; |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Tags Dropdown |
||
| 272 | * |
||
| 273 | * Renders an HTML Dropdown of all the Tags. |
||
| 274 | * |
||
| 275 | * @since 1.8 |
||
| 276 | * @access public |
||
| 277 | * |
||
| 278 | * @param string $name Name attribute of the dropdown. Default is 'give_forms_tags'. |
||
| 279 | * @param int $selected Tag to select automatically. Default is 0. |
||
| 280 | * @param array $args Select box options. |
||
| 281 | * |
||
| 282 | * @return string Tags dropdown. |
||
| 283 | */ |
||
| 284 | View Code Duplication | public function tags_dropdown( $name = 'give_forms_tags', $selected = 0, $args = array() ) { |
|
| 285 | $tags = get_terms( 'give_forms_tag', apply_filters( 'give_forms_tag_dropdown', array() ) ); |
||
| 286 | $options = array(); |
||
| 287 | |||
| 288 | foreach ( $tags as $tag ) { |
||
| 289 | $options[ absint( $tag->term_id ) ] = esc_html( $tag->name ); |
||
| 290 | } |
||
| 291 | |||
| 292 | $output = $this->select( wp_parse_args( $args, array( |
||
| 293 | 'name' => $name, |
||
| 294 | 'selected' => $selected, |
||
| 295 | 'options' => $options, |
||
| 296 | 'show_option_all' => esc_html__( 'All Tags', 'give' ), |
||
| 297 | 'show_option_none' => false, |
||
| 298 | ) ) ); |
||
| 299 | |||
| 300 | return $output; |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Years Dropdown |
||
| 305 | * |
||
| 306 | * Renders an HTML Dropdown of years. |
||
| 307 | * |
||
| 308 | * @since 1.0 |
||
| 309 | * @access public |
||
| 310 | * |
||
| 311 | * @param string $name Name attribute of the dropdown. Default is 'year'. |
||
| 312 | * @param int $selected Year to select automatically. Default is 0. |
||
| 313 | * @param int $years_before Number of years before the current year the dropdown should start with. Default is 5. |
||
| 314 | * @param int $years_after Number of years after the current year the dropdown should finish at. Default is 0. |
||
| 315 | * |
||
| 316 | * @return string Years dropdown. |
||
| 317 | */ |
||
| 318 | public function year_dropdown( $name = 'year', $selected = 0, $years_before = 5, $years_after = 0 ) { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Months Dropdown |
||
| 343 | * |
||
| 344 | * Renders an HTML Dropdown of months. |
||
| 345 | * |
||
| 346 | * @since 1.0 |
||
| 347 | * @access public |
||
| 348 | * |
||
| 349 | * @param string $name Name attribute of the dropdown. Default is 'month'. |
||
| 350 | * @param int $selected Month to select automatically. Default is 0. |
||
| 351 | * |
||
| 352 | * @return string Months dropdown. |
||
| 353 | */ |
||
| 354 | public function month_dropdown( $name = 'month', $selected = 0 ) { |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Dropdown |
||
| 377 | * |
||
| 378 | * Renders an HTML Dropdown. |
||
| 379 | * |
||
| 380 | * @since 1.0 |
||
| 381 | * @access public |
||
| 382 | * |
||
| 383 | * @param array $args Arguments for the dropdown. |
||
| 384 | * |
||
| 385 | * @return string The dropdown. |
||
| 386 | */ |
||
| 387 | public function select( $args = array() ) { |
||
| 388 | $defaults = array( |
||
| 389 | 'options' => array(), |
||
| 390 | 'name' => null, |
||
| 391 | 'class' => '', |
||
| 392 | 'id' => '', |
||
| 393 | 'selected' => 0, |
||
| 394 | 'chosen' => false, |
||
| 395 | 'placeholder' => null, |
||
| 396 | 'multiple' => false, |
||
| 397 | 'select_atts' => false, |
||
| 398 | 'show_option_all' => __( 'All', 'give' ), |
||
| 399 | 'show_option_none' => __( 'None', 'give' ), |
||
| 400 | 'data' => array(), |
||
| 401 | 'readonly' => false, |
||
| 402 | 'disabled' => false, |
||
| 403 | ); |
||
| 404 | |||
| 405 | $args = wp_parse_args( $args, $defaults ); |
||
| 406 | |||
| 407 | $data_elements = ''; |
||
| 408 | foreach ( $args['data'] as $key => $value ) { |
||
| 409 | $data_elements .= ' data-' . esc_attr( $key ) . '="' . esc_attr( $value ) . '"'; |
||
| 410 | } |
||
| 411 | |||
| 412 | if ( $args['multiple'] ) { |
||
| 413 | $multiple = ' MULTIPLE'; |
||
| 414 | } else { |
||
| 415 | $multiple = ''; |
||
| 416 | } |
||
| 417 | |||
| 418 | if ( $args['chosen'] ) { |
||
| 419 | $args['class'] .= ' give-select-chosen'; |
||
| 420 | } |
||
| 421 | |||
| 422 | if ( $args['placeholder'] ) { |
||
| 423 | $placeholder = $args['placeholder']; |
||
| 424 | } else { |
||
| 425 | $placeholder = ''; |
||
| 426 | } |
||
| 427 | |||
| 428 | $output = '<select name="' . esc_attr( $args['name'] ) . '" id="' . esc_attr( sanitize_key( str_replace( '-', '_', $args['id'] ) ) ) . '" class="give-select ' . esc_attr( $args['class'] ) . '"' . $multiple . ' ' . $args['select_atts'] . ' data-placeholder="' . $placeholder . '"' . $data_elements . '>'; |
||
| 429 | |||
| 430 | View Code Duplication | if ( $args['show_option_all'] ) { |
|
| 431 | if ( $args['multiple'] ) { |
||
| 432 | $selected = selected( true, in_array( 0, $args['selected'] ), false ); |
||
| 433 | } else { |
||
| 434 | $selected = selected( $args['selected'], 0, false ); |
||
| 435 | } |
||
| 436 | $output .= '<option value="all"' . $selected . '>' . esc_html( $args['show_option_all'] ) . '</option>'; |
||
| 437 | } |
||
| 438 | |||
| 439 | if ( ! empty( $args['options'] ) ) { |
||
| 440 | |||
| 441 | View Code Duplication | if ( $args['show_option_none'] ) { |
|
| 442 | if ( $args['multiple'] ) { |
||
| 443 | $selected = selected( true, in_array( - 1, $args['selected'] ), false ); |
||
| 444 | } else { |
||
| 445 | $selected = selected( $args['selected'], - 1, false ); |
||
| 446 | } |
||
| 447 | $output .= '<option value="-1"' . $selected . '>' . esc_html( $args['show_option_none'] ) . '</option>'; |
||
| 448 | } |
||
| 449 | |||
| 450 | foreach ( $args['options'] as $key => $option ) { |
||
| 451 | |||
| 452 | if ( $args['multiple'] && is_array( $args['selected'] ) ) { |
||
| 453 | $selected = selected( true, in_array( $key, $args['selected'] ), false ); |
||
| 454 | } else { |
||
| 455 | $selected = selected( $args['selected'], $key, false ); |
||
| 456 | } |
||
| 457 | |||
| 458 | $output .= '<option value="' . esc_attr( $key ) . '"' . $selected . '>' . esc_html( $option ) . '</option>'; |
||
| 459 | } |
||
| 460 | } |
||
| 461 | |||
| 462 | $output .= '</select>'; |
||
| 463 | |||
| 464 | return $output; |
||
| 465 | } |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Checkbox |
||
| 469 | * |
||
| 470 | * Renders an HTML Checkbox. |
||
| 471 | * |
||
| 472 | * @since 1.0 |
||
| 473 | * @access public |
||
| 474 | * |
||
| 475 | * @param array $args Arguments for the Checkbox. |
||
| 476 | * |
||
| 477 | * @return string The checkbox. |
||
| 478 | */ |
||
| 479 | public function checkbox( $args = array() ) { |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Text Field |
||
| 506 | * |
||
| 507 | * Renders an HTML Text field. |
||
| 508 | * |
||
| 509 | * @since 1.0 |
||
| 510 | * @access public |
||
| 511 | * |
||
| 512 | * @param array $args Arguments for the text field. |
||
| 513 | * |
||
| 514 | * @return string The text field. |
||
| 515 | */ |
||
| 516 | public function text( $args = array() ) { |
||
| 517 | // Backwards compatibility. |
||
| 518 | if ( func_num_args() > 1 ) { |
||
| 519 | $args = func_get_args(); |
||
| 520 | |||
| 521 | $name = $args[0]; |
||
| 522 | $value = isset( $args[1] ) ? $args[1] : ''; |
||
| 523 | $label = isset( $args[2] ) ? $args[2] : ''; |
||
| 524 | $desc = isset( $args[3] ) ? $args[3] : ''; |
||
| 525 | } |
||
| 526 | |||
| 527 | $defaults = array( |
||
| 528 | 'name' => isset( $name ) ? $name : 'text', |
||
| 529 | 'value' => isset( $value ) ? $value : null, |
||
| 530 | 'label' => isset( $label ) ? $label : null, |
||
| 531 | 'desc' => isset( $desc ) ? $desc : null, |
||
| 532 | 'placeholder' => '', |
||
| 533 | 'class' => 'regular-text', |
||
| 534 | 'disabled' => false, |
||
| 535 | 'autocomplete' => '', |
||
| 536 | 'data' => false, |
||
| 537 | ); |
||
| 538 | |||
| 539 | $args = wp_parse_args( $args, $defaults ); |
||
| 540 | |||
| 541 | $disabled = ''; |
||
| 542 | if ( $args['disabled'] ) { |
||
| 543 | $disabled = ' disabled="disabled"'; |
||
| 544 | } |
||
| 545 | |||
| 546 | $data = ''; |
||
| 547 | if ( ! empty( $args['data'] ) ) { |
||
| 548 | foreach ( $args['data'] as $key => $value ) { |
||
| 549 | $data .= 'data-' . $key . '="' . $value . '" '; |
||
| 550 | } |
||
| 551 | } |
||
| 552 | |||
| 553 | $output = '<span id="give-' . sanitize_key( $args['name'] ) . '-wrap">'; |
||
| 554 | |||
| 555 | $output .= '<label class="give-label" for="give-' . sanitize_key( $args['name'] ) . '">' . esc_html( $args['label'] ) . '</label>'; |
||
| 556 | |||
| 557 | View Code Duplication | if ( ! empty( $args['desc'] ) ) { |
|
| 558 | $output .= '<span class="give-description">' . esc_html( $args['desc'] ) . '</span>'; |
||
| 559 | } |
||
| 560 | |||
| 561 | $output .= '<input type="text" name="' . esc_attr( $args['name'] ) . '" id="' . esc_attr( $args['name'] ) . '" autocomplete="' . esc_attr( $args['autocomplete'] ) . '" value="' . esc_attr( $args['value'] ) . '" placeholder="' . esc_attr( $args['placeholder'] ) . '" class="' . $args['class'] . '" ' . $data . '' . $disabled . '/>'; |
||
| 562 | |||
| 563 | $output .= '</span>'; |
||
| 564 | |||
| 565 | return $output; |
||
| 566 | } |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Date Picker |
||
| 570 | * |
||
| 571 | * Renders a date picker field. |
||
| 572 | * |
||
| 573 | * @since 1.5 |
||
| 574 | * @access public |
||
| 575 | * |
||
| 576 | * @param array $args Arguments for the date picker. |
||
| 577 | * |
||
| 578 | * @return string The date picker. |
||
| 579 | */ |
||
| 580 | public function date_field( $args = array() ) { |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Textarea |
||
| 593 | * |
||
| 594 | * Renders an HTML textarea. |
||
| 595 | * |
||
| 596 | * @since 1.0 |
||
| 597 | * @access public |
||
| 598 | * |
||
| 599 | * @param array $args Arguments for the textarea. |
||
| 600 | * |
||
| 601 | * @return string The textarea. |
||
| 602 | */ |
||
| 603 | public function textarea( $args = array() ) { |
||
| 604 | $defaults = array( |
||
| 605 | 'name' => 'textarea', |
||
| 606 | 'value' => null, |
||
| 607 | 'label' => null, |
||
| 608 | 'desc' => null, |
||
| 609 | 'class' => 'large-text', |
||
| 610 | 'disabled' => false, |
||
| 611 | ); |
||
| 612 | |||
| 613 | $args = wp_parse_args( $args, $defaults ); |
||
| 614 | |||
| 615 | $disabled = ''; |
||
| 616 | if ( $args['disabled'] ) { |
||
| 617 | $disabled = ' disabled="disabled"'; |
||
| 618 | } |
||
| 619 | |||
| 620 | $output = '<span id="give-' . sanitize_key( $args['name'] ) . '-wrap">'; |
||
| 621 | |||
| 622 | $output .= '<label class="give-label" for="give-' . sanitize_key( $args['name'] ) . '">' . esc_html( $args['label'] ) . '</label>'; |
||
| 623 | |||
| 624 | $output .= '<textarea name="' . esc_attr( $args['name'] ) . '" id="' . esc_attr( $args['name'] ) . '" class="' . $args['class'] . '"' . $disabled . '>' . esc_attr( $args['value'] ) . '</textarea>'; |
||
| 625 | |||
| 626 | View Code Duplication | if ( ! empty( $args['desc'] ) ) { |
|
| 627 | $output .= '<span class="give-description">' . esc_html( $args['desc'] ) . '</span>'; |
||
| 628 | } |
||
| 629 | |||
| 630 | $output .= '</span>'; |
||
| 631 | |||
| 632 | return $output; |
||
| 633 | } |
||
| 634 | |||
| 635 | /** |
||
| 636 | * User Search Field |
||
| 637 | * |
||
| 638 | * Renders an ajax user search field. |
||
| 639 | * |
||
| 640 | * @since 1.0 |
||
| 641 | * @access public |
||
| 642 | * |
||
| 643 | * @param array $args Arguments for the search field. |
||
| 644 | * |
||
| 645 | * @return string The text field with ajax search. |
||
| 646 | */ |
||
| 647 | public function ajax_user_search( $args = array() ) { |
||
| 715 | |||
| 716 | } |
||
| 717 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.