forked from itthinx/groups-404-redirect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroups-404-redirect.php
385 lines (329 loc) · 13.6 KB
/
groups-404-redirect.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<?php
/**
* groups-404-redirect.php
*
* Copyright (c) 2013 "kento" Karim Rahimpur www.itthinx.com
*
* This code is released under the GNU General Public License.
* See COPYRIGHT.txt and LICENSE.txt.
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This header and all notices must be kept intact.
*
* @author Karim Rahimpur
* @package groups-404-redirect
* @since groups-404-redirect 1.0.0
*
* Plugin Name: Groups 404 Redirect
* Plugin URI: http://www.itthinx.com/plugins/groups
* Description: Redirect 404's when a visitor tries to access a page protected by <a href="http://wordpress.org/extend/plugins/groups/">Groups</a>.
* Version: 1.2.3
* Author: itthinx
* Author URI: http://www.itthinx.com
* Donate-Link: http://www.itthinx.com
* License: GPLv3
*/
define( 'GROUPS_404_REDIRECT_PLUGIN_DOMAIN', 'groups-404-redirect' );
/**
* Redirection.
*/
class Groups_404_Redirect {
/**
* Initialize hooks.
*/
public static function init() {
// register_activation_hook(__FILE__, array( __CLASS__,'activate' ) );
register_deactivation_hook(__FILE__, array( __CLASS__,'deactivate' ) );
add_action( 'wp', array( __CLASS__, 'wp' ) );
add_action( 'admin_menu', array( __CLASS__, 'admin_menu' ) );
if ( is_admin() ) {
add_filter( 'plugin_action_links_'. plugin_basename( __FILE__ ), array( __CLASS__, 'admin_settings_link' ) );
}
}
/**
* Nothing to do.
*/
public static function activate() {
}
/**
* Delete settings.
*/
public static function deactivate() {
if ( self::groups_is_active() ) {
Groups_Options::delete_option( 'groups-404-redirect-to' );
Groups_Options::delete_option( 'groups-404-redirect-post-id' );
}
}
/**
* Add the Settings > Groups 404 section.
*/
public static function admin_menu() {
add_options_page(
'Groups 404 Redirect',
'Groups 404',
'manage_options',
'groups-404-redirect',
array( __CLASS__, 'settings' )
);
}
/**
* Adds plugin links.
*
* @param array $links
* @param array $links with additional links
*/
public static function admin_settings_link( $links ) {
$links[] = '<a href="' . get_admin_url( null,'options-general.php?page=groups-404-redirect' ) . '">' . __( 'Settings', GROUPS_404_REDIRECT_PLUGIN_DOMAIN ) . '</a>';
return $links;
}
/**
* Admin settings.
*/
public static function settings() {
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'Access denied.', GROUPS_404_REDIRECT_PLUGIN_DOMAIN ) );
}
if ( !self::groups_is_active() ) {
echo '<p>';
echo __( 'Please install and activate <a href="http://wordpress.org/extend/plugins/groups/">Groups</a> to use this plugin.', GROUPS_404_REDIRECT_PLUGIN_DOMAIN );
echo '</p>';
return;
}
$http_status_codes = array(
'301' => __( 'Moved Permanently', GROUPS_404_REDIRECT_PLUGIN_DOMAIN ),
'302' => __( 'Found', GROUPS_404_REDIRECT_PLUGIN_DOMAIN ),
'303' => __( 'See Other', GROUPS_404_REDIRECT_PLUGIN_DOMAIN ),
'307' => __( 'Temporary Redirect', GROUPS_404_REDIRECT_PLUGIN_DOMAIN )
);
if ( isset( $_POST['action'] ) && ( $_POST['action'] == 'save' ) && wp_verify_nonce( $_POST['groups-404-redirect'], 'admin' ) ) {
$redirect_to = 'post';
if ( !empty( $_POST['redirect_to'] ) ) {
switch( $_POST['redirect_to'] ) {
case 'post' :
case 'login' :
Groups_Options::update_option( 'groups-404-redirect-to', $_POST['redirect_to'] );
break;
}
}
if ( !empty( $_POST['post_id'] ) ) {
Groups_Options::update_option( 'groups-404-redirect-post-id', intval( $_POST['post_id'] ) );
} else {
Groups_Options::delete_option( 'groups-404-redirect-post-id' );
}
if ( !empty( $_POST['post_param'] ) ) {
Groups_Options::update_option( 'groups-404-redirect-post-param', $_POST['post_param'] );
} else {
Groups_Options::delete_option( 'groups-404-redirect-post-param' );
}
Groups_Options::update_option( 'groups-404-redirect-restricted-terms', !empty( $_POST['redirect_restricted_terms'] ) );
if ( key_exists( $_POST['status'], $http_status_codes ) ) {
Groups_Options::update_option( 'groups-404-redirect-status', $_POST['status'] );
}
echo
'<p class="info">' .
__( 'The settings have been saved.', GROUPS_404_REDIRECT_PLUGIN_DOMAIN ) .
'</p>';
}
$redirect_to = Groups_Options::get_option( 'groups-404-redirect-to', 'post' );
$post_id = Groups_Options::get_option( 'groups-404-redirect-post-id', '' );
$post_param = Groups_Options::get_option( 'groups-404-redirect-post-param', '' );
$redirect_status = Groups_Options::get_option( 'groups-404-redirect-status', '301' );
$redirect_restricted_terms = Groups_Options::get_option( 'groups-404-redirect-restricted-terms', false );
echo '<h1>';
echo __( 'Groups 404 Redirect', GROUPS_404_REDIRECT_PLUGIN_DOMAIN );
echo '</h1>';
echo '<p>';
echo __( 'Redirect settings when a visitor tries to access a page protected by Groups.', GROUPS_404_REDIRECT_PLUGIN_DOMAIN );
echo '</p>';
echo '<div class="settings">';
echo '<form name="settings" method="post" action="">';
echo '<div>';
echo '<label>';
echo sprintf( '<input type="radio" name="redirect_to" value="post" %s />', $redirect_to == 'post' ? ' checked="checked" ' : '' );
echo ' ';
echo __( 'Redirect to a page or post', GROUPS_404_REDIRECT_PLUGIN_DOMAIN );
echo '</label>';
echo '<div style="margin: 1em 0 0 2em">';
echo '<label>';
echo __( 'Page or Post ID', GROUPS_404_REDIRECT_PLUGIN_DOMAIN );
echo ' ';
echo sprintf( '<input type="text" name="post_id" value="%s" />', $post_id );
echo '</label>';
if ( !empty( $post_id ) ) {
$post_title = get_the_title( $post_id );
echo '<p>';
echo sprintf( __( 'Title: <em>%s</em>', GROUPS_404_REDIRECT_PLUGIN_DOMAIN ), $post_title );
echo '</p>';
}
echo '<p class="description">';
echo __( 'Indicate the ID of a page or a post to redirect to, leave it empty to redirect to the home page.', GROUPS_404_REDIRECT_PLUGIN_DOMAIN );
echo '<br/>';
echo __( 'The title of the page will be shown if a valid ID has been given.', GROUPS_404_REDIRECT_PLUGIN_DOMAIN );
echo '</p>';
echo '<p class="description">';
echo __( 'If the <strong>Redirect to the WordPress login</strong> option is chosen instead, visitors who are logged in but may not access a requested page, can be redirected to a specific page by setting the Page or Post ID here.', GROUPS_404_REDIRECT_PLUGIN_DOMAIN );
echo '</p>';
echo '<label>';
echo __( 'Parameter name', GROUPS_404_REDIRECT_PLUGIN_DOMAIN );
echo ' ';
echo sprintf( '<input type="text" name="post_param" value="%s" />', $post_param );
echo '</label>';
echo '<p class="description">';
echo __( 'Indicate the parameter name which gets the original url requested when redirecting to given page or post.', GROUPS_404_REDIRECT_PLUGIN_DOMAIN );
echo '</p>';
echo '</div>';
echo '<br/>';
echo '<label>';
echo sprintf( '<input type="radio" name="redirect_to" value="login" %s />', $redirect_to == 'login' ? ' checked="checked" ' : '' );
echo ' ';
echo __( 'Redirect to the WordPress login', GROUPS_404_REDIRECT_PLUGIN_DOMAIN );
echo '</label>';
echo '<div style="margin: 1em 0 0 2em">';
echo '<p class="description">';
echo __( 'If the visitor is logged in but is not allowed to access the requested page, the visitor will be taken to the home page, or, if a Page or Post ID is set, to the page indicated above.', GROUPS_404_REDIRECT_PLUGIN_DOMAIN );
echo '</p>';
echo '</div>';
echo '<br/>';
echo '<label>';
echo sprintf( '<input type="checkbox" name="redirect_restricted_terms" %s />', $redirect_restricted_terms ? ' checked="checked" ' : '' );
echo ' ';
echo __( 'Redirect restricted categories, tags and taxonomy terms …', GROUPS_404_REDIRECT_PLUGIN_DOMAIN );
echo '</label>';
echo '<div style="margin: 1em 0 0 2em">';
echo '<p class="description">';
echo __( 'If the visitor is not allowed to access the requested taxonomy term, including restricted categories and tags, the visitor will be redirected as indicated above.', GROUPS_404_REDIRECT_PLUGIN_DOMAIN );
echo '</p>';
echo '<p class="description">';
echo __( 'This option will only take effect if <a href="http://www.itthinx.com/plugins/groups-restrict-categories/">Groups Restrict Categories</a> is used.', GROUPS_404_REDIRECT_PLUGIN_DOMAIN );
echo '</p>';
echo '</div>';
echo '<br/>';
echo
'<p style="border-top:1px solid #eee; margin-top:1em; padding-top: 1em;">' .
'<label>' .
__( 'Redirect Status Code', GROUPS_404_REDIRECT_PLUGIN_DOMAIN ) .
' ' .
'<select name="status">';
foreach ( $http_status_codes as $code => $name ) {
echo '<option value="' . esc_attr( $code ) . '" ' . ( $redirect_status == $code ? ' selected="selected" ' : '' ) . '>' . $name . ' (' . $code . ')' . '</option>';
}
echo
'</select>' .
'</label>' .
'</p>';
echo '<p class="description">';
echo __( '<a href="http://www.w3.org/Protocols/rfc2616/rfc2616.html">RFC 2616</a> provides details on <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html">Status Code Definitions</a>.', GROUPS_404_REDIRECT_PLUGIN_DOMAIN );
echo '</p>';
wp_nonce_field( 'admin', 'groups-404-redirect', true, true );
echo '<br/>';
echo '<div class="buttons">';
echo sprintf( '<input class="create button" type="submit" name="submit" value="%s" />', __( 'Save', GROUPS_404_REDIRECT_PLUGIN_DOMAIN ) );
echo '<input type="hidden" name="action" value="save" />';
echo '</div>';
echo '</div>';
echo '</form>';
echo '</div>';
}
/**
* Handles redirection.
*/
public static function wp() {
global $wp_query;
$is_restricted_term = false;
if ( class_exists( 'Groups_Options' ) && class_exists( 'Groups_Restrict_Categories' ) ) {
$redirect_restricted_terms = Groups_Options::get_option( 'groups-404-redirect-restricted-terms', false );
if ( $redirect_restricted_terms ) {
$is_term = $wp_query->is_category || $wp_query->is_tag || $wp_query->is_tax;
if ( $is_term ) {
$restricted_term_ids = Groups_Restrict_Categories::get_user_restricted_term_ids( get_current_user_id() );
$term_id = $wp_query->get_queried_object_id();
if ( in_array( $term_id, $restricted_term_ids ) ) {
$is_restricted_term = true;
}
}
}
}
if ( $wp_query->is_404 || $is_restricted_term ) {
if ( self::groups_is_active() ) {
$redirect_to = Groups_Options::get_option( 'groups-404-redirect-to', 'post' );
$post_id = Groups_Options::get_option( 'groups-404-redirect-post-id', '' );
$post_param = Groups_Options::get_option( 'groups-404-redirect-post-param', '' );
$redirect_status = intval( Groups_Options::get_option( 'groups-404-redirect-status', '301' ) );
$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$current_post_id = url_to_postid( $current_url );
if ( !$current_post_id ) {
$current_post_id = $wp_query->get_queried_object_id();
}
if ( !$current_post_id ) {
require_once 'groups-404-url-to-postid.php';
$current_post_id = groups_404_url_to_postid( $current_url );
}
if ( $current_post_id ) {
$is_restricted_by_term = false;
if ( class_exists( 'Groups_Restrict_Categories' ) && method_exists( 'Groups_Restrict_Categories', 'user_can_read' ) ) {
$is_restricted_by_term = !Groups_Restrict_Categories::user_can_read( $current_post_id );
}
if ( !Groups_Post_Access::user_can_read_post( $current_post_id, get_current_user_id() ) || $is_restricted_by_term || $is_restricted_term ) {
switch( $redirect_to ) {
case 'login' :
if ( !is_user_logged_in() ) {
wp_redirect( wp_login_url( $current_url ), $redirect_status );
exit;
} else {
// If the user is already logged in, we can't
// redirect to the WordPress login again,
// we either send them to the home page, or
// to the page indicated in the settings.
if ( empty( $post_id ) ) {
wp_redirect( get_home_url(), $redirect_status );
} else {
$post_id = apply_filters( 'groups_404_redirect_post_id', $post_id, $current_post_id, $current_url );
if ( $post_id != $current_post_id ) {
wp_redirect( get_permalink( $post_id ), $redirect_status );
} else {
return;
}
}
exit;
}
default: // 'post'
if ( empty( $post_id ) ) {
$redirect_url = get_home_url();
} else {
$post_id = apply_filters( 'groups_404_redirect_post_id', $post_id, $current_post_id, $current_url );
if ( $post_id != $current_post_id ) {
$redirect_url = get_permalink( $post_id );
} else {
return;
}
}
if ( !empty( $post_param ) ) {
$redirect_url = add_query_arg($post_param, urlencode($current_url), $redirect_url);
}
wp_redirect( $redirect_url, $redirect_status );
exit;
}
}
}
}
}
}
/**
* Returns true if the Groups plugin is active.
* @return boolean true if Groups is active
*/
private static function groups_is_active() {
$active_plugins = get_option( 'active_plugins', array() );
if ( is_multisite() ) {
$active_sitewide_plugins = get_site_option( 'active_sitewide_plugins', array() );
$active_sitewide_plugins = array_keys( $active_sitewide_plugins );
$active_plugins = array_merge( $active_plugins, $active_sitewide_plugins );
}
return in_array( 'groups/groups.php', $active_plugins );
}
}
Groups_404_Redirect::init();