Skip to content

Commit

Permalink
Merge pull request #1 from BeAPI/fix-replace-site-subfolder
Browse files Browse the repository at this point in the history
replace domain only for current site
  • Loading branch information
lphoumpakka authored Nov 17, 2020
2 parents 9597f07 + 511c7b6 commit 0cee0fa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,9 @@ If you really like what we do or want to thank us for our quick work, feel free
## License

Mercator Domain Replacements is licensed under the [GPLv3 or later](LICENSE.md).

## Changelog

### 1.0.2

* Fixed: replace by active current mapped domain only for current sub-site (not use the network domain)
40 changes: 24 additions & 16 deletions mercator-domain-replacements.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/*
Plugin Name: Mercator Domain Replacements
Version: 1.0.0
Version: 1.0.2
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
Expand Down Expand Up @@ -32,7 +32,7 @@

class Mapping {
/** @var array $domains */
private $domains = array();
private $domains = [];

/**
* Mapping constructor.
Expand Down Expand Up @@ -78,27 +78,35 @@ public function translate_network_url() {
return;
}

$domain_internal = untrailingslashit( 'https://' . $current_network->domain . $current_network->path );
$domain_mapped = mangle_url( untrailingslashit( $current_site->siteurl ), '', '', $current_site->id );
if ( $domain_internal === $domain_mapped ) {
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;
}

$this->domains[ $domain_internal ] = $domain_mapped;
// Network domain URL
if ( $network_domain_internal !== $domain_mapped ) {
$upload_mapped_url = wp_upload_dir()['baseurl'];
$upload_path = str_replace( $domain_mapped, '', $upload_mapped_url );
$this->domains[ $network_domain_internal . $upload_path ] = $domain_mapped . $upload_path;
}
}

/**
* @return void
*/
public function translate_sites_url() {
$site_query = new \WP_Site_Query(
array(
[
'fields' => 'ids',
'number' => 500,
'public' => '1',
'order' => 'ASC',
'orderby' => 'id',
)
]
);

$sites = $site_query->get_sites();
Expand All @@ -123,7 +131,7 @@ public function translate_sites_url() {
$domain_internal = untrailingslashit( $current_site->siteurl );

foreach ( $mappings as $mapping ) {
/** @var $mapping \Mercator\Mapping */
/** @var \Mercator\Mapping $mapping */
if ( ! $mapping->is_active() ) {
continue;
}
Expand All @@ -148,10 +156,10 @@ public function translate_sites_url() {
* https://monsite.com > https://mondomain.com
* https:\/\/monsite.com > https:\/\/mondomain.com localized in JS
*
* @return string
*
* @param string $buffer
*
* @return string
*
* @author Alexandre Sadowski
*/
public function replace_urls( $buffer ) {
Expand All @@ -177,10 +185,10 @@ public function replace_urls( $buffer ) {
/**
* Add backslashes for JS
*
* @return string|string[]
*
* @param string $content
*
* @return string
*
* @author Alexandre Sadowski
*/
private function replace_slashes( $content ) {
Expand All @@ -190,10 +198,10 @@ private function replace_slashes( $content ) {
/**
* Transform also URL with ://, without defined scheme
*
* @return string|string[]
*
* @param string $content
*
* @return string
*
* @author Alexandre Sadowski
*/
private function replace_scheme( $content ) {
Expand Down

0 comments on commit 0cee0fa

Please sign in to comment.