Posted on

Import ISO 3166 country codes

/**
 * Import
 */
class Import {
	/**
	 * Construct
	 */
	public function __construct() {
		add_action( 'init', array( $this, 'import_terms' ) );
	}

	/**
	 * Import terms
	 */
	public function import_terms() {
		$import = filter_input( INPUT_GET, 'import', FILTER_SANITIZE_STRING );

		if ( empty( $import ) ) {
			return;
		}

		$csv  = file( get_template_directory() . '/includes/countries.csv' );
		$data = [];

		foreach ( $csv as $line ) {
			$data[] = str_getcsv( $line );
		}

		foreach ( $data as $term ) {
			wp_insert_term(
				$term[1],
				'country',
				array(
					'slug' => $term[2],
				)
			);
		}
	}
}