Passed
Push — master ( 345b42...86cae5 )
by Brian
05:49
created

getpaid_save_invoice_user_address()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 30
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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

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