-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
65 lines (55 loc) · 1.87 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
<?php
@include_once __DIR__ . "/vendor/autoload.php";
use KirbyAlgolia\Index;
$settings = option("mlbrgl.kirby-algolia");
$hook_update_page = function ($newPage, $oldPage) use ($settings) {
if (Index::is_page_indexable($oldPage, $settings)) {
kirby_algolia_delete_page($oldPage, $settings);
}
if (Index::is_page_indexable($newPage, $settings)) {
kirby_algolia_create_update_page($newPage, $settings);
}
};
$hook_delete_page = function ($status, $page) use ($settings) {
if (Index::is_page_indexable($page, $settings)) {
kirby_algolia_delete_page($page, $settings);
}
};
\Kirby::plugin("mlbrgl/kirby-algolia", [
"hooks" => [
"page.update:after" => $hook_update_page,
"page.changeTitle:after" => $hook_update_page,
"page.changeStatus:after" => $hook_update_page,
"page.changeSlug:after" => $hook_update_page,
"page.changeTemplate:after" => $hook_update_page,
"page.delete:after" => $hook_delete_page,
// "page.create:after" => not necessary, pages are created as drafts
// "page.duplicate:after" => not necessary, pages are duplicated as drafts
// "page.changeNum:after" => not necessary, does not affect indexed data
],
]);
/**
* Master parsing and indexing function for a single page.
*
* @param <type> $page The page being indexed
* @param <type> $settings The settings
*/
function kirby_algolia_create_update_page($page, $settings)
{
$index = new \KirbyAlgolia\Index($settings);
$parser = new \KirbyAlgolia\Parser($settings);
$fragments = $parser->parse($page);
$index->send_fragments_algolia($fragments);
}
/**
* Deletes records relating to a single page
*
* @param <type> $page The page
* @param <type> $settings The settings
*/
function kirby_algolia_delete_page($page, $settings)
{
$index = new \KirbyAlgolia\Index($settings);
$index->delete_fragments_algolia($page->id());
}
?>