get_id() ); } return ''; } /** * @param int $post_id A product (or, for an attachment subject, its * parent product) post id. * @return string */ private static function languageForPost( int $post_id ): string { if ( function_exists( 'pll_get_post_language' ) ) { return self::fromPolylang( static function ( string $field ) use ( $post_id ) { return pll_get_post_language( $post_id, $field ); } ); } if ( defined( 'ICL_SITEPRESS_VERSION' ) ) { return self::fromWpmlDetails( apply_filters( 'wpml_post_language_details', null, $post_id ) ); } return ''; } /** * A product's own WPML/Polylang language code exactly as the plugin * records it (Polylang's slug, WPML's `language_code`), lowercased. * Unlike {@see languageForPost()}, which maps a language to the prompt * names AI providers are asked to write in, two regional variants * (pt_BR and pt_PT) stay different here, and a language with no prompt * name still has a code, so a section that sets other products next to * this one can keep them to its language. * * @param int $post_id Product id. * @return string The code, or '' when neither plugin records one. */ public static function postLanguageCode( int $post_id ): string { if ( function_exists( 'pll_get_post_language' ) ) { return strtolower( (string) pll_get_post_language( $post_id, 'slug' ) ); } if ( defined( 'ICL_SITEPRESS_VERSION' ) ) { return strtolower( self::wpmlField( apply_filters( 'wpml_post_language_details', null, $post_id ), 'language_code' ) ); } return ''; } /** * @param \WP_Term $term Taxonomy term subject. * @return string */ private static function termLanguage( \WP_Term $term ): string { $term_id = (int) $term->term_id; if ( function_exists( 'pll_get_term_language' ) ) { return self::fromPolylang( static function ( string $field ) use ( $term_id ) { return pll_get_term_language( $term_id, $field ); } ); } if ( defined( 'ICL_SITEPRESS_VERSION' ) ) { $details = apply_filters( 'wpml_element_language_details', null, array( 'element_id' => (int) ( $term->term_taxonomy_id ?? 0 ), 'element_type' => 'tax_' . (string) ( $term->taxonomy ?? '' ), ) ); return self::fromWpmlDetails( $details ); } return ''; } /** * Polylang's locale field ('en_US') disambiguates the one regional * variant {@see LanguageNames} needs (English vs English (UK)); its * bare language-slug field ('en') is tried next when the locale itself * did not map to anything, so a site using only short slugs still * resolves. * * @param callable $read fn(string $field): mixed — pll_get_post_language()/pll_get_term_language() bound to one subject. * @return string */ private static function fromPolylang( callable $read ): string { $by_locale = LanguageNames::forCode( (string) $read( 'locale' ) ); if ( '' !== $by_locale ) { return $by_locale; } return LanguageNames::forCode( (string) $read( 'slug' ) ); } /** * WPML's `locale` field ('en_GB') disambiguates the one regional variant * {@see LanguageNames} needs (English vs English (UK)), the same * precedence {@see fromPolylang()} already uses; its bare * `language_code` field ('en') is tried next when the locale itself did * not map to anything, so a site using only bare codes still resolves. * * @param mixed $details `wpml_post_language_details`/ * `wpml_element_language_details` filter result * (documented as an array on every WPML version; * read defensively as an object too, since no WPML * install is available locally to confirm this * against a real site). * @return string */ private static function fromWpmlDetails( $details ): string { $name = LanguageNames::forCode( self::wpmlField( $details, 'locale' ) ); if ( '' !== $name ) { return $name; } return LanguageNames::forCode( self::wpmlField( $details, 'language_code' ) ); } /** * @param mixed $details WPML filter result. * @param string $key Field name. * @return string */ private static function wpmlField( $details, string $key ): string { if ( is_array( $details ) ) { return (string) ( $details[ $key ] ?? '' ); } if ( is_object( $details ) ) { return (string) ( $details->$key ?? '' ); } return ''; } /** * The one system-prompt instruction line every Blueprint prompt builder * uses to ask the model for a non-default language, extracted here so * every generator (Description/Taxonomy through PromptCompiler, and * Title, SEO meta, FAQ and Image alt through their own Formula prompt * builders) states it identically and the wording can never drift * between them. * * @param string $language A {@see self::resolve()}'d language (or any * string a caller already holds). * @return string The instruction line, or '' when $language is the * {@see self::STORE} sentinel or empty — the store's * default language needs no explicit instruction. * * Note: this line is global to the whole system prompt and is never * special-cased per unit. A unit that must otherwise stick to literal * source text (an isolated unit whose rules permit only "a faithful * translation of the given excerpts' meaning into the required * language", say) names its own bounded exception in its own * `instruction` (its extension's output rules) rather than this * line being reworded for one unit type. */ public static function instructionLine( string $language ): string { if ( '' === $language || self::isStoreSentinel( $language ) ) { return ''; } return 'Write every string in language code: ' . $language . '.'; } }