Skip to content

Commit cffd3b4

Browse files
authored
Global Search - exclude archived records by default (#2752)
* Add archived filter to advanced search functionality - Introduced a new checkbox for filtering archived posts in the advanced search modal. - Updated JavaScript to include the archived filter in the search parameters. - Modified PHP logic to handle the archived filter in the search endpoint and post retrieval process. - Ensured that the archived status is correctly processed and applied during post filtering. * Refactor whitespace in archived posts filter condition for improved code consistency * Add archived filter to global search unit tests - Updated unit tests to include the 'archived' filter in various search scenarios. - Ensured comprehensive coverage for the archived status in the search functionality. * Remove 'checked' attribute from archived filter checkbox in advanced search modal for improved functionality
1 parent b9e6217 commit cffd3b4

File tree

6 files changed

+29
-0
lines changed

6 files changed

+29
-0
lines changed

dt-assets/js/advanced-search.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ jQuery(document).ready(function ($) {
108108
meta: $('#advanced-search-modal-filters-meta-' + location).prop(
109109
'checked',
110110
),
111+
archived: $('#advanced-search-modal-filters-archived-' + location).prop(
112+
'checked',
113+
),
111114
status: 'all',
112115
};
113116
}

dt-assets/js/shared-functions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ window.API = {
218218
post: filters['post'],
219219
comment: filters['comment'],
220220
meta: filters['meta'],
221+
archived: filters['archived'],
221222
status: filters['status'],
222223
},
223224
'dt-posts/v2/posts/search/',

dt-assets/parts/modals/modal-advanced-search.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ class="advanced-search-modal-filters" checked>
101101
<label
102102
for="advanced-search-modal-filters-meta-<?php echo esc_attr( $location ); ?>"><?php echo esc_html__( 'Communication', 'disciple_tools' ); ?></label>
103103
<br>
104+
105+
<input type="checkbox" id="advanced-search-modal-filters-archived-<?php echo esc_attr( $location ); ?>"
106+
class="advanced-search-modal-filters">
107+
<label
108+
for="advanced-search-modal-filters-archived-<?php echo esc_attr( $location ); ?>"><?php echo esc_html__( 'Archived', 'disciple_tools' ); ?></label>
109+
<br>
104110
<?php
105111
}
106112
?>

dt-posts/dt-posts-endpoints.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,12 +801,14 @@ public function advanced_search( WP_REST_Request $request ): array {
801801
$post = ( strtolower( $request->get_param( 'post' ) ) === 'true' );
802802
$comment = ( strtolower( $request->get_param( 'comment' ) ) === 'true' );
803803
$meta = ( strtolower( $request->get_param( 'meta' ) ) === 'true' );
804+
$archived = ( strtolower( $request->get_param( 'archived' ) ) === 'true' );
804805
$status = $request->get_param( 'status' );
805806

806807
return DT_Posts::advanced_search( $query, $post_type, $offset, [
807808
'post' => $post,
808809
'comment' => $comment,
809810
'meta' => $meta,
811+
'archived' => $archived,
810812
'status' => $status
811813
] );
812814
}

dt-posts/dt-posts.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2916,7 +2916,9 @@ private static function advanced_search_by_post( string $post_type, array $query
29162916

29172917
// Ensure status filter is captured accordingly
29182918
$post_settings = self::get_post_settings( $post_type, false );
2919+
$status_archived_key = '';
29192920
if ( ! empty( $filters['status'] ) && ! empty( $post_settings['status_field'] ) ) {
2921+
$status_archived_key = $post_settings['status_field']['archived_key'];
29202922
$status_where_condition = ( $filters['status'] === 'all' ) ? 'IN (' . dt_array_to_sql( self::get_post_field_options_keys( $post_settings['fields'], $post_settings['status_field']['status_key'] ) ) . ')' : "= '" . $filters['status'] . "'";
29212923
$extra_fields .= 'if(adv_search_post_status.meta_value ' . $status_where_condition . ", 'Y', 'N') status_hit,";
29222924
$extra_fields .= 'if(adv_search_post_status.meta_value ' . $status_where_condition . ", adv_search_post_status.meta_value, '') status_hit_value,";
@@ -2981,6 +2983,11 @@ private static function advanced_search_by_post( string $post_type, array $query
29812983
$add_post = true;
29822984
}
29832985

2986+
// Accordingly filter posts, based on archived flag.
2987+
if ( !$filters['archived'] && isset( $post->status_hit_value ) && $post->status_hit_value === $status_archived_key ) {
2988+
$add_post = false;
2989+
}
2990+
29842991
// Add post accordingly, based on flag!
29852992
if ( $add_post ) {
29862993
$post_hits[] = $post;

tests/dt-posts/dt-posts/unit-test-global-search.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public function provide_global_search_query_data(): array{
7676
'post' => true,
7777
'comment' => true,
7878
'meta' => true,
79+
'archived' => true,
7980
'status' => 'all'
8081
]
8182
],
@@ -92,6 +93,7 @@ public function provide_global_search_query_data(): array{
9293
'post' => true,
9394
'comment' => true,
9495
'meta' => true,
96+
'archived' => true,
9597
'status' => 'all'
9698
]
9799
],
@@ -108,6 +110,7 @@ public function provide_global_search_query_data(): array{
108110
'post' => true,
109111
'comment' => true,
110112
'meta' => true,
113+
'archived' => true,
111114
'status' => 'all'
112115
]
113116
],
@@ -124,6 +127,7 @@ public function provide_global_search_query_data(): array{
124127
'post' => true,
125128
'comment' => true,
126129
'meta' => true,
130+
'archived' => true,
127131
'status' => 'all'
128132
]
129133
],
@@ -140,6 +144,7 @@ public function provide_global_search_query_data(): array{
140144
'post' => true,
141145
'comment' => true,
142146
'meta' => true,
147+
'archived' => true,
143148
'status' => 'all'
144149
]
145150
],
@@ -156,6 +161,7 @@ public function provide_global_search_query_data(): array{
156161
'post' => true,
157162
'comment' => true,
158163
'meta' => true,
164+
'archived' => true,
159165
'status' => 'all'
160166
]
161167
],
@@ -172,6 +178,7 @@ public function provide_global_search_query_data(): array{
172178
'post' => true,
173179
'comment' => true,
174180
'meta' => true,
181+
'archived' => true,
175182
'status' => 'all'
176183
]
177184
],
@@ -188,6 +195,7 @@ public function provide_global_search_query_data(): array{
188195
'post' => true,
189196
'comment' => true,
190197
'meta' => false,
198+
'archived' => true,
191199
'status' => 'all'
192200
]
193201
],
@@ -204,6 +212,7 @@ public function provide_global_search_query_data(): array{
204212
'post' => true,
205213
'comment' => true,
206214
'meta' => true,
215+
'archived' => true,
207216
'status' => 'all'
208217
]
209218
],
@@ -220,6 +229,7 @@ public function provide_global_search_query_data(): array{
220229
'post' => true,
221230
'comment' => true,
222231
'meta' => true,
232+
'archived' => true,
223233
'status' => 'all'
224234
]
225235
],

0 commit comments

Comments
 (0)