WebDevStudios / wp-search-with-algolia

Improve search on your site. Autocomplete is included, along with full control over look, feel and relevance.
https://wordpress.org/plugins/wp-search-with-algolia/
141 stars 54 forks source link

Add support for WP All Import when "fast mode" is enabled. #342

Closed tw2113 closed 1 year ago

tw2113 commented 1 year ago

In step 4 of WP All Import's import process, they have a "Increase speed by disabling do_action calls in wp_insert_post during import." option in the "Configure Advanced Settings" panel. This removes a lot of the actions that occur as part of a wp_insert_post() call, including save_post. This is an important detail because that's the hook that WP Search with Algolia listens to, to trigger a rebuild of a given post's index object.

I found the following hook from WP All Import's record processing method:

do_action( 'pmxi_saved_post', $pid, $rootNodes[$i], $is_update );

This is run after the long long amount of checks it does, and the $pid variable is going to be the post ID of the imported post. This is true regardless of new post or updating an existing post with All Import data.

The $rootNodes I believe is just an array of items being processed and $i would be the current array index. Lastly the $is_update is just a variable saying whether it's a new post or an updated one.

Let's add in a new callback that does the following, as much as possible:

  1. We should NOT need an early return if All Import isn't active, because the action wouldn't be registered in the first place.
  2. Get a post object from the provided post ID.
  3. Set up applicable Algolia indexes that the given post should be included in.
  4. Perform a sync with the object.

Below is an example snippet of code that could be modeled and used to set up the indices. This is not perfect code to be copy/pasted, just a starting example. It is adapted from WP Search with Algolia Pro, so some of the code methods may be able to be replaced with code more readily available via other methods.

$searchable_post_types = get_post_types(
    [
        'exclude_from_search' => false,
    ],
);
$indices[]             = new \Algolia_Searchable_Posts_Index( $searchable_post_types );

$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();

foreach ( $indices as $index ) {
    $index->set_name_prefix( $index_name_prefix );
    $index->set_client( $client );

    if ( in_array( $index->get_id(), $synced_indices_ids, true ) ) {
        $index->set_enabled( true );
    }
}

$thepost = get_post( $pid );
foreach ( $indices as $index ) {
    if ( ! $index->supports( $thepost ) ) {
        continue;
    }

    try {
        $index->sync( $thepost );
    } catch ( AlgoliaException $exception ) {
        error_log( $exception->getMessage() ); // phpcs:ignore -- Legacy.
    }
}
asharirfan commented 1 year ago

@tw2113

Just leaving my notes in the ticket for what I have got so far.

I have been working on checking if the "Fast Mode" is enabled while the plugin is importing posts.

It looks like the hook pmxi_saved_post contains information about the post, like you mentioned above. However, pmxi_after_post_import contains the import id that we need to check if the "fast mode" is enabled for the import process.

This hook runs right after the first one. So we will need to come up with a way to work around these two hooks for this to work.

Work in progress on this issue.

tw2113 commented 1 year ago

Fixed and merged with https://github.com/WebDevStudios/wp-search-with-algolia/pull/345