Passed
Pull Request — master (#116)
by
unknown
03:57
created

wpinv-payment-functions.php ➔ wpinv_get_pretty_subscription_period()   D

Complexity

Conditions 9
Paths 9

Size

Total Lines 27
Code Lines 23

Duplication

Lines 27
Ratio 100 %

Importance

Changes 0
Metric Value
cc 9
eloc 23
nc 9
nop 1
dl 27
loc 27
rs 4.909
c 0
b 0
f 0
1
<?php
2
3
function wpinv_is_subscription_payment( $invoice = '' ) {
4
    if ( empty( $invoice ) ) {
5
        return false;
6
    }
7
    
8
    if ( !is_object( $invoice ) && is_scalar( $invoice ) ) {
9
        $invoice = wpinv_get_invoice( $invoice );
10
    }
11
    
12
    if ( empty( $invoice ) ) {
13
        return false;
14
    }
15
        
16
    if ( $invoice->is_renewal() ) {
17
        return true;
18
    }
19
20
    return false;
21
}
22
23
function wpinv_payment_link_transaction_id( $invoice = '' ) {
24
    if ( empty( $invoice ) ) {
25
        return false;
26
    }
27
    
28
    if ( !is_object( $invoice ) && is_scalar( $invoice ) ) {
29
        $invoice = wpinv_get_invoice( $invoice );
30
    }
31
    
32
    if ( empty( $invoice ) ) {
33
        return false;
34
    }
35
36
    return apply_filters( 'wpinv_payment_details_transaction_id-' . $invoice->gateway, $invoice->get_transaction_id(), $invoice->ID, $invoice );
37
}
38
39
function wpinv_subscription_initial_payment_desc( $amount, $period, $interval, $trial_period = '', $trial_interval = 0 ) {
40
    $interval   = (int)$interval > 0 ? (int)$interval : 1;
41
    
42
    if ( $trial_interval > 0 && !empty( $trial_period ) ) {
43
        $amount = __( 'Free', 'invoicing' );
44
        $interval = $trial_interval;
45
        $period = $trial_period;
46
    }
47
    
48
    $description = '';
49
    switch ( $period ) {
50
        case 'D' :
51
        case 'day' :
52
            $description = wp_sprintf( _n( '%s for the first day.', '%s for the first %d days.', $interval, 'invoicing' ), $amount, $interval );
53
            break;
54
        case 'W' :
55
        case 'week' :
56
            $description = wp_sprintf( _n( '%s for the first week.', '%s for the first %d weeks.', $interval, 'invoicing' ), $amount, $interval );
57
            break;
58
        case 'M' :
59
        case 'month' :
60
            $description = wp_sprintf( _n( '%s for the first month.', '%s for the first %d months.', $interval, 'invoicing' ), $amount, $interval );
61
            break;
62
        case 'Y' :
63
        case 'year' :
64
            $description = wp_sprintf( _n( '%s for the first year.', '%s for the first %d years.', $interval, 'invoicing' ), $amount, $interval );
65
            break;
66
    }
67
68
    return apply_filters( 'wpinv_subscription_initial_payment_desc', $description, $amount, $period, $interval, $trial_period, $trial_interval  );
69
}
70
71
function wpinv_subscription_recurring_payment_desc( $amount, $period, $interval, $bill_times = 0, $trial_period = '', $trial_interval = 0 ) {
72
    $interval   = (int)$interval > 0 ? (int)$interval : 1;
73
    $bill_times = (int)$bill_times > 0 ? (int)$bill_times : 0;
74
    
75
    $description = '';
76
    switch ( $period ) {
77
        case 'D' :
78 View Code Duplication
        case 'day' :            
79
            if ( (int)$bill_times > 0 ) {
80
                if ( $interval > 1 ) {
81
                    if ( $bill_times > 1 ) {
82
                        $description = wp_sprintf( __( '%s for each %d days, for %d installments.', 'invoicing' ), $amount, $interval, $bill_times );
83
                    } else {
84
                        $description = wp_sprintf( __( '%s for %d days.', 'invoicing' ), $amount, $interval );
85
                    }
86
                } else {
87
                    $description = wp_sprintf( _n( '%s for one day.', '%s for each day, for %d installments.', $bill_times, 'invoicing' ), $amount, $bill_times );
88
                }
89
            } else {
90
                $description = wp_sprintf( _n( '%s for each day.', '%s for each %d days.', $interval, 'invoicing'), $amount, $interval );
91
            }
92
            break;
93
        case 'W' :
94 View Code Duplication
        case 'week' :            
95
            if ( (int)$bill_times > 0 ) {
96
                if ( $interval > 1 ) {
97
                    if ( $bill_times > 1 ) {
98
                        $description = wp_sprintf( __( '%s for each %d weeks, for %d installments.', 'invoicing' ), $amount, $interval, $bill_times );
99
                    } else {
100
                        $description = wp_sprintf( __( '%s for %d weeks.', 'invoicing' ), $amount, $interval );
101
                    }
102
                } else {
103
                    $description = wp_sprintf( _n( '%s for one week.', '%s for each week, for %d installments.', $bill_times, 'invoicing' ), $amount, $bill_times );
104
                }
105
            } else {
106
                $description = wp_sprintf( _n( '%s for each week.', '%s for each %d weeks.', $interval, 'invoicing' ), $amount, $interval );
107
            }
108
            break;
109
        case 'M' :
110 View Code Duplication
        case 'month' :            
111
            if ( (int)$bill_times > 0 ) {
112
                if ( $interval > 1 ) {
113
                    if ( $bill_times > 1 ) {
114
                        $description = wp_sprintf( __( '%s for each %d months, for %d installments.', 'invoicing' ), $amount, $interval, $bill_times );
115
                    } else {
116
                        $description = wp_sprintf( __( '%s for %d months.', 'invoicing' ), $amount, $interval );
117
                    }
118
                } else {
119
                    $description = wp_sprintf( _n( '%s for one month.', '%s for each month, for %d installments.', $bill_times, 'invoicing' ), $amount, $bill_times );
120
                }
121
            } else {
122
                $description = wp_sprintf( _n( '%s for each month.', '%s for each %d months.', $interval, 'invoicing' ), $amount, $interval );
123
            }
124
            break;
125
        case 'Y' :
126 View Code Duplication
        case 'year' :            
127
            if ( (int)$bill_times > 0 ) {
128
                if ( $interval > 1 ) {
129
                    if ( $bill_times > 1 ) {
130
                        $description = wp_sprintf( __( '%s for each %d years, for %d installments.', 'invoicing' ), $amount, $interval, $bill_times );
131
                    } else {
132
                        $description = wp_sprintf( __( '%s for %d years.', 'invoicing'), $amount, $interval );
133
                    }
134
                } else {
135
                    $description = wp_sprintf( _n( '%s for one year.', '%s for each year, for %d installments.', $bill_times, 'invoicing' ), $amount, $bill_times );
136
                }
137
            } else {
138
                $description = wp_sprintf( _n( '%s for each year.', '%s for each %d years.', $interval, 'invoicing' ), $amount, $interval );
139
            }
140
            break;
141
    }
142
143
    return apply_filters( 'wpinv_subscription_recurring_payment_desc', $description, $amount, $period, $interval, $bill_times, $trial_period, $trial_interval );
144
}
145
146
function wpinv_subscription_payment_desc( $invoice ) {
147
    if ( empty( $invoice ) ) {
148
        return NULL;
149
    }
150
    
151
    $description = '';
152
    if ( $invoice->is_parent() && $item = $invoice->get_recurring( true ) ) {
153
        if ( $item->has_free_trial() ) {
154
            $trial_period = $item->get_trial_period();
155
            $trial_interval = $item->get_trial_interval();
156
        } else {
157
            $trial_period = '';
158
            $trial_interval = 0;
159
        }
160
        
161
        $description = wpinv_get_billing_cycle( $invoice->get_total(), $invoice->get_recurring_details( 'total' ), $item->get_recurring_period(), $item->get_recurring_interval(), $item->get_recurring_limit(), $trial_period, $trial_interval, $invoice->get_currency() );
162
    }
163
    
164
    return apply_filters( 'wpinv_subscription_payment_desc', $description, $invoice );
165
}
166
167
function wpinv_get_billing_cycle( $initial, $recurring, $period, $interval, $bill_times, $trial_period = '', $trial_interval = 0, $currency = '' ) {
168
    $initial_total      = wpinv_round_amount( $initial );
169
    $recurring_total    = wpinv_round_amount( $recurring );
170
    
171
    if ( $trial_interval > 0 && !empty( $trial_period ) ) {
172
        // Free trial
173
    } else {
174
        if ( $bill_times == 1 ) {
175
            $recurring_total = $initial_total;
176
        } else if ( $bill_times > 1 && $initial_total != $recurring_total ) {
177
            $bill_times--;
178
        }
179
    }
180
    
181
    $initial_amount     = wpinv_price( wpinv_format_amount( $initial_total ), $currency );
182
    $recurring_amount   = wpinv_price( wpinv_format_amount( $recurring_total ), $currency );
183
    
184
    $recurring          = wpinv_subscription_recurring_payment_desc( $recurring_amount, $period, $interval, $bill_times, $trial_period, $trial_interval );
185
        
186
    if ( $initial_total != $recurring_total ) {
187
        $initial        = wpinv_subscription_initial_payment_desc( $initial_amount, $period, $interval, $trial_period, $trial_interval );
188
        
189
        $description    = wp_sprintf( __( '%s Then %s', 'invoicing' ), $initial, $recurring );
190
    } else {
191
        $description    = $recurring;
192
    }
193
    
194
    return apply_filters( 'wpinv_get_billing_cycle', $description, $initial, $recurring, $period, $interval, $bill_times, $trial_period, $trial_interval, $currency );
195
}
196
197
function wpinv_recurring_send_payment_failed( $invoice ) {
198
    if ( !empty( $invoice->ID ) ) {
199
        wpinv_failed_invoice_notification( $invoice->ID );
200
    }
201
}
202
add_action( 'wpinv_recurring_payment_failed', 'wpinv_recurring_send_payment_failed', 10, 1 );