PluginProbe
Redirection / 3.3
Redirection v3.3
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / models / group.php

group.php in Redirection 3.3, at models/group.php

257 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $this->id = intval( $this->id, 10 );
18 $this->module_id = intval( $this->module_id, 10 );
19 }
20 }
21
22 public function get_name() {
23 return $this->name;
24 }
25
26 public function get_id() {
27 return $this->id;
28 }
29
30 public function is_enabled() {
31 return $this->status === 'enabled' ? true : false;
32 }
33
34 static function get( $id ) {
35 global $wpdb;
36
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 ) );
38 if ( $row ) {
39 return new Red_Group( $row );
40 }
41
42 return false;
43 }
44
45 static function get_all() {
46 global $wpdb;
47
48 $data = array();
49 $rows = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}redirection_groups" );
50
51 if ( $rows ) {
52 foreach ( $rows as $row ) {
53 $group = new Red_Group( $row );
54 $data[] = $group->to_json();
55 }
56 }
57
58 return $data;
59 }
60
61 static function get_all_for_module( $module_id ) {
62 global $wpdb;
63
64 $data = array();
65 $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_groups WHERE module_id=%d", $module_id ) );
66
67 if ( $rows ) {
68 foreach ( $rows as $row ) {
69 $group = new Red_Group( $row );
70 $data[] = $group->to_json();
71 }
72 }
73
74 return $data;
75 }
76
77 static function get_for_select() {
78 global $wpdb;
79
80 $data = array();
81 $rows = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}redirection_groups" );
82
83 if ( $rows ) {
84 foreach ( $rows as $row ) {
85 $module = Red_Module::get( $row->module_id );
86 if ( $module ) {
87 $data[ $module->get_name() ][ intval( $row->id, 10 ) ] = $row->name;
88 }
89 }
90 }
91
92 return $data;
93 }
94
95 static function create( $name, $module_id ) {
96 global $wpdb;
97
98 $name = trim( substr( $name, 0, 50 ) );
99 $module_id = intval( $module_id, 10 );
100
101 if ( $name !== '' && Red_Module::is_valid_id( $module_id ) ) {
102 $position = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( * ) FROM {$wpdb->prefix}redirection_groups WHERE module_id=%d", $module_id ) );
103
104 $data = array(
105 'name' => trim( $name ),
106 'module_id' => intval( $module_id ),
107 'position' => intval( $position ),
108 );
109
110 $wpdb->insert( $wpdb->prefix.'redirection_groups', $data );
111
112 return Red_Group::get( $wpdb->insert_id );
113 }
114
115 return false;
116 }
117
118 public function update( $data ) {
119 global $wpdb;
120
121 $old_id = $this->module_id;
122 $this->name = trim( wp_kses( $data['name'], array() ) );
123
124 if ( Red_Module::is_valid_id( intval( $data['moduleId'], 10 ) ) ) {
125 $this->module_id = intval( $data['moduleId'], 10 );
126 }
127
128 $wpdb->update( $wpdb->prefix.'redirection_groups', array( 'name' => $this->name, 'module_id' => $this->module_id ), array( 'id' => intval( $this->id ) ) );
129
130 if ( $old_id !== $this->module_id ) {
131 Red_Module::flush_by_module( $old_id );
132 Red_Module::flush_by_module( $this->module_id );
133 }
134
135 return true;
136 }
137
138 public function delete() {
139 global $wpdb;
140
141 // Delete all items in this group
142 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_items WHERE group_id=%d", $this->id ) );
143
144 Red_Module::flush( $this->id );
145
146 // Delete the group
147 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_groups WHERE id=%d", $this->id ) );
148
149 if ( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_groups" ) === 0 )
150 $wpdb->insert( $wpdb->prefix.'redirection_groups', array( 'name' => __( 'Redirections' ), 'module_id' => 1, 'position' => 0 ) );
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 $table = $wpdb->prefix.'redirection_groups';
219 $sql = trim( "SELECT * FROM {$table} $where " ).$wpdb->prepare( " ORDER BY $orderby $direction LIMIT %d,%d", $offset, $limit );
220
221 $rows = $wpdb->get_results( $sql );
222 $total_items = intval( $wpdb->get_var( "SELECT COUNT(*) FROM {$table} ".$where ) );
223 $items = array();
224
225 $options = red_get_options();
226
227 foreach ( $rows as $row ) {
228 $group = new Red_Group( $row );
229 $group_json = $group->to_json();
230
231 if ( $group->get_id() === $options['last_group_id'] ) {
232 $group_json['default'] = true;
233 }
234
235 $items[] = $group_json;
236 }
237
238 return array(
239 'items' => $items,
240 'total' => intval( $total_items, 10 ),
241 );
242 }
243
244 public function to_json() {
245 $module = Red_Module::get( $this->get_module_id() );
246
247 return array(
248 'id' => $this->get_id(),
249 'name' => $this->get_name(),
250 'redirects' => $this->get_total_redirects(),
251 'module_id' => $this->get_module_id(),
252 'moduleName' => $module ? $module->get_name() : '',
253 'enabled' => $this->is_enabled(),
254 );
255 }
256 }
257