Passed
Pull Request — master (#377)
by Brian
06:57
created

WPInv_Meta_Box_Details::subscriptions()   C

Complexity

Conditions 12
Paths 14

Size

Total Lines 52
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 47
c 0
b 0
f 0
nc 14
nop 1
dl 0
loc 52
rs 6.9666

How to fix   Long Method    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
class WPInv_Meta_Box_Details {
8
    
9
    public static function resend_invoice( $post ) {
10
        global $wpi_mb_invoice;
11
        
12
        if ( empty( $wpi_mb_invoice ) ) {
13
            return;
14
        }
15
        
16
        $text = array(
17
            'message'       => esc_attr__( 'This will send a copy of the invoice to the customer&#8217;s email address.', 'invoicing' ),
18
            'button_text'   =>  __( 'Resend Invoice', 'invoicing' ),
19
        );
20
            
21
        $text = apply_filters('wpinv_resend_invoice_metabox_text', $text);
22
        do_action( 'wpinv_metabox_resend_invoice_before', $wpi_mb_invoice );
23
24
        if ( $email = $wpi_mb_invoice->get_email() ) {
0 ignored issues
show
Unused Code introduced by
The assignment to $email is dead and can be removed.
Loading history...
25
            $email_actions = array();
26
            $email_actions['email_url']      = remove_query_arg( 'wpinv-message', add_query_arg( array( 'wpi_action' => 'send_invoice', 'invoice_id' => $post->ID ) ) );
27
            $email_actions['reminder_url']   = add_query_arg( array( 'wpi_action' => 'send_reminder', 'invoice_id' => $post->ID ) );
28
            
29
            $email_actions = apply_filters('wpinv_resend_invoice_email_actions', $email_actions );
30
        ?>
31
        <p class="wpi-meta-row wpi-resend-info"><?php echo $text['message']; ?></p>
32
        <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>
33
        <?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() ) ) { ?>
0 ignored issues
show
Unused Code introduced by
The assignment to $due_date is dead and can be removed.
Loading history...
34
        <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>
35
        <?php } ?>
36
        <?php
37
        }
38
        
39
        do_action( 'wpinv_metabox_resend_invoice_after', $wpi_mb_invoice );
40
    }
41
    
42
    public static function subscriptions( $post ) {
43
        $invoice = wpinv_get_invoice( $post->ID );
44
45
        if ( ! empty( $invoice ) && $invoice->is_recurring() && $invoice->is_parent() ) {
46
            $subscription = wpinv_get_subscription( $invoice );
47
48
            if ( empty( $subscription ) ) {
49
                ?>
50
                <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>
51
                <?php
52
                return;
53
            }
54
            $frequency = WPInv_Subscriptions::wpinv_get_pretty_subscription_frequency( $subscription->period, $subscription->frequency );
0 ignored issues
show
Bug introduced by
It seems like $subscription->frequency can also be of type WP_Error; however, parameter $frequency_count of WPInv_Subscriptions::wpi...ubscription_frequency() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
            $frequency = WPInv_Subscriptions::wpinv_get_pretty_subscription_frequency( $subscription->period, /** @scrutinizer ignore-type */ $subscription->frequency );
Loading history...
Bug Best Practice introduced by
The property frequency does not exist on WPInv_Subscription. Since you implemented __get, consider adding a @property annotation.
Loading history...
55
            $billing = wpinv_price(wpinv_format_amount( $subscription->recurring_amount ), wpinv_get_invoice_currency_code( $subscription->parent_payment_id ) ) . ' / ' . $frequency;
56
            $initial = wpinv_price(wpinv_format_amount( $subscription->initial_amount ), wpinv_get_invoice_currency_code( $subscription->parent_payment_id ) );
57
            $payments = $subscription->get_child_payments();
58
            ?>
59
            <p class="wpi-meta-row wpi-sub-label <?php echo 'status-' . $subscription->status; ?>"><?php _e('Recurring Payment', 'invoicing'); ?></p>
60
            <?php if ( ! empty( $subscription ) && ! empty( $subscription->id ) ) { ?>
61
                <p class="wpi-meta-row wpi-sub-id">
62
                    <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>
63
            <?php } ?>
64
            <p class="wpi-meta-row wpi-bill-cycle">
65
                <label><?php _e( 'Billing Cycle:', 'invoicing'); ?> </label><?php printf( _x( '%s then %s', 'Initial subscription amount then billing cycle and amount', 'invoicing' ), $initial, $billing ); ?>
66
            </p>
67
            <p class="wpi-meta-row wpi-billed-times">
68
                <label><?php _e( 'Times Billed:', 'invoicing' ); ?> </label><?php echo $subscription->get_times_billed() . ' / ' . ( ( $subscription->bill_times == 0 ) ? 'Until Cancelled' : $subscription->bill_times ); ?>
69
            </p>
70
            <p class="wpi-meta-row wpi-start-date">
71
                <label><?php _e( 'Start Date:', 'invoicing' ); ?> </label><?php echo date_i18n( get_option( 'date_format' ), strtotime( $subscription->created, current_time( 'timestamp' ) ) ); ?>
0 ignored issues
show
Bug introduced by
It seems like get_option('date_format') can also be of type false; however, parameter $format of date_i18n() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
                <label><?php _e( 'Start Date:', 'invoicing' ); ?> </label><?php echo date_i18n( /** @scrutinizer ignore-type */ get_option( 'date_format' ), strtotime( $subscription->created, current_time( 'timestamp' ) ) ); ?>
Loading history...
72
            </p>
73
            <p class="wpi-meta-row wpi-end-date">
74
                <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' ) ) ); ?>
75
            </p>
76
            <?php if ( $subscription->status ) { ?>
77
                <p class="wpi-meta-row wpi-sub-status">
78
                    <label><?php _e( 'Subscription Status:', 'invoicing'); ?> </label><?php echo $subscription->get_status_label(); ?>
79
                </p>
80
            <?php } ?>
81
            <?php if ( !empty( $payments ) ) { ?>
82
                <p><strong><?php _e( 'Renewal Payments:', 'invoicing' ); ?></strong></p>
83
                <ul id="wpi-sub-payments">
84
                <?php foreach ( $payments as $payment ) {
85
                    $invoice_id = $payment->ID;
86
                    ?>
87
                    <li>
88
                        <a href="<?php echo esc_url( get_edit_post_link( $invoice_id ) ); ?>"><?php echo wpinv_get_invoice_number( $invoice_id ); ?></a>&nbsp;&ndash;&nbsp;
89
                        <span><?php echo wpinv_get_invoice_date( $invoice_id ); ?>&nbsp;&ndash;&nbsp;</span>
90
                        <span><?php echo wpinv_payment_total( $invoice_id, true ); ?></span>
91
                    </li>
92
                <?php } ?>
93
                </ul>
94
            <?php }
95
        }
96
    }
97
    
98
    public static function renewals( $post ) {
99
        $invoice = wpinv_get_invoice( $post->ID );
100
        
101
        if ( wpinv_is_subscription_payment( $invoice ) ) {
102
            $parent_url = get_edit_post_link( $invoice->parent_invoice );
0 ignored issues
show
Bug Best Practice introduced by
The property parent_invoice does not exist on WPInv_Invoice. Since you implemented __get, consider adding a @property annotation.
Loading history...
103
            $parent_id  = wpinv_get_invoice_number( $invoice->parent_invoice );
104
            $subscription = wpinv_get_subscription( $invoice );
105
        ?>
106
        <?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 } ?>
107
        <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>
108
        <?php
109
        }
110
    }
111
112
}
113