Skip to content

Instantly share code, notes, and snippets.

<?php
add_filter('get_algolia_index_name', 'get_algolia_index_name');
// Filter to construct canonical index names
function get_algolia_index_name($name = '') {
global $wpdb;
$env_prefix = getenv('ALGOLIA_INDEX_PREFIX') ?: ''; // local, dev, stage, or prod
$base_prefix = $wpdb->base_prefix; // wp_
return "${env_prefix}_${base_prefix}${name}";
{
"hitsPerPage": 20,
"searchableAttributes": [
"unordered(title)",
"unordered(content)",
],
"distinct": true,
"attributesForFaceting": [],
"attributesToSnippet": ["content:10", "title:10"],
"attributesToHighlight": null,
<?php
define('THEME_PATH', get_stylesheet_directory() . '/');
function algolia_get_settings($index_name) {
$settings_file_path = THEME_PATH . 'algolia-json/' . $index_name . '-settings.json';
if (!file_exists($settings_file_path)) {
return false;
}
<?php
public function set_config($args, $assoc_args) {
global $algolia;
$canonical_index_name = apply_filters('algolia_index_name', 'global_search');
$index = $algolia->initIndex($canonical_index_name);
// Set index settings if '--settings' flag exists
if (isset($assoc_args['settings'])) {
<?php
add_action('save_post', 'algolia_save_post', 10, 3);
function algolia_save_post($id, $post, $update) {
global $algolia;
$post_type = $post->post_type;
$post_status = $post->post_status;
<?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) {
<?php
public function reindex($args, $assoc_args) {
// Init global index
$index = $algolia->initIndex('global_search');
// Clear all objects in the index
$index->clearObjects()->wait();
// Get all blog IDs in multisite network
<?php
if (! (defined('WP_CLI') && WP_CLI)) {
return;
}
class Algolia_Command {
public function reindex($args, $assoc_args) {
...
}
@bchiang7
bchiang7 / event-bus.js
Last active January 11, 2020 05:13
Simple event bus in plain JS
// Inspired by Vue: https://alligator.io/vuejs/global-event-bus/
// Sending events: EventBus.emit('clicky', clickArg);
// Receiving events: EventBus.on('clicky', clickArg => console.log(clickArg));
const EventBus = {
events: {},
emit(event, data) {
if (!this.events[event]) {
return;
}
#!/bin/bash
bold=$(tput bold)
normal=$(tput sgr0)
set -e
echo "-----------------------------------------------"
echo " 🚀 Creating a new release 🚀"
echo "-----------------------------------------------"