PluginProbe
Redirection / 5.0
Redirection v5.0
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 5.0, at models/group.php

385 lines 9.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 = $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 global $wpdb;
101
102 $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 ) );
103 if ( $row ) {
104 return new Red_Group( $row );
105 }
106
107 return false;
108 }
109
110 /**
111 * Get all groups
112 *
113 * @return Red_Group[]
114 */
115 public static function get_all( $params = [] ) {
116 global $wpdb;
117
118 $where = '';
119 if ( isset( $params['filterBy'] ) && is_array( $params['filterBy'] ) ) {
120 $filters = new Red_Group_Filters( $params['filterBy'] );
121 $where = $filters->get_as_sql();
122 }
123
124 $data = [];
125 $rows = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}redirection_groups $where" );
126
127 if ( $rows ) {
128 foreach ( $rows as $row ) {
129 $group = new Red_Group( $row );
130 $data[] = $group->to_json();
131 }
132 }
133
134 return $data;
135 }
136
137 public static function get_all_for_module( $module_id ) {
138 global $wpdb;
139
140 $data = array();
141 $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_groups WHERE module_id=%d", $module_id ) );
142
143 if ( $rows ) {
144 foreach ( $rows as $row ) {
145 $group = new Red_Group( $row );
146 $data[] = $group->to_json();
147 }
148 }
149
150 return $data;
151 }
152
153 public static function get_for_select() {
154 global $wpdb;
155
156 $data = array();
157 $rows = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}redirection_groups" );
158
159 if ( $rows ) {
160 foreach ( $rows as $row ) {
161 $module = Red_Module::get( $row->module_id );
162 if ( $module ) {
163 $data[ $module->get_name() ][ intval( $row->id, 10 ) ] = $row->name;
164 }
165 }
166 }
167
168 return $data;
169 }
170
171 public static function create( $name, $module_id, $enabled = true ) {
172 global $wpdb;
173
174 $name = trim( substr( $name, 0, 50 ) );
175 $module_id = intval( $module_id, 10 );
176
177 if ( $name !== '' && Red_Module::is_valid_id( $module_id ) ) {
178 $position = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( * ) FROM {$wpdb->prefix}redirection_groups WHERE module_id=%d", $module_id ) );
179
180 $data = array(
181 'name' => trim( $name ),
182 'module_id' => intval( $module_id ),
183 'position' => intval( $position ),
184 'status' => $enabled ? 'enabled' : 'disabled',
185 );
186
187 $wpdb->insert( $wpdb->prefix . 'redirection_groups', $data );
188
189 return Red_Group::get( $wpdb->insert_id );
190 }
191
192 return false;
193 }
194
195 public function update( $data ) {
196 global $wpdb;
197
198 $old_id = $this->module_id;
199 $this->name = trim( wp_kses( $data['name'], array() ) );
200
201 if ( Red_Module::is_valid_id( intval( $data['moduleId'], 10 ) ) ) {
202 $this->module_id = intval( $data['moduleId'], 10 );
203 }
204
205 $wpdb->update( $wpdb->prefix . 'redirection_groups', array( 'name' => $this->name, 'module_id' => $this->module_id ), array( 'id' => intval( $this->id ) ) );
206
207 if ( $old_id !== $this->module_id ) {
208 Red_Module::flush_by_module( $old_id );
209 Red_Module::flush_by_module( $this->module_id );
210 }
211
212 return true;
213 }
214
215 public function delete() {
216 global $wpdb;
217
218 // Delete all items in this group
219 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_items WHERE group_id=%d", $this->id ) );
220
221 Red_Module::flush( $this->id );
222
223 // Delete the group
224 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_groups WHERE id=%d", $this->id ) );
225
226 if ( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_groups" ) === 0 ) {
227 $wpdb->insert( $wpdb->prefix . 'redirection_groups', array( 'name' => __( 'Redirections', 'redirection' ), 'module_id' => 1, 'position' => 0 ) );
228 }
229 }
230
231 public function get_total_redirects() {
232 global $wpdb;
233
234 return intval( $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_items WHERE group_id=%d", $this->id ) ), 10 );
235 }
236
237 public function enable() {
238 global $wpdb;
239
240 $wpdb->update( $wpdb->prefix . 'redirection_groups', array( 'status' => 'enabled' ), array( 'id' => $this->id ) );
241 $wpdb->update( $wpdb->prefix . 'redirection_items', array( 'status' => 'enabled' ), array( 'group_id' => $this->id ) );
242
243 Red_Module::flush( $this->id );
244 }
245
246 public function disable() {
247 global $wpdb;
248
249 $wpdb->update( $wpdb->prefix . 'redirection_groups', array( 'status' => 'disabled' ), array( 'id' => $this->id ) );
250 $wpdb->update( $wpdb->prefix . 'redirection_items', array( 'status' => 'disabled' ), array( 'group_id' => $this->id ) );
251
252 Red_Module::flush( $this->id );
253 }
254
255 public function get_module_id() {
256 return $this->module_id;
257 }
258
259 public static function get_filtered( array $params ) {
260 global $wpdb;
261
262 $orderby = 'name';
263 $direction = 'DESC';
264 $limit = RED_DEFAULT_PER_PAGE;
265 $offset = 0;
266 $where = '';
267
268 if ( isset( $params['orderby'] ) && in_array( $params['orderby'], array( 'name', 'id' ), true ) ) {
269 $orderby = $params['orderby'];
270 }
271
272 if ( isset( $params['direction'] ) && in_array( $params['direction'], array( 'asc', 'desc' ), true ) ) {
273 $direction = strtoupper( $params['direction'] );
274 }
275
276 if ( isset( $params['filterBy'] ) && is_array( $params['filterBy'] ) ) {
277 $filters = new Red_Group_Filters( $params['filterBy'] );
278 $where = $filters->get_as_sql();
279 }
280
281 if ( isset( $params['per_page'] ) ) {
282 $limit = intval( $params['per_page'], 10 );
283 $limit = min( RED_MAX_PER_PAGE, $limit );
284 $limit = max( 5, $limit );
285 }
286
287 if ( isset( $params['page'] ) ) {
288 $offset = intval( $params['page'], 10 );
289 $offset = max( 0, $offset );
290 $offset *= $limit;
291 }
292
293 $rows = $wpdb->get_results(
294 "SELECT * FROM {$wpdb->prefix}redirection_groups $where " . $wpdb->prepare( "ORDER BY $orderby $direction LIMIT %d,%d", $offset, $limit )
295 );
296 $total_items = intval( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_groups " . $where ) );
297 $items = array();
298
299 $options = red_get_options();
300
301 foreach ( $rows as $row ) {
302 $group = new Red_Group( $row );
303 $group_json = $group->to_json();
304
305 if ( $group->get_id() === $options['last_group_id'] ) {
306 $group_json['default'] = true;
307 }
308
309 $items[] = $group_json;
310 }
311
312 return array(
313 'items' => $items,
314 'total' => intval( $total_items, 10 ),
315 );
316 }
317
318 public function to_json() {
319 $module = Red_Module::get( $this->get_module_id() );
320
321 return array(
322 'id' => $this->get_id(),
323 'name' => $this->get_name(),
324 'redirects' => $this->get_total_redirects(),
325 'module_id' => $this->get_module_id(),
326 'moduleName' => $module ? $module->get_name() : '',
327 'enabled' => $this->is_enabled(),
328 );
329 }
330
331 public static function delete_all( array $params ) {
332 global $wpdb;
333
334 $filters = new Red_Group_Filters( isset( $params['filterBy'] ) ? $params['filterBy'] : [] );
335 $query = $filters->get_as_sql();
336
337 $sql = "DELETE FROM {$wpdb->prefix}redirection_groups {$query}";
338
339 // phpcs:ignore
340 $wpdb->query( $sql );
341 }
342
343 public static function set_status_all( $action, array $params ) {
344 global $wpdb;
345
346 $filters = new Red_Group_Filters( isset( $params['filterBy'] ) ? $params['filterBy'] : [] );
347 $query = $filters->get_as_sql();
348
349 $sql = $wpdb->prepare( "UPDATE {$wpdb->prefix}redirection_groups SET status=%s {$query}", $action === 'enable' ? 'enable' : 'disable' );
350
351 // phpcs:ignore
352 $wpdb->query( $sql );
353 }
354 }
355
356 class Red_Group_Filters {
357 private $filters = [];
358
359 public function __construct( $filter_params ) {
360 global $wpdb;
361
362 foreach ( $filter_params as $filter_by => $filter ) {
363 if ( $filter_by === 'status' ) {
364 if ( $filter === 'enabled' ) {
365 $this->filters[] = "status='enabled'";
366 } else {
367 $this->filters[] = "status='disabled'";
368 }
369 } elseif ( $filter_by === 'module' ) {
370 $this->filters[] = $wpdb->prepare( 'module_id=%d', intval( $filter, 10 ) );
371 } elseif ( $filter_by === 'name' ) {
372 $this->filters[] = $wpdb->prepare( 'name LIKE %s', '%' . $wpdb->esc_like( trim( $filter ) ) . '%' );
373 }
374 }
375 }
376
377 public function get_as_sql() {
378 if ( count( $this->filters ) > 0 ) {
379 return ' WHERE ' . implode( ' AND ', $this->filters );
380 }
381
382 return '';
383 }
384 }
385