PluginProbe
Redirection / 4.9.1
Redirection v4.9.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 4.9.1, at redirection-settings.php

326 lines 9.2 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 function red_get_default_options() {
40 $flags = new Red_Source_Flags();
41 $defaults = [
42 'support' => false,
43 'token' => md5( uniqid() ),
44 'monitor_post' => 0, // Dont monitor posts by default
45 'monitor_types' => [],
46 'associated_redirect' => '',
47 'auto_target' => '',
48 'expire_redirect' => 7, // Expire in 7 days
49 'expire_404' => 7, // Expire in 7 days
50 'log_external' => false,
51 'log_header' => false,
52 'track_hits' => true,
53 'modules' => [],
54 'newsletter' => false,
55 'redirect_cache' => 1, // 1 hour
56 'ip_logging' => 1, // Full IP logging
57 'last_group_id' => 0,
58 'rest_api' => REDIRECTION_API_JSON,
59 'https' => false,
60 'headers' => [],
61 'database' => '',
62 'relocate' => '',
63 'preferred_domain' => '',
64 'aliases' => [],
65 ];
66 $defaults = array_merge( $defaults, $flags->get_json() );
67
68 return apply_filters( 'red_default_options', $defaults );
69 }
70
71 function red_set_options( array $settings = array() ) {
72 $options = red_get_options();
73 $monitor_types = array();
74
75 if ( isset( $settings['database'] ) ) {
76 $options['database'] = $settings['database'];
77 }
78
79 if ( isset( $settings['rest_api'] ) && in_array( intval( $settings['rest_api'], 10 ), array( 0, 1, 2, 3, 4 ), true ) ) {
80 $options['rest_api'] = intval( $settings['rest_api'], 10 );
81 }
82
83 if ( isset( $settings['monitor_types'] ) && is_array( $settings['monitor_types'] ) ) {
84 $allowed = red_get_post_types( false );
85
86 foreach ( $settings['monitor_types'] as $type ) {
87 if ( in_array( $type, $allowed, true ) ) {
88 $monitor_types[] = $type;
89 }
90 }
91
92 $options['monitor_types'] = $monitor_types;
93 }
94
95 if ( isset( $settings['associated_redirect'] ) ) {
96 $options['associated_redirect'] = '';
97
98 if ( strlen( $settings['associated_redirect'] ) > 0 ) {
99 $sanitizer = new Red_Item_Sanitize();
100 $options['associated_redirect'] = trim( $sanitizer->sanitize_url( $settings['associated_redirect'] ) );
101 }
102 }
103
104 if ( isset( $settings['monitor_types'] ) && count( $monitor_types ) === 0 ) {
105 $options['monitor_post'] = 0;
106 $options['associated_redirect'] = '';
107 } elseif ( isset( $settings['monitor_post'] ) ) {
108 $options['monitor_post'] = max( 0, intval( $settings['monitor_post'], 10 ) );
109
110 if ( ! Red_Group::get( $options['monitor_post'] ) && $options['monitor_post'] !== 0 ) {
111 $groups = Red_Group::get_all();
112
113 if ( count( $groups ) > 0 ) {
114 $options['monitor_post'] = $groups[0]['id'];
115 }
116 }
117 }
118
119 if ( isset( $settings['auto_target'] ) ) {
120 $options['auto_target'] = $settings['auto_target'];
121 }
122
123 if ( isset( $settings['last_group_id'] ) ) {
124 $options['last_group_id'] = max( 0, intval( $settings['last_group_id'], 10 ) );
125
126 if ( ! Red_Group::get( $options['last_group_id'] ) ) {
127 $groups = Red_Group::get_all();
128 $options['last_group_id'] = $groups[0]['id'];
129 }
130 }
131
132 if ( isset( $settings['token'] ) ) {
133 $options['token'] = $settings['token'];
134 }
135
136 if ( isset( $settings['token'] ) && trim( $options['token'] ) === '' ) {
137 $options['token'] = md5( uniqid() );
138 }
139
140 // Boolean settings
141 foreach ( [ 'support', 'https', 'newsletter', 'log_external', 'log_header', 'track_hits' ] as $name ) {
142 if ( isset( $settings[ $name ] ) ) {
143 $options[ $name ] = $settings[ $name ] ? true : false;
144 }
145 }
146
147 if ( isset( $settings['expire_redirect'] ) ) {
148 $options['expire_redirect'] = max( -1, min( intval( $settings['expire_redirect'], 10 ), 60 ) );
149 }
150
151 if ( isset( $settings['expire_404'] ) ) {
152 $options['expire_404'] = max( -1, min( intval( $settings['expire_404'], 10 ), 60 ) );
153 }
154
155 if ( isset( $settings['ip_logging'] ) ) {
156 $options['ip_logging'] = max( 0, min( 2, intval( $settings['ip_logging'], 10 ) ) );
157 }
158
159 if ( isset( $settings['redirect_cache'] ) ) {
160 $options['redirect_cache'] = intval( $settings['redirect_cache'], 10 );
161
162 if ( ! in_array( $options['redirect_cache'], array( -1, 0, 1, 24, 24 * 7 ), true ) ) {
163 $options['redirect_cache'] = 1;
164 }
165 }
166
167 if ( isset( $settings['location'] ) && ( ! isset( $options['location'] ) || $options['location'] !== $settings['location'] ) ) {
168 $module = Red_Module::get( 2 );
169 $options['modules'][2] = $module->update( $settings );
170 }
171
172 if ( ! empty( $options['monitor_post'] ) && count( $options['monitor_types'] ) === 0 ) {
173 // If we have a monitor_post set, but no types, then blank everything
174 $options['monitor_post'] = 0;
175 $options['associated_redirect'] = '';
176 }
177
178 $flags = new Red_Source_Flags();
179 $flags_present = [];
180
181 foreach ( array_keys( $flags->get_json() ) as $flag ) {
182 if ( isset( $settings[ $flag ] ) ) {
183 $flags_present[ $flag ] = $settings[ $flag ];
184 }
185 }
186
187 if ( count( $flags_present ) > 0 ) {
188 $flags->set_flags( $flags_present );
189 $options = array_merge( $options, $flags->get_json() );
190 }
191
192 if ( isset( $settings['headers'] ) ) {
193 $headers = new Red_Http_Headers( $settings['headers'] );
194 $options['headers'] = $headers->get_json();
195 }
196
197 if ( isset( $settings['aliases'] ) && is_array( $settings['aliases'] ) ) {
198 $options['aliases'] = array_values( array_filter( array_map( 'red_parse_domain_only', $settings['aliases'] ) ) );
199 $options['aliases'] = array_slice( $options['aliases'], 0, 10 ); // Max 10 aliases
200 }
201
202 if ( isset( $settings['preferred_domain'] ) && in_array( $settings['preferred_domain'], [ '', 'www', 'nowww' ], true ) ) {
203 $options['preferred_domain'] = $settings['preferred_domain'];
204 }
205
206 if ( isset( $settings['relocate'] ) ) {
207 $options['relocate'] = red_parse_domain_path( $settings['relocate'] );
208
209 if ( strlen( $options['relocate'] ) > 0 ) {
210 $options['preferred_domain'] = '';
211 $options['aliases'] = [];
212 $options['https'] = false;
213 }
214 }
215
216 update_option( REDIRECTION_OPTION, apply_filters( 'redirection_save_options', $options ) );
217 return $options;
218 }
219
220 function red_parse_url( $url ) {
221 $domain = filter_var( $url, FILTER_SANITIZE_URL );
222 if ( substr( $domain, 0, 5 ) !== 'http:' && substr( $domain, 0, 6 ) !== 'https:' ) {
223 $domain = ( is_ssl() ? 'https://' : 'http://' ) . $domain;
224 }
225
226 return wp_parse_url( $domain );
227 }
228
229 function red_parse_domain_only( $domain ) {
230 $parsed = red_parse_url( $domain );
231
232 if ( $parsed && isset( $parsed['host'] ) ) {
233 return $parsed['host'];
234 }
235
236 return '';
237 }
238
239 function red_parse_domain_path( $domain ) {
240 $parsed = red_parse_url( $domain );
241
242 if ( $parsed && isset( $parsed['host'] ) ) {
243 return $parsed['scheme'] . '://' . $parsed['host'] . ( isset( $parsed['path'] ) ? $parsed['path'] : '' );
244 }
245
246 return '';
247 }
248
249 /**
250 * Have redirects been disabled?
251 *
252 * @return boolean
253 */
254 function red_is_disabled() {
255 return ( defined( 'REDIRECTION_DISABLE' ) && REDIRECTION_DISABLE ) || file_exists( __DIR__ . '/redirection-disable.txt' );
256 }
257
258 /**
259 * Get Redirection options
260 *
261 * @return array
262 */
263 function red_get_options() {
264 $options = get_option( REDIRECTION_OPTION );
265
266 if ( is_array( $options ) && red_is_disabled() ) {
267 $options['https'] = false;
268 }
269
270 if ( $options === false ) {
271 // Default flags for new installs - ignore case and trailing slashes
272 $options = [
273 'flags_case' => true,
274 'flags_trailing' => true,
275 ];
276 }
277
278 $defaults = red_get_default_options();
279
280 foreach ( $defaults as $key => $value ) {
281 if ( ! isset( $options[ $key ] ) ) {
282 $options[ $key ] = $value;
283 }
284 }
285
286 // Back-compat. If monitor_post is set without types then it's from an older Redirection
287 if ( $options['monitor_post'] > 0 && count( $options['monitor_types'] ) === 0 ) {
288 $options['monitor_types'] = [ 'post' ];
289 }
290
291 // Remove old options not in red_get_default_options()
292 foreach ( $options as $key => $value ) {
293 if ( ! isset( $defaults[ $key ] ) ) {
294 unset( $options[ $key ] );
295 }
296 }
297
298 // Back-compat fix
299 if ( $options['rest_api'] === false || ! in_array( $options['rest_api'], [ REDIRECTION_API_JSON, REDIRECTION_API_JSON_INDEX, REDIRECTION_API_JSON_RELATIVE ], true ) ) {
300 $options['rest_api'] = REDIRECTION_API_JSON;
301 }
302
303 return $options;
304 }
305
306 function red_get_rest_api( $type = false ) {
307 if ( $type === false ) {
308 $options = red_get_options();
309 $type = $options['rest_api'];
310 }
311
312 $url = get_rest_url(); // REDIRECTION_API_JSON
313
314 if ( $type === REDIRECTION_API_JSON_INDEX ) {
315 $url = home_url( '/?rest_route=/' );
316 } elseif ( $type === REDIRECTION_API_JSON_RELATIVE ) {
317 $relative = wp_parse_url( $url, PHP_URL_PATH );
318
319 if ( $relative ) {
320 $url = $relative;
321 }
322 }
323
324 return $url;
325 }
326