-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
151 lines (126 loc) · 3.78 KB
/
index.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
namespace WPPerformance\Search;
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/src/SearchClient.php';
require_once __DIR__ . '/src/wp-cli.php';
use WPPerformance\Search\Inc\Search_Client;
/**
* Plugin Name: WP Performance Search Meilisearch
* Description: Add Meilisearch Search feature
* Text Domain: wp-performance-meilisearch
* Version: 1.0.0
*/
if (!defined('WP_ENV')) {
define('WP_ENV', 'development');
}
/**
* get current post types
*/
function getPostTypes()
{
return ['post', 'page', 'snippet'];
}
/**
* get meta keys autorized for meilisearch
*/
function getMetaKeys()
{
// exemple for seo press
return ['_seopress_titles_title', '_seopress_titles_desc'];
}
/**
* get index name for meilisearch
* add prefix env
*/
function wp_perf_search_index_name($defaultName = 'content')
{
return WP_ENV . '_' . $defaultName;
}
add_action(
'wp_footer',
function () {
echo '<script type="text/javascript">'
. 'var MEILISEARCH_URL = "' . MEILISEARCH_URL . '";'
. 'var MEILISEARCH_KEY_PUBLIC = "' . MEILISEARCH_KEY_PUBLIC . '";'
. 'var MEILISEARCH_APP_INDEX = "' . namespace\wp_perf_search_index_name() . '";'
. '</script>';
}
);
// init keys for search
Search_Client::initKeys(MEILISEARCH_URL, MEILISEARCH_KEY_SECRET);
/**
* map data
*/
function wp_perf_post_to_record($post)
{
/** add tags */
$tags = array_map(function (\WP_Term $term) {
return $term->name;
}, wp_get_post_terms($post->ID, 'post_tag'));
/** add cat */
$cat = array_map(function (\WP_Term $term) {
return $term->name;
}, wp_get_post_terms($post->ID, 'category'));
return [
'id' => $post->ID,
'title' => $post->post_title,
'author' => [
'id' => $post->post_author,
'name' => get_user_by('ID', $post->post_author)->display_name,
],
'excerpt' => html_entity_decode(get_the_excerpt($post)),
'content' => html_entity_decode(strip_tags($post->post_content)),
'tags' => $tags,
'categories' => $cat,
'url' => get_permalink($post->ID),
'custom_field' => get_post_meta($post->id, $post->custom_type),
];
}
/** same for all but you can change by post type */
// add_filter('post_to_record', __NAMESPACE__ . '\wp_perf_post_to_record');
// add_filter('snippet_to_record', __NAMESPACE__ . '\wp_perf_post_to_record');
// add_filter('page_to_record', __NAMESPACE__ . '\wp_perf_post_to_record');
/**
* hook meta update
*/
function wp_perf_update_post_meta($meta_id, $object_id, $meta_key, $_meta_value)
{
$search = Search_Client::getInstance();
if (in_array($meta_key, namespace\getMetaKeys())) {
$index = $search->index(
namespace\wp_perf_search_index_name()
);
$index->updateDocuments([
'id' => $object_id,
$meta_key => $_meta_value,
]);
}
}
add_action('update_post_meta', __NAMESPACE__ . '\wp_perf_update_post_meta', 10, 4);
/**
* hook post update or create
*/
function wp_perf_update_post($id, \WP_Post $post, $update)
{
if (wp_is_post_revision($id) || wp_is_post_autosave($id)) {
return $post;
}
if (!in_array($post->post_type, namespace\getPostTypes())) {
return $post;
}
$search = Search_Client::getInstance();
$record = (array) wp_perf_post_to_record($post);
if (!isset($record['id'])) {
$record['id'] = $post->ID;
}
$index = $search->index(
namespace\wp_perf_search_index_name()
);
if ('trash' == $post->post_status) {
$index->deleteDocument($record['id']);
} else {
$index->updateDocuments([$record]);
}
return $post;
}
add_action('save_post', __NAMESPACE__ . '\wp_perf_update_post', 10, 3);