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_Donate_Form 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_Donate_Form, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class Give_Donate_Form { |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The donation ID. |
||
| 36 | * |
||
| 37 | * @since 1.0 |
||
| 38 | * @access public |
||
| 39 | * |
||
| 40 | * @var int |
||
| 41 | */ |
||
| 42 | public $ID = 0; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The donation price. |
||
| 46 | * |
||
| 47 | * @since 1.0 |
||
| 48 | * @access private |
||
| 49 | * |
||
| 50 | * @var float |
||
| 51 | */ |
||
| 52 | private $price; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The minimum donation price. |
||
| 56 | * |
||
| 57 | * @since 1.3.6 |
||
| 58 | * @access private |
||
| 59 | * |
||
| 60 | * @var float |
||
| 61 | */ |
||
| 62 | private $minimum_price; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * The donation prices, if Price Levels are enabled. |
||
| 66 | * |
||
| 67 | * @since 1.0 |
||
| 68 | * @access private |
||
| 69 | * |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | private $prices; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The donation goal. |
||
| 76 | * |
||
| 77 | * @since 1.0 |
||
| 78 | * @access private |
||
| 79 | * |
||
| 80 | * @var float |
||
| 81 | */ |
||
| 82 | private $goal; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * The form's sale count. |
||
| 86 | * |
||
| 87 | * @since 1.0 |
||
| 88 | * @access private |
||
| 89 | * |
||
| 90 | * @var int |
||
| 91 | */ |
||
| 92 | private $sales; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * The form's total earnings |
||
| 96 | * |
||
| 97 | * @since 1.0 |
||
| 98 | * @access private |
||
| 99 | * |
||
| 100 | * @var float |
||
| 101 | */ |
||
| 102 | private $earnings; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Declare the default properties in WP_Post as we can't extend it |
||
| 106 | * Anything we've declared above has been removed. |
||
| 107 | */ |
||
| 108 | |||
| 109 | /** |
||
| 110 | * The post author |
||
| 111 | * |
||
| 112 | * @since 1.0 |
||
| 113 | * @access public |
||
| 114 | * |
||
| 115 | * @var int |
||
| 116 | */ |
||
| 117 | public $post_author = 0; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * The post date |
||
| 121 | * |
||
| 122 | * @since 1.0 |
||
| 123 | * @access public |
||
| 124 | * |
||
| 125 | * @var string |
||
| 126 | */ |
||
| 127 | public $post_date = '0000-00-00 00:00:00'; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * The post GTM date |
||
| 131 | * |
||
| 132 | * @since 1.0 |
||
| 133 | * @access public |
||
| 134 | * |
||
| 135 | * @var string |
||
| 136 | */ |
||
| 137 | public $post_date_gmt = '0000-00-00 00:00:00'; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * The post content |
||
| 141 | * |
||
| 142 | * @since 1.0 |
||
| 143 | * @access public |
||
| 144 | * |
||
| 145 | * @var string |
||
| 146 | */ |
||
| 147 | public $post_content = ''; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * The post title |
||
| 151 | * |
||
| 152 | * @since 1.0 |
||
| 153 | * @access public |
||
| 154 | * |
||
| 155 | * @var string |
||
| 156 | */ |
||
| 157 | public $post_title = ''; |
||
| 158 | |||
| 159 | /** |
||
| 160 | * The post excerpt |
||
| 161 | * |
||
| 162 | * @since 1.0 |
||
| 163 | * @access public |
||
| 164 | * |
||
| 165 | * @var string |
||
| 166 | */ |
||
| 167 | public $post_excerpt = ''; |
||
| 168 | |||
| 169 | /** |
||
| 170 | * The post status |
||
| 171 | * |
||
| 172 | * @since 1.0 |
||
| 173 | * @access public |
||
| 174 | * |
||
| 175 | * @var string |
||
| 176 | */ |
||
| 177 | public $post_status = 'publish'; |
||
| 178 | |||
| 179 | /** |
||
| 180 | * The comment status |
||
| 181 | * |
||
| 182 | * @since 1.0 |
||
| 183 | * @access public |
||
| 184 | * |
||
| 185 | * @var string |
||
| 186 | */ |
||
| 187 | public $comment_status = 'open'; |
||
| 188 | |||
| 189 | /** |
||
| 190 | * The ping status |
||
| 191 | * |
||
| 192 | * @since 1.0 |
||
| 193 | * @access public |
||
| 194 | * |
||
| 195 | * @var string |
||
| 196 | */ |
||
| 197 | public $ping_status = 'open'; |
||
| 198 | |||
| 199 | /** |
||
| 200 | * The post password |
||
| 201 | * |
||
| 202 | * @since 1.0 |
||
| 203 | * @access public |
||
| 204 | * |
||
| 205 | * @var string |
||
| 206 | */ |
||
| 207 | public $post_password = ''; |
||
| 208 | |||
| 209 | /** |
||
| 210 | * The post name |
||
| 211 | * |
||
| 212 | * @since 1.0 |
||
| 213 | * @access public |
||
| 214 | * |
||
| 215 | * @var string |
||
| 216 | */ |
||
| 217 | public $post_name = ''; |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Ping |
||
| 221 | * |
||
| 222 | * @since 1.0 |
||
| 223 | * @access public |
||
| 224 | * |
||
| 225 | * @var string |
||
| 226 | */ |
||
| 227 | public $to_ping = ''; |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Pinged |
||
| 231 | * |
||
| 232 | * @since 1.0 |
||
| 233 | * @access public |
||
| 234 | * |
||
| 235 | * @var string |
||
| 236 | */ |
||
| 237 | public $pinged = ''; |
||
| 238 | |||
| 239 | /** |
||
| 240 | * The post modified date |
||
| 241 | * |
||
| 242 | * @since 1.0 |
||
| 243 | * @access public |
||
| 244 | * |
||
| 245 | * @var string |
||
| 246 | */ |
||
| 247 | public $post_modified = '0000-00-00 00:00:00'; |
||
| 248 | |||
| 249 | /** |
||
| 250 | * The post modified GTM date |
||
| 251 | * |
||
| 252 | * @since 1.0 |
||
| 253 | * @access public |
||
| 254 | * |
||
| 255 | * @var string |
||
| 256 | */ |
||
| 257 | public $post_modified_gmt = '0000-00-00 00:00:00'; |
||
| 258 | |||
| 259 | /** |
||
| 260 | * The post filtered content |
||
| 261 | * |
||
| 262 | * @since 1.0 |
||
| 263 | * @access public |
||
| 264 | * |
||
| 265 | * @var string |
||
| 266 | */ |
||
| 267 | public $post_content_filtered = ''; |
||
| 268 | |||
| 269 | /** |
||
| 270 | * The post parent |
||
| 271 | * |
||
| 272 | * @since 1.0 |
||
| 273 | * @access public |
||
| 274 | * |
||
| 275 | * @var int |
||
| 276 | */ |
||
| 277 | public $post_parent = 0; |
||
| 278 | |||
| 279 | /** |
||
| 280 | * The post GUID |
||
| 281 | * |
||
| 282 | * @since 1.0 |
||
| 283 | * @access public |
||
| 284 | * |
||
| 285 | * @var string |
||
| 286 | */ |
||
| 287 | public $guid = ''; |
||
| 288 | |||
| 289 | /** |
||
| 290 | * The menu order |
||
| 291 | * |
||
| 292 | * @since 1.0 |
||
| 293 | * @access public |
||
| 294 | * |
||
| 295 | * @var int |
||
| 296 | */ |
||
| 297 | public $menu_order = 0; |
||
| 298 | |||
| 299 | /** |
||
| 300 | * The mime type0 |
||
| 301 | * |
||
| 302 | * @since 1.0 |
||
| 303 | * @access public |
||
| 304 | * |
||
| 305 | * @var string |
||
| 306 | */ |
||
| 307 | public $post_mime_type = ''; |
||
| 308 | |||
| 309 | /** |
||
| 310 | * The comment count |
||
| 311 | * |
||
| 312 | * @since 1.0 |
||
| 313 | * @access public |
||
| 314 | * |
||
| 315 | * @var int |
||
| 316 | */ |
||
| 317 | public $comment_count = 0; |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Filtered |
||
| 321 | * |
||
| 322 | * @since 1.0 |
||
| 323 | * @access public |
||
| 324 | * |
||
| 325 | * @var string |
||
| 326 | */ |
||
| 327 | public $filter; |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Class Constructor |
||
| 331 | * |
||
| 332 | * Set up the Give Donate Form Class. |
||
| 333 | * |
||
| 334 | * @since 1.0 |
||
| 335 | * @access public |
||
| 336 | * |
||
| 337 | * @param bool $_id Post id. Default is false. |
||
| 338 | * @param array $_args Arguments passed. |
||
| 339 | */ |
||
| 340 | public function __construct( $_id = false, $_args = array() ) { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Given the donation form data, let's set the variables |
||
| 349 | * |
||
| 350 | * @since 1.5 |
||
| 351 | * @access private |
||
| 352 | * |
||
| 353 | * @param WP_Post $donation_form WP_Post Object for the donation form. |
||
| 354 | * |
||
| 355 | * @return bool If the setup was successful or not. |
||
| 356 | */ |
||
| 357 | private function setup_donation_form( $donation_form ) { |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Magic __get function to dispatch a call to retrieve a private property |
||
| 389 | * |
||
| 390 | * @since 1.0 |
||
| 391 | * @access public |
||
| 392 | * |
||
| 393 | * @param string $key |
||
| 394 | * |
||
| 395 | * @return mixed |
||
| 396 | */ |
||
| 397 | View Code Duplication | public function __get( $key ) { |
|
| 398 | |||
| 399 | if ( method_exists( $this, 'get_' . $key ) ) { |
||
| 400 | |||
| 401 | return call_user_func( array( $this, 'get_' . $key ) ); |
||
| 402 | |||
| 403 | } else { |
||
| 404 | |||
| 405 | /* translators: %s: property key */ |
||
| 406 | return new WP_Error( 'give-form-invalid-property', sprintf( esc_html__( 'Can\'t get property %s.', 'give' ), $key ) ); |
||
| 407 | |||
| 408 | } |
||
| 409 | |||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Creates a donation form |
||
| 414 | * |
||
| 415 | * @since 1.5 |
||
| 416 | * @access public |
||
| 417 | * |
||
| 418 | * @param array $data Array of attributes for a donation form. |
||
| 419 | * |
||
| 420 | * @return bool|int False if data isn't passed and class not instantiated for creation, or New Form ID. |
||
| 421 | */ |
||
| 422 | public function create( $data = array() ) { |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Retrieve the ID |
||
| 461 | * |
||
| 462 | * @since 1.0 |
||
| 463 | * @access public |
||
| 464 | * |
||
| 465 | * @return int Donation form ID. |
||
| 466 | */ |
||
| 467 | public function get_ID() { |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Retrieve the donation form name |
||
| 473 | * |
||
| 474 | * @since 1.5 |
||
| 475 | * @access public |
||
| 476 | * |
||
| 477 | * @return string Donation form name. |
||
| 478 | */ |
||
| 479 | public function get_name() { |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Retrieve the price |
||
| 485 | * |
||
| 486 | * @since 1.0 |
||
| 487 | * @access public |
||
| 488 | * |
||
| 489 | * @return float Price. |
||
| 490 | */ |
||
| 491 | View Code Duplication | public function get_price() { |
|
| 519 | |||
| 520 | /** |
||
| 521 | * Retrieve the minimum price. |
||
| 522 | * |
||
| 523 | * @since 1.3.6 |
||
| 524 | * @access public |
||
| 525 | * |
||
| 526 | * @return float Minimum price. |
||
| 527 | */ |
||
| 528 | public function get_minimum_price() { |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Retrieve the variable prices |
||
| 545 | * |
||
| 546 | * @since 1.0 |
||
| 547 | * @access public |
||
| 548 | * |
||
| 549 | * @return array Variable prices. |
||
| 550 | */ |
||
| 551 | public function get_prices() { |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Retrieve the goal |
||
| 573 | * |
||
| 574 | * @since 1.0 |
||
| 575 | * @access public |
||
| 576 | * |
||
| 577 | * @return float Goal. |
||
| 578 | */ |
||
| 579 | public function get_goal() { |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Determine if single price mode is enabled or disabled |
||
| 597 | * |
||
| 598 | * @since 1.0 |
||
| 599 | * @access public |
||
| 600 | * |
||
| 601 | * @return bool |
||
| 602 | */ |
||
| 603 | View Code Duplication | public function is_single_price_mode() { |
|
| 623 | |||
| 624 | /** |
||
| 625 | * Determine if custom price mode is enabled or disabled |
||
| 626 | * |
||
| 627 | * @since 1.6 |
||
| 628 | * @access public |
||
| 629 | * |
||
| 630 | * @return bool |
||
| 631 | */ |
||
| 632 | public function is_custom_price_mode() { |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Determine if custom price mode is enabled or disabled |
||
| 655 | * |
||
| 656 | * @since 1.8.18 |
||
| 657 | * @access public |
||
| 658 | * |
||
| 659 | * @param string|float $amount Donation Amount. |
||
| 660 | * |
||
| 661 | * @return bool |
||
| 662 | */ |
||
| 663 | public function is_custom_price( $amount ) { |
||
| 691 | |||
| 692 | /** |
||
| 693 | * Has Variable Prices |
||
| 694 | * |
||
| 695 | * Determine if the donation form has variable prices enabled |
||
| 696 | * |
||
| 697 | * @since 1.0 |
||
| 698 | * @access public |
||
| 699 | * |
||
| 700 | * @return bool |
||
| 701 | */ |
||
| 702 | View Code Duplication | public function has_variable_prices() { |
|
| 720 | |||
| 721 | /** |
||
| 722 | * Retrieve the donation form type, set or multi-level |
||
| 723 | * |
||
| 724 | * @since 1.5 |
||
| 725 | * @access public |
||
| 726 | * |
||
| 727 | * @return string Type of donation form, either 'set' or 'multi'. |
||
| 728 | */ |
||
| 729 | View Code Duplication | public function get_type() { |
|
| 744 | |||
| 745 | /** |
||
| 746 | * Get form tag classes. |
||
| 747 | * |
||
| 748 | * Provides the classes for the donation <form> html tag and filters for customization. |
||
| 749 | * |
||
| 750 | * @since 1.6 |
||
| 751 | * @access public |
||
| 752 | * |
||
| 753 | * @param $args |
||
| 754 | * |
||
| 755 | * @return string |
||
| 756 | */ |
||
| 757 | public function get_form_classes( $args ) { |
||
| 776 | |||
| 777 | /** |
||
| 778 | * Get form wrap Classes. |
||
| 779 | * |
||
| 780 | * Provides the classes for the donation form div wrapper and filters for customization. |
||
| 781 | * |
||
| 782 | * @access public |
||
| 783 | * |
||
| 784 | * @param $args |
||
| 785 | * |
||
| 786 | * @return string |
||
| 787 | */ |
||
| 788 | public function get_form_wrap_classes( $args ) { |
||
| 820 | |||
| 821 | /** |
||
| 822 | * Get if form type set or not. |
||
| 823 | * |
||
| 824 | * @since 1.6 |
||
| 825 | * @access public |
||
| 826 | * |
||
| 827 | * @return bool |
||
| 828 | */ |
||
| 829 | public function is_set_type_donation_form() { |
||
| 834 | |||
| 835 | /** |
||
| 836 | * Get if form type multi or not. |
||
| 837 | * |
||
| 838 | * @since 1.6 |
||
| 839 | * @access public |
||
| 840 | * |
||
| 841 | * @return bool True if form type is 'multi' and false otherwise. |
||
| 842 | */ |
||
| 843 | public function is_multi_type_donation_form() { |
||
| 849 | |||
| 850 | /** |
||
| 851 | * Retrieve the sale count for the donation form |
||
| 852 | * |
||
| 853 | * @since 1.0 |
||
| 854 | * @access public |
||
| 855 | * |
||
| 856 | * @return int Donation form sale count. |
||
| 857 | */ |
||
| 858 | View Code Duplication | public function get_sales() { |
|
| 878 | |||
| 879 | /** |
||
| 880 | * Increment the sale count by one |
||
| 881 | * |
||
| 882 | * @since 1.0 |
||
| 883 | * @access public |
||
| 884 | * |
||
| 885 | * @param int $quantity The quantity to increase the donations by. Default is 1. |
||
| 886 | * |
||
| 887 | * @return int|false New number of total sales. |
||
| 888 | */ |
||
| 889 | public function increase_sales( $quantity = 1 ) { |
||
| 905 | |||
| 906 | /** |
||
| 907 | * Decrement the sale count by one |
||
| 908 | * |
||
| 909 | * @since 1.0 |
||
| 910 | * @access public |
||
| 911 | * |
||
| 912 | * @param int $quantity The quantity to decrease by. Default is 1. |
||
| 913 | * |
||
| 914 | * @return int|false New number of total sales. |
||
| 915 | */ |
||
| 916 | public function decrease_sales( $quantity = 1 ) { |
||
| 939 | |||
| 940 | /** |
||
| 941 | * Retrieve the total earnings for the form |
||
| 942 | * |
||
| 943 | * @since 1.0 |
||
| 944 | * @access public |
||
| 945 | * |
||
| 946 | * @return float Donation form total earnings. |
||
| 947 | */ |
||
| 948 | View Code Duplication | public function get_earnings() { |
|
| 968 | |||
| 969 | /** |
||
| 970 | * Increase the earnings by the given amount |
||
| 971 | * |
||
| 972 | * @since 1.0 |
||
| 973 | * @access public |
||
| 974 | * |
||
| 975 | * @param int $amount Amount of donation. Default is 0. |
||
| 976 | * |
||
| 977 | * @return float|false |
||
| 978 | */ |
||
| 979 | View Code Duplication | public function increase_earnings( $amount = 0 ) { |
|
| 995 | |||
| 996 | /** |
||
| 997 | * Decrease the earnings by the given amount |
||
| 998 | * |
||
| 999 | * @since 1.0 |
||
| 1000 | * @access public |
||
| 1001 | * |
||
| 1002 | * @param int $amount Amount of donation. |
||
| 1003 | * |
||
| 1004 | * @return float|false |
||
| 1005 | */ |
||
| 1006 | View Code Duplication | public function decrease_earnings( $amount ) { |
|
| 1028 | |||
| 1029 | /** |
||
| 1030 | * Determine if the donation is free or if the given price ID is free |
||
| 1031 | * |
||
| 1032 | * @since 1.0 |
||
| 1033 | * @access public |
||
| 1034 | * |
||
| 1035 | * @param int $price_id Price ID. Default is false. |
||
| 1036 | * |
||
| 1037 | * @return bool |
||
| 1038 | */ |
||
| 1039 | public function is_free( $price_id = false ) { |
||
| 1057 | |||
| 1058 | /** |
||
| 1059 | * Determine if donation form closed or not |
||
| 1060 | * |
||
| 1061 | * Form will be close if: |
||
| 1062 | * a. form has fixed goal |
||
| 1063 | * b. close form when goal achieved cmb2 setting is set to 'Yes' |
||
| 1064 | * c. goal has been achieved |
||
| 1065 | * |
||
| 1066 | * @since 1.4.5 |
||
| 1067 | * @access public |
||
| 1068 | * |
||
| 1069 | * @return bool |
||
| 1070 | */ |
||
| 1071 | public function is_close_donation_form() { |
||
| 1090 | |||
| 1091 | /** |
||
| 1092 | * Updates a single meta entry for the donation form |
||
| 1093 | * |
||
| 1094 | * @since 1.5 |
||
| 1095 | * @access private |
||
| 1096 | * |
||
| 1097 | * @param string $meta_key The meta_key to update. |
||
| 1098 | * @param string|array|object $meta_value The value to put into the meta. |
||
| 1099 | * |
||
| 1100 | * @return bool The result of the update query. |
||
| 1101 | */ |
||
| 1102 | private function update_meta( $meta_key = '', $meta_value = '' ) { |
||
| 1132 | |||
| 1133 | } |
||
| 1134 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.