-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprimary-redirect.php
More file actions
333 lines (299 loc) · 8.69 KB
/
Copy pathprimary-redirect.php
File metadata and controls
333 lines (299 loc) · 8.69 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
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
325
326
327
328
329
330
331
332
333
<?php
/**
* Plugin Name: Primary Redirect
* Plugin URI: https://handyplugins.co
* Description: Redirects users to a custom URL or their primary blog's dashboard after login, replacing the default WordPress behavior.
* Version: 2.0.2
* Requires at least: 5.0
* Requires PHP: 7.4
* Author: HandyPlugins
* Author URI: https://handyplugins.co/
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: primary-redirect
* Domain Path: /languages
*
* @package PrimaryRedirect
*/
// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Define plugin constants.
define( 'PRIMARY_REDIRECT_VERSION', '2.0.2' );
define( 'PRIMARY_REDIRECT_PLUGIN_FILE', __FILE__ );
define( 'PRIMARY_REDIRECT_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'PRIMARY_REDIRECT_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
/**
* Main Primary Redirect Class
*
* @since 2.0.0
*/
class Primary_Redirect {
/**
* Plugin instance.
*
* @since 2.0.0
* @var Primary_Redirect
*/
private static $instance = null;
/**
* Get plugin instance.
*
* @since 2.0.0
* @return Primary_Redirect
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor.
*
* @since 2.0.0
*/
private function __construct() {
$this->init();
}
/**
* Initialize the plugin.
*
* @since 2.0.0
*/
private function init() {
add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) );
add_filter( 'login_redirect', array( $this, 'handle_login_redirect' ), 10, 3 );
if ( is_multisite() ) {
add_action( 'wpmu_options', array( $this, 'render_network_settings' ) );
add_action( 'update_wpmu_options', array( $this, 'update_network_settings' ) );
} else {
add_action( 'admin_init', array( $this, 'register_single_site_settings' ) );
}
}
/**
* Load plugin textdomain.
*
* @since 2.0.0
*/
public function load_textdomain() {
load_plugin_textdomain(
'primary-redirect',
false,
dirname( plugin_basename( __FILE__ ) ) . '/languages/'
);
}
/**
* Handle login redirect.
*
* @since 2.0.0
* @param string $redirect_to The redirect destination URL.
* @param string $requested_redirect_to The requested redirect destination URL passed as a parameter.
* @param WP_User $user The WP_User object for the user being redirected.
* @return string The redirect URL.
*/
public function handle_login_redirect( $redirect_to, $requested_redirect_to, $user ) {
// Don't redirect if there's an error with the user.
if ( is_wp_error( $user ) ) {
return $redirect_to;
}
// Don't redirect for interim login or reauth.
$interim_login = isset( $_REQUEST['interim-login'] );
$reauth = ! empty( $_REQUEST['reauth'] );
if ( $interim_login || $reauth ) {
return $redirect_to;
}
// Check if primary dashboard redirect is enabled (multisite only).
if ( is_multisite() && $this->is_primary_dashboard_redirect_enabled() ) {
$primary_redirect_url = $this->get_primary_dashboard_url( $user );
if ( $primary_redirect_url ) {
return $primary_redirect_url;
}
}
// Check for custom redirect URL.
$custom_redirect_url = $this->get_custom_redirect_url();
if ( ! empty( $custom_redirect_url ) ) {
return esc_url_raw( $custom_redirect_url );
}
return $redirect_to;
}
/**
* Check if primary dashboard redirect is enabled.
*
* @since 2.0.0
* @return bool
*/
private function is_primary_dashboard_redirect_enabled() {
return '1' === get_site_option( 'primary_redirect_dashboard_enabled', '0' );
}
/**
* Get primary dashboard URL for user.
*
* @since 2.0.0
* @param WP_User $user The user object.
* @return string|false Primary dashboard URL or false if not available.
*/
private function get_primary_dashboard_url( $user ) {
if ( ! is_multisite() || ! isset( $user->primary_blog ) ) {
return false;
}
$primary_blog_id = absint( $user->primary_blog );
if ( $primary_blog_id <= 0 ) {
return false;
}
$primary_url = get_blogaddress_by_id( $primary_blog_id );
if ( $primary_url ) {
return trailingslashit( $primary_url ) . 'wp-admin/';
}
return false;
}
/**
* Get custom redirect URL.
*
* @since 2.0.0
* @return string
*/
private function get_custom_redirect_url() {
if ( is_multisite() ) {
return get_site_option( 'primary_redirect_url', '' );
}
return get_option( 'primary_redirect_url', '' );
}
/**
* Render network settings.
*
* @since 2.0.0
*/
public function render_network_settings() {
$redirect_url = get_site_option( 'primary_redirect_url', '' );
$dashboard_enabled = get_site_option( 'primary_redirect_dashboard_enabled', '0' );
?>
<h3><?php esc_html_e( 'Primary Redirect Settings', 'primary-redirect' ); ?></h3>
<table class="form-table">
<tr>
<th scope="row">
<label for="primary_redirect_url"><?php esc_html_e( 'Custom Redirect URL', 'primary-redirect' ); ?></label>
</th>
<td>
<input
name="primary_redirect_url"
type="url"
id="primary_redirect_url"
value="<?php echo esc_attr( $redirect_url ); ?>"
class="regular-text"
placeholder="https://example.com/dashboard"
/>
<p class="description">
<?php esc_html_e( 'Enter a custom URL to redirect users after login. Leave empty to use WordPress default behavior.', 'primary-redirect' ); ?>
</p>
</td>
</tr>
<tr>
<th scope="row">
<?php esc_html_e( 'Primary Dashboard Redirect', 'primary-redirect' ); ?>
</th>
<td>
<label>
<input
type="checkbox"
name="primary_redirect_dashboard_enabled"
value="1"
<?php checked( $dashboard_enabled, '1' ); ?>
/>
<?php esc_html_e( 'Redirect users to their primary blog dashboard (overrides custom URL)', 'primary-redirect' ); ?>
</label>
<p class="description">
<?php esc_html_e( 'This option is only available in multisite installations.', 'primary-redirect' ); ?>
</p>
</td>
</tr>
</table>
<?php
}
/**
* Update network settings.
*
* @since 2.0.0
*/
public function update_network_settings() {
// Verify user capabilities.
if ( ! current_user_can( 'manage_network_options' ) ) {
return;
}
// Sanitize and update redirect URL.
$redirect_url = isset( $_POST['primary_redirect_url'] ) ? esc_url_raw( wp_unslash( $_POST['primary_redirect_url'] ) ) : '';
update_site_option( 'primary_redirect_url', $redirect_url );
// Update dashboard redirect setting.
$dashboard_enabled = isset( $_POST['primary_redirect_dashboard_enabled'] ) ? '1' : '0';
update_site_option( 'primary_redirect_dashboard_enabled', $dashboard_enabled );
}
/**
* Register single site settings.
*
* @since 2.0.0
*/
public function register_single_site_settings() {
add_settings_section(
'primary_redirect_settings',
__( 'Primary Redirect Settings', 'primary-redirect' ),
array( $this, 'render_settings_section_description' ),
'general'
);
add_settings_field(
'primary_redirect_url',
__( 'Redirect URL after login', 'primary-redirect' ),
array( $this, 'render_single_site_setting_field' ),
'general',
'primary_redirect_settings'
);
register_setting(
'general',
'primary_redirect_url',
array(
'type' => 'string',
'sanitize_callback' => 'esc_url_raw',
'default' => '',
)
);
}
/**
* Render settings section description.
*
* @since 2.0.0
*/
public function render_settings_section_description() {
echo '<p>' . esc_html__( 'Configure where users should be redirected after logging in.', 'primary-redirect' ) . '</p>';
}
/**
* Render single site setting field.
*
* @since 2.0.0
*/
public function render_single_site_setting_field() {
$redirect_url = get_option( 'primary_redirect_url', '' );
?>
<input
name="primary_redirect_url"
type="url"
id="primary_redirect_url"
value="<?php echo esc_attr( $redirect_url ); ?>"
class="regular-text"
placeholder="https://example.com/dashboard"
/>
<p class="description">
<?php esc_html_e( 'Enter a custom URL to redirect users after login. Leave empty to use WordPress default behavior.', 'primary-redirect' ); ?>
</p>
<?php
}
}
/**
* Initialize the plugin.
*
* @since 2.0.0
*/
function primary_redirect_init() {
Primary_Redirect::get_instance();
}
// Initialize the plugin.
add_action( 'init', 'primary_redirect_init' );