| 1 |
<?php |
| 2 |
|
| 3 |
class Red_Group { |
| 4 |
private $items = 0; |
| 5 |
private $name; |
| 6 |
|
| 7 |
public function __construct( $values = '' ) { |
| 8 |
if ( is_object( $values ) ) { |
| 9 |
foreach ( $values AS $key => $value ) { |
| 10 |
$this->$key = $value; |
| 11 |
} |
| 12 |
} |
| 13 |
} |
| 14 |
|
| 15 |
public function get_name() { |
| 16 |
return $this->name; |
| 17 |
} |
| 18 |
|
| 19 |
public function get_id() { |
| 20 |
return $this->id; |
| 21 |
} |
| 22 |
|
| 23 |
public function is_disabled() { |
| 24 |
return $this->status == 'disabled' ? true : false; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Get list of groups |
| 29 |
*/ |
| 30 |
static function get( $id ) { |
| 31 |
global $wpdb; |
| 32 |
|
| 33 |
$row = $wpdb->get_row( $wpdb->prepare( "SELECT {$wpdb->prefix}redirection_groups.*,COUNT( {$wpdb->prefix}redirection_items.id ) AS items,SUM( {$wpdb->prefix}redirection_items.last_count ) AS redirects FROM {$wpdb->prefix}redirection_groups LEFT JOIN {$wpdb->prefix}redirection_items ON {$wpdb->prefix}redirection_items.group_id={$wpdb->prefix}redirection_groups.id WHERE {$wpdb->prefix}redirection_groups.id=%d GROUP BY {$wpdb->prefix}redirection_groups.id", $id ) ); |
| 34 |
if ( $row ) |
| 35 |
return new Red_Group( $row ); |
| 36 |
return false; |
| 37 |
} |
| 38 |
|
| 39 |
static function get_for_module( $module ) { |
| 40 |
global $wpdb; |
| 41 |
|
| 42 |
$sql = $wpdb->prepare( "SELECT SQL_CALC_FOUND_ROWS {$wpdb->prefix}redirection_groups.* FROM {$wpdb->prefix}redirection_groups WHERE {$wpdb->prefix}redirection_groups.module_id=%d", $module ); |
| 43 |
|
| 44 |
$rows = $wpdb->get_results( $sql ); |
| 45 |
$items = array(); |
| 46 |
if ( count( $rows ) > 0 ) { |
| 47 |
foreach( $rows AS $row ) { |
| 48 |
$items[] = new Red_Group( $row ); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
return $items; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Get all groups with number of items in each group |
| 57 |
* DBW |
| 58 |
*/ |
| 59 |
static function get_all( $module, $pager ) { |
| 60 |
global $wpdb; |
| 61 |
|
| 62 |
$sql = $wpdb->prepare( "SELECT SQL_CALC_FOUND_ROWS {$wpdb->prefix}redirection_groups.*,COUNT( {$wpdb->prefix}redirection_items.id ) AS items,SUM( {$wpdb->prefix}redirection_items.last_count ) AS redirects FROM {$wpdb->prefix}redirection_groups LEFT JOIN {$wpdb->prefix}redirection_items ON {$wpdb->prefix}redirection_items.group_id={$wpdb->prefix}redirection_groups.id WHERE {$wpdb->prefix}redirection_groups.module_id=%d", $module ); |
| 63 |
$sql .= str_replace( 'WHERE', 'AND', $pager->to_limits( '', array( 'name' ), '', "GROUP BY {$wpdb->prefix}redirection_groups.id" ) ); |
| 64 |
|
| 65 |
$rows = $wpdb->get_results( $sql ); |
| 66 |
$pager->set_total( $wpdb->get_var( "SELECT FOUND_ROWS()" ) ); |
| 67 |
$items = array(); |
| 68 |
if ( count( $rows ) > 0 ) { |
| 69 |
foreach( $rows AS $row ) { |
| 70 |
$items[] = new Red_Group( $row ); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
return $items; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Get list of groups |
| 79 |
* DBW |
| 80 |
*/ |
| 81 |
static function get_for_select() { |
| 82 |
global $wpdb; |
| 83 |
|
| 84 |
$data = array(); |
| 85 |
$rows = $wpdb->get_results( "SELECT {$wpdb->prefix}redirection_modules.name AS module_name,{$wpdb->prefix}redirection_groups.name AS group_name,{$wpdb->prefix}redirection_groups.id FROM {$wpdb->prefix}redirection_groups INNER JOIN {$wpdb->prefix}redirection_modules ON {$wpdb->prefix}redirection_modules.id={$wpdb->prefix}redirection_groups.module_id ORDER BY {$wpdb->prefix}redirection_modules.name,{$wpdb->prefix}redirection_groups.position" ); |
| 86 |
if ( $rows ) { |
| 87 |
foreach ( $rows AS $row ) { |
| 88 |
$data[$row->module_name][$row->id] = $row->group_name; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
return $data; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Get first group ID |
| 97 |
*/ |
| 98 |
static function get_first_id() { |
| 99 |
global $wpdb; |
| 100 |
|
| 101 |
return intval( $wpdb->get_var( "SELECT id FROM {$wpdb->prefix}redirection_groups ORDER BY id LIMIT 0,1" ) ); |
| 102 |
} |
| 103 |
|
| 104 |
static function create( $data ) { |
| 105 |
global $wpdb; |
| 106 |
|
| 107 |
$name = trim( $data['name'] ); |
| 108 |
$module = intval( $data['module_id'] ); |
| 109 |
|
| 110 |
if ( $name != '' && $module > 0 ) { |
| 111 |
$position = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( * ) FROM {$wpdb->prefix}redirection_groups WHERE module_id=%d", $module ) ); |
| 112 |
if ( isset( $data['position'] ) ) |
| 113 |
$position = $data['position']; |
| 114 |
|
| 115 |
$data = array( |
| 116 |
'name' => trim( $name ), |
| 117 |
'module_id' => intval( $module ), |
| 118 |
'position' => intval( $position ) |
| 119 |
); |
| 120 |
|
| 121 |
if ( isset( $data['status'] ) && isset( $data['position'] ) ) |
| 122 |
$data['status'] = $data['status']; |
| 123 |
|
| 124 |
$wpdb->insert( $wpdb->prefix.'redirection_groups', $data ); |
| 125 |
|
| 126 |
Red_Module::flush( $module ); |
| 127 |
return Red_Group::get( $wpdb->insert_id ); |
| 128 |
} |
| 129 |
|
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
function update( $data ) { |
| 134 |
global $wpdb; |
| 135 |
|
| 136 |
$data = array_map( 'stripslashes', $data ); |
| 137 |
|
| 138 |
$this->tracking = isset( $data['tracking'] ) ? true : false; |
| 139 |
$this->status = isset( $data['status'] ) ? 'enabled' : 'disabled'; |
| 140 |
$this->name = trim( wp_kses( $data['name'], array() ) ); |
| 141 |
|
| 142 |
$wpdb->update( $wpdb->prefix.'redirection_groups', array( 'name' => $this->name, 'status' => $this->status, 'tracking' => intval( $this->tracking ) ), array( 'id' => intval( $this->id ) ) ); |
| 143 |
|
| 144 |
Red_Module::flush( $this->module_id ); |
| 145 |
} |
| 146 |
|
| 147 |
function delete() { |
| 148 |
global $wpdb; |
| 149 |
|
| 150 |
// Delete all items in this group |
| 151 |
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_items WHERE group_id=%d", $this->id ) ); |
| 152 |
|
| 153 |
// Delete the group |
| 154 |
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_groups WHERE id=%d", $this->id ) ); |
| 155 |
} |
| 156 |
|
| 157 |
static function save_order( $items, $start ) { |
| 158 |
global $wpdb; |
| 159 |
|
| 160 |
foreach ( $items AS $pos => $id ) { |
| 161 |
$wpdb->update( $wpdb->prefix.'redirection_groups', array( 'position' => $pos + $start ), array( 'id' => intval( $id ) ) ); |
| 162 |
} |
| 163 |
|
| 164 |
$group = self::get( $items[0] ); |
| 165 |
Red_Module::flush( $group->module_id ); |
| 166 |
} |
| 167 |
|
| 168 |
function move_to( $module ) { |
| 169 |
global $wpdb; |
| 170 |
|
| 171 |
$wpdb->update( $wpdb->prefix.'redirection_groups', array( 'module_id' => intval( $module ) ), array( 'id' => $this->id ) ); |
| 172 |
|
| 173 |
Red_Module::flush( $module ); |
| 174 |
Red_Module::flush( $this->id ); |
| 175 |
} |
| 176 |
|
| 177 |
function reset() { |
| 178 |
global $wpdb; |
| 179 |
|
| 180 |
$this->last_count = 0; |
| 181 |
$this->last_access = '0000-00-00 00:00:00'; |
| 182 |
|
| 183 |
$wpdb->update( $wpdb->prefix.'redirection_items', array( 'last_count' => 0, 'last_access' => $this->last_access ), array( 'group_id' => $this->id ) ); |
| 184 |
|
| 185 |
RE_Log::delete_for_group( $this->id ); |
| 186 |
} |
| 187 |
|
| 188 |
function get_item_count() { |
| 189 |
global $wpdb; |
| 190 |
|
| 191 |
return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_items WHERE group_id=%d", $this->id ) ); |
| 192 |
} |
| 193 |
|
| 194 |
function type() { |
| 195 |
if ( $this->apache ) |
| 196 |
return '.ht'; |
| 197 |
return 'WP'; |
| 198 |
} |
| 199 |
|
| 200 |
function tracked() { |
| 201 |
if ( $this->tracking == 1 ) |
| 202 |
return __( 'Yes', 'redirection' ); |
| 203 |
return __( 'No', 'redirection' ); |
| 204 |
} |
| 205 |
|
| 206 |
function toggle_status() { |
| 207 |
global $wpdb; |
| 208 |
|
| 209 |
$this->status = ( $this->status == 'enabled' ) ? 'disabled' : 'enabled'; |
| 210 |
|
| 211 |
$wpdb->update( $wpdb->prefix.'redirection_groups', array( 'status' => $this->status ), array( 'id' => $this->id ) ); |
| 212 |
} |
| 213 |
|
| 214 |
function hits() { |
| 215 |
global $wpdb; |
| 216 |
|
| 217 |
return (int)$wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_logs WHERE group_id=%d", $this->id ) ); |
| 218 |
} |
| 219 |
} |
| 220 |
|