/
nginx-helper-purge-for-editors.php
122 lines (91 loc) · 3.03 KB
/
nginx-helper-purge-for-editors.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
<?php
add_action( 'admin_init' , 'refresh_admin_after_purge' , 9999 );
function refresh_admin_after_purge() {
global $wp;
$custom = false;
$method = filter_input( INPUT_SERVER, 'REQUEST_METHOD', FILTER_SANITIZE_STRING );
if ( 'POST' === $method ) {
$action = filter_input( INPUT_POST, 'nginx_helper_action', FILTER_SANITIZE_STRING );
} else {
$action = filter_input( INPUT_GET, 'nginx_helper_action', FILTER_SANITIZE_STRING );
}
if( $action == null ) {
if ( 'POST' === $method ) {
$action = filter_input( INPUT_POST, 'nginx_custom_action', FILTER_SANITIZE_STRING );
} else {
$action = filter_input( INPUT_GET, 'nginx_custom_action', FILTER_SANITIZE_STRING );
}
$custom = true;
}
if ( empty( $action ) ) {
return;
}
if ( 'done' === $action && is_admin() ) {
if( $custom ) {
add_action( 'admin_notices', 'purge_display_notices' );
add_action( 'network_admin_notices', 'purge_display_notices' );
return;
}
$req= remove_query_arg( array_keys( $_GET ), $wp->request );
$redirect_url = add_query_arg( 'nginx_custom_action', 'done', $req );
wp_redirect( esc_url_raw( $redirect_url ) );
exit;
}
}
add_filter( 'user_has_cap', 'let_editors_purge', 9999, 3 );
function let_editors_purge( $allcaps, $caps, $args ){
// Bail out for users who can already purge:
if ( $allcaps['manage_options'] )
return $allcaps;
// Bail out for users who can't publish posts:
if ( !isset( $allcaps['publish_posts'] ) or !$allcaps['publish_posts'] )
return $allcaps;
if( isset( $_GET['nginx_helper_action'] ) && isset( $_GET['nginx_helper_urls'] ) ) {
$allcaps['manage_options'] = true;
}
return $allcaps;
}
function purge_display_notices() {
echo '<div class="updated"><p>' . esc_html__( 'Purge initiated', 'nginx-helper' ) . '</p></div>';
}
add_action( 'admin_bar_menu', 'admin_bar_add_nginx_helper_purge_button_for_editors', 80 );
function admin_bar_add_nginx_helper_purge_button_for_editors() {
global $wp_admin_bar;
// https://wordpress.org/plugins/nginx-helper/
// check if plugin nginx helper is active
if( ! is_plugin_active('nginx-helper/nginx-helper.php')) {
return;
}
// check if shop_manager or editor
//if( ! current_user_can('editor') && ! current_user_can('shop_manager')) {
// return;
//}
if( ! current_user_can('publish_posts') ) {
return;
}
//if ( !isset( $allcaps['publish_posts'] ) or !$allcaps['publish_posts'] )
// return $allcaps;
if ( is_admin() ) {
$nginx_helper_urls = 'all';
$link_title = __( 'Purge Cache', 'kick-off-wp' );
} else {
$nginx_helper_urls = 'current-url';
$link_title = __( 'Purge Cache Page', 'kick-off-wp' );
}
$purge_url = add_query_arg(
array(
'nginx_helper_action' => 'purge',
'nginx_helper_urls' => $nginx_helper_urls,
)
);
$nonced_url = wp_nonce_url( $purge_url, 'nginx_helper-purge_all' );
$args = array(
'id' => 'kowp_purge_nginx_helper_cache',
'title' => $link_title,
'href' => $nonced_url,
'meta' => array(
'title' => $link_title,
),
);
$wp_admin_bar->add_menu( $args );
}