From 5229e835a1d523b663488b7d8b63588bb587d60b Mon Sep 17 00:00:00 2001 From: Thor Brink Date: Fri, 24 Nov 2023 09:04:38 +0000 Subject: [PATCH 1/2] perf: reduce number of calls to db By passing already fetched wp_post to internal functions get_post will make less db calls. --- source/php/Bulk.php | 5 ++-- source/php/Index.php | 60 +++++++++++++++++++++++++++++++------------- 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/source/php/Bulk.php b/source/php/Bulk.php index 8ee4c579..58cb6191 100644 --- a/source/php/Bulk.php +++ b/source/php/Bulk.php @@ -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); } } } @@ -85,7 +85,8 @@ public function getPosts($postType) { return get_posts([ 'post_type' => $postType, - 'numberposts' => -1 + 'numberposts' => -1, + 'suppress_filters' => false, ]); } } diff --git a/source/php/Index.php b/source/php/Index.php index 44fdb7cb..336fc850 100644 --- a/source/php/Index.php +++ b/source/php/Index.php @@ -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)) { @@ -92,7 +93,7 @@ public function index($postId) } //Check if is indexable post - if (!self::shouldIndex($postId)) { + if (!self::shouldIndex($post)) { return; } @@ -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); @@ -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) { @@ -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; } @@ -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)]); @@ -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)) { @@ -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)) { @@ -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); @@ -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]; + } } From 3e47b914d6672d7e5386a9e922e1358f724800f4 Mon Sep 17 00:00:00 2001 From: Thor Brink Date: Fri, 24 Nov 2023 09:52:56 +0000 Subject: [PATCH 2/2] test: fix broken test --- source/php/Index.Test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/php/Index.Test.php b/source/php/Index.Test.php index ed835c6d..507fba4b 100644 --- a/source/php/Index.Test.php +++ b/source/php/Index.Test.php @@ -57,7 +57,7 @@ public function testThatShouldIndexIsDisabledByMeta() ); // Then - $this->assertTrue($isIndexable); + $this->assertFalse($isIndexable); } public function testThatASmallRecordIsentTooLarge() {