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

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