1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
<?php
use SIW\Data\Country;
function siw_get_countries( string $context = 'all', string $index = 'slug', string $return = 'objects' ) {
$countries = wp_cache_get( "{$context}_{$index}_{$return}", 'siw_countries' );
if ( false !== $countries ) {
return $countries;
}
$continents = siw_get_data_file_ids( 'countries' );
foreach ( $continents as $continent ) {
$continent = str_replace( '_', '-', $continent );
$continent_data[ $continent ] = siw_get_data( "countries/{$continent}" );
}
$data = [];
foreach ( $continent_data as $continent => $countries_data ) {
$countries_data = array_map( function( $country_data ) use ( $continent ) {
$country_data['continent'] = $continent;
return $country_data;
}, $countries_data );
$data = array_merge( $data, $countries_data );
}
$data = wp_list_sort( $data, 'name' );
$data = array_column( $data , null, $index );
$countries = array_map(
function( $item ) {
return new Country( $item );
},
$data
);
$countries = array_filter(
$countries,
function( $country ) use ( $context ) {
return ( 'all' == $context
|| ( 'workcamps' == $context && $country->has_workcamps() )
|| ( 'esc_projects' == $context && $country->has_esc_projects() )
|| ( 'tailor_made_projects' == $context && $country->has_tailor_made_projects() )
);
}
);
if ( 'array' == $return ) {
$countries = array_map(
function( $country ) {
return $country->get_name();
},
$countries
);
}
wp_cache_set( "{$context}_{$index}_{$return}", $countries, 'siw_countries' );
return $countries;
}
function siw_get_country( string $country, string $index = 'slug' ) {
$countries = siw_get_countries( 'all', $index );
return $countries[ $country ] ?? false;
}