Passed
Push — master ( ece56d...190d73 )
by Brian
09:21 queued 04:12
created

wpinv_get_user_address()   A

Complexity

Conditions 5
Paths 10

Size

Total Lines 33
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
c 0
b 0
f 0
dl 0
loc 33
rs 9.3888
cc 5
nc 10
nop 2
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
 * GeoLocates an IP Address.
23
 *
24
 * @return string
25
 */
26
function getpaid_get_ip_country( $ip_address = '' ) {
27
    $country = GetPaid_Geolocation::geolocate_ip( $ip_address, true );
28
    return $country['country'];
29
}
30
31
/**
32
 * Sanitizes a country code.
33
 * 
34
 * @param string $country The country code to sanitize
35
 * @return array
36
 */
37
function wpinv_sanitize_country( $country ) {
38
39
	// Enure the country is specified
40
    if ( empty( $country ) ) {
41
        $country = wpinv_get_default_country();
42
    }
43
    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...
44
45
}
46
47
function wpinv_is_base_country( $country ) {
48
    $base_country = wpinv_get_default_country();
49
    
50
    if ( $base_country === 'UK' ) {
51
        $base_country = 'GB';
52
    }
53
    if ( $country == 'UK' ) {
54
        $country = 'GB';
55
    }
56
57
    return ( $country && $country === $base_country ) ? true : false;
58
}
59
60
function wpinv_country_name( $country_code = '' ) { 
61
    $countries = wpinv_get_country_list();
62
    $country_code = $country_code == 'UK' ? 'GB' : $country_code;
63
    $country = isset( $countries[$country_code] ) ? $countries[$country_code] : $country_code;
64
65
    return apply_filters( 'wpinv_country_name', $country, $country_code );
66
}
67
68
function wpinv_get_default_state() {
69
	$state = wpinv_get_option( 'default_state', '' );
70
71
	return apply_filters( 'wpinv_default_state', $state );
72
}
73
74
function wpinv_state_name( $state_code = '', $country_code = '' ) {
75
    $state = $state_code;
76
    
77
    if ( !empty( $country_code ) ) {
78
        $states = wpinv_get_country_states( $country_code );
79
        
80
        $state = !empty( $states ) && isset( $states[$state_code] ) ? $states[$state_code] : $state;
81
    }
82
83
    return apply_filters( 'wpinv_state_name', $state, $state_code, $country_code );
84
}
85
86
function wpinv_store_address() {
87
    $address = wpinv_get_option( 'store_address', '' );
88
89
    return apply_filters( 'wpinv_store_address', $address );
90
}
91
92
/**
93
 * (Maybe) adds the default address to an invoice.
94
 *
95
 * @param WPInv_Invoice $invoice
96
 */
97
function getpaid_maybe_add_default_address( &$invoice ) {
98
99
    $user_id = $invoice->get_user_id();
100
101
    // Abort if the invoice belongs to no one.
102
    if ( empty( $user_id ) ) {
103
        return;
104
    }
105
106
    // Fill in defaults whenever necessary.
107
    foreach ( wpinv_get_user_address( $user_id ) as $key => $value ) {
108
109
        if ( is_callable( $invoice, "get_$key" ) ) {
0 ignored issues
show
Bug introduced by
'get_'.$key of type string is incompatible with the type boolean expected by parameter $syntax_only of is_callable(). ( Ignorable by Annotation )

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

109
        if ( is_callable( $invoice, /** @scrutinizer ignore-type */ "get_$key" ) ) {
Loading history...
110
            $current = call_user_func( array( $invoice, "get_$key" ) );
111
112
            if ( empty( $current ) ) {
113
                $method = "set_$key";
114
                $invoice->$method( $value );
115
            }
116
117
        }
118
119
    }
120
121
}
122
123
/**
124
 * Returns an array of user address fields
125
 * 
126
 * @return array
127
 */
128
function getpaid_user_address_fields() {
129
130
    return apply_filters(
131
        'getpaid_user_address_fields',
132
        array(
133
            'first_name' => __( 'First Name', 'invoicing' ),
134
            'last_name'  => __( 'Last Name', 'invoicing' ),
135
            'country'    => __( 'Country', 'invoicing' ),
136
            'state'      => __( 'State', 'invoicing' ),
137
            'city'       => __( 'City', 'invoicing' ),
138
            'zip'        => __( 'Zip/Postal Code', 'invoicing' ),
139
            'address'    => __( 'Address', 'invoicing' ),
140
            'phone'      => __( 'Phone Number', 'invoicing' ),
141
            'company'    => __( 'Company', 'invoicing' ),
142
            'vat_number' => __( 'VAT Number', 'invoicing' ),
143
        )
144
    );
145
146
}
147
148
/**
149
 * Checks whether or not an address field is whitelisted.
150
 * 
151
 * @return bool
152
 */
153
function getpaid_is_address_field_whitelisted( $key ) {
154
    return array_key_exists( $key, getpaid_user_address_fields() );
155
}
156
157
/**
158
 * Saves a user address.
159
 *
160
 * This function is called whenever an invoice is created/updated to ensure that the user address is always up to date.
161
 *
162
 * @param WPInv_Invoice $invoice
163
 */
164
function getpaid_save_invoice_user_address( $invoice ) {
165
166
    // Retrieve the invoice.
167
    $invoice = wpinv_get_invoice( $invoice );
168
169
    // Abort if it does not exist.
170
    if ( empty( $invoice ) ) {
171
        return;
172
    }
173
174
    foreach ( array_keys( getpaid_user_address_fields() ) as $field ) {
175
176
        if ( is_callable( array( $invoice, "get_$field" ) ) ) {
177
            $value = call_user_func( array( $invoice, "get_$field" ) );
178
179
            // Only save if it is not empty.
180
            if ( ! empty( $value ) ) {
181
                update_user_meta( $invoice->get_user_id(), '_wpinv_' . $field, $value );
182
            }
183
184
        }
185
186
    }
187
188
}
189
add_action( 'getpaid_new_invoice', 'getpaid_save_invoice_user_address' );
190
add_action( 'getpaid_update_invoice', 'getpaid_save_invoice_user_address' );
191
192
/**
193
 * Retrieves a saved user address.
194
 *
195
 * @param int $user_id The user id whose address we should get. Defaults to the current user id.
196
 * @param bool $with_default Whether or not we should use the default country and state.
197
 * @return array
198
 */
199
function wpinv_get_user_address( $user_id = 0, $with_default = true ) {
200
201
    // Prepare the user id.
202
    $user_id   = empty( $user_id ) ? get_current_user_id() : $user_id;
203
    $user_info = get_userdata( $user_id );
204
205
    // Abort if non exists.
206
    if ( empty( $user_info ) ) {
207
        return array();
208
    }
209
210
    // Prepare the address.
211
    $address = array(
212
        'user_id' => $user_id,
213
        'email'   => $user_info->user_email,
214
    );
215
216
    foreach ( array_keys( getpaid_user_address_fields() ) as $field ) {
217
        $address[$field] = getpaid_get_user_address_field( $user_id, $field );
218
    }
219
220
    if ( ! $with_default ) {
221
        return $address;
222
    }
223
224
    $defaults = array(
225
        'first_name' => $user_info->first_name,
226
        'last_name'  => $user_info->last_name,
227
        'state'      => wpinv_get_default_state(),
228
        'state'      => wpinv_get_default_country(),
229
    );
230
231
    return getpaid_array_merge_if_empty( $address, $defaults );
232
233
}
234
235
/**
236
 * Retrieves a saved user address field.
237
 *
238
 * @param int $user_id The user id whose address field we should get.
239
 * @param string $field The field to use.
240
 * @return string|null
241
 */
242
function getpaid_get_user_address_field( $user_id, $field ) {
243
244
    $prefixes = array(
245
        '_wpinv_',
246
        'billing_',
247
        ''
248
    );
249
250
    foreach ( $prefixes as $prefix ) {
251
252
        // Meta table.
253
        $value = get_user_meta( $user_id, $prefix . $field, true );
254
        
255
        // UWP table.
256
        $value = ( empty( $value ) && function_exists( 'uwp_get_usermeta' ) ) ? uwp_get_usermeta( $user_id, $prefix . $field ) : $value;
257
258
        if ( ! empty( $value ) ) {
259
            return $value;
260
        }
261
262
    }
263
264
    return null;
265
266
}
267
268
/**
269
 * Get all continents.
270
 * 
271
 * @since 1.0.14
272
 * @param string $return What to return.
273
 * @return array
274
 */
275
function wpinv_get_continents( $return = 'all' ) {
276
277
    $continents = wpinv_get_data( 'continents' );
278
279
    switch( $return ) {
280
        case 'name' :
281
            return wp_list_pluck( $continents, 'name' );
282
            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...
283
        case 'countries' :
284
            return wp_list_pluck( $continents, 'countries' );
285
            break;
286
        default :
287
            return $continents;
288
            break;
289
    }
290
291
}
292
293
/**
294
 * Get continent code for a country code.
295
 * 
296
 * @since 1.0.14
297
 * @param string $country Country code. If no code is specified, defaults to the default country.
298
 * @return string
299
 */
300
function wpinv_get_continent_code_for_country( $country = false ) {
301
302
    $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

302
    $country = wpinv_sanitize_country( /** @scrutinizer ignore-type */ $country );
Loading history...
303
    
304
	foreach ( wpinv_get_continents( 'countries' ) as $continent_code => $countries ) {
305
		if ( false !== array_search( $country, $countries, true ) ) {
306
			return $continent_code;
307
		}
308
	}
309
310
    return '';
311
    
312
}
313
314
/**
315
 * Get all calling codes.
316
 * 
317
 * @since 1.0.14
318
 * @param string $country Country code. If no code is specified, defaults to the default country.
319
 * @return array
320
 */
321
function wpinv_get_country_calling_code( $country = null) {
322
323
    $country = wpinv_sanitize_country( $country );
324
    $codes   = wpinv_get_data( 'phone-codes' );
325
    $code    = isset( $codes[ $country ] ) ? $codes[ $country ] : '';
326
327
    if ( is_array( $code ) ) {
328
        return $code[0];
329
    }
330
    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...
331
332
}
333
334
/**
335
 * Get all countries.
336
 * 
337
 * @param bool $first_empty Whether or not the first item in the list should be empty
338
 * @return array
339
 */
340
function wpinv_get_country_list( $first_empty = false ) {
341
    return wpinv_maybe_add_empty_option( apply_filters( 'wpinv_countries', wpinv_get_data( 'countries' ) ), $first_empty );
342
}
343
344
/**
345
 * Retrieves a given country's states.
346
 * 
347
 * @param string $country Country code. If no code is specified, defaults to the default country.
348
 * @param bool $first_empty Whether or not the first item in the list should be empty
349
 * @return array
350
 */
351
function wpinv_get_country_states( $country = null, $first_empty = false ) {
352
    
353
    // Prepare the country.
354
    $country = wpinv_sanitize_country( $country );
355
356
    // Fetch all states.
357
    $all_states = wpinv_get_data( 'states' );
358
359
    // Fetch the specified country's states.
360
    $states     = isset( $all_states[ $country ] ) ? $all_states[ $country ] : array() ;
361
    $states     = apply_filters( "wpinv_{$country}_states", $states );
362
    $states     = apply_filters( 'wpinv_country_states', $states, $country );
363
364
    asort( $states );
365
     
366
    return wpinv_maybe_add_empty_option( $states, $first_empty );
367
}
368
369
/**
370
 * Returns US states.
371
 * 
372
 * @deprecated 1.0.14
373
 * @return array
374
 */
375
function wpinv_get_us_states_list() {
376
    return apply_filters( 'wpinv_usa_states', wpinv_get_country_states( 'US' ) );
377
}
378
379
/**
380
 * Returns Canada states.
381
 * 
382
 * @deprecated 1.0.14
383
 * @return array
384
 */
385
function wpinv_get_canada_states_list() {
386
    return apply_filters( 'wpinv_canada_provinces', wpinv_get_country_states( 'CA' ) );
387
}
388
389
/**
390
 * Returns australian states.
391
 * 
392
 * @deprecated 1.0.14
393
 * @return array
394
 */
395
function wpinv_get_australia_states_list() {
396
    return apply_filters( 'wpinv_australia_states', wpinv_get_country_states( 'AU' ) );
397
}
398
399
/**
400
 * Returns bangladeshi states.
401
 * 
402
 * @deprecated 1.0.14
403
 * @return array
404
 */
405
function wpinv_get_bangladesh_states_list() {
406
    return apply_filters( 'wpinv_bangladesh_states', wpinv_get_country_states( 'BD' ) );
407
}
408
409
/**
410
 * Returns brazilianUS states.
411
 * 
412
 * @deprecated 1.0.14
413
 * @return array
414
 */
415
function wpinv_get_brazil_states_list() {
416
    return apply_filters( 'wpinv_brazil_states', wpinv_get_country_states( 'BR' ) );
417
}
418
419
/**
420
 * Returns bulgarian states.
421
 * 
422
 * @deprecated 1.0.14
423
 * @return array
424
 */
425
function wpinv_get_bulgaria_states_list() {
426
    return apply_filters( 'wpinv_bulgaria_states', wpinv_get_country_states( 'BG' ) );
427
}
428
429
/**
430
 * Returns hong kon states.
431
 * 
432
 * @deprecated 1.0.14
433
 * @return array
434
 */
435
function wpinv_get_hong_kong_states_list() {
436
    return apply_filters( 'wpinv_hong_kong_states', wpinv_get_country_states( 'HK' ) );
437
}
438
439
/**
440
 * Returns hungarian states.
441
 * 
442
 * @deprecated 1.0.14
443
 * @return array
444
 */
445
function wpinv_get_hungary_states_list() {
446
    return apply_filters( 'wpinv_hungary_states', wpinv_get_country_states( 'HU' ) );
447
}
448
449
/**
450
 * Returns japanese states.
451
 * 
452
 * @deprecated 1.0.14
453
 * @return array
454
 */
455
function wpinv_get_japan_states_list() {
456
    return apply_filters( 'wpinv_japan_states', wpinv_get_country_states( 'JP' ) );
457
}
458
459
/**
460
 * Returns chinese states.
461
 * 
462
 * @deprecated 1.0.14
463
 * @return array
464
 */
465
function wpinv_get_china_states_list() {
466
    return apply_filters( 'wpinv_china_states', wpinv_get_country_states( 'CN' ) );
467
}
468
469
/**
470
 * Returns new zealand states.
471
 * 
472
 * @deprecated 1.0.14
473
 * @return array
474
 */
475
function wpinv_get_new_zealand_states_list() {
476
    return apply_filters( 'wpinv_new_zealand_states', wpinv_get_country_states( 'NZ' ) );
477
}
478
479
/**
480
 * Returns perusian states.
481
 * 
482
 * @deprecated 1.0.14
483
 * @return array
484
 */
485
function wpinv_get_peru_states_list() {
486
    return apply_filters( 'wpinv_peru_states', wpinv_get_country_states( 'PE' ) );
487
}
488
489
/**
490
 * Returns indonesian states.
491
 * 
492
 * @deprecated 1.0.14
493
 * @return array
494
 */
495
function wpinv_get_indonesia_states_list() {
496
    return apply_filters( 'wpinv_indonesia_states', wpinv_get_country_states( 'ID' ) );
497
}
498
499
/**
500
 * Returns indian states.
501
 * 
502
 * @deprecated 1.0.14
503
 * @return array
504
 */
505
function wpinv_get_india_states_list() {
506
    return apply_filters( 'wpinv_india_states', wpinv_get_country_states( 'IN' ) );
507
}
508
509
/**
510
 * Returns iranian states.
511
 * 
512
 * @deprecated 1.0.14
513
 * @return array
514
 */
515
function wpinv_get_iran_states_list() {
516
    return apply_filters( 'wpinv_iran_states', wpinv_get_country_states( 'IR' ) );
517
}
518
519
/**
520
 * Returns italian states.
521
 * 
522
 * @deprecated 1.0.14
523
 * @return array
524
 */
525
function wpinv_get_italy_states_list() {
526
    return apply_filters( 'wpinv_italy_states', wpinv_get_country_states( 'IT' ) );
527
}
528
529
/**
530
 * Returns malaysian states.
531
 * 
532
 * @deprecated 1.0.14
533
 * @return array
534
 */
535
function wpinv_get_malaysia_states_list() {
536
    return apply_filters( 'wpinv_malaysia_states', wpinv_get_country_states( 'MY' ) );
537
}
538
539
/**
540
 * Returns mexican states.
541
 * 
542
 * @deprecated 1.0.14
543
 * @return array
544
 */
545
function wpinv_get_mexico_states_list() {
546
    return apply_filters( 'wpinv_mexico_states', wpinv_get_country_states( 'MX' ) );
547
}
548
549
/**
550
 * Returns nepal states.
551
 * 
552
 * @deprecated 1.0.14
553
 * @return array
554
 */
555
function wpinv_get_nepal_states_list() {
556
    return apply_filters( 'wpinv_nepal_states', wpinv_get_country_states( 'NP' ) );
557
}
558
559
/**
560
 * Returns south african states.
561
 * 
562
 * @deprecated 1.0.14
563
 * @return array
564
 */
565
function wpinv_get_south_africa_states_list() {
566
    return apply_filters( 'wpinv_south_africa_states', wpinv_get_country_states( 'ZA' ) );
567
}
568
569
/**
570
 * Returns thailandese states.
571
 * 
572
 * @deprecated 1.0.14
573
 * @return array
574
 */
575
function wpinv_get_thailand_states_list() {
576
    return apply_filters( 'wpinv_thailand_states', wpinv_get_country_states( 'TH' ) );
577
}
578
579
/**
580
 * Returns turkish states.
581
 * 
582
 * @deprecated 1.0.14
583
 * @return array
584
 */
585
function wpinv_get_turkey_states_list() {
586
    return apply_filters( 'wpinv_turkey_states', wpinv_get_country_states( 'TR' ) );
587
}
588
589
/**
590
 * Returns spanish states.
591
 * 
592
 * @deprecated 1.0.14
593
 * @return array
594
 */
595
function wpinv_get_spain_states_list() {
596
    return apply_filters( 'wpinv_spain_states', wpinv_get_country_states( 'ES' ) );
597
}
598
599
function wpinv_get_states_field() {
600
	if( empty( $_POST['country'] ) ) {
601
		$_POST['country'] = wpinv_get_default_country();
602
	}
603
	$states = wpinv_get_country_states( sanitize_text_field( $_POST['country'] ) );
604
605
	if( !empty( $states ) ) {
606
		$sanitized_field_name = sanitize_text_field( $_POST['field_name'] );
607
        
608
        $args = array(
609
			'name'    => $sanitized_field_name,
610
			'id'      => $sanitized_field_name,
611
			'class'   => $sanitized_field_name . 'custom-select wpinv-select wpi_select2',
612
			'options' => array_merge( array( '' => '' ), $states ),
613
			'show_option_all'  => false,
614
			'show_option_none' => false
615
		);
616
617
		$response = wpinv_html_select( $args );
618
619
	} else {
620
		$response = 'nostates';
621
	}
622
623
	return $response;
624
}
625
626
function wpinv_default_billing_country( $country = '', $user_id = 0 ) {
627
    $country = !empty( $country ) ? $country : wpinv_get_default_country();
628
    
629
    return apply_filters( 'wpinv_default_billing_country', $country, $user_id );
630
}
631
632
/**
633
 * Returns country address formats.
634
 *
635
 * These define how addresses are formatted for display in various countries.
636
 *
637
 * @return array
638
 */
639
function wpinv_get_address_formats() {
640
641
		return apply_filters( 'wpinv_localisation_address_formats',
642
			array(
643
				'default' => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{state}}\n{{zip}}\n{{country}}",
644
				'AU'      => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{state}} {{zip}}\n{{country}}",
645
				'AT'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
646
				'BE'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
647
				'CA'      => "{{company}}\n{{name}}\n{{address}}\n{{city}} {{state_code}}&nbsp;&nbsp;{{zip}}\n{{country}}",
648
				'CH'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
649
				'CL'      => "{{company}}\n{{name}}\n{{address}}\n{{state}}\n{{zip}} {{city}}\n{{country}}",
650
				'CN'      => "{{country}} {{zip}}\n{{state}}, {{city}}, {{address}}\n{{company}}\n{{name}}",
651
				'CZ'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
652
				'DE'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
653
				'EE'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
654
				'FI'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
655
				'DK'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
656
				'FR'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city_upper}}\n{{country}}",
657
				'HK'      => "{{company}}\n{{first_name}} {{last_name_upper}}\n{{address}}\n{{city_upper}}\n{{state_upper}}\n{{country}}",
658
				'HU'      => "{{name}}\n{{company}}\n{{city}}\n{{address}}\n{{zip}}\n{{country}}",
659
				'IN'      => "{{company}}\n{{name}}\n{{address}}\n{{city}} {{zip}}\n{{state}}, {{country}}",
660
				'IS'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
661
				'IT'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}}\n{{city}}\n{{state_upper}}\n{{country}}",
662
				'JP'      => "{{zip}}\n{{state}} {{city}} {{address}}\n{{company}}\n{{last_name}} {{first_name}}\n{{country}}",
663
				'TW'      => "{{company}}\n{{last_name}} {{first_name}}\n{{address}}\n{{state}}, {{city}} {{zip}}\n{{country}}",
664
				'LI'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
665
				'NL'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
666
				'NZ'      => "{{name}}\n{{company}}\n{{address}}\n{{city}} {{zip}}\n{{country}}",
667
				'NO'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
668
				'PL'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
669
				'PT'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
670
				'SK'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
671
				'RS'      => "{{name}}\n{{company}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
672
				'SI'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
673
				'ES'      => "{{name}}\n{{company}}\n{{address}}\n{{zip}} {{city}}\n{{state}}\n{{country}}",
674
				'SE'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
675
				'TR'      => "{{name}}\n{{company}}\n{{address}}\n{{zip}} {{city}} {{state}}\n{{country}}",
676
				'UG'      => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{state}}, {{country}}",
677
				'US'      => "{{name}}\n{{company}}\n{{address}}\n{{city}}, {{state_code}} {{zip}}\n{{country}}",
678
				'VN'      => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{country}}",
679
			)
680
		);
681
}
682
683
/**
684
 * Retrieves the address format to use on Invoices.
685
 * 
686
 * @since 1.0.13
687
 * @see `wpinv_get_invoice_address_replacements`
688
 * @return string
689
 */
690
function wpinv_get_full_address_format( $country = false) {
691
692
    if( empty( $country ) ) {
693
        $country = wpinv_get_default_country();
694
    }
695
696
    // Get all formats.
697
	$formats = wpinv_get_address_formats();
698
699
	// Get format for the specified country.
700
	$format = ( $country && isset( $formats[ $country ] ) ) ? $formats[ $country ] : $formats['default'];
701
    
702
    /**
703
	 * Filters the address format to use on Invoices.
704
     * 
705
     * New lines will be replaced by a `br` element. Double new lines will be replaced by a paragraph. HTML tags are allowed.
706
	 *
707
	 * @since 1.0.13
708
	 *
709
	 * @param string $format  The address format to use.
710
     * @param string $country The country who's address format is being retrieved.
711
	 */
712
    return apply_filters( 'wpinv_get_full_address_format', $format, $country );
713
}
714
715
/**
716
 * Retrieves the address format replacements to use on Invoices.
717
 * 
718
 * @since 1.0.13
719
 * @see `wpinv_get_full_address_format`
720
 * @param array $billing_details customer's billing details
721
 * @return array
722
 */
723
function wpinv_get_invoice_address_replacements( $billing_details ) {
724
725
    $default_args = array(
726
        'address'           => '',
727
        'city'              => '',
728
        'state'             => '',
729
        'country'           => '',
730
        'zip'               => '',
731
        'first_name'        => '',
732
		'last_name'         => '',
733
		'company'           => '',
734
    );
735
736
    $args    = map_deep( wp_parse_args( $billing_details, $default_args ), 'trim' );
737
    $state   = $args['state'];
738
    $country = $args['country'];
739
740
    // Handle full country name.
741
    $full_country = empty( $country ) ? $country : wpinv_country_name( $country );
742
743
    // Handle full state name.
744
    $full_state   = ( $country && $state ) ?  wpinv_state_name( $state, $country ) : $state;
745
746
    $args['postcode']    = $args['zip'];
747
    $args['name']        = $args['first_name'] . ' ' . $args['last_name'];
748
    $args['state']       = $full_state;
749
    $args['state_code']  = $state;
750
    $args['country']     = $full_country;
751
    $args['country_code']= $country;
752
753
    /**
754
	 * Filters the address format replacements to use on Invoices.
755
     * 
756
	 *
757
	 * @since 1.0.13
758
	 *
759
	 * @param array $replacements  The address replacements to use.
760
     * @param array $billing_details  The billing details to use.
761
	 */
762
    $replacements = apply_filters( 'wpinv_get_invoice_address_replacements', $args, $billing_details );
763
764
    $return = array();
765
766
    foreach( $replacements as $key => $value ) {
767
        $value  = is_scalar( $value ) ? trim( sanitize_text_field( $value ) ) : '';
768
        $return['{{' . $key . '}}'] = $value;
769
        $return['{{' . $key . '_upper}}'] = wpinv_utf8_strtoupper( $value );
770
    }
771
772
    return $return;
773
774
}
775
776
/**
777
 * Trim white space and commas off a line.
778
 *
779
 * @param  string $line Line.
780
 * @since 1.0.14
781
 * @return string
782
 */
783
function wpinv_trim_formatted_address_line( $line ) {
784
	return trim( $line, ', ' );
785
}