Files
wp-graphql-woocommerce/includes/utils/class-label.php
T
Geoff TaylorandGitHub f63dbb75e8 Improve i18n compatibility for WPML, Polylang, and non-latin characters (#994)
* fix: improve i18n compatibility for WPML, Polylang, and non-latin character support

- Add Label::get_safe_enum_name() utility with optional transliteration
  for non-latin tax class/attribute/taxonomy names (#637, #409)
- Add "Transliterate non-latin characters" admin setting
- Replace get_page_by_path() with WP_Query for product slug resolution
  so WPML/Polylang can hook into the standard query pipeline (#403, #368)
- Split product connections: `products` (toType: Product) and
  `productsWithVariations` (toType: ProductUnion) so i18n plugins can
  register language where args on the standard type name (#811, #952)
- Add ProductTypesWithVariationsEnum for the ProductUnion connection
- Add i18n compatibility tests

* chore: Linter compliances met
2026-03-24 20:42:15 -04:00

50 lines
1.2 KiB
PHP

<?php
/**
* Label utility functions for GraphQL name formatting.
*
* @package WPGraphQL\WooCommerce\Utils
* @since TBD
*/
namespace WPGraphQL\WooCommerce\Utils;
use WPGraphQL\Type\WPEnumType;
/**
* Class Label
*/
class Label {
/**
* Returns a safe GraphQL enum name for the given value, optionally
* transliterating non-latin characters when the setting is enabled.
*
* Falls back to WPEnumType::get_safe_name() and returns null when
* the result contains no alphanumeric characters (i.e. is meaningless).
*
* @param string $value The raw value to convert to a safe enum name.
*
* @return string|null The safe name, or null if the value cannot produce a valid GraphQL name.
*/
public static function get_safe_enum_name( string $value ): ?string {
if (
'on' === woographql_setting( 'enable_transliteration', 'off' )
&& function_exists( 'transliterator_transliterate' )
&& preg_match( '/[^\x20-\x7E]/', $value )
) {
$value = transliterator_transliterate( 'Any-Latin; Latin-ASCII', $value );
}
if ( empty( $value ) ) {
return null;
}
$safe_name = WPEnumType::get_safe_name( $value );
if ( ! preg_match( '/[A-Za-z0-9]/', $safe_name ) ) {
return null;
}
return $safe_name;
}
}