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

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