| @@ -6,9 +6,12 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace WPE\FaustWP\Replacement; |
| 9 | 9 | |
| 10 | -use function WPE\FaustWP\Settings\is_rewrites_enabled; | |
| 10 | +use function WPE\FaustWP\Settings\{ | |
| 11 | + faustwp_get_setting, | |
| 12 | + is_rewrites_enabled | |
| 13 | +}; | |
| 11 | 14 | |
| 12 | 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | 16 | exit; |
| 14 | 17 | } |
| @@ -29,4 +32,82 @@ | ||
| 29 | 32 | * @param bool $enabled True if domain replacement is enabled, false if else. |
| 30 | 33 | */ |
| 31 | 34 | return apply_filters( 'faustwp_domain_replacement_enabled', is_rewrites_enabled() ); |
| 32 | 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 | + | |