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

247 lines 7.0 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' ),
21 );
22
23 $post_types = array();
24 foreach ( $types as $type ) {
25 if ( $type->name === 'attachment' ) {
26 continue;
27 }
28
29 if ( $full ) {
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' => array(),
46 'associated_redirect' => '',
47 'auto_target' => '',
48 'expire_redirect' => 7, // Expire in 7 days
49 'expire_404' => 7, // Expire in 7 days
50 'modules' => array(),
51 'newsletter' => false,
52 'redirect_cache' => 1, // 1 hour
53 'ip_logging' => 1, // Full IP logging
54 'last_group_id' => 0,
55 'rest_api' => REDIRECTION_API_JSON,
56 'https' => false,
57 'database' => '',
58 ];
59 $defaults = array_merge( $defaults, $flags->get_json() );
60
61 return apply_filters( 'red_default_options', $defaults );
62 }
63
64 function red_set_options( array $settings = array() ) {
65 $options = red_get_options();
66 $monitor_types = array();
67
68 if ( isset( $settings['database'] ) ) {
69 $options['database'] = $settings['database'];
70 }
71
72 if ( isset( $settings['rest_api'] ) && in_array( intval( $settings['rest_api'], 10 ), array( 0, 1, 2, 3, 4 ) ) ) {
73 $options['rest_api'] = intval( $settings['rest_api'], 10 );
74 }
75
76 if ( isset( $settings['monitor_types'] ) && is_array( $settings['monitor_types'] ) ) {
77 $allowed = red_get_post_types( false );
78
79 foreach ( $settings['monitor_types'] as $type ) {
80 if ( in_array( $type, $allowed ) ) {
81 $monitor_types[] = $type;
82 }
83 }
84
85 $options['monitor_types'] = $monitor_types;
86 }
87
88 if ( isset( $settings['associated_redirect'] ) ) {
89 $options['associated_redirect'] = '';
90
91 if ( strlen( $settings['associated_redirect'] ) > 0 ) {
92 $sanitizer = new Red_Item_Sanitize();
93 $options['associated_redirect'] = trim( $sanitizer->sanitize_url( $settings['associated_redirect'] ) );
94 }
95 }
96
97 if ( isset( $settings['monitor_types'] ) && count( $monitor_types ) === 0 ) {
98 $options['monitor_post'] = 0;
99 $options['associated_redirect'] = '';
100 } elseif ( isset( $settings['monitor_post'] ) ) {
101 $options['monitor_post'] = max( 0, intval( $settings['monitor_post'], 10 ) );
102
103 if ( ! Red_Group::get( $options['monitor_post'] ) && $options['monitor_post'] !== 0 ) {
104 $groups = Red_Group::get_all();
105 $options['monitor_post'] = $groups[0]['id'];
106 }
107 }
108
109 if ( isset( $settings['auto_target'] ) ) {
110 $options['auto_target'] = $settings['auto_target'];
111 }
112
113 if ( isset( $settings['last_group_id'] ) ) {
114 $options['last_group_id'] = max( 0, intval( $settings['last_group_id'], 10 ) );
115
116 if ( ! Red_Group::get( $options['last_group_id'] ) ) {
117 $groups = Red_Group::get_all();
118 $options['last_group_id'] = $groups[0]['id'];
119 }
120 }
121
122 if ( isset( $settings['support'] ) ) {
123 $options['support'] = $settings['support'] ? true : false;
124 }
125
126 if ( isset( $settings['token'] ) ) {
127 $options['token'] = $settings['token'];
128 }
129
130 if ( isset( $settings['https'] ) ) {
131 $options['https'] = $settings['https'] ? true : false;
132 }
133
134 if ( isset( $settings['token'] ) && trim( $options['token'] ) === '' ) {
135 $options['token'] = md5( uniqid() );
136 }
137
138 if ( isset( $settings['newsletter'] ) ) {
139 $options['newsletter'] = $settings['newsletter'] ? true : false;
140 }
141
142 if ( isset( $settings['expire_redirect'] ) ) {
143 $options['expire_redirect'] = max( -1, min( intval( $settings['expire_redirect'], 10 ), 60 ) );
144 }
145
146 if ( isset( $settings['expire_404'] ) ) {
147 $options['expire_404'] = max( -1, min( intval( $settings['expire_404'], 10 ), 60 ) );
148 }
149
150 if ( isset( $settings['ip_logging'] ) ) {
151 $options['ip_logging'] = max( 0, min( 2, intval( $settings['ip_logging'], 10 ) ) );
152 }
153
154 if ( isset( $settings['redirect_cache'] ) ) {
155 $options['redirect_cache'] = intval( $settings['redirect_cache'], 10 );
156
157 if ( ! in_array( $options['redirect_cache'], array( -1, 0, 1, 24, 24 * 7 ), true ) ) {
158 $options['redirect_cache'] = 1;
159 }
160 }
161
162 if ( isset( $settings['location'] ) ) {
163 $module = Red_Module::get( 2 );
164 $options['modules'][2] = $module->update( $settings );
165 }
166
167 if ( ! empty( $options['monitor_post'] ) && count( $options['monitor_types'] ) === 0 ) {
168 // If we have a monitor_post set, but no types, then blank everything
169 $options['monitor_post'] = 0;
170 $options['associated_redirect'] = '';
171 }
172
173 $flags = new Red_Source_Flags();
174 $flags_present = [];
175
176 foreach ( array_keys( $flags->get_json() ) as $flag ) {
177 if ( isset( $settings[ $flag ] ) ) {
178 $flags_present[ $flag ] = $settings[ $flag ];
179 }
180 }
181
182 if ( count( $flags_present ) > 0 ) {
183 $flags->set_flags( $flags_present );
184 $options = array_merge( $options, $flags->get_json() );
185 }
186
187 update_option( REDIRECTION_OPTION, apply_filters( 'redirection_save_options', $options ) );
188 return $options;
189 }
190
191 function red_get_options() {
192 $options = get_option( REDIRECTION_OPTION );
193 if ( $options === false ) {
194 // Default flags for new installs - ignore case and trailing slashes
195 $options['flags_case'] = true;
196 $options['flags_trailing'] = true;
197 }
198
199 $defaults = red_get_default_options();
200
201 foreach ( $defaults as $key => $value ) {
202 if ( ! isset( $options[ $key ] ) ) {
203 $options[ $key ] = $value;
204 }
205 }
206
207 // Back-compat. If monitor_post is set without types then it's from an older Redirection
208 if ( $options['monitor_post'] > 0 && count( $options['monitor_types'] ) === 0 ) {
209 $options['monitor_types'] = [ 'post' ];
210 }
211
212 // Remove old options not in red_get_default_options()
213 foreach ( $options as $key => $value ) {
214 if ( ! isset( $defaults[ $key ] ) ) {
215 unset( $options[ $key ] );
216 }
217 }
218
219 // Back-compat fix
220 if ( $options['rest_api'] === false || ! in_array( $options['rest_api'], [ REDIRECTION_API_JSON, REDIRECTION_API_JSON_INDEX, REDIRECTION_API_JSON_RELATIVE ], true ) ) {
221 $options['rest_api'] = REDIRECTION_API_JSON;
222 }
223
224 return $options;
225 }
226
227 function red_get_rest_api( $type = false ) {
228 if ( $type === false ) {
229 $options = red_get_options();
230 $type = $options['rest_api'];
231 }
232
233 $url = get_rest_url(); // REDIRECTION_API_JSON
234
235 if ( $type === REDIRECTION_API_JSON_INDEX ) {
236 $url = home_url( '/index.php?rest_route=/' );
237 } elseif ( $type === REDIRECTION_API_JSON_RELATIVE ) {
238 $relative = wp_parse_url( $url, PHP_URL_PATH );
239
240 if ( $relative ) {
241 $url = $relative;
242 }
243 }
244
245 return $url;
246 }
247