Passed
Push — master ( 6f31f4...23a3a5 )
by Brian
04:37
created

getpaid_save_invoice_user_address()   B

Complexity

Conditions 8
Paths 33

Size

Total Lines 42
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 19
c 1
b 0
f 0
nc 33
nop 1
dl 0
loc 42
rs 8.4444
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
    $first_name = $invoice->get_first_name();
189
    $last_name  = $invoice->get_last_name();
190
    $update     = array();
191
192
    if ( ! empty( $first_name ) ) {
193
        $update['first_name'] = $first_name;
194
    }
195
196
    if ( ! empty( $last_name ) ) {
197
        $update['last_name'] = $last_name;
198
    }
199
200
    if ( 2 == count( $update ) ) {
201
        $update['display_name'] = $first_name . ' ' .$last_name;
202
    }
203
204
    $update['ID'] = $invoice->get_user_id();
205
    wp_update_user( $update );
206
207
}
208
add_action( 'getpaid_new_invoice', 'getpaid_save_invoice_user_address' );
209
add_action( 'getpaid_update_invoice', 'getpaid_save_invoice_user_address' );
210
211
/**
212
 * Retrieves a saved user address.
213
 *
214
 * @param int $user_id The user id whose address we should get. Defaults to the current user id.
215
 * @param bool $with_default Whether or not we should use the default country and state.
216
 * @return array
217
 */
218
function wpinv_get_user_address( $user_id = 0, $with_default = true ) {
219
220
    // Prepare the user id.
221
    $user_id   = empty( $user_id ) ? get_current_user_id() : $user_id;
222
    $user_info = get_userdata( $user_id );
223
224
    // Abort if non exists.
225
    if ( empty( $user_info ) ) {
226
        return array();
227
    }
228
229
    // Prepare the address.
230
    $address = array(
231
        'user_id' => $user_id,
232
        'email'   => $user_info->user_email,
233
    );
234
235
    foreach ( array_keys( getpaid_user_address_fields() ) as $field ) {
236
        $address[$field] = getpaid_get_user_address_field( $user_id, $field );
237
    }
238
239
    if ( ! $with_default ) {
240
        return $address;
241
    }
242
243
    $defaults = array(
244
        'first_name' => $user_info->first_name,
245
        'last_name'  => $user_info->last_name,
246
        'state'      => wpinv_get_default_state(),
247
        'state'      => wpinv_get_default_country(),
248
    );
249
250
    return getpaid_array_merge_if_empty( $address, $defaults );
251
252
}
253
254
/**
255
 * Retrieves a saved user address field.
256
 *
257
 * @param int $user_id The user id whose address field we should get.
258
 * @param string $field The field to use.
259
 * @return string|null
260
 */
261
function getpaid_get_user_address_field( $user_id, $field ) {
262
263
    $prefixes = array(
264
        '_wpinv_',
265
        'billing_',
266
        ''
267
    );
268
269
    foreach ( $prefixes as $prefix ) {
270
271
        // Meta table.
272
        $value = get_user_meta( $user_id, $prefix . $field, true );
273
        
274
        // UWP table.
275
        $value = ( empty( $value ) && function_exists( 'uwp_get_usermeta' ) ) ? uwp_get_usermeta( $user_id, $prefix . $field ) : $value;
276
277
        if ( ! empty( $value ) ) {
278
            return $value;
279
        }
280
281
    }
282
283
    return null;
284
285
}
286
287
/**
288
 * Get all continents.
289
 * 
290
 * @since 1.0.14
291
 * @param string $return What to return.
292
 * @return array
293
 */
294
function wpinv_get_continents( $return = 'all' ) {
295
296
    $continents = wpinv_get_data( 'continents' );
297
298
    switch( $return ) {
299
        case 'name' :
300
            return wp_list_pluck( $continents, 'name' );
301
            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...
302
        case 'countries' :
303
            return wp_list_pluck( $continents, 'countries' );
304
            break;
305
        default :
306
            return $continents;
307
            break;
308
    }
309
310
}
311
312
/**
313
 * Get continent code for a country code.
314
 * 
315
 * @since 1.0.14
316
 * @param string $country Country code. If no code is specified, defaults to the default country.
317
 * @return string
318
 */
319
function wpinv_get_continent_code_for_country( $country = false ) {
320
321
    $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

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