Passed
Push — master ( 8806b9...c1548d )
by Brian
04:18
created

getpaid_save_invoice_user_address()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 8
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 18
rs 9.6111
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
    $address_fields = 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
    if ( ! wpinv_use_taxes() && isset( $address_fields['vat_number'] ) ) {
147
        unset( $address_fields['vat_number'] );
148
    }
149
150
    return $address_fields;
151
}
152
153
/**
154
 * Checks whether or not an address field is whitelisted.
155
 * 
156
 * @return bool
157
 */
158
function getpaid_is_address_field_whitelisted( $key ) {
159
    return array_key_exists( $key, getpaid_user_address_fields() );
160
}
161
162
/**
163
 * Saves a user address.
164
 *
165
 * This function is called whenever an invoice is created/updated to ensure that the user address is always up to date.
166
 *
167
 * @param WPInv_Invoice $invoice
168
 */
169
function getpaid_save_invoice_user_address( $invoice ) {
170
171
    // Retrieve the invoice.
172
    $invoice = wpinv_get_invoice( $invoice );
173
174
    // Abort if it does not exist.
175
    if ( empty( $invoice ) ) {
176
        return;
177
    }
178
179
    foreach ( array_keys( getpaid_user_address_fields() ) as $field ) {
180
181
        if ( is_callable( array( $invoice, "get_$field" ) ) ) {
182
            $value = call_user_func( array( $invoice, "get_$field" ) );
183
184
            // Only save if it is not empty.
185
            if ( ! empty( $value ) ) {
186
                update_user_meta( $invoice->get_user_id(), '_wpinv_' . $field, $value );
187
            }
188
189
        }
190
191
    }
192
193
}
194
add_action( 'getpaid_new_invoice', 'getpaid_save_invoice_user_address' );
195
add_action( 'getpaid_update_invoice', 'getpaid_save_invoice_user_address' );
196
197
/**
198
 * Retrieves a saved user address.
199
 *
200
 * @param int $user_id The user id whose address we should get. Defaults to the current user id.
201
 * @param bool $with_default Whether or not we should use the default country and state.
202
 * @return array
203
 */
204
function wpinv_get_user_address( $user_id = 0, $with_default = true ) {
205
206
    // Prepare the user id.
207
    $user_id   = empty( $user_id ) ? get_current_user_id() : $user_id;
208
    $user_info = get_userdata( $user_id );
209
210
    // Abort if non exists.
211
    if ( empty( $user_info ) ) {
212
        return array();
213
    }
214
215
    // Prepare the address.
216
    $address = array(
217
        'user_id' => $user_id,
218
        'email'   => $user_info->user_email,
219
    );
220
221
    foreach ( array_keys( getpaid_user_address_fields() ) as $field ) {
222
        $address[$field] = getpaid_get_user_address_field( $user_id, $field );
223
    }
224
225
    if ( ! $with_default ) {
226
        return $address;
227
    }
228
229
    $defaults = array(
230
        'first_name' => $user_info->first_name,
231
        'last_name'  => $user_info->last_name,
232
        'state'      => wpinv_get_default_state(),
233
        'country'    => wpinv_get_default_country(),
234
    );
235
236
    return getpaid_array_merge_if_empty( $address, $defaults );
237
238
}
239
240
/**
241
 * Retrieves a saved user address field.
242
 *
243
 * @param int $user_id The user id whose address field we should get.
244
 * @param string $field The field to use.
245
 * @return string|null
246
 */
247
function getpaid_get_user_address_field( $user_id, $field ) {
248
249
    $prefixes = array(
250
        '_wpinv_',
251
        'billing_',
252
        ''
253
    );
254
255
    foreach ( $prefixes as $prefix ) {
256
257
        // Meta table.
258
        $value = get_user_meta( $user_id, $prefix . $field, true );
259
        
260
        // UWP table.
261
        $value = ( empty( $value ) && function_exists( 'uwp_get_usermeta' ) ) ? uwp_get_usermeta( $user_id, $prefix . $field ) : $value;
262
263
        if ( ! empty( $value ) ) {
264
            return $value;
265
        }
266
267
    }
268
269
    return null;
270
271
}
272
273
/**
274
 * Get all continents.
275
 * 
276
 * @since 1.0.14
277
 * @param string $return What to return.
278
 * @return array
279
 */
280
function wpinv_get_continents( $return = 'all' ) {
281
282
    $continents = wpinv_get_data( 'continents' );
283
284
    switch( $return ) {
285
        case 'name' :
286
            return wp_list_pluck( $continents, 'name' );
287
            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...
288
        case 'countries' :
289
            return wp_list_pluck( $continents, 'countries' );
290
            break;
291
        default :
292
            return $continents;
293
            break;
294
    }
295
296
}
297
298
/**
299
 * Get continent code for a country code.
300
 * 
301
 * @since 1.0.14
302
 * @param string $country Country code. If no code is specified, defaults to the default country.
303
 * @return string
304
 */
305
function wpinv_get_continent_code_for_country( $country = false ) {
306
307
    $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

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