| @@ -1,91 +1,26 @@ | ||
| 1 | 1 | <?php |
| 2 | 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 ); | |
| 3 | +const REDIRECTION_OPTION = 'redirection_options'; | |
| 9 | 4 | |
| 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 | - $flags = new Red_Source_Flags(); | |
| 43 | - $defaults = [ | |
| 44 | - 'support' => false, | |
| 45 | - 'token' => md5( uniqid() ), | |
| 46 | - 'monitor_post' => 0, // Dont monitor posts by default | |
| 47 | - 'monitor_types' => array(), | |
| 48 | - 'associated_redirect' => '', | |
| 49 | - 'auto_target' => '', | |
| 50 | - 'expire_redirect' => 7, // Expire in 7 days | |
| 51 | - 'expire_404' => 7, // Expire in 7 days | |
| 52 | - 'modules' => array(), | |
| 53 | - 'newsletter' => false, | |
| 54 | - 'redirect_cache' => 1, // 1 hour | |
| 55 | - 'ip_logging' => 1, // Full IP logging | |
| 56 | - 'last_group_id' => 0, | |
| 57 | - 'rest_api' => false, | |
| 58 | - 'https' => false, | |
| 59 | - 'database' => '', | |
| 60 | - ]; | |
| 61 | - $defaults = array_merge( $defaults, $flags->get_json() ); | |
| 62 | - | |
| 63 | - return apply_filters( 'red_default_options', $defaults ); | |
| 64 | -} | |
| 65 | - | |
| 66 | 5 | function red_set_options( array $settings = array() ) { |
| 67 | 6 | $options = red_get_options(); |
| 68 | 7 | $monitor_types = array(); |
| 8 | + $have_monitor = false; | |
| 69 | 9 | |
| 70 | - if ( isset( $settings['database'] ) ) { | |
| 71 | - $options['database'] = $settings['database']; | |
| 10 | + if ( isset( $settings['monitor_type_post'] ) ) { | |
| 11 | + $monitor_types[] = $settings['monitor_type_post'] ? 'post' : false; | |
| 12 | + $have_monitor = true; | |
| 72 | 13 | } |
| 73 | 14 | |
| 74 | - if ( isset( $settings['rest_api'] ) && in_array( intval( $settings['rest_api'], 10 ), array( 0, 1, 2, 3, 4 ) ) ) { | |
| 75 | - $options['rest_api'] = intval( $settings['rest_api'], 10 ); | |
| 15 | + if ( isset( $settings['monitor_type_page'] ) ) { | |
| 16 | + $monitor_types[] = $settings['monitor_type_page'] ? 'page' : false; | |
| 17 | + $have_monitor = true; | |
| 76 | 18 | } |
| 77 | 19 | |
| 78 | - if ( isset( $settings['monitor_types'] ) && is_array( $settings['monitor_types'] ) ) { | |
| 79 | - $allowed = red_get_post_types( false ); | |
| 80 | - | |
| 81 | - foreach ( $settings['monitor_types'] as $type ) { | |
| 82 | - if ( in_array( $type, $allowed ) ) { | |
| 83 | - $monitor_types[] = $type; | |
| 84 | - } | |
| 85 | - } | |
| 86 | - | |
| 87 | - $options['monitor_types'] = $monitor_types; | |
| 20 | + if ( isset( $settings['monitor_type_trash'] ) ) { | |
| 21 | + $monitor_types[] = $settings['monitor_type_trash'] ? 'trash' : false; | |
| 22 | + $have_monitor = true; | |
| 88 | 23 | } |
| 89 | 24 | |
| 90 | 25 | if ( isset( $settings['associated_redirect'] ) ) { |
| 91 | 26 | $options['associated_redirect'] = ''; |
| @@ -95,46 +30,35 @@ | ||
| 95 | 30 | $options['associated_redirect'] = trim( $sanitizer->sanitize_url( $settings['associated_redirect'] ) ); |
| 96 | 31 | } |
| 97 | 32 | } |
| 98 | 33 | |
| 99 | - if ( isset( $settings['monitor_types'] ) && count( $monitor_types ) === 0 ) { | |
| 34 | + $monitor_types = array_values( array_filter( $monitor_types ) ); | |
| 35 | + | |
| 36 | + if ( count( $monitor_types ) === 0 ) { | |
| 100 | 37 | $options['monitor_post'] = 0; |
| 101 | 38 | $options['associated_redirect'] = ''; |
| 102 | 39 | } elseif ( isset( $settings['monitor_post'] ) ) { |
| 103 | 40 | $options['monitor_post'] = max( 0, intval( $settings['monitor_post'], 10 ) ); |
| 104 | 41 | |
| 105 | - if ( ! Red_Group::get( $options['monitor_post'] ) && $options['monitor_post'] !== 0 ) { | |
| 42 | + if ( ! Red_Group::get( $options['monitor_post'] ) ) { | |
| 106 | 43 | $groups = Red_Group::get_all(); |
| 107 | - $options['monitor_post'] = $groups[0]['id']; | |
| 44 | + $options['monitor_post'] = $groups[ 0 ]['id']; | |
| 108 | 45 | } |
| 109 | 46 | } |
| 110 | 47 | |
| 111 | 48 | if ( isset( $settings['auto_target'] ) ) { |
| 112 | - $options['auto_target'] = $settings['auto_target']; | |
| 49 | + $options['auto_target'] = stripslashes( $settings['auto_target'] ); | |
| 113 | 50 | } |
| 114 | 51 | |
| 115 | - if ( isset( $settings['last_group_id'] ) ) { | |
| 116 | - $options['last_group_id'] = max( 0, intval( $settings['last_group_id'], 10 ) ); | |
| 117 | - | |
| 118 | - if ( ! Red_Group::get( $options['last_group_id'] ) ) { | |
| 119 | - $groups = Red_Group::get_all(); | |
| 120 | - $options['last_group_id'] = $groups[0]['id']; | |
| 121 | - } | |
| 122 | - } | |
| 123 | - | |
| 124 | 52 | if ( isset( $settings['support'] ) ) { |
| 125 | 53 | $options['support'] = $settings['support'] ? true : false; |
| 126 | 54 | } |
| 127 | 55 | |
| 128 | 56 | if ( isset( $settings['token'] ) ) { |
| 129 | - $options['token'] = $settings['token']; | |
| 57 | + $options['token'] = stripslashes( $settings['token'] ); | |
| 130 | 58 | } |
| 131 | 59 | |
| 132 | - if ( isset( $settings['https'] ) ) { | |
| 133 | - $options['https'] = $settings['https'] ? true : false; | |
| 134 | - } | |
| 135 | - | |
| 136 | - if ( isset( $settings['token'] ) && trim( $options['token'] ) === '' ) { | |
| 60 | + if ( !isset( $settings['token'] ) || trim( $options['token'] ) === '' ) { | |
| 137 | 61 | $options['token'] = md5( uniqid() ); |
| 138 | 62 | } |
| 139 | 63 | |
| 140 | 64 | if ( isset( $settings['newsletter'] ) ) { |
| @@ -148,12 +72,8 @@ | ||
| 148 | 72 | if ( isset( $settings['expire_404'] ) ) { |
| 149 | 73 | $options['expire_404'] = max( -1, min( intval( $settings['expire_404'], 10 ), 60 ) ); |
| 150 | 74 | } |
| 151 | 75 | |
| 152 | - if ( isset( $settings['ip_logging'] ) ) { | |
| 153 | - $options['ip_logging'] = max( 0, min( 2, intval( $settings['ip_logging'], 10 ) ) ); | |
| 154 | - } | |
| 155 | - | |
| 156 | 76 | if ( isset( $settings['redirect_cache'] ) ) { |
| 157 | 77 | $options['redirect_cache'] = intval( $settings['redirect_cache'], 10 ); |
| 158 | 78 | |
| 159 | 79 | if ( ! in_array( $options['redirect_cache'], array( -1, 0, 1, 24, 24 * 7 ), true ) ) { |
| @@ -165,28 +85,12 @@ | ||
| 165 | 85 | $module = Red_Module::get( 2 ); |
| 166 | 86 | $options['modules'][2] = $module->update( $settings ); |
| 167 | 87 | } |
| 168 | 88 | |
| 169 | - if ( ! empty( $options['monitor_post'] ) && count( $options['monitor_types'] ) === 0 ) { | |
| 170 | - // If we have a monitor_post set, but no types, then blank everything | |
| 171 | - $options['monitor_post'] = 0; | |
| 172 | - $options['associated_redirect'] = ''; | |
| 89 | + if ( $have_monitor ) { | |
| 90 | + $options['monitor_types'] = $monitor_types; | |
| 173 | 91 | } |
| 174 | 92 | |
| 175 | - $flags = new Red_Source_Flags(); | |
| 176 | - $flags_present = []; | |
| 177 | - | |
| 178 | - foreach ( array_keys( $flags->get_json() ) as $flag ) { | |
| 179 | - if ( isset( $settings[ $flag ] ) ) { | |
| 180 | - $flags_present[ $flag ] = $settings[ $flag ]; | |
| 181 | - } | |
| 182 | - } | |
| 183 | - | |
| 184 | - if ( count( $flags_present ) > 0 ) { | |
| 185 | - $flags->set_flags( $flags_present ); | |
| 186 | - $options = array_merge( $options, $flags->get_json() ); | |
| 187 | - } | |
| 188 | - | |
| 189 | 93 | update_option( REDIRECTION_OPTION, apply_filters( 'redirection_save_options', $options ) ); |
| 190 | 94 | return $options; |
| 191 | 95 | } |
| 192 | 96 | |
| @@ -192,14 +96,24 @@ | ||
| 192 | 96 | |
| 193 | 97 | function red_get_options() { |
| 194 | 98 | $options = get_option( REDIRECTION_OPTION ); |
| 195 | 99 | if ( $options === false ) { |
| 196 | - // Default flags for new installs - ignore case and trailing slashes | |
| 197 | - $options['flags_case'] = true; | |
| 198 | - $options['flags_trailing'] = true; | |
| 100 | + $options = array(); | |
| 199 | 101 | } |
| 200 | 102 | |
| 201 | - $defaults = red_get_default_options(); | |
| 103 | + $defaults = apply_filters( 'red_default_options', array( | |
| 104 | + 'support' => false, | |
| 105 | + 'token' => md5( uniqid() ), | |
| 106 | + 'monitor_post' => 0, // Dont monitor posts by default | |
| 107 | + 'monitor_types' => array(), | |
| 108 | + 'associated_redirect' => '', | |
| 109 | + 'auto_target' => '', | |
| 110 | + 'expire_redirect' => 7, // Expire in 7 days | |
| 111 | + 'expire_404' => 7, // Expire in 7 days | |
| 112 | + 'modules' => array(), | |
| 113 | + 'newsletter' => false, | |
| 114 | + 'redirect_cache' => 1, // 1 hour | |
| 115 | + ) ); | |
| 202 | 116 | |
| 203 | 117 | foreach ( $defaults as $key => $value ) { |
| 204 | 118 | if ( ! isset( $options[ $key ] ) ) { |
| 205 | 119 | $options[ $key ] = $value; |
| @@ -210,34 +124,6 @@ | ||
| 210 | 124 | if ( $options['monitor_post'] > 0 && count( $options['monitor_types'] ) === 0 ) { |
| 211 | 125 | $options['monitor_types'] = array( 'post' ); |
| 212 | 126 | } |
| 213 | 127 | |
| 214 | - // Remove old options not in red_get_default_options() | |
| 215 | - foreach ( $options as $key => $value ) { | |
| 216 | - if ( ! isset( $defaults[ $key ] ) ) { | |
| 217 | - unset( $options[ $key ] ); | |
| 218 | - } | |
| 219 | - } | |
| 220 | - | |
| 221 | 128 | return $options; |
| 222 | -} | |
| 223 | - | |
| 224 | -function red_get_rest_api( $type = false ) { | |
| 225 | - if ( $type === false ) { | |
| 226 | - $options = red_get_options(); | |
| 227 | - $type = $options['rest_api']; | |
| 228 | - } | |
| 229 | - | |
| 230 | - $url = get_rest_url(); // REDIRECTION_API_JSON | |
| 231 | - | |
| 232 | - if ( $type === REDIRECTION_API_JSON_INDEX ) { | |
| 233 | - $url = home_url( '/index.php?rest_route=/' ); | |
| 234 | - } elseif ( $type === REDIRECTION_API_JSON_RELATIVE ) { | |
| 235 | - $relative = wp_parse_url( $url, PHP_URL_PATH ); | |
| 236 | - | |
| 237 | - if ( $relative ) { | |
| 238 | - $url = $relative; | |
| 239 | - } | |
| 240 | - } | |
| 241 | - | |
| 242 | - return $url; | |
| 243 | 129 | } |