PluginProbe
Redirection / 5.2.1
Redirection v5.2.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.2.1, at redirection-settings.php

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