Skip to content

Commit

Permalink
Release260 (#346)
Browse files Browse the repository at this point in the history
* Watch for post slug changes before saving them (#340)

* Watch for post slug changes before saving them

* Code formatting

* Code formatting

* [Feature-327] Wait for delete operations to complete before updates (#328)

* Wait for delete operations to complete before updates

* Set wait flag to true

* Fix PHPDoc params

* Replace ternary operator with gate condition

* Update JS packages (#341)

* Update algolia search from 4.14.2 to 4.18.0

* Update instantsearch.js from 4.49.1 to 4.56.5

* Update autocomplete package.json

* Update dependencies

* Fix #342 support for fast mode during import via WP All Import (#345)

* Feature 297/post term index (#343)

* Add method to sync posts after updated term

* doc update

* update and add @SInCE tags

---------

Co-authored-by: Michael Beckwith <[email protected]>

* only changing this spot, but make use of array shorthand where able with new changes

* only changing this spot, but make use of array shorthand where able with new changes

* fix since tag

* another shorthand array as we merge in new code

* bump the current branch version

* bump composer and package.json versions

* bump readme tested up to and stable tag

* add changelog

* fix some phpcs reports

* missed extra space

---------

Co-authored-by: Ramsés Del Rosario <[email protected]>
Co-authored-by: Carmine Colicino <[email protected]>
Co-authored-by: Ashar Irfan <[email protected]>
  • Loading branch information
4 people authored Aug 23, 2023
1 parent 9857db5 commit fcab8a4
Show file tree
Hide file tree
Showing 567 changed files with 25,664 additions and 29,120 deletions.
12 changes: 10 additions & 2 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
Contributors: WebDevStudios, williamsba1, tw2113, mrasharirfan, scottbasgaard, gregrickaby, richaber
Tags: search, algolia, autocomplete, instantsearch, relevance search, faceted search, find-as-you-type search, ecommerce, seo, woocommerce, advanced search
Requires at least: 5.0
Tested up to: 6.2.2
Tested up to: 6.3.0
Requires PHP: 7.4
Stable tag: 2.5.4
Stable tag: 2.6.0
License: GNU General Public License v2.0, MIT License

Use the power of Algolia to enhance your website's search. Enable Autocomplete and Instantsearch for fast and accurate results. Control the look, feel, and relevance.
Expand Down Expand Up @@ -125,6 +125,14 @@ All development is handled on [GitHub](https://github.com/WebDevStudios/wp-searc

Follow along with the changelog on [Github](https://github.com/WebDevStudios/wp-search-with-algolia/releases).

= 2.6.0 =
* Added: Support for syncing imported items when "fast mode" from WP All Import enabled.
* Added: Support for updating child posts if parent post's slug has been updated.
* Added: Support for updating posts when an associated term has been updatd.
* Added: Wait for delete operations to complete before moving to updates.
* Updated: Algolia Search library to 4.18.x
* Updated: InstantSearch library to 4.56.x

= 2.5.4 =
* Updated: Ensure reindexing completes when using the from_batch flag with CLI.
* Updated: Assigned Algolia_Admin instance to a property for access elsewhere.
Expand Down
4 changes: 2 additions & 2 deletions algolia.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: WP Search with Algolia
* Plugin URI: https://github.com/WebDevStudios/wp-search-with-algolia
* Description: Integrate the powerful Algolia search service with WordPress
* Version: 2.5.4
* Version: 2.6.0
* Requires at least: 5.0
* Requires PHP: 7.4
* Author: WebDevStudios
Expand All @@ -26,7 +26,7 @@
}

// The Algolia Search plugin version.
define( 'ALGOLIA_VERSION', '2.5.4' );
define( 'ALGOLIA_VERSION', '2.6.0' );

// The minmum required PHP version.
define( 'ALGOLIA_MIN_PHP_VERSION', '7.4' );
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webdevstudios/wp-search-with-algolia",
"version": "2.5.2",
"version": "2.6.0",
"description": "Integrate the powerful Algolia search service with WordPress.",
"authors": [
{
Expand Down
16 changes: 12 additions & 4 deletions includes/indices/class-algolia-posts-index.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ private function update_post_records( WP_Post $post, array $records ) {
// If there are no records, parent `update_records` will take care of the deletion.
// In case of posts, we ALWAYS need to delete existing records.
if ( ! empty( $records ) ) {
$this->delete_item( $post );
$this->delete_item( $post, true );
}

parent::update_records( $post, $records );
Expand Down Expand Up @@ -469,8 +469,9 @@ protected function get_items( $page, $batch_size ) {
* @since 1.0.0
*
* @param mixed $item The item to delete.
* @param bool $wait Wait for the operation to complete synchronously.
*/
public function delete_item( $item ) {
public function delete_item( $item, $wait = false ) {
$this->assert_is_supported( $item );

$records_count = $this->get_post_records_count( $item->ID );
Expand All @@ -479,9 +480,16 @@ public function delete_item( $item ) {
$object_ids[] = $this->get_post_object_id( $item->ID, $i );
}

if ( ! empty( $object_ids ) ) {
$this->get_index()->deleteObjects( $object_ids );
if ( empty( $object_ids ) ) {
return;
}

if ( $wait ) {
$this->get_index()->deleteObjects( $object_ids )->wait();
return;
}

$this->get_index()->deleteObjects( $object_ids );
}

/**
Expand Down
16 changes: 12 additions & 4 deletions includes/indices/class-algolia-searchable-posts-index.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ private function update_post_records( WP_Post $post, array $records ) {
// If there are no records, parent `update_records` will take care of the deletion.
// In case of posts, we ALWAYS need to delete existing records.
if ( ! empty( $records ) ) {
$this->delete_item( $post );
$this->delete_item( $post, true );
}

parent::update_records( $post, $records );
Expand Down Expand Up @@ -456,8 +456,9 @@ public function get_items( $page, $batch_size ) {
* @since 1.0.0
*
* @param mixed $item The item to delete.
* @param bool $wait Wait for the operation to complete synchronously.
*/
public function delete_item( $item ) {
public function delete_item( $item, $wait = false ) {
$this->assert_is_supported( $item );

$records_count = $this->get_post_records_count( $item->ID );
Expand All @@ -466,9 +467,16 @@ public function delete_item( $item ) {
$object_ids[] = $this->get_post_object_id( $item->ID, $i );
}

if ( ! empty( $object_ids ) ) {
$this->get_index()->deleteObjects( $object_ids );
if ( empty( $object_ids ) ) {
return;
}

if ( $wait ) {
$this->get_index()->deleteObjects( $object_ids )->wait();
return;
}

$this->get_index()->deleteObjects( $object_ids );
}

/**
Expand Down
101 changes: 100 additions & 1 deletion includes/watchers/class-algolia-post-changes-watcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,20 @@ class Algolia_Post_Changes_Watcher implements Algolia_Changes_Watcher {
* @author WebDevStudios <[email protected]>
* @since 1.0.0
*
* @var Array
* @var array
*/
private $posts_deleted = array();

/**
* Updated posts array.
*
* @author WebDevStudios <[email protected]>
* @since 2.6.0
*
* @var array
*/
private $posts_updated = [];

/**
* Algolia_Post_Changes_Watcher constructor.
*
Expand Down Expand Up @@ -72,6 +82,11 @@ public function watch() {
add_action( 'add_attachment', array( $this, 'sync_item' ) );
add_action( 'attachment_updated', array( $this, 'sync_item' ) );
add_action( 'delete_attachment', array( $this, 'delete_item' ) );
add_action( 'pre_post_update', [ $this, 'check_slug_update' ], 10, 2 );

// Support for WP All Import "fast mode".
add_action( 'pmxi_saved_post', [ $this, 'track_updated_posts' ] );
add_action( 'pmxi_after_post_import', [ $this, 'sync_item_for_pmxi' ] );
}

/**
Expand Down Expand Up @@ -99,13 +114,53 @@ public function sync_item( $post_id ) {
return;
}

$child_posts = get_transient( 'wp_algolia_child_posts_' . $post_id );

if ( false !== $child_posts ) {
foreach ( $child_posts as $child_post ) {
$this->index->sync( $child_post );
}
delete_transient( 'wp_algolia_child_posts_' . $post_id );
}
try {
$this->index->sync( $post );
} catch ( AlgoliaException $exception ) {
error_log( $exception->getMessage() ); // phpcs:ignore -- Legacy.
}
}

/**
* Check if a slug has been changed and add the childrens into a transient ( if it has children ).
*
* @author WebDevStudios <[email protected]>
*
* @since 2.6.0
*
* @param int $post_id The parent ID.
* @param array $post_data An array containing the new post data.
* @return void
*/
public function check_slug_update( $post_id, $post_data ) {
$post = get_post( (int) $post_id );
if ( isset( $post_data['post_name'] ) && $post_data['post_name'] !== $post->post_name ) {

$child_posts = get_children(
[
'post_parent' => $post_id,
]
);

$pending_childs = [];
foreach ( $child_posts as $child_post ) {
if ( 'inherit' !== $child_post->post_status ) {
$pending_childs[] = $child_post;
}
}
set_transient( 'wp_algolia_child_posts_' . $post_id, $pending_childs, 60 );
}

}

/**
* Delete item.
*
Expand Down Expand Up @@ -154,4 +209,48 @@ public function on_meta_change( $meta_id, $object_id, $meta_key, $meta_value ) {

$this->sync_item( $object_id );
}

/**
* Track updated post ids.
*
* @author WebDevStudios <[email protected]>
* @since 2.6.0
*
* @param int $post_id Post id.
* @return void
*/
public function track_updated_posts( $post_id ) {

// If `save_post` has not been executed, it means "fast mode" of WP All Import is enabled.
if ( in_array( $post_id, $this->posts_updated, true ) ) {
return;
}

$this->posts_updated[] = $post_id;
}

/**
* Sync item to Algolia if "Fast Mode" option is enabled for WP All Import.
*
* @author WebDevStudios <[email protected]>
* @since 2.6.0
*
* @param int $import_id Import id.
* @return void
*/
public function sync_item_for_pmxi( $import_id ) {

$import = new PMXI_Import_Record();
$import->getById( $import_id );

if ( empty( $import->options['is_fast_mode'] ) ) {
return;
}

$post_id = end( $this->posts_updated );

if ( $post_id ) {
$this->sync_item( $post_id );
}
}
}
102 changes: 101 additions & 1 deletion includes/watchers/class-algolia-term-changes-watcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/

use WebDevStudios\WPSWA\Algolia\AlgoliaSearch\Exceptions\AlgoliaException;

/**
* Class Algolia_Term_Changes_Watcher
*
Expand All @@ -27,6 +26,15 @@ class Algolia_Term_Changes_Watcher implements Algolia_Changes_Watcher {
*/
private $index;

/**
* Active Algolia Indices
*
* @author WebDevStudios <[email protected]>
* @since 2.6.0
* @var post_indices
*/
private $post_indices;

/**
* Algolia_Term_Changes_Watcher constructor.
*
Expand All @@ -48,6 +56,7 @@ public function __construct( Algolia_Index $index ) {
public function watch() {
// Fires immediately after the given terms are edited.
add_action( 'edited_term', array( $this, 'sync_item' ) );
add_action( 'edited_term', [ $this, 'sync_term_posts' ], 10, 3 );

// Fires after an object's terms have been set.
add_action( 'set_object_terms', array( $this, 'handle_changes' ), 10, 6 );
Expand All @@ -61,6 +70,97 @@ public function watch() {
add_action( 'delete_term', array( $this, 'on_delete_term' ), 10, 4 );
}

/**
* Check if the current term has post assigned to it, if it does and it supports posts, then sync them.
*
* @since 2.6.0
*
* @param int $term_id The current term to be updated.
* @param int $tt_id The Term Taxonomy ID.
* @param string $taxonomy The taxonomy slug.
*
* @return void
*/
public function sync_term_posts( $term_id, $tt_id, $taxonomy ) {
$term = get_term( (int) $term_id );
if ( ! $term || ! $this->index->supports( $term ) ) {
return;
}
$args = [
'posts_per_page' => -1,
'tax_query' => [
[
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => $term_id,
],
],
];

$posts = get_posts( $args );
$post_types = wp_list_pluck( $posts, 'post_type' );
$post_types = array_unique( $post_types );
$this->post_indices = $this->get_searchable_indexes( $post_types );
$this->sync_posts( $posts );
}

/**
* Returns an array of indexes based on selected post types.
*
* @since 2.6.0
*
* @param array $post_types An array of searchable post_types.
*/
private function get_searchable_indexes( $post_types ) {

$post_indices = [];
$algolia_plugin = \Algolia_Plugin_Factory::create();
$synced_indices_ids = $algolia_plugin->get_settings()->get_synced_indices_ids();
$index_name_prefix = $algolia_plugin->get_settings()->get_index_name_prefix();
$client = $algolia_plugin->get_api()->get_client();
$searchable_post_types = get_post_types(
[
'exclude_from_search' => false,
]
);
$searchable_index = new \Algolia_Searchable_Posts_Index( $searchable_post_types );
$searchable_index->set_name_prefix( $index_name_prefix );
$searchable_index->set_client( $client );
$searchable_index->set_enabled( true );
$post_indices[] = $searchable_index;

foreach ( $post_types as $post_type ) {
$post_index = new \Algolia_Posts_Index( $post_type );
$post_index->set_name_prefix( $index_name_prefix );
$post_index->set_client( $client );
$post_index->set_enabled( true );
$post_indices[] = $post_index;

}
return $post_indices;
}

/**
* Looks for a valid index base on the post type and triggers an Algolia sync.
*
* @since 2.6.0
*
* @param array $posts The post type to look for an index.
*
* @return void
*/
public function sync_posts( $posts ) {
try {
foreach ( $this->post_indices as $index ) {
foreach ( $posts as $post ) {
$index->sync( $post );
}
}
} catch ( AlgoliaException $exception ) {
error_log( $exception->getMessage() ); // phpcs:ignore -- Legacy.
}
}

/**
* Sync item.
*
Expand Down
Loading

0 comments on commit fcab8a4

Please sign in to comment.