PluginProbe
Redirection / 3.7.2
Redirection v3.7.2
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 3.7.2, at redirection-settings.php

225 lines 6.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_ADMIN', 2 );
7 define( 'REDIRECTION_API_JSON_RELATIVE', 3 );
8 define( 'REDIRECTION_API_POST', 4 );
9
10 function red_get_plugin_data( $plugin ) {
11 if ( ! function_exists( 'get_plugin_data' ) ) {
12 include_once ABSPATH . '/wp-admin/includes/plugin.php';
13 }
14
15 return get_plugin_data( $plugin );
16 }
17
18 function red_get_post_types( $full = true ) {
19 $types = get_post_types( array( 'public' => true ), 'objects' );
20 $types[] = (object) array(
21 'name' => 'trash',
22 'label' => __( 'Trash' ),
23 );
24
25 $post_types = array();
26 foreach ( $types as $type ) {
27 if ( $type->name === 'attachment' ) {
28 continue;
29 }
30
31 if ( $full ) {
32 $post_types[ $type->name ] = $type->label;
33 } else {
34 $post_types[] = $type->name;
35 }
36 }
37
38 return apply_filters( 'redirection_post_types', $post_types );
39 }
40
41 function red_get_default_options() {
42 return apply_filters( 'red_default_options', array(
43 'support' => false,
44 'token' => md5( uniqid() ),
45 'monitor_post' => 0, // Dont monitor posts by default
46 'monitor_types' => array(),
47 'associated_redirect' => '',
48 'auto_target' => '',
49 'expire_redirect' => 7, // Expire in 7 days
50 'expire_404' => 7, // Expire in 7 days
51 'modules' => array(),
52 'newsletter' => false,
53 'redirect_cache' => 1, // 1 hour
54 'ip_logging' => 1, // Full IP logging
55 'last_group_id' => 0,
56 'rest_api' => false,
57 'https' => false,
58 'database' => '',
59 ) );
60 }
61
62 function red_set_options( array $settings = array() ) {
63 $options = red_get_options();
64 $monitor_types = array();
65
66 if ( isset( $settings['database'] ) ) {
67 $options['database'] = $settings['database'];
68 }
69
70 if ( isset( $settings['rest_api'] ) && in_array( intval( $settings['rest_api'], 10 ), array( 0, 1, 2, 3, 4 ) ) ) {
71 $options['rest_api'] = intval( $settings['rest_api'], 10 );
72 }
73
74 if ( isset( $settings['monitor_types'] ) && is_array( $settings['monitor_types'] ) ) {
75 $allowed = red_get_post_types( false );
76
77 foreach ( $settings['monitor_types'] as $type ) {
78 if ( in_array( $type, $allowed ) ) {
79 $monitor_types[] = $type;
80 }
81 }
82
83 $options['monitor_types'] = $monitor_types;
84 }
85
86 if ( isset( $settings['associated_redirect'] ) ) {
87 $options['associated_redirect'] = '';
88
89 if ( strlen( $settings['associated_redirect'] ) > 0 ) {
90 $sanitizer = new Red_Item_Sanitize();
91 $options['associated_redirect'] = trim( $sanitizer->sanitize_url( $settings['associated_redirect'] ) );
92 }
93 }
94
95 if ( isset( $settings['monitor_types'] ) && count( $monitor_types ) === 0 ) {
96 $options['monitor_post'] = 0;
97 $options['associated_redirect'] = '';
98 } elseif ( isset( $settings['monitor_post'] ) ) {
99 $options['monitor_post'] = max( 0, intval( $settings['monitor_post'], 10 ) );
100
101 if ( ! Red_Group::get( $options['monitor_post'] ) && $options['monitor_post'] !== 0 ) {
102 $groups = Red_Group::get_all();
103 $options['monitor_post'] = $groups[0]['id'];
104 }
105 }
106
107 if ( isset( $settings['auto_target'] ) ) {
108 $options['auto_target'] = $settings['auto_target'];
109 }
110
111 if ( isset( $settings['last_group_id'] ) ) {
112 $options['last_group_id'] = max( 0, intval( $settings['last_group_id'], 10 ) );
113
114 if ( ! Red_Group::get( $options['last_group_id'] ) ) {
115 $groups = Red_Group::get_all();
116 $options['last_group_id'] = $groups[0]['id'];
117 }
118 }
119
120 if ( isset( $settings['support'] ) ) {
121 $options['support'] = $settings['support'] ? true : false;
122 }
123
124 if ( isset( $settings['token'] ) ) {
125 $options['token'] = $settings['token'];
126 }
127
128 if ( isset( $settings['https'] ) ) {
129 $options['https'] = $settings['https'] ? true : false;
130 }
131
132 if ( isset( $settings['token'] ) && trim( $options['token'] ) === '' ) {
133 $options['token'] = md5( uniqid() );
134 }
135
136 if ( isset( $settings['newsletter'] ) ) {
137 $options['newsletter'] = $settings['newsletter'] ? true : false;
138 }
139
140 if ( isset( $settings['expire_redirect'] ) ) {
141 $options['expire_redirect'] = max( -1, min( intval( $settings['expire_redirect'], 10 ), 60 ) );
142 }
143
144 if ( isset( $settings['expire_404'] ) ) {
145 $options['expire_404'] = max( -1, min( intval( $settings['expire_404'], 10 ), 60 ) );
146 }
147
148 if ( isset( $settings['ip_logging'] ) ) {
149 $options['ip_logging'] = max( 0, min( 2, intval( $settings['ip_logging'], 10 ) ) );
150 }
151
152 if ( isset( $settings['redirect_cache'] ) ) {
153 $options['redirect_cache'] = intval( $settings['redirect_cache'], 10 );
154
155 if ( ! in_array( $options['redirect_cache'], array( -1, 0, 1, 24, 24 * 7 ), true ) ) {
156 $options['redirect_cache'] = 1;
157 }
158 }
159
160 if ( isset( $settings['location'] ) ) {
161 $module = Red_Module::get( 2 );
162 $options['modules'][2] = $module->update( $settings );
163 }
164
165 if ( ! empty( $options['monitor_post'] ) && count( $options['monitor_types'] ) === 0 ) {
166 // If we have a monitor_post set, but no types, then blank everything
167 $options['monitor_post'] = 0;
168 $options['associated_redirect'] = '';
169 }
170
171 update_option( REDIRECTION_OPTION, apply_filters( 'redirection_save_options', $options ) );
172 return $options;
173 }
174
175 function red_get_options() {
176 $options = get_option( REDIRECTION_OPTION );
177 if ( $options === false ) {
178 // New users don't see the new version information
179 $options = [];
180 }
181
182 $defaults = red_get_default_options();
183
184 foreach ( $defaults as $key => $value ) {
185 if ( ! isset( $options[ $key ] ) ) {
186 $options[ $key ] = $value;
187 }
188 }
189
190 // Back-compat. If monitor_post is set without types then it's from an older Redirection
191 if ( $options['monitor_post'] > 0 && count( $options['monitor_types'] ) === 0 ) {
192 $options['monitor_types'] = array( 'post' );
193 }
194
195 // Remove old options not in red_get_default_options()
196 foreach ( $options as $key => $value ) {
197 if ( ! isset( $defaults[ $key ] ) ) {
198 unset( $options[ $key ] );
199 }
200 }
201
202 return $options;
203 }
204
205 function red_get_rest_api( $type = false ) {
206 if ( $type === false ) {
207 $options = red_get_options();
208 $type = $options['rest_api'];
209 }
210
211 $url = get_rest_url(); // REDIRECTION_API_JSON
212
213 if ( $type === REDIRECTION_API_JSON_INDEX ) {
214 $url = home_url( '/index.php?rest_route=/' );
215 } elseif ( $type === REDIRECTION_API_JSON_RELATIVE ) {
216 $relative = wp_parse_url( $url, PHP_URL_PATH );
217
218 if ( $relative ) {
219 $url = $relative;
220 }
221 }
222
223 return $url;
224 }
225