| 1190 | if ( !function_exists('wp_maybe_add_redirect_preload_headers') ) : |
| 1191 | /** |
| 1192 | * Checks if redirected asset can be preloaded as document |
| 1193 | * |
| 1194 | * @param string $location The path to redirect to. |
| 1195 | * @return bool Redirect-sanitized URL. |
| 1196 | **/ |
| 1197 | function wp_maybe_add_redirect_preload_headers($location) { |
| 1198 | |
| 1199 | $link = null; |
| 1200 | |
| 1201 | /** |
| 1202 | * Check if this is redirect to the same domain so we can add Link headers |
| 1203 | */ |
| 1204 | $siteurl = site_url(); |
| 1205 | if ( substr( $location, 0, 1 ) === '/' || substr( $location, 0, strlen( $siteurl ) ) === $siteurl ) { |
| 1206 | |
| 1207 | $location_parts = parse_url( $location ); |
| 1208 | |
| 1209 | if ( substr($location_parts['path'],-1) === '/' || |
| 1210 | substr($location_parts['path'],-4) === '.php' || |
| 1211 | substr($location_parts['path'],-5) === '.html' ) { |
| 1212 | |
| 1213 | $link = $location_parts['path']; |
| 1214 | if ( isset( $location_parts['fragment'] ) ) |
| 1215 | $link .= $location_parts['fragment']; |
| 1216 | if ( isset( $location_parts['query'] ) ) |
| 1217 | $link .= '?' . $location_parts['query']; |
| 1218 | } |
| 1219 | } |
| 1220 | $link = apply_filters( 'wp_maybe_add_preload_headers', $link, $location ); |
| 1221 | |
| 1222 | if ( $link ) |
| 1223 | header("Link: <{$link}>; rel=preload; as=document", false); |
| 1224 | } |
| 1225 | endif; |
| 1226 | |