Complex classes like Give_DB_Customers 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_DB_Customers, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class Give_DB_Customers extends Give_DB { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Get things started |
||
| 28 | * |
||
| 29 | * @access public |
||
| 30 | * @since 1.0 |
||
| 31 | */ |
||
| 32 | 52 | public function __construct() { |
|
| 33 | |||
| 34 | 52 | global $wpdb; |
|
| 35 | |||
| 36 | 52 | $this->table_name = $wpdb->prefix . 'give_customers'; |
|
| 37 | 52 | $this->primary_key = 'id'; |
|
| 38 | 52 | $this->version = '1.0'; |
|
| 39 | |||
| 40 | 52 | add_action( 'profile_update', array( $this, 'update_customer_email_on_user_update' ), 10, 2 ); |
|
| 41 | |||
| 42 | 52 | } |
|
| 43 | |||
| 44 | /** |
||
| 45 | * Get columns and formats |
||
| 46 | * |
||
| 47 | * @access public |
||
| 48 | * @since 1.0 |
||
| 49 | */ |
||
| 50 | 52 | public function get_columns() { |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Get default column values |
||
| 66 | * |
||
| 67 | * @access public |
||
| 68 | * @since 1.0 |
||
| 69 | */ |
||
| 70 | 52 | public function get_column_defaults() { |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Add a customer |
||
| 85 | * |
||
| 86 | * @access public |
||
| 87 | * @since 1.0 |
||
| 88 | */ |
||
| 89 | 52 | public function add( $data = array() ) { |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Delete a customer |
||
| 144 | * |
||
| 145 | * NOTE: This should not be called directly as it does not make necessary changes to |
||
| 146 | * the payment meta and logs. Use give_customer_delete() instead |
||
| 147 | * |
||
| 148 | * @access public |
||
| 149 | * @since 1.0 |
||
| 150 | * |
||
| 151 | * @param bool|false $_id_or_email |
||
| 152 | * |
||
| 153 | * @return bool|false|int |
||
| 154 | */ |
||
| 155 | public function delete( $_id_or_email = false ) { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Checks if a customer exists |
||
| 178 | * |
||
| 179 | * @access public |
||
| 180 | * @since 1.0 |
||
| 181 | */ |
||
| 182 | 1 | public function exists( $value = '', $field = 'email' ) { |
|
| 183 | |||
| 184 | 1 | $columns = $this->get_columns(); |
|
| 185 | 1 | if ( ! array_key_exists( $field, $columns ) ) { |
|
| 186 | return false; |
||
| 187 | } |
||
| 188 | |||
| 189 | 1 | return (bool) $this->get_column_by( 'id', $field, $value ); |
|
| 190 | |||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Attaches a payment ID to a customer |
||
| 195 | * |
||
| 196 | * @access public |
||
| 197 | * @since 1.0 |
||
| 198 | */ |
||
| 199 | 2 | public function attach_payment( $customer_id = 0, $payment_id = 0 ) { |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Removes a payment ID from a customer |
||
| 214 | * |
||
| 215 | * @access public |
||
| 216 | * @since 1.0 |
||
| 217 | */ |
||
| 218 | 1 | public function remove_payment( $customer_id = 0, $payment_id = 0 ) { |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Increments customer purchase stats |
||
| 233 | * |
||
| 234 | * @param int $customer_id |
||
| 235 | * @param float $amount |
||
| 236 | * |
||
| 237 | * @return bool |
||
| 238 | */ |
||
| 239 | 1 | public function increment_stats( $customer_id = 0, $amount = 0.00 ) { |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Decrements customer purchase stats |
||
| 256 | * |
||
| 257 | * @access public |
||
| 258 | * @since 1.0 |
||
| 259 | */ |
||
| 260 | 1 | public function decrement_stats( $customer_id = 0, $amount = 0.00 ) { |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Updates the email address of a customer record when the email on a user is updated |
||
| 277 | * |
||
| 278 | * @access public |
||
| 279 | * |
||
| 280 | * @since 1.4.3 |
||
| 281 | * |
||
| 282 | * @param int $user_id |
||
| 283 | * @param $old_user_data |
||
| 284 | * |
||
| 285 | * @return bool |
||
| 286 | */ |
||
| 287 | public function update_customer_email_on_user_update( $user_id = 0, $old_user_data ) { |
||
| 288 | |||
| 289 | $customer = new Give_Customer( $user_id, true ); |
||
| 290 | |||
| 291 | if( ! $customer ) { |
||
| 292 | return false; |
||
| 293 | } |
||
| 294 | |||
| 295 | $user = get_userdata( $user_id ); |
||
| 296 | |||
| 297 | if( ! empty( $user ) && $user->user_email !== $customer->email ) { |
||
| 298 | |||
| 299 | if( ! $this->get_customer_by( 'email', $user->user_email ) ) { |
||
| 300 | |||
| 301 | $success = $this->update( $customer->id, array( 'email' => $user->user_email ) ); |
||
| 302 | |||
| 303 | if( $success ) { |
||
| 304 | // Update some payment meta if we need to |
||
| 305 | $payments_array = explode( ',', $customer->payment_ids ); |
||
| 306 | |||
| 307 | if( ! empty( $payments_array ) ) { |
||
| 308 | |||
| 309 | foreach ( $payments_array as $payment_id ) { |
||
| 310 | |||
| 311 | give_update_payment_meta( $payment_id, 'email', $user->user_email ); |
||
| 312 | |||
| 313 | } |
||
| 314 | |||
| 315 | } |
||
| 316 | |||
| 317 | do_action( 'give_update_customer_email_on_user_update', $user, $customer ); |
||
| 318 | |||
| 319 | } |
||
| 320 | |||
| 321 | } |
||
| 322 | |||
| 323 | } |
||
| 324 | |||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Retrieves a single customer from the database |
||
| 329 | * |
||
| 330 | * @access public |
||
| 331 | * @since 1.0 |
||
| 332 | * |
||
| 333 | * @param string $field id or email |
||
| 334 | * @param mixed $value The Customer ID or email to search |
||
| 335 | * |
||
| 336 | * @return mixed Upon success, an object of the customer. Upon failure, NULL |
||
| 337 | */ |
||
| 338 | 52 | public function get_customer_by( $field = 'id', $value = 0 ) { |
|
| 339 | 52 | global $wpdb; |
|
| 340 | |||
| 341 | 52 | if ( empty( $field ) || empty( $value ) ) { |
|
| 342 | 5 | return null; |
|
| 343 | } |
||
| 344 | |||
| 345 | 52 | if ( 'id' == $field || 'user_id' == $field ) { |
|
| 346 | // Make sure the value is numeric to avoid casting objects, for example, |
||
| 347 | // to int 1. |
||
| 348 | 52 | if ( ! is_numeric( $value ) ) { |
|
| 349 | return false; |
||
| 350 | } |
||
| 351 | |||
| 352 | 52 | $value = intval( $value ); |
|
| 353 | |||
| 354 | 52 | if ( $value < 1 ) { |
|
| 355 | return false; |
||
| 356 | } |
||
| 357 | |||
| 358 | 52 | } elseif ( 'email' === $field ) { |
|
| 359 | |||
| 360 | 52 | if ( ! is_email( $value ) ) { |
|
| 361 | return false; |
||
| 362 | } |
||
| 363 | |||
| 364 | 52 | $value = trim( $value ); |
|
| 365 | 52 | } |
|
| 366 | |||
| 367 | 52 | if ( ! $value ) { |
|
| 368 | return false; |
||
| 369 | } |
||
| 370 | |||
| 371 | switch ( $field ) { |
||
| 372 | 52 | case 'id': |
|
| 373 | 52 | $db_field = 'id'; |
|
| 374 | 52 | break; |
|
| 375 | 52 | case 'email': |
|
| 376 | 52 | $value = sanitize_text_field( $value ); |
|
| 377 | 52 | $db_field = 'email'; |
|
| 378 | 52 | break; |
|
| 379 | 6 | case 'user_id': |
|
| 380 | 6 | $db_field = 'user_id'; |
|
| 381 | 6 | break; |
|
| 382 | default: |
||
| 383 | return false; |
||
| 384 | } |
||
| 385 | |||
| 386 | 52 | if ( ! $customer = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->table_name WHERE $db_field = %s LIMIT 1", $value ) ) ) { |
|
| 387 | 52 | return false; |
|
| 388 | } |
||
| 389 | |||
| 390 | 52 | return $customer; |
|
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Retrieve customers from the database |
||
| 395 | * |
||
| 396 | * @access public |
||
| 397 | * @since 1.0 |
||
| 398 | */ |
||
| 399 | 2 | public function get_customers( $args = array() ) { |
|
| 400 | |||
| 401 | 2 | global $wpdb; |
|
| 402 | |||
| 403 | $defaults = array( |
||
| 404 | 2 | 'number' => 20, |
|
| 405 | 2 | 'offset' => 0, |
|
| 406 | 2 | 'user_id' => 0, |
|
| 407 | 2 | 'orderby' => 'id', |
|
| 408 | 'order' => 'DESC' |
||
| 409 | 2 | ); |
|
| 410 | |||
| 411 | 2 | $args = wp_parse_args( $args, $defaults ); |
|
| 412 | |||
| 413 | 2 | if ( $args['number'] < 1 ) { |
|
| 414 | $args['number'] = 999999999999; |
||
| 415 | } |
||
| 416 | |||
| 417 | 2 | $where = ' WHERE 1=1 '; |
|
| 418 | |||
| 419 | // specific customers |
||
| 420 | 2 | if ( ! empty( $args['id'] ) ) { |
|
| 421 | |||
| 422 | if ( is_array( $args['id'] ) ) { |
||
| 423 | $ids = implode( ',', array_map( 'intval', $args['id'] ) ); |
||
| 424 | } else { |
||
| 425 | $ids = intval( $args['id'] ); |
||
| 426 | } |
||
| 427 | |||
| 428 | $where .= " AND `id` IN( {$ids} ) "; |
||
| 429 | |||
| 430 | } |
||
| 431 | |||
| 432 | // customers for specific user accounts |
||
| 433 | 2 | if ( ! empty( $args['user_id'] ) ) { |
|
| 434 | |||
| 435 | if ( is_array( $args['user_id'] ) ) { |
||
| 436 | $user_ids = implode( ',', array_map( 'intval', $args['user_id'] ) ); |
||
| 437 | } else { |
||
| 438 | $user_ids = intval( $args['user_id'] ); |
||
| 439 | } |
||
| 440 | |||
| 441 | $where .= " AND `user_id` IN( {$user_ids} ) "; |
||
| 442 | |||
| 443 | } |
||
| 444 | |||
| 445 | //specific customers by email |
||
| 446 | 2 | if( ! empty( $args['email'] ) ) { |
|
| 447 | |||
| 448 | if( is_array( $args['email'] ) ) { |
||
| 449 | |||
| 450 | $emails_count = count( $args['email'] ); |
||
| 451 | $emails_placeholder = array_fill( 0, $emails_count, '%s' ); |
||
| 452 | $emails = implode( ', ', $emails_placeholder ); |
||
| 453 | |||
| 454 | $where .= $wpdb->prepare( " AND `email` IN( $emails ) ", $args['email'] ); |
||
| 455 | } else { |
||
| 456 | $where .= $wpdb->prepare( " AND `email` = %s ", $args['email'] ); |
||
| 457 | } |
||
| 458 | } |
||
| 459 | |||
| 460 | // specific customers by name |
||
| 461 | 2 | if( ! empty( $args['name'] ) ) { |
|
| 462 | $where .= $wpdb->prepare( " AND `name` LIKE '%%%%" . '%s' . "%%%%' ", $args['name'] ); |
||
| 463 | } |
||
| 464 | |||
| 465 | // Customers created for a specific date or in a date range |
||
| 466 | 2 | if ( ! empty( $args['date'] ) ) { |
|
| 467 | |||
| 468 | if ( is_array( $args['date'] ) ) { |
||
| 469 | |||
| 470 | if ( ! empty( $args['date']['start'] ) ) { |
||
| 471 | |||
| 472 | $start = date( 'Y-m-d H:i:s', strtotime( $args['date']['start'] ) ); |
||
| 473 | |||
| 474 | $where .= " AND `date_created` >= '{$start}'"; |
||
| 475 | |||
| 476 | } |
||
| 477 | |||
| 478 | if ( ! empty( $args['date']['end'] ) ) { |
||
| 479 | |||
| 480 | $end = date( 'Y-m-d H:i:s', strtotime( $args['date']['end'] ) ); |
||
| 481 | |||
| 482 | $where .= " AND `date_created` <= '{$end}'"; |
||
| 483 | |||
| 484 | } |
||
| 485 | |||
| 486 | } else { |
||
| 487 | |||
| 488 | $year = date( 'Y', strtotime( $args['date'] ) ); |
||
| 489 | $month = date( 'm', strtotime( $args['date'] ) ); |
||
| 490 | $day = date( 'd', strtotime( $args['date'] ) ); |
||
| 491 | |||
| 492 | $where .= " AND $year = YEAR ( date_created ) AND $month = MONTH ( date_created ) AND $day = DAY ( date_created )"; |
||
| 493 | } |
||
| 494 | |||
| 495 | } |
||
| 496 | |||
| 497 | 2 | $args['orderby'] = ! array_key_exists( $args['orderby'], $this->get_columns() ) ? 'id' : $args['orderby']; |
|
| 498 | |||
| 499 | 2 | if ( 'purchase_value' == $args['orderby'] ) { |
|
| 500 | $args['orderby'] = 'purchase_value+0'; |
||
| 501 | } |
||
| 502 | |||
| 503 | 2 | $cache_key = md5( 'give_customers_' . serialize( $args ) ); |
|
| 504 | |||
| 505 | 2 | $customers = wp_cache_get( $cache_key, 'customers' ); |
|
| 506 | |||
| 507 | 2 | $args['orderby'] = esc_sql( $args['orderby'] ); |
|
| 508 | 2 | $args['order'] = esc_sql( $args['order'] ); |
|
| 509 | |||
| 510 | 2 | if ( $customers === false ) { |
|
| 511 | 2 | $customers = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $this->table_name $where ORDER BY {$args['orderby']} {$args['order']} LIMIT %d,%d;", absint( $args['offset'] ), absint( $args['number'] ) ) ); |
|
| 512 | 2 | wp_cache_set( $cache_key, $customers, 'customers', 3600 ); |
|
| 513 | 2 | } |
|
| 514 | |||
| 515 | 2 | return $customers; |
|
| 516 | |||
| 517 | } |
||
| 518 | |||
| 519 | |||
| 520 | /** |
||
| 521 | * Count the total number of customers in the database |
||
| 522 | * |
||
| 523 | * @access public |
||
| 524 | * @since 1.0 |
||
| 525 | */ |
||
| 526 | 1 | public function count( $args = array() ) { |
|
| 565 | |||
| 566 | /** |
||
| 567 | * Create the table |
||
| 568 | * |
||
| 569 | * @access public |
||
| 570 | * @since 1.0 |
||
| 571 | */ |
||
| 572 | 2 | public function create_table() { |
|
| 573 | |||
| 574 | 2 | require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
|
| 575 | |||
| 576 | 2 | $sql = "CREATE TABLE " . $this->table_name . " ( |
|
| 577 | id bigint(20) NOT NULL AUTO_INCREMENT, |
||
| 578 | user_id bigint(20) NOT NULL, |
||
| 579 | email varchar(50) NOT NULL, |
||
| 580 | name mediumtext NOT NULL, |
||
| 581 | purchase_value mediumtext NOT NULL, |
||
| 582 | purchase_count bigint(20) NOT NULL, |
||
| 583 | payment_ids longtext NOT NULL, |
||
| 584 | notes longtext NOT NULL, |
||
| 585 | date_created datetime NOT NULL, |
||
| 586 | PRIMARY KEY (id), |
||
| 587 | UNIQUE KEY email (email), |
||
| 588 | KEY user (user_id) |
||
| 589 | 2 | ) CHARACTER SET utf8 COLLATE utf8_general_ci;"; |
|
| 590 | |||
| 591 | 2 | dbDelta( $sql ); |
|
| 592 | |||
| 593 | 2 | update_option( $this->table_name . '_db_version', $this->version ); |
|
| 594 | 2 | } |
|
| 595 | |||
| 596 | /** |
||
| 597 | * Check if the Customers table was ever installed |
||
| 598 | * |
||
| 599 | * @since 1.4.3 |
||
| 600 | * @return bool Returns if the customers table was installed and upgrade routine run |
||
| 601 | */ |
||
| 602 | 2 | public function installed() { |
|
| 605 | } |
||
| 606 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.