Pledge list: Add sort filter

This commit is contained in:
Kelly Dwan 2024-08-29 17:49:00 -04:00
parent eac1ed9006
commit 8df86334e3
No known key found for this signature in database
GPG key ID: 8BA5575F3D11575D
2 changed files with 72 additions and 1 deletions

View file

@ -9,6 +9,9 @@ namespace WordPressDotOrg\Theme\FiveForTheFuture_2024\Block_Config;
* Actions and filters. * Actions and filters.
*/ */
add_filter( 'wporg_block_navigation_menus', __NAMESPACE__ . '\add_site_navigation_menus' ); add_filter( 'wporg_block_navigation_menus', __NAMESPACE__ . '\add_site_navigation_menus' );
add_filter( 'wporg_query_total_label', __NAMESPACE__ . '\update_query_total_label', 10, 2 );
add_filter( 'wporg_query_filter_options_sort', __NAMESPACE__ . '\get_sort_options' );
add_action( 'wporg_query_filter_in_form', __NAMESPACE__ . '\inject_other_filters' );
add_filter( 'render_block_core/post-excerpt', __NAMESPACE__ . '\inject_excerpt_more_link', 10, 3 ); add_filter( 'render_block_core/post-excerpt', __NAMESPACE__ . '\inject_excerpt_more_link', 10, 3 );
/** /**
@ -50,6 +53,74 @@ function add_site_navigation_menus( $menus ) {
); );
} }
/**
* Update the query total label to reflect "pledges" found.
*
* @param string $label The maybe-pluralized label to use, a result of `_n()`.
* @param int $found_posts The number of posts to use for determining pluralization.
*
* @return string Updated string with total placeholder.
*/
function update_query_total_label( $label, $found_posts ) {
/* translators: %s: the result count. */
return _n( '%s pledge', '%s pledges', $found_posts, 'wporg-5ftf' );
}
/**
* Provide a list of sort options.
*
* @param array $options The options for this filter.
* @return array New list of category options.
*/
function get_sort_options( $options ) {
global $wp_query;
$sort = isset( $_GET['order'] ) ? $_GET['order'] : '';
$label = __( 'Sort: Random', 'wporg-5ftf' );
switch ( $sort ) {
case 'alphabetical':
$label = __( 'Sort: Alphabetical', 'wporg-5ftf' );
break;
case 'contributors':
$label = __( 'Sort: Total Contributors', 'wporg-5ftf' );
break;
case 'hours':
$label = __( 'Sort: Total Hours', 'wporg-5ftf' );
break;
}
return array(
'label' => $label,
'title' => __( 'Sort', 'wporg-5ftf' ),
'key' => 'order',
'action' => home_url( '/pledges/' ),
'options' => array(
'' => __( 'Random', 'wporg-5ftf' ),
'alphabetical' => __( 'Alphabetical', 'wporg-5ftf' ),
'contributors' => __( 'Total Contributors', 'wporg-5ftf' ),
'hours' => __( 'Total Hours', 'wporg-5ftf' ),
),
'selected' => [ $sort ],
);
}
/**
* Add in the search term as a hidden input in the filter form.
*
* This enables the sort filter to apply to search results, as opposed to
* clearing the search when selected.
*
* @param string $key The key for the current filter.
*/
function inject_other_filters( $key ) {
global $wp_query;
// Pass through search query.
if ( isset( $wp_query->query['s'] ) ) {
printf( '<input type="hidden" name="s" value="%s" />', esc_attr( $wp_query->query['s'] ) );
}
}
/** /**
* Update the excerpt block content, replacing the placeholder string with * Update the excerpt block content, replacing the placeholder string with
* dynamic text including the pledge title for unique link text. * dynamic text including the pledge title for unique link text.

View file

@ -44,7 +44,7 @@ if ( is_search() ) {
<!-- wp:group {"style":{"spacing":{"blockGap":"0"}},"layout":{"type":"flex","flexWrap":"nowrap"},"className":"wporg-query-filters"} --> <!-- wp:group {"style":{"spacing":{"blockGap":"0"}},"layout":{"type":"flex","flexWrap":"nowrap"},"className":"wporg-query-filters"} -->
<div class="wp-block-group wporg-query-filters"> <div class="wp-block-group wporg-query-filters">
<!-- wp:wporg/query-filter {"key":"sort"} /--> <!-- wp:wporg/query-filter {"key":"sort","multiple":false} /-->
</div> </div>
<!-- /wp:group --> <!-- /wp:group -->
</div> </div>