-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmercator-domain-replacements.php
268 lines (221 loc) · 7.13 KB
/
mercator-domain-replacements.php
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
/*
Plugin Name: Mercator Domain Replacements
Version: 1.0.8
Plugin URI: https://beapi.fr
Description: Force the replacement of all the original domains of the network by the corresponding mapped domains
Author: Be API
Author URI: https://beapi.fr
----
Copyright 2020 Be API Technical team ([email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
namespace BEAPI\Domain_Mapping;
use function Mercator\mangle_url;
class Mapping {
/** @var array $domains */
private $domains = [];
/**
* Mapping constructor.
*
* @return void
*/
public function __construct() {
if ( ! function_exists( '\Mercator\mangle_url' ) ) {
return;
}
if ( $this->is_facet_wp() ) {
add_action( 'init', [ $this, 'domain_mapping_replacements' ], 0 );
}
add_action( 'init', [ $this, 'domain_mapping_replacements' ], 999 );
}
/**
* Force to replace URL to final mapped domain
*
* @return void
*
* @author Alexandre Sadowski
*/
public function domain_mapping_replacements() {
if ( ! isset( $GLOBALS['mercator_current_mapping'] ) ) {
return;
}
$this->translate_sites_url();
$this->translate_network_url();
if ( empty( $this->domains ) ) {
return;
}
ob_start( [ $this, 'replace_urls' ] );
}
/**
* @return void
*/
public function translate_network_url() {
$current_network = get_network();
$current_site = get_site();
if ( null === $current_site || null === $current_network ) {
return;
}
$network_domain_internal = untrailingslashit( 'https://' . $current_network->domain . $current_network->path );
$domain_internal = untrailingslashit( 'https://' . $current_site->domain . $current_site->path );
$domain_mapped = mangle_url( untrailingslashit( $current_site->siteurl ), '', '', $current_site->id );
// Site domain URL
if ( $domain_internal !== $domain_mapped ) {
$this->domains[ $domain_internal ] = $domain_mapped;
}
// Network domain URL
if ( $network_domain_internal !== $domain_mapped ) {
// Change default upload path to the mapped upload path
$upload_mapped_url = wp_upload_dir()['baseurl'];
$this->domains[ $upload_mapped_url ] = str_replace( $network_domain_internal, $domain_mapped, $upload_mapped_url );
// Change default plugin path to the mapped plugin path
$plugin_default_path = plugins_url();
$this->domains[ $plugin_default_path ] = str_replace( $network_domain_internal, $domain_mapped, $plugin_default_path );
// Change default theme path to the mapped theme path
$theme_default_path = get_theme_file_uri();
$this->domains[ $theme_default_path ] = str_replace( $network_domain_internal, $domain_mapped, $theme_default_path );
// Change parent theme path to the mapped theme path
$theme_parent_path = get_parent_theme_file_uri();
$this->domains[ $theme_parent_path ] = str_replace( $network_domain_internal, $domain_mapped, $theme_parent_path );
// Change DNS Prefetch path
// Use "href" to replace only network URLs starting with "//" and not all network URLs because there may be network URLs contributing.
$this->domains[ "href='//" . wp_parse_url( $network_domain_internal, PHP_URL_HOST ) ] = "href='//" . wp_parse_url( $domain_mapped, PHP_URL_HOST );
}
}
/**
* @return void
*/
public function translate_sites_url() {
$site_query_args = [
'fields' => 'ids',
'number' => 500,
'public' => '1',
'order' => 'ASC',
'orderby' => 'id',
];
/**
* Filter args use to retrieve sites for domain mapping replacement.
*
* @param array $site_query_args
*/
$site_query_args = apply_filters( 'mercator.domain_replacement.site_query_args', $site_query_args );
$site_query = new \WP_Site_Query( $site_query_args );
$sites = $site_query->get_sites();
if ( empty( $sites ) ) {
return;
}
// Backup GLOBAL if existing
$_tmp = isset( $GLOBALS['mercator_current_mapping'] ) ? $GLOBALS['mercator_current_mapping'] : null;
foreach ( $sites as $site_id ) {
$mappings = \Mercator\Mapping::get_by_site( $site_id );
if ( ! $mappings || is_wp_error( $mappings ) ) {
continue;
}
$current_site = get_site( $site_id );
if ( null === $current_site ) {
continue;
}
$domain_internal = untrailingslashit( $current_site->siteurl );
foreach ( $mappings as $mapping ) {
/** @var \Mercator\Mapping $mapping */
if ( ! $mapping->is_active() ) {
continue;
}
$GLOBALS['mercator_current_mapping'] = $mapping;
$mapped_url = mangle_url( $domain_internal, '', '', $site_id );
$GLOBALS['mercator_current_mapping'] = $_tmp;
if ( $mapped_url === $domain_internal ) {
continue;
}
$this->domains[ $domain_internal ] = $mapped_url;
}
}
$GLOBALS['mercator_current_mapping'] = $_tmp;
}
/**
* Replace URLS
* Example :
* https://monsite.com > https://mondomain.com
* https:\/\/monsite.com > https:\/\/mondomain.com localized in JS
*
* @param string $buffer
*
* @return string
*
* @author Alexandre Sadowski
*/
public function replace_urls( $buffer ) {
foreach ( $this->domains as $source => $target ) {
$buffer = str_replace(
[
$source,
$this->replace_slashes( $source ),
$this->replace_scheme( $source ),
],
[
$target,
$this->replace_slashes( $target ),
$this->replace_scheme( $target ),
],
$buffer
);
}
return $buffer;
}
/**
* Add backslashes for JS
*
* @param string $content
*
* @return string
*
* @author Alexandre Sadowski
*/
private function replace_slashes( $content ) {
return str_replace( '/', '\/', $content );
}
/**
* Transform also URL with ://, without defined scheme
*
* @param string $content
*
* @return string
*
* @author Alexandre Sadowski
*/
private function replace_scheme( $content ) {
return str_replace( [ 'http://', 'https://' ], '//', $content );
}
/**
* Is facet WP
*
* @return bool
*
* @author Léonard Phoumpakka
*
*/
protected function is_facet_wp() {
$action = isset( $_POST['action'] ) ? sanitize_key( $_POST['action'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
$is_css_tpl = isset( $_POST['data']['template'] ) && 'wp' == $_POST['data']['template']; // phpcs:ignore WordPress.Security.NonceVerification
$valid_actions = [
'facetwp_refresh',
'facetwp_autocomplete_load',
];
$is_preload = ! in_array( $action, $valid_actions );
if ( ! $is_preload && $is_css_tpl && 'facetwp_autocomplete_load' !== $action ) {
return true;
}
return false;
}
}
new Mapping();