Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to toggle term counter #3007

Merged
merged 2 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion assets/js/instant-results/components/common/checkbox-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { __, _n, sprintf } from '@wordpress/i18n';
*/
import Checkbox from './checkbox';
import SmallButton from './small-button';
import { termCount } from '../../config';

/**
* Checkbox list component.
Expand Down Expand Up @@ -124,6 +125,10 @@ export default ({ disabled, label, options, onChange, selected, sortBy }) => {
*/
const displayOption = ({ count, id, label, value }) => {
const children = childOptions[value];
/**
* Check for term count option.
*/
const counter = termCount === '1' ? count : '';

if (!showAll && optionsShown >= optionsLimit) {
return <Fragment key={value} />;
Expand All @@ -133,7 +138,7 @@ export default ({ disabled, label, options, onChange, selected, sortBy }) => {
<li className="ep-search-options-list__item" key={value}>
<Checkbox
checked={selected.includes(value)}
count={count}
count={counter}
disabled={disabled}
id={id}
label={label}
Expand Down
2 changes: 2 additions & 0 deletions assets/js/instant-results/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const {
paramPrefix,
postTypeLabels,
taxonomyLabels,
termCount,
} = window.epInstantResults;

/**
Expand Down Expand Up @@ -72,4 +73,5 @@ export {
postTypeLabels,
sortOptions,
taxonomyLabels,
termCount,
};
29 changes: 29 additions & 0 deletions includes/classes/Feature/InstantResults/InstantResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public function __construct() {
'highlight_tag' => 'mark',
'facets' => 'post_type,category,post_tag',
'match_type' => 'all',
'term_count' => '1',
];

$settings = $this->get_settings() ? $this->get_settings() : array();
Expand Down Expand Up @@ -171,6 +172,18 @@ public function output_feature_box_settings() {
<p class="field-description"><?php esc_html_e( '"All" will only show content that matches all facets. "Any" will show content that matches any facet.', 'elasticpress' ); ?></p>
</div>
</div>
<div class="field">
<div class="field-name status"><?php esc_html_e( 'Term Count', 'elasticpress' ); ?></div>
<div class="input-wrap">
<label>
<input name="settings[term_count]" <?php checked( (bool) $this->settings['term_count'] ); ?> type="radio" value="1"><?php esc_html_e( 'Enabled', 'elasticpress' ); ?>
</label><br>
<label>
<input name="settings[term_count]" <?php checked( ! (bool) $this->settings['term_count'] ); ?> type="radio" value="0"><?php esc_html_e( 'Disabled', 'elasticpress' ); ?>
</label>
<p class="field-description"><?php esc_html_e( 'When enabled, it will show the term count in the instant results widget.', 'elasticpress' ); ?></p>
</div>
</div>

<?php
}
Expand Down Expand Up @@ -224,6 +237,7 @@ public function setup() {
add_action( 'pre_get_posts', [ $this, 'maybe_apply_product_visibility' ] );
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_frontend_assets' ] );
add_action( 'wp_footer', [ $this, 'render' ] );
add_filter( 'ep_sanitize_feature_settings', [ $this, 'sanitize_count_settings' ] );
}

/**
Expand Down Expand Up @@ -281,6 +295,7 @@ public function enqueue_frontend_assets() {
'matchType' => $this->settings['match_type'],
'paramPrefix' => 'ep-',
'postTypeLabels' => $this->get_post_type_labels(),
'termCount' => $this->settings['term_count'],
)
);
}
Expand Down Expand Up @@ -923,4 +938,18 @@ public function get_args_schema() {
}
return $args;
}

/**
* Sanitizes our term count settings.
*
* @param array $settings Array of current settings
* @return mixed
*/
public function sanitize_count_settings( $settings ) {
if ( ! empty( $settings['instant-results']['term_count'] ) ) {
$settings['instant-results']['term_count'] = (bool) $settings['instant-results']['term_count'];
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MARQAS couldn't this be simplified just calling

$settings['instant-results']['term_count'] = ! empty( $settings['instant-results']['term_count'] );

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MARQAS @felipeelia Is this function required at all? None of the other Enabled/Disabled settings on the other features have anything like this. Also, sanitising as boolean isn't really worth it as the values are saved as '0' or '1' strings anyway, and the default value is '1' too.


return $settings;
}
}