Skip to content

Commit

Permalink
Merge pull request #11 from helsingborg-stad/perf/wp_post_vs_id
Browse files Browse the repository at this point in the history
perf: reduce number of calls to db
  • Loading branch information
Anna authored Dec 18, 2023
2 parents 64f738f + 3e47b91 commit 22c57f0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 21 deletions.
5 changes: 3 additions & 2 deletions source/php/Bulk.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function build($args, $assocArgs)
$post = $postToIndex;

\WP_CLI::log("Indexing '" . $postToIndex->post_title . "' of posttype " . $postType);
do_action('AlgoliaIndex/IndexPostId', $postToIndex->ID);
do_action('AlgoliaIndex/IndexPostId', $postToIndex);
}
}
}
Expand All @@ -85,7 +85,8 @@ public function getPosts($postType)
{
return get_posts([
'post_type' => $postType,
'numberposts' => -1
'numberposts' => -1,
'suppress_filters' => false,
]);
}
}
2 changes: 1 addition & 1 deletion source/php/Index.Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function testThatShouldIndexIsDisabledByMeta()
);

// Then
$this->assertTrue($isIndexable);
$this->assertFalse($isIndexable);
}

public function testThatASmallRecordIsentTooLarge() {
Expand Down
60 changes: 42 additions & 18 deletions source/php/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,15 @@ public function delete($postId, $isSplitRecord = false)
/**
* Submit post to index
*
* @param int $postId
* @param int|WP_Post $post
* @return void
*/
public function index($postId)
public function index($post)
{
list($post, $postId) = self::getPostAndPostId($post);

//Check if post should be removed
$shouldPostBeRemoved = [isset($_POST['exclude-from-search']) && $_POST['exclude-from-search'] == "true", get_post_status($postId) !== 'publish'];
$shouldPostBeRemoved = [isset($_POST['exclude-from-search']) && $_POST['exclude-from-search'] == "true", get_post_status($post) !== 'publish'];

if(in_array(true, $shouldPostBeRemoved)) {
if ($isSplitRecord = self::isSplitRecord($postId)) {
Expand All @@ -92,7 +93,7 @@ public function index($postId)
}

//Check if is indexable post
if (!self::shouldIndex($postId)) {
if (!self::shouldIndex($post)) {
return;
}

Expand All @@ -107,7 +108,7 @@ public function index($postId)
}

//Get post data
$post = self::getPost($postId);
$post = self::getPost($post);

//Sanity check (convert data)
$post = _wp_json_sanity_check($post, 10);
Expand Down Expand Up @@ -138,11 +139,12 @@ public function index($postId)
/**
* Determine if the post should be indexed.
*
* @param int $post
* @param int|WP_Post $post
* @return boolean
*/
private static function shouldIndex($post)
{
list($post, $postId) = self::getPostAndPostId($post);

//Do not index on autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE == true) {
Expand All @@ -165,12 +167,12 @@ private static function shouldIndex($post)
}

//Do not index checkbox
if(get_post_meta($post, 'exclude_from_search', true)) {
if(get_post_meta($postId, 'exclude_from_search', true)) {
return false;
}

//Anything else
if (!apply_filters('AlgoliaIndex/ShouldIndex', true, $post)) {
if (!apply_filters('AlgoliaIndex/ShouldIndex', true, $postId)) {
return false;
}

Expand All @@ -180,11 +182,12 @@ private static function shouldIndex($post)
/**
* Check if record in algolia matches locally stored record.
*
* @param int $postId
* @param int|WP_Post $post
* @return boolean
*/
private static function hasChanged($postId)
private static function hasChanged($post)
{
list($post, $postId) = self::getPostAndPostId($post);

//Make search
$response = (object) Instance::getIndex()->getObjects([Id::getId($postId)]);
Expand All @@ -197,7 +200,7 @@ private static function hasChanged($postId)
}

//Get stored record
$storedRecord = self::getPost($postId);
$storedRecord = self::getPost($post);

//Check for null responses, update needed
if (is_null($indexRecord) || is_null($storedRecord)) {
Expand Down Expand Up @@ -248,15 +251,17 @@ private static function streamlineRecord($record)
/**
* Get post by ID
*
* @param int $postId
* @param int|WP_Post $post
* @return array
*/
private static function getPost($postId)
private static function getPost($post)
{
if ($post = get_post($postId)) {
list($post, $postId) = self::getPostAndPostId($post);

if ($post = get_post($post)) {

/* Tags */
$taxonomies = get_post_taxonomies($postId, 'names');
$taxonomies = get_post_taxonomies($post, 'names');
$tags = [];

if(is_array($taxonomies) && !empty($taxonomies)) {
Expand Down Expand Up @@ -317,10 +322,12 @@ private static function getPost($postId)

public function getTheExcerpt($post, int $numberOfWords = 55) {

$excerpt = get_the_excerpt($post);
$excerpt = get_the_excerpt($post);

if(empty($excerpt) || strlen($excerpt) > 10) {
$excerpt = $post->post_content;
if (empty($excerpt) || strlen($excerpt) > 10) {
$excerpt = !empty($post->post_content)
? $post->post_content
: $excerpt;
}

$blocks = parse_blocks($excerpt);
Expand Down Expand Up @@ -424,4 +431,21 @@ private static function isSplitRecord($postId)

return false;
}

/**
* Get post and post id
*
* @param int|WP_Post $post
* @return array [WP_Post, int] or [int, int] depending on input.
*/
private static function getPostAndPostId($post)
{
$postId = $post;

if (is_a($post, 'WP_Post')) {
$postId = $post->ID;
}

return [$post, $postId];
}
}

0 comments on commit 22c57f0

Please sign in to comment.