diff --git a/content/themes/custom/functions/class-apitranslation.php b/content/themes/custom/functions/class-apitranslation.php index 73b6a0b..0b34700 100644 --- a/content/themes/custom/functions/class-apitranslation.php +++ b/content/themes/custom/functions/class-apitranslation.php @@ -40,7 +40,7 @@ public static function get_translations() { // Get translations from the api and transform them into an easily searchable format $kaannos_json = get_field( 'kaannos-json', 'option' ); $translations = \POF\Api::get( $kaannos_json, true ); - $locale = substr( get_locale(), 0, 2 ); + $locale = get_short_locale(); foreach ( $translations as &$group ) { foreach ( $group as $lang ) { if ( $lang['lang'] === $locale ) { diff --git a/content/themes/custom/functions/function-api_data_helpers.php b/content/themes/custom/functions/function-api_data_helpers.php index 028d698..093de48 100644 --- a/content/themes/custom/functions/function-api_data_helpers.php +++ b/content/themes/custom/functions/function-api_data_helpers.php @@ -322,12 +322,14 @@ function sort_by_order( $a, $b ) { /** * Get age groups from the api * + * @param bool $filter Whether we should filter the results to the current language. * @return array */ -function get_age_groups() { +function get_age_groups( bool $filter = false ) : array { $ohjelma_json = get_field( 'ohjelma-json', 'option' ); - $program = \POF\Api::get( $ohjelma_json, true ); - $age_groups = $program['program'][0]['agegroups']; + $program = \POF\Api::get( $ohjelma_json, true )['program']; + $program = $filter ? array_reduce( $program, 'filter_api_items', [] ) : $program; + $age_groups = $program[0]['agegroups']; usort( $age_groups, 'sort_by_order' ); sort_results( $age_groups ); @@ -335,6 +337,48 @@ function get_age_groups() { return $age_groups; } +/** + * Recursively filter api items and its child items to current language + * Should only be called via array_reduce + * + * @param array $items Final result. + * @param array $item Api item to filter. + * @return array Modified $items. + */ +function filter_api_items( array $items, array $item ) : array { + if ( array_key_exists( 'languages', $item ) ) { + $lang_exists = array_reduce( $item['languages'], 'languages_has_current', false ); + + if ( $lang_exists ) { + $keys_to_traverse = [ + 'agegroups', + 'taskgroups', + 'tasks', + ]; + foreach ( $keys_to_traverse as $key ) { + if ( ! empty( $item[ $key ] ) ) { + $item[ $key ] = array_reduce( $item[ $key ], 'filter_api_items', [] ); + } + } + $items[] = $item; + } + } + + return $items; +} + +/** + * Check api items languages if it contains current language + * Should only be called via array_reduce + * + * @param bool $result True or false depending on if current language exists. + * @param array $lang Language to check. + * @return bool Whether language exists. + */ +function languages_has_current( bool $result, array $lang ) { + return $result ? $result : $lang['lang'] === get_short_locale(); +} + /** * Flatten api program tree into a single array * diff --git a/content/themes/custom/functions/function-lang_helpers.php b/content/themes/custom/functions/function-lang_helpers.php index 6495d3d..81b2502 100644 --- a/content/themes/custom/functions/function-lang_helpers.php +++ b/content/themes/custom/functions/function-lang_helpers.php @@ -74,3 +74,13 @@ function generate_search_url( $search = '', $lang = null ) { return $url; } + +/** + * Get the short version of the locale + * + * @return string + */ +function get_short_locale() : string { + $locale = reset( str_word_count( get_locale(), 1 ) ); + return $locale; +} diff --git a/content/themes/custom/functions/function-program_lang_helper.php b/content/themes/custom/functions/function-program_lang_helper.php index 995b0b7..dd1254f 100644 --- a/content/themes/custom/functions/function-program_lang_helper.php +++ b/content/themes/custom/functions/function-program_lang_helper.php @@ -16,7 +16,7 @@ class ApiTitle extends Helper { * @return string */ public function output() { - $locale = $this->params->locale ?? $this->context->get( 'locale' ) ?? explode( '_', get_locale() )[0]; + $locale = $this->params->locale ?? $this->context->get( 'locale' ) ?? get_short_locale(); $title = $this->params->title ?? $this->context->get( 'title' ); $languages = $this->params->languages ?? $this->context->get( 'languages' ); diff --git a/content/themes/custom/models/search.php b/content/themes/custom/models/search.php index 7190a81..929ad22 100644 --- a/content/themes/custom/models/search.php +++ b/content/themes/custom/models/search.php @@ -39,16 +39,6 @@ public function Content() { return true; } - /** - * Get short locale - * - * @return string First part of locale. - */ - public static function get_locale() { - $locale = explode( '_', get_locale() )[0]; - return $locale; - } - /** * Get search terms and translations from api */ @@ -57,7 +47,7 @@ public static function ApiSearchTerms() { // Get remote data $haku_json = get_field( 'haku-json', 'option' ); $search_terms = \POF\Api::get( $haku_json, true ); - $age_groups = get_age_groups(); + $age_groups = get_age_groups( true ); // Get age groups filtered to current language // Create pseudo filtering field for api_type $search_terms['api_type'] = [ @@ -366,7 +356,7 @@ protected function get_post_data( stdClass $result, stdClass $params ) { 'guid' => $post->fields['api_guid'], 'languages' => [ [ - 'lang' => static::get_locale(), + 'lang' => get_short_locale(), 'title' => $parent_post->post_title, ], ],