-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgolia-bogo.php
75 lines (68 loc) · 2.55 KB
/
algolia-bogo.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
<?php
/**
* Plugin Name: Search with Algolia Bogo extension
* Plugin URI: https://wp-kyoto.net
* Description: Simply extension of Bogo and WP Search with Algolia. Put locale attributes into the indices.
* Author: Hidetaka Okamoto
* Author URI: https://wp-kyoto.net/en
* Version: 0.1.0
*
* @package Algolia_Bogo
*/
class Algolia_Bogo {
private $locale_attribute_name = 'locale';
function __construct() {
add_filter( 'algolia_post_shared_attributes', array( $this, "put_bogo_attributes" ), 10, 2);
add_filter( 'algolia_searchable_post_shared_attributes', array( $this, "put_bogo_attributes" ), 10, 2);
add_filter( 'algolia_posts_index_settings', array( $this, 'put_index_settings' ), 10, 2 );
add_filter( 'algolia_searchable_posts_index_settings', array( $this, 'put_index_settings' ), 10, 1 );
}
public function put_index_settings( $settings, $post_type = 'searchable_posts' ) {
if ( ! in_array( $post_type, $this->get_allowed_post_types() ) ) {
return $settings;
}
array_push( $settings['attributesForFaceting'], $this->locale_attribute_name );
array_push( $settings['attributesToIndex'], 'unordered(' .$this->locale_attribute_name . ')' );
error_log( json_encode( $settings ) );
return $settings;
}
/**
* Get the locale attribtues or default locale setting from bogo
*/
public function get_the_post_locale ( $post ) {
$locales = get_post_meta( $post->ID, "_locale" );
if ( empty( $locales ) ) {
if ( function_exists( 'bogo_get_default_locale' ) ) {
return bogo_get_default_locale();
}
return null;
}
$locale = $locales[0];
return $locale;
}
/**
* Supported post types
*/
public function get_allowed_post_types() {
return apply_filters( 'algolia_bogo_allower_post_type', array(
'post',
'page',
'searchable_posts',
) );
}
/**
* Put attributes
*/
public function put_bogo_attributes( $shared_attributes, $item ) {
$allowed_posts = $this->get_allowed_post_types();
if ( ! in_array( $item->post_type , $allowed_posts, true ) ) {
return $shared_attributes;
}
$locale = $this->get_the_post_locale( $item );
if ( $locale && $locale !== null ) {
$shared_attributes[ $this->locale_attribute_name ] = $locale;
}
return $shared_attributes;
}
}
new Algolia_Bogo();