| 1 |
<?php |
| 2 |
/** |
| 3 |
* Replacement functions. |
| 4 |
* |
| 5 |
* @package FaustWP |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPE\FaustWP\Replacement; |
| 9 |
|
| 10 |
use function WPE\FaustWP\Settings\{ |
| 11 |
faustwp_get_setting, |
| 12 |
is_rewrites_enabled |
| 13 |
}; |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Determine if domain replacement can be done. |
| 21 |
* |
| 22 |
* Enabled if query string parameter 'replace-domain' is present. |
| 23 |
* |
| 24 |
* @return bool True if can proceed with replacement, false if else. |
| 25 |
*/ |
| 26 |
function domain_replacement_enabled() { |
| 27 |
/** |
| 28 |
* Filter 'faustwp_domain_replacement_enabled'. |
| 29 |
* |
| 30 |
* Used to override or extend if domain replacement is enabled. |
| 31 |
* |
| 32 |
* @param bool $enabled True if domain replacement is enabled, false if else. |
| 33 |
*/ |
| 34 |
return apply_filters( 'faustwp_domain_replacement_enabled', is_rewrites_enabled() ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Returns the equivalent WordPress URL given a frontend URL. |
| 39 |
* |
| 40 |
* @param string $url The URL to normalize. |
| 41 |
* @param bool $frontend Returns an equivalent frontend URL given a WordPress URL if true. |
| 42 |
* @return string The WordPress URL. |
| 43 |
*/ |
| 44 |
function normalize_url( $url, $frontend = false ) { |
| 45 |
$frontend_uri = faustwp_get_setting( 'frontend_uri' ); |
| 46 |
|
| 47 |
// Return the URL as is if frontend uri is empty. |
| 48 |
if ( ! $frontend_uri ) { |
| 49 |
return $url; |
| 50 |
} |
| 51 |
|
| 52 |
$frontend_uri = trailingslashit( $frontend_uri ); |
| 53 |
$home_url = trailingslashit( get_home_url() ); |
| 54 |
|
| 55 |
$normalized_url = $frontend |
| 56 |
? str_replace( $home_url, $frontend_uri, $url ) |
| 57 |
: str_replace( $frontend_uri, $home_url, $url ); |
| 58 |
|
| 59 |
return $normalized_url; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Returns the equivalent WordPress URL given a frontend URL. |
| 64 |
* |
| 65 |
* @param string $url The frontend URL. |
| 66 |
* @return string The WordPress URL. |
| 67 |
*/ |
| 68 |
function equivalent_wp_url( $url ) { |
| 69 |
return normalize_url( $url, false ); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Returns the equivalent frontend URL given a WordPress URL. |
| 74 |
* |
| 75 |
* @param string $url The WordPress URL. |
| 76 |
* @return string The frontend URL. |
| 77 |
*/ |
| 78 |
function equivalent_frontend_url( $url ) { |
| 79 |
return normalize_url( $url, true ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Normalizes a sitemap URL to be the original WordPress URL. |
| 84 |
* |
| 85 |
* @param array $sitemap_entry The sitemap entry containing the URL to normalize. |
| 86 |
* @return array |
| 87 |
*/ |
| 88 |
function normalize_sitemap_entry( $sitemap_entry ) { |
| 89 |
if ( ! isset( $sitemap_entry['loc'] ) ) { |
| 90 |
return $sitemap_entry; |
| 91 |
} |
| 92 |
|
| 93 |
$sitemap_entry['loc'] = equivalent_wp_url( $sitemap_entry['loc'] ); |
| 94 |
|
| 95 |
return $sitemap_entry; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Check if a string has a file extension. |
| 100 |
* |
| 101 |
* @param string $string The string to check. |
| 102 |
* @return boolean |
| 103 |
*/ |
| 104 |
function has_file_extension( $string ) { |
| 105 |
$file_extension_pattern = '/\.[a-zA-Z0-9]+$/'; |
| 106 |
|
| 107 |
if ( preg_match( $file_extension_pattern, $string ) ) { |
| 108 |
return true; |
| 109 |
} else { |
| 110 |
return false; // String does not have a file extension. |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|