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