PluginProbe
Redirection / 4.7.2
Redirection v4.7.2
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / redirection-settings.php

redirection-settings.php in Redirection 4.7.2, at redirection-settings.php

318 lines 9.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 define( 'REDIRECTION_OPTION', 'redirection_options' );
4 define( 'REDIRECTION_API_JSON', 0 );
5 define( 'REDIRECTION_API_JSON_INDEX', 1 );
6 define( 'REDIRECTION_API_JSON_RELATIVE', 3 );
7
8 function red_get_plugin_data( $plugin ) {
9 if ( ! function_exists( 'get_plugin_data' ) ) {
10 include_once ABSPATH . '/wp-admin/includes/plugin.php';
11 }
12
13 return get_plugin_data( $plugin );
14 }
15
16 function red_get_post_types( $full = true ) {
17 $types = get_post_types( array( 'public' => true ), 'objects' );
18 $types[] = (object) array(
19 'name' => 'trash',
20 'label' => __( 'Trash', 'default' ),
21 );
22
23 $post_types = array();
24 foreach ( $types as $type ) {
25 if ( $type->name === 'attachment' ) {
26 continue;
27 }
28
29 if ( $full && strlen( $type->label ) > 0 ) {
30 $post_types[ $type->name ] = $type->label;
31 } else {
32 $post_types[] = $type->name;
33 }
34 }
35
36 return apply_filters( 'redirection_post_types', $post_types );
37 }
38
39 function red_get_default_options() {
40 $flags = new Red_Source_Flags();
41 $defaults = [
42 'support' => false,
43 'token' => md5( uniqid() ),
44 'monitor_post' => 0, // Dont monitor posts by default
45 'monitor_types' => [],
46 'associated_redirect' => '',
47 'auto_target' => '',
48 'expire_redirect' => 7, // Expire in 7 days
49 'expire_404' => 7, // Expire in 7 days
50 'modules' => [],
51 'newsletter' => false,
52 'redirect_cache' => 1, // 1 hour
53 'ip_logging' => 1, // Full IP logging
54 'last_group_id' => 0,
55 'rest_api' => REDIRECTION_API_JSON,
56 'https' => false,
57 'headers' => [],
58 'database' => '',
59 'relocate' => '',
60 'preferred_domain' => '',
61 'aliases' => [],
62 ];
63 $defaults = array_merge( $defaults, $flags->get_json() );
64
65 return apply_filters( 'red_default_options', $defaults );
66 }
67
68 function red_set_options( array $settings = array() ) {
69 $options = red_get_options();
70 $monitor_types = array();
71
72 if ( isset( $settings['database'] ) ) {
73 $options['database'] = $settings['database'];
74 }
75
76 if ( isset( $settings['rest_api'] ) && in_array( intval( $settings['rest_api'], 10 ), array( 0, 1, 2, 3, 4 ), true ) ) {
77 $options['rest_api'] = intval( $settings['rest_api'], 10 );
78 }
79
80 if ( isset( $settings['monitor_types'] ) && is_array( $settings['monitor_types'] ) ) {
81 $allowed = red_get_post_types( false );
82
83 foreach ( $settings['monitor_types'] as $type ) {
84 if ( in_array( $type, $allowed, true ) ) {
85 $monitor_types[] = $type;
86 }
87 }
88
89 $options['monitor_types'] = $monitor_types;
90 }
91
92 if ( isset( $settings['associated_redirect'] ) ) {
93 $options['associated_redirect'] = '';
94
95 if ( strlen( $settings['associated_redirect'] ) > 0 ) {
96 $sanitizer = new Red_Item_Sanitize();
97 $options['associated_redirect'] = trim( $sanitizer->sanitize_url( $settings['associated_redirect'] ) );
98 }
99 }
100
101 if ( isset( $settings['monitor_types'] ) && count( $monitor_types ) === 0 ) {
102 $options['monitor_post'] = 0;
103 $options['associated_redirect'] = '';
104 } elseif ( isset( $settings['monitor_post'] ) ) {
105 $options['monitor_post'] = max( 0, intval( $settings['monitor_post'], 10 ) );
106
107 if ( ! Red_Group::get( $options['monitor_post'] ) && $options['monitor_post'] !== 0 ) {
108 $groups = Red_Group::get_all();
109
110 if ( count( $groups ) > 0 ) {
111 $options['monitor_post'] = $groups[0]['id'];
112 }
113 }
114 }
115
116 if ( isset( $settings['auto_target'] ) ) {
117 $options['auto_target'] = $settings['auto_target'];
118 }
119
120 if ( isset( $settings['last_group_id'] ) ) {
121 $options['last_group_id'] = max( 0, intval( $settings['last_group_id'], 10 ) );
122
123 if ( ! Red_Group::get( $options['last_group_id'] ) ) {
124 $groups = Red_Group::get_all();
125 $options['last_group_id'] = $groups[0]['id'];
126 }
127 }
128
129 if ( isset( $settings['support'] ) ) {
130 $options['support'] = $settings['support'] ? true : false;
131 }
132
133 if ( isset( $settings['token'] ) ) {
134 $options['token'] = $settings['token'];
135 }
136
137 if ( isset( $settings['https'] ) ) {
138 $options['https'] = $settings['https'] ? true : false;
139 }
140
141 if ( isset( $settings['token'] ) && trim( $options['token'] ) === '' ) {
142 $options['token'] = md5( uniqid() );
143 }
144
145 if ( isset( $settings['newsletter'] ) ) {
146 $options['newsletter'] = $settings['newsletter'] ? true : false;
147 }
148
149 if ( isset( $settings['expire_redirect'] ) ) {
150 $options['expire_redirect'] = max( -1, min( intval( $settings['expire_redirect'], 10 ), 60 ) );
151 }
152
153 if ( isset( $settings['expire_404'] ) ) {
154 $options['expire_404'] = max( -1, min( intval( $settings['expire_404'], 10 ), 60 ) );
155 }
156
157 if ( isset( $settings['ip_logging'] ) ) {
158 $options['ip_logging'] = max( 0, min( 2, intval( $settings['ip_logging'], 10 ) ) );
159 }
160
161 if ( isset( $settings['redirect_cache'] ) ) {
162 $options['redirect_cache'] = intval( $settings['redirect_cache'], 10 );
163
164 if ( ! in_array( $options['redirect_cache'], array( -1, 0, 1, 24, 24 * 7 ), true ) ) {
165 $options['redirect_cache'] = 1;
166 }
167 }
168
169 if ( isset( $settings['location'] ) && ( ! isset( $options['location'] ) || $options['location'] !== $settings['location'] ) ) {
170 $module = Red_Module::get( 2 );
171 $options['modules'][2] = $module->update( $settings );
172 }
173
174 if ( ! empty( $options['monitor_post'] ) && count( $options['monitor_types'] ) === 0 ) {
175 // If we have a monitor_post set, but no types, then blank everything
176 $options['monitor_post'] = 0;
177 $options['associated_redirect'] = '';
178 }
179
180 $flags = new Red_Source_Flags();
181 $flags_present = [];
182
183 foreach ( array_keys( $flags->get_json() ) as $flag ) {
184 if ( isset( $settings[ $flag ] ) ) {
185 $flags_present[ $flag ] = $settings[ $flag ];
186 }
187 }
188
189 if ( count( $flags_present ) > 0 ) {
190 $flags->set_flags( $flags_present );
191 $options = array_merge( $options, $flags->get_json() );
192 }
193
194 if ( isset( $settings['headers'] ) ) {
195 $headers = new Red_Http_Headers( $settings['headers'] );
196 $options['headers'] = $headers->get_json();
197 }
198
199 if ( isset( $settings['aliases'] ) && is_array( $settings['aliases'] ) ) {
200 $options['aliases'] = array_values( array_filter( array_map( 'red_parse_domain_only', $settings['aliases'] ) ) );
201 $options['aliases'] = array_slice( $options['aliases'], 0, 10 ); // Max 10 aliases
202 }
203
204 if ( isset( $settings['preferred_domain'] ) && in_array( $settings['preferred_domain'], [ '', 'www', 'nowww' ], true ) ) {
205 $options['preferred_domain'] = $settings['preferred_domain'];
206 }
207
208 if ( isset( $settings['relocate'] ) ) {
209 $options['relocate'] = red_parse_domain_path( $settings['relocate'] );
210
211 if ( strlen( $options['relocate'] ) > 0 ) {
212 $options['preferred_domain'] = '';
213 $options['aliases'] = [];
214 $options['https'] = false;
215 }
216 }
217
218 update_option( REDIRECTION_OPTION, apply_filters( 'redirection_save_options', $options ) );
219 return $options;
220 }
221
222 function red_parse_url( $url ) {
223 $domain = filter_var( $url, FILTER_SANITIZE_URL );
224 if ( substr( $domain, 0, 5 ) !== 'http:' && substr( $domain, 0, 6 ) !== 'https:' ) {
225 $domain = ( is_ssl() ? 'https://' : 'http://' ) . $domain;
226 }
227
228 return wp_parse_url( $domain );
229 }
230
231 function red_parse_domain_only( $domain ) {
232 $parsed = red_parse_url( $domain );
233
234 if ( $parsed && isset( $parsed['host'] ) ) {
235 return $parsed['host'];
236 }
237
238 return '';
239 }
240
241 function red_parse_domain_path( $domain ) {
242 $parsed = red_parse_url( $domain );
243
244 if ( $parsed && isset( $parsed['host'] ) ) {
245 return $parsed['scheme'] . '://' . $parsed['host'] . ( isset( $parsed['path'] ) ? $parsed['path'] : '' );
246 }
247
248 return '';
249 }
250
251 function red_is_disabled() {
252 return ( defined( 'REDIRECTION_DISABLE' ) && REDIRECTION_DISABLE ) || file_exists( __DIR__ . '/redirection-disable.txt' );
253 }
254
255 function red_get_options() {
256 $options = get_option( REDIRECTION_OPTION );
257
258 if ( is_array( $options ) && red_is_disabled() ) {
259 $options['https'] = false;
260 }
261
262 if ( $options === false ) {
263 // Default flags for new installs - ignore case and trailing slashes
264 $options = [
265 'flags_case' => true,
266 'flags_trailing' => true,
267 ];
268 }
269
270 $defaults = red_get_default_options();
271
272 foreach ( $defaults as $key => $value ) {
273 if ( ! isset( $options[ $key ] ) ) {
274 $options[ $key ] = $value;
275 }
276 }
277
278 // Back-compat. If monitor_post is set without types then it's from an older Redirection
279 if ( $options['monitor_post'] > 0 && count( $options['monitor_types'] ) === 0 ) {
280 $options['monitor_types'] = [ 'post' ];
281 }
282
283 // Remove old options not in red_get_default_options()
284 foreach ( $options as $key => $value ) {
285 if ( ! isset( $defaults[ $key ] ) ) {
286 unset( $options[ $key ] );
287 }
288 }
289
290 // Back-compat fix
291 if ( $options['rest_api'] === false || ! in_array( $options['rest_api'], [ REDIRECTION_API_JSON, REDIRECTION_API_JSON_INDEX, REDIRECTION_API_JSON_RELATIVE ], true ) ) {
292 $options['rest_api'] = REDIRECTION_API_JSON;
293 }
294
295 return $options;
296 }
297
298 function red_get_rest_api( $type = false ) {
299 if ( $type === false ) {
300 $options = red_get_options();
301 $type = $options['rest_api'];
302 }
303
304 $url = get_rest_url(); // REDIRECTION_API_JSON
305
306 if ( $type === REDIRECTION_API_JSON_INDEX ) {
307 $url = home_url( '/index.php?rest_route=/' );
308 } elseif ( $type === REDIRECTION_API_JSON_RELATIVE ) {
309 $relative = wp_parse_url( $url, PHP_URL_PATH );
310
311 if ( $relative ) {
312 $url = $relative;
313 }
314 }
315
316 return $url;
317 }
318