Skip to content

Commit 81d9b24

Browse files
committed
New GoDaddy compatibility
1 parent d843c5f commit 81d9b24

File tree

1 file changed

+130
-42
lines changed

1 file changed

+130
-42
lines changed

inc/3rd-party/hosting/godaddy.php

Lines changed: 130 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,136 @@
1-
<?php
1+
<?php
22
defined( 'ABSPATH' ) or die( 'Cheatin\' uh?' );
33

4-
if ( class_exists( 'GD_System_Plugin_Cache_Purge' ) ) :
5-
6-
/**
7-
* Clear WP Rocket cache after purged the Varnish cache via GoDaddy Hosting
8-
*
9-
* @since 2.6.5
10-
*
11-
* @return void
12-
*/
13-
add_action( 'init', '__rocket_clear_cache_after_godaddy', 0 );
14-
function __rocket_clear_cache_after_godaddy() {
15-
if ( ! isset( $_REQUEST['GD_COMMAND'] ) || $_REQUEST['GD_COMMAND'] != 'FLUSH_CACHE' ) {
16-
return;
4+
if ( class_exists( 'WPaaS\Plugin' ) ) :
5+
6+
add_filter( 'rocket_display_varnish_options_tab', '__return_false' );
7+
add_filter( 'set_rocket_wp_cache_define', '__return_true' );
8+
9+
/**
10+
* Remove expiration on HTML to prevent issue with Varnish cache
11+
*
12+
* @since 2.9.5
13+
* @author Remy Perona
14+
*
15+
* @param string $rules htaccess rules.
16+
* @return Updated htaccess rules
17+
*/
18+
function rocket_remove_html_expire_goddady( $rules ) {
19+
$rules = <<<HTACCESS
20+
# Expires headers (for better cache control)
21+
<IfModule mod_expires.c>
22+
ExpiresActive on
23+
# Perhaps better to whitelist expires rules? Perhaps.
24+
ExpiresDefault "access plus 1 month"
25+
# cache.appcache needs re-requests in FF 3.6 (thanks Remy ~Introducing HTML5)
26+
ExpiresByType text/cache-manifest "access plus 0 seconds"
27+
# Data
28+
ExpiresByType text/xml "access plus 0 seconds"
29+
ExpiresByType application/xml "access plus 0 seconds"
30+
ExpiresByType application/json "access plus 0 seconds"
31+
# Feed
32+
ExpiresByType application/rss+xml "access plus 1 hour"
33+
ExpiresByType application/atom+xml "access plus 1 hour"
34+
# Favicon (cannot be renamed)
35+
ExpiresByType image/x-icon "access plus 1 week"
36+
# Media: images, video, audio
37+
ExpiresByType image/gif "access plus 1 month"
38+
ExpiresByType image/png "access plus 1 month"
39+
ExpiresByType image/jpeg "access plus 1 month"
40+
ExpiresByType video/ogg "access plus 1 month"
41+
ExpiresByType audio/ogg "access plus 1 month"
42+
ExpiresByType video/mp4 "access plus 1 month"
43+
ExpiresByType video/webm "access plus 1 month"
44+
# HTC files (css3pie)
45+
ExpiresByType text/x-component "access plus 1 month"
46+
# Webfonts
47+
ExpiresByType application/x-font-ttf "access plus 1 month"
48+
ExpiresByType font/opentype "access plus 1 month"
49+
ExpiresByType application/x-font-woff "access plus 1 month"
50+
ExpiresByType application/x-font-woff2 "access plus 1 month"
51+
ExpiresByType image/svg+xml "access plus 1 month"
52+
ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
53+
# CSS and JavaScript
54+
ExpiresByType text/css "access plus 1 year"
55+
ExpiresByType application/javascript "access plus 1 year"
56+
</IfModule>
57+
58+
HTACCESS;
59+
60+
return $rules;
61+
}
62+
add_filter('rocket_htaccess_mod_expires', 'rocket_remove_html_expire_goddady');
63+
64+
/**
65+
* Call the Varnish server to purge the cache with GoDaddy.
66+
*
67+
* @since 2.9.5
68+
*
69+
* @return void
70+
*/
71+
function rocket_clean_domain_godaddy() {
72+
rocket_godaddy_request( 'BAN' );
73+
}
74+
add_action( 'before_rocket_clean_domain', 'rocket_clean_domain_godaddy' );
75+
76+
/**
77+
* Call the Varnish server to purge a specific URL with GoDaddy.
78+
*
79+
* @since 2.9.5
80+
*
81+
* @return void
82+
*/
83+
function rocket_clean_file_godaddy( $url ) {
84+
rocket_godaddy_request( 'PURGE', home_url( $url ) );
1785
}
18-
19-
if ( wp_verify_nonce( $_REQUEST['GD_NONCE'], 'GD_FLUSH_CACHE' ) ) {
20-
// Clear all caching files
21-
rocket_clean_domain();
22-
23-
// Preload cache
24-
run_rocket_preload_cache( 'cache-preload' );
86+
add_action( 'before_rocket_clean_file', 'rocket_clean_file_godaddy' );
87+
88+
/**
89+
* Call the Varnish server to purge the home with GoDaddy.
90+
*
91+
* @since 2.9.5
92+
*
93+
* @return void
94+
*/
95+
function rocket_clean_home_godaddy( $root, $lang ) {
96+
$home_url = trailingslashit( get_rocket_i18n_home_url( $lang ) );
97+
$home_pagination_url = $home_url . trailingslashit( $GLOBALS['wp_rewrite']->pagination_base );
98+
99+
rocket_godaddy_request( 'PURGE', $home_url );
100+
rocket_godaddy_request( 'PURGE', $home_pagination_url );
25101
}
26-
}
27-
28-
/* @since 2.6.5
29-
* For not conflit with GoDaddy
30-
*/
31-
add_action( 'after_rocket_clean_domain', 'rocket_clean_godaddy' );
32-
33-
/**
34-
* Call the cache server to purge the cache with GoDaddy hosting.
35-
*
36-
* @since 2.6.5
37-
*
38-
* @return void
39-
*/
40-
function rocket_clean_godaddy() {
41-
global $gd_cache_purge;
42-
43-
if( is_a( $gd_cache_purge, 'GD_System_Plugin_Cache_Purge' ) && method_exists( 'GD_System_Plugin_Cache_Purge', 'ban_cache' ) ) {
44-
$gd_cache_purge->ban_cache();
102+
add_action( 'before_rocket_clean_home', 'rocket_clean_home_godaddy', 10, 2 );
103+
104+
/**
105+
* Perform the call to the Varnish server to purge
106+
*
107+
* @since 2.9.5
108+
* @source WPaaS\Cache
109+
*
110+
* @param string $method can be BAN or PURGE.
111+
* @param string $url URL to purge.
112+
* @return void
113+
*/
114+
function rocket_godaddy_request( $method, $url = null ) {
115+
$url = empty( $url ) ? home_url() : $url;
116+
$host = parse_url( $url, PHP_URL_HOST );
117+
$url = set_url_scheme( str_replace( $host, WPaas\Plugin::vip(), $url ), 'http' );
118+
119+
wp_cache_flush();
120+
121+
// This forces the APC cache to flush across the server
122+
update_option( 'gd_system_last_cache_flush', time() );
123+
124+
wp_remote_request(
125+
esc_url_raw( $url ),
126+
array(
127+
'method' => $method,
128+
'blocking' => false,
129+
'headers' => array(
130+
'Host' => $host,
131+
),
132+
)
133+
);
45134
}
46-
}
47135

48-
endif;
136+
endif;

0 commit comments

Comments
 (0)