forked from wpscholar-wp-plugins/simple-website-redirect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleWebsiteRedirect.php
259 lines (221 loc) · 6.68 KB
/
SimpleWebsiteRedirect.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
<?php
/*
* Plugin Name: Simple Website Redirect
* Plugin URI: https://wpscholar.com/wordpress-plugins/simple-website-redirect/
* Description: A simple plugin designed to redirect an entire website (except the WordPress admin) to another website.
* Version: 1.0
* Author: Micah Wood
* Author URI: https://wpscholar.com
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: simple-website-redirect
*/
/**
* Class SimpleWebsiteRedirect
*/
class SimpleWebsiteRedirect {
/**
* @var string Plugin admin menu page slug.
*/
const PAGE = 'simple-website-redirect';
/**
* Hook our custom functions into WordPress core.
*/
public static function initialize() {
load_plugin_textdomain( 'simple-website-redirect', false, __DIR__ . '/languages' );
add_action( 'init', array( __CLASS__, '_init' ) );
add_action( 'admin_init', array( __CLASS__, '_admin_init' ) );
add_action( 'admin_menu', array( __CLASS__, '_admin_menu' ), 99 );
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( __CLASS__, '_plugin_action_links' ) );
}
/**
* Primary functionality - handles website redirect based on current configuration.
*/
public static function _init() {
global $pagenow;
// Allow requests to /wp-admin and wp-login so that admins can attempt to login
if ( $pagenow !== 'wp-login.php' && ! is_admin() ) {
$redirect_enabled = wp_validate_boolean( get_option( 'simple_website_redirect_status', false ) );
$redirect_url = self::sanitize_redirect_url( get_option( 'simple_website_redirect_url' ) );
if ( $redirect_enabled && ! empty( $redirect_url ) ) {
$redirect_type = self::sanitize_redirect_type( get_option( 'simple_website_redirect_type' ) );
wp_redirect( $redirect_url . $_SERVER['REQUEST_URI'], $redirect_type );
exit;
}
}
}
/**
* Sanitize redirect URL
*
* @param string $url
*
* @return string
*/
public static function sanitize_redirect_url( $url ) {
$clean_url = '';
$scheme = parse_url( $url, PHP_URL_SCHEME );
$host = untrailingslashit( parse_url( $url, PHP_URL_HOST ) );
if ( $scheme && $host ) {
$clean_url = "$scheme://$host";
}
return $clean_url;
}
/**
* Sanitize redirect type
*
* @param int $type
*
* @return int
*/
public static function sanitize_redirect_type( $type ) {
$clean_type = 301;
if ( 302 === absint( $type ) ) {
$clean_type = 302;
}
return $clean_type;
}
/**
* Register our settings.
*/
public static function _admin_init() {
register_setting( self::PAGE, 'simple_website_redirect_url', array( __CLASS__, 'sanitize_redirect_url' ) );
register_setting( self::PAGE, 'simple_website_redirect_type', array( __CLASS__, 'sanitize_redirect_type' ) );
register_setting( self::PAGE, 'simple_website_redirect_status', 'wp_validate_boolean' );
add_settings_section(
'settings',
esc_html__( 'Settings', 'simple-website-redirect' ),
'__return_null',
self::PAGE
);
add_settings_field(
'simple_website_redirect_url',
esc_html__( 'Redirect URL', 'simple-website-redirect' ),
array( __CLASS__, '_input_field' ),
self::PAGE,
'settings',
[
'name' => 'simple_website_redirect_url',
'type' => 'url',
'class' => 'regular-text',
'placeholder' => 'https://',
]
);
add_settings_field(
'simple_website_redirect_type',
esc_html__( 'Redirect Type', 'simple-website-redirect' ),
array( __CLASS__, '_select_field' ),
self::PAGE,
'settings',
[
'name' => 'simple_website_redirect_type',
'options' => array(
301 => esc_html__( 'Permanent', 'simple-website-redirect' ),
302 => esc_html__( 'Temporary', 'simple-website-redirect' ),
),
]
);
add_settings_field(
'simple_website_redirect_status',
esc_html__( 'Redirect Status', 'simple-website-redirect' ),
array( __CLASS__, '_select_field' ),
self::PAGE,
'settings',
[
'name' => 'simple_website_redirect_status',
'options' => array(
0 => esc_html__( 'Disabled', 'simple-website-redirect' ),
1 => esc_html__( 'Enabled', 'simple-website-redirect' ),
),
]
);
}
/**
* Add our custom admin menu page.
*/
public static function _admin_menu() {
add_submenu_page(
'options-general.php',
esc_html__( 'Simple Website Redirect', 'simple-website-redirect' ),
esc_html__( 'Website Redirect', 'simple-website-redirect' ),
'manage_options',
self::PAGE,
array( __CLASS__, '_render_page' )
);
}
/**
* Render admin page.
*/
public static function _render_page() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
settings_errors( self::PAGE );
?>
<div class="wrap">
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
<form action="options.php" method="post">
<?php
do_settings_sections( self::PAGE );
settings_fields( self::PAGE );
submit_button( esc_html__( 'Save Settings', 'simple-website-redirect' ) );
?>
</form>
</div>
<?php
}
/**
* Add settings link to plugin on plugin list in the WP admin.
*
* @param array $links
*
* @return array
*/
public static function _plugin_action_links( $links ) {
$settings_link = sprintf(
'<a href="%s">%s</a>',
esc_url( admin_url( 'options-general.php?page=' . self::PAGE ) ),
esc_html__( 'Settings', 'simple-website-redirect' )
);
$links[] = $settings_link;
return $links;
}
/**
* Outputs an input field.
*
* @param array $args
*/
public static function _input_field( array $args ) {
$name = isset( $args['name'] ) ? $args['name'] : '';
printf(
'<input type="%s" class="%s" name="%s" value="%s" placeholder="%s" />',
esc_attr( isset( $args['type'] ) ? $args['type'] : 'text' ),
esc_attr( isset( $args['class'] ) ? $args['class'] : '' ),
esc_attr( $name ),
esc_attr( get_option( $name, '' ) ),
esc_attr( isset( $args['placeholder'] ) ? $args['placeholder'] : '' )
);
}
/**
* Outputs a select field.
*
* @param array $args
*/
public static function _select_field( array $args ) {
$name = isset( $args['name'] ) ? $args['name'] : '';
$options = isset( $args['options'] ) ? (array) $args['options'] : [];
$value = get_option( $name, '' );
$opening_tag = sprintf( '<select name="%s">', esc_attr( $name ) ) . PHP_EOL;
$option_elements = array();
foreach ( $options as $option_value => $option_label ) {
$option_elements[] = sprintf(
'<option value="%s"%s>%s</option>',
esc_attr( $option_value ),
selected( $option_value, $value, false ),
esc_html( $option_label )
);
}
$closing_tag = '</select>' . PHP_EOL;
echo $opening_tag . implode( PHP_EOL, $option_elements ) . $closing_tag;
}
}
SimpleWebsiteRedirect::initialize();