-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdefault-wp-rocket-varnish.php
85 lines (71 loc) · 1.65 KB
/
default-wp-rocket-varnish.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
<?php
/**
* Plugin Name: Be API - WP Rocket handles Varnish
* Description: Tweaks config for Varnish flush cache
* Version: 1.0
* Author: BeAPI
* Author URI: http://beapi.fr/
*/
namespace BEAPI\Plugin_Defaults\WP_Rocket_Varnish;
add_filter( 'rocket_varnish_http_purge_scheme', __NAMESPACE__ . '\\purge_rocket_varnish_scheme' );
/**
* Force HTTP.
*/
function purge_rocket_varnish_scheme() {
return 'http';
}
add_filter( 'rocket_varnish_ip', __NAMESPACE__ . '\\set_custom_varnish_ip' );
/**
* Return custom Varnish IP
*
* @param (array) $ips Array containing custom Varnish IP's
*
* @return array
*
* @author Arun Basil Lal
*
*/
function set_custom_varnish_ip( $ips ) {
if ( ! is_array( $ips ) ) {
$ips = (array) $ips;
}
if ( ! defined( 'VARNISH_IPS' ) ) {
return $ips;
}
$env_ips = constant( 'VARNISH_IPS' );
$env_ips = array_filter( explode( ',', $env_ips ) );
if ( empty( $env_ips ) ) {
return $ips;
}
foreach ( $env_ips as $env_ip ) {
$ips[] = $env_ip . ':6081'; // Append custom port for each IP
}
return $ips;
}
/**
* Disable page caching in WP Rocket.
*
* Varnish handles cache
*
* @link http://docs.wp-rocket.me/article/61-disable-page-caching
*/
add_filter(
'do_rocket_generate_caching_files',
function () {
return empty( set_custom_varnish_ip( [] ) );
}
);
/***
* Enable features by default
* @see https://docs.wp-rocket.me/article/1561-programmatically-toggle-wp-rocket-options-under-specific-conditions
*/
// Enable varnish purge auto
add_filter(
'pre_get_rocket_option_varnish_auto_purge',
function ( $active ) {
if ( ! empty( set_custom_varnish_ip( [] ) ) ) {
return true;
}
return $active;
}
);