| 1 |
<?php |
| 2 |
|
| 3 |
class Red_Module |
| 4 |
{ |
| 5 |
function Red_Module ($values = '') |
| 6 |
{ |
| 7 |
if (is_array ($values)) |
| 8 |
{ |
| 9 |
foreach ($values AS $key => $value) |
| 10 |
$this->$key = $value; |
| 11 |
|
| 12 |
if ($this->options) |
| 13 |
$this->load (unserialize ($this->options)); |
| 14 |
} |
| 15 |
} |
| 16 |
|
| 17 |
function module_flush ($items) {} |
| 18 |
function module_flush_delete () {} |
| 19 |
|
| 20 |
function flush ($id) |
| 21 |
{ |
| 22 |
$module = Red_Module::get ($id); |
| 23 |
if ($module && $module->is_valid ()) |
| 24 |
$module->module_flush (Red_Item::get_all_for_module ($id)); |
| 25 |
} |
| 26 |
|
| 27 |
function flush_delete ($id) |
| 28 |
{ |
| 29 |
$module = Red_Module::get ($id); |
| 30 |
if ($module) |
| 31 |
$module->module_flush_delete (); |
| 32 |
} |
| 33 |
|
| 34 |
function update ($data) |
| 35 |
{ |
| 36 |
global $wpdb; |
| 37 |
|
| 38 |
$this->name = $data['name']; |
| 39 |
$name = $wpdb->escape ($data['name']); |
| 40 |
|
| 41 |
$options = $this->save ($data); |
| 42 |
if (empty ($options)) |
| 43 |
$options = 'NULL'; |
| 44 |
else |
| 45 |
$options = "'".$wpdb->escape (serialize ($options))."'"; |
| 46 |
|
| 47 |
$wpdb->query ("UPDATE {$wpdb->prefix}redirection_modules SET name='$name', options=$options WHERE id='{$this->id}'"); |
| 48 |
|
| 49 |
Red_Module::clear_cache ($this->id); |
| 50 |
} |
| 51 |
|
| 52 |
function delete () |
| 53 |
{ |
| 54 |
global $wpdb; |
| 55 |
|
| 56 |
$groups = Red_Group::get_for_module ($this->id); |
| 57 |
if (count ($groups) > 0) |
| 58 |
{ |
| 59 |
foreach ($groups AS $group) |
| 60 |
$group->delete ($group->id); |
| 61 |
} |
| 62 |
|
| 63 |
$wpdb->query ("DELETE FROM {$wpdb->prefix}redirection_modules WHERE id='{$this->id}'"); |
| 64 |
|
| 65 |
RE_Log::delete_for_module ($this->id); |
| 66 |
Red_Module::clear_cache ($this->id); |
| 67 |
Red_Module::flush_delete ($this->id); |
| 68 |
} |
| 69 |
|
| 70 |
function clear_cache ($module) |
| 71 |
{ |
| 72 |
delete_option ('redirection_module_cache'); |
| 73 |
Red_Module::flush ($module); |
| 74 |
} |
| 75 |
|
| 76 |
function create ($data) |
| 77 |
{ |
| 78 |
global $wpdb; |
| 79 |
|
| 80 |
$type = $data['type']; |
| 81 |
$name = $wpdb->escape ($data['name']); |
| 82 |
if (strlen ($name) > 0) |
| 83 |
{ |
| 84 |
$extra2 = $extra = ''; |
| 85 |
if (isset ($data['options'])) |
| 86 |
{ |
| 87 |
$extra = ',options'; |
| 88 |
$extra2 = ",'".$wpdb->escape (serialize ($data['options']))."'"; |
| 89 |
} |
| 90 |
|
| 91 |
$wpdb->query ("INSERT INTO {$wpdb->prefix}redirection_modules (name,type$extra) VALUES ('$name','$type'$extra2)"); |
| 92 |
|
| 93 |
Red_Module::flush ($wpdb->insert_id); |
| 94 |
return $wpdb->insert_id; |
| 95 |
} |
| 96 |
|
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
function get ($id) |
| 101 |
{ |
| 102 |
global $wpdb; |
| 103 |
|
| 104 |
$id = intval ($id); |
| 105 |
$row = $wpdb->get_row ("SELECT * FROM {$wpdb->prefix}redirection_modules WHERE id='$id'", ARRAY_A); |
| 106 |
if ($row) |
| 107 |
return Red_Module::new_item ($row); |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
function get_by_type ($type) |
| 112 |
{ |
| 113 |
global $wpdb; |
| 114 |
|
| 115 |
$cache = get_option ('redirection_module_cache'); |
| 116 |
if ($cache && isset ($cache[$type])) |
| 117 |
return $cache[$type]; |
| 118 |
|
| 119 |
$rows = $wpdb->get_results ("SELECT * FROM {$wpdb->prefix}redirection_modules WHERE type='$type' ORDER BY id", ARRAY_A); |
| 120 |
$items = array (); |
| 121 |
if (count ($rows) > 0) |
| 122 |
{ |
| 123 |
foreach ($rows AS $row) |
| 124 |
$items[] = Red_Module::new_item ($row); |
| 125 |
} |
| 126 |
|
| 127 |
$cache[$type] = $items; |
| 128 |
update_option ('redirection_module_cache', $cache); |
| 129 |
return $items; |
| 130 |
} |
| 131 |
|
| 132 |
function get_all () |
| 133 |
{ |
| 134 |
global $wpdb; |
| 135 |
|
| 136 |
$sql = |
| 137 |
|
| 138 |
$rows = $wpdb->get_results ("SELECT * FROM {$wpdb->prefix}redirection_modules ORDER BY id", ARRAY_A); |
| 139 |
$items = array (); |
| 140 |
if (count ($rows) > 0) |
| 141 |
{ |
| 142 |
foreach ($rows AS $row) |
| 143 |
$items[] = Red_Module::new_item ($row); |
| 144 |
} |
| 145 |
|
| 146 |
return $items; |
| 147 |
} |
| 148 |
|
| 149 |
function get_first_id () |
| 150 |
{ |
| 151 |
global $wpdb; |
| 152 |
return $wpdb->get_var ("SELECT id FROM {$wpdb->prefix}redirection_modules ORDER BY id LIMIT 0,1"); |
| 153 |
} |
| 154 |
|
| 155 |
function get_for_select () |
| 156 |
{ |
| 157 |
$data = array (); |
| 158 |
$items = Red_Module::get_all (); |
| 159 |
foreach ($items AS $item) |
| 160 |
$data[$item->id] = $item->name; |
| 161 |
return $data; |
| 162 |
} |
| 163 |
|
| 164 |
function get_types () |
| 165 |
{ |
| 166 |
return array |
| 167 |
( |
| 168 |
'apache' => __ ('Apache', 'redirection'), |
| 169 |
'wp' => __ ('WordPress', 'redirection'), |
| 170 |
'404' => __ ('404 Errors', 'redirection'), |
| 171 |
); |
| 172 |
} |
| 173 |
|
| 174 |
function new_item ($data) |
| 175 |
{ |
| 176 |
$map = array |
| 177 |
( |
| 178 |
'apache' => array ('Apache_Module', 'apache.php'), |
| 179 |
'wp' => array ('WordPress_Module', 'wordpress.php'), |
| 180 |
'404' => array ('Error404_Module', '404.php') |
| 181 |
); |
| 182 |
|
| 183 |
$obj = $map[$data['type']][0]; |
| 184 |
$file = $map[$data['type']][1]; |
| 185 |
|
| 186 |
if (!class_exists ($obj)) |
| 187 |
include (dirname (__FILE__)."/../modules/$file"); |
| 188 |
return new $obj ($data); |
| 189 |
} |
| 190 |
|
| 191 |
function canonical () |
| 192 |
{ |
| 193 |
$can = array ('none' => '—', 'nowww' => __ ('Strip WWW', 'redirection'), 'www' => __ ('Force WWW', 'redirection')); |
| 194 |
return $can[$this->canonical]; |
| 195 |
} |
| 196 |
|
| 197 |
function index () |
| 198 |
{ |
| 199 |
$can = array ('ignore' => '—', 'remove' => __ ('Strip index.php', 'redirection')); |
| 200 |
return $can[$this->index]; |
| 201 |
} |
| 202 |
|
| 203 |
function options () { } |
| 204 |
|
| 205 |
function type () |
| 206 |
{ |
| 207 |
$types = $this->get_types (); |
| 208 |
return $types[$this->type]; |
| 209 |
} |
| 210 |
|
| 211 |
function checked ($item, $field = '') |
| 212 |
{ |
| 213 |
if ($field && is_array ($item)) |
| 214 |
{ |
| 215 |
if (isset ($item[$field]) && $item[$field]) |
| 216 |
echo ' checked="checked"'; |
| 217 |
} |
| 218 |
else if (!empty ($item)) |
| 219 |
echo ' checked="checked"'; |
| 220 |
} |
| 221 |
|
| 222 |
function select ($items, $default = '') |
| 223 |
{ |
| 224 |
if (count ($items) > 0) |
| 225 |
{ |
| 226 |
foreach ($items AS $key => $value) |
| 227 |
echo '<option value="'.$key.'"'.($key == $default ? ' selected="selected"' : '').'>'.$value.'</option>'; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
function groups () |
| 232 |
{ |
| 233 |
global $wpdb; |
| 234 |
|
| 235 |
return $wpdb->get_var ("SELECT COUNT(id) FROM {$wpdb->prefix}redirection_groups WHERE module_id='{$this->id}'"); |
| 236 |
} |
| 237 |
|
| 238 |
function redirects () |
| 239 |
{ |
| 240 |
global $wpdb; |
| 241 |
|
| 242 |
$count = $wpdb->get_var ("SELECT COUNT({$wpdb->prefix}redirection_items.id) FROM {$wpdb->prefix}redirection_groups INNER JOIN {$wpdb->prefix}redirection_items ON {$wpdb->prefix}redirection_items.group_id={$wpdb->prefix}redirection_groups.id WHERE module_id='{$this->id}' GROUP BY {$wpdb->prefix}redirection_items.group_id"); |
| 243 |
if ($count > 0) |
| 244 |
return $count; |
| 245 |
return 0; |
| 246 |
} |
| 247 |
|
| 248 |
function hits () |
| 249 |
{ |
| 250 |
global $wpdb; |
| 251 |
// $count = $wpdb->get_var ("SELECT SUM(last_count) FROM {$wpdb->prefix}redirection_groups INNER JOIN {$wpdb->prefix}redirection_items ON {$wpdb->prefix}redirection_items.group_id=wp_redirection_groups.id WHERE wp_redirection_groups.module_id='{$this->id}'"); |
| 252 |
$count = $wpdb->get_var ("SELECT COUNT(id) FROM {$wpdb->prefix}redirection_logs WHERE module_id='{$this->id}'"); |
| 253 |
if ($count > 0) |
| 254 |
return $count; |
| 255 |
return 0; |
| 256 |
} |
| 257 |
|
| 258 |
function reset () |
| 259 |
{ |
| 260 |
Red_Module::clear_cache ($this->id); |
| 261 |
|
| 262 |
$groups = Red_Group::get_for_module ($this->id); |
| 263 |
if (count ($groups) > 0) |
| 264 |
{ |
| 265 |
foreach ($groups AS $group) |
| 266 |
$group->reset (); |
| 267 |
} |
| 268 |
|
| 269 |
RE_Log::delete_for_module ($this->id); |
| 270 |
} |
| 271 |
|
| 272 |
function name_extra () { return '';} |
| 273 |
function is_valid () { return true; } |
| 274 |
function load ($data) { } |
| 275 |
function config () { } |
| 276 |
} |
| 277 |
|
| 278 |
?> |