-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneric-purges.php
163 lines (139 loc) · 3.16 KB
/
generic-purges.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
152
153
154
155
156
157
158
159
160
161
162
163
<?php
/**
* Generic purge functions.
*
* @since 1.0
*/
/**
* Send request to the given $uri in the specified $method.
*
* @since 1.0
*/
function vh_generic_purge($uri, $method) {
$blog_url = get_bloginfo('url');
if ($_SERVER['HTTPS'] == 'on' && stripos($blog_url, 'https') == FALSE) {
$blog_url = str_ireplace('http', 'https', $blog_url);
}
return wp_remote_request($blog_url . $uri, array('method' => $method));
}
/**
* Send PURGE request to the given $uri.
*
* @since 1.0
*/
function vh_purge($uri) {
vh_generic_purge($uri, 'PURGE');
}
/**
* Send BAN request to the given $uri.
*
* @since 1.0
*/
function vh_ban($uri) {
vh_generic_purge($uri, 'BAN');
}
/**
* Purge the common object: '/', '/feed/', '/feed/atom/', '/page/.*'.
*
* @since 1.0
*/
function vh_purge_common() {
vh_purge('/');
vh_purge('/feed/');
vh_purge('/feed/atom/');
vh_ban('/page/.*');
}
/**
* Purge the post with the give post id.
*
* @since 1.0
*/
function vh_purge_post($post_id) {
$post = get_post($post_id);
if (!wp_is_post_revision($post)) {
$permalink = get_permalink($post_id);
$uri = str_replace(get_bloginfo('url'), '', $permalink);
vh_purge($uri);
vh_purge($uri . 'feed/');
}
}
/**
* Purge pages of the archive that the post of the given post id within.
*
* @since 1.0
*/
function vh_purge_archive($post_id) {
$slug = get_the_time('/Y/m', $post_id);
$uri = $slug . '/.*';
vh_ban($uri);
}
/**
* Purge pages of the author of the post with the given post id.
*
* @since 1.0
*/
function vh_purge_author($post_id) {
$post = get_post($post_id);
$author_id = $post->post_author;
$author_posts_link = get_author_posts_url($author_id);
$uri = str_replace(get_bloginfo('url'), '', $author_posts_link) . '.*';
vh_ban($uri);
}
/**
* Purge pages of the category of the post with the given post id.
*
* @since 1.0
*/
function vh_purge_category($post_id) {
$categories = get_the_category($post_id);
foreach ($categories as $category) {
$uri = '/category/' . $category->slug . '/.*';
vh_ban($uri);
}
}
/**
* Purge pages of the tag of the post with the given post id.
*
* @since 1.0
*/
function vh_purge_tag($post_id) {
$tags = get_the_tags($post_id);
foreach ($tags as $tag) {
$uri = '/tag/' . $tag->slug . '/.*';
vh_ban($uri);
}
}
/**
* Purge the related pages of the post with the given post id.
*
* @since 1.0
*/
function vh_purge_post_related_pages($post_id) {
vh_purge_archive($post_id);
vh_purge_author($post_id);
vh_purge_category($post_id);
vh_purge_tag($post_id);
}
/**
* Purge ESI sidebar.
*
* @since 1.0
*/
function vh_purge_esi_sidebar() {
$url = plugin_dir_url(__FILE__) . 'esi-sidebar.php';
$blog_url = get_bloginfo('url');
if ($_SERVER['HTTPS'] == on && stripos($blog_url, 'https') == FALSE) {
$blog_url = str_ireplace('http', 'https', $blog_url);
}
$uri = str_replace($blog_url, '', $url);
vh_purge($uri);
}
/**
* Purge the whole site.
*
* @since 1.0
*/
function vh_purge_all() {
vh_ban('/.*');
}
?>