api.php
3.21 KB
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
<?php
/**
* Get an URL with one of CNAMES added in options
*
* @since 2.1
*
* @param string $url The URL to parse.
* @param array $zone (default: array( 'all' )). Deprecated.
* @return string
*/
function get_rocket_cdn_url( $url, $zone = [ 'all' ] ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
$container = apply_filters( 'rocket_container', '' );
$cdn = $container->get( 'cdn' );
return $cdn->rewrite_url( $url );
}
/**
* Wrapper of get_rocket_cdn_url() and print result
*
* @since 2.1
*
* @param string $url The URL to parse.
* @param array $zone (default: array( 'all' )). Deprecated.
*/
function rocket_cdn_url( $url, $zone = [ 'all' ] ) {
echo esc_url( get_rocket_cdn_url( $url, $zone ) );
}
/**
* Get all CNAMES.
*
* @since 2.1
* @since 3.0 Don't check for WP Rocket CDN option activated to be able to use the function on Hosting with CDN auto-enabled.
*
* @param string $zone List of zones. Default is 'all'.
* @return array List of CNAMES
*/
function get_rocket_cdn_cnames( $zone = 'all' ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
$hosts = [];
$cnames = get_rocket_option( 'cdn_cnames', [] );
if ( $cnames ) {
$cnames_zone = get_rocket_option( 'cdn_zone', [] );
$zone = (array) $zone;
foreach ( $cnames as $k => $_urls ) {
if ( ! in_array( $cnames_zone[ $k ], $zone, true ) ) {
continue;
}
$_urls = explode( ',', $_urls );
$_urls = array_map( 'trim', $_urls );
foreach ( $_urls as $url ) {
$hosts[] = $url;
}
}
}
/**
* Filter all CNAMES.
*
* @since 2.7
*
* @param array $hosts List of CNAMES.
* @param array $zone Array of CDN zones.
*/
$hosts = (array) apply_filters( 'rocket_cdn_cnames', $hosts, $zone );
$hosts = array_filter( $hosts );
$hosts = array_flip( array_flip( $hosts ) );
$hosts = array_values( $hosts );
return $hosts;
}
/**
* Check if the current URL is for a live site (not local, not staging).
*
* @since 3.5
* @author Remy Perona
*
* @return bool True if live, false otherwise.
*/
function rocket_is_live_site() {
if ( rocket_get_constant( 'WP_ROCKET_DEBUG' ) ) {
return true;
}
$host = wp_parse_url( home_url(), PHP_URL_HOST );
if ( ! $host ) {
return false;
}
// Check for local development sites.
$local_tlds = [
'127.0.0.1',
'localhost',
'.local',
'.test',
'.docksal',
'.docksal.site',
'.dev.cc',
'.lndo.site',
];
foreach ( $local_tlds as $local_tld ) {
if ( $host === $local_tld ) {
return false;
}
// Check the TLD.
if ( substr( $host, -strlen( $local_tld ) ) === $local_tld ) {
return false;
}
}
// Check for staging sites.
$staging = [
'.wpengine.com',
'.wpenginepowered.com',
'.pantheonsite.io',
'.flywheelsites.com',
'.flywheelstaging.com',
'.kinsta.com',
'.kinsta.cloud',
'.cloudwaysapps.com',
'.azurewebsites.net',
'.wpserveur.net',
'-liquidwebsites.com',
'.myftpupload.com',
'.dream.press',
'.sg-host.com',
'.platformsh.site',
'.wpstage.net',
'.bigscoots-staging.com',
'.wpsc.site',
'.runcloud.link',
'.onrocket.site',
'.singlestaging.com',
'.myraidbox.de',
];
foreach ( $staging as $partial_host ) {
if ( strpos( $host, $partial_host ) ) {
return false;
}
}
return true;
}