| 1 |
<?php |
| 2 |
|
| 3 |
class Red_Group { |
| 4 |
private $items = 0; |
| 5 |
private $name; |
| 6 |
private $module_id; |
| 7 |
private $status; |
| 8 |
private $position; |
| 9 |
|
| 10 |
public function __construct( $values = '' ) { |
| 11 |
if ( is_object( $values ) ) { |
| 12 |
$this->name = $values->name; |
| 13 |
$this->module_id = intval( $values->module_id, 10 ); |
| 14 |
$this->status = $values->status; |
| 15 |
$this->id = intval( $values->id, 10 ); |
| 16 |
$this->position = intval( $values->position, 10 ); |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
public function get_name() { |
| 21 |
return $this->name; |
| 22 |
} |
| 23 |
|
| 24 |
public function get_id() { |
| 25 |
return $this->id; |
| 26 |
} |
| 27 |
|
| 28 |
public function is_enabled() { |
| 29 |
return $this->status === 'enabled' ? true : false; |
| 30 |
} |
| 31 |
|
| 32 |
static function get( $id ) { |
| 33 |
global $wpdb; |
| 34 |
|
| 35 |
$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 ) ); |
| 36 |
if ( $row ) { |
| 37 |
return new Red_Group( $row ); |
| 38 |
} |
| 39 |
|
| 40 |
return false; |
| 41 |
} |
| 42 |
|
| 43 |
static function get_all() { |
| 44 |
global $wpdb; |
| 45 |
|
| 46 |
$data = array(); |
| 47 |
$rows = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}redirection_groups" ); |
| 48 |
|
| 49 |
if ( $rows ) { |
| 50 |
foreach ( $rows as $row ) { |
| 51 |
$group = new Red_Group( $row ); |
| 52 |
$data[] = $group->to_json(); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
return $data; |
| 57 |
} |
| 58 |
|
| 59 |
static function get_all_for_module( $module_id ) { |
| 60 |
global $wpdb; |
| 61 |
|
| 62 |
$data = array(); |
| 63 |
$rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_groups WHERE module_id=%d", $module_id ) ); |
| 64 |
|
| 65 |
if ( $rows ) { |
| 66 |
foreach ( $rows as $row ) { |
| 67 |
$group = new Red_Group( $row ); |
| 68 |
$data[] = $group->to_json(); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
return $data; |
| 73 |
} |
| 74 |
|
| 75 |
static function get_for_select() { |
| 76 |
global $wpdb; |
| 77 |
|
| 78 |
$data = array(); |
| 79 |
$rows = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}redirection_groups" ); |
| 80 |
|
| 81 |
if ( $rows ) { |
| 82 |
foreach ( $rows as $row ) { |
| 83 |
$module = Red_Module::get( $row->module_id ); |
| 84 |
if ( $module ) { |
| 85 |
$data[ $module->get_name() ][ intval( $row->id, 10 ) ] = $row->name; |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
return $data; |
| 91 |
} |
| 92 |
|
| 93 |
static function create( $name, $module_id, $enabled = true ) { |
| 94 |
global $wpdb; |
| 95 |
|
| 96 |
$name = trim( substr( $name, 0, 50 ) ); |
| 97 |
$module_id = intval( $module_id, 10 ); |
| 98 |
|
| 99 |
if ( $name !== '' && Red_Module::is_valid_id( $module_id ) ) { |
| 100 |
$position = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( * ) FROM {$wpdb->prefix}redirection_groups WHERE module_id=%d", $module_id ) ); |
| 101 |
|
| 102 |
$data = array( |
| 103 |
'name' => trim( $name ), |
| 104 |
'module_id' => intval( $module_id ), |
| 105 |
'position' => intval( $position ), |
| 106 |
'status' => $enabled ? 'enabled' : 'disabled', |
| 107 |
); |
| 108 |
|
| 109 |
$wpdb->insert( $wpdb->prefix . 'redirection_groups', $data ); |
| 110 |
|
| 111 |
return Red_Group::get( $wpdb->insert_id ); |
| 112 |
} |
| 113 |
|
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
public function update( $data ) { |
| 118 |
global $wpdb; |
| 119 |
|
| 120 |
$old_id = $this->module_id; |
| 121 |
$this->name = trim( wp_kses( $data['name'], array() ) ); |
| 122 |
|
| 123 |
if ( Red_Module::is_valid_id( intval( $data['moduleId'], 10 ) ) ) { |
| 124 |
$this->module_id = intval( $data['moduleId'], 10 ); |
| 125 |
} |
| 126 |
|
| 127 |
$wpdb->update( $wpdb->prefix . 'redirection_groups', array( 'name' => $this->name, 'module_id' => $this->module_id ), array( 'id' => intval( $this->id ) ) ); |
| 128 |
|
| 129 |
if ( $old_id !== $this->module_id ) { |
| 130 |
Red_Module::flush_by_module( $old_id ); |
| 131 |
Red_Module::flush_by_module( $this->module_id ); |
| 132 |
} |
| 133 |
|
| 134 |
return true; |
| 135 |
} |
| 136 |
|
| 137 |
public function delete() { |
| 138 |
global $wpdb; |
| 139 |
|
| 140 |
// Delete all items in this group |
| 141 |
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_items WHERE group_id=%d", $this->id ) ); |
| 142 |
|
| 143 |
Red_Module::flush( $this->id ); |
| 144 |
|
| 145 |
// Delete the group |
| 146 |
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_groups WHERE id=%d", $this->id ) ); |
| 147 |
|
| 148 |
if ( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_groups" ) === 0 ) { |
| 149 |
$wpdb->insert( $wpdb->prefix . 'redirection_groups', array( 'name' => __( 'Redirections' ), 'module_id' => 1, 'position' => 0 ) ); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
public function get_total_redirects() { |
| 154 |
global $wpdb; |
| 155 |
|
| 156 |
return intval( $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_items WHERE group_id=%d", $this->id ) ), 10 ); |
| 157 |
} |
| 158 |
|
| 159 |
public function enable() { |
| 160 |
global $wpdb; |
| 161 |
|
| 162 |
$wpdb->update( $wpdb->prefix . 'redirection_groups', array( 'status' => 'enabled' ), array( 'id' => $this->id ) ); |
| 163 |
$wpdb->update( $wpdb->prefix . 'redirection_items', array( 'status' => 'enabled' ), array( 'group_id' => $this->id ) ); |
| 164 |
|
| 165 |
Red_Module::flush( $this->id ); |
| 166 |
} |
| 167 |
|
| 168 |
public function disable() { |
| 169 |
global $wpdb; |
| 170 |
|
| 171 |
$wpdb->update( $wpdb->prefix . 'redirection_groups', array( 'status' => 'disabled' ), array( 'id' => $this->id ) ); |
| 172 |
$wpdb->update( $wpdb->prefix . 'redirection_items', array( 'status' => 'disabled' ), array( 'group_id' => $this->id ) ); |
| 173 |
|
| 174 |
Red_Module::flush( $this->id ); |
| 175 |
} |
| 176 |
|
| 177 |
public function get_module_id() { |
| 178 |
return $this->module_id; |
| 179 |
} |
| 180 |
|
| 181 |
public static function get_filtered( array $params ) { |
| 182 |
global $wpdb; |
| 183 |
|
| 184 |
$orderby = 'id'; |
| 185 |
$direction = 'DESC'; |
| 186 |
$limit = RED_DEFAULT_PER_PAGE; |
| 187 |
$offset = 0; |
| 188 |
$where = ''; |
| 189 |
|
| 190 |
if ( isset( $params['orderby'] ) && in_array( $params['orderby'], array( 'name' ), true ) ) { |
| 191 |
$orderby = $params['orderby']; |
| 192 |
} |
| 193 |
|
| 194 |
if ( isset( $params['direction'] ) && in_array( $params['direction'], array( 'asc', 'desc' ), true ) ) { |
| 195 |
$direction = strtoupper( $params['direction'] ); |
| 196 |
} |
| 197 |
|
| 198 |
if ( isset( $params['filter'] ) && strlen( $params['filter'] ) > 0 ) { |
| 199 |
if ( isset( $params['filterBy'] ) && $params['filterBy'] === 'module' ) { |
| 200 |
$where = $wpdb->prepare( 'WHERE module_id=%d', intval( $params['filter'], 10 ) ); |
| 201 |
} else { |
| 202 |
$where = $wpdb->prepare( 'WHERE name LIKE %s', '%' . $wpdb->esc_like( trim( $params['filter'] ) ) . '%' ); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
if ( isset( $params['per_page'] ) ) { |
| 207 |
$limit = intval( $params['per_page'], 10 ); |
| 208 |
$limit = min( RED_MAX_PER_PAGE, $limit ); |
| 209 |
$limit = max( 5, $limit ); |
| 210 |
} |
| 211 |
|
| 212 |
if ( isset( $params['page'] ) ) { |
| 213 |
$offset = intval( $params['page'], 10 ); |
| 214 |
$offset = max( 0, $offset ); |
| 215 |
$offset *= $limit; |
| 216 |
} |
| 217 |
|
| 218 |
$rows = $wpdb->get_results( |
| 219 |
"SELECT * FROM {$wpdb->prefix}redirection_groups $where " . $wpdb->prepare( "ORDER BY $orderby $direction LIMIT %d,%d", $offset, $limit ) |
| 220 |
); |
| 221 |
$total_items = intval( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_groups " . $where ) ); |
| 222 |
$items = array(); |
| 223 |
|
| 224 |
$options = red_get_options(); |
| 225 |
|
| 226 |
foreach ( $rows as $row ) { |
| 227 |
$group = new Red_Group( $row ); |
| 228 |
$group_json = $group->to_json(); |
| 229 |
|
| 230 |
if ( $group->get_id() === $options['last_group_id'] ) { |
| 231 |
$group_json['default'] = true; |
| 232 |
} |
| 233 |
|
| 234 |
$items[] = $group_json; |
| 235 |
} |
| 236 |
|
| 237 |
return array( |
| 238 |
'items' => $items, |
| 239 |
'total' => intval( $total_items, 10 ), |
| 240 |
); |
| 241 |
} |
| 242 |
|
| 243 |
public function to_json() { |
| 244 |
$module = Red_Module::get( $this->get_module_id() ); |
| 245 |
|
| 246 |
return array( |
| 247 |
'id' => $this->get_id(), |
| 248 |
'name' => $this->get_name(), |
| 249 |
'redirects' => $this->get_total_redirects(), |
| 250 |
'module_id' => $this->get_module_id(), |
| 251 |
'moduleName' => $module ? $module->get_name() : '', |
| 252 |
'enabled' => $this->is_enabled(), |
| 253 |
); |
| 254 |
} |
| 255 |
} |
| 256 |
|