| 1 |
<?php |
| 2 |
define('option_group_name','clogica_option_group'); |
| 3 |
if(!class_exists('clogica_SR_redirect_cache')){ |
| 4 |
class clogica_SR_redirect_cache { |
| 5 |
|
| 6 |
public static $show_notifications=true; |
| 7 |
private $option_group_name='clogica_option_group'; |
| 8 |
private $cf; |
| 9 |
private $request; |
| 10 |
|
| 11 |
|
| 12 |
/* Set the object's parent cf to access all objects ------------------ */ |
| 13 |
public function set_cf($cf) |
| 14 |
{ |
| 15 |
$this->cf=$cf; |
| 16 |
$this->request= call_user_func(array($cf, 'get_request')); |
| 17 |
//$this->request = $cf::get_request(); |
| 18 |
} |
| 19 |
|
| 20 |
/* initialization -------------------------------------------------- */ |
| 21 |
public function init($option_group_name) |
| 22 |
{ |
| 23 |
$this->option_group_name=$option_group_name; |
| 24 |
} |
| 25 |
|
| 26 |
/* set_option_group -------------------------------------------------- */ |
| 27 |
public static function set_option_group($option_group_name) |
| 28 |
{ |
| 29 |
$this->option_group_name=$option_group_name; |
| 30 |
} |
| 31 |
|
| 32 |
/* get_option_group -------------------------------------------------- */ |
| 33 |
public static function get_option_group() |
| 34 |
{ |
| 35 |
return option_group_name; |
| 36 |
} |
| 37 |
|
| 38 |
/* update_my_options ------------------------------------------------- */ |
| 39 |
public static function update_my_options($options,$blog=0) |
| 40 |
{ |
| 41 |
if(intval($blog)<=0) |
| 42 |
{ |
| 43 |
update_site_option(self::get_option_group(),$options); |
| 44 |
}else |
| 45 |
{ |
| 46 |
update_blog_option($blog, self::get_option_group(), $options); |
| 47 |
} |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
/* get_my_options ---------------------------------------------------- */ |
| 52 |
public static function get_my_options($blog=0) |
| 53 |
{ |
| 54 |
if(intval($blog)<=0) |
| 55 |
{ |
| 56 |
$options=get_site_option(self::get_option_group()); |
| 57 |
if(!is_array($options)) |
| 58 |
{ |
| 59 |
$options= array(); |
| 60 |
add_site_option(self::get_option_group(),$options); |
| 61 |
} |
| 62 |
return $options; |
| 63 |
}else |
| 64 |
{ |
| 65 |
$options=get_blog_option($blog, self::get_option_group()); |
| 66 |
if(!is_array($options)) |
| 67 |
{ |
| 68 |
$options= array(); |
| 69 |
add_blog_option($blog, self::get_option_group(),$options); |
| 70 |
} |
| 71 |
return $options; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/* read_option_value ------------------------------------------------- */ |
| 76 |
public static function read_option_value($key,$default='',$blog=0) |
| 77 |
{ |
| 78 |
$options=self::get_my_options($blog); |
| 79 |
if(array_key_exists($key,$options)) |
| 80 |
{ |
| 81 |
return $options[$key]; |
| 82 |
}else |
| 83 |
{ |
| 84 |
self::save_option_value($key,$default,$blog); |
| 85 |
return $default; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
/* save_option_value ------------------------------------------------- */ |
| 90 |
public static function save_option_value($key,$value,$blog=0) |
| 91 |
{ |
| 92 |
$options=self::get_my_options($blog); |
| 93 |
$options[$key]=$value; |
| 94 |
self::update_my_options($options,$blog); |
| 95 |
} |
| 96 |
|
| 97 |
/* save_post_option_value ------------------------------------------------- */ |
| 98 |
public function save_post_option_value($key,$type='text',$blog=0) |
| 99 |
{ |
| 100 |
$options=self::get_my_options($blog); |
| 101 |
$options[$key]=$this->request->post($key,$type); |
| 102 |
$this->update_my_options($options,$blog); |
| 103 |
} |
| 104 |
|
| 105 |
/* save_get_option_value -------------------------------------------------- */ |
| 106 |
public function save_get_option_value($key,$type='text',$blog=0) |
| 107 |
{ |
| 108 |
$options=self::get_my_options($blog); |
| 109 |
$options[$key]=$this->request->get($key,$type); |
| 110 |
self::update_my_options($options,$blog); |
| 111 |
} |
| 112 |
|
| 113 |
/* delete_my_options ----------------------------------------------------- */ |
| 114 |
public function delete_my_options($blog) |
| 115 |
{ |
| 116 |
if(intval($blog)<=0) |
| 117 |
{ |
| 118 |
delete_site_option(self::get_option_group()); |
| 119 |
}else |
| 120 |
{ |
| 121 |
delete_blog_option($blog,self::get_option_group()); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
/*- Add Redirect ----------------------------------------*/ |
| 126 |
public function add_redirect($post_id,$is_redirected,$redirect_from,$redirect_to,$redirect_type=301) |
| 127 |
{ |
| 128 |
if(self::cache_enabled()){ |
| 129 |
global $wpdb,$table_prefix; |
| 130 |
$table_name = $table_prefix."WP_SEO_Cache"; |
| 131 |
$wpdb->query(" insert IGNORE into $table_name(ID,is_redirected,redirect_from,redirect_to,redirect_type) values('$post_id','$is_redirected','$redirect_from','$redirect_to','$redirect_type'); "); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
/*- Fetch Redirect ----------------------------------------*/ |
| 136 |
public function fetch_redirect($post_id) |
| 137 |
{ |
| 138 |
global $wpdb,$table_prefix; |
| 139 |
$table_name = $table_prefix."WP_SEO_Cache"; |
| 140 |
return $wpdb->get_row("select * from $table_name where ID='$post_id'; "); |
| 141 |
} |
| 142 |
|
| 143 |
/*- Redirect Cache ----------------------------------------*/ |
| 144 |
public function redirect_cached($post_id) |
| 145 |
{ |
| 146 |
$redirect = self::fetch_redirect($post_id); |
| 147 |
if($redirect != null && $redirect->redirect_from==self::get_current_relative_url() && self::cache_enabled()) |
| 148 |
{ |
| 149 |
|
| 150 |
if($redirect->is_redirected==1) |
| 151 |
{ |
| 152 |
if($redirect->redirect_type==301) |
| 153 |
{ |
| 154 |
header ('HTTP/1.1 301 Moved Permanently'); |
| 155 |
header ("Location: " . $redirect->redirect_to); |
| 156 |
exit(); |
| 157 |
} |
| 158 |
else if($redirect->redirect_type==307) |
| 159 |
{ |
| 160 |
header ('HTTP/1.0 307 Temporary Redirect'); |
| 161 |
header ("Location: " . $redirect->redirect_to); |
| 162 |
exit(); |
| 163 |
} |
| 164 |
else if($redirect->redirect_type==302) |
| 165 |
{ |
| 166 |
header ("Location: " . $redirect->redirect_to); |
| 167 |
exit(); |
| 168 |
} |
| 169 |
} |
| 170 |
return 'not_redirected'; |
| 171 |
} |
| 172 |
return 'not_found'; |
| 173 |
} |
| 174 |
|
| 175 |
/*- Delete Redirect ----------------------------------------*/ |
| 176 |
public function del_redirect($post_id) |
| 177 |
{ |
| 178 |
if(isset($_REQUEST['_wpnonce'])) |
| 179 |
$nonce = WPSR_sanitize_text_or_array_field($_REQUEST['_wpnonce']); |
| 180 |
|
| 181 |
if(wp_verify_nonce( $nonce, 'seoredirection' )){ |
| 182 |
|
| 183 |
global $wpdb,$table_prefix; |
| 184 |
$table_name = $table_prefix."WP_SEO_Cache"; |
| 185 |
return $wpdb->get_var("delete from $table_name where ID='$post_id'; "); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
/*- Free Cache ----------------------------------------*/ |
| 190 |
public static function free_cache($force=0) |
| 191 |
{ |
| 192 |
if(self::cache_enabled() || $force==1){ |
| 193 |
global $wpdb,$table_prefix; |
| 194 |
$table_name = $table_prefix."WP_SEO_Cache"; |
| 195 |
$wpdb->query("delete * from $table_name"); |
| 196 |
if(self::show_notifications() && $force!=2) |
| 197 |
echo "<b>".__("All cached redirects are deleted!",'seo-redirection')."</b>"; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
/*- Free Cache ----------------------------------------*/ |
| 202 |
public static function free_cache_without_notification(){ |
| 203 |
self::free_cache(2); |
| 204 |
} |
| 205 |
|
| 206 |
/*- Cache Count ----------------------------------------*/ |
| 207 |
public function count_cache() |
| 208 |
{ |
| 209 |
global $wpdb,$table_prefix; |
| 210 |
$table_name = $table_prefix."WP_SEO_Cache"; |
| 211 |
return $wpdb->get_var("select count(*) as cnt from $table_name "); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
/* ----------------------------------------------- */ |
| 216 |
public static function show_notifications() |
| 217 |
{ |
| 218 |
return self::$show_notifications; |
| 219 |
} |
| 220 |
|
| 221 |
/* ----------------------------------------------- */ |
| 222 |
public static function cache_enabled() |
| 223 |
{ |
| 224 |
return (self::read_option_value("cache_enable")!=0); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
}} |