LsTemplateDomainUpdater.php
3.11 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
<?php
namespace WPML\LanguageSwitcher;
use WPML\FP\Fns;
use WPML\FP\Obj;
use WPML\FP\Str;
use WPML\Collect\Support\Collection;
use WPML\FP\Either;
class LsTemplateDomainUpdater {
public function run( Collection $data, \wpdb $wpdb ) {
$this->runUpdate();
return Either::of( true );
}
public function runUpdate( $siteurl = null, $homepath = null ) {
$data = get_option( \WPML_LS_Templates::OPTION_NAME );
if ( ! $data ) {
return;
}
if ( is_null( $siteurl ) ) {
$siteurl = site_url();
}
if ( is_null( $homepath ) ) {
$homepath = get_home_path();
}
$homepath = untrailingslashit( trim( $homepath ) );
$siteurl = untrailingslashit( trim( $siteurl ) );
$propsWithUrls = [ 'css', 'js', 'base_uri', 'flags_base_uri' ];
$getTemplateData = function ( $template ) {
return method_exists( $template, 'get_template_data' ) ? $template->get_template_data() : $template;
};
$updatePath = function ( $templatePropValue ) use ( $homepath ) {
return Fns::map(
function ( $value ) use ( $homepath ) {
return $this->setPath( $value, $homepath );
},
$templatePropValue
);
};
$updateUrlOfSingleValue = function ( $templatePropValue ) use ( $siteurl ) {
return $this->setUrl( $templatePropValue, $siteurl );
};
$updateUrl = function ( $templatePropValue ) use ( $updateUrlOfSingleValue ) {
return is_array( $templatePropValue )
? Fns::map( $updateUrlOfSingleValue, $templatePropValue )
: $updateUrlOfSingleValue( $templatePropValue );
};
$transformations = [ 'path' => $updatePath ];
foreach ( $propsWithUrls as $prop ) {
$transformations[ $prop ] = $updateUrl;
}
$updateTemplateData = function ( $template ) use ( $getTemplateData, $transformations ) {
$updatedTemplateData = Obj::evolve( $transformations, $getTemplateData( $template ) );
if ( method_exists( $template, 'set_template_data' ) ) {
$template->set_template_data( $updatedTemplateData );
} else {
$template = $updatedTemplateData;
}
return $template;
};
$data = Fns::map( $updateTemplateData, $data );
update_option( \WPML_LS_Templates::OPTION_NAME, $data );
return $data;
}
private function setPath( $path, $homepath ) {
$pathParts = explode( '/wp-content', $path );
$origHomepath = $pathParts[0];
return Str::replace( $origHomepath, $homepath, $path );
}
private function setUrl( $url, $siteurl ) {
$url = trim( $url );
return $this->maybeSetUrl(
$this->maybeSetProtocol(
$url,
$siteurl
),
$siteurl
);
}
private function maybeSetProtocol( $url, $siteurl ) {
if ( Str::startsWith( 'http', $url ) ) {
return $url;
}
$protocol = ( Str::startsWith( 'https', $siteurl ) ) ? 'https' : 'http';
if ( ! Str::startsWith( ':', $url ) ) {
$protocol .= ':';
}
if ( ! Str::startsWith( '//', $url ) ) {
$protocol .= '//';
}
return $protocol . $url;
}
private function maybeSetUrl( $url, $siteurl ) {
if ( Str::startsWith( $siteurl, $url ) ) {
return $url;
}
$urlData = wp_parse_url( $url );
$urlDomain = $urlData['scheme'] . '://' . $urlData['host'];
return Str::replace( $urlDomain, $siteurl, $url );
}
}