-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkdk_domain_url_normalizer.php
184 lines (162 loc) · 6.7 KB
/
kdk_domain_url_normalizer.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
<?php
/*
* Plugin Name: KDK Domain URL Normalizer (by Kodeka)
* Plugin URI: https://github.com/kodeka/kdk_domain_url_normalizer
* Description: A simple plugin to rewrite multiple domains or subdomains to a single domain (domain URL normalization) in the site's HTML output.
* Version: 1.0.0
* Author: Kodeka
* Author URI: https://kodeka.io
* License: GNU/GPL https://www.gnu.org/copyleft/gpl.html
*/
// Exit if accessed directly
if (!defined('ABSPATH')) {
exit;
}
function kdk_domain_url_normalizer_callback($buffer)
{
$options = get_option('kdk_domain_url_normalizer_options');
$enabled = isset($options['enabled']) && $options['enabled'] ? true : false;
$urls = isset($options['urls']) && trim($options['urls']) != '' ? explode(PHP_EOL, $options['urls']) : null;
$domain = isset($options['domain']) ? trim($options['domain']) : null;
if ($enabled && is_array($urls) && count($urls) && $domain) {
$domainFragments = explode('://', $domain);
foreach ($urls as $url) {
$url = trim($url);
if ($url) {
$buffer = str_ireplace('http://'.$url, $domain, $buffer);
$buffer = str_ireplace('https://'.$url, $domain, $buffer);
$buffer = str_ireplace('http:\/\/'.$url, $domainFragments[0].':\/\/'.$domainFragments[1], $buffer);
$buffer = str_ireplace('https:\/\/'.$url, $domainFragments[0].':\/\/'.$domainFragments[1], $buffer);
$buffer = str_ireplace('"'.$url, '"'.$domainFragments[1], $buffer);
$buffer = str_ireplace('\''.$url, '\''.$domainFragments[1], $buffer);
$buffer = str_ireplace('"//'.$url, '"//'.$domainFragments[1], $buffer);
$buffer = str_ireplace('\'//'.$url, '\'//'.$domainFragments[1], $buffer);
}
}
}
// Set caching headers
$cacheInSeconds_Frontpage = '120';
$cacheInSeconds_Inner = '600';
if (!is_user_logged_in()) {
header_remove("Cache-Control");
if (is_front_page()) {
header('Cache-Control: public, max-age='.$cacheInSeconds_Frontpage.', stale-while-revalidate='.($cacheInSeconds_Frontpage*2).', stale-if-error='.($cacheInSeconds_Frontpage*3));
} else {
header('Cache-Control: public, max-age='.$cacheInSeconds_Inner.', stale-while-revalidate='.($cacheInSeconds_Inner*2).', stale-if-error='.($cacheInSeconds_Inner*3));
}
}
return $buffer;
}
function kdk_domain_url_normalizer_start()
{
ob_start('kdk_domain_url_normalizer_callback');
}
function kdk_domain_url_normalizer_end()
{
if (ob_get_length()) {
ob_end_flush();
}
}
// Control execution
if (is_admin() || strpos($_SERVER['REQUEST_URI'], 'wp-login.php') !== false || defined('STDIN')) {
// Do nothing
} else {
//add_action('wp_head', 'kdk_domain_url_normalizer_start', 1);
//add_action('wp_footer', 'kdk_domain_url_normalizer_end', 1);
add_action('after_setup_theme', 'kdk_domain_url_normalizer_start', 999998);
add_action('shutdown', 'kdk_domain_url_normalizer_end', 999999);
}
function kdk_domain_url_normalizer_settings_menu()
{
add_options_page(
'Domain URL Normalizer (by Kodeka)',
'Domain URL Normalizer (by Kodeka)',
'manage_options',
'kdk-domain-url-normalizer',
'kdk_domain_url_normalizer_create_admin_page'
);
}
function kdk_domain_url_normalizer_create_admin_page()
{
require WP_PLUGIN_DIR.'/kdk_domain_url_normalizer/admin/settings.php';
}
function kdk_domain_url_normalizer_print_section_info()
{
}
function kdk_domain_url_normalizer_field_enabled()
{
$options = get_option('kdk_domain_url_normalizer_options');
$enabled = false;
if (isset($options['enabled']) && $options['enabled']) {
$enabled = true;
}
if ($enabled) {
$checked1 = '';
$checked2 = 'checked="checked"';
} else {
$checked1 = 'checked="checked"';
$checked2 = '';
}
print('
<label>
'.__('No', 'kdk_domain_url_normalizer').'
<input type="radio" name="kdk_domain_url_normalizer_options[enabled]" value="0" '.$checked1.' />
</label>
<label>
'.__('Yes', 'kdk_domain_url_normalizer').'
<input type="radio" name="kdk_domain_url_normalizer_options[enabled]" value="1" '.$checked2.' /></label>
');
}
function kdk_domain_url_normalizer_field_match_urls()
{
$options = get_option('kdk_domain_url_normalizer_options');
$value = isset($options['urls']) ? $options['urls'] : '';
print('<textarea name="kdk_domain_url_normalizer_options[urls]" cols="30" rows="6" placeholder="'.__('Example (domain per line): example.com www2.example.com www2.example.com:8080', 'kdk_domain_url_normalizer').'">'.$value.'</textarea>');
}
function kdk_domain_url_normalizer_field_replace_url()
{
$options = get_option('kdk_domain_url_normalizer_options');
$value = isset($options['domain']) ? $options['domain'] : '';
print('<input type="text" name="kdk_domain_url_normalizer_options[domain]" value="'.htmlspecialchars($value).'" placeholder="'.__('Example: http://www.example.com', 'kdk_domain_url_normalizer').'" style="width:300px;" />');
}
function kdk_domain_url_normalizer_settings_page()
{
register_setting(
'kdk_domain_url_normalizer_option_group',
'kdk_domain_url_normalizer_options'
);
add_settings_section(
'setting_section_id',
__('Options', 'kdk_domain_url_normalizer'),
'kdk_domain_url_normalizer_print_section_info',
'kdk-domain-url-normalizer-admin'
);
add_settings_field(
'enabled',
__('Enabled', 'kdk_domain_url_normalizer'),
'kdk_domain_url_normalizer_field_enabled',
'kdk-domain-url-normalizer-admin',
'setting_section_id'
);
add_settings_field(
'urls',
__('Domain URLs to match (do not include http:// or https:// - add each domain per line)', 'kdk_domain_url_normalizer'),
'kdk_domain_url_normalizer_field_match_urls',
'kdk-domain-url-normalizer-admin',
'setting_section_id'
);
add_settings_field(
'domain',
__('Domain URL to replace with (MUST include http:// or https:// - no trailing slash)', 'kdk_domain_url_normalizer'),
'kdk_domain_url_normalizer_field_replace_url',
'kdk-domain-url-normalizer-admin',
'setting_section_id'
);
}
add_action('admin_menu', 'kdk_domain_url_normalizer_settings_menu');
add_action('admin_init', 'kdk_domain_url_normalizer_settings_page');
add_action('plugins_loaded', 'kdk_domain_url_normalizer_load_textdomain');
function kdk_domain_url_normalizer_load_textdomain()
{
load_plugin_textdomain('kdk_domain_url_normalizer', false, dirname(plugin_basename(__FILE__)).'/lang/');
}