| @@ -2,19 +2,21 @@ | ||
| 2 | 2 | |
| 3 | 3 | class Red_Group { |
| 4 | 4 | private $items = 0; |
| 5 | 5 | private $name; |
| 6 | + private $tracking; | |
| 6 | 7 | private $module_id; |
| 7 | 8 | private $status; |
| 8 | 9 | private $position; |
| 9 | 10 | |
| 10 | - public function __construct( $values = '' ) { | |
| 11 | + public function __construct( $values = '' ) { | |
| 11 | 12 | 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 ); | |
| 13 | + foreach ( $values as $key => $value ) { | |
| 14 | + $this->$key = $value; | |
| 15 | + } | |
| 16 | + | |
| 17 | + $this->id = intval( $this->id, 10 ); | |
| 18 | + $this->module_id = intval( $this->module_id, 10 ); | |
| 17 | 19 | } |
| 18 | 20 | } |
| 19 | 21 | |
| 20 | 22 | public function get_name() { |
| @@ -32,12 +34,10 @@ | ||
| 32 | 34 | static function get( $id ) { |
| 33 | 35 | global $wpdb; |
| 34 | 36 | |
| 35 | 37 | $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 ) { | |
| 38 | + if ( $row ) | |
| 37 | 39 | return new Red_Group( $row ); |
| 38 | - } | |
| 39 | - | |
| 40 | 40 | return false; |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | static function get_all() { |
| @@ -89,12 +89,12 @@ | ||
| 89 | 89 | |
| 90 | 90 | return $data; |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | - static function create( $name, $module_id, $enabled = true ) { | |
| 93 | + static function create( $name, $module_id ) { | |
| 94 | 94 | global $wpdb; |
| 95 | 95 | |
| 96 | - $name = trim( substr( $name, 0, 50 ) ); | |
| 96 | + $name = trim( stripslashes( $name ) ); | |
| 97 | 97 | $module_id = intval( $module_id, 10 ); |
| 98 | 98 | |
| 99 | 99 | if ( $name !== '' && Red_Module::is_valid_id( $module_id ) ) { |
| 100 | 100 | $position = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( * ) FROM {$wpdb->prefix}redirection_groups WHERE module_id=%d", $module_id ) ); |
| @@ -102,12 +102,11 @@ | ||
| 102 | 102 | $data = array( |
| 103 | 103 | 'name' => trim( $name ), |
| 104 | 104 | 'module_id' => intval( $module_id ), |
| 105 | 105 | 'position' => intval( $position ), |
| 106 | - 'status' => $enabled ? 'enabled' : 'disabled', | |
| 107 | 106 | ); |
| 108 | 107 | |
| 109 | - $wpdb->insert( $wpdb->prefix . 'redirection_groups', $data ); | |
| 108 | + $wpdb->insert( $wpdb->prefix.'redirection_groups', $data ); | |
| 110 | 109 | |
| 111 | 110 | return Red_Group::get( $wpdb->insert_id ); |
| 112 | 111 | } |
| 113 | 112 | |
| @@ -117,15 +116,15 @@ | ||
| 117 | 116 | public function update( $data ) { |
| 118 | 117 | global $wpdb; |
| 119 | 118 | |
| 120 | 119 | $old_id = $this->module_id; |
| 121 | - $this->name = trim( wp_kses( $data['name'], array() ) ); | |
| 120 | + $this->name = trim( wp_kses( stripslashes( $data['name'] ), array() ) ); | |
| 122 | 121 | |
| 123 | 122 | if ( Red_Module::is_valid_id( intval( $data['moduleId'], 10 ) ) ) { |
| 124 | 123 | $this->module_id = intval( $data['moduleId'], 10 ); |
| 125 | 124 | } |
| 126 | 125 | |
| 127 | - $wpdb->update( $wpdb->prefix . 'redirection_groups', array( 'name' => $this->name, 'module_id' => $this->module_id ), array( 'id' => intval( $this->id ) ) ); | |
| 126 | + $wpdb->update( $wpdb->prefix.'redirection_groups', array( 'name' => $this->name, 'module_id' => $this->module_id ), array( 'id' => intval( $this->id ) ) ); | |
| 128 | 127 | |
| 129 | 128 | if ( $old_id !== $this->module_id ) { |
| 130 | 129 | Red_Module::flush_by_module( $old_id ); |
| 131 | 130 | Red_Module::flush_by_module( $this->module_id ); |
| @@ -144,11 +143,10 @@ | ||
| 144 | 143 | |
| 145 | 144 | // Delete the group |
| 146 | 145 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_groups WHERE id=%d", $this->id ) ); |
| 147 | 146 | |
| 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 | - } | |
| 147 | + if ( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_groups" ) === 0 ) | |
| 148 | + $wpdb->insert( $wpdb->prefix.'redirection_groups', array( 'name' => __( 'Redirections' ), 'module_id' => 1, 'position' => 0 ) ); | |
| 151 | 149 | } |
| 152 | 150 | |
| 153 | 151 | public function get_total_redirects() { |
| 154 | 152 | global $wpdb; |
| @@ -158,10 +156,10 @@ | ||
| 158 | 156 | |
| 159 | 157 | public function enable() { |
| 160 | 158 | global $wpdb; |
| 161 | 159 | |
| 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 ) ); | |
| 160 | + $wpdb->update( $wpdb->prefix.'redirection_groups', array( 'status' => 'enabled' ), array( 'id' => $this->id ) ); | |
| 161 | + $wpdb->update( $wpdb->prefix.'redirection_items', array( 'status' => 'enabled' ), array( 'group_id' => $this->id ) ); | |
| 164 | 162 | |
| 165 | 163 | Red_Module::flush( $this->id ); |
| 166 | 164 | } |
| 167 | 165 | |
| @@ -167,10 +165,10 @@ | ||
| 167 | 165 | |
| 168 | 166 | public function disable() { |
| 169 | 167 | global $wpdb; |
| 170 | 168 | |
| 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 ) ); | |
| 169 | + $wpdb->update( $wpdb->prefix.'redirection_groups', array( 'status' => 'disabled' ), array( 'id' => $this->id ) ); | |
| 170 | + $wpdb->update( $wpdb->prefix.'redirection_items', array( 'status' => 'disabled' ), array( 'group_id' => $this->id ) ); | |
| 173 | 171 | |
| 174 | 172 | Red_Module::flush( $this->id ); |
| 175 | 173 | } |
| 176 | 174 | |
| @@ -186,10 +184,10 @@ | ||
| 186 | 184 | $limit = RED_DEFAULT_PER_PAGE; |
| 187 | 185 | $offset = 0; |
| 188 | 186 | $where = ''; |
| 189 | 187 | |
| 190 | - if ( isset( $params['orderby'] ) && in_array( $params['orderby'], array( 'name' ), true ) ) { | |
| 191 | - $orderby = $params['orderby']; | |
| 188 | + if ( isset( $params['orderBy'] ) && in_array( $params['orderBy'], array( 'name' ), true ) ) { | |
| 189 | + $orderby = $params['orderBy']; | |
| 192 | 190 | } |
| 193 | 191 | |
| 194 | 192 | if ( isset( $params['direction'] ) && in_array( $params['direction'], array( 'asc', 'desc' ), true ) ) { |
| 195 | 193 | $direction = strtoupper( $params['direction'] ); |
| @@ -196,17 +194,17 @@ | ||
| 196 | 194 | } |
| 197 | 195 | |
| 198 | 196 | if ( isset( $params['filter'] ) && strlen( $params['filter'] ) > 0 ) { |
| 199 | 197 | if ( isset( $params['filterBy'] ) && $params['filterBy'] === 'module' ) { |
| 200 | - $where = $wpdb->prepare( 'WHERE module_id=%d', intval( $params['filter'], 10 ) ); | |
| 198 | + $where = $wpdb->prepare( "WHERE module_id=%d", intval( $params['filter'], 10 ) ); | |
| 201 | 199 | } else { |
| 202 | - $where = $wpdb->prepare( 'WHERE name LIKE %s', '%' . $wpdb->esc_like( trim( $params['filter'] ) ) . '%' ); | |
| 200 | + $where = $wpdb->prepare( 'WHERE name LIKE %s', '%'.$wpdb->esc_like( trim( $params['filter'] ) ).'%' ); | |
| 203 | 201 | } |
| 204 | 202 | } |
| 205 | 203 | |
| 206 | - if ( isset( $params['per_page'] ) ) { | |
| 207 | - $limit = intval( $params['per_page'], 10 ); | |
| 208 | - $limit = min( RED_MAX_PER_PAGE, $limit ); | |
| 204 | + if ( isset( $params['perPage'] ) ) { | |
| 205 | + $limit = intval( $params['perPage'], 10 ); | |
| 206 | + $limit = min( 100, $limit ); | |
| 209 | 207 | $limit = max( 5, $limit ); |
| 210 | 208 | } |
| 211 | 209 | |
| 212 | 210 | if ( isset( $params['page'] ) ) { |
| @@ -214,25 +212,18 @@ | ||
| 214 | 212 | $offset = max( 0, $offset ); |
| 215 | 213 | $offset *= $limit; |
| 216 | 214 | } |
| 217 | 215 | |
| 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 ) ); | |
| 216 | + $table = $wpdb->prefix.'redirection_groups'; | |
| 217 | + $sql = trim( "SELECT * FROM {$table} $where " ).$wpdb->prepare( " ORDER BY $orderby $direction LIMIT %d,%d", $offset, $limit ); | |
| 218 | + | |
| 219 | + $rows = $wpdb->get_results( $sql ); | |
| 220 | + $total_items = intval( $wpdb->get_var( "SELECT COUNT(*) FROM {$table} ".$where ) ); | |
| 222 | 221 | $items = array(); |
| 223 | 222 | |
| 224 | - $options = red_get_options(); | |
| 225 | - | |
| 226 | 223 | foreach ( $rows as $row ) { |
| 227 | 224 | $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; | |
| 225 | + $items[] = $group->to_json(); | |
| 235 | 226 | } |
| 236 | 227 | |
| 237 | 228 | return array( |
| 238 | 229 | 'items' => $items, |