Skip to content

Commit 637630a

Browse files
author
Mustafa Şükrü Kapusuz
committed
refactor: a try to reduce reduntant code in get_setting methods
1 parent f14580e commit 637630a

File tree

5 files changed

+71
-144
lines changed

5 files changed

+71
-144
lines changed

includes/indices/class-algolia-index.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* @since 1.0.0
1919
*/
2020
abstract class Algolia_Index {
21+
protected $settings = [];
2122

2223
/**
2324
* The SearchClient instance.
@@ -685,7 +686,29 @@ protected function get_re_index_batch_size() {
685686
*
686687
* @return array
687688
*/
688-
abstract public function get_settings();
689+
public function get_settings() {
690+
$settings = (array) apply_filters( 'algolia_' . $this->get_id() . '_index_settings', $this->settings );
691+
692+
/**
693+
* Replacing `attributesToIndex` with `searchableAttributes` as
694+
* it has been replaced by Algolia.
695+
*
696+
* @link https://www.algolia.com/doc/api-reference/api-parameters/searchableAttributes/
697+
* @since 2.2.0
698+
*/
699+
if (
700+
array_key_exists( 'attributesToIndex', $settings )
701+
&& is_array( $settings['attributesToIndex'] )
702+
) {
703+
$settings['searchableAttributes'] = array_merge(
704+
$settings['searchableAttributes'],
705+
$settings['attributesToIndex']
706+
);
707+
unset( $settings['attributesToIndex'] );
708+
}
709+
710+
return $settings;
711+
}
689712

690713
/**
691714
* Get synonyms.

includes/indices/class-algolia-posts-index.php

Lines changed: 25 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,30 @@
1414
* @since 1.0.0
1515
*/
1616
final class Algolia_Posts_Index extends Algolia_Index {
17+
protected $settings = [
18+
'searchableAttributes' => array(
19+
'unordered(post_title)',
20+
'unordered(taxonomies)',
21+
'unordered(content)',
22+
),
23+
'customRanking' => array(
24+
'desc(is_sticky)',
25+
'desc(post_date)',
26+
'asc(record_index)',
27+
),
28+
'attributeForDistinct' => 'post_id',
29+
'distinct' => true,
30+
'attributesForFaceting' => array(
31+
'taxonomies',
32+
'taxonomies_hierarchical',
33+
'post_author.display_name',
34+
),
35+
'attributesToSnippet' => array(
36+
'post_title:30',
37+
'content:30',
38+
),
39+
'snippetEllipsisText' => '',
40+
];
1741

1842
/**
1943
* The post type.
@@ -271,53 +295,7 @@ private function get_post_shared_attributes( WP_Post $post ) {
271295
* @return array
272296
*/
273297
public function get_settings() {
274-
$settings = array(
275-
'searchableAttributes' => array(
276-
'unordered(post_title)',
277-
'unordered(taxonomies)',
278-
'unordered(content)',
279-
),
280-
'customRanking' => array(
281-
'desc(is_sticky)',
282-
'desc(post_date)',
283-
'asc(record_index)',
284-
),
285-
'attributeForDistinct' => 'post_id',
286-
'distinct' => true,
287-
'attributesForFaceting' => array(
288-
'taxonomies',
289-
'taxonomies_hierarchical',
290-
'post_author.display_name',
291-
),
292-
'attributesToSnippet' => array(
293-
'post_title:30',
294-
'content:30',
295-
),
296-
'snippetEllipsisText' => '',
297-
);
298-
299-
$settings = (array) apply_filters( 'algolia_posts_index_settings', $settings, $this->post_type );
300-
$settings = (array) apply_filters( 'algolia_posts_' . $this->post_type . '_index_settings', $settings );
301-
302-
/**
303-
* Replacing `attributesToIndex` with `searchableAttributes` as
304-
* it has been replaced by Algolia.
305-
*
306-
* @link https://www.algolia.com/doc/api-reference/api-parameters/searchableAttributes/
307-
* @since 2.2.0
308-
*/
309-
if (
310-
array_key_exists( 'attributesToIndex', $settings )
311-
&& is_array( $settings['attributesToIndex'] )
312-
) {
313-
$settings['searchableAttributes'] = array_merge(
314-
$settings['searchableAttributes'],
315-
$settings['attributesToIndex']
316-
);
317-
unset( $settings['attributesToIndex'] );
318-
}
319-
320-
return $settings;
298+
return (array) apply_filters( 'algolia_posts_index_settings', parent::get_settings(), $this->post_type );
321299
}
322300

323301
/**

includes/indices/class-algolia-searchable-posts-index.php

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,8 @@ private function get_post_shared_attributes( WP_Post $post ) {
255255
* @return array
256256
*/
257257
public function get_settings() {
258-
$settings = array(
258+
// override settings prop to inject the WP Filter Hook.
259+
$this->settings = [
259260
'searchableAttributes' => array(
260261
'unordered(post_title)',
261262
'unordered(taxonomies)',
@@ -279,29 +280,9 @@ public function get_settings() {
279280
'content:' . intval( apply_filters( 'excerpt_length', 55 ) ), // phpcs:ignore -- Legitimate use of Core hook.
280281
),
281282
'snippetEllipsisText' => '',
282-
);
283-
284-
$settings = (array) apply_filters( 'algolia_searchable_posts_index_settings', $settings );
285-
286-
/**
287-
* Replacing `attributesToIndex` with `searchableAttributes` as
288-
* it has been replaced by Algolia.
289-
*
290-
* @link https://www.algolia.com/doc/api-reference/api-parameters/searchableAttributes/
291-
* @since 2.2.0
292-
*/
293-
if (
294-
array_key_exists( 'attributesToIndex', $settings )
295-
&& is_array( $settings['attributesToIndex'] )
296-
) {
297-
$settings['searchableAttributes'] = array_merge(
298-
$settings['searchableAttributes'],
299-
$settings['attributesToIndex']
300-
);
301-
unset( $settings['attributesToIndex'] );
302-
}
283+
];
303284

304-
return $settings;
285+
return parent::get_settings();
305286
}
306287

307288
/**

includes/indices/class-algolia-terms-index.php

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@
1414
* @since 1.0.0
1515
*/
1616
final class Algolia_Terms_Index extends Algolia_Index {
17+
protected $settings = [
18+
'searchableAttributes' => array(
19+
'unordered(name)',
20+
'unordered(description)',
21+
),
22+
'customRanking' => array(
23+
'desc(posts_count)',
24+
),
25+
];
1726

1827
/**
1928
* What this index contains.
@@ -130,38 +139,7 @@ protected function get_re_index_items_count() {
130139
* @return array
131140
*/
132141
public function get_settings() {
133-
$settings = array(
134-
'searchableAttributes' => array(
135-
'unordered(name)',
136-
'unordered(description)',
137-
),
138-
'customRanking' => array(
139-
'desc(posts_count)',
140-
),
141-
);
142-
143-
$settings = (array) apply_filters( 'algolia_terms_index_settings', $settings, $this->taxonomy );
144-
$settings = (array) apply_filters( 'algolia_terms_' . $this->taxonomy . '_index_settings', $settings );
145-
146-
/**
147-
* Replacing `attributesToIndex` with `searchableAttributes` as
148-
* it has been replaced by Algolia.
149-
*
150-
* @link https://www.algolia.com/doc/api-reference/api-parameters/searchableAttributes/
151-
* @since 2.2.0
152-
*/
153-
if (
154-
array_key_exists( 'attributesToIndex', $settings )
155-
&& is_array( $settings['attributesToIndex'] )
156-
) {
157-
$settings['searchableAttributes'] = array_merge(
158-
$settings['searchableAttributes'],
159-
$settings['attributesToIndex']
160-
);
161-
unset( $settings['attributesToIndex'] );
162-
}
163-
164-
return $settings;
142+
return apply_filters( 'algolia_terms_index_settings', parent::get_settings(), $this->taxonomy );
165143
}
166144

167145
/**

includes/indices/class-algolia-users-index.php

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414
* @since 1.0.0
1515
*/
1616
final class Algolia_Users_Index extends Algolia_Index {
17+
protected $settings = [
18+
'searchableAttributes' => array(
19+
'unordered(display_name)',
20+
),
21+
'customRanking' => array(
22+
'desc(posts_count)',
23+
),
24+
];
1725

1826
/**
1927
* What this index contains.
@@ -113,47 +121,6 @@ protected function get_re_index_items_count() {
113121
return (int) $users_count['total_users'];
114122
}
115123

116-
/**
117-
* Get settings.
118-
*
119-
* @author WebDevStudios <contact@webdevstudios.com>
120-
* @since 1.0.0
121-
*
122-
* @return array
123-
*/
124-
public function get_settings() {
125-
$settings = array(
126-
'searchableAttributes' => array(
127-
'unordered(display_name)',
128-
),
129-
'customRanking' => array(
130-
'desc(posts_count)',
131-
),
132-
);
133-
134-
$settings = (array) apply_filters( 'algolia_users_index_settings', $settings );
135-
136-
/**
137-
* Replacing `attributesToIndex` with `searchableAttributes` as
138-
* it has been replaced by Algolia.
139-
*
140-
* @link https://www.algolia.com/doc/api-reference/api-parameters/searchableAttributes/
141-
* @since 2.2.0
142-
*/
143-
if (
144-
array_key_exists( 'attributesToIndex', $settings )
145-
&& is_array( $settings['attributesToIndex'] )
146-
) {
147-
$settings['searchableAttributes'] = array_merge(
148-
$settings['searchableAttributes'],
149-
$settings['attributesToIndex']
150-
);
151-
unset( $settings['attributesToIndex'] );
152-
}
153-
154-
return $settings;
155-
}
156-
157124
/**
158125
* Get synonyms.
159126
*

0 commit comments

Comments
 (0)