PluginProbe
Redirection / 5.5.1
Redirection v5.5.1
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 5.5.1, at redirection-settings.php

428 lines 12.6 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 /** @psalm-suppress MissingFile */
11 include_once ABSPATH . '/wp-admin/includes/plugin.php';
12 }
13
14 return get_plugin_data( $plugin );
15 }
16
17 function red_get_post_types( $full = true ) {
18 $types = get_post_types( [ 'public' => true, 'rewrite' => false ], 'objects' );
19 $types[] = (object) array(
20 'name' => 'trash',
21 'label' => __( 'Trash', 'default' ),
22 );
23
24 $post_types = [];
25 foreach ( $types as $type ) {
26 if ( $type->name === 'attachment' ) {
27 continue;
28 }
29
30 if ( $full && strlen( $type->label ) > 0 ) {
31 $post_types[ $type->name ] = $type->label;
32 } else {
33 $post_types[] = $type->name;
34 }
35 }
36
37 return apply_filters( 'redirection_post_types', $post_types );
38 }
39
40 /**
41 * Get default options. Contains all valid options
42 *
43 * @return array
44 */
45 function red_get_default_options() {
46 $flags = new Red_Source_Flags();
47 $defaults = [
48 'support' => false,
49 'token' => md5( uniqid() ),
50 'monitor_post' => 0, // Dont monitor posts by default
51 'monitor_types' => [],
52 'associated_redirect' => '',
53 'auto_target' => '',
54 'expire_redirect' => 7, // Expire in 7 days
55 'expire_404' => 7, // Expire in 7 days
56 'log_external' => false,
57 'log_header' => false,
58 'track_hits' => true,
59 'modules' => [],
60 'newsletter' => false,
61 'redirect_cache' => 1, // 1 hour
62 'ip_logging' => 0, // No IP logging
63 'ip_headers' => [],
64 'ip_proxy' => [],
65 'last_group_id' => 0,
66 'rest_api' => REDIRECTION_API_JSON,
67 'https' => false,
68 'headers' => [],
69 'database' => '',
70 'relocate' => '',
71 'preferred_domain' => '',
72 'aliases' => [],
73 'permalinks' => [],
74 'cache_key' => 0,
75 'plugin_update' => 'prompt',
76 'update_notice' => 0,
77 ];
78 $defaults = array_merge( $defaults, $flags->get_json() );
79
80 return apply_filters( 'red_default_options', $defaults );
81 }
82
83 /**
84 * Set options
85 *
86 * @param array $settings Partial settings.
87 * @return array
88 */
89 function red_set_options( array $settings = [] ) {
90 $options = red_get_options();
91 $monitor_types = [];
92
93 if ( isset( $settings['database'] ) ) {
94 $options['database'] = sanitize_text_field( $settings['database'] );
95 }
96
97 if ( array_key_exists( 'database_stage', $settings ) ) {
98 if ( $settings['database_stage'] === false ) {
99 unset( $options['database_stage'] );
100 } else {
101 $options['database_stage'] = $settings['database_stage'];
102 }
103 }
104
105 if ( isset( $settings['ip_proxy'] ) && is_array( $settings['ip_proxy'] ) ) {
106 $options['ip_proxy'] = array_map( function( $ip ) {
107 $ip = new Redirection_IP( $ip );
108 return $ip->get();
109 }, $settings['ip_proxy'] );
110
111 $options['ip_proxy'] = array_values( array_filter( $options['ip_proxy'] ) );
112 }
113
114 if ( isset( $settings['ip_headers'] ) && is_array( $settings['ip_headers'] ) ) {
115 $available = Redirection_Request::get_ip_headers();
116 $options['ip_headers'] = array_filter( $settings['ip_headers'], function( $header ) use ( $available ) {
117 return in_array( $header, $available, true );
118 } );
119 $options['ip_headers'] = array_values( $options['ip_headers'] );
120 }
121
122 if ( isset( $settings['rest_api'] ) && in_array( intval( $settings['rest_api'], 10 ), array( 0, 1, 2, 3, 4 ), true ) ) {
123 $options['rest_api'] = intval( $settings['rest_api'], 10 );
124 }
125
126 if ( isset( $settings['monitor_types'] ) && is_array( $settings['monitor_types'] ) ) {
127 $allowed = red_get_post_types( false );
128
129 foreach ( $settings['monitor_types'] as $type ) {
130 if ( in_array( $type, $allowed, true ) ) {
131 $monitor_types[] = $type;
132 }
133 }
134
135 $options['monitor_types'] = $monitor_types;
136 }
137
138 if ( isset( $settings['associated_redirect'] ) && is_string( $settings['associated_redirect'] ) ) {
139 $options['associated_redirect'] = '';
140
141 if ( strlen( $settings['associated_redirect'] ) > 0 ) {
142 $sanitizer = new Red_Item_Sanitize();
143 $options['associated_redirect'] = trim( $sanitizer->sanitize_url( $settings['associated_redirect'] ) );
144 }
145 }
146
147 if ( isset( $settings['monitor_types'] ) && count( $monitor_types ) === 0 ) {
148 $options['monitor_post'] = 0;
149 $options['associated_redirect'] = '';
150 } elseif ( isset( $settings['monitor_post'] ) ) {
151 $options['monitor_post'] = max( 0, intval( $settings['monitor_post'], 10 ) );
152
153 if ( ! Red_Group::get( $options['monitor_post'] ) && $options['monitor_post'] !== 0 ) {
154 $groups = Red_Group::get_all();
155
156 if ( count( $groups ) > 0 ) {
157 $options['monitor_post'] = $groups[0]['id'];
158 }
159 }
160 }
161
162 if ( isset( $settings['auto_target'] ) && is_string( $settings['auto_target'] ) ) {
163 $options['auto_target'] = sanitize_text_field( $settings['auto_target'] );
164 }
165
166 if ( isset( $settings['last_group_id'] ) ) {
167 $options['last_group_id'] = max( 0, intval( $settings['last_group_id'], 10 ) );
168
169 if ( ! Red_Group::get( $options['last_group_id'] ) ) {
170 $groups = Red_Group::get_all();
171 $options['last_group_id'] = $groups[0]['id'];
172 }
173 }
174
175 if ( isset( $settings['token'] ) && is_string( $settings['token'] ) ) {
176 $options['token'] = sanitize_text_field( $settings['token'] );
177 }
178
179 if ( isset( $settings['token'] ) && trim( $options['token'] ) === '' ) {
180 $options['token'] = md5( uniqid() );
181 }
182
183 // Boolean settings
184 foreach ( [ 'support', 'https', 'newsletter', 'log_external', 'log_header', 'track_hits' ] as $name ) {
185 if ( isset( $settings[ $name ] ) ) {
186 $options[ $name ] = $settings[ $name ] ? true : false;
187 }
188 }
189
190 if ( isset( $settings['expire_redirect'] ) ) {
191 $options['expire_redirect'] = max( -1, min( intval( $settings['expire_redirect'], 10 ), 60 ) );
192 }
193
194 if ( isset( $settings['expire_404'] ) ) {
195 $options['expire_404'] = max( -1, min( intval( $settings['expire_404'], 10 ), 60 ) );
196 }
197
198 if ( isset( $settings['ip_logging'] ) ) {
199 $options['ip_logging'] = max( 0, min( 2, intval( $settings['ip_logging'], 10 ) ) );
200 }
201
202 if ( isset( $settings['redirect_cache'] ) ) {
203 $options['redirect_cache'] = intval( $settings['redirect_cache'], 10 );
204
205 if ( ! in_array( $options['redirect_cache'], array( -1, 0, 1, 24, 24 * 7 ), true ) ) {
206 $options['redirect_cache'] = 1;
207 }
208 }
209
210 if ( isset( $settings['location'] ) && ( ! isset( $options['location'] ) || $options['location'] !== $settings['location'] ) ) {
211 $module = Red_Module::get( 2 );
212 if ( $module ) {
213 $options['modules'][2] = $module->update( $settings );
214 }
215 }
216
217 if ( ! empty( $options['monitor_post'] ) && count( $options['monitor_types'] ) === 0 ) {
218 // If we have a monitor_post set, but no types, then blank everything
219 $options['monitor_post'] = 0;
220 $options['associated_redirect'] = '';
221 }
222
223 if ( isset( $settings['plugin_update'] ) && in_array( $settings['plugin_update'], [ 'prompt', 'admin' ], true ) ) {
224 $options['plugin_update'] = sanitize_text_field( $settings['plugin_update'] );
225 }
226
227 $flags = new Red_Source_Flags();
228 $flags_present = [];
229
230 foreach ( array_keys( $flags->get_json() ) as $flag ) {
231 if ( isset( $settings[ $flag ] ) ) {
232 $flags_present[ $flag ] = $settings[ $flag ];
233 }
234 }
235
236 if ( count( $flags_present ) > 0 ) {
237 $flags->set_flags( $flags_present );
238 $options = array_merge( $options, $flags->get_json() );
239 }
240
241 if ( isset( $settings['headers'] ) ) {
242 $headers = new Red_Http_Headers( $settings['headers'] );
243 $options['headers'] = $headers->get_json();
244 }
245
246 if ( isset( $settings['aliases'] ) && is_array( $settings['aliases'] ) ) {
247 $options['aliases'] = array_map( 'sanitize_text_field', $settings['aliases'] );
248 $options['aliases'] = array_values( array_filter( array_map( 'red_parse_domain_only', $settings['aliases'] ) ) );
249 $options['aliases'] = array_slice( $options['aliases'], 0, 20 ); // Max 20
250 }
251
252 if ( isset( $settings['permalinks'] ) && is_array( $settings['permalinks'] ) ) {
253 $options['permalinks'] = array_map(
254 function ( $permalink ) {
255 return sanitize_option( 'permalink_structure', $permalink );
256 },
257 $settings['permalinks']
258 );
259 $options['permalinks'] = array_values( array_filter( array_map( 'trim', $options['permalinks'] ) ) );
260 $options['permalinks'] = array_slice( $options['permalinks'], 0, 10 ); // Max 10
261 }
262
263 if ( isset( $settings['preferred_domain'] ) && in_array( $settings['preferred_domain'], [ '', 'www', 'nowww' ], true ) ) {
264 $options['preferred_domain'] = sanitize_text_field( $settings['preferred_domain'] );
265 }
266
267 if ( isset( $settings['relocate'] ) && is_string( $settings['relocate'] ) ) {
268 $options['relocate'] = red_parse_domain_path( sanitize_text_field( $settings['relocate'] ) );
269
270 if ( strlen( $options['relocate'] ) > 0 ) {
271 $options['preferred_domain'] = '';
272 $options['aliases'] = [];
273 $options['https'] = false;
274 }
275 }
276
277 if ( isset( $settings['cache_key'] ) ) {
278 $key = intval( $settings['cache_key'], 10 );
279
280 if ( $settings['cache_key'] === true ) {
281 $key = time();
282 } elseif ( $settings['cache_key'] === false ) {
283 $key = 0;
284 }
285
286 $options['cache_key'] = $key;
287 }
288
289 if ( isset( $settings['update_notice'] ) ) {
290 $major_version = explode( '-', REDIRECTION_VERSION )[0]; // Remove any beta suffix
291 $major_version = implode( '.', array_slice( explode( '.', $major_version ), 0, 2 ) );
292 $options['update_notice'] = $major_version;
293 }
294
295 update_option( REDIRECTION_OPTION, apply_filters( 'redirection_save_options', $options ) );
296 return $options;
297 }
298
299 function red_parse_url( $url ) {
300 $domain = filter_var( $url, FILTER_SANITIZE_URL );
301 if ( substr( $domain, 0, 5 ) !== 'http:' && substr( $domain, 0, 6 ) !== 'https:' ) {
302 $domain = ( is_ssl() ? 'https://' : 'http://' ) . $domain;
303 }
304
305 return wp_parse_url( $domain );
306 }
307
308 function red_parse_domain_only( $domain ) {
309 $parsed = red_parse_url( $domain );
310
311 if ( $parsed && isset( $parsed['host'] ) ) {
312 return $parsed['host'];
313 }
314
315 return '';
316 }
317
318 function red_parse_domain_path( $domain ) {
319 $parsed = red_parse_url( $domain );
320
321 if ( $parsed && isset( $parsed['host'] ) ) {
322 return $parsed['scheme'] . '://' . $parsed['host'] . ( isset( $parsed['path'] ) ? $parsed['path'] : '' );
323 }
324
325 return '';
326 }
327
328 /**
329 * Have redirects been disabled?
330 *
331 * @return boolean
332 */
333 function red_is_disabled() {
334 return ( defined( 'REDIRECTION_DISABLE' ) && REDIRECTION_DISABLE ) || file_exists( __DIR__ . '/redirection-disable.txt' );
335 }
336
337 /**
338 * Get Redirection options
339 *
340 * @return array
341 */
342 function red_get_options() {
343 $options = get_option( REDIRECTION_OPTION );
344 $fresh_install = false;
345
346 if ( $options === false ) {
347 $fresh_install = true;
348 }
349
350 if ( ! is_array( $options ) ) {
351 $options = [];
352 }
353
354 if ( red_is_disabled() ) {
355 $options['https'] = false;
356 }
357
358 $defaults = red_get_default_options();
359
360 foreach ( $defaults as $key => $value ) {
361 if ( ! isset( $options[ $key ] ) ) {
362 $options[ $key ] = $value;
363 }
364 }
365
366 if ( $fresh_install ) {
367 // Default flags for new installs - ignore case and trailing slashes
368 $options['flag_case'] = true;
369 $options['flag_trailing'] = true;
370 }
371
372 // Back-compat. If monitor_post is set without types then it's from an older Redirection
373 if ( $options['monitor_post'] > 0 && count( $options['monitor_types'] ) === 0 ) {
374 $options['monitor_types'] = [ 'post' ];
375 }
376
377 // Remove old options not in red_get_default_options()
378 foreach ( array_keys( $options ) as $key ) {
379 if ( ! isset( $defaults[ $key ] ) && $key !== 'database_stage' ) {
380 unset( $options[ $key ] );
381 }
382 }
383
384 // Back-compat fix
385 if ( $options['rest_api'] === false || ! in_array( $options['rest_api'], [ REDIRECTION_API_JSON, REDIRECTION_API_JSON_INDEX, REDIRECTION_API_JSON_RELATIVE ], true ) ) {
386 $options['rest_api'] = REDIRECTION_API_JSON;
387 }
388
389 if ( isset( $options['modules'] ) && isset( $options['modules']['2'] ) && isset( $options['modules']['2']['location'] ) ) {
390 $options['location'] = $options['modules']['2']['location'];
391 }
392
393 return $options;
394 }
395
396 /**
397 * Get the current REST API
398 *
399 * @param boolean $type Override with a specific API type.
400 * @return string
401 */
402 function red_get_rest_api( $type = false ) {
403 if ( $type === false ) {
404 $options = red_get_options();
405 $type = $options['rest_api'];
406 }
407
408 $url = get_rest_url(); // REDIRECTION_API_JSON
409
410 if ( $type === REDIRECTION_API_JSON_INDEX ) {
411 $url = home_url( '/?rest_route=/' );
412 } elseif ( $type === REDIRECTION_API_JSON_RELATIVE ) {
413 /** @psalm-suppress TooManyArguments, InvalidCast */
414 $relative = (string) wp_parse_url( $url, PHP_URL_PATH );
415
416 if ( $relative ) {
417 $url = $relative;
418 }
419
420 if ( $url === '/index.php' ) {
421 // No permalinks. Default to normal REST API
422 $url = home_url( '/?rest_route=/' );
423 }
424 }
425
426 return $url;
427 }
428