-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-algolia-search-addons.php
97 lines (83 loc) · 3.35 KB
/
wp-algolia-search-addons.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/*
* Plugin Name: WP Algolia Search Addons
* Description: Addons for WP Search with Algolia
* Version: 0.1.0
* Requires at least: 5.0
* Requires PHP: 7.4
* Author: Craftweeks
* Author URI: https://craftweeks.com
* License: MIT License
* Text Domain: wp-algolia-search-addons
* Domain Path: /languages
* Requires Plugins: polylang, wp-search-with-algolia
*/
// Define the path to the Algolia addons directory if not already defined.
if (!defined('ALGOLIA_ADDONS_PATH')) {
define(
'ALGOLIA_ADDONS_PATH',
__DIR__ . '/'
);
}
// Define the path to the Algolia addons templates directory if not already defined.
if (!defined('ALGOLIA_ADDONS_PATH_TEMPLATE_PATH')) {
define(
'ALGOLIA_ADDONS_PATH_TEMPLATE_PATH',
ALGOLIA_ADDONS_PATH . 'templates/'
);
}
// Function to load the autocomplete template.
function load_autocomplete_template($location, $file)
{
// Check if the file is either 'autocomplete.php' or 'instantsearch.php'.
if (!in_array($file, ['autocomplete.php', 'instantsearch.php'])) {
return $location;
}
// Return the path to the template file.
return ALGOLIA_ADDONS_PATH_TEMPLATE_PATH . $file;
}
add_filter('algolia_custom_template_location', 'load_autocomplete_template', 11, 2);
// Add the locale of every post to every record of every post type indexed.
function add_locales_to_records(array $attrs, WP_Post $post)
{
// Check if the Polylang function exists and add the locale to the attributes.
if (function_exists('pll_get_post_language')) {
$attrs['locale'] = pll_get_post_language($post->ID, "locale");
}
return $attrs;
}
add_filter('algolia_post_shared_attributes', 'add_locales_to_records', 10, 2);
add_filter('algolia_searchable_post_shared_attributes', 'add_locales_to_records', 10, 2);
// Register the locale attribute as an Algolia facet which will allow us to filter on the currently displayed locale.
function add_locale_to_facets(array $settings)
{
// Add 'locale' to the attributes for faceting.
$settings['attributesForFaceting'][] = 'locale';
return $settings;
}
add_filter('algolia_searchable_posts_index_settings', 'add_locale_to_facets');
add_filter('algolia_posts_index_settings', 'add_locale_to_facets');
add_filter('algolia_terms_index_settings', 'add_locale_to_facets');
// Expose the current locale of the displayed page in JavaScript.
function enqueue_locale() {
// Get and sanitize the current locale.
$current_locale = sanitize_text_field( get_locale() );
// Add the current locale as an inline script before the 'algolia-search' script.
wp_add_inline_script( 'algolia-search', sprintf('var current_locale = "%s";', $current_locale), 'before' );
}
add_action('wp_enqueue_scripts', 'enqueue_locale', 99);
// Include the admin page
function add_plugin_menu() {
add_options_page(
esc_html__( 'Algolia Search Addons', 'wp-algolia-search-addons' ),
esc_html__( 'Algolia Search Addons', 'wp-algolia-search-addons' ),
'manage_options',
'algolia-addons',
'algolia_addons_settings_page' // Function to display the page content
// The position in the menu order
);
}
add_action('admin_menu', 'add_plugin_menu');
function algolia_addons_settings_page() {
include_once plugin_dir_path( __FILE__ ) . 'includes/admin/partials/page-settings.php';
}