<?php
/**
* Fields helpers
* This file contains all the functions to manage fields
*/
/**
* Main search
*/
add_filter( 'findstr_default_search_query_args', 'findstr_main_search_default', 10, 2 );
function findstr_main_search_default( $args, $group ) {
$findstr = findstr( $group );
if ( ! empty( $findstr->get_queried_search( 'q' ) ) ) {
$args['q'] = $findstr->get_queried_search( 'q' );
}
return $args;
}
/**
* Datepicker
* In order to use the datepicker, you need to transform dates in the right format (timestamp) before sending them to Meilisearch
*
*/
add_filter( 'findstr_indexable_fields_loop_to_index', 'findstr_date_to_timestamp', 10, 3 );
function findstr_date_to_timestamp( $fields, $field, $post ) {
if ( ! empty( $field->type ) && 'date' === $field->type ) {
$meta_key = explode( '/', $field->id );
$meta_key = end( $meta_key );
if ( ! empty( $fields[ $meta_key ] ) ) {
$fields[ $meta_key . '_timestamp' ] = strtotime( $fields[ $meta_key ] );
}
}
return $fields;
}
add_filter( 'findstr_sortable_attributes', 'findstr_field_date_sortable_attributes' );
function findstr_field_date_sortable_attributes( $sortable_attributes ) {
$indexable_fields = (array) ( new \FindStr\SettingsIndexableFields() )->get();
foreach ( $indexable_fields as $field ) {
if ( ! empty( $field->type ) && 'date' === $field->type ) {
$source_name = explode( '/', $field->id );
$sortable_attributes[] = end( $source_name ) . '_timestamp';
}
}
return $sortable_attributes;
}
add_filter( 'findstr_filterable_attributes', 'findstr_field_date_findstr_filterable_attributes' );
function findstr_field_date_findstr_filterable_attributes( $filterable_attributes ) {
$indexable_fields = (array) ( new \FindStr\SettingsIndexableFields() )->get();
foreach ( $indexable_fields as $field ) {
if ( ! empty( $field->type ) && 'date' === $field->type ) {
$source_name = explode( '/', $field->id );
$filterable_attributes[] = end( $source_name ) . '_timestamp';
}
}
return $filterable_attributes;
}
/**
* Search overlay fields
*
*/
add_filter(
'findstr_get_fields',
function ( $fields ) {
$id = 1000000000010;
$fields[ $id ] = array(
'id' => $id,
'fieldName' => __( 'Search', 'findstr' ),
'fieldSlug' => '_overlay-search', // unique slug, starts with an underscore to avoid conflicts with other fields
'fieldType' => 'search',
'options' => array(),
);
$id = 1000000000011;
$fields[ $id ] = array(
'id' => $id,
'fieldName' => __( 'Post Types', 'findstr' ),
'fieldSlug' => '_overlay-post-types', // unique slug, starts with an underscore to avoid conflicts with other fields
'fieldType' => 'checkbox',
'options' => array(
'sourceName' => 'post_type',
'hideFieldTitle' => true,
'showCount' => false,
),
);
$id = 1000000000012;
$fields[ $id ] = array(
'id' => $id,
'fieldName' => __( 'Load More', 'findstr' ),
'fieldSlug' => '_overlay-load-more', // unique slug, starts with an underscore to avoid conflicts with other fields
'fieldType' => 'loadMore',
'options' => array(
'showProgressBar' => true,
'showPagesNumber' => true,
),
);
return $fields;
}
);
add_filter( 'nav_menu_item_title', 'findstr_nav_menu_item_title', 10, 4 );
function findstr_nav_menu_item_title( $title, $item, $args, $depth ) {
if ( 'findstr-menu-item' === $item->type ) {
/**
* Filter the search menu item title
*
* Use this filter to change the menu item title or markup.
* By default, it displays a search icon with an HTML span element.
* ```
* <span class="findstr-search-icon">
* <svg class="feather feather-search" fill="none" height="24" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">
* <circle cx="11" cy="11" r="8"/><line x1="21" x2="16.65" y1="21" y2="16.65"/>
* </svg>
* </span>
* ```
*
* @hook findstr_search_menu_item_title
*
* @param {string} $title The title
* @param {WP_Post} $item The menu item
*
* @return {string} Title
*/
$title = apply_filters(
'findstr_search_menu_item_title',
'<span class="findstr-search-icon">
<svg class="feather feather-search" fill="none" height="24" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">
<circle cx="11" cy="11" r="8"/><line x1="21" x2="16.65" y1="21" y2="16.65"/>
</svg>
</span>',
$item
);
if ( ! defined( 'FINDSTR_SEARCH_MENU_ITEM' ) ) {
define( 'FINDSTR_SEARCH_MENU_ITEM', true );
}
}
return $title;
}
add_filter( 'nav_menu_link_attributes', 'findstr_nav_menu_link_attributes', 10, 4 );
function findstr_nav_menu_link_attributes( $atts, $item, $args, $depth ) {
if ( 'findstr-menu-item' === $item->type ) {
$atts['href'] = '#findstr-search';
$atts['title'] = _x( 'Search', 'search menu item', 'findstr' );
}
return $atts;
}
/**
* When a filter use the excludeOptions option, the exclusion is added to the search query
*/
add_filter(
'findstr_field_args',
function( $args, $slug, $group ) {
$field = $args['field'];
if ( ! empty( $field->options['excludeOptions'] ) ) {
$field->options['excludeOptions'] = (array) $field->options['excludeOptions'];
}
return $args;
},
999,
3
);
/**
* field value count
*/
add_action(
'findstr_before_template_part',
function ( $template_name, $template_path, $template_file, $args ) {
if ( 'results-container' === $template_name ) {
$group = $args['group'];
if ( ! empty( $args['facetDistribution'] ) ) {
?>
<script type="text/javascript">
document.addEventListener('findstrLoaded', function (e) {
findstr.hooks.addAction( 'findstrInit', 'findstr-set-facetDistribution', function (findstr) {
if( findstr.groups[ '<?php echo esc_attr( $group ); ?>' ] ) {
findstr.groups[ '<?php echo esc_attr( $group ); ?>' ].facets = <?php echo wp_json_encode( $args['facetDistribution'] ); ?>;
}
});
});
</script>
<?php
}
}
},
10,
4
);