Exemplo n.º 1
0
function WPSR_redirect()
{
    global $wpdb, $post, $table_prefix, $util;
    if ($util->get_option_value('plugin_status') == '1') {
        $table_name = $table_prefix . 'WP_SEO_Redirection';
        $permalink = urldecode($util->get_current_relative_url());
        if (substr($permalink, 0, 1) == ":") {
            $first_slash = stripos($permalink, "/");
            $permalink = substr($permalink, $first_slash, strlen($permalink) - $first_slash);
        }
        $post_cache_result = "";
        $SR_redirect_cache = new free_SR_redirect_cache();
        if (is_singular()) {
            $post_cache_result = $SR_redirect_cache->redirect_cached($post->ID);
        }
        if ($post_cache_result == 'not_redirected') {
            return 0;
        }
        $permalink_alternative = "";
        if (substr($permalink, -1) == '/') {
            $permalink_alternative = substr($permalink, 0, intval(strlen($permalink) - 1));
        } else {
            $permalink_alternative = $permalink . '/';
        }
        $permalink_options = "(redirect_from='{$permalink}' or redirect_from='{$permalink_alternative}' )";
        $permalink_regex_options = "('{$permalink}' regexp regex or '{$permalink_alternative}'  regexp regex )";
        if ($util->get_option_value('redirect_control_panel') != '1' || $util->get_option_value('redirect_control_panel') == '1' && !preg_match('/^' . str_replace('/', '\\/', get_admin_url()) . '/i', $permalink) && !preg_match('/^' . str_replace('/', '\\/', site_url()) . '\\/wp-login.php/i', $permalink)) {
            $theurl = $wpdb->get_row(" select * from {$table_name} where enabled=1 and regex='' and {$permalink_options}  ");
            if ($wpdb->num_rows > 0 && $theurl->redirect_to != '') {
                WPSR_make_redirect($theurl->redirect_to, $theurl->redirect_type, $permalink, $theurl);
            }
            $theurl = $wpdb->get_row(" select * from {$table_name} where enabled=1 and regex<>'' and {$permalink_regex_options} order by LENGTH(regex) desc ");
            if ($wpdb->num_rows > 0 && $theurl->redirect_to != '') {
                WPSR_make_redirect($theurl->redirect_to, $theurl->redirect_type, $permalink, $theurl);
            }
            if (is_404()) {
                if ($util->get_option_value('p404_discovery_status') == '1') {
                    WPSR_log_404_redirection($permalink);
                }
                $options = $util->get_my_options();
                if ($options['p404_status'] == '1') {
                    WPSR_make_redirect($options['p404_redirect_to'], '301', $permalink);
                }
            }
        }
        if (is_singular() && $post_cache_result == 'not_found') {
            $SR_redirect_cache->add_redirect($post->ID, 0, '', '', 0);
        }
    }
}
        } else {
            if ($redirect_from == '' || $redirect_to == '' || $redirect_type == '') {
                $util->failure_option_msg('Please input all required fields!');
            } else {
                $wpdb->query(" insert into {$table_name}(redirect_from,redirect_to,redirect_type,url_type,redirect_from_type,redirect_from_folder_settings,redirect_from_subfolders,redirect_to_type,redirect_to_folder_settings,regex,enabled) values('{$redirect_from}','{$redirect_to}','{$redirect_type}',1,'{$redirect_from_type}','{$redirect_from_folder_settings}','{$redirect_from_subfolders}','{$redirect_to_type}','{$redirect_to_folder_settings}','{$regex}','{$enabled}') ");
                $wpdb->query($wpdb->prepare(" delete from {$table_name_404} where link=%s ", $redirect_from));
                $SR_redirect_cache = new free_SR_redirect_cache();
                $SR_redirect_cache->free_cache();
            }
        }
    } else {
        if ($util->post('edit_exist') != '') {
            $edit = $util->post('edit');
            if ($redirect_from == '' || $redirect_to == '' || $redirect_type == '') {
                $util->failure_option_msg('Please input all required fields!');
            } else {
                $wpdb->query("update {$table_name} set redirect_from='{$redirect_from}',redirect_to='{$redirect_to}',redirect_type='{$redirect_type}',redirect_from_type='{$redirect_from_type}' ,redirect_from_folder_settings='{$redirect_from_folder_settings}',redirect_from_subfolders='{$redirect_from_subfolders}' ,redirect_to_type='{$redirect_to_type}' ,redirect_to_folder_settings='{$redirect_to_folder_settings}' ,regex='{$regex}',enabled='{$enabled}'  where ID='{$edit}' ");
                $SR_redirect_cache = new free_SR_redirect_cache();
                $SR_redirect_cache->free_cache();
            }
        }
    }
    if ($util->there_is_cache() != '') {
        $util->info_option_msg("You have a cache plugin installed <b>'" . $util->there_is_cache() . "'</b>, you have to clear cache after any changes to get the changes reflected immediately! ");
    }
}
if ($util->get('add') != '' || $util->get('edit') != '') {
    include "option_page_custome_redirection_add_update.php";
} else {
    include "option_page_custome_redirection_list.php";
}