Created
September 30, 2019 13:16
-
-
Save ElliottLandsborough/c63d41a7252e8c452f0a725594d8048f to your computer and use it in GitHub Desktop.
Rofl wordpress relative urls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Api\Utils; | |
class RelativeUrls | |
{ | |
public function __construct() | |
{ | |
// get the path | |
$uri_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); | |
// exclude the urls from the replace | |
$exclude = [ | |
'/wp-json/posts/single/', | |
'/wp-json/document/', | |
]; | |
$enable = true; | |
// loop through whitelisted prefixes | |
foreach ($exclude as $prefix) { | |
// if we have matched one | |
if (substr($uri_path, 0, strlen($prefix)) !== $prefix) { | |
// disable object find/replace | |
$enable = false; | |
} | |
} | |
if ($enable) { | |
// http://codex.wordpress.org/Plugin_API/Action_Reference | |
add_action('registered_taxonomy', [$this, 'bufferStartRelativeUrl']); | |
add_action('shutdown', [$this, 'bufferEndRelativeUrl']); | |
} | |
} | |
public function bufferStartRelativeUrl() | |
{ | |
ob_start([$this, 'callbackRelativeUrl']); | |
} | |
public function bufferEndRelativeUrl() | |
{ | |
@ob_end_flush(); | |
} | |
public function callbackRelativeUrl($buffer) | |
{ | |
// Replace normal URLs | |
$home_url = esc_url(home_url('/')); | |
// remove base domain - DISABLED | |
$home_url_relative = wp_make_link_relative($home_url); | |
// get base domain from .env file instead of db lol... | |
//$home_url_relative = getenv('ATHERTON_WP_DOMAIN') . wp_make_link_relative($home_url); | |
// Replace URLs in inline scripts | |
$home_url_escaped = str_replace('/', '\/', $home_url); | |
$home_url_escaped_relative = str_replace('/', '\/', $home_url_relative); | |
$buffer = str_replace($home_url, $home_url_relative, $buffer); | |
$buffer = str_replace($home_url_escaped, $home_url_escaped_relative, $buffer); | |
return $buffer; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment