-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathfunctions.php
More file actions
199 lines (170 loc) · 4.99 KB
/
functions.php
File metadata and controls
199 lines (170 loc) · 4.99 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/**
* Replacement functions.
*
* @package FaustWP
*/
declare(strict_types=1);
namespace WPE\FaustWP\Replacement;
use function WPE\FaustWP\Settings\{
faustwp_get_setting,
is_rewrites_enabled,
use_wp_domain_for_post_and_category_urls,
};
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Determine if domain replacement can be done.
*
* @return bool True if can proceed with replacement, false if else.
*/
function domain_replacement_enabled() {
/**
* Filter 'faustwp_domain_replacement_enabled'.
*
* Used to override or extend if domain replacement is enabled.
*
* @param bool $enabled True if domain replacement is enabled, false if else.
*/
return apply_filters( 'faustwp_domain_replacement_enabled', ! use_wp_domain_for_post_and_category_urls() );
}
/**
* Returns the equivalent WordPress URL given a frontend URL.
*
* @param string $url The URL to normalize.
* @param bool $frontend Returns an equivalent frontend URL given a WordPress URL if true.
* @return string The WordPress URL.
*/
function normalize_url( $url, $frontend = false ) {
$frontend_uri = faustwp_get_setting( 'frontend_uri' );
// Return the URL as is if frontend uri is empty.
if ( ! $frontend_uri ) {
return $url;
}
$frontend_uri = trailingslashit( $frontend_uri );
$home_url = trailingslashit( get_home_url() );
$normalized_url = $frontend
? str_replace( $home_url, $frontend_uri, $url )
: str_replace( $frontend_uri, $home_url, $url );
return $normalized_url;
}
/**
* Returns the equivalent WordPress URL given a frontend URL.
*
* @param string $url The frontend URL.
* @return string The WordPress URL.
*/
function equivalent_wp_url( $url ) {
return normalize_url( $url, false );
}
/**
* Returns the equivalent frontend URL given a WordPress URL.
*
* @param string $url The WordPress URL.
* @return string The frontend URL.
*/
function equivalent_frontend_url( $url ) {
return normalize_url( $url, true );
}
/**
* Normalizes a sitemap URL to be the original WordPress URL.
*
* @param array $sitemap_entry The sitemap entry containing the URL to normalize.
* @return array
*/
function normalize_sitemap_entry( $sitemap_entry ) {
if ( ! isset( $sitemap_entry['loc'] ) ) {
return $sitemap_entry;
}
$sitemap_entry['loc'] = equivalent_wp_url( $sitemap_entry['loc'] );
return $sitemap_entry;
}
/**
* Check if a string has a file extension.
*
* @param string $file The string to check.
* @return boolean
*/
function has_file_extension( $file ) {
$file_extension_pattern = '/\.[a-zA-Z0-9]+$/';
if ( preg_match( $file_extension_pattern, $file ) ) {
return true;
} else {
return false; // String does not have a file extension.
}
}
/**
* Determines if an AJAX request to generate permalinks is in progress.
*
* @return boolean
*/
function is_ajax_generate_permalink_request(): bool {
return ( ! empty( $_POST['samplepermalinknonce'] ) && check_ajax_referer( 'samplepermalink', 'samplepermalinknonce' ) );
}
/**
* Determines if a wp-link-ajax request is in progress.
*
* @return boolean
*/
function is_wp_link_ajax_request(): bool {
return ( wp_doing_ajax()
&& ! empty( $_POST['_ajax_linking_nonce'] )
&& wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_ajax_linking_nonce'] ) ), 'internal-linking' )
&& ! empty( $_POST['action'] )
&& 'wp-link-ajax' === $_POST['action'] );
}
/**
* Get all site URLs for each possible HTTP protocol.
*
* @param string $site_url The site url.
*
* @return array<string> An array of site urls.
*/
function faustwp_get_wp_site_urls( string $site_url ): array {
$host_url_parse = wp_parse_url( $site_url );
$host_url = $host_url_parse['host'] ?? '';
if ( ! empty( $host_url_parse['port'] ) ) {
$host_url .= ':' . $host_url_parse['port'];
}
$is_https = strpos( $site_url, 'https://' ) === 0;
return apply_filters(
'faustwp_get_wp_site_urls',
array(
$is_https ? "https://$host_url" : "http://$host_url",
$is_https ? "http://$host_url" : "https://$host_url",
"//$host_url",
)
);
}
/**
* Get all media urls based off the available site urls
*
* @param array<string> $wp_site_urls The array of potential site urls.
* @param string $relative_upload_url The relative upload url.
*
* @return array<string> The array of media Urls
*/
function faustwp_get_wp_media_urls( array $wp_site_urls, string $relative_upload_url ) {
$media_urls = array();
foreach ( $wp_site_urls as $site_url ) {
$media_urls[] = $site_url . $relative_upload_url;
}
return apply_filters( 'faustwp_get_wp_site_media_urls', $media_urls );
}
/**
* Gets the relative wp-content upload URL.
*
* @param array<string>|string $site_urls An array of site URLs.
* @param string $upload_url An array of site URLs.
*
* @return string The relative upload URL.
*/
function faustwp_get_relative_upload_url( array $site_urls, string $upload_url = '' ): string {
foreach ( $site_urls as $site_url ) {
if ( strpos( $upload_url, $site_url ) === 0 ) {
return (string) str_replace( $site_url, '', $upload_url );
}
}
return '';
}