Created
March 26, 2020 21:38
-
-
Save bchiang7/4ef2b05b8ca8e0e60037ccb0d41c0c4f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?php | |
class AlgoliaSerializer { | |
public function run() { | |
add_filter('post_to_record', array($this, 'post_to_record')); | |
... | |
} | |
// Converts a Post to an Algolia record | |
function post_to_record($post) { | |
$tags = $this->getTermNames($post, 'post_tag'); | |
$post_record_attrs = [ | |
'introduction' => get_field('introduction', $post->ID), | |
'tags' => $tags, | |
]; | |
return $this->serializeRecord($post, $post_record_attrs); | |
} | |
// Handles formatting record attributes and splitting large content | |
function serializeRecord($post, $post_attrs = []) { | |
$blog_id = get_current_blog_id(); | |
$records = []; | |
// Split records on post_content | |
$records = $this->splitContent('content', $post->post_content); | |
// Merge all attributes for each split record and add a unique objectID | |
foreach ($records as $key => $split) { | |
$records[$key] = array_merge( | |
[ | |
'objectID' => implode('#', [$blog_id, $post->post_type, $post->ID]), | |
'distinct_key' => implode('#', [$blog_id, $post->post_type, $post->ID]), | |
'blog_id' => $blog_id, | |
'type' => $post->post_type, | |
'title' => $post->post_title, | |
'date' => $post->post_date, | |
'url' => get_permalink($post->ID), | |
], | |
$post_attrs, | |
$split, | |
['objectID' => implode('-', [$blog_id, $post->post_type, $post->ID, $key])] | |
); | |
}; | |
return $records; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment