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

423 lines 12.5 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( array( 'public' => true ), '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( 'sanitize_text_field', $settings['permalinks'] );
254 $options['permalinks'] = array_values( array_filter( array_map( 'trim', $options['permalinks'] ) ) );
255 $options['permalinks'] = array_slice( $options['permalinks'], 0, 10 ); // Max 10
256 }
257
258 if ( isset( $settings['preferred_domain'] ) && in_array( $settings['preferred_domain'], [ '', 'www', 'nowww' ], true ) ) {
259 $options['preferred_domain'] = sanitize_text_field( $settings['preferred_domain'] );
260 }
261
262 if ( isset( $settings['relocate'] ) && is_string( $settings['relocate'] ) ) {
263 $options['relocate'] = red_parse_domain_path( sanitize_text_field( $settings['relocate'] ) );
264
265 if ( strlen( $options['relocate'] ) > 0 ) {
266 $options['preferred_domain'] = '';
267 $options['aliases'] = [];
268 $options['https'] = false;
269 }
270 }
271
272 if ( isset( $settings['cache_key'] ) ) {
273 $key = intval( $settings['cache_key'], 10 );
274
275 if ( $settings['cache_key'] === true ) {
276 $key = time();
277 } elseif ( $settings['cache_key'] === false ) {
278 $key = 0;
279 }
280
281 $options['cache_key'] = $key;
282 }
283
284 if ( isset( $settings['update_notice'] ) ) {
285 $major_version = explode( '-', REDIRECTION_VERSION )[0]; // Remove any beta suffix
286 $major_version = implode( '.', array_slice( explode( '.', $major_version ), 0, 2 ) );
287 $options['update_notice'] = $major_version;
288 }
289
290 update_option( REDIRECTION_OPTION, apply_filters( 'redirection_save_options', $options ) );
291 return $options;
292 }
293
294 function red_parse_url( $url ) {
295 $domain = filter_var( $url, FILTER_SANITIZE_URL );
296 if ( substr( $domain, 0, 5 ) !== 'http:' && substr( $domain, 0, 6 ) !== 'https:' ) {
297 $domain = ( is_ssl() ? 'https://' : 'http://' ) . $domain;
298 }
299
300 return wp_parse_url( $domain );
301 }
302
303 function red_parse_domain_only( $domain ) {
304 $parsed = red_parse_url( $domain );
305
306 if ( $parsed && isset( $parsed['host'] ) ) {
307 return $parsed['host'];
308 }
309
310 return '';
311 }
312
313 function red_parse_domain_path( $domain ) {
314 $parsed = red_parse_url( $domain );
315
316 if ( $parsed && isset( $parsed['host'] ) ) {
317 return $parsed['scheme'] . '://' . $parsed['host'] . ( isset( $parsed['path'] ) ? $parsed['path'] : '' );
318 }
319
320 return '';
321 }
322
323 /**
324 * Have redirects been disabled?
325 *
326 * @return boolean
327 */
328 function red_is_disabled() {
329 return ( defined( 'REDIRECTION_DISABLE' ) && REDIRECTION_DISABLE ) || file_exists( __DIR__ . '/redirection-disable.txt' );
330 }
331
332 /**
333 * Get Redirection options
334 *
335 * @return array
336 */
337 function red_get_options() {
338 $options = get_option( REDIRECTION_OPTION );
339 $fresh_install = false;
340
341 if ( $options === false ) {
342 $fresh_install = true;
343 }
344
345 if ( ! is_array( $options ) ) {
346 $options = [];
347 }
348
349 if ( red_is_disabled() ) {
350 $options['https'] = false;
351 }
352
353 $defaults = red_get_default_options();
354
355 foreach ( $defaults as $key => $value ) {
356 if ( ! isset( $options[ $key ] ) ) {
357 $options[ $key ] = $value;
358 }
359 }
360
361 if ( $fresh_install ) {
362 // Default flags for new installs - ignore case and trailing slashes
363 $options['flag_case'] = true;
364 $options['flag_trailing'] = true;
365 }
366
367 // Back-compat. If monitor_post is set without types then it's from an older Redirection
368 if ( $options['monitor_post'] > 0 && count( $options['monitor_types'] ) === 0 ) {
369 $options['monitor_types'] = [ 'post' ];
370 }
371
372 // Remove old options not in red_get_default_options()
373 foreach ( array_keys( $options ) as $key ) {
374 if ( ! isset( $defaults[ $key ] ) && $key !== 'database_stage' ) {
375 unset( $options[ $key ] );
376 }
377 }
378
379 // Back-compat fix
380 if ( $options['rest_api'] === false || ! in_array( $options['rest_api'], [ REDIRECTION_API_JSON, REDIRECTION_API_JSON_INDEX, REDIRECTION_API_JSON_RELATIVE ], true ) ) {
381 $options['rest_api'] = REDIRECTION_API_JSON;
382 }
383
384 if ( isset( $options['modules'] ) && isset( $options['modules']['2'] ) && isset( $options['modules']['2']['location'] ) ) {
385 $options['location'] = $options['modules']['2']['location'];
386 }
387
388 return $options;
389 }
390
391 /**
392 * Get the current REST API
393 *
394 * @param boolean $type Override with a specific API type.
395 * @return string
396 */
397 function red_get_rest_api( $type = false ) {
398 if ( $type === false ) {
399 $options = red_get_options();
400 $type = $options['rest_api'];
401 }
402
403 $url = get_rest_url(); // REDIRECTION_API_JSON
404
405 if ( $type === REDIRECTION_API_JSON_INDEX ) {
406 $url = home_url( '/?rest_route=/' );
407 } elseif ( $type === REDIRECTION_API_JSON_RELATIVE ) {
408 /** @psalm-suppress TooManyArguments, InvalidCast */
409 $relative = (string) wp_parse_url( $url, PHP_URL_PATH );
410
411 if ( $relative ) {
412 $url = $relative;
413 }
414
415 if ( $url === '/index.php' ) {
416 // No permalinks. Default to normal REST API
417 $url = home_url( '/?rest_route=/' );
418 }
419 }
420
421 return $url;
422 }
423