sorting_to_wp_query_param( $default_sort );
$current_sort = "{$orderby}|{$order}";
// we need to dynamically inject the sort field into the search box when the search box is enabled, and display
// it separately when it's not.
if ( ! empty( $instance['search_box_enabled'] ) ) {
Jetpack_Search_Template_Tags::render_widget_search_form( $instance['post_types'], $orderby, $order );
}
if ( ! empty( $instance['search_box_enabled'] ) && ! empty( $instance['user_sort_enabled'] ) ) :
?>
';
echo $args['after_widget'];
}
/**
* Renders JavaScript for the sorting controls on the frontend.
*
* This JS is a bit complicated, but here's what it's trying to do:
* - find the search form
* - find the orderby/order fields and set default values
* - detect changes to the sort field, if it exists, and use it to set the order field values
*
* @since 5.8.0
*
* @param array $instance The current widget instance.
* @param string $order The order to initialize the select with.
* @param string $orderby The orderby to initialize the select with.
*/
private function maybe_render_sort_javascript( $instance, $order, $orderby ) {
if ( ! empty( $instance['user_sort_enabled'] ) ) :
?>
$type ) {
$count = intval( $new_instance['num_filters'][ $index ] );
$count = min( 50, $count ); // Set max boundary at 50.
$count = max( 1, $count ); // Set min boundary at 1.
switch ( $type ) {
case 'taxonomy':
$filters[] = array(
'name' => sanitize_text_field( $new_instance['filter_name'][ $index ] ),
'type' => 'taxonomy',
'taxonomy' => sanitize_key( $new_instance['taxonomy_type'][ $index ] ),
'count' => $count,
);
break;
case 'post_type':
$filters[] = array(
'name' => sanitize_text_field( $new_instance['filter_name'][ $index ] ),
'type' => 'post_type',
'count' => $count,
);
break;
case 'date_histogram':
$filters[] = array(
'name' => sanitize_text_field( $new_instance['filter_name'][ $index ] ),
'type' => 'date_histogram',
'count' => $count,
'field' => sanitize_key( $new_instance['date_histogram_field'][ $index ] ),
'interval' => sanitize_key( $new_instance['date_histogram_interval'][ $index ] ),
);
break;
}
}
}
if ( ! empty( $filters ) ) {
$instance['filters'] = $filters;
}
return $instance;
}
/**
* Outputs the settings update form.
*
* @since 5.0.0
*
* @param array $instance Current settings.
*/
public function form( $instance ) {
$instance = $this->jetpack_search_populate_defaults( $instance );
$title = strip_tags( $instance['title'] );
$hide_filters = Jetpack_Search_Helpers::are_filters_by_widget_disabled();
$classes = sprintf(
'jetpack-search-filters-widget %s %s %s',
$hide_filters ? 'hide-filters' : '',
$instance['search_box_enabled'] ? '' : 'hide-post-types',
$this->id
);
?>
false ), 'objects' ) as $post_type ) : ?>
render_widget_edit_filter( $filter ); ?>
" : esc_attr( $value );
}
/**
* We need to render HTML in two formats: an Underscore template (client-size)
* and native PHP (server-side). This helper function allows for easy rendering
* of the "selected" attribute in both formats.
*
* @since 5.8.0
*
* @param string $name Attribute name.
* @param string $value Attribute value.
* @param string $compare Value to compare to the attribute value to decide if it should be selected.
* @param bool $is_template Whether this is for an Underscore template or not.
*/
private function render_widget_option_selected( $name, $value, $compare, $is_template ) {
$compare_js = rawurlencode( $compare );
echo $is_template ? "<%= decodeURIComponent( '$compare_js' ) === $name ? 'selected=\"selected\"' : '' %>" : selected( $value, $compare );
}
/**
* Responsible for rendering a single filter in the customizer or the widget administration screen in wp-admin.
*
* We use this method for two purposes - rendering the fields server-side, and also rendering a script template for Underscore.
*
* @since 5.7.0
*
* @param array $filter The filter to render.
* @param bool $is_template Whether this is for an Underscore template or not.
*/
public function render_widget_edit_filter( $filter, $is_template = false ) {
$args = wp_parse_args(
$filter, array(
'name' => '',
'type' => 'taxonomy',
'taxonomy' => '',
'post_type' => '',
'field' => '',
'interval' => '',
'count' => self::DEFAULT_FILTER_COUNT,
)
);
$args['name_placeholder'] = Jetpack_Search_Helpers::generate_widget_filter_name( $args );
?>