-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
9857db5
commit fcab8a4
Showing
567 changed files
with
25,664 additions
and
29,120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
* | ||
|
@@ -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' ] ); | ||
} | ||
|
||
/** | ||
|
@@ -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. | ||
* | ||
|
@@ -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 ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,6 @@ | |
*/ | ||
|
||
use WebDevStudios\WPSWA\Algolia\AlgoliaSearch\Exceptions\AlgoliaException; | ||
|
||
/** | ||
* Class Algolia_Term_Changes_Watcher | ||
* | ||
|
@@ -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. | ||
* | ||
|
@@ -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 ); | ||
|
@@ -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. | ||
* | ||
|
Oops, something went wrong.