| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* A group of redirects |
| 5 |
*/ |
| 6 |
class Red_Group { |
| 7 |
/** |
| 8 |
* Group ID |
| 9 |
* |
| 10 |
* @var integer |
| 11 |
*/ |
| 12 |
private $id = 0; |
| 13 |
|
| 14 |
/** |
| 15 |
* Group name |
| 16 |
* |
| 17 |
* @var String |
| 18 |
*/ |
| 19 |
private $name = ''; |
| 20 |
|
| 21 |
/** |
| 22 |
* Module ID |
| 23 |
* |
| 24 |
* @var integer |
| 25 |
*/ |
| 26 |
private $module_id = 0; |
| 27 |
|
| 28 |
/** |
| 29 |
* Group status - 'enabled' or 'disabled' |
| 30 |
* |
| 31 |
* @var String |
| 32 |
*/ |
| 33 |
private $status = 'enabled'; |
| 34 |
|
| 35 |
/** |
| 36 |
* Group position. Currently not used |
| 37 |
* |
| 38 |
* @var integer |
| 39 |
*/ |
| 40 |
private $position = 0; |
| 41 |
|
| 42 |
/** |
| 43 |
* Constructor |
| 44 |
* |
| 45 |
* @param string|Object $values Values. |
| 46 |
*/ |
| 47 |
public function __construct( $values = '' ) { |
| 48 |
if ( is_object( $values ) ) { |
| 49 |
$this->name = sanitize_text_field( $values->name ); |
| 50 |
$this->id = intval( $values->id, 10 ); |
| 51 |
|
| 52 |
if ( isset( $values->module_id ) ) { |
| 53 |
$this->module_id = intval( $values->module_id, 10 ); |
| 54 |
} |
| 55 |
|
| 56 |
if ( isset( $values->status ) ) { |
| 57 |
$this->status = $values->status; |
| 58 |
} |
| 59 |
|
| 60 |
if ( isset( $values->position ) ) { |
| 61 |
$this->position = intval( $values->position, 10 ); |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Get group name |
| 68 |
* |
| 69 |
* @return string |
| 70 |
*/ |
| 71 |
public function get_name() { |
| 72 |
return $this->name; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Get group ID |
| 77 |
* |
| 78 |
* @return integer |
| 79 |
*/ |
| 80 |
public function get_id() { |
| 81 |
return $this->id; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Is the group enabled or disabled? |
| 86 |
* |
| 87 |
* @return boolean |
| 88 |
*/ |
| 89 |
public function is_enabled() { |
| 90 |
return $this->status === 'enabled' ? true : false; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Get a group given an ID |
| 95 |
* |
| 96 |
* @param integer $id Group ID. |
| 97 |
* @return Red_Group|boolean |
| 98 |
*/ |
| 99 |
public static function get( $id ) { |
| 100 |
static $groups = []; |
| 101 |
global $wpdb; |
| 102 |
|
| 103 |
if ( isset( $groups[ $id ] ) ) { |
| 104 |
$row = $groups[ $id ]; |
| 105 |
} else { |
| 106 |
$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 ) ); |
| 107 |
} |
| 108 |
|
| 109 |
if ( $row ) { |
| 110 |
$groups[ $id ] = $row; |
| 111 |
return new Red_Group( $row ); |
| 112 |
} |
| 113 |
|
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Get all groups |
| 119 |
* |
| 120 |
* @return Red_Group[] |
| 121 |
*/ |
| 122 |
public static function get_all( $params = [] ) { |
| 123 |
global $wpdb; |
| 124 |
|
| 125 |
$where = ''; |
| 126 |
if ( isset( $params['filterBy'] ) && is_array( $params['filterBy'] ) ) { |
| 127 |
$filters = new Red_Group_Filters( $params['filterBy'] ); |
| 128 |
$where = $filters->get_as_sql(); |
| 129 |
} |
| 130 |
|
| 131 |
$data = []; |
| 132 |
$rows = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}redirection_groups $where" ); |
| 133 |
|
| 134 |
if ( $rows ) { |
| 135 |
foreach ( $rows as $row ) { |
| 136 |
$group = new Red_Group( $row ); |
| 137 |
$data[] = $group->to_json(); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
return $data; |
| 142 |
} |
| 143 |
|
| 144 |
public static function get_all_for_module( $module_id ) { |
| 145 |
global $wpdb; |
| 146 |
|
| 147 |
$data = array(); |
| 148 |
$rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_groups WHERE module_id=%d", $module_id ) ); |
| 149 |
|
| 150 |
if ( $rows ) { |
| 151 |
foreach ( $rows as $row ) { |
| 152 |
$group = new Red_Group( $row ); |
| 153 |
$data[] = $group->to_json(); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
return $data; |
| 158 |
} |
| 159 |
|
| 160 |
public static function get_for_select() { |
| 161 |
global $wpdb; |
| 162 |
|
| 163 |
$data = array(); |
| 164 |
$rows = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}redirection_groups" ); |
| 165 |
|
| 166 |
if ( $rows ) { |
| 167 |
foreach ( $rows as $row ) { |
| 168 |
$module = Red_Module::get( $row->module_id ); |
| 169 |
if ( $module ) { |
| 170 |
$data[ $module->get_name() ][ intval( $row->id, 10 ) ] = $row->name; |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
return $data; |
| 176 |
} |
| 177 |
|
| 178 |
public static function create( $name, $module_id, $enabled = true ) { |
| 179 |
global $wpdb; |
| 180 |
|
| 181 |
$name = trim( wp_kses( sanitize_text_field( $name ), 'strip' ) ); |
| 182 |
$name = substr( $name, 0, 50 ); |
| 183 |
$module_id = intval( $module_id, 10 ); |
| 184 |
|
| 185 |
if ( $name !== '' && Red_Module::is_valid_id( $module_id ) ) { |
| 186 |
$position = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( * ) FROM {$wpdb->prefix}redirection_groups WHERE module_id=%d", $module_id ) ); |
| 187 |
|
| 188 |
$data = array( |
| 189 |
'name' => trim( $name ), |
| 190 |
'module_id' => intval( $module_id ), |
| 191 |
'position' => intval( $position ), |
| 192 |
'status' => $enabled ? 'enabled' : 'disabled', |
| 193 |
); |
| 194 |
|
| 195 |
$wpdb->insert( $wpdb->prefix . 'redirection_groups', $data ); |
| 196 |
|
| 197 |
return Red_Group::get( $wpdb->insert_id ); |
| 198 |
} |
| 199 |
|
| 200 |
return false; |
| 201 |
} |
| 202 |
|
| 203 |
public function update( $data ) { |
| 204 |
global $wpdb; |
| 205 |
|
| 206 |
$old_id = $this->module_id; |
| 207 |
$this->name = trim( wp_kses( sanitize_text_field( $data['name'] ), 'strip' ) ); |
| 208 |
$this->name = substr( $this->name, 0, 50 ); |
| 209 |
|
| 210 |
if ( Red_Module::is_valid_id( intval( $data['moduleId'], 10 ) ) ) { |
| 211 |
$this->module_id = intval( $data['moduleId'], 10 ); |
| 212 |
} |
| 213 |
|
| 214 |
$wpdb->update( $wpdb->prefix . 'redirection_groups', array( 'name' => $this->name, 'module_id' => $this->module_id ), array( 'id' => intval( $this->id ) ) ); |
| 215 |
|
| 216 |
if ( $old_id !== $this->module_id ) { |
| 217 |
Red_Module::flush_by_module( $old_id ); |
| 218 |
Red_Module::flush_by_module( $this->module_id ); |
| 219 |
} |
| 220 |
|
| 221 |
return true; |
| 222 |
} |
| 223 |
|
| 224 |
public function delete() { |
| 225 |
global $wpdb; |
| 226 |
|
| 227 |
// Delete all items in this group |
| 228 |
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_items WHERE group_id=%d", $this->id ) ); |
| 229 |
|
| 230 |
Red_Module::flush( $this->id ); |
| 231 |
|
| 232 |
// Delete the group |
| 233 |
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_groups WHERE id=%d", $this->id ) ); |
| 234 |
|
| 235 |
if ( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_groups" ) === 0 ) { |
| 236 |
$wpdb->insert( $wpdb->prefix . 'redirection_groups', array( 'name' => __( 'Redirections', 'redirection' ), 'module_id' => 1, 'position' => 0 ) ); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
public function get_total_redirects() { |
| 241 |
global $wpdb; |
| 242 |
|
| 243 |
return intval( $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_items WHERE group_id=%d", $this->id ) ), 10 ); |
| 244 |
} |
| 245 |
|
| 246 |
public function enable() { |
| 247 |
global $wpdb; |
| 248 |
|
| 249 |
$wpdb->update( $wpdb->prefix . 'redirection_groups', array( 'status' => 'enabled' ), array( 'id' => $this->id ) ); |
| 250 |
$wpdb->update( $wpdb->prefix . 'redirection_items', array( 'status' => 'enabled' ), array( 'group_id' => $this->id ) ); |
| 251 |
|
| 252 |
Red_Module::flush( $this->id ); |
| 253 |
} |
| 254 |
|
| 255 |
public function disable() { |
| 256 |
global $wpdb; |
| 257 |
|
| 258 |
$wpdb->update( $wpdb->prefix . 'redirection_groups', array( 'status' => 'disabled' ), array( 'id' => $this->id ) ); |
| 259 |
$wpdb->update( $wpdb->prefix . 'redirection_items', array( 'status' => 'disabled' ), array( 'group_id' => $this->id ) ); |
| 260 |
|
| 261 |
Red_Module::flush( $this->id ); |
| 262 |
} |
| 263 |
|
| 264 |
public function get_module_id() { |
| 265 |
return $this->module_id; |
| 266 |
} |
| 267 |
|
| 268 |
public static function get_filtered( array $params ) { |
| 269 |
global $wpdb; |
| 270 |
|
| 271 |
$orderby = 'name'; |
| 272 |
$direction = 'DESC'; |
| 273 |
$limit = RED_DEFAULT_PER_PAGE; |
| 274 |
$offset = 0; |
| 275 |
$where = ''; |
| 276 |
|
| 277 |
if ( isset( $params['orderby'] ) && in_array( $params['orderby'], array( 'name', 'id' ), true ) ) { |
| 278 |
$orderby = $params['orderby']; |
| 279 |
} |
| 280 |
|
| 281 |
if ( isset( $params['direction'] ) && in_array( $params['direction'], array( 'asc', 'desc' ), true ) ) { |
| 282 |
$direction = strtoupper( $params['direction'] ); |
| 283 |
} |
| 284 |
|
| 285 |
if ( isset( $params['filterBy'] ) && is_array( $params['filterBy'] ) ) { |
| 286 |
$filters = new Red_Group_Filters( $params['filterBy'] ); |
| 287 |
$where = $filters->get_as_sql(); |
| 288 |
} |
| 289 |
|
| 290 |
if ( isset( $params['per_page'] ) ) { |
| 291 |
$limit = intval( $params['per_page'], 10 ); |
| 292 |
$limit = min( RED_MAX_PER_PAGE, $limit ); |
| 293 |
$limit = max( 5, $limit ); |
| 294 |
} |
| 295 |
|
| 296 |
if ( isset( $params['page'] ) ) { |
| 297 |
$offset = intval( $params['page'], 10 ); |
| 298 |
$offset = max( 0, $offset ); |
| 299 |
$offset *= $limit; |
| 300 |
} |
| 301 |
|
| 302 |
$rows = $wpdb->get_results( |
| 303 |
"SELECT * FROM {$wpdb->prefix}redirection_groups $where " . $wpdb->prepare( "ORDER BY $orderby $direction LIMIT %d,%d", $offset, $limit ) |
| 304 |
); |
| 305 |
$total_items = intval( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_groups " . $where ) ); |
| 306 |
$items = array(); |
| 307 |
|
| 308 |
$options = red_get_options(); |
| 309 |
|
| 310 |
foreach ( $rows as $row ) { |
| 311 |
$group = new Red_Group( $row ); |
| 312 |
$group_json = $group->to_json(); |
| 313 |
|
| 314 |
if ( $group->get_id() === $options['last_group_id'] ) { |
| 315 |
$group_json['default'] = true; |
| 316 |
} |
| 317 |
|
| 318 |
$items[] = $group_json; |
| 319 |
} |
| 320 |
|
| 321 |
return array( |
| 322 |
'items' => $items, |
| 323 |
'total' => intval( $total_items, 10 ), |
| 324 |
); |
| 325 |
} |
| 326 |
|
| 327 |
public function to_json() { |
| 328 |
$module = Red_Module::get( $this->get_module_id() ); |
| 329 |
|
| 330 |
return array( |
| 331 |
'id' => $this->get_id(), |
| 332 |
'name' => $this->get_name(), |
| 333 |
'redirects' => $this->get_total_redirects(), |
| 334 |
'module_id' => $this->get_module_id(), |
| 335 |
'moduleName' => $module ? $module->get_name() : '', |
| 336 |
'enabled' => $this->is_enabled(), |
| 337 |
); |
| 338 |
} |
| 339 |
|
| 340 |
public static function delete_all( array $params ) { |
| 341 |
global $wpdb; |
| 342 |
|
| 343 |
$filters = new Red_Group_Filters( isset( $params['filterBy'] ) ? $params['filterBy'] : [] ); |
| 344 |
$query = $filters->get_as_sql(); |
| 345 |
|
| 346 |
$sql = "DELETE FROM {$wpdb->prefix}redirection_groups {$query}"; |
| 347 |
|
| 348 |
// phpcs:ignore |
| 349 |
$wpdb->query( $sql ); |
| 350 |
} |
| 351 |
|
| 352 |
public static function set_status_all( $action, array $params ) { |
| 353 |
global $wpdb; |
| 354 |
|
| 355 |
$filters = new Red_Group_Filters( isset( $params['filterBy'] ) ? $params['filterBy'] : [] ); |
| 356 |
$query = $filters->get_as_sql(); |
| 357 |
|
| 358 |
$sql = $wpdb->prepare( "UPDATE {$wpdb->prefix}redirection_groups SET status=%s {$query}", $action === 'enable' ? 'enable' : 'disable' ); |
| 359 |
|
| 360 |
// phpcs:ignore |
| 361 |
$wpdb->query( $sql ); |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
class Red_Group_Filters { |
| 366 |
private $filters = []; |
| 367 |
|
| 368 |
public function __construct( $filter_params ) { |
| 369 |
global $wpdb; |
| 370 |
|
| 371 |
foreach ( $filter_params as $filter_by => $filter ) { |
| 372 |
$filter_by = sanitize_text_field( $filter_by ); |
| 373 |
$filter = sanitize_text_field( $filter ); |
| 374 |
|
| 375 |
if ( $filter_by === 'status' ) { |
| 376 |
if ( $filter === 'enabled' ) { |
| 377 |
$this->filters[] = "status='enabled'"; |
| 378 |
} else { |
| 379 |
$this->filters[] = "status='disabled'"; |
| 380 |
} |
| 381 |
} elseif ( $filter_by === 'module' ) { |
| 382 |
$this->filters[] = $wpdb->prepare( 'module_id=%d', intval( $filter, 10 ) ); |
| 383 |
} elseif ( $filter_by === 'name' ) { |
| 384 |
$this->filters[] = $wpdb->prepare( 'name LIKE %s', '%' . $wpdb->esc_like( trim( $filter ) ) . '%' ); |
| 385 |
} |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
public function get_as_sql() { |
| 390 |
if ( count( $this->filters ) > 0 ) { |
| 391 |
return ' WHERE ' . implode( ' AND ', $this->filters ); |
| 392 |
} |
| 393 |
|
| 394 |
return ''; |
| 395 |
} |
| 396 |
} |
| 397 |
|