array( 'label' => 'English', 'badge' => 'EN', ), 'ja' => array( 'label' => '日本語', 'badge' => 'JA', ), ); } public static function detect_and_switch_locale() { if ( is_admin() ) { return; } $post_type = get_post_type(); if ( ! in_array( $post_type, array( 'slms_course', 'slms_lesson', 'slms_quiz' ), true ) ) { return; } $post_id = get_the_ID(); $course_id = Access_Control::get_course_id_from_content( $post_id, $post_type ); if ( ! $course_id ) { return; } $language = self::get_course_language( $course_id ); self::$current_language = $language; // The course's language pack from translate.wordpress.org, when the site has it installed; // otherwise the page stays in the site's language. $mo_file = WP_LANG_DIR . '/plugins/accessnow-lms-' . $language . '.mo'; if ( self::DEFAULT_LANGUAGE !== $language && ( is_readable( $mo_file ) || is_readable( substr( $mo_file, 0, -3 ) . '.l10n.php' ) ) ) { unload_textdomain( 'accessnow-lms', true ); if ( load_textdomain( 'accessnow-lms', $mo_file ) ) { self::$switched = true; } } } public static function restore_locale() { if ( self::$switched ) { // Reloadable, so the site language's translations load again just in time. unload_textdomain( 'accessnow-lms', true ); self::$switched = false; } } public static function filter_courses_by_language( $query ) { if ( is_admin() || ! $query->is_main_query() ) { return; } if ( ! $query->is_post_type_archive( 'slms_course' ) ) { return; } $lang = self::get_requested_language(); if ( $lang ) { $existing = $query->get( 'meta_query' ) ?: array(); $existing[] = array( 'key' => self::META_KEY, 'value' => $lang, ); $query->set( 'meta_query', $existing ); } } public static function get_requested_language() { // Read-only catalogue filter from a public link: no nonce needed. if ( ! isset( $_GET['lang'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended return ''; } $lang = sanitize_text_field( wp_unslash( $_GET['lang'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended return isset( self::get_supported_languages()[ $lang ] ) ? $lang : ''; } /** * The languages the site's published courses are in, in the supported-languages order. The * catalogue's language filter only appears when there are two or more. * * @return string[] Language codes. */ public static function languages_in_use() { $used = array(); foreach ( get_posts( array( 'post_type' => 'slms_course', 'post_status' => 'publish', 'posts_per_page' => -1, 'fields' => 'ids' ) ) as $course_id ) { $used[ self::get_course_language( $course_id ) ] = true; } $codes = array_values( array_filter( array_keys( self::get_supported_languages() ), function ( $code ) use ( $used ) { return isset( $used[ $code ] ); } ) ); /** * The languages offered by the catalogue's language filter (shown when there are two or more). * * @param string[] $codes Language codes in use by published courses. */ return (array) apply_filters( 'accessnow_slms_catalogue_languages', $codes ); } public static function get_course_language( $course_id ) { return Course_Meta::get( $course_id, self::META_KEY, self::DEFAULT_LANGUAGE ); } public static function get_current_language() { return self::$current_language ?: self::DEFAULT_LANGUAGE; } }