This repository was archived by the owner on Sep 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.php
104 lines (77 loc) · 2.46 KB
/
main.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
<?php
/*
Plugin Name: CacheCleaner
Description: Takes care of clearing the permanantly cached items when necessary.
Author: Jen Wachter
Version: 0.1
*/
use \Secrets\Secret;
class CacheCleanerMain
{
protected $logger;
protected $gearmanClient;
public function __construct($logger)
{
$this->logger = $logger;
$this->setupGearmanClient();
$this->addHooks();
}
/**
* Sets up the Gearman client, adding only
* the admin server.
*/
protected function setupGearmanClient()
{
$this->gearmanClient = new \GearmanClient();
$servers = Secret::get("jhu", ENV, "servers");
if (!$servers) {
$wp_logger->addCritical("Servers unavailable for Gearman " . __FILE__ . " on line " . __LINE__);
}
// add admin server only
$server = array_shift($servers);
$this->gearmanClient->addServer($server->hostname);
}
protected function addHooks()
{
// posts
add_action("save_post", array($this, "savedPost"));
// if trash is turned off, add a hook to take care of deleted posts.
// Otherwise, deleted posts are treated with save_post as a status change
if (defined("EMPTY_TRASH_DAYS") && EMPTY_TRASH_DAYS == 0) {
add_action("deleted_post", array($this, "deletedPost"));
}
// menu
add_action("wp_update_nav_menu", function () {
$this->gearmanClient->doHighBackground("api_clear_cache", json_encode(array(
"endpoint" => "menus"
)));
});
}
public function savedPost($id)
{
$post = get_post($id);
// do nothing if this is just an auto draft
if ($post->post_status == "auto-draft") return;
if (in_array($post->post_type, array("field_of_study", "search_response"))) {
$workload = array();
if ($post->post_status == "publish") {
// only clear the ID if the post if published
$workload["id"] = $id;
}
// always clear the endpoint (removes posts reverted to draft status)
$workload["endpoint"] = "program-explorer";
$this->gearmanClient->doHighBackground("api_clear_cache", json_encode($workload));
}
}
public function deletedPost($id)
{
$post = get_post($id);
if (in_array($post->post_type, array("field_of_study", "search_response"))) {
// remove this post from the program explorer
$this->gearmanClient->doHighBackground("api_clear_cache", json_encode(array(
"endpoint" => "program-explorer"
)));
}
}
}
$jhu_cache_clearer = new CacheCleanerMain($wp_logger);