PluginProbe
Redirection / 5.3.10
Redirection v5.3.10
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.10, at redirection-settings.php

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