Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
require_once ALGOLIA_PATH . 'includes/class-algolia-styles.php';
require_once ALGOLIA_PATH . 'includes/class-algolia-scripts.php';

require_once ALGOLIA_PATH . 'includes/indices/settings/interface-algolia-index-settings.php';
require_once ALGOLIA_PATH . 'includes/indices/settings/class-algolia-primary-index-settings.php';
require_once ALGOLIA_PATH . 'includes/indices/settings/class-algolia-index-settings-decorator.php';
require_once ALGOLIA_PATH . 'includes/indices/settings/class-algolia-replica-index-settings.php';
require_once ALGOLIA_PATH . 'includes/indices/class-algolia-index.php';
require_once ALGOLIA_PATH . 'includes/indices/class-algolia-index-replica.php';
require_once ALGOLIA_PATH . 'includes/indices/class-algolia-searchable-posts-index.php';
Expand Down
62 changes: 41 additions & 21 deletions includes/indices/class-algolia-index.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,22 @@
use WebDevStudios\WPSWA\Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
use WebDevStudios\WPSWA\Algolia\AlgoliaSearch\SearchClient;
use WebDevStudios\WPSWA\Algolia\AlgoliaSearch\SearchIndex;
use WebDevStudios\WPSWA\Algolia\AlgoliaSearch\Exceptions\NotFoundException;

/**
* Class Algolia_Index
*
* @since 1.0.0
*/
abstract class Algolia_Index {
const AC_INDEX_NOT_EXISTS_EXCEPTION_MSG = 'Index does not exist';

/**
* Default index settings
*
* @var array
*/
protected array $default_settings = [];

/**
* The SearchClient instance.
Expand Down Expand Up @@ -152,7 +161,7 @@ final public function set_client( SearchClient $client ) {
*
* @throws LogicException If the SearchClient has not been set.
*/
final protected function get_client() {
public function get_client() {
if ( null === $this->client ) {
throw new LogicException( 'SearchClient has not been set.' );
}
Expand Down Expand Up @@ -579,8 +588,7 @@ public function push_settings() {
$index = $this->get_index();

// This will create the index if it does not exist.
$settings = $this->get_settings();
$index->setSettings( $settings );
( new Algolia_Primary_Index_Settings( $this ) )->push();

// Push synonyms.
$synonyms = $this->get_synonyms();
Expand Down Expand Up @@ -686,7 +694,29 @@ protected function get_re_index_batch_size() {
*
* @return array
*/
abstract protected function get_settings();
public function get_default_settings() {
$settings = (array) apply_filters( 'algolia_' . $this->get_id() . '_index_settings', $this->default_settings );

/**
* Replacing `attributesToIndex` with `searchableAttributes` as
* it has been replaced by Algolia.
*
* @link https://www.algolia.com/doc/api-reference/api-parameters/searchableAttributes/
* @since 2.2.0
*/
if (
array_key_exists( 'attributesToIndex', $settings )
&& is_array( $settings['attributesToIndex'] )
) {
$settings['searchableAttributes'] = array_merge(
$settings['searchableAttributes'],
$settings['attributesToIndex']
);
unset( $settings['attributesToIndex'] );
}

return $settings;
}

/**
* Get synonyms.
Expand Down Expand Up @@ -824,10 +854,7 @@ private function sync_replicas() {
)
);

$client = $this->get_client();

// Ensure we re-push the master index settings each time.
$settings = $this->get_settings();
$settings = new Algolia_Primary_Index_Settings( $this ); // TODO: maybe move a common place.

/**
* Loop over the replicas.
Expand All @@ -838,10 +865,7 @@ private function sync_replicas() {
* @var Algolia_Index_Replica $replica
*/
foreach ( $replicas as $replica ) {
$settings['ranking'] = $replica->get_ranking();
$replica_index_name = $replica->get_replica_index_name( $this );
$index = $client->initIndex( $replica_index_name );
$index->setSettings( $settings );
( new Algolia_Replica_Index_Settings( $settings, $replica ) );
}
}

Expand All @@ -858,27 +882,23 @@ abstract public function delete_item( $item );
/**
* Check if the index exists in Algolia.
*
* Returns true if the index exists in Algolia.
* false otherwise.
* Returns true if the index exists in Algolia, false otherwise.
*
* @throws \Exception various exceptions can be thrown by Algolia Client.
* @author WebDevStudios <contact@webdevstudios.com>
* @since 1.0.0
*
* @return bool
*
* @throws AlgoliaException If the index does not exist in Algolia.
*/
public function exists() {
try {
$this->get_index()->getSettings();
} catch ( AlgoliaException $exception ) {
if ( $exception->getMessage() === 'Index does not exist' ) {
} catch ( NotFoundException $exception ) {
if ( self::AC_INDEX_NOT_EXISTS_EXCEPTION_MSG === $exception->getMessage() ) {
return false;
}

error_log( $exception->getMessage() ); // phpcs:ignore -- Legacy.

return false;
throw $exception;
}

return true;
Expand Down
76 changes: 28 additions & 48 deletions includes/indices/class-algolia-posts-index.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,30 @@
* @since 1.0.0
*/
final class Algolia_Posts_Index extends Algolia_Index {
protected array $default_settings = [
'searchableAttributes' => array(
'unordered(post_title)',
'unordered(taxonomies)',
'unordered(content)',
),
'customRanking' => array(
'desc(is_sticky)',
'desc(post_date)',
'asc(record_index)',
),
'attributeForDistinct' => 'post_id',
'distinct' => true,
'attributesForFaceting' => array(
'taxonomies',
'taxonomies_hierarchical',
'post_author.display_name',
),
'attributesToSnippet' => array(
'post_title:30',
'content:30',
),
'snippetEllipsisText' => '…',
];

/**
* The post type.
Expand Down Expand Up @@ -264,60 +288,16 @@ private function get_post_shared_attributes( WP_Post $post ) {

/**
* Get settings.
* Overridden to able to have "algolia_posts_index_settings" filter.
*
* @author WebDevStudios <contact@webdevstudios.com>
* @since 1.0.0
*
* @return array
*/
protected function get_settings() {
$settings = array(
'searchableAttributes' => array(
'unordered(post_title)',
'unordered(taxonomies)',
'unordered(content)',
),
'customRanking' => array(
'desc(is_sticky)',
'desc(post_date)',
'asc(record_index)',
),
'attributeForDistinct' => 'post_id',
'distinct' => true,
'attributesForFaceting' => array(
'taxonomies',
'taxonomies_hierarchical',
'post_author.display_name',
),
'attributesToSnippet' => array(
'post_title:30',
'content:30',
),
'snippetEllipsisText' => '…',
);

$settings = (array) apply_filters( 'algolia_posts_index_settings', $settings, $this->post_type );
$settings = (array) apply_filters( 'algolia_posts_' . $this->post_type . '_index_settings', $settings );

/**
* Replacing `attributesToIndex` with `searchableAttributes` as
* it has been replaced by Algolia.
*
* @link https://www.algolia.com/doc/api-reference/api-parameters/searchableAttributes/
* @since 2.2.0
*/
if (
array_key_exists( 'attributesToIndex', $settings )
&& is_array( $settings['attributesToIndex'] )
) {
$settings['searchableAttributes'] = array_merge(
$settings['searchableAttributes'],
$settings['attributesToIndex']
);
unset( $settings['attributesToIndex'] );
}

return $settings;
public function get_default_settings() {
$this->default_settings = (array) apply_filters( 'algolia_posts_index_settings', $this->default_settings, $this->post_type );
return parent::get_default_settings();
}

/**
Expand Down
30 changes: 6 additions & 24 deletions includes/indices/class-algolia-searchable-posts-index.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,15 @@ private function get_post_shared_attributes( WP_Post $post ) {
/**
* Get settings.
*
* Overridden to able to have "excerpt_length" WP filter hook for the attributesToSnippet.content
*
* @author WebDevStudios <contact@webdevstudios.com>
* @since 1.0.0
*
* @return array
*/
protected function get_settings() {
$settings = array(
public function get_default_settings() {
$this->default_settings = [
'searchableAttributes' => array(
'unordered(post_title)',
'unordered(taxonomies)',
Expand All @@ -279,29 +281,9 @@ protected function get_settings() {
'content:' . intval( apply_filters( 'excerpt_length', 55 ) ), // phpcs:ignore -- Legitimate use of Core hook.
),
'snippetEllipsisText' => '…',
);

$settings = (array) apply_filters( 'algolia_searchable_posts_index_settings', $settings );

/**
* Replacing `attributesToIndex` with `searchableAttributes` as
* it has been replaced by Algolia.
*
* @link https://www.algolia.com/doc/api-reference/api-parameters/searchableAttributes/
* @since 2.2.0
*/
if (
array_key_exists( 'attributesToIndex', $settings )
&& is_array( $settings['attributesToIndex'] )
) {
$settings['searchableAttributes'] = array_merge(
$settings['searchableAttributes'],
$settings['attributesToIndex']
);
unset( $settings['attributesToIndex'] );
}
];

return $settings;
return parent::get_default_settings();
}

/**
Expand Down
46 changes: 13 additions & 33 deletions includes/indices/class-algolia-terms-index.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
* @since 1.0.0
*/
final class Algolia_Terms_Index extends Algolia_Index {
protected array $default_settings = [
'searchableAttributes' => array(
'unordered(name)',
'unordered(description)',
),
'customRanking' => array(
'desc(posts_count)',
),
];

/**
* What this index contains.
Expand Down Expand Up @@ -129,39 +138,10 @@ protected function get_re_index_items_count() {
*
* @return array
*/
protected function get_settings() {
$settings = array(
'searchableAttributes' => array(
'unordered(name)',
'unordered(description)',
),
'customRanking' => array(
'desc(posts_count)',
),
);

$settings = (array) apply_filters( 'algolia_terms_index_settings', $settings, $this->taxonomy );
$settings = (array) apply_filters( 'algolia_terms_' . $this->taxonomy . '_index_settings', $settings );

/**
* Replacing `attributesToIndex` with `searchableAttributes` as
* it has been replaced by Algolia.
*
* @link https://www.algolia.com/doc/api-reference/api-parameters/searchableAttributes/
* @since 2.2.0
*/
if (
array_key_exists( 'attributesToIndex', $settings )
&& is_array( $settings['attributesToIndex'] )
) {
$settings['searchableAttributes'] = array_merge(
$settings['searchableAttributes'],
$settings['attributesToIndex']
);
unset( $settings['attributesToIndex'] );
}

return $settings;
public function get_default_settings() {
// override settings prop to have a custom WP filter hook for terms only
$this->default_settings = apply_filters( 'algolia_terms_index_settings', $this->default_settings, $this->taxonomy );
return parent::get_default_settings();
}

/**
Expand Down
Loading