Passed
Pull Request — master (#126)
by Kiran
03:46
created

WPInv_Meta_Box_Details::renewals()   D

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 10

Duplication

Lines 1
Ratio 7.69 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 1
dl 1
loc 13
rs 4.8292
c 0
b 0
f 0
1
<?php
2
// MUST have WordPress.
3
if ( !defined( 'WPINC' ) ) {
4
    exit( 'Do NOT access this file directly: ' . basename( __FILE__ ) );
5
}
6
7
class WPInv_Meta_Box_Details {
8
    public static function output( $post ) {
9
        $currency_symbol    = wpinv_currency_symbol();
0 ignored issues
show
Unused Code introduced by
$currency_symbol is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
10
        $statuses           = wpinv_get_invoice_statuses();
11
        
12
        $post_id            = !empty( $post->ID ) ? $post->ID : 0;
13
        $invoice            = new WPInv_Invoice( $post_id );
14
        
15
        $status             = $invoice->get_status( false ); // Current status    
16
        $discount           = $invoice->get_discount();
17
        $discount_code      = $discount > 0 ? $invoice->get_discount_code() : '';
18
        $invoice_number     = $invoice->get_number();
19
        
20
        $date_created       = $invoice->get_created_date();
21
        $datetime_created   = strtotime( $date_created );
22
        $date_created       = $date_created != '' && $date_created != '0000-00-00 00:00:00' ? date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $datetime_created ) : '';
23
        $date_completed     = $invoice->get_completed_date();
24
        $date_completed     = $date_completed != '' && $date_completed != '0000-00-00 00:00:00' ? date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), strtotime( $date_completed ) ) : 'n/a';
25
        $title['status'] = __( 'Invoice Status:', 'invoicing' );
0 ignored issues
show
Coding Style Comprehensibility introduced by
$title was never initialized. Although not strictly required by PHP, it is generally a good practice to add $title = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
26
        $title['number'] = __( 'Invoice Number:', 'invoicing' );
27
        $mail_notice = esc_attr__( 'After saving invoice, this will send a copy of the invoice to the user&#8217;s email address.', 'invoicing' );
28
        
29
        $title = apply_filters('wpinv_details_metabox_titles', $title, $invoice);
30
        $statuses = apply_filters('wpinv_invoice_statuses', $statuses, $invoice);
31
        $mail_notice = apply_filters('wpinv_metabox_mail_notice', $mail_notice, $invoice);
32
        $post_obj = get_post_type_object($invoice->post_type);
33
        ?>
34
<div class="gdmbx2-wrap form-table">
35
    <div class="gdmbx2-metabox gdmbx-field-list" id="gdmbx2-metabox-wpinv_details">
36
        <div class="gdmbx-row gdmbx-type-select gdmbx2-id-wpinv-date-created">
37
            <div class="gdmbx-th"><label><?php _e( 'Date Created:', 'invoicing' );?></label></div>
38
            <div class="gdmbx-td"><?php echo $date_created;?></div>
39
        </div>
40
        <?php if ( $invoice->post_type == 'wpi_invoice' && wpinv_get_option( 'overdue_active' ) && ( $invoice->needs_payment() || $invoice->has_status( array( 'auto-draft' ) ) ) ) { ?>
41
        <div class="gdmbx-row gdmbx-type-select gdmbx2-id-wpinv-date-overdue">
42
            <div class="gdmbx-th"><label for="wpinv_due_date"><?php _e( 'Due Date:', 'invoicing' );?></label></div>
43
            <div class="gdmbx-td">
44
                <input type="text" placeholder="<?php esc_attr_e( 'Y-m-d', 'invoicing' );?>" value="<?php echo esc_attr( $invoice->get_due_date() );?>" id="wpinv_due_date" name="wpinv_due_date" class="regular-text wpiDatepicker" data-minDate="<?php echo esc_attr( date_i18n( 'Y-m-d', $datetime_created ) );?>" data-dateFormat="yy-mm-dd">
45
            </div>
46
        </div>
47
        <?php } ?>
48
        <?php do_action( 'wpinv_meta_box_details_after_due_date', $post_id ); ?>
49
        <?php if ( $date_completed && $date_completed != 'n/a' ) { ?>
50
        <div class="gdmbx-row gdmbx-type-select gdmbx2-id-wpinv-date-completed">
51
            <div class="gdmbx-th"><label><?php _e( 'Payment Date:', 'invoicing' );?></label></div>
52
            <div class="gdmbx-td"><?php echo $date_completed;?></div>
53
        </div>
54
        <?php } ?>
55
        <?php $is_viewed = wpinv_is_invoice_viewed( $post_id ); ?>
56
        <div class="gdmbx-row gdmbx-type-select gdmbx2-id-wpinv-customer-viewed">
57
            <div class="gdmbx-th"><label><?php _e( 'Viewed by Customer:', 'invoicing' );?></label></div>
58
            <div class="gdmbx-td"><?php ( 1 == $is_viewed ) ? _e( 'Yes', 'invoicing' ) : _e( 'No', 'invoicing' ); ?></div>
59
        </div>
60
        <div class="gdmbx-row gdmbx-type-select gdmbx2-id-wpinv-status">
61
            <div class="gdmbx-th"><label for="wpinv_status"><?php echo $title['status']; ?></label></div>
62
            <div class="gdmbx-td">
63
                <select required="required" id="wpinv_status" name="wpinv_status" class="gdmbx2_select">
64
                    <?php foreach ( $statuses as $value => $label ) { ?>
65
                    <option value="<?php echo $value;?>" <?php selected( $status, $value );?>><?php echo $label;?></option>
66
                    <?php } ?>
67
                </select>
68
            </div>
69
        </div>
70
        <div class="gdmbx-row gdmbx-type-text gdmbx2-id-wpinv-number table-layout">
71
            <div class="gdmbx-th"><label for="wpinv_number"><?php echo $title['number']; ?></label></div>
72
            <div class="gdmbx-td">
73
                <input type="text" value="<?php echo esc_attr( $invoice_number );?>" id="wpinv_number" name="wpinv_number" class="regular-text" readonly>
74
            </div>
75
        </div>
76
        <?php do_action( 'wpinv_meta_box_details_inner', $post_id );
77
        $disable_discount = apply_filters('wpinv_disable_apply_discount', false, $invoice, $post_id);
78
        ?>
79
        <?php if ( !( $is_paid = ( $invoice->is_paid() || $invoice->is_refunded() ) ) && !$disable_discount || $discount_code ) { ?>
80
        <div class="gdmbx-row gdmbx-type-text gdmbx2-id-wpinv-discount-code table-layout">
81
            <div class="gdmbx-th"><label for="wpinv_discount_code"><?php _e( 'Discount Code:', 'invoicing' );?></label></div>
82
            <div class="gdmbx-td">
83
                <input type="text" value="<?php echo esc_attr( $discount_code ); ?>" id="wpinv_discount" class="medium-text" <?php echo ( $discount_code ? 'readonly' : '' ); ?> /><?php if ( !$is_paid && !$disable_discount ) { ?><input value="<?php echo esc_attr_e( 'Apply', 'invoicing' ); ?>" class="button button-small button-primary <?php echo ( $discount_code ? 'wpi-hide' : 'wpi-inlineb' ); ?>" id="wpinv-apply-code" type="button" /><input value="<?php echo esc_attr_e( 'Remove', 'invoicing' ); ?>" class="button button-small button-primary <?php echo ( $discount_code ? 'wpi-inlineb' : 'wpi-hide' ); ?>" id="wpinv-remove-code" type="button" /><?php } ?>
84
            </div>
85
        </div>
86
        <?php } ?>
87
    </div>
88
</div>
89
<div class="gdmbx-row gdmbx-type-text gdmbx-wpinv-save-send table-layout">
90
    <p class="wpi-meta-row wpi-save-send"><label for="wpi_save_send"><?php echo sprintf(__( 'Send %s:', 'invoicing' ),$post_obj->labels->singular_name) ; ?></label>
91
        <select id="wpi_save_send" name="wpi_save_send">
92
            <option value="1"><?php _e( 'Yes', 'invoicing' ); ?></option>
93
            <option value="" selected="selected"><?php _e( 'No', 'invoicing' ); ?></option>
94
        </select>
95
    </p>
96
    <p class="wpi-meta-row wpi-send-info"><?php echo $mail_notice; ?></p>
97
</div>
98
<?php wp_nonce_field( 'wpinv_details', 'wpinv_details_nonce' ) ;?>
99
        <?php
100
    }
101
    
102
    public static function resend_invoice( $post ) {
103
        global $wpi_mb_invoice;
104
        
105
        if ( empty( $wpi_mb_invoice ) ) {
106
            return;
107
        }
108
        
109
        $text = array(
110
            'message'       => esc_attr__( 'This will send a copy of the invoice to the customer&#8217;s email address.', 'invoicing' ),
111
            'button_text'   =>  __( 'Resend Invoice', 'invoicing' ),
112
        );
113
            
114
        $text = apply_filters('wpinv_resend_invoice_metabox_text', $text);
115
        do_action( 'wpinv_metabox_resend_invoice_before', $wpi_mb_invoice );
116
        
117
        if ( $email = $wpi_mb_invoice->get_email() ) {
0 ignored issues
show
Unused Code introduced by
$email is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
118
            $email_actions = array();
119
            $email_actions['email_url']      = remove_query_arg( 'wpinv-message', add_query_arg( array( 'wpi_action' => 'send_invoice', 'invoice_id' => $post->ID ) ) );
120
            $email_actions['reminder_url']   = add_query_arg( array( 'wpi_action' => 'send_reminder', 'invoice_id' => $post->ID ) );
121
            
122
            $email_actions = apply_filters('wpinv_resend_invoice_email_actions', $email_actions );
123
        ?>
124
        <p class="wpi-meta-row wpi-resend-info"><?php echo $text['message']; ?></p>
125
        <p class="wpi-meta-row wpi-resend-email"><a href="<?php echo esc_url( $email_actions['email_url'] ); ?>" class="button button-secondary"><?php echo $text['button_text']; ?></a></p>
126
        <?php if ( wpinv_get_option( 'overdue_active' ) && "wpi_invoice" === $wpi_mb_invoice->post_type && $wpi_mb_invoice->needs_payment() && ( $due_date = $wpi_mb_invoice->get_due_date() ) ) { ?>
127
        <p class="wpi-meta-row wpi-send-reminder"><a title="<?php esc_attr_e( 'Send overdue reminder notification to customer', 'invoicing' ); ?>" href="<?php echo esc_url( $email_actions['reminder_url'] ); ?>" class="button button-secondary"><?php esc_attr_e( 'Send Reminder', 'invoicing' ); ?></a></p>
128
        <?php } ?>
129
        <?php
130
        }
131
        
132
        do_action( 'wpinv_metabox_resend_invoice_after', $wpi_mb_invoice );
133
    }
134
    
135
    public static function subscriptions( $post ) {
136
        $invoice = wpinv_get_invoice( $post->ID );
137
138
        if ( ! empty( $invoice ) && $invoice->is_recurring() && $invoice->is_parent() ) {
139
            $subscription = wpinv_get_subscription( $invoice );
140
141
            if ( empty( $subscription ) ) {
142
                ?>
143
                <p class="wpi-meta-row"><?php echo wp_sprintf( __( 'New Subscription will be created when customer will checkout and pay the invoice. Go to: %sSubscriptions%s', 'invoicing' ), '<a href="' . admin_url( 'admin.php?page=wpinv-subscriptions' ).'">', '</a>' ); ?></p>
144
                <?php
145
                return;
146
            }
147
            $frequency = WPInv_Subscriptions::wpinv_get_pretty_subscription_frequency( $subscription->period, $subscription->frequency );
148
            $billing = wpinv_price(wpinv_format_amount( $subscription->recurring_amount ), wpinv_get_invoice_currency_code( $subscription->parent_payment_id ) ) . ' / ' . $frequency;
149
            $initial = wpinv_price(wpinv_format_amount( $subscription->initial_amount ), wpinv_get_invoice_currency_code( $subscription->parent_payment_id ) );
150
            $payments = $subscription->get_child_payments();
151
            ?>
152
            <p class="wpi-meta-row wpi-sub-label <?php echo 'status-' . $subscription->status; ?>"><?php _e('Recurring Payment', 'invoicing'); ?></p>
153 View Code Duplication
            <?php if ( ! empty( $subscription ) && ! empty( $subscription->id ) ) { ?>
154
                <p class="wpi-meta-row wpi-sub-id">
155
                    <label><?php _e( 'Subscription ID:', 'invoicing' ); ?> </label><a href="<?php echo esc_url( admin_url( 'admin.php?page=wpinv-subscriptions&id=' . $subscription->id ) ); ?>" title="<?php esc_attr_e( 'View or edit subscription', 'invoicing' ); ?>" target="_blank"><?php echo $subscription->id; ?></a></p>
156
            <?php } ?>
157
            <p class="wpi-meta-row wpi-bill-cycle">
158
                <label><?php _e( 'Billing Cycle:', 'invoicing'); ?> </label><?php printf( _x( '%s then %s', 'Initial subscription amount then billing cycle and amount', 'invoicing' ), $initial, $billing ); ?>
159
            </p>
160
            <p class="wpi-meta-row wpi-billed-times">
161
                <label><?php _e( 'Times Billed:', 'invoicing' ); ?> </label><?php echo $subscription->get_times_billed() . ' / ' . ( ( $subscription->bill_times == 0 ) ? 'Until Cancelled' : $subscription->bill_times ); ?>
162
            </p>
163
            <p class="wpi-meta-row wpi-start-date">
164
                <label><?php _e( 'Start Date:', 'invoicing' ); ?> </label><?php echo date_i18n( get_option( 'date_format' ), strtotime( $subscription->created, current_time( 'timestamp' ) ) ); ?>
165
            </p>
166
            <p class="wpi-meta-row wpi-end-date">
167
                <label><?php echo ( 'trialling' == $subscription->status ? __( 'Trialling Until:', 'invoicing' ) : __( 'Expiration Date:', 'invoicing' ) ); ?> </label><?php echo date_i18n( get_option( 'date_format' ), strtotime( $subscription->expiration, current_time( 'timestamp' ) ) ); ?>
168
            </p>
169
            <?php if ( $subscription->status ) { ?>
170
                <p class="wpi-meta-row wpi-sub-status">
171
                    <label><?php _e( 'Subscription Status:', 'invoicing'); ?> </label><?php echo $subscription->get_status_label(); ?>
172
                </p>
173
            <?php } ?>
174
            <?php if ( !empty( $payments ) ) { ?>
175
                <p><strong><?php _e( 'Renewal Payments:', 'invoicing' ); ?></strong></p>
176
                <ul id="wpi-sub-payments">
177
                <?php foreach ( $payments as $payment ) {
178
                    $invoice_id = $payment->ID;
179
                    ?>
180
                    <li>
181
                        <a href="<?php echo esc_url( get_edit_post_link( $invoice_id ) ); ?>"><?php echo wpinv_get_invoice_number( $invoice_id ); ?></a>&nbsp;&ndash;&nbsp;
182
                        <span><?php echo wpinv_get_invoice_date( $invoice_id ); ?>&nbsp;&ndash;&nbsp;</span>
183
                        <span><?php echo wpinv_payment_total( $invoice_id, true ); ?></span>
184
                    </li>
185
                <?php } ?>
186
                </ul>
187
            <?php }
188
        }
189
    }
190
    
191
    public static function renewals( $post ) {
192
        $invoice = wpinv_get_invoice( $post->ID );
193
        
194
        if ( wpinv_is_subscription_payment( $invoice ) ) {
0 ignored issues
show
Documentation introduced by
$invoice is of type object<WPInv_Invoice>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
195
            $parent_url = get_edit_post_link( $invoice->parent_invoice );
196
            $parent_id  = wpinv_get_invoice_number( $invoice->parent_invoice );
197
            $subscription = wpinv_get_subscription( $invoice );
198
        ?>
199 View Code Duplication
        <?php if ( ! empty( $subscription ) ) { ?><p class="wpi-meta-row wpi-sub-id"><label><?php _e('Subscription ID:', 'invoicing'); ?> </label><a href="<?php echo esc_url( admin_url( 'admin.php?page=wpinv-subscriptions&id=' . $subscription->id ) ); ?>" title="<?php esc_attr_e( 'View or edit subscription', 'invoicing' ); ?>" target="_blank"><?php echo $subscription->id; ?></a></p><?php } ?>
200
        <p class="wpi-meta-row wpi-parent-id"><label><?php _e( 'Parent Invoice:', 'invoicing' );?> </label><a href="<?php echo esc_url( $parent_url ); ?>"><?php echo $parent_id; ?></a></p>
201
        <?php
202
        }
203
    }
204
    
205
    public static function payment_meta( $post ) {
0 ignored issues
show
Unused Code introduced by
The parameter $post is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
206
        global $wpi_mb_invoice;
207
208
        $set_dateway = empty( $wpi_mb_invoice->gateway ) ? true : false;
209
        if ( !$set_dateway && !$wpi_mb_invoice->get_meta( '_wpinv_checkout', true ) && !$wpi_mb_invoice->is_paid() && !$wpi_mb_invoice->is_refunded() ) {
210
            $set_dateway = true;
211
        }
212
        
213
        ?>
214
        <p class="wpi-meta-row">
215
        <?php if ( $set_dateway ) { $gateways = wpinv_get_enabled_payment_gateways( true ); ?>
216
            <label for="wpinv_gateway"><?php _e( 'Gateway:', 'invoicing' ) ; ?></label>
217
            <select required="required" id="wpinv_gateway" name="wpinv_gateway">
218
                <?php foreach ( $gateways as $name => $gateway ) {
219
                    if ( $wpi_mb_invoice->is_recurring() && !wpinv_gateway_support_subscription( $name ) ) {
220
                        continue;
221
                    }
222
                    ?>
223
                <option value="<?php echo $name;?>" <?php selected( $wpi_mb_invoice->gateway, $name );?>><?php echo !empty( $gateway['admin_label'] ) ? $gateway['admin_label'] : $gateway['checkout_label']; ?></option>
224
                <?php } ?>
225
            </select>
226
        <?php } else { 
227
            echo wp_sprintf( __( '<label>Gateway:</label> %s', 'invoicing' ), wpinv_get_gateway_admin_label( $wpi_mb_invoice->gateway ) );
228
        } ?>
229
        </p>
230
        <?php if ( $key = $wpi_mb_invoice->get_key() ) { ?>
231
        <p class="wpi-meta-row"><?php echo wp_sprintf( __( '<label>Key:</label> %s', 'invoicing' ), $key ); ?></p>
232
        <?php } ?>
233
        <?php if ( $wpi_mb_invoice->is_paid() || $wpi_mb_invoice->is_refunded() ) { ?>
234
        <p class="wpi-meta-row"><?php echo wp_sprintf( __( '<label>Transaction ID:</label> %s', 'invoicing' ), wpinv_payment_link_transaction_id( $wpi_mb_invoice ) ); ?></p>
235
        <?php } ?>
236
        <?php
237
    }
238
}
239