Passed
Pull Request — master (#376)
by Brian
106:18
created

wpinv_discount_metabox_save()   D

Complexity

Conditions 20
Paths 16

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 20
eloc 14
c 0
b 0
f 0
nc 16
nop 3
dl 0
loc 28
rs 4.1666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
// MUST have WordPress.
3
if ( !defined( 'WPINC' ) ) {
4
    exit( 'Do NOT access this file directly: ' . basename( __FILE__ ) );
5
}
6
7
function wpinv_add_meta_boxes( $post_type, $post ) {
8
    global $wpi_mb_invoice;
9
    if ( $post_type == 'wpi_invoice' && !empty( $post->ID ) ) {
10
        $wpi_mb_invoice = wpinv_get_invoice( $post->ID );
11
    }
12
    
13
    if ( !empty( $wpi_mb_invoice ) && !$wpi_mb_invoice->has_status( array( 'draft', 'auto-draft' ) ) ) {
14
        add_meta_box( 'wpinv-mb-resend-invoice', __( 'Resend Invoice', 'invoicing' ), 'WPInv_Meta_Box_Details::resend_invoice', 'wpi_invoice', 'side', 'high' );
15
    }
16
    
17
    if ( !empty( $wpi_mb_invoice ) && $wpi_mb_invoice->is_recurring() && $wpi_mb_invoice->is_parent() ) {
18
        add_meta_box( 'wpinv-mb-subscriptions', __( 'Subscriptions', 'invoicing' ), 'WPInv_Meta_Box_Details::subscriptions', 'wpi_invoice', 'side', 'high' );
19
    }
20
    
21
    if ( wpinv_is_subscription_payment( $wpi_mb_invoice ) ) {
22
        add_meta_box( 'wpinv-mb-renewals', __( 'Renewal Payment', 'invoicing' ), 'WPInv_Meta_Box_Details::renewals', 'wpi_invoice', 'side', 'high' );
23
    }
24
    
25
    add_meta_box( 'wpinv-details', __( 'Invoice Details', 'invoicing' ), 'WPInv_Meta_Box_Details::output', 'wpi_invoice', 'side', 'default' );
26
    add_meta_box( 'wpinv-payment-meta', __( 'Payment Meta', 'invoicing' ), 'WPInv_Meta_Box_Details::payment_meta', 'wpi_invoice', 'side', 'default' );
27
28
    add_meta_box( 'wpinv-payment-form-design', __( 'Payment Form', 'invoicing' ), 'GetPaid_Meta_Box_Payment_Form::output', 'wpi_payment_form', 'normal' );
29
    add_meta_box( 'wpinv-payment-form-info', __( 'Details', 'invoicing' ), 'GetPaid_Meta_Box_Payment_Form_Info::output', 'wpi_payment_form', 'side' );
30
   
31
    add_meta_box( 'wpinv-address', __( 'Billing Details', 'invoicing' ), 'WPInv_Meta_Box_Billing_Details::output', 'wpi_invoice', 'normal', 'high' );
32
    add_meta_box( 'wpinv-items', __( 'Invoice Items', 'invoicing' ), 'WPInv_Meta_Box_Items::output', 'wpi_invoice', 'normal', 'high' );
33
    add_meta_box( 'wpinv-notes', __( 'Invoice Notes', 'invoicing' ), 'WPInv_Meta_Box_Notes::output', 'wpi_invoice', 'normal', 'high' );
34
    
35
    if ( ! empty( $post->ID ) && get_post_meta( $post->ID, 'payment_form_data', true ) ) {
36
        add_meta_box( 'wpinv-invoice-payment-form-details', __( 'Payment Form Details', 'invoicing' ), 'WPInv_Meta_Box_Payment_Form::output_details', 'wpi_invoice', 'side', 'high' );
37
    }
38
39
	remove_meta_box('wpseo_meta', 'wpi_invoice', 'normal');
40
}
41
add_action( 'add_meta_boxes', 'wpinv_add_meta_boxes', 30, 2 );
42
43
/**
44
 * Saves meta boxes.
45
 */
46
function wpinv_save_meta_boxes( $post_id, $post ) {
47
    remove_action( 'save_post', __FUNCTION__ );
48
49
    // $post_id and $post are required.
50
    if ( empty( $post_id ) || empty( $post ) ) {
51
        return;
52
    }
53
54
    // Ensure that this user can edit the post.
55
    if ( ! current_user_can( 'edit_post', $post_id ) ) {
56
        return;
57
    }
58
59
    // Dont' save meta boxes for revisions or autosaves
60
    if ( defined( 'DOING_AUTOSAVE' ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) {
61
        return;
62
    }
63
64
    // Do not save for ajax requests.
65
    if ( ( defined( 'DOING_AJAX') && DOING_AJAX ) || isset( $_REQUEST['bulk_edit'] ) ) {
66
        return;
67
    }
68
69
    $post_types_map = array(
70
        'wpi_invoice'      => 'WPInv_Meta_Box_Items',
71
        'wpi_quote'        => 'WPInv_Meta_Box_Items',
72
        'wpi_item'         => 'GetPaid_Meta_Box_Item_Details',
73
        'wpi_payment_form' => 'GetPaid_Meta_Box_Payment_Form',
74
        'wpi_discount'     => 'GetPaid_Meta_Box_Discount_Details',
75
    );
76
77
    // Is this our post type?
78
    if ( empty( $post->post_type ) || ! isset( $post_types_map[ $post->post_type ] ) ) {
79
        return;
80
    }
81
82
    // Save the post.
83
    $class = $post_types_map[ $post->post_type ];
84
    $class::save( $post_id, $_POST, $post );
85
86
}
87
add_action( 'save_post', 'wpinv_save_meta_boxes', 10, 3 );
88
89
function wpinv_register_item_meta_boxes() {    
90
    global $wpinv_euvat;
91
92
    // Item details metabox.
93
    add_meta_box( 'wpinv_item_details', __( 'Item Details', 'invoicing' ), 'GetPaid_Meta_Box_Item_Details::output', 'wpi_item', 'normal', 'high' );
94
95
    // If taxes are enabled, register the tax metabox.
96
    if ( $wpinv_euvat->allow_vat_rules() || $wpinv_euvat->allow_vat_classes() ) {
97
        add_meta_box( 'wpinv_item_vat', __( 'VAT / Tax', 'invoicing' ), 'GetPaid_Meta_Box_Item_VAT::output', 'wpi_item', 'normal', 'high' );
98
    }
99
100
    // Item info.
101
    add_meta_box( 'wpinv_field_item_info', __( 'Item info', 'invoicing' ), 'GetPaid_Meta_Box_Item_Info::output', 'wpi_item', 'side', 'core' );
102
    
103
}
104
105
function wpinv_register_discount_meta_boxes() {
106
    add_meta_box( 'wpinv_discount_details', __( 'Discount Details', 'invoicing' ), 'GetPaid_Meta_Box_Discount_Details::output', 'wpi_discount', 'normal', 'high' );
107
}
108
109
/**
110
 * Remove trash link from the default form.
111
 */
112
function getpaid_remove_action_link( $actions, $post ) {
113
    $post = get_post( $post );
114
    if ( 'wpi_payment_form' == $post->post_type && $post->ID == wpinv_get_default_payment_form() ) {
115
        unset( $actions['trash'] );
116
        unset( $actions['inline hide-if-no-js'] );
117
    }
118
    return $actions;
119
}
120
add_filter( 'post_row_actions', 'getpaid_remove_action_link', 10, 2 );
121