/
Settings.php
324 lines (291 loc) · 9.84 KB
/
Settings.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
/**
* Admin Settings Module
*
* Adds the settings page.
*
* @author Mike Ems
* @package WordPressHTTPS
*
*/
class WordPressHTTPS_Module_Settings extends Mvied_Plugin_Module {
/**
* Initialize Module
*
* @param none
* @return void
*/
public function init() {
if ( is_admin() ) {
add_action('wp_ajax_' . $this->getPlugin()->getSlug() . '_settings_save', array(&$this, 'save'));
add_action('wp_ajax_' . $this->getPlugin()->getSlug() . '_settings_reset', array(&$this, 'reset'));
add_action('wp_ajax_' . $this->getPlugin()->getSlug() . '_settings_cache_reset', array(&$this, 'cache_reset'));
add_action('wp_ajax_' . $this->getPlugin()->getSlug() . '_ajax_metabox', array(&$this, 'ajax_metabox'));
if ( isset($_GET['page']) && strpos($_GET['page'], $this->getPlugin()->getSlug()) !== false ) {
// Add meta boxes
add_action('admin_init', array(&$this, 'add_meta_boxes'));
// Add scripts
add_action('admin_enqueue_scripts', array(&$this, 'admin_enqueue_scripts'));
}
// Add admin menus
add_action('admin_menu', array(&$this, 'admin_menu'));
}
}
/**
* Admin panel menu option
* WordPress Hook - admin_menu
*
* @param none
* @return void
*/
public function admin_menu() {
if ( $this->getPlugin()->getSetting('admin_menu') === 'side' ) {
add_menu_page('HTTPS', 'HTTPS', 'manage_options', $this->getPlugin()->getSlug(), array($this->getPlugin()->getModule('Settings'), 'dispatch'), '', 88);
} else {
add_options_page('HTTPS', 'HTTPS', 'manage_options', $this->getPlugin()->getSlug(), array($this->getPlugin()->getModule('Settings'), 'dispatch'));
}
}
/**
* Add meta boxes to WordPress HTTPS Settings page.
*
* @param none
* @return void
*/
public function add_meta_boxes() {
add_meta_box(
$this->getPlugin()->getSlug() . '_settings',
__( 'General Settings', $this->getPlugin()->getSlug() ),
array($this->getPlugin()->getModule('Admin'), 'meta_box_render'),
'toplevel_page_' . $this->getPlugin()->getSlug(),
'main',
'high',
array( 'metabox' => 'settings' )
);
add_meta_box(
$this->getPlugin()->getSlug() . '_updates',
__( 'Developer Updates', $this->getPlugin()->getSlug() ),
array($this->getPlugin()->getModule('Admin'), 'meta_box_render'),
'toplevel_page_' . $this->getPlugin()->getSlug(),
'side',
'high',
array( 'metabox' => 'ajax', 'url' => 'http://wordpresshttps.com/client/updates.php' )
);
add_meta_box(
$this->getPlugin()->getSlug() . '_support',
__( 'Support', $this->getPlugin()->getSlug() ),
array($this->getPlugin()->getModule('Admin'), 'meta_box_render'),
'toplevel_page_' . $this->getPlugin()->getSlug(),
'side',
'high',
array( 'metabox' => 'ajax', 'url' => 'http://wordpresshttps.com/client/support.php' )
);
add_meta_box(
$this->getPlugin()->getSlug() . '_rate',
__( 'Feedback', $this->getPlugin()->getSlug() ),
array($this->getPlugin()->getModule('Admin'), 'meta_box_render'),
'toplevel_page_' . $this->getPlugin()->getSlug(),
'side',
'core',
array( 'metabox' => 'ajax', 'url' => 'http://wordpresshttps.com/client/rate.php' )
);
add_meta_box(
$this->getPlugin()->getSlug() . '_donate',
__( 'Donate', $this->getPlugin()->getSlug() ),
array($this->getPlugin()->getModule('Admin'), 'meta_box_render'),
'toplevel_page_' . $this->getPlugin()->getSlug(),
'side',
'core',
array( 'metabox' => 'ajax', 'url' => 'http://wordpresshttps.com/client/donate.php' )
);
add_meta_box(
$this->getPlugin()->getSlug() . '_donate2',
__( 'Loading...', $this->getPlugin()->getSlug() ),
array($this->getPlugin()->getModule('Admin'), 'meta_box_render'),
'toplevel_page_' . $this->getPlugin()->getSlug(),
'main',
'low',
array( 'metabox' => 'ajax', 'url' => 'http://wordpresshttps.com/client/donate2.php' )
);
}
/**
* Dispatch request for settings page
*
* @param none
* @return void
*/
public function dispatch() {
if ( !current_user_can('manage_options') ) {
wp_die( __('You do not have sufficient permissions to access this page.') );
}
self::render();
}
/**
* Dispatch request for ajax metabox
*
* @param none
* @return void
*/
public function ajax_metabox() {
// Disable errors
error_reporting(0);
// Set headers
header("Status: 200");
header("HTTP/1.1 200 OK");
header('Content-Type: text/html');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', FALSE);
header('Pragma: no-cache');
header("Vary: Accept-Encoding");
if ( ! wp_verify_nonce($_POST['_nonce'], $this->getPlugin()->getSlug()) ) {
exit;
}
$content = Mvied_Url::fromString( $_POST['url'] )->getContent();
if ( $content ) {
echo $content;
}
exit;
}
/**
* Adds javascript and stylesheets to settings page in the admin panel.
* WordPress Hook - enqueue_scripts
*
* @param none
* @return void
*/
public function admin_enqueue_scripts() {
wp_enqueue_style($this->getPlugin()->getSlug() . '-admin-page', $this->getPlugin()->getPluginUrl() . '/css/settings.css', array(), $this->getPlugin()->getVersion());
wp_enqueue_script('jquery-form');
wp_enqueue_script('post');
if ( function_exists('add_thickbox') ) {
add_thickbox();
}
}
/**
* Render settings page
*
* @param none
* @return void
*/
public function render() {
$this->getPlugin()->renderView('settings');
}
/**
* Reset Cache
*
* @param none
* @return void
*/
public function cache_reset() {
if ( !wp_verify_nonce($_POST['_wpnonce'], $this->getPlugin()->getSlug()) ) {
return false;
}
$message = __('Cache reset.','wordpress-https');
$errors = array();
$reload = false;
$this->getPlugin()->setSetting('secure_external_urls', array());
$this->getPlugin()->setSetting('unsecure_external_urls', array());
$this->getPlugin()->setSetting('path_cache', array());
$this->getPlugin()->setSetting('blog_cache', array());
$this->getPlugin()->renderView('ajax_message', array('message' => $message, 'errors' => $errors, 'reload' => $reload));
}
/**
* Reset Settings
*
* @param none
* @return void
*/
public function reset() {
if ( !wp_verify_nonce($_POST['_wpnonce'], $this->getPlugin()->getSlug()) ) {
return false;
}
$message = __('Settings reset.','wordpress-https');
$errors = array();
$reload = true;
foreach ($this->getPlugin()->getSettings() as $key => $default) {
$this->getPlugin()->setSetting($key, $default);
}
$this->getPlugin()->install();
$this->getPlugin()->renderView('ajax_message', array('message' => $message, 'errors' => $errors, 'reload' => $reload));
}
/**
* Save Settings
*
* @param none
* @return void
*/
public function save() {
if ( !wp_verify_nonce($_POST['_wpnonce'], $this->getPlugin()->getSlug()) ) {
return false;
}
$message = __('Settings saved.','wordpress-https');
$errors = array();
$reload = false;
$logout = false;
$ssl_host = clone $this->getPlugin()->getHttpsUrl();
foreach ($this->getPlugin()->getSettings() as $key => $default) {
if ( !array_key_exists($key, $_POST) && $default == 0 ) {
$_POST[$key] = 0;
$this->getPlugin()->setSetting($key, $_POST[$key]);
} else if ( array_key_exists($key, $_POST) ) {
if ( $key == 'ssl_host' ) {
if ( $_POST[$key] != '' ) {
$_POST[$key] = strtolower($_POST[$key]);
// Add Scheme
if ( strpos($_POST[$key], 'http://') === false && strpos($_POST[$key], 'https://') === false ) {
$_POST[$key] = 'https://' . $_POST[$key];
}
$ssl_host = Mvied_Url::fromString($_POST[$key]);
// Add Path
if ( strpos($ssl_host->getPath(), $this->getPlugin()->getHttpUrl()->getPath()) !== true ) {
$path = '/'. ltrim(str_replace(rtrim($this->getPlugin()->getHttpUrl()->getPath(), '/'), '', $ssl_host->getPath()), '/');
$ssl_host->setPath(rtrim($path, '/') . $this->getPlugin()->getHttpUrl()->getPath());
}
$ssl_host->setPath(rtrim($ssl_host->getPath(), '/') . '/');
if ( $ssl_host->toString() != $this->getPlugin()->getHttpsUrl()->toString() ) {
// Ensure that the WordPress installation is accessible at this host
//if ( $ssl_host->isValid() ) {
// If secure domain has changed and currently on SSL, logout user
if ( $this->getPlugin()->isSsl() ) {
$logout = true;
}
$_POST[$key] = $ssl_host->toString();
/*} else {
$errors[] = '<strong>SSL Host</strong> - Invalid WordPress installation at ' . $ssl_host;
$_POST[$key] = get_option($key);
}*/
} else {
$_POST[$key] = $this->getPlugin()->getHttpsUrl()->toString();
}
} else {
$_POST[$key] = get_option($key);
}
} else if ( $key == 'ssl_proxy' ) {
// Reload if we're auto detecting the proxy and we're not in SSL
if ( $_POST[$key] == 'auto' && ! $this->getPlugin()->isSsl() ) {
$reload = true;
}
} else if ( $key == 'ssl_admin' ) {
if ( force_ssl_admin() && $this->getPlugin()->getSetting('ssl_host_diff') ) {
$errors[] = '<strong>SSL Admin</strong> - FORCE_SSL_ADMIN should not be set to true in your wp-config.php while using a non-default SSL Host.';
// If forcing SSL Admin and currently not SSL, logout user
} else if ( $_POST[$key] == 1 && !$this->getPlugin()->isSsl() ) {
$logout = true;
}
} else if ( $key == 'ssl_host_subdomain' ) {
// Checks to see if the SSL Host is a subdomain
$is_subdomain = $this->getPlugin()->getHttpsUrl()->isSubdomain($this->getPlugin()->getHttpUrl());
if ( $ssl_host->setScheme('http') != $this->getPlugin()->getHttpUrl() && $is_subdomain ) {
$_POST[$key] = 1;
} else {
$_POST[$key] = 0;
}
}
$this->getPlugin()->setSetting($key, $_POST[$key]);
}
}
if ( $logout ) {
wp_logout();
}
$this->getPlugin()->renderView('ajax_message', array('message' => $message, 'errors' => $errors, 'reload' => $reload, 'logout' => $logout));
}
}