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

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