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

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