Passed
Pull Request — master (#255)
by
unknown
03:43
created

wpinv_get_turkey_states_list()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * Contains functions related to Invoicing plugin.
4
 *
5
 * @since 1.0.0
6
 * @package Invoicing
7
 */
8
 
9
// MUST have WordPress.
10
if ( !defined( 'WPINC' ) ) {
11
    exit( 'Do NOT access this file directly: ' . basename( __FILE__ ) );
12
}
13
14
15
function wpinv_get_default_country() {
16
	$country = wpinv_get_option( 'default_country', 'UK' );
17
18
	return apply_filters( 'wpinv_default_country', $country );
19
}
20
21
/**
22
 * Sanitizes a country code.
23
 * 
24
 * @param string $country The country code to sanitize
25
 * @return array
26
 */
27
function wpinv_sanitize_country( $country ) {
28
29
	// Enure the country is specified
30
    if ( empty( $country ) ) {
31
        $country = wpinv_get_default_country();
32
    }
33
    return trim( wpinv_utf8_strtoupper( $country ) );
0 ignored issues
show
Bug Best Practice introduced by
The expression return trim(wpinv_utf8_strtoupper($country)) returns the type string which is incompatible with the documented return type array.
Loading history...
34
35
}
36
37
function wpinv_is_base_country( $country ) {
38
    $base_country = wpinv_get_default_country();
39
    
40
    if ( $base_country === 'UK' ) {
41
        $base_country = 'GB';
42
    }
43
    if ( $country == 'UK' ) {
44
        $country = 'GB';
45
    }
46
47
    return ( $country && $country === $base_country ) ? true : false;
48
}
49
50
function wpinv_country_name( $country_code = '' ) { 
51
    $countries = wpinv_get_country_list();
52
    $country_code = $country_code == 'UK' ? 'GB' : $country_code;
53
    $country = isset( $countries[$country_code] ) ? $countries[$country_code] : $country_code;
54
55
    return apply_filters( 'wpinv_country_name', $country, $country_code );
56
}
57
58
function wpinv_get_default_state() {
59
	$state = wpinv_get_option( 'default_state', false );
60
61
	return apply_filters( 'wpinv_default_state', $state );
62
}
63
64
function wpinv_state_name( $state_code = '', $country_code = '' ) {
65
    $state = $state_code;
66
    
67
    if ( !empty( $country_code ) ) {
68
        $states = wpinv_get_country_states( $country_code );
69
        
70
        $state = !empty( $states ) && isset( $states[$state_code] ) ? $states[$state_code] : $state;
71
    }
72
73
    return apply_filters( 'wpinv_state_name', $state, $state_code, $country_code );
74
}
75
76
function wpinv_store_address() {
77
    $address = wpinv_get_option( 'store_address', '' );
78
79
    return apply_filters( 'wpinv_store_address', $address );
80
}
81
82
function wpinv_get_user_address( $user_id = 0, $with_default = true ) {
83
    global $wpi_userID;
84
    
85
    if( empty( $user_id ) ) {
86
        $user_id = !empty( $wpi_userID ) ? $wpi_userID : get_current_user_id();
87
    }
88
    
89
    $address_fields = array(
90
        ///'user_id',
91
        'first_name',
92
        'last_name',
93
        'company',
94
        'vat_number',
95
        ///'email',
96
        'phone',
97
        'address',
98
        'city',
99
        'state',
100
        'country',
101
        'zip',
102
    );
103
    
104
    $user_info = get_userdata( $user_id );
105
    
106
    $address = array();
107
    $address['user_id'] = $user_id;
108
    $address['email'] = !empty( $user_info ) ? $user_info->user_email : '';
109
    foreach ( $address_fields as $field ) {
110
        $address[$field] = get_user_meta( $user_id, '_wpinv_' . $field, true );
111
    }
112
113
    if ( !empty( $user_info ) ) {
114
        if( empty( $address['first_name'] ) )
115
            $address['first_name'] = $user_info->first_name;
116
        
117
        if( empty( $address['last_name'] ) )
118
            $address['last_name'] = $user_info->last_name;
119
    }
120
    
121
    $address['name'] = trim( trim( $address['first_name'] . ' ' . $address['last_name'] ), "," );
122
    
123
    if( empty( $address['state'] ) && $with_default )
124
        $address['state'] = wpinv_get_default_state();
125
126
    if( empty( $address['country'] ) && $with_default )
127
        $address['country'] = wpinv_get_default_country();
128
129
130
    return $address;
131
}
132
133
/**
134
 * Get all continents.
135
 * 
136
 * @since 1.0.14
137
 * @param string $return What to return.
138
 * @return array
139
 */
140
function wpinv_get_continents( $return = 'all' ) {
141
142
    $continents = wpinv_get_data( 'continents' );
143
144
    switch( $return ) {
145
        case 'name' :
146
            return wp_list_pluck( $continents, 'name' );
0 ignored issues
show
Bug introduced by
It seems like $continents can also be of type true; however, parameter $list of wp_list_pluck() does only seem to accept array, 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

146
            return wp_list_pluck( /** @scrutinizer ignore-type */ $continents, 'name' );
Loading history...
147
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
148
        case 'countries' :
149
            return wp_list_pluck( $continents, 'countries' );
150
            break;
151
        default :
152
            return $continents;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $continents also could return the type true which is incompatible with the documented return type array.
Loading history...
153
            break;
154
    }
155
156
}
157
158
/**
159
 * Get continent code for a country code.
160
 * 
161
 * @since 1.0.14
162
 * @param string $country Country code. If no code is specified, defaults to the default country.
163
 * @return string
164
 */
165
function wpinv_get_continent_code_for_country( $country = false ) {
166
167
    $country = wpinv_sanitize_country( $country );
0 ignored issues
show
Bug introduced by
It seems like $country can also be of type false; however, parameter $country of wpinv_sanitize_country() 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

167
    $country = wpinv_sanitize_country( /** @scrutinizer ignore-type */ $country );
Loading history...
168
    
169
	foreach ( wpinv_get_continents( 'countries' ) as $continent_code => $countries ) {
170
		if ( false !== array_search( $country, $countries, true ) ) {
171
			return $continent_code;
172
		}
173
	}
174
175
    return '';
176
    
177
}
178
179
/**
180
 * Get all calling codes.
181
 * 
182
 * @since 1.0.14
183
 * @param string $country Country code. If no code is specified, defaults to the default country.
184
 * @return array
185
 */
186
function wpinv_get_country_calling_code( $country = null) {
187
188
    $country = wpinv_sanitize_country( $country );
189
    $codes   = wpinv_get_data( 'phone-codes' );
190
    $code    = isset( $codes[ $country ] ) ? $codes[ $country ] : '';
191
192
    if ( is_array( $code ) ) {
193
        return $code[0];
194
    }
195
    return $code;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $code also could return the type string which is incompatible with the documented return type array.
Loading history...
196
197
}
198
199
/**
200
 * Get all countries.
201
 * 
202
 * @param bool $first_empty Whether or not the first item in the list should be empty
203
 * @return array
204
 */
205
function wpinv_get_country_list( $first_empty = false ) {
206
    return wpinv_maybe_add_empty_option( apply_filters( 'wpinv_countries', wpinv_get_data( 'countries' ) ), $first_empty );
0 ignored issues
show
Bug introduced by
It seems like apply_filters('wpinv_cou..._get_data('countries')) can also be of type true; however, parameter $options of wpinv_maybe_add_empty_option() does only seem to accept array, 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

206
    return wpinv_maybe_add_empty_option( /** @scrutinizer ignore-type */ apply_filters( 'wpinv_countries', wpinv_get_data( 'countries' ) ), $first_empty );
Loading history...
207
}
208
209
/**
210
 * Retrieves a given country's states.
211
 * 
212
 * @param string $country Country code. If no code is specified, defaults to the default country.
213
 * @param bool $first_empty Whether or not the first item in the list should be empty
214
 * @return array
215
 */
216
function wpinv_get_country_states( $country = null, $first_empty = false ) {
217
    
218
    // Prepare the country.
219
    $country = wpinv_sanitize_country( $country );
220
221
    // Fetch all states.
222
    $all_states = wpinv_get_data( 'states' );
223
224
    // Fetch the specified country's states.
225
    $states     = isset( $all_states[ $country ] ) ? $all_states[ $country ] : array() ;
226
    $states     = apply_filters( "wpinv_{$country}_states", $states );
227
    $states     = apply_filters( 'wpinv_country_states', $states, $country );
228
229
    asort( $states );
230
     
231
    return wpinv_maybe_add_empty_option( $states, $first_empty );
232
}
233
234
/**
235
 * Returns US states.
236
 * 
237
 * @deprecated 1.0.14
238
 * @return array
239
 */
240
function wpinv_get_us_states_list() {
241
    return apply_filters( 'wpinv_usa_states', wpinv_get_country_states( 'US' ) );
242
}
243
244
/**
245
 * Returns Canada states.
246
 * 
247
 * @deprecated 1.0.14
248
 * @return array
249
 */
250
function wpinv_get_canada_states_list() {
251
    return apply_filters( 'wpinv_canada_provinces', wpinv_get_country_states( 'CA' ) );
252
}
253
254
/**
255
 * Returns australian states.
256
 * 
257
 * @deprecated 1.0.14
258
 * @return array
259
 */
260
function wpinv_get_australia_states_list() {
261
    return apply_filters( 'wpinv_australia_states', wpinv_get_country_states( 'AU' ) );
262
}
263
264
/**
265
 * Returns bangladeshi states.
266
 * 
267
 * @deprecated 1.0.14
268
 * @return array
269
 */
270
function wpinv_get_bangladesh_states_list() {
271
    return apply_filters( 'wpinv_bangladesh_states', wpinv_get_country_states( 'BD' ) );
272
}
273
274
/**
275
 * Returns brazilianUS states.
276
 * 
277
 * @deprecated 1.0.14
278
 * @return array
279
 */
280
function wpinv_get_brazil_states_list() {
281
    return apply_filters( 'wpinv_brazil_states', wpinv_get_country_states( 'BR' ) );
282
}
283
284
/**
285
 * Returns bulgarian states.
286
 * 
287
 * @deprecated 1.0.14
288
 * @return array
289
 */
290
function wpinv_get_bulgaria_states_list() {
291
    return apply_filters( 'wpinv_bulgaria_states', wpinv_get_country_states( 'BG' ) );
292
}
293
294
/**
295
 * Returns hong kon states.
296
 * 
297
 * @deprecated 1.0.14
298
 * @return array
299
 */
300
function wpinv_get_hong_kong_states_list() {
301
    return apply_filters( 'wpinv_hong_kong_states', wpinv_get_country_states( 'HK' ) );
302
}
303
304
/**
305
 * Returns hungarian states.
306
 * 
307
 * @deprecated 1.0.14
308
 * @return array
309
 */
310
function wpinv_get_hungary_states_list() {
311
    return apply_filters( 'wpinv_hungary_states', wpinv_get_country_states( 'HU' ) );
312
}
313
314
/**
315
 * Returns japanese states.
316
 * 
317
 * @deprecated 1.0.14
318
 * @return array
319
 */
320
function wpinv_get_japan_states_list() {
321
    return apply_filters( 'wpinv_japan_states', wpinv_get_country_states( 'JP' ) );
322
}
323
324
/**
325
 * Returns chinese states.
326
 * 
327
 * @deprecated 1.0.14
328
 * @return array
329
 */
330
function wpinv_get_china_states_list() {
331
    return apply_filters( 'wpinv_china_states', wpinv_get_country_states( 'CN' ) );
332
}
333
334
/**
335
 * Returns new zealand states.
336
 * 
337
 * @deprecated 1.0.14
338
 * @return array
339
 */
340
function wpinv_get_new_zealand_states_list() {
341
    return apply_filters( 'wpinv_new_zealand_states', wpinv_get_country_states( 'NZ' ) );
342
}
343
344
/**
345
 * Returns perusian states.
346
 * 
347
 * @deprecated 1.0.14
348
 * @return array
349
 */
350
function wpinv_get_peru_states_list() {
351
    return apply_filters( 'wpinv_peru_states', wpinv_get_country_states( 'PE' ) );
352
}
353
354
/**
355
 * Returns indonesian states.
356
 * 
357
 * @deprecated 1.0.14
358
 * @return array
359
 */
360
function wpinv_get_indonesia_states_list() {
361
    return apply_filters( 'wpinv_indonesia_states', wpinv_get_country_states( 'ID' ) );
362
}
363
364
/**
365
 * Returns indian states.
366
 * 
367
 * @deprecated 1.0.14
368
 * @return array
369
 */
370
function wpinv_get_india_states_list() {
371
    return apply_filters( 'wpinv_india_states', wpinv_get_country_states( 'IN' ) );
372
}
373
374
/**
375
 * Returns iranian states.
376
 * 
377
 * @deprecated 1.0.14
378
 * @return array
379
 */
380
function wpinv_get_iran_states_list() {
381
    return apply_filters( 'wpinv_iran_states', wpinv_get_country_states( 'IR' ) );
382
}
383
384
/**
385
 * Returns italian states.
386
 * 
387
 * @deprecated 1.0.14
388
 * @return array
389
 */
390
function wpinv_get_italy_states_list() {
391
    return apply_filters( 'wpinv_italy_states', wpinv_get_country_states( 'IT' ) );
392
}
393
394
/**
395
 * Returns malaysian states.
396
 * 
397
 * @deprecated 1.0.14
398
 * @return array
399
 */
400
function wpinv_get_malaysia_states_list() {
401
    return apply_filters( 'wpinv_malaysia_states', wpinv_get_country_states( 'MY' ) );
402
}
403
404
/**
405
 * Returns mexican states.
406
 * 
407
 * @deprecated 1.0.14
408
 * @return array
409
 */
410
function wpinv_get_mexico_states_list() {
411
    return apply_filters( 'wpinv_mexico_states', wpinv_get_country_states( 'MX' ) );
412
}
413
414
/**
415
 * Returns nepal states.
416
 * 
417
 * @deprecated 1.0.14
418
 * @return array
419
 */
420
function wpinv_get_nepal_states_list() {
421
    return apply_filters( 'wpinv_nepal_states', wpinv_get_country_states( 'NP' ) );
422
}
423
424
/**
425
 * Returns south african states.
426
 * 
427
 * @deprecated 1.0.14
428
 * @return array
429
 */
430
function wpinv_get_south_africa_states_list() {
431
    return apply_filters( 'wpinv_south_africa_states', wpinv_get_country_states( 'ZA' ) );
432
}
433
434
/**
435
 * Returns thailandese states.
436
 * 
437
 * @deprecated 1.0.14
438
 * @return array
439
 */
440
function wpinv_get_thailand_states_list() {
441
    return apply_filters( 'wpinv_thailand_states', wpinv_get_country_states( 'TH' ) );
442
}
443
444
/**
445
 * Returns turkish states.
446
 * 
447
 * @deprecated 1.0.14
448
 * @return array
449
 */
450
function wpinv_get_turkey_states_list() {
451
    return apply_filters( 'wpinv_turkey_states', wpinv_get_country_states( 'TR' ) );
452
}
453
454
/**
455
 * Returns spanish states.
456
 * 
457
 * @deprecated 1.0.14
458
 * @return array
459
 */
460
function wpinv_get_spain_states_list() {
461
    return apply_filters( 'wpinv_spain_states', wpinv_get_country_states( 'ES' ) );
462
}
463
464
function wpinv_get_states_field() {
465
	if( empty( $_POST['country'] ) ) {
466
		$_POST['country'] = wpinv_get_default_country();
467
	}
468
	$states = wpinv_get_country_states( sanitize_text_field( $_POST['country'] ) );
469
470
	if( !empty( $states ) ) {
471
		$sanitized_field_name = sanitize_text_field( $_POST['field_name'] );
472
        
473
        $args = array(
474
			'name'    => $sanitized_field_name,
475
			'id'      => $sanitized_field_name,
476
			'class'   => $sanitized_field_name . ' wpinv-select wpi_select2',
477
			'options' => array_merge( array( '' => '' ), $states ),
478
			'show_option_all'  => false,
479
			'show_option_none' => false
480
		);
481
482
		$response = wpinv_html_select( $args );
483
484
	} else {
485
		$response = 'nostates';
486
	}
487
488
	return $response;
489
}
490
491
function wpinv_default_billing_country( $country = '', $user_id = 0 ) {
492
    $country = !empty( $country ) ? $country : wpinv_get_default_country();
493
    
494
    return apply_filters( 'wpinv_default_billing_country', $country, $user_id );
495
}
496
497
/**
498
 * Retrieves the address format to use on Invoices.
499
 * 
500
 * @since 1.0.13
501
 * @see `wpinv_get_invoice_address_replacements`
502
 * @return string
503
 */
504
function wpinv_get_full_address_format() {
505
506
    $format = "{{address}} \n\n {{city}}, {{state}} \n\n {{country}} {{zip}}";
507
    
508
    /**
509
	 * Filters the address format to use on Invoices.
510
     * 
511
     * New lines will be replaced by a `br` element. Double new lines will be replaced by a paragraph. HTML tags are allowed.
512
	 *
513
	 * @since 1.0.13
514
	 *
515
	 * @param string $format  The address format to use.
516
	 */
517
    return apply_filters( 'wpinv_get_full_address_format', $format );
518
}
519
520
/**
521
 * Retrieves the address format replacements to use on Invoices.
522
 * 
523
 * @since 1.0.13
524
 * @see `wpinv_get_full_address_format`
525
 * @param array $billing_details customer's billing details
526
 * @return array
527
 */
528
function wpinv_get_invoice_address_replacements( $billing_details ) {
529
530
    $replacements = array(
531
        'address'        => '',
532
        'city'           => '',
533
        'state'          => '',
534
        'country'        => '',
535
        'country_code'   => '',
536
        'zip'            => '',
537
    );
538
539
    if( ! empty( $billing_details['address'] ) ) {
540
        $replacements['address'] = sanitize_text_field( $billing_details['address'] );
541
    }
542
543
    if( ! empty( $billing_details['city'] ) ) {
544
        $replacements['city'] = sanitize_text_field( $billing_details['city'] );
545
    }
546
547
    if( ! empty( $billing_details['zip'] ) ) {
548
        $replacements['zip'] = sanitize_text_field( $billing_details['zip'] );
549
    }
550
    
551
    $billing_country = !empty( $billing_details['country'] ) ? $billing_details['country'] : '';
552
    if ( !empty( $billing_details['state'] ) ) {
553
        $replacements['state'] = sanitize_text_field( wpinv_state_name( $billing_details['state'], $billing_country ) );
554
    }
555
556
    if ( !empty( $billing_country ) ) {
557
        $replacements['country']      = wpinv_country_name( $billing_country );
558
        $replacements['country_code'] = sanitize_text_field( $billing_country );
559
    }
560
    
561
    /**
562
	 * Filters the address format replacements to use on Invoices.
563
     * 
564
	 *
565
	 * @since 1.0.13
566
	 *
567
	 * @param array $replacements  The address replacements to use.
568
     * @param array $billing_details  The billing details to use.
569
	 */
570
    return apply_filters( 'wpinv_get_invoice_address_replacements', $replacements, $billing_details );
571
}