Completed
Push — master ( 260ce8...60cf75 )
by Brian
21s queued 16s
created

getpaid_maybe_add_default_address()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 18
rs 9.6111
cc 5
nc 5
nop 1
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
 * (Maybe) adds the default address to an invoice.
84
 *
85
 * @param WPInv_Invoice $invoice
86
 */
87
function getpaid_maybe_add_default_address( &$invoice ) {
88
89
    $user_id = $invoice->get_user_id();
90
91
    // Abort if the invoice belongs to no one.
92
    if ( empty( $user_id ) ) {
93
        return;
94
    }
95
96
    // Fill in defaults whenever necessary.
97
    foreach ( wpinv_get_user_address( $user_id ) as $key => $value ) {
98
99
        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

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

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