Passed
Branch master (50908e)
by Stiofan
07:01
created

wpinv-tax-functions.php ➔ wpinv_get_tax_rate()   F

Complexity

Conditions 33
Paths 4360

Size

Total Lines 85

Duplication

Lines 46
Ratio 54.12 %

Importance

Changes 0
Metric Value
cc 33
nc 4360
nop 3
dl 46
loc 85
rs 0
c 0
b 0
f 0

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
function wpinv_use_taxes() {
8
    $ret = wpinv_get_option( 'enable_taxes', false );
9
    
10
    return (bool) apply_filters( 'wpinv_use_taxes', $ret );
11
}
12
13
function wpinv_get_tax_rates() {
14
    $rates = get_option( 'wpinv_tax_rates', array() );
15
    
16
    return apply_filters( 'wpinv_get_tax_rates', $rates );
17
}
18
19
function wpinv_get_tax_rate( $country = false, $state = false, $item_id = 0 ) {
20
    global $wpinv_euvat, $wpi_tax_rates, $wpi_userID;
21
    $wpi_tax_rates = !empty( $wpi_tax_rates ) ? $wpi_tax_rates : array();
22
    
23
    if ( !empty( $wpi_tax_rates ) && !empty( $item_id ) && isset( $wpi_tax_rates[$item_id] ) ) {
24
        return $wpi_tax_rates[$item_id];
25
    }
26
    
27
    if ( !$wpinv_euvat->item_is_taxable( $item_id, $country, $state ) ) {
28
        $wpi_tax_rates[$item_id] = 0;
29
        return 0;
30
    }
31
    
32
    $is_global = false;
33
    if ( $item_id == 'global' ) {
34
        $is_global = true;
35
        $item_id = 0;
36
    }
37
    
38
    $rate           = (float)wpinv_get_option( 'tax_rate', 0 );
39
    $user_address   = wpinv_get_user_address( $wpi_userID );
40
    
41
    if( empty( $country ) ) {
42
        if( !empty( $_POST['wpinv_country'] ) ) {
43
            $country = $_POST['wpinv_country'];
44
        } elseif( !empty( $_POST['wpinv_country'] ) ) {
45
            $country = $_POST['wpinv_country'];
46
        } elseif( !empty( $_POST['country'] ) ) {
47
            $country = $_POST['country'];
48
        } elseif( is_user_logged_in() && !empty( $user_address ) ) {
49
            $country = $user_address['country'];
50
        }
51
        $country = !empty( $country ) ? $country : wpinv_get_default_country();
52
    }
53
54
    if( empty( $state ) ) {
55
        if( !empty( $_POST['wpinv_state'] ) ) {
56
            $state = $_POST['wpinv_state'];
57
        } elseif( !empty( $_POST['wpinv_state'] ) ) {
58
            $state = $_POST['wpinv_state'];
59
        } elseif( !empty( $_POST['state'] ) ) {
60
            $state = $_POST['state'];
61
        } elseif( is_user_logged_in() && !empty( $user_address ) ) {
62
            $state = $user_address['state'];
63
        }
64
        $state = !empty( $state ) ? $state : wpinv_get_default_state();
65
    }
66
    
67
    if( !empty( $country ) ) {
68
        $tax_rates   = wpinv_get_tax_rates();
69
70
        if( !empty( $tax_rates ) ) {
71
            // Locate the tax rate for this country / state, if it exists
72
            foreach( $tax_rates as $key => $tax_rate ) {
73
                if( $country != $tax_rate['country'] )
74
                    continue;
75
76
                if( !empty( $tax_rate['global'] ) ) {
77
                    if( !empty( $tax_rate['rate'] ) ) {
78
                        $rate = number_format( $tax_rate['rate'], 4 );
79
                    }
80
                } else {
81
82
                    if( empty( $tax_rate['state'] ) || strtolower( $state ) != strtolower( $tax_rate['state'] ) )
83
                        continue;
84
85
                    $state_rate = $tax_rate['rate'];
86
                    if( 0 !== $state_rate || !empty( $state_rate ) ) {
87
                        $rate = number_format( $state_rate, 4 );
88
                    }
89
                }
90
            }
91
        }
92
    }
93
    
94
    $rate = apply_filters( 'wpinv_tax_rate', $rate, $country, $state, $item_id );
95
    
96
    if ( !empty( $item_id ) ) {
97
        $wpi_tax_rates[$item_id] = $rate;
98
    } else if ( $is_global ) {
99
        $wpi_tax_rates['global'] = $rate;
100
    }
101
    
102
    return $rate;
103
}
104
105
function wpinv_get_formatted_tax_rate( $country = false, $state = false, $item_id ) {
106
    $rate = wpinv_get_tax_rate( $country, $state, $item_id );
107
    $rate = round( $rate, 4 );
0 ignored issues
show
Bug introduced by
It seems like $rate can also be of type string; however, parameter $val of round() does only seem to accept double, 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

107
    $rate = round( /** @scrutinizer ignore-type */ $rate, 4 );
Loading history...
108
    $formatted = $rate .= '%';
109
    return apply_filters( 'wpinv_formatted_tax_rate', $formatted, $rate, $country, $state, $item_id );
110
}
111
112
function wpinv_calculate_tax( $amount = 0, $country = false, $state = false, $item_id = 0 ) {
113
    $rate = wpinv_get_tax_rate( $country, $state, $item_id );
114
    $tax  = 0.00;
115
116
    if ( wpinv_use_taxes() ) {        
117
        if ( wpinv_prices_include_tax() ) {
118
            $pre_tax = ( $amount / ( ( 1 + $rate ) * 0.01 ) );
119
            $tax     = $amount - $pre_tax;
120
        } else {
121
            $tax = $amount * $rate * 0.01;
122
        }
123
124
    }
125
126
    return apply_filters( 'wpinv_taxed_amount', $tax, $rate, $country, $state, $item_id );
127
}
128
129
function wpinv_prices_include_tax() {
130
    return false; // TODO
131
    $ret = ( wpinv_get_option( 'prices_include_tax', false ) == 'yes' && wpinv_use_taxes() );
0 ignored issues
show
Unused Code introduced by
$ret = wpinv_get_option(...s' && wpinv_use_taxes() is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
132
133
    return apply_filters( 'wpinv_prices_include_tax', $ret );
134
}
135
136
function wpinv_sales_tax_for_year( $year = null ) {
137
    return wpinv_price( wpinv_format_amount( wpinv_get_sales_tax_for_year( $year ) ) );
138
}
139
140
function wpinv_get_sales_tax_for_year( $year = null ) {
141
    global $wpdb;
142
143
    // Start at zero
144
    $tax = 0;
145
146
    if ( ! empty( $year ) ) {
147
        $args = array(
148
            'post_type'      => 'wpi_invoice',
149
            'post_status'    => array( 'publish' ),
150
            'posts_per_page' => -1,
151
            'year'           => $year,
152
            'fields'         => 'ids'
153
        );
154
155
        $payments    = get_posts( $args );
156
        $payment_ids = implode( ',', $payments );
157
158
        if ( count( $payments ) > 0 ) {
159
            $sql = "SELECT SUM( meta_value ) FROM $wpdb->postmeta WHERE meta_key = '_wpinv_tax' AND post_id IN( $payment_ids )";
160
            $tax = $wpdb->get_var( $sql );
161
        }
162
163
    }
164
165
    return apply_filters( 'wpinv_get_sales_tax_for_year', $tax, $year );
166
}
167
168
function wpinv_is_cart_taxed() {
169
    return wpinv_use_taxes();
170
}
171
172
function wpinv_prices_show_tax_on_checkout() {
173
    return false; // TODO
174
    $ret = ( wpinv_get_option( 'checkout_include_tax', false ) == 'yes' && wpinv_use_taxes() );
0 ignored issues
show
Unused Code introduced by
$ret = wpinv_get_option(...s' && wpinv_use_taxes() is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
175
176
    return apply_filters( 'wpinv_taxes_on_prices_on_checkout', $ret );
177
}
178
179
function wpinv_display_tax_rate() {
180
    $ret = wpinv_use_taxes() && wpinv_get_option( 'display_tax_rate', false );
181
182
    return apply_filters( 'wpinv_display_tax_rate', $ret );
183
}
184
185
function wpinv_cart_needs_tax_address_fields() {
186
    if( !wpinv_is_cart_taxed() )
187
        return false;
188
189
    return ! did_action( 'wpinv_after_cc_fields', 'wpinv_default_cc_address_fields' );
0 ignored issues
show
Unused Code introduced by
The call to did_action() has too many arguments starting with 'wpinv_default_cc_address_fields'. ( Ignorable by Annotation )

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

189
    return ! /** @scrutinizer ignore-call */ did_action( 'wpinv_after_cc_fields', 'wpinv_default_cc_address_fields' );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
190
}
191
192
function wpinv_item_is_tax_exclusive( $item_id = 0 ) {
193
    $ret = (bool)get_post_meta( $item_id, '_wpinv_tax_exclusive', false );
194
    return apply_filters( 'wpinv_is_tax_exclusive', $ret, $item_id );
195
}
196
197
function wpinv_currency_decimal_filter( $decimals = 2 ) {
198
    $currency = wpinv_get_currency();
199
200
    switch ( $currency ) {
201
        case 'RIAL' :
202
        case 'JPY' :
203
        case 'TWD' :
204
        case 'HUF' :
205
            $decimals = 0;
206
            break;
207
    }
208
209
    return apply_filters( 'wpinv_currency_decimal_count', $decimals, $currency );
210
}
211
212
function wpinv_tax_amount() {
213
    $output = 0.00;
214
    
215
    return apply_filters( 'wpinv_tax_amount', $output );
216
}
217
218
function wpinv_recalculated_tax() {
219
    define( 'WPINV_RECALCTAX', true );
220
}
221
add_action( 'wp_ajax_wpinv_recalculate_tax', 'wpinv_recalculated_tax', 1 );
222
223
function wpinv_recalculate_tax( $return = false ) {
224
    $invoice_id = (int)wpinv_get_invoice_cart_id();
225
    if ( empty( $invoice_id ) ) {
226
        return false;
227
    }
228
    
229
    $invoice = wpinv_get_invoice_cart( $invoice_id );
230
231
    if ( empty( $invoice ) ) {
232
        return false;
233
    }
234
235
    if ( empty( $_POST['country'] ) ) {
236
        $_POST['country'] = !empty($invoice->country) ? $invoice->country : wpinv_get_default_country();
237
    }
238
        
239
    $invoice->country = sanitize_text_field($_POST['country']);
240
    $invoice->set( 'country', sanitize_text_field( $_POST['country'] ) );
241
    if (isset($_POST['state'])) {
242
        $invoice->state = sanitize_text_field($_POST['state']);
243
        $invoice->set( 'state', sanitize_text_field( $_POST['state'] ) );
244
    }
245
246
    $invoice->cart_details  = wpinv_get_cart_content_details();
247
    
248
    $subtotal               = wpinv_get_cart_subtotal( $invoice->cart_details );
249
    $tax                    = wpinv_get_cart_tax( $invoice->cart_details );
250
    $total                  = wpinv_get_cart_total( $invoice->cart_details );
251
252
    $invoice->tax           = $tax;
253
    $invoice->subtotal      = $subtotal;
254
    $invoice->total         = $total;
255
256
    $invoice->save();
257
    
258
    if ( $invoice->is_free_trial() ) {
259
        $total = 0;
260
    }
261
    
262
    $response = array(
263
        'total'        => html_entity_decode( wpinv_price( wpinv_format_amount( $total ) ), ENT_COMPAT, 'UTF-8' ),
264
        'total_raw'    => $total,
265
        'free'         => !( (float)$total > 0 ) && $invoice->is_free() ? true : false,
266
        'html'         => wpinv_checkout_cart( $invoice->cart_details, false ),
267
    );
268
    
269
    if ( $return ) {
270
        return $response;
271
    }
272
273
    wp_send_json( $response );
274
}
275
add_action( 'wp_ajax_wpinv_recalculate_tax', 'wpinv_recalculate_tax' );
276
add_action( 'wp_ajax_nopriv_wpinv_recalculate_tax', 'wpinv_recalculate_tax' );
277
278
// VAT Settings
279
function wpinv_vat_rate_add_callback( $args ) {
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed. ( Ignorable by Annotation )

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

279
function wpinv_vat_rate_add_callback( /** @scrutinizer ignore-unused */ $args ) {

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

Loading history...
280
    ?>
281
    <p class="wpi-vat-rate-actions"><input id="wpi_vat_rate_add" type="button" value="<?php esc_attr_e( 'Add', 'invoicing' );?>" class="button button-primary" />&nbsp;&nbsp;<i style="display:none;" class="fa fa-refresh fa-spin"></i></p>
282
    <?php
283
}
284
285
function wpinv_vat_rate_delete_callback( $args ) {
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed. ( Ignorable by Annotation )

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

285
function wpinv_vat_rate_delete_callback( /** @scrutinizer ignore-unused */ $args ) {

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

Loading history...
286
    global $wpinv_euvat;
287
    
288
    $vat_classes = $wpinv_euvat->get_rate_classes();
289
    $vat_class = isset( $_REQUEST['wpi_sub'] ) && $_REQUEST['wpi_sub'] !== '' && isset( $vat_classes[$_REQUEST['wpi_sub']] )? sanitize_text_field( $_REQUEST['wpi_sub'] ) : '';
290
    if ( isset( $vat_classes[$vat_class] ) ) {
291
    ?>
292
    <p class="wpi-vat-rate-actions"><input id="wpi_vat_rate_delete" type="button" value="<?php echo wp_sprintf( esc_attr__( 'Delete class "%s"', 'invoicing' ), $vat_classes[$vat_class] );?>" class="button button-primary" />&nbsp;&nbsp;<i style="display:none;" class="fa fa-refresh fa-spin"></i></p>
293
    <?php
294
    }
295
}
296
297
function wpinv_vat_rates_callback( $args ) {
298
    global $wpinv_euvat;
299
    
300
    $vat_classes    = $wpinv_euvat->get_rate_classes();
301
    $vat_class      = isset( $_REQUEST['wpi_sub'] ) && $_REQUEST['wpi_sub'] !== '' && isset( $vat_classes[$_REQUEST['wpi_sub']] )? sanitize_text_field( $_REQUEST['wpi_sub'] ) : '_standard';
302
    
303
    $eu_states      = $wpinv_euvat->get_eu_states();
304
    $countries      = wpinv_get_country_list();
305
    $vat_groups     = $wpinv_euvat->get_vat_groups();
306
    $rates          = $wpinv_euvat->get_vat_rates( $vat_class );
307
    ob_start();
308
?>
309
</td><tr>
310
    <td colspan="2" class="wpinv_vat_tdbox">
311
    <input type="hidden" name="wpi_vat_class" value="<?php echo $vat_class;?>" />
312
    <p><?php echo ( isset( $args['desc'] ) ? $args['desc'] : '' ); ?></p>
313
    <table id="wpinv_vat_rates" class="wp-list-table widefat fixed posts">
314
        <colgroup>
315
            <col width="50px" />
316
            <col width="auto" />
317
            <col width="auto" />
318
            <col width="auto" />
319
            <col width="auto" />
320
            <col width="auto" />
321
        </colgroup>
322
        <thead>
323
            <tr>
324
                <th scope="col" colspan="2" class="wpinv_vat_country_name"><?php _e( 'Country', 'invoicing' ); ?></th>
325
                <th scope="col" class="wpinv_vat_global" title="<?php esc_attr_e( 'Apply rate to whole country', 'invoicing' ); ?>"><?php _e( 'Country Wide', 'invoicing' ); ?></th>
326
                <th scope="col" class="wpinv_vat_rate"><?php _e( 'Rate %', 'invoicing' ); ?></th> 
327
                <th scope="col" class="wpinv_vat_name"><?php _e( 'VAT Name', 'invoicing' ); ?></th>
328
                <th scope="col" class="wpinv_vat_group"><?php _e( 'Tax Group', 'invoicing' ); ?></th>
329
            </tr>
330
        </thead>
331
        <tbody>
332
        <?php if( !empty( $eu_states ) ) { ?>
333
        <?php 
334
        foreach ( $eu_states as $state ) { 
335
            $country_name = isset( $countries[$state] ) ? $countries[$state] : '';
336
            
337
            // Filter the rate for each country
338
            $country_rate = array_filter( $rates, function( $rate ) use( $state ) { return $rate['country'] === $state; } );
339
            
340
            // If one does not exist create a default
341
            $country_rate = is_array( $country_rate ) && count( $country_rate ) > 0 ? reset( $country_rate ) : array();
342
            
343
            $vat_global = isset( $country_rate['global'] ) ? !empty( $country_rate['global'] ) : true;
344
            $vat_rate = isset( $country_rate['rate'] ) ? $country_rate['rate'] : '';
345
            $vat_name = !empty( $country_rate['name'] ) ? esc_attr( stripslashes( $country_rate['name'] ) ) : '';
346
            $vat_group = !empty( $country_rate['group'] ) ? $country_rate['group'] : ( $vat_class === '_standard' ? 'standard' : 'reduced' );
347
        ?>
348
        <tr>
349
            <td class="wpinv_vat_country"><?php echo $state; ?><input type="hidden" name="vat_rates[<?php echo $state; ?>][country]" value="<?php echo $state; ?>" /><input type="hidden" name="vat_rates[<?php echo $state; ?>][state]" value="" /></td>
350
            <td class="wpinv_vat_country_name"><?php echo $country_name; ?></td>
351
            <td class="wpinv_vat_global">
352
                <input type="checkbox" name="vat_rates[<?php echo $state;?>][global]" id="vat_rates[<?php echo $state;?>][global]" value="1" <?php checked( true, $vat_global );?> disabled="disabled" />
353
                <label for="tax_rates[<?php echo $state;?>][global]"><?php _e( 'Apply to whole country', 'invoicing' ); ?></label>
354
                <input type="hidden" name="vat_rates[<?php echo $state;?>][global]" value="1" checked="checked" />
355
            </td>
356
            <td class="wpinv_vat_rate"><input type="number" class="small-text" step="any" min="0" max="99" name="vat_rates[<?php echo $state;?>][rate]" value="<?php echo $vat_rate; ?>" /></td>
357
            <td class="wpinv_vat_name"><input type="text" class="regular-text" name="vat_rates[<?php echo $state;?>][name]" value="<?php echo $vat_name; ?>" /></td>
358
            <td class="wpinv_vat_group">
359
            <?php
360
            echo wpinv_html_select( array(
361
                                        'name'             => 'vat_rates[' . $state . '][group]',
362
                                        'selected'         => $vat_group,
363
                                        'id'               => 'vat_rates[' . $state . '][group]',
364
                                        'class'            => 'wpi_select2',
365
                                        'options'          => $vat_groups,
366
                                        'multiple'         => false,
367
                                        'show_option_all'  => false,
368
                                        'show_option_none' => false
369
                                    ) );
370
            ?>
371
            </td>
372
        </tr>
373
        <?php } ?>
374
        <tr>
375
            <td colspan="6" style="background-color:#fafafa;">
376
                <span><input id="wpi_vat_get_rates_group" type="button" class="button-secondary" value="<?php esc_attr_e( 'Update EU VAT Rates', 'invoicing' ); ?>" />&nbsp;&nbsp;<i style="display:none" class="fa fa-refresh fa-spin"></i></span><span id="wpinv-rates-error-wrap" class="wpinv_errors" style="display:none;"></span>
377
            </td>
378
        </tr>
379
        <?php } ?>
380
        </tbody>
381
    </table>
382
    <?php
383
    $content = ob_get_clean();
384
    
385
    echo $content;
386
}
387
388
function wpinv_vat_number_callback( $args ) {
389
    global $wpinv_euvat;
390
    
391
    $vat_number     = $wpinv_euvat->get_vat_number();
392
    $vat_valid      = $wpinv_euvat->is_vat_validated();
393
394
    $size           = ( isset( $args['size'] ) && !is_null( $args['size'] ) ) ? $args['size'] : 'regular';
395
    $validated_text = $vat_valid ? __( 'VAT number validated', 'invoicing' ) : __( 'VAT number not validated', 'invoicing' );
396
    $disabled       = $vat_valid ? 'disabled="disabled"' : " ";
397
    
398
    $html = '<input type="text" class="' . $size . '-text" id="wpinv_settings[' . $args['id'] . ']" name="wpinv_settings[' . $args['id'] . ']" placeholder="GB123456789" value="' . esc_attr( stripslashes( $vat_number ) ) . '"/>';
399
    $html .= '<span>&nbsp;<input type="button" id="wpinv_vat_validate" class="wpinv_validate_vat_button button-secondary" ' . $disabled . ' value="' . esc_attr__( 'Validate VAT Number', 'invoicing' ) . '" /></span>';
400
    $html .= '<span class="wpinv-vat-stat wpinv-vat-stat-' . (int)$vat_valid . '"><i class="fa"></i> <font>' . $validated_text . '</font></span>';
401
    $html .= '<label for="wpinv_settings[' . $args['id'] . ']">' . '<p>' . __( 'Enter your VAT number including country identifier, eg: GB123456789 (Settings must be saved after validation)', 'invoicing' ).'</p>' . '</label>';
402
    $html .= '<input type="hidden" name="_wpi_nonce" value="' . wp_create_nonce( 'vat_validation' ) . '">';
403
404
    echo $html;
405
}
406
407
function wpinv_eu_fallback_rate_callback( $args ) {
408
    global $wpinv_options;
409
410
    $value = isset( $wpinv_options[$args['id']] ) ? $wpinv_options[ $args['id'] ] : ( isset( $args['std'] ) ? $args['std'] : '' );
411
    $size = ( isset( $args['size'] ) && !is_null( $args['size'] ) ) ? $args['size'] : 'small';
412
    
413
    $html = '<input type="number" min="0" max="99" step="any" class="' . $size . '-text" id="wpinv_settings_' . $args['section'] . '_' . $args['id'] . '" name="wpinv_settings[' . $args['id'] . ']" value="' . esc_attr( stripslashes( $value ) ) . '" />';
414
    $html .= '<span>&nbsp;<input id="wpi_add_eu_states" type="button" class="button-secondary" value="' . esc_attr__( 'Add EU Member States', 'invoicing' ) . '" /></span>';
415
    $html .= '<span>&nbsp;<input id="wpi_remove_eu_states" type="button" class="button-secondary" value="' . esc_attr__( 'Remove EU Member States', 'invoicing' ) . '" /></span>';
416
    $html .= '<span>&nbsp;<input id="wpi_vat_get_rates" type="button" class="button-secondary" value="' . esc_attr__( 'Update EU VAT Rates', 'invoicing' ) . '" />&nbsp;&nbsp;<i style="display:none" class="fa fa-refresh fa-spin"></i></span>';
417
    $html .= '<p><label for="wpinv_settings_' . $args['section'] . '_' . $args['id'] . '">' . $args['desc'] . '</label></p>';
418
    echo $html;
419
    ?>
420
    <span id="wpinv-rates-error-wrap" class="wpinv_errors" style="display:none;"></span>
421
    <?php
422
}
423
424
function wpinv_vat_ip_lookup_callback( $args ) {
425
    global $wpinv_options, $wpinv_euvat;
426
427
    $value =  isset( $wpinv_options[ $args['id'] ] ) ? $wpinv_options[ $args['id'] ]  : ( isset( $args['std'] ) ? $args['std'] : 'default' );
428
    
429
    $options = array();
430
    if ( function_exists( 'geoip_country_code_by_name' ) ) {
431
        $options['geoip'] = __( 'PHP GeoIP extension', 'invoicing' );
432
    }
433
    
434
    $geoip2_database = $wpinv_euvat->geoip2_country_dbfile();
435
    
436
    if ( !function_exists( 'bcadd' ) ) {
437
        $geoip2_message = __( 'GeoIP2 service requires the BC Math PHP extension, it is not loaded in your version of PHP!', 'invoicing' );
438
    } else {
439
        $geoip2_message = ini_get('safe_mode') ? __( 'GeoIP2 is not supported with PHP safe mode enabled!', 'invoicing' ) : '';
440
    }
441
    
442
    if ( $geoip2_database !== false && empty( $geoip2_message ) ) {
443
        $options['geoip2'] = __( 'GeoIP2 Database', 'invoicing' );
444
    }
445
    
446
    if ( function_exists( 'simplexml_load_file' ) ) {
447
        $options['geoplugin'] = __( 'geoPlugin Web Service', 'invoicing' );
448
    }
449
    
450
    $options['site']    = __( 'Use default country', 'invoicing' );
451
    $options['default'] = __( 'Auto', 'invoicing' );
452
453
    $html = wpinv_html_select( array(
454
        'name'             => "wpinv_settings[{$args['id']}]",
455
        'selected'         => $value,
456
        'id'               => "wpinv_settings[{$args['id']}]",
457
        'class'            => isset($args['class']) ? $args['class'] : "",
458
        'options'          => $options,
459
        'multiple'         => false,
460
        'show_option_all'  => false,
461
        'show_option_none' => false
462
    ));
463
    
464
    $desc = '<label for="wpinv_settings[' . $args['id'] . ']">';
465
    $desc .= __( 'Select the option Invoicing should use to determine the country from the IP address of the user.', 'invoicing' );
466
    $desc .= '<p>';
467
    if ( empty( $geoip2_message ) ) {
468
        if ( $geoip2_database ) {
469
            $last_updated = '';
470
            if ( $time_updated = wpinv_get_option( 'wpinv_geoip2_date_updated' ) ) {
471
                $date_updated = date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $time_updated );
0 ignored issues
show
Bug introduced by
Are you sure get_option('time_format') of type false|mixed can be used in concatenation? ( Ignorable by Annotation )

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

471
                $date_updated = date_i18n( get_option( 'date_format' ) . ' ' . /** @scrutinizer ignore-type */ get_option( 'time_format' ), $time_updated );
Loading history...
Bug introduced by
Are you sure get_option('date_format') of type false|mixed can be used in concatenation? ( Ignorable by Annotation )

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

471
                $date_updated = date_i18n( /** @scrutinizer ignore-type */ get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $time_updated );
Loading history...
472
                $last_updated = '<br>' . sprintf( __( 'The GeoIP2 database was last updated on: <b>%s</b>', 'invoicing' ), $date_updated );
473
            }
474
            $desc .= __(  'GeoIP2 database exists:', 'invoicing' ) . $last_updated . '&nbsp;<input type="button" id="wpi_geoip2" action="update" class="wpinv-refresh-geoip2-btn button-secondary" value="' . __( 'Update GeoIP2 database now (~55MB)', 'invoicing' ) . '"></input>';
475
        } else {
476
            $desc .= __( 'GeoIP2 database does not exist:', 'invoicing' ) . '&nbsp;<input type="button" id="wpi_geoip2" action="download" class="wpinv-download-geoip2-btn button-secondary" value="' . __( 'Download GeoIP2 database now', 'invoicing' ) . ' (~53MB)"></input><br>' . __(  'After downloading the GeoIP2 database the GeoIP2 lookup option will show.', 'invoicing' );
477
        }
478
    } else {
479
        $desc .= $geoip2_message;
480
    }
481
    $desc .= '</p><p>'. __( 'geoPlugin is a great free service please consider supporting them: ', 'invoicing' ) . ' <a href="http://www.geoplugin.com/" target="_blank">GeoPlugin.com</a></p>';
482
    $desc .= '</label>';
483
    
484
    $html .= $desc;
485
486
    echo $html;
487
    ?>
488
    <span id="wpinv-geoip2-errors" class="wpinv_errors" style="display:none;padding:4px;"></span>
489
    <?php
490
}
491