Skip to content
This repository was archived by the owner on Sep 2, 2021. It is now read-only.

Commit 6bf6bb9

Browse files
committed
Refactor some classes of the redirect feature. #174 WIP!
1 parent f1bfebc commit 6bf6bb9

File tree

4 files changed

+143
-116
lines changed

4 files changed

+143
-116
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php # -*- coding: utf-8 -*-
2+
3+
/**
4+
* Stops the redirect if the user has turned it off.
5+
*/
6+
class Mlp_Redirect_Filter {
7+
8+
/**
9+
* @var int
10+
*/
11+
private $current_user_id;
12+
13+
/**
14+
* @var string
15+
*/
16+
private $meta_key;
17+
18+
/**
19+
* Constructor.
20+
*
21+
* @param string $meta_key Meta key of the redirect user setting.
22+
*/
23+
public function __construct( $meta_key ) {
24+
25+
$this->current_user_id = get_current_user_id();
26+
}
27+
28+
/**
29+
* Stops the redirect if the user has turned it off.
30+
*
31+
* @wp-hook mlp_do_redirect
32+
*
33+
* @param bool $redirect Redirect the current request?
34+
*
35+
* @return bool
36+
*/
37+
public function filter_redirect( $redirect ) {
38+
39+
if ( ! $this->current_user_id ) {
40+
return $redirect;
41+
}
42+
43+
$user_setting = (bool) get_user_meta( $this->current_user_id, $this->meta_key );
44+
if ( $user_setting ) {
45+
return false;
46+
}
47+
48+
return $redirect;
49+
}
50+
}
Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
<?php
1+
<?php # -*- coding: utf-8 -*-
22

33
/**
4-
* Redirect visitors to the best matching language alternative.
5-
*
6-
* @version 2015.08.24
7-
* @author Inpsyde GmbH, toscho
8-
* @license GPL
9-
* @package MultilingualPress
10-
* @subpackage Redirect
4+
* Redirects visitors to the best matching language alternative.
115
*/
126
class Mlp_Redirect_Frontend {
137

@@ -24,17 +18,18 @@ class Mlp_Redirect_Frontend {
2418
/**
2519
* Constructor.
2620
*
27-
* @param Mlp_Redirect_Response_Interface $response
28-
* @param string $option_name
21+
* @param Mlp_Redirect_Response_Interface $response Redirect response object.
22+
* @param string $option_name Option name.
2923
*/
3024
public function __construct( Mlp_Redirect_Response_Interface $response, $option_name ) {
3125

3226
$this->response = $response;
27+
3328
$this->option_name = $option_name;
3429
}
3530

3631
/**
37-
* Initialize redirect data and response classes.
32+
* Initializes the redirect data and response classes.
3833
*
3934
* @return void
4035
*/
@@ -50,68 +45,69 @@ public function setup() {
5045
}
5146

5247
/**
53-
* add noredirect query var to the links
48+
* Adds the noredirect query var to the given URL.
5449
*
5550
* @wp-hook mlp_linked_element_link
5651
*
57-
* @param string $link
58-
* @param int $site_id
52+
* @param string $url URL.
53+
* @param int $site_id Site ID.
5954
*
60-
* @return string
55+
* @return string
6156
*/
62-
public function add_noredirect_parameter( $link, $site_id ) {
57+
public function add_noredirect_parameter( $url, $site_id ) {
6358

64-
$link = (string) $link;
65-
if ( empty( $link ) ) {
66-
return $link;
59+
$url = (string) $url;
60+
if ( ! $url ) {
61+
return $url;
6762
}
6863

6964
$languages = mlp_get_available_languages();
7065
if ( empty( $languages[ $site_id ] ) ) {
71-
return $link;
66+
return $url;
7267
}
7368

74-
return add_query_arg( 'noredirect', $languages[ $site_id ], $link );
69+
$url = add_query_arg( 'noredirect', $languages[ $site_id ], $url );
70+
71+
return $url;
7572
}
7673

7774
/**
78-
* Is there an accept header and the feature is active for the site and the current language is not in the
79-
* noredirect cookie?
75+
* Checks if the current request should be redirected.
76+
*
77+
* Requires an accept header, the redirect feature being active for the current site, and the current language not
78+
* being included in the $_SESSION's noredirect element.
8079
*
8180
* @return bool
8281
*/
8382
public function is_redirectable() {
8483

85-
if ( empty( $_SERVER[ 'HTTP_ACCEPT_LANGUAGE' ] ) ) {
86-
return FALSE;
84+
if ( empty( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ) {
85+
return false;
86+
}
87+
88+
if ( ! get_option( $this->option_name ) ) {
89+
return false;
8790
}
8891

8992
if ( ! isset( $_SESSION ) && ! session_id() ) {
9093
session_start();
9194
}
9295

93-
if ( isset( $_SESSION[ 'noredirect' ] ) ) {
96+
if ( isset( $_SESSION['noredirect'] ) ) {
9497
$current_site_language = mlp_get_current_blog_language();
9598

96-
$existing = (array) $_SESSION[ 'noredirect' ];
97-
98-
if ( in_array( $current_site_language, $existing ) ) {
99-
return FALSE;
99+
if ( in_array( $current_site_language, (array) $_SESSION['noredirect'] ) ) {
100+
return false;
100101
}
101102
}
102103

103104
/**
104-
* Filter whether the user should be redirected.
105+
* Filters if the current request should be redirected.
105106
*
106-
* @param bool $do_redirect Redirect or not?
107+
* @param bool $redirect Redirect the current request?
107108
*
108109
* @return bool
109110
*/
110-
if ( ! apply_filters( 'mlp_do_redirect', TRUE ) ) {
111-
return FALSE;
112-
}
113-
114-
return (bool) get_option( $this->option_name );
111+
return (bool) apply_filters( 'mlp_do_redirect', true );
115112
}
116-
117113
}
Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
<?php
1+
<?php # -*- coding: utf-8 -*-
22

33
/**
4-
* Send redirect headers.
5-
*
6-
* @version 2015.08.24
7-
* @author Inpsyde GmbH, toscho
8-
* @license GPL
9-
* @package MultilingualPress
10-
* @subpackage Redirect
4+
* Sends redirect headers.
115
*/
126
class Mlp_Redirect_Response implements Mlp_Redirect_Response_Interface {
137

@@ -19,87 +13,94 @@ class Mlp_Redirect_Response implements Mlp_Redirect_Response_Interface {
1913
/**
2014
* Constructor.
2115
*
22-
* @param Mlp_Language_Negotiation_Interface $negotiation
16+
* @param Mlp_Language_Negotiation_Interface $negotiation Language negotiation object.
2317
*/
2418
public function __construct( Mlp_Language_Negotiation_Interface $negotiation ) {
2519

2620
$this->negotiation = $negotiation;
2721
}
2822

2923
/**
30-
* Redirect if needed.
24+
* Redirects if needed.
3125
*
32-
* @return void
26+
* @return bool
3327
*/
3428
public function redirect() {
3529

36-
if ( ! empty( $_GET[ 'noredirect' ] ) ) {
37-
$this->save_session( $_GET[ 'noredirect' ] );
30+
if ( ! empty( $_GET['noredirect'] ) ) {
31+
$this->save_session( $_GET['noredirect'] );
3832

39-
return;
33+
return false;
4034
}
4135

42-
$match = $this->negotiation->get_redirect_match();
43-
if ( $match[ 'site_id' ] === get_current_blog_id() ) {
44-
return;
45-
}
36+
$redirect_match = $this->negotiation->get_redirect_match();
4637

47-
$url = $match[ 'url' ];
38+
$current_site_id = get_current_blog_id();
4839

49-
$current_blog_id = get_current_blog_id();
40+
if ( $redirect_match['site_id'] === $current_site_id ) {
41+
return false;
42+
}
5043

5144
/**
52-
* Filter the redirect URL.
53-
*
54-
* You might add support for other types than singular posts and home here.
55-
* If you return an empty string, the redirect will not happen. The result will be validated with esc_url().
45+
* Filters the redirect URL.
5646
*
5747
* @param string $url Redirect URL.
58-
* @param array $match Redirect match. {
59-
* 'priority' => int
60-
* 'url' => string
61-
* 'language' => string
62-
* 'site_id' => int
48+
* @param array $redirect_match Redirect match. {
49+
* 'priority' => int
50+
* 'url' => string
51+
* 'language' => string
52+
* 'site_id' => int
6353
* }
64-
* @param int $current_blog_id Current blog ID.
54+
* @param int $current_site_id Current site ID.
6555
*
6656
* @return string
6757
*/
68-
$url = apply_filters( 'mlp_redirect_url', $url, $match, $current_blog_id );
69-
if ( empty( $url ) ) {
70-
return;
58+
$url = (string) apply_filters( 'mlp_redirect_url', $redirect_match['url'], $redirect_match, $current_site_id );
59+
if ( ! $url ) {
60+
return false;
7161
}
7262

73-
$this->save_session( $match[ 'language' ] );
63+
$this->save_session( $redirect_match['language'] );
7464

7565
wp_redirect( $url );
76-
exit;
66+
$this->call_exit();
67+
68+
return true;
69+
}
70+
71+
/**
72+
* Wrapper for exit(). Allows to easily test this class.
73+
*
74+
* @return void
75+
*/
76+
public function call_exit() {
77+
78+
exit();
7779
}
7880

7981
/**
80-
* Save no redirect data in session
82+
* Saves the given language to the noredirect data in the $_SESSION superglobal.
8183
*
8284
* @param string $language Language code.
8385
*
8486
* @return void
8587
*/
86-
function save_session( $language ) {
88+
private function save_session( $language ) {
8789

88-
// Check to only session_start if no session is active
8990
if ( ! isset( $_SESSION ) && ! session_id() ) {
9091
session_start();
9192
}
9293

93-
if ( isset( $_SESSION[ 'noredirect' ] ) ) {
94-
$existing = (array) $_SESSION[ 'noredirect' ];
94+
if ( ! isset( $_SESSION['noredirect'] ) ) {
95+
$_SESSION['noredirect'] = array();
96+
} else {
97+
$_SESSION['noredirect'] = (array) $_SESSION['noredirect'];
9598

96-
if ( in_array( $language, $existing ) ) {
99+
if ( in_array( $language, $_SESSION['noredirect'] ) ) {
97100
return;
98101
}
99-
100102
}
101103

102-
$_SESSION[ 'noredirect' ][] = $language;
104+
$_SESSION['noredirect'][] = $language;
103105
}
104-
105106
}
Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,31 @@
1-
<?php
1+
<?php # -*- coding: utf-8 -*-
2+
23
/**
3-
* Allow users to disable automatic redirects in their profile.
4-
*
5-
* @version 2014.07.05
6-
* @author Inpsyde GmbH, toscho
7-
* @license GPL
4+
* Allows users to disable automatic redirects in their profile.
85
*/
96
class Mlp_Redirect_User_Settings {
107

118
/**
129
* @var string
1310
*/
14-
private $key = 'mlp_redirect';
11+
private $meta_key = 'mlp_redirect';
1512

1613
/**
17-
* Initialize the objects.
14+
* Initializes the objects.
1815
*
1916
* @return void
2017
*/
2118
public function setup() {
2219

23-
$nonce = new Inpsyde_Nonce_Validator( $this->key );
24-
$view = new Mlp_Redirect_User_Settings_Html( $this->key, $nonce );
25-
$updater = new Mlp_User_Settings_Updater( $this->key, $nonce );
26-
$controller = new Mlp_User_Settings_Controller( $view, $updater );
27-
$controller->setup();
28-
29-
add_filter( 'mlp_do_redirect', array ( $this, 'intercept_redirect' ) );
30-
}
31-
32-
/**
33-
* Stop redirect when the user has turned it off.
34-
*
35-
* This needs a better place.
36-
*
37-
* @param bool $bool
38-
* @return bool
39-
*/
40-
public function intercept_redirect( $bool ) {
41-
42-
$user = wp_get_current_user();
43-
44-
if ( ! is_a( $user, 'WP_User' ) )
45-
return $bool;
20+
$nonce = new Inpsyde_Nonce_Validator( $this->meta_key );
4621

47-
$current = (int) get_user_meta( $user->ID, $this->key );
22+
$user_settings_controller = new Mlp_User_Settings_Controller(
23+
new Mlp_Redirect_User_Settings_Html( $this->meta_key, $nonce ),
24+
new Mlp_User_Settings_Updater( $this->meta_key, $nonce )
25+
);
26+
$user_settings_controller->setup();
4827

49-
return 1 === $current ? FALSE : $bool;
28+
$redirect_filter = new Mlp_Redirect_Filter( $this->meta_key );
29+
add_filter( 'mlp_do_redirect', array( $redirect_filter, 'filter_redirect' ) );
5030
}
51-
}
31+
}

0 commit comments

Comments
 (0)