Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: language filtering (resolves #237) #244

Merged
merged 4 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -116,7 +118,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 @@ -374,10 +376,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 @@ -136,11 +136,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 @@ -150,7 +150,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(__('Showing %1$s of %2$s resources for:', 'coop-library'), $found_posts, App::totalPosts('lc_resource')) }}</p>
<p class="h3">{{ sprintf(__('Showing %1$s of %2$s resources for:', 'coop-library'), $found_posts, App::totalPosts('lc_resource', $current_language)) }}</p>
@if(isset($_GET['s']))
<h2 class="h4">{{ __('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 @@ -69,7 +69,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