PluginProbe
Redirection / 5.3.2
Redirection v5.3.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.3.2, at models/group.php

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