| 1 |
<?php |
| 2 |
|
| 3 |
class Red_Group { |
| 4 |
private $items = 0; |
| 5 |
private $name; |
| 6 |
private $tracking; |
| 7 |
private $module_id; |
| 8 |
private $status; |
| 9 |
private $position; |
| 10 |
|
| 11 |
public function __construct( $values = '' ) { |
| 12 |
if ( is_object( $values ) ) { |
| 13 |
foreach ( $values AS $key => $value ) { |
| 14 |
$this->$key = $value; |
| 15 |
} |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
public function get_name() { |
| 20 |
return $this->name; |
| 21 |
} |
| 22 |
|
| 23 |
public function get_id() { |
| 24 |
return $this->id; |
| 25 |
} |
| 26 |
|
| 27 |
public function is_enabled() { |
| 28 |
return $this->status === 'enabled' ? true : false; |
| 29 |
} |
| 30 |
|
| 31 |
static function get( $id ) { |
| 32 |
global $wpdb; |
| 33 |
|
| 34 |
$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 ) ); |
| 35 |
if ( $row ) |
| 36 |
return new Red_Group( $row ); |
| 37 |
return false; |
| 38 |
} |
| 39 |
|
| 40 |
static function get_for_select() { |
| 41 |
global $wpdb; |
| 42 |
|
| 43 |
$data = array(); |
| 44 |
$rows = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}redirection_groups" ); |
| 45 |
|
| 46 |
if ( $rows ) { |
| 47 |
foreach ( $rows AS $row ) { |
| 48 |
$module = Red_Module::get( $row->module_id ); |
| 49 |
if ( $module ) { |
| 50 |
$data[$module->get_name()][$row->id] = $row->name; |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
return $data; |
| 56 |
} |
| 57 |
|
| 58 |
static function create( $name, $module_id ) { |
| 59 |
global $wpdb; |
| 60 |
|
| 61 |
$name = trim( $name ); |
| 62 |
|
| 63 |
if ( $name !== '' && $module_id > 0 ) { |
| 64 |
$position = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( * ) FROM {$wpdb->prefix}redirection_groups WHERE module_id=%d", $module_id ) ); |
| 65 |
|
| 66 |
$data = array( |
| 67 |
'name' => trim( $name ), |
| 68 |
'module_id' => intval( $module_id ), |
| 69 |
'position' => intval( $position ) |
| 70 |
); |
| 71 |
|
| 72 |
$wpdb->insert( $wpdb->prefix.'redirection_groups', $data ); |
| 73 |
|
| 74 |
return Red_Group::get( $wpdb->insert_id ); |
| 75 |
} |
| 76 |
|
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
public function update( $data ) { |
| 81 |
global $wpdb; |
| 82 |
|
| 83 |
$old_id = $this->module_id; |
| 84 |
$this->name = trim( wp_kses( stripslashes( $data['name'] ), array() ) ); |
| 85 |
|
| 86 |
if ( Red_Module::is_valid_id( intval( $data['module_id'] ) ) ) |
| 87 |
$this->module_id = intval( $data['module_id'] ); |
| 88 |
|
| 89 |
$wpdb->update( $wpdb->prefix.'redirection_groups', array( 'name' => $this->name, 'module_id' => $this->module_id ), array( 'id' => intval( $this->id ) ) ); |
| 90 |
|
| 91 |
if ( $old_id !== $this->module_id ) { |
| 92 |
Red_Module::flush_by_module( $old_id ); |
| 93 |
Red_Module::flush_by_module( $this->module_id ); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
public function delete() { |
| 98 |
global $wpdb; |
| 99 |
|
| 100 |
// Delete all items in this group |
| 101 |
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_items WHERE group_id=%d", $this->id ) ); |
| 102 |
|
| 103 |
Red_Module::flush( $this->id ); |
| 104 |
|
| 105 |
// Delete the group |
| 106 |
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_groups WHERE id=%d", $this->id ) ); |
| 107 |
|
| 108 |
if ( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_groups" ) == 0 ) |
| 109 |
$wpdb->insert( $wpdb->prefix.'redirection_groups', array( 'name' => __( 'Redirections' ), 'module_id' => 1, 'position' => 0 ) ); |
| 110 |
} |
| 111 |
|
| 112 |
public function get_total_redirects() { |
| 113 |
global $wpdb; |
| 114 |
|
| 115 |
return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_items WHERE group_id=%d", $this->id ) ); |
| 116 |
} |
| 117 |
|
| 118 |
public function enable() { |
| 119 |
global $wpdb; |
| 120 |
|
| 121 |
$wpdb->update( $wpdb->prefix.'redirection_groups', array( 'status' => 'enabled' ), array( 'id' => $this->id ) ); |
| 122 |
$wpdb->update( $wpdb->prefix.'redirection_items', array( 'status' => 'enabled' ), array( 'group_id' => $this->id ) ); |
| 123 |
|
| 124 |
Red_Module::flush( $this->id ); |
| 125 |
} |
| 126 |
|
| 127 |
public function disable() { |
| 128 |
global $wpdb; |
| 129 |
|
| 130 |
$wpdb->update( $wpdb->prefix.'redirection_groups', array( 'status' => 'disabled' ), array( 'id' => $this->id ) ); |
| 131 |
$wpdb->update( $wpdb->prefix.'redirection_items', array( 'status' => 'disabled' ), array( 'group_id' => $this->id ) ); |
| 132 |
|
| 133 |
Red_Module::flush( $this->id ); |
| 134 |
} |
| 135 |
|
| 136 |
public function get_module_id() { |
| 137 |
return $this->module_id; |
| 138 |
} |
| 139 |
} |
| 140 |
|