| Total Complexity | 68 |
| Total Lines | 580 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like GetPaid_Payment_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.
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 GetPaid_Payment_Form, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class GetPaid_Payment_Form extends GetPaid_Data { |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Which data store to load. |
||
| 14 | * |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | protected $data_store_name = 'payment_form'; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * This is the name of this object type. |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $object_type = 'payment_form'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Form Data array. This is the core form data exposed in APIs. |
||
| 28 | * |
||
| 29 | * @since 1.0.19 |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected $data = array( |
||
| 33 | 'status' => 'draft', |
||
| 34 | 'version' => '', |
||
| 35 | 'date_created' => null, |
||
| 36 | 'date_modified' => null, |
||
| 37 | 'name' => '', |
||
| 38 | 'author' => 1, |
||
| 39 | 'elements' => null, |
||
| 40 | 'items' => null, |
||
| 41 | 'earned' => 0, |
||
| 42 | 'refunded' => 0, |
||
| 43 | 'cancelled' => 0, |
||
| 44 | 'failed' => 0, |
||
| 45 | ); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Stores meta in cache for future reads. |
||
| 49 | * |
||
| 50 | * A group must be set to to enable caching. |
||
| 51 | * |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | protected $cache_group = 'getpaid_forms'; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Stores a reference to the original WP_Post object |
||
| 58 | * |
||
| 59 | * @var WP_Post |
||
| 60 | */ |
||
| 61 | protected $post = null; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Get the form if ID is passed, otherwise the form is new and empty. |
||
| 65 | * |
||
| 66 | * @param int|object|GetPaid_Payment_Form|WP_Post $form Form to read. |
||
| 67 | */ |
||
| 68 | public function __construct( $form = 0 ) { |
||
| 69 | parent::__construct( $form ); |
||
| 70 | |||
| 71 | if ( is_numeric( $form ) && $form > 0 ) { |
||
| 72 | $this->set_id( $form ); |
||
| 73 | } elseif ( $form instanceof self ) { |
||
| 74 | $this->set_id( $form->get_id() ); |
||
| 75 | } elseif ( ! empty( $form->ID ) ) { |
||
| 76 | $this->set_id( $form->ID ); |
||
| 77 | } else { |
||
| 78 | $this->set_object_read( true ); |
||
| 79 | } |
||
| 80 | |||
| 81 | // Load the datastore. |
||
| 82 | $this->data_store = GetPaid_Data_Store::load( $this->data_store_name ); |
||
| 83 | |||
| 84 | if ( $this->get_id() > 0 ) { |
||
| 85 | $this->post = get_post( $this->get_id() ); |
||
| 86 | $this->data_store->read( $this ); |
||
| 87 | } |
||
| 88 | |||
| 89 | } |
||
| 90 | |||
| 91 | /* |
||
| 92 | |-------------------------------------------------------------------------- |
||
| 93 | | CRUD methods |
||
| 94 | |-------------------------------------------------------------------------- |
||
| 95 | | |
||
| 96 | | Methods which create, read, update and delete items from the database. |
||
| 97 | | |
||
| 98 | */ |
||
| 99 | |||
| 100 | /* |
||
| 101 | |-------------------------------------------------------------------------- |
||
| 102 | | Getters |
||
| 103 | |-------------------------------------------------------------------------- |
||
| 104 | */ |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Get plugin version when the form was created. |
||
| 108 | * |
||
| 109 | * @since 1.0.19 |
||
| 110 | * @param string $context View or edit context. |
||
| 111 | * @return string |
||
| 112 | */ |
||
| 113 | public function get_version( $context = 'view' ) { |
||
| 114 | return $this->get_prop( 'version', $context ); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Get date when the form was created. |
||
| 119 | * |
||
| 120 | * @since 1.0.19 |
||
| 121 | * @param string $context View or edit context. |
||
| 122 | * @return string |
||
| 123 | */ |
||
| 124 | public function get_date_created( $context = 'view' ) { |
||
| 125 | return $this->get_prop( 'date_created', $context ); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Get GMT date when the form was created. |
||
| 130 | * |
||
| 131 | * @since 1.0.19 |
||
| 132 | * @param string $context View or edit context. |
||
| 133 | * @return string |
||
| 134 | */ |
||
| 135 | public function get_date_created_gmt( $context = 'view' ) { |
||
| 136 | $date = $this->get_date_created( $context ); |
||
| 137 | |||
| 138 | if ( $date ) { |
||
| 139 | $date = get_gmt_from_date( $date ); |
||
| 140 | } |
||
| 141 | return $date; |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Get date when the form was last modified. |
||
| 146 | * |
||
| 147 | * @since 1.0.19 |
||
| 148 | * @param string $context View or edit context. |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public function get_date_modified( $context = 'view' ) { |
||
| 152 | return $this->get_prop( 'date_modified', $context ); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Get GMT date when the form was last modified. |
||
| 157 | * |
||
| 158 | * @since 1.0.19 |
||
| 159 | * @param string $context View or edit context. |
||
| 160 | * @return string |
||
| 161 | */ |
||
| 162 | public function get_date_modified_gmt( $context = 'view' ) { |
||
| 163 | $date = $this->get_date_modified( $context ); |
||
| 164 | |||
| 165 | if ( $date ) { |
||
| 166 | $date = get_gmt_from_date( $date ); |
||
| 167 | } |
||
| 168 | return $date; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Get the form name. |
||
| 173 | * |
||
| 174 | * @since 1.0.19 |
||
| 175 | * @param string $context View or edit context. |
||
| 176 | * @return string |
||
| 177 | */ |
||
| 178 | public function get_name( $context = 'view' ) { |
||
| 179 | return $this->get_prop( 'name', $context ); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Alias of self::get_name(). |
||
| 184 | * |
||
| 185 | * @since 1.0.19 |
||
| 186 | * @param string $context View or edit context. |
||
| 187 | * @return string |
||
| 188 | */ |
||
| 189 | public function get_title( $context = 'view' ) { |
||
| 190 | return $this->get_name( $context ); |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Get the owner of the form. |
||
| 195 | * |
||
| 196 | * @since 1.0.19 |
||
| 197 | * @param string $context View or edit context. |
||
| 198 | * @return int |
||
| 199 | */ |
||
| 200 | public function get_author( $context = 'view' ) { |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Get the elements that make up the form. |
||
| 206 | * |
||
| 207 | * @since 1.0.19 |
||
| 208 | * @param string $context View or edit context. |
||
| 209 | * @return array |
||
| 210 | */ |
||
| 211 | public function get_elements( $context = 'view' ) { |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Get the items sold via the form. |
||
| 222 | * |
||
| 223 | * @since 1.0.19 |
||
| 224 | * @param string $context View or edit context. |
||
| 225 | * @param string $return objects or arrays. |
||
| 226 | * @return GetPaid_Form_Item[]|array |
||
| 227 | */ |
||
| 228 | public function get_items( $context = 'view', $return = 'objects' ) { |
||
| 229 | $items = $this->get_prop( 'items', $context ); |
||
| 230 | |||
| 231 | if ( empty( $items ) || ! is_array( $items ) ) { |
||
| 232 | $items = wpinv_get_data( 'sample-payment-form-items' ); |
||
| 233 | } |
||
| 234 | |||
| 235 | if ( 'view' != $context ) { |
||
| 236 | return $items; |
||
| 237 | } |
||
| 238 | |||
| 239 | // Convert the items. |
||
| 240 | $prepared = array(); |
||
| 241 | |||
| 242 | foreach ( $items as $key => $value ) { |
||
| 243 | |||
| 244 | // $item_id => $quantity |
||
| 245 | if ( is_numeric( $key ) && is_numeric( $value ) ) { |
||
| 246 | $item = new GetPaid_Form_Item( $key ); |
||
| 247 | |||
| 248 | if ( $item->can_purchase() ) { |
||
| 249 | $item->set_quantity( $value ); |
||
| 250 | $prepared[] = $item; |
||
| 251 | } |
||
| 252 | |||
| 253 | continue; |
||
| 254 | } |
||
| 255 | |||
| 256 | if ( is_array( $value ) && isset( $value['id'] ) ) { |
||
| 257 | |||
| 258 | $item = new GetPaid_Form_Item( $value['id'] ); |
||
| 259 | |||
| 260 | if ( ! $item->can_purchase() ) { |
||
| 261 | continue; |
||
| 262 | } |
||
| 263 | |||
| 264 | // Sub-total (Cart items). |
||
| 265 | if ( isset( $value['subtotal'] ) ) { |
||
| 266 | $item->set_price( $value['subtotal'] ); |
||
| 267 | } |
||
| 268 | |||
| 269 | if ( isset( $value['quantity'] ) ) { |
||
| 270 | $item->set_quantity( $value['quantity'] ); |
||
| 271 | } |
||
| 272 | |||
| 273 | if ( isset( $value['allow_quantities'] ) ) { |
||
| 274 | $item->set_allow_quantities( $value['allow_quantities'] ); |
||
| 275 | } |
||
| 276 | |||
| 277 | if ( isset( $value['required'] ) ) { |
||
| 278 | $item->set_is_required( $value['required'] ); |
||
| 279 | } |
||
| 280 | |||
| 281 | if ( isset( $value['description'] ) ) { |
||
| 282 | $item->set_custom_description( $value['description'] ); |
||
| 283 | } |
||
| 284 | |||
| 285 | $prepared[] = $item; |
||
| 286 | continue; |
||
| 287 | |||
| 288 | } |
||
| 289 | } |
||
| 290 | |||
| 291 | if ( 'objects' == $return ) { |
||
| 292 | return $prepared; |
||
| 293 | } |
||
| 294 | |||
| 295 | $items = array(); |
||
| 296 | foreach ( $prepared as $item ) { |
||
| 297 | $items[] = $item->prepare_data_for_use(); |
||
| 298 | } |
||
| 299 | |||
| 300 | return $items; |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Get the total amount earned via this form. |
||
| 305 | * |
||
| 306 | * @since 1.0.19 |
||
| 307 | * @param string $context View or edit context. |
||
| 308 | * @return array |
||
| 309 | */ |
||
| 310 | public function get_earned( $context = 'view' ) { |
||
| 311 | return $this->get_prop( 'earned', $context ); |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Get the total amount refunded via this form. |
||
| 316 | * |
||
| 317 | * @since 1.0.19 |
||
| 318 | * @param string $context View or edit context. |
||
| 319 | * @return array |
||
| 320 | */ |
||
| 321 | public function get_refunded( $context = 'view' ) { |
||
| 322 | return $this->get_prop( 'refunded', $context ); |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Get the total amount cancelled via this form. |
||
| 327 | * |
||
| 328 | * @since 1.0.19 |
||
| 329 | * @param string $context View or edit context. |
||
| 330 | * @return array |
||
| 331 | */ |
||
| 332 | public function get_cancelled( $context = 'view' ) { |
||
| 333 | return $this->get_prop( 'cancelled', $context ); |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Get the total amount failed via this form. |
||
| 338 | * |
||
| 339 | * @since 1.0.19 |
||
| 340 | * @param string $context View or edit context. |
||
| 341 | * @return array |
||
| 342 | */ |
||
| 343 | public function get_failed( $context = 'view' ) { |
||
| 344 | return $this->get_prop( 'failed', $context ); |
||
| 345 | } |
||
| 346 | |||
| 347 | /* |
||
| 348 | |-------------------------------------------------------------------------- |
||
| 349 | | Setters |
||
| 350 | |-------------------------------------------------------------------------- |
||
| 351 | | |
||
| 352 | | Functions for setting order data. These should not update anything in the |
||
| 353 | | database itself and should only change what is stored in the class |
||
| 354 | | object. |
||
| 355 | */ |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Set plugin version when the item was created. |
||
| 359 | * |
||
| 360 | * @since 1.0.19 |
||
| 361 | */ |
||
| 362 | public function set_version( $value ) { |
||
| 363 | $this->set_prop( 'version', $value ); |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Set date when the item was created. |
||
| 368 | * |
||
| 369 | * @since 1.0.19 |
||
| 370 | * @param string $value Value to set. |
||
| 371 | * @return bool Whether or not the date was set. |
||
| 372 | */ |
||
| 373 | public function set_date_created( $value ) { |
||
| 374 | $date = strtotime( $value ); |
||
| 375 | |||
| 376 | if ( $date ) { |
||
| 377 | $this->set_prop( 'date_created', date( 'Y-m-d H:i:s', $date ) ); |
||
| 378 | return true; |
||
| 379 | } |
||
| 380 | |||
| 381 | return false; |
||
| 382 | |||
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Set date when the item was last modified. |
||
| 387 | * |
||
| 388 | * @since 1.0.19 |
||
| 389 | * @param string $value Value to set. |
||
| 390 | * @return bool Whether or not the date was set. |
||
| 391 | */ |
||
| 392 | public function set_date_modified( $value ) { |
||
| 393 | $date = strtotime( $value ); |
||
| 394 | |||
| 395 | if ( $date ) { |
||
| 396 | $this->set_prop( 'date_modified', date( 'Y-m-d H:i:s', $date ) ); |
||
| 397 | return true; |
||
| 398 | } |
||
| 399 | |||
| 400 | return false; |
||
| 401 | |||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Set the item name. |
||
| 406 | * |
||
| 407 | * @since 1.0.19 |
||
| 408 | * @param string $value New name. |
||
| 409 | */ |
||
| 410 | public function set_name( $value ) { |
||
| 411 | $this->set_prop( 'name', sanitize_text_field( $value ) ); |
||
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Alias of self::set_name(). |
||
| 416 | * |
||
| 417 | * @since 1.0.19 |
||
| 418 | * @param string $value New name. |
||
| 419 | */ |
||
| 420 | public function set_title( $value ) { |
||
| 421 | $this->set_name( $value ); |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Set the owner of the item. |
||
| 426 | * |
||
| 427 | * @since 1.0.19 |
||
| 428 | * @param int $value New author. |
||
| 429 | */ |
||
| 430 | public function set_author( $value ) { |
||
| 431 | $this->set_prop( 'author', (int) $value ); |
||
| 432 | } |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Set the form elements. |
||
| 436 | * |
||
| 437 | * @since 1.0.19 |
||
| 438 | * @param array $value Form elements. |
||
| 439 | */ |
||
| 440 | public function set_elements( $value ) { |
||
| 441 | if ( is_array( $value ) ) { |
||
| 442 | $this->set_prop( 'elements', $value ); |
||
| 443 | } |
||
| 444 | } |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Set the form items. |
||
| 448 | * |
||
| 449 | * @since 1.0.19 |
||
| 450 | * @param array $value Form elements. |
||
| 451 | */ |
||
| 452 | public function set_items( $value ) { |
||
| 455 | } |
||
| 456 | } |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Set the total amount earned via this form. |
||
| 460 | * |
||
| 461 | * @since 1.0.19 |
||
| 462 | * @param float $value Amount earned. |
||
| 463 | * @return array |
||
| 464 | */ |
||
| 465 | public function set_earned( $value ) { |
||
| 466 | return $this->set_prop( 'earned', $value ); |
||
| 467 | } |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Set the total amount refunded via this form. |
||
| 471 | * |
||
| 472 | * @since 1.0.19 |
||
| 473 | * @param float $value Amount refunded. |
||
| 474 | * @return array |
||
| 475 | */ |
||
| 476 | public function set_refunded( $value ) { |
||
| 477 | return $this->set_prop( 'refunded', $value ); |
||
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Set the total amount cancelled via this form. |
||
| 482 | * |
||
| 483 | * @since 1.0.19 |
||
| 484 | * @param float $value Amount cancelled. |
||
| 485 | * @return array |
||
| 486 | */ |
||
| 487 | public function set_cancelled( $value ) { |
||
| 488 | return $this->set_prop( 'cancelled', $value ); |
||
| 489 | } |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Set the total amount failed via this form. |
||
| 493 | * |
||
| 494 | * @since 1.0.19 |
||
| 495 | * @param float $value Amount cancelled. |
||
| 496 | * @return array |
||
| 497 | */ |
||
| 498 | public function set_failed( $value ) { |
||
| 499 | return $this->set_prop( 'failed', $value ); |
||
| 500 | } |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Create an item. For backwards compatibilty. |
||
| 504 | * |
||
| 505 | * @deprecated |
||
| 506 | * @return int item id |
||
| 507 | */ |
||
| 508 | public function create( $data = array() ) { |
||
| 517 | |||
| 518 | } |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Updates an item. For backwards compatibilty. |
||
| 522 | * |
||
| 523 | * @deprecated |
||
| 524 | * @return int item id |
||
| 525 | */ |
||
| 526 | public function update( $data = array() ) { |
||
| 527 | return $this->create( $data ); |
||
| 528 | } |
||
| 529 | |||
| 530 | /* |
||
| 531 | |-------------------------------------------------------------------------- |
||
| 532 | | Conditionals |
||
| 533 | |-------------------------------------------------------------------------- |
||
| 534 | | |
||
| 535 | | Checks if a condition is true or false. |
||
| 536 | | |
||
| 537 | */ |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Checks whether this is the default payment form. |
||
| 541 | * |
||
| 542 | * @since 1.0.19 |
||
| 543 | * @return bool |
||
| 544 | */ |
||
| 545 | public function is_default() { |
||
| 546 | $is_default = $this->get_id() == wpinv_get_default_payment_form(); |
||
| 547 | return (bool) apply_filters( 'wpinv_is_default_payment_form', $is_default, $this->get_id(), $this ); |
||
| 548 | } |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Checks whether the form is active. |
||
| 552 | * |
||
| 553 | * @since 1.0.19 |
||
| 554 | * @return bool |
||
| 555 | */ |
||
| 556 | public function is_active() { |
||
| 557 | $is_active = null !== $this->get_id(); |
||
| 558 | |||
| 559 | if ( $is_active && ! current_user_can( 'edit_post', $this->get_id() ) && $this->get_status() != 'publish' ) { |
||
| 560 | $is_active = false; |
||
| 561 | } |
||
| 562 | |||
| 563 | return (bool) apply_filters( 'wpinv_is_payment_form_active', $is_active, $this ); |
||
| 564 | } |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Displays the payment form. |
||
| 568 | * |
||
| 569 | * @param bool $echo whether to return or echo the value. |
||
| 570 | * @since 1.0.19 |
||
| 571 | */ |
||
| 572 | public function display( $echo = true ) { |
||
| 590 | } |
||
| 591 | } |
||
| 592 | |||
| 593 | } |
||
| 594 |