PluginProbe
ElasticPress / 5.3.5
ElasticPress v5.3.5
5.3.5 5.3.4 3.6.5 3.6.6 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.7.0 4.7.1 4.7.2 5.0.0 5.0.1 5.0.2 All 108 releases
← All changes | includes/classes/Feature/Search/Weighting.php +308 -183 4.2.25.3.5 View file →
@@ -7,10 +7,9 @@
7 7
8 8 namespace ElasticPress\Feature\Search;
9 9
10 10 use ElasticPress\Features;
11 -use ElasticPress\Indexable\Post\Post;
12 -use ElasticPress\Utils as Utils;
11 +use ElasticPress\Utils;
13 12
14 13 /**
15 14 * Controls search weighting and search fields dashboard
16 15 *
@@ -34,11 +33,12 @@
34 33 return;
35 34 }
36 35
37 36 add_action( 'admin_menu', [ $this, 'add_weighting_submenu_page' ], 15 );
38 - add_action( 'admin_post_ep-weighting', [ $this, 'handle_save' ] );
39 37 add_filter( 'ep_formatted_args', [ $this, 'do_weighting' ], 20, 2 ); // After date decay, etc are injected
40 38 add_filter( 'ep_query_weighting_fields', [ $this, 'adjust_weight_for_cross_fields' ], 10, 5 );
39 + add_action( 'rest_api_init', [ $this, 'register_rest_routes' ] );
40 + add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] );
41 41 }
42 42
43 43 /**
44 44 * Returns a grouping of all the fields that support weighting for the post type
@@ -81,9 +81,9 @@
81 81 $post_type_taxonomies = get_object_taxonomies( $post_type );
82 82
83 83 $taxonomies = array_intersect( $public_taxonomies, $post_type_taxonomies );
84 84
85 - if ( $taxonomies ) {
85 + if ( ! empty( $taxonomies ) ) {
86 86 $fields['taxonomies'] = [
87 87 'label' => __( 'Taxonomies', 'elasticpress' ),
88 88 'children' => [],
89 89 ];
@@ -98,8 +98,41 @@
98 98 ];
99 99 }
100 100 }
101 101
102 + $fields['ep_metadata'] = [
103 + 'label' => 'Metadata',
104 + 'children' => [],
105 + ];
106 +
107 + $empty_post = new \WP_Post( new \stdClass() );
108 +
109 + $empty_post->post_type = $post_type;
110 +
111 + /** This filter is documented in includes/classes/Indexable/Post/Post.php */
112 + $allowed_protected_keys = apply_filters( 'ep_prepare_meta_allowed_protected_keys', [], $empty_post );
113 +
114 + sort( $allowed_protected_keys, SORT_STRING );
115 +
116 + /** This filter is documented in includes/classes/Indexable/Post/Post.php */
117 + $excluded_public_keys = apply_filters( 'ep_prepare_meta_excluded_public_keys', [], $empty_post );
118 +
119 + foreach ( $allowed_protected_keys as $meta_key ) {
120 + $key = "meta.$meta_key.value";
121 +
122 + if ( in_array( $key, $excluded_public_keys, true ) ) {
123 + continue;
124 + }
125 +
126 + $required = in_array( $meta_key, $allowed_protected_keys, true );
127 +
128 + $fields['ep_metadata']['children'][ $key ] = [
129 + 'key' => $key,
130 + 'label' => $meta_key,
131 + 'required' => $required,
132 + ];
133 + }
134 +
102 135 /**
103 136 * Filter weighting fields for a post type
104 137 *
105 138 * @hook ep_weighting_fields_for_post_type
@@ -110,8 +143,54 @@
110 143 return apply_filters( 'ep_weighting_fields_for_post_type', $fields, $post_type );
111 144 }
112 145
113 146 /**
147 + * Get weightable fields for all searchable post types.
148 + *
149 + * @since 5.0.0
150 + * @return array
151 + */
152 + public function get_weightable_fields() {
153 + $weightable = array();
154 + $post_types = Features::factory()->get_registered_feature( 'search' )->get_searchable_post_types();
155 +
156 + foreach ( $post_types as $post_type ) {
157 + $post_type_object = get_post_type_object( $post_type );
158 + $post_type_labels = get_post_type_labels( $post_type_object );
159 +
160 + $weightable_fields = $this->get_weightable_fields_for_post_type( $post_type );
161 +
162 + $groups = [];
163 + $fields = [];
164 +
165 + foreach ( $weightable_fields as $group => $weightable_field ) {
166 + $groups[] = [
167 + 'key' => $group,
168 + 'label' => $weightable_field['label'],
169 + ];
170 +
171 + foreach ( $weightable_field['children'] as $field ) {
172 + $fields[] = [
173 + 'group' => $group,
174 + 'key' => $field['key'],
175 + 'label' => $field['label'],
176 + 'required' => isset( $field['required'] ) ? $field['required'] : false,
177 + ];
178 + }
179 + }
180 +
181 + $weightable[] = [
182 + 'key' => $post_type,
183 + 'label' => $post_type_labels->menu_name,
184 + 'groups' => $groups,
185 + 'fields' => $fields,
186 + ];
187 + }
188 +
189 + return $weightable;
190 + }
191 +
192 + /**
114 193 * Returns default settings for any post type
115 194 *
116 195 * Defaults to title, content, excerpt, and author name enabled with zero weight
117 196 *
@@ -191,17 +270,68 @@
191 270 return apply_filters( 'ep_weighting_configuration', get_option( 'elasticpress_weighting', [] ) );
192 271 }
193 272
194 273 /**
274 + * Returns the current weighting configuration with defaults for any
275 + * missing fields.
276 + *
277 + * @return array Current weighting configuration with defaults.
278 + * @since 5.0.0
279 + */
280 + public function get_weighting_configuration_with_defaults() {
281 + $search = Features::factory()->get_registered_feature( 'search' );
282 +
283 + $post_types = $search->get_searchable_post_types();
284 + $weighting = $this->get_weighting_configuration();
285 +
286 + foreach ( $post_types as $post_type ) {
287 + $current = isset( $weighting[ $post_type ] ) ? $weighting[ $post_type ] : [];
288 + $defaults = $this->get_post_type_default_settings( $post_type );
289 +
290 + $weighting[ $post_type ] = wp_parse_args( $current, $defaults );
291 + }
292 +
293 + return $weighting;
294 + }
295 +
296 + /**
297 + * Returns the current meta mode.
298 + *
299 + * @return array
300 + * @since 5.0.0
301 + */
302 + public function get_meta_mode() {
303 + /**
304 + * Filter meta management mode.
305 + *
306 + * Setting the meta mode to 'auto' will restore the pre-5.0.0 behavior
307 + * of syncing all public meta fields, but without the ability to
308 + * add fields to make them searchable through the UI. Weighting settings
309 + * will need to be changed, and a sync performed, any time this value
310 + * is changed.
311 + *
312 + * @hook ep_meta_mode
313 + * @since 5.0.0
314 + * @param string $ep_meta_mode Meta mode.
315 + * @return string New meta mode.
316 + */
317 + return apply_filters( 'ep_meta_mode', 'manual' );
318 + }
319 +
320 + /**
195 321 * Adds the submenu page for controlling weighting
196 322 */
197 323 public function add_weighting_submenu_page() {
324 + $menu_slug = ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK && ! Utils\is_top_level_admin_context() ) ?
325 + 'elasticpress' :
326 + 'elasticpress-weighting';
327 +
198 328 add_submenu_page(
199 329 'elasticpress',
200 330 esc_html__( 'ElasticPress Search Fields & Weighting', 'elasticpress' ),
201 331 esc_html__( 'Search Fields & Weighting', 'elasticpress' ),
202 - 'manage_options',
203 - 'elasticpress-weighting',
332 + Utils\get_capability(),
333 + $menu_slug,
204 334 [ $this, 'render_settings_page' ]
205 335 );
206 336 }
207 337
@@ -210,213 +340,205 @@
210 340 */
211 341 public function render_settings_page() {
212 342 include EP_PATH . '/includes/partials/header.php'; ?>
213 343 <div class="wrap">
214 -
215 - <h1><?php esc_html_e( 'Manage Search Fields & Weighting', 'elasticpress' ); ?></h1>
216 - <p><?php esc_html_e( 'Adding more weight to an item will mean it will have more presence during searches. Add more weight to the items that are more important and need more prominence during searches.', 'elasticpress' ); ?></p>
217 - <p><?php esc_html_e( 'For example, adding more weight to the title attribute will cause search matches on the post title to appear more prominently.', 'elasticpress' ); ?></p>
218 -
219 - <form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post" class="weighting-settings metabox-holder">
220 - <input type="hidden" name="action" value="ep-weighting">
221 - <?php wp_nonce_field( 'save-weighting', 'ep-weighting-nonce' ); ?>
222 - <?php
223 - if ( isset( $_GET['settings-updated'] ) ) : // phpcs:ignore WordPress.Security.NonceVerification
224 - if ( $_GET['settings-updated'] ) : // phpcs:ignore WordPress.Security.NonceVerification
225 - ?>
226 - <div class="notice notice-success is-dismissible">
227 - <p><?php esc_html_e( 'Changes Saved!', 'elasticpress' ); ?></p>
228 - </div>
229 - <?php else : ?>
230 - <div class="notice notice-error is-dismissible">
231 - <p><?php esc_html_e( 'An error occurred when saving!', 'elasticpress' ); ?></p>
232 - </div>
233 - <?php
234 - endif;
235 - endif;
236 -
237 - /** Features Class @var Features $features */
238 - $features = Features::factory();
239 -
240 - /** Search Feature @var Feature\Search\Search $search */
241 - $search = $features->get_registered_feature( 'search' );
242 -
243 - $post_types = $search->get_searchable_post_types();
244 -
245 - $current_values = $this->get_weighting_configuration();
246 -
247 - foreach ( $post_types as $post_type ) :
248 - $fields = $this->get_weightable_fields_for_post_type( $post_type );
249 - $post_type_object = get_post_type_object( $post_type );
250 - ?>
251 - <div class="postbox">
252 - <h2 class="hndle"><?php echo esc_html( $post_type_object->labels->menu_name ); ?></h2>
253 -
254 - <?php
255 - foreach ( $fields as $field_group ) :
256 - $this->render_settings_section( $post_type, $field_group, $current_values );
257 - endforeach;
258 - ?>
259 - </div>
260 - <?php
261 - endforeach;
262 -
263 - submit_button();
264 - ?>
265 - </form>
344 + <div id="ep-weighting-screen"></div>
266 345 </div>
267 346 <?php
268 347 }
269 348
270 349 /**
271 - * Recursively renders each settings section and its children
350 + * Recursively renders each settings section and its children.
272 351 *
273 352 * @param string $post_type Current post type we're rendering
274 353 * @param array $field Current field to render
275 354 * @param array $current_values Current stored weighting values
355 + *
356 + * @deprecated
276 357 */
277 - public function render_settings_section( $post_type, $field, $current_values ) {
278 - if ( isset( $field['children'] ) && ! empty( $field['children'] ) ) :
279 - ?>
280 - <div class="field-group">
281 - <h3><?php echo esc_html( $field['label'] ); ?></h3>
282 - <div class="fields">
283 - <?php
284 - foreach ( $field['children'] as $child ) {
285 - $this->render_settings_section( $post_type, $child, $current_values );
286 - }
287 - ?>
288 - </div>
289 - </div>
290 - <?php
291 - elseif ( isset( $field['key'] ) ) :
292 - $key = $field['key'];
358 + public function render_settings_section( $post_type, $field, $current_values ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
359 + _doing_it_wrong(
360 + __METHOD__,
361 + esc_html( 'Weighting sections display are now handled via React components.' ),
362 + 'ElasticPress 5.0.0'
363 + );
364 + }
293 365
294 - $post_type_settings = isset( $current_values[ $post_type ] ) ? $current_values[ $post_type ] : $this->get_post_type_default_settings( $post_type );
366 + /**
367 + * Handles processing the new weighting values and saving them
368 + * to the elasticpress.io service.
369 + *
370 + * @deprecated
371 + */
372 + public function handle_save() {
373 + _doing_it_wrong(
374 + __METHOD__,
375 + esc_html( 'Weighting settings are now updated using the REST API.' ),
376 + 'ElasticPress 5.0.0'
377 + );
378 + }
295 379
296 - $weight = isset( $post_type_settings[ $key ] ) && isset( $post_type_settings[ $key ]['weight'] ) ? (int) $post_type_settings[ $key ]['weight'] : 0;
380 + /**
381 + * We need this method to test handle_save properly.
382 + *
383 + * @param string $redirect_url Redirect URL.
384 + * @deprecated
385 + */
386 + protected function redirect( $redirect_url ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed,Generic.CodeAnalysis.UnusedFunctionParameter.Found
387 + _doing_it_wrong(
388 + __METHOD__,
389 + esc_html( 'Weighting settings are now updated using the REST API, and do not redirect server-side.' ),
390 + 'ElasticPress 5.0.0'
391 + );
392 + }
297 393
298 - $range_disabled = '';
394 + /**
395 + * Save weighting configuration for each searchable post_type.
396 + *
397 + * @param array $settings weighting settings
398 + * @return void
399 + * @since 3.4.1
400 + * @deprecated
401 + */
402 + public function save_weighting_configuration( $settings ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed,Generic.CodeAnalysis.UnusedFunctionParameter.Found
403 + _doing_it_wrong(
404 + __METHOD__,
405 + esc_html( 'Weighting sections display are now handled via React components.' ),
406 + 'ElasticPress 5.0.0'
407 + );
408 + }
299 409
300 - $enabled = (
301 - isset( $post_type_settings ) &&
302 - isset( $post_type_settings[ $key ] ) &&
303 - isset( $post_type_settings[ $key ]['enabled'] )
304 - )
305 - ? boolval( $post_type_settings[ $key ]['enabled'] ) : false;
306 -
307 - if ( ! $enabled ) {
308 - $range_disabled = 'disabled="disabled" ';
309 - $weight = 0;
310 - }
311 - ?>
312 - <fieldset>
313 - <legend><?php echo esc_html( $field['label'] ); ?></legend>
314 -
315 - <p class="searchable">
316 - <input type="checkbox" value="on" <?php checked( $enabled ); ?> id="<?php echo esc_attr( "{$post_type}-{$key}-enabled" ); ?>" name="weighting[<?php echo esc_attr( $post_type ); ?>][<?php echo esc_attr( $key ); ?>][enabled]">
317 - <label for="<?php echo esc_attr( "{$post_type}-{$key}-enabled" ); ?>"><?php esc_html_e( 'Searchable', 'elasticpress' ); ?></label>
318 - </p>
319 -
320 - <p class="weighting">
321 - <label for="<?php echo esc_attr( "{$post_type}-{$key}-weight" ); ?>">
322 - <?php esc_html_e( 'Weight: ', 'elasticpress' ); ?>
323 - <span class="weighting-value">
324 - <?php echo esc_html( $weight ); ?>
325 - </span>
326 - </label>
327 - <input type="range" min="1" max="100" step="1" value="<?php echo esc_attr( $weight ); ?>" id="<?php echo esc_attr( "{$post_type}-{$key}-weight" ); ?>" name="weighting[<?php echo esc_attr( $post_type ); ?>][<?php echo esc_attr( $key ); ?>][weight]" <?php echo $range_disabled; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
328 - </p>
329 - </fieldset>
330 - <?php
331 - endif;
410 + /**
411 + * Register REST routes.
412 + *
413 + * @return void
414 + * @since 5.0.0
415 + */
416 + public function register_rest_routes() {
417 + register_rest_route(
418 + 'elasticpress/v1',
419 + 'weighting',
420 + [
421 + 'callback' => [ $this, 'update_weighting' ],
422 + 'methods' => 'POST',
423 + 'permission_callback' => function () {
424 + return current_user_can( Utils\get_capability() );
425 + },
426 + ]
427 + );
332 428 }
333 429
334 430 /**
335 - * Handles processing the new weighting values and saving them to the elasticpress.io service
431 + * Enqueue scripts and styles.
432 + *
433 + * @since 5.3.3
434 + * @return void
336 435 */
337 - public function handle_save() {
338 - if ( ! isset( $_POST['ep-weighting-nonce'] ) || ! wp_verify_nonce( $_POST['ep-weighting-nonce'], 'save-weighting' ) ) {
436 + public function admin_enqueue_scripts() {
437 + if ( 'weighting' !== \ElasticPress\Screen::factory()->get_current_screen() ) {
339 438 return;
340 439 }
341 440
342 - if ( ! current_user_can( 'manage_options' ) ) {
343 - return;
344 - }
441 + wp_enqueue_style(
442 + 'ep_weighting_styles',
443 + EP_URL . 'dist/css/weighting-script.css',
444 + [ 'wp-components', 'wp-edit-post' ],
445 + Utils\get_asset_info( 'weighting-script', 'version' )
446 + );
345 447
346 - $this->save_weighting_configuration( $_POST );
448 + wp_enqueue_script(
449 + 'ep_weighting_script',
450 + EP_URL . 'dist/js/weighting-script.js',
451 + Utils\get_asset_info( 'weighting-script', 'dependencies' ),
452 + Utils\get_asset_info( 'weighting-script', 'version' ),
453 + true
454 + );
347 455
348 - $redirect_url = admin_url( 'admin.php?page=elasticpress-weighting' );
349 - $redirect_url = add_query_arg( 'settings-updated', true, $redirect_url );
456 + $api_url = esc_url_raw( rest_url( 'elasticpress/v1/weighting' ) );
457 + $meta_mode = $this->get_meta_mode();
458 + $weightable_fields = $this->get_weightable_fields();
459 + $weighting_configuration = $this->get_weighting_configuration_with_defaults();
350 460
351 - $this->redirect( $redirect_url );
352 - }
461 + /**
462 + * Filter weighting dashboard options.
463 + *
464 + * @hook ep_weighting_options
465 + * @param {array} $data Weighting dashboard options
466 + * @return {array} New options array
467 + * @since 5.1.0
468 + */
469 + $data = apply_filters(
470 + 'ep_weighting_options',
471 + [
472 + 'apiUrl' => $api_url,
473 + 'metaMode' => $meta_mode,
474 + 'weightableFields' => $weightable_fields,
475 + 'weightingConfiguration' => $weighting_configuration,
476 + ]
477 + );
353 478
354 - /**
355 - * We need this method to test handle_save properly.
356 - *
357 - * @param string $redirect_url Redirect URL.
358 - */
359 - protected function redirect( $redirect_url ) {
360 - // @codeCoverageIgnoreStart
361 - wp_safe_redirect( $redirect_url );
362 - exit();
363 - // @codeCoverageIgnoreEnd
479 + wp_localize_script(
480 + 'ep_weighting_script',
481 + 'epWeighting',
482 + $data
483 + );
484 +
485 + wp_set_script_translations( 'ep_weighting_script', 'elasticpress' );
364 486 }
365 487
366 488 /**
367 - * Save weighting configuration for each searchable post_type
489 + * Handles processing the new weighting values and saving them.
368 490 *
369 - * @param array $settings weighting settings
370 - *
371 - * @return array final settings
372 - * @since 3.4.1
491 + * @param \WP_Rest_Request $request REST API request.
492 + * @return array
493 + * @since 5.0.0
373 494 */
374 - public function save_weighting_configuration( $settings ) {
375 - $new_config = array();
376 - $previous_config_formatted = array();
377 - $current_config = $this->get_weighting_configuration();
495 + public function update_weighting( $request = null ) {
496 + $meta_mode = $this->get_meta_mode();
497 + $weighting = $request->get_json_params();
378 498
379 - foreach ( $current_config as $post_type => $post_type_weighting ) {
380 - // This also ensures the string is safe, since this would return false otherwise
381 - if ( ! post_type_exists( $post_type ) ) {
382 - continue;
383 - }
499 + $post_types = Features::factory()->get_registered_feature( 'search' )->get_searchable_post_types();
384 500
385 - // We need a way to know if fields have been explicitly set before, let's compare a previous state against $_POST['weighting']
386 - foreach ( $post_type_weighting as $weighting_field => $weighting_values ) {
387 - $previous_config_formatted[ $post_type ][ sanitize_text_field( $weighting_field ) ] = [
388 - 'weight' => isset( $settings['weighting'][ $post_type ][ $weighting_field ]['weight'] ) ? intval( $settings['weighting'][ $post_type ][ $weighting_field ]['weight'] ) : 0,
389 - 'enabled' => isset( $settings['weighting'][ $post_type ][ $weighting_field ]['enabled'] ) && 'on' === $settings['weighting'][ $post_type ][ $weighting_field ]['enabled'] ? true : false,
390 - ];
501 + // Add default fields for post types not sent.
502 + foreach ( $post_types as $post_type ) {
503 + if ( ! isset( $weighting[ $post_type ] ) ) {
504 + $weighting[ $post_type ] = [];
391 505 }
392 506 }
393 507
394 - $search = Features::factory()->get_registered_feature( 'search' );
395 - $post_types = $search->get_searchable_post_types();
508 + /**
509 + * If metadata is not being managed manually, remove any custom
510 + * fields that have not been registered as weightable fields.
511 + */
512 + if ( 'manual' !== $meta_mode ) {
513 + foreach ( $weighting as $post_type => $fields ) {
514 + $weightable_fields = $this->get_weightable_fields_for_post_type( $post_type );
396 515
397 - foreach ( $post_types as $post_type ) {
398 - // This also ensures the string is safe, since this would return false otherwise
399 - if ( ! post_type_exists( $post_type ) ) {
400 - continue;
401 - }
516 + foreach ( $fields as $key => $value ) {
517 + $is_weightable = false;
402 518
403 - /** override default post_type settings while saving */
404 - $new_config[ $post_type ] = array();
519 + foreach ( $weightable_fields as $group ) {
520 + if ( isset( $group['children'][ $key ] ) ) {
521 + $is_weightable = true;
522 + continue;
523 + }
524 + }
405 525
406 - if ( isset( $settings['weighting'][ $post_type ] ) ) {
407 - foreach ( $settings['weighting'][ $post_type ] as $weighting_field => $weighting_values ) {
408 - $new_config[ $post_type ][ sanitize_text_field( $weighting_field ) ] = [
409 - 'weight' => isset( $weighting_values['weight'] ) ? intval( $weighting_values['weight'] ) : 0,
410 - 'enabled' => isset( $weighting_values['enabled'] ) && 'on' === $weighting_values['enabled'] ? true : false,
411 - ];
526 + if ( ! $is_weightable ) {
527 + unset( $weighting[ $post_type ][ $key ] );
528 + }
412 529 }
413 530 }
414 531 }
415 532
416 - $final_config = array_replace_recursive( $previous_config_formatted, $new_config );
533 + // Cleanup any post type sent (or added via filters) that does not exist.
534 + foreach ( $weighting as $post_type => $fields ) {
535 + if ( ! post_type_exists( $post_type ) ) {
536 + unset( $weighting[ $post_type ] );
537 + }
538 + }
417 539
418 - update_option( 'elasticpress_weighting', $final_config );
540 + update_option( 'elasticpress_weighting', $weighting );
419 541
420 542 /**
421 543 * Fires right after the weighting configuration is saved.
422 544 *
@@ -424,9 +546,12 @@
424 546 * @hook ep_saved_weighting_configuration
425 547 */
426 548 do_action( 'ep_saved_weighting_configuration' );
427 549
428 - return $final_config;
550 + return [
551 + 'data' => $weighting,
552 + 'success' => true,
553 + ];
429 554 }
430 555
431 556 /**
432 557 * Iterates through arrays in the formatted args to find "fields" and injects weighting values
@@ -447,9 +572,9 @@
447 572 }
448 573 }
449 574
450 575 foreach ( $fieldset['fields'] as $key => $field ) {
451 - if ( isset( $weights[ $field ] ) && false !== $weights[ $field ]['enabled'] ) {
576 + if ( isset( $weights[ $field ]['enabled'] ) && false !== $weights[ $field ]['enabled'] ) {
452 577 $weight = (int) $weights[ $field ]['weight'];
453 578
454 579 if ( 0 !== $weight ) {
455 580 if ( 'author_name' === $field ) {
@@ -517,18 +642,8 @@
517 642 * @param array $args WP_Query args
518 643 * @return boolean true/false depending on any fields enabled == true
519 644 */
520 645 public function post_type_has_fields( $post_type, $args = [] ) {
521 - // define keys which are irrelevant for this consideration
522 -
523 - /**
524 - * Filter fields considered in weighting
525 - *
526 - * @hook ep_weighting_ignore_fields_in_consideration
527 - * @param {array} $fields Current fields
528 - * @return {array} New fields
529 - */
530 - $ignore_keys = apply_filters( 'ep_weighting_ignore_fields_in_consideration', [ 'terms.ep_custom_result.name' => true ] );
531 646 $weight_config = $this->get_weighting_configuration();
532 647
533 648 /**
534 649 * Filter weighting configuration for search
@@ -535,9 +650,9 @@
535 650 *
536 651 * @hook ep_weighting_configuration_for_search
537 652 * @param {array} $weight_config Current weight config
538 653 * @param {array} $args WP Query arguments
539 - * @return {array} New configutation
654 + * @return {array} New configuration
540 655 */
541 656 $weight_config = apply_filters( 'ep_weighting_configuration_for_search', $weight_config, $args );
542 657
543 658 if ( ! isset( $weight_config[ $post_type ] ) ) {
@@ -545,13 +660,23 @@
545 660 } else {
546 661 $weights = $weight_config[ $post_type ];
547 662 }
548 663
549 - $fields = array_diff_key( $weights, $ignore_keys );
664 + /**
665 + * Filter fields considered in weighting
666 + *
667 + * Define keys which are irrelevant for this consideration, like `terms.ep_custom_result.name`.
668 + *
669 + * @hook ep_weighting_ignore_fields_in_consideration
670 + * @param {array} $fields Current fields
671 + * @return {array} New fields
672 + */
673 + $ignore_keys = apply_filters( 'ep_weighting_ignore_fields_in_consideration', [ 'terms.ep_custom_result.name' => true ] );
674 + $fields = array_diff_key( $weights, $ignore_keys );
550 675
551 676 $found_enabled = false;
552 677 foreach ( $fields as $field ) {
553 - if ( filter_var( $field['enabled'], FILTER_VALIDATE_BOOLEAN ) ) {
678 + if ( isset( $field['enabled'] ) && filter_var( $field['enabled'], FILTER_VALIDATE_BOOLEAN ) ) {
554 679 $found_enabled = true;
555 680 break;
556 681 }
557 682 }