Skip to content

Commit

Permalink
PLANET-6652: Add p4_action type to search
Browse files Browse the repository at this point in the history
Add p4_action type to search filters and template
  • Loading branch information
lithrel committed May 13, 2022
1 parent 2c0fe60 commit 8d989da
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/ElasticSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function set_filters_args( &$args ) {
case 'cat':
case 'tag':
case 'ptype':
case 'atype':
break;
case 'ctype':
switch ( $filter['id'] ) {
Expand Down Expand Up @@ -72,6 +73,7 @@ function ( $formatted_args ) use ( $args ) {
);
break;
case 5:
case 6:
break;
default:
throw new UnexpectedValueException( 'Unexpected content type!' );
Expand Down Expand Up @@ -237,6 +239,11 @@ public function add_aggregations( $formatted_args ) {
'field' => 'terms.p4-page-type.term_id',
],
],
ActionPage::TAXONOMY => [
'terms' => [
'field' => 'terms.' . ActionPage::TAXONOMY . '.term_id',
],
],
],
],
];
Expand Down
37 changes: 35 additions & 2 deletions src/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use UnexpectedValueException;
use WP_Query;
use WPML_Post_Element;
use P4\MasterTheme\Features\ActionPostType;


/**
Expand Down Expand Up @@ -613,6 +614,15 @@ public function set_filters_args( &$args ) {
'terms' => $filter['id'],
];
break;
case 'atype':
// This taxonomy is used only for Posts.
$args['post_type'] = ActionPage::POST_TYPE;
$args['tax_query'][] = [
'taxonomy' => ActionPage::TAXONOMY,
'field' => 'term_id',
'terms' => $filter['id'],
];
break;
case 'ctype':
switch ( $filter['id'] ) {
case 0:
Expand Down Expand Up @@ -643,6 +653,10 @@ public function set_filters_args( &$args ) {
case 5:
$args['post_type'] = 'archive';
break;
case 6:
$args['post_type'] = ActionPage::POST_TYPE;
$args['post_status'] = 'publish';
break;
default:
throw new UnexpectedValueException( 'Unexpected content type!' );
}
Expand Down Expand Up @@ -734,9 +748,15 @@ protected function set_filters_context( &$context ) {
// Post Types.
$context['post_types'] = Search\Filters\PostTypes::get_filters();

// Action Types.
$context['action_types'] = ActionPostType::is_active()
? Search\Filters\ActionTypes::get_filters()
: [];

// Content Types.
$context['content_types'] = Search\Filters\ContentTypes::get_filters(
self::should_include_archive()
self::should_include_archive(),
ActionPostType::is_active()
);

// Tag <-> Campaign.
Expand All @@ -760,6 +780,9 @@ protected function set_filters_context( &$context ) {
case 'ctype':
$context['content_types'][ $filter['id'] ]['checked'] = 'checked';
break;
case 'atype':
$context['action_types'][ $filter['id'] ]['checked'] = 'checked';
break;
default:
throw new UnexpectedValueException( 'Unexpected filter!' );
}
Expand Down Expand Up @@ -799,7 +822,7 @@ protected function set_results_context( &$context ) {

foreach ( $aggs['post_type']['buckets'] as $post_type_agg ) {
if ( 'page' === $post_type_agg['key'] ) {
// We show act pages as a separate item, so subtract there count from the other pages.
// We show act pages as a separate item, so subtract their count from the other pages.
// But counts can be off in ES so don't use lower than 0.
$context['content_types']['2']['results'] = max(
0,
Expand All @@ -818,6 +841,9 @@ protected function set_results_context( &$context ) {
if ( 'archive' === $post_type_agg['key'] && self::should_include_archive() ) {
$context['content_types']['5']['results'] = $post_type_agg['doc_count'];
}
if ( Search\Filters\ContentTypes::ACTION === $post_type_agg['key'] ) {
$context['content_types']['6']['results'] = $post_type_agg['doc_count'];
}
}

foreach ( $aggs['p4-page-type']['buckets'] as $p4_post_type_agg ) {
Expand All @@ -827,6 +853,13 @@ protected function set_results_context( &$context ) {
$context['post_types'][ $p4_post_type_agg['key'] ]['results'] = $p4_post_type_agg['doc_count'];
}

foreach ( $aggs[ ActionPage::TAXONOMY ]['buckets'] as $p4_action_type_agg ) {
if ( ! isset( $context['action_types'][ $p4_action_type_agg['key'] ] ) ) {
continue;
}
$context['action_types'][ $p4_action_type_agg['key'] ]['results'] = $p4_action_type_agg['doc_count'];
}

foreach ( $aggs['categories']['buckets'] as $category_agg ) {
if ( ! isset( $context['categories'][ $category_agg['key'] ] ) ) {
continue;
Expand Down
44 changes: 44 additions & 0 deletions src/Search/Filters/ActionTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace P4\MasterTheme\Search\Filters;

use P4\MasterTheme\ActionPage;

/**
* Action types used for search (Petition, Event, etc.).
*/
class ActionTypes {
/**
* Get all content types.
*
* @return array<WP_Post_Type>
*/
public static function get_all(): array {
return get_terms(
[
'taxonomy' => ActionPage::TAXONOMY,
'hide_empty' => false,
]
);
}

/**
* @return array{int, array{term_id: int, name: string, results: int}}
*/
public static function get_filters(): array {
$types = self::get_all();
$filters = [];

foreach ( $types as $type ) {
$filters[ $type->term_id ] = [
'term_id' => $type->term_id,
'name' => $type->name,
'results' => 0,
];
}

return $filters;
}
}
12 changes: 11 additions & 1 deletion src/Search/Filters/ContentTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ContentTypes {
public const ACT_PAGE = 'action';
public const POST = 'post';
public const PAGE = 'page';
public const ACTION = 'p4_action';
public const CAMPAIGN = 'campaign';
public const ATTACHMENT = 'attachment';
public const ARCHIVE = 'archive';
Expand Down Expand Up @@ -46,7 +47,8 @@ function ( $type ) use ( $config ) {
* @return array{string, array{id: int, name: string, results: int}}
*/
public static function get_filters(
bool $include_archive = false
bool $include_archive = false,
bool $include_action = false
): array {
$types = self::get_all();
$config = self::get_config();
Expand All @@ -65,6 +67,10 @@ public static function get_filters(
continue;
}

if ( self::ACTION === $name && ! $include_action ) {
continue;
}

$type_data = $config[ $type->name ] ?? null;
if ( ! $type_data ) {
continue;
Expand Down Expand Up @@ -111,6 +117,10 @@ public static function get_config(): array {
'id' => 5,
'label' => __( 'Archive', 'planet4-master-theme' ),
],
self::ACTION => [
'id' => 6,
'label' => __( 'Action', 'planet4-master-theme' ),
],
];
}
}
29 changes: 29 additions & 0 deletions templates/search.twig
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,35 @@
</div>
</div>
{% endif %}
{% if ( action_types|length > 0 ) %}
<div class="filteritem">
<a data-bs-toggle="collapse" href="#item-content" class="{{ collapsed }}" aria-expanded="{{ expanded }}">
{{ __( 'Action Type', 'planet4-master-theme' ) }} <span></span>
</a>
<div id="item-content" class="collapse {{ show }}" role="tabpanel">
<ul class="list-unstyled">
{% for id, action_type in action_types %}
{% if ( action_type.results > 0 ) %}
<li>
<label class="custom-control">
<input
type="checkbox"
name="f[atype][{{ action_type.name }}]"
value="{{ id }}"
class="p4-custom-control-input"
data-ga-category="Search Page"
data-ga-action="Content Filter"
data-ga-label="{{ action_type.name|e('wp_kses_post')|raw }}"
{{ action_type.checked }} {{ action_type.results > 0 ? '' : 'disabled' }} />
<span class="custom-control-description">{{ action_type.name }} ({{ action_type.results }})</span>
</label>
</li>
{% endif %}
{% endfor %}
</ul>
</div>
</div>
{% endif %}
{% if ( content_types|length > 0 ) %}
<div class="filteritem">
<a data-bs-toggle="collapse" href="#item-content" class="{{ collapsed }}" aria-expanded="{{ expanded }}">
Expand Down

0 comments on commit 8d989da

Please sign in to comment.