Last active
December 3, 2022 02:47
-
-
Save webaware/985156c48d8fea3cc77d9a9c624aad18 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: GF Countries for WooCommerce | |
Plugin URI: https://gist.github.com/webaware/985156c48d8fea3cc77d9a9c624aad18 | |
Update URI: gf-countries-woo | |
Description: restrict Gravity Forms addresses to WooCommerce-allowed countries | |
Version: 2 | |
Author: WebAware | |
Author URI: https://shop.webaware.com.au/ | |
*/ | |
/* | |
copyright (c) 2022 WebAware Pty Ltd (email : support@webaware.com.au) | |
This program is free software; you can redistribute it and/or | |
modify it under the terms of the GNU General Public License | |
as published by the Free Software Foundation; either version 2 | |
of the License, or (at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program; if not, write to the Free Software | |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
*/ | |
namespace webaware\gf_countries_woo; | |
use WC_Countries; | |
use function webaware\gf_address_enhanced\get_country_name; | |
if (!defined('ABSPATH')) { | |
exit; | |
} | |
/** | |
* if WooCommerce is active, hook Gravity Forms to match supported countries | |
*/ | |
add_action('plugins_loaded', function() : void { | |
if (function_exists('WC')) { | |
add_filter('gform_countries', __NAMESPACE__ . '\\gf_restrict_countries'); | |
add_filter('gf_address_enhanced_smart_states_countries', __NAMESPACE__ . '\\address_enhanced_restrict_countries'); | |
} | |
}); | |
/** | |
* restrict which countries are available in Address field | |
* @param array $countries | |
* @return array | |
*/ | |
function gf_restrict_countries($countries) { | |
$wc_countries = new WC_Countries(); | |
$woo_countries = $wc_countries->get_allowed_countries(); | |
if ($woo_countries) { | |
// build a list of Gravity Forms country names (which might differ from WooCommerce names) | |
$countries = []; | |
foreach (array_keys($woo_countries) as $code) { | |
$countries[] = get_country_name($code); | |
} | |
} | |
return $countries; | |
} | |
/** | |
* restrict which countries have Smart States data loaded for them | |
*/ | |
function address_enhanced_restrict_countries(array $countries) : array { | |
$wc_countries = new WC_Countries(); | |
$woo_countries = $wc_countries->get_allowed_countries(); | |
if ($woo_countries) { | |
// map Gravity Forms country name to country code | |
$countries = []; | |
foreach (array_keys($woo_countries) as $code) { | |
$countries[get_country_name($code)] = $code; | |
} | |
} | |
return $countries; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment