Passed
Push — master ( c396d4...1c83fa )
by Brian
09:37 queued 04:59
created

getpaid_get_user_address_field()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 23
rs 9.6111
cc 5
nc 9
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
 * Sanitizes a country code.
23
 * 
24
 * @param string $country The country code to sanitize
25
 * @return array
26
 */
27
function wpinv_sanitize_country( $country ) {
28
29
	// Enure the country is specified
30
    if ( empty( $country ) ) {
31
        $country = wpinv_get_default_country();
32
    }
33
    return trim( wpinv_utf8_strtoupper( $country ) );
0 ignored issues
show
Bug Best Practice introduced by
The expression return trim(wpinv_utf8_strtoupper($country)) returns the type string which is incompatible with the documented return type array.
Loading history...
34
35
}
36
37
function wpinv_is_base_country( $country ) {
38
    $base_country = wpinv_get_default_country();
39
    
40
    if ( $base_country === 'UK' ) {
41
        $base_country = 'GB';
42
    }
43
    if ( $country == 'UK' ) {
44
        $country = 'GB';
45
    }
46
47
    return ( $country && $country === $base_country ) ? true : false;
48
}
49
50
function wpinv_country_name( $country_code = '' ) { 
51
    $countries = wpinv_get_country_list();
52
    $country_code = $country_code == 'UK' ? 'GB' : $country_code;
53
    $country = isset( $countries[$country_code] ) ? $countries[$country_code] : $country_code;
54
55
    return apply_filters( 'wpinv_country_name', $country, $country_code );
56
}
57
58
function wpinv_get_default_state() {
59
	$state = wpinv_get_option( 'default_state', false );
60
61
	return apply_filters( 'wpinv_default_state', $state );
62
}
63
64
function wpinv_state_name( $state_code = '', $country_code = '' ) {
65
    $state = $state_code;
66
    
67
    if ( !empty( $country_code ) ) {
68
        $states = wpinv_get_country_states( $country_code );
69
        
70
        $state = !empty( $states ) && isset( $states[$state_code] ) ? $states[$state_code] : $state;
71
    }
72
73
    return apply_filters( 'wpinv_state_name', $state, $state_code, $country_code );
74
}
75
76
function wpinv_store_address() {
77
    $address = wpinv_get_option( 'store_address', '' );
78
79
    return apply_filters( 'wpinv_store_address', $address );
80
}
81
82
/**
83
 * Returns an array of user address fields
84
 * 
85
 * @return array
86
 */
87
function getpaid_user_address_fields() {
88
    return apply_filters(
89
        'getpaid_user_address_fields',
90
        array(
91
            'first_name',
92
            'last_name',
93
            'company',
94
            'vat_number',
95
            'phone',
96
            'address',
97
            'city',
98
            'state',
99
            'country',
100
            'zip'
101
        )
102
    );
103
}
104
105
/**
106
 * Saves a user address.
107
 *
108
 * This function is called whenever an invoice is created/updated to ensure that the user address is always up to date.
109
 *
110
 * @param WPInv_Invoice $invoice
111
 */
112
function getpaid_save_invoice_user_address( $invoice ) {
113
114
    // Retrieve the invoice.
115
    $invoice = wpinv_get_invoice( $invoice );
116
117
    // Abort if it does not exist.
118
    if ( empty( $invoice ) ) {
119
        return;
120
    }
121
122
    foreach ( getpaid_user_address_fields() as $field ) {
123
124
        if ( is_callable( array( $invoice, "get_$field" ) ) ) {
125
            $value = call_user_func( array( $invoice, "get_$field" ) );
126
127
            // Only save if it is not empty.
128
            if ( ! empty( $value ) ) {
129
                update_user_meta( $invoice->get_user_id(), '_wpinv_' . $field, $value );
130
            }
131
132
        }
133
134
    }
135
136
}
137
add_action( 'getpaid_new_invoice', 'getpaid_save_invoice_user_address' );
138
add_action( 'getpaid_update_invoice', 'getpaid_save_invoice_user_address' );
139
140
/**
141
 * Retrieves a saved user address.
142
 *
143
 * @param int $user_id The user id whose address we should get. Defaults to the current user id.
144
 * @param bool $with_default Whether or not we should use the default country and state.
145
 * @return array
146
 */
147
function wpinv_get_user_address( $user_id = 0, $with_default = true ) {
148
149
    // Prepare the user id.
150
    $user_id   = empty( $user_id ) ? get_current_user_id() : $user_id;
151
    $user_info = get_userdata( $user_id );
152
153
    // Abort if non exists.
154
    if ( empty( $user_info ) ) {
155
        return array();
156
    }
157
158
    // Prepare the address.
159
    $address = array(
160
        'user_id' => $user_id,
161
        'email'   => $user_info->user_email,
162
    );
163
164
    foreach ( getpaid_user_address_fields() as $field ) {
165
        $address[$field] = getpaid_get_user_address_field( $user_id, $field );
166
    }
167
168
    if ( ! $with_default ) {
169
        return $address;
170
    }
171
172
    if ( isset( $address['first_name'] ) && empty( $address['first_name'] ) ) {
173
        $address['first_name'] = $user_info->first_name;
174
    }
175
176
    if ( isset( $address['last_name'] ) && empty( $address['last_name'] ) ) {
177
        $address['last_name'] = $user_info->last_name;
178
    }
179
180
    if ( isset( $address['state'] ) && empty( $address['state'] ) ) {
181
        $address['state'] = wpinv_get_default_state();
182
    }
183
184
    if ( isset( $address['country'] ) && empty( $address['country'] ) ) {
185
        $address['country'] = wpinv_get_default_country();
186
    }
187
188
    return $address;
189
}
190
191
/**
192
 * Retrieves a saved user address field.
193
 *
194
 * @param int $user_id The user id whose address field we should get.
195
 * @param string $field The field to use.
196
 * @return string|null
197
 */
198
function getpaid_get_user_address_field( $user_id, $field ) {
199
200
    $prefixes = array(
201
        '_wpinv_',
202
        'billing_',
203
        ''
204
    );
205
206
    foreach ( $prefixes as $prefix ) {
207
208
        // Meta table.
209
        $value = get_user_meta( $user_id, $prefix . $field, true );
210
        
211
        // UWP table.
212
        $value = ( empty( $value ) && function_exists( 'uwp_get_usermeta' ) ) ? uwp_get_usermeta( $user_id, $prefix . $field ) : $value;
213
214
        if ( ! empty( $value ) ) {
215
            return $value;
216
        }
217
218
    }
219
220
    return null;
221
222
}
223
224
/**
225
 * Get all continents.
226
 * 
227
 * @since 1.0.14
228
 * @param string $return What to return.
229
 * @return array
230
 */
231
function wpinv_get_continents( $return = 'all' ) {
232
233
    $continents = wpinv_get_data( 'continents' );
234
235
    switch( $return ) {
236
        case 'name' :
237
            return wp_list_pluck( $continents, 'name' );
238
            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...
239
        case 'countries' :
240
            return wp_list_pluck( $continents, 'countries' );
241
            break;
242
        default :
243
            return $continents;
244
            break;
245
    }
246
247
}
248
249
/**
250
 * Get continent code for a country code.
251
 * 
252
 * @since 1.0.14
253
 * @param string $country Country code. If no code is specified, defaults to the default country.
254
 * @return string
255
 */
256
function wpinv_get_continent_code_for_country( $country = false ) {
257
258
    $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

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