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