PluginProbe
Redirection / 4.8
Redirection v4.8
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 4.8, at models/group.php

283 lines 7.7 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 $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 = 'name';
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['filterBy'] ) && is_array( $params['filterBy'] ) ) {
199 $filters = new Red_Group_Filters( $params['filterBy'] );
200 $where = $filters->get_as_sql();
201 }
202
203 if ( isset( $params['per_page'] ) ) {
204 $limit = intval( $params['per_page'], 10 );
205 $limit = min( RED_MAX_PER_PAGE, $limit );
206 $limit = max( 5, $limit );
207 }
208
209 if ( isset( $params['page'] ) ) {
210 $offset = intval( $params['page'], 10 );
211 $offset = max( 0, $offset );
212 $offset *= $limit;
213 }
214
215 $rows = $wpdb->get_results(
216 "SELECT * FROM {$wpdb->prefix}redirection_groups $where " . $wpdb->prepare( "ORDER BY $orderby $direction LIMIT %d,%d", $offset, $limit )
217 );
218 $total_items = intval( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_groups " . $where ) );
219 $items = array();
220
221 $options = red_get_options();
222
223 foreach ( $rows as $row ) {
224 $group = new Red_Group( $row );
225 $group_json = $group->to_json();
226
227 if ( $group->get_id() === $options['last_group_id'] ) {
228 $group_json['default'] = true;
229 }
230
231 $items[] = $group_json;
232 }
233
234 return array(
235 'items' => $items,
236 'total' => intval( $total_items, 10 ),
237 );
238 }
239
240 public function to_json() {
241 $module = Red_Module::get( $this->get_module_id() );
242
243 return array(
244 'id' => $this->get_id(),
245 'name' => $this->get_name(),
246 'redirects' => $this->get_total_redirects(),
247 'module_id' => $this->get_module_id(),
248 'moduleName' => $module ? $module->get_name() : '',
249 'enabled' => $this->is_enabled(),
250 );
251 }
252 }
253
254 class Red_Group_Filters {
255 private $filters = [];
256
257 public function __construct( $filter_params ) {
258 global $wpdb;
259
260 foreach ( $filter_params as $filter_by => $filter ) {
261 if ( $filter_by === 'status' ) {
262 if ( $filter === 'enabled' ) {
263 $this->filters[] = "status='enabled'";
264 } else {
265 $this->filters[] = "status='disabled'";
266 }
267 } elseif ( $filter_by === 'module' ) {
268 $this->filters[] = $wpdb->prepare( 'module_id=%d', intval( $filter, 10 ) );
269 } elseif ( $filter_by === 'name' ) {
270 $this->filters[] = $wpdb->prepare( 'name LIKE %s', '%' . $wpdb->esc_like( trim( $filter ) ) . '%' );
271 }
272 }
273 }
274
275 public function get_as_sql() {
276 if ( count( $this->filters ) > 0 ) {
277 return ' WHERE ' . implode( ' AND ', $this->filters );
278 }
279
280 return '';
281 }
282 }
283