code => language */ function blocklingo_languages(): array { return Failsafe::call( static fn(): array => array_map( static fn( \AddBlack\Lang\Language\Language $lang ): array => $lang->to_array(), Plugin::instance()->settings()->get()->languages() ), array(), __FUNCTION__ ); } /** Code of the default language (no URL prefix). */ function blocklingo_default_lang(): string { return Failsafe::call( static fn(): string => Plugin::instance()->settings()->get()->default_code(), '', __FUNCTION__ ); } /** Language of the current request. */ function blocklingo_current_lang(): string { return Failsafe::call( static fn(): string => Plugin::instance()->current_language()->get(), '', __FUNCTION__ ); } function blocklingo_is_language( mixed $code ): bool { return Failsafe::call( static fn(): bool => Plugin::instance()->settings()->get()->is_language( $code ), false, __FUNCTION__ ); } function blocklingo_is_translatable_type( mixed $post_type ): bool { return Failsafe::call( static fn(): bool => Plugin::instance()->settings()->get()->is_translatable_type( (string) $post_type ), false, __FUNCTION__ ); } /** Language code of a post (default language when untagged). */ function blocklingo_get_post_lang( mixed $post_id ): string { return Failsafe::call( static fn(): string => Plugin::instance()->posts()->language_of( (int) $post_id ), '', __FUNCTION__ ); } function blocklingo_set_post_lang( mixed $post_id, mixed $lang ): bool|WP_Error { if ( ! blocklingo_is_language( $lang ) ) { return new WP_Error( 'blocklingo_bad_lang', __( 'Unknown language.', 'blocklingo' ) ); } return Failsafe::call( static fn(): bool|WP_Error => Plugin::instance()->translations()->assign( (int) $post_id, (string) $lang ), false, __FUNCTION__ ); } /** @return array lang => post ID, the post itself included. */ function blocklingo_get_post_translations( mixed $post_id ): array { return Failsafe::call( static fn(): array => Plugin::instance()->posts()->group( (int) $post_id ), array(), __FUNCTION__ ); } /** Post ID of the translation in $lang, or 0. */ function blocklingo_get_post_translation( mixed $post_id, mixed $lang ): int { return Failsafe::call( static fn(): int => Plugin::instance()->posts()->in_language( (int) $post_id, (string) $lang ), 0, __FUNCTION__ ); } /** * Language switcher for classic themes and PHP templates. * * @param array $args display, hideCurrent, hideMissing (as in the block). */ function blocklingo_language_switcher( array $args = array() ): void { $html = Failsafe::call( static fn(): string => Plugin::instance()->switcher()->render( $args ), '', __FUNCTION__ ); $allowed = wp_kses_allowed_html( 'post' ); $allowed['a'] = array_merge( $allowed['a'] ?? array(), array( 'hreflang' => true ) ); // wp_kses_post() drops hreflang. echo wp_kses( $html, $allowed ); }