Skip to content

Commit

Permalink
fix: language filtering (resolves #237) (#244)
Browse files Browse the repository at this point in the history
* fix: language filtering (resolves #237)
* fix: only show current language on front page
  • Loading branch information
Ned Zimmerman authored Mar 31, 2020
1 parent ca12937 commit cf2b939
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 18 deletions.
26 changes: 17 additions & 9 deletions app/Controllers/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function queriedResourceTerms()
{
$langs = get_language_list(pll_current_language('locale'));
$terms = [
'language' => [],
'resource_language' => [],
'lc_format' => [],
'lc_goal' => [],
'lc_region' => [],
Expand All @@ -55,14 +55,16 @@ public function queriedResourceTerms()
global $wp_query;
if ($wp_query->tax_query) {
foreach ($wp_query->tax_query->queries as $value) {
foreach ($value['terms'] as $t) {
$terms[$value['taxonomy']][$t] = get_term_by('slug', $t, $value['taxonomy'])->name;
if ($value['taxonomy'] !== 'language') {
foreach ($value['terms'] as $t) {
$terms[$value['taxonomy']][$t] = get_term_by('slug', $t, $value['taxonomy'])->name;
}
}
}
}
if (isset($_GET['language'])) {
foreach ($_GET['language'] as $lang) {
$terms['language'][ $lang ] = $langs[$lang];
if (isset($_GET['resource_language'])) {
foreach ($_GET['resource_language'] as $lang) {
$terms['resource_language'][ $lang ] = $langs[$lang];
}
}
return $terms;
Expand Down Expand Up @@ -135,7 +137,7 @@ public function currentLanguage()
return 'en';
}

public function foundPosts()
public function foundPosts($lang = 'en')
{
global $wp_query;
return $wp_query->found_posts;
Expand Down Expand Up @@ -393,10 +395,16 @@ public static function getPagination($args = [])
return $navigation;
}

public static function totalPosts($post_type = null)
public static function totalPosts($post_type = null, $lang = 'en')
{
if ($post_type) {
return wp_count_posts($post_type)->publish;
$q = new \WP_Query([
'post_type' => $post_type,
'post_status' => 'publish',
'lang' => $lang,
'fields' => 'ids'
]);
return $q->found_posts;
}

return 0;
Expand Down
3 changes: 0 additions & 3 deletions app/Controllers/FrontPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public function mostViewed()
'post__not_in' => $viewed_ids,
'posts_per_page' => 4,
'fields' => 'ids',
'lang' => '',
]);
$ids = array_merge($viewed_ids + $unviewed_ids);

Expand All @@ -36,7 +35,6 @@ public function mostViewed()
'post__in' => $ids,
'orderby' => 'post__in',
'posts_per_page' => 4,
'lang' => '',
]);
}

Expand All @@ -48,7 +46,6 @@ public function recentlyPublished()
'orderby' => ['meta_value', 'date'],
'posts_per_page' => 4,
'order' => 'desc',
'lang' => '',
]);
}
}
5 changes: 2 additions & 3 deletions app/filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@
break;
}
}
if (isset($_GET['language'])) {
if (isset($_GET['resource_language'])) {
$meta_query = [
'relation' => 'OR'
];
foreach ($_GET['language'] as $lang) {
foreach ($_GET['resource_language'] as $lang) {
$meta_query[] = [
'key' => 'language',
'value' => $lang,
Expand All @@ -149,7 +149,6 @@
}
$query->set('posts_per_page', 12);
$query->set('order', 'desc');
$query->set('lang', '');
}
}
});
Expand Down
4 changes: 2 additions & 2 deletions resources/views/partials/current-filters.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@if(isset($_GET['s']) || $found_posts < App::totalPosts('lc_resource'))
@if(isset($_GET['s']) || $found_posts < App::totalPosts('lc_resource', $current_language))
<div class="current-filters">
<p class="h3">{{ sprintf(__('%1$s of %2$s resources matched', 'coop-library'), $found_posts, App::totalPosts('lc_resource')) }}</p>
<p class="h3">{{ sprintf(__('%1$s of %2$s resources matched', 'coop-library'), $found_posts, App::totalPosts('lc_resource', $current_language)) }}</p>
@if(isset($_GET['s']))
<h2 class="h4">{{ __('Your search term:', 'coop-library') }}</h2>
<p>&ldquo;{{ $_GET['s'] }}&rdquo;</p>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/partials/filters.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
<ul id="language" class="input-group input-group__parent language">
@foreach(App::getMetaValues('language', 'lc_resource') as $language)
<li>
<input id="language-{{ $language }}" name="language[]" type="checkbox" value="{{ $language }}" {{ checked(in_array($language, array_keys($queried_resource_terms['language']))) }} />
<input id="language-{{ $language }}" name="resource_language[]" type="checkbox" value="{{ $language }}" {{ checked(in_array($language, array_keys($queried_resource_terms['resource_language']))) }} />
<label for="language-{{ $language }}">{!! $languages[$language] !!}</label>
</li>
@endforeach
Expand Down

0 comments on commit cf2b939

Please sign in to comment.