| 1 |
<?php |
| 2 |
if(!class_exists('free_SR_redirect_cache')){ |
| 3 |
class free_SR_redirect_cache { |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
/*- Add Redirect ----------------------------------------*/ |
| 8 |
public function add_redirect($post_id,$is_redirected,$redirect_from,$redirect_to,$redirect_type=301) |
| 9 |
{ |
| 10 |
global $wpdb,$table_prefix; |
| 11 |
$table_name = $table_prefix . 'WP_SEO_Cache'; |
| 12 |
$wpdb->query($wpdb->prepare(" insert IGNORE into $table_name(ID,is_redirected,redirect_from,redirect_to,redirect_type) values(%d,%d,%s,%s,%d); ",$post_id,$is_redirected,$redirect_from,$redirect_to,$redirect_type)); |
| 13 |
} |
| 14 |
|
| 15 |
/*- Fetch Redirect ----------------------------------------*/ |
| 16 |
public function fetch_redirect($post_id) |
| 17 |
{ |
| 18 |
global $wpdb,$table_prefix; |
| 19 |
$table_name = $table_prefix . 'WP_SEO_Cache'; |
| 20 |
return $wpdb->get_row("select * from $table_name where ID='$post_id'; "); |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
/* get_current_URL ---------------------------------------------- */ |
| 28 |
public function get_current_URL() |
| 29 |
{ |
| 30 |
$pageURL = 'http'; |
| 31 |
if ( array_key_exists("HTTPS",$_SERVER) && $_SERVER["HTTPS"] == "on") |
| 32 |
{ |
| 33 |
$pageURL .= "s"; |
| 34 |
} |
| 35 |
$pageURL .= "://"; |
| 36 |
|
| 37 |
if (array_key_exists("SERVER_PORT",$_SERVER) && $_SERVER["SERVER_PORT"] != "80") { |
| 38 |
$pageURL .= ($_SERVER["HTTP_HOST"]).":".intval($_SERVER["SERVER_PORT"]).($_SERVER["REQUEST_URI"]); |
| 39 |
} else { |
| 40 |
$pageURL .= ($_SERVER["HTTP_HOST"]).($_SERVER["REQUEST_URI"]); |
| 41 |
} |
| 42 |
|
| 43 |
return $pageURL; |
| 44 |
} |
| 45 |
|
| 46 |
//----------------------------------------------------- |
| 47 |
|
| 48 |
public function remove_url_http_www($url) |
| 49 |
{ |
| 50 |
$url = str_ireplace("http://www.",'',$url); |
| 51 |
$url = str_ireplace("https://www.",'',$url); |
| 52 |
$url = str_ireplace("http://",'',$url); |
| 53 |
$url = str_ireplace("https://",'',$url); |
| 54 |
return ($url); |
| 55 |
} |
| 56 |
//----------------------------------------------------- |
| 57 |
public function make_relative_url($url) |
| 58 |
{ |
| 59 |
if($url=="") |
| 60 |
{ |
| 61 |
return ""; |
| 62 |
} |
| 63 |
$site_url = $this->remove_url_http_www(site_url()); |
| 64 |
if(stripos($url,$site_url) !==false) |
| 65 |
{ |
| 66 |
$url_no_www = $this->remove_url_http_www($url); |
| 67 |
if(strtolower(substr($url_no_www,0,strlen($site_url))) == strtolower($site_url)) |
| 68 |
{ |
| 69 |
$url = str_ireplace($site_url,'',$url_no_www); |
| 70 |
} |
| 71 |
} |
| 72 |
if($url=="") |
| 73 |
{ |
| 74 |
$url="/"; |
| 75 |
} |
| 76 |
return $url; |
| 77 |
} |
| 78 |
|
| 79 |
//---------------------------------------------------- |
| 80 |
public function make_absolute_url($url) |
| 81 |
{ |
| 82 |
if(substr($url,0,1)=='/') |
| 83 |
{ |
| 84 |
$url = site_url() . $url; |
| 85 |
} |
| 86 |
return $url; |
| 87 |
} |
| 88 |
|
| 89 |
//---------------------------------------------------- |
| 90 |
|
| 91 |
public function get_current_relative_url() |
| 92 |
{ |
| 93 |
return $this->make_relative_url($this->get_current_URL()); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
/*- Redirect Cache ----------------------------------------*/ |
| 99 |
public function redirect_cached($post_id) |
| 100 |
{ |
| 101 |
$redirect = $this->fetch_redirect($post_id); |
| 102 |
|
| 103 |
if($redirect != null) |
| 104 |
{ |
| 105 |
if($redirect->is_redirected==1) |
| 106 |
{ |
| 107 |
if($redirect->redirect_type==301) |
| 108 |
{ |
| 109 |
header ('HTTP/1.1 301 Moved Permanently'); |
| 110 |
header ("Location: " . $redirect->redirect_to); |
| 111 |
exit(); |
| 112 |
} |
| 113 |
else if($redirect->redirect_type==307) |
| 114 |
{ |
| 115 |
header ('HTTP/1.0 307 Temporary Redirect'); |
| 116 |
header ("Location: " . $redirect->redirect_to); |
| 117 |
exit(); |
| 118 |
} |
| 119 |
else if($redirect->redirect_type==302) |
| 120 |
{ |
| 121 |
header ("Location: " . $redirect->redirect_to); |
| 122 |
exit(); |
| 123 |
} |
| 124 |
} |
| 125 |
return 'not_redirected'; |
| 126 |
} |
| 127 |
return 'not_found'; |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
/*- Delete Redirect ----------------------------------------*/ |
| 132 |
public function del_redirect($post_id) |
| 133 |
{ |
| 134 |
|
| 135 |
if(isset($_REQUEST['_wpnonce'])) |
| 136 |
$nonce = WPSR_sanitize_text_or_array_field($_REQUEST['_wpnonce']); |
| 137 |
|
| 138 |
if(wp_verify_nonce( $nonce, 'seoredirection' )){ |
| 139 |
|
| 140 |
|
| 141 |
global $wpdb,$table_prefix; |
| 142 |
$table_name = $table_prefix . 'WP_SEO_Cache'; |
| 143 |
return $wpdb->get_var("delete from $table_name where ID='$post_id'; "); |
| 144 |
|
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
/*- Free Cache ----------------------------------------*/ |
| 149 |
public function free_cache() |
| 150 |
{ |
| 151 |
global $wpdb,$table_prefix; |
| 152 |
$table_name = $table_prefix . 'WP_SEO_Cache'; |
| 153 |
$wpdb->query(" TRUNCATE TABLE $table_name "); |
| 154 |
} |
| 155 |
|
| 156 |
/*- Cache Count ----------------------------------------*/ |
| 157 |
public function count_cache() |
| 158 |
{ |
| 159 |
global $wpdb,$table_prefix; |
| 160 |
$table_name = $table_prefix . 'WP_SEO_Cache'; |
| 161 |
return $wpdb->get_var("select count(*) as cnt from $table_name where 1; "); |
| 162 |
} |
| 163 |
|
| 164 |
}} |