PluginProbe
Redirection / 3.6.1
Redirection v3.6.1
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.6.1, at models/group.php

255 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 $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 ) {
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 );
107
108 $wpdb->insert( $wpdb->prefix . 'redirection_groups', $data );
109
110 return Red_Group::get( $wpdb->insert_id );
111 }
112
113 return false;
114 }
115
116 public function update( $data ) {
117 global $wpdb;
118
119 $old_id = $this->module_id;
120 $this->name = trim( wp_kses( $data['name'], array() ) );
121
122 if ( Red_Module::is_valid_id( intval( $data['moduleId'], 10 ) ) ) {
123 $this->module_id = intval( $data['moduleId'], 10 );
124 }
125
126 $wpdb->update( $wpdb->prefix . 'redirection_groups', array( 'name' => $this->name, 'module_id' => $this->module_id ), array( 'id' => intval( $this->id ) ) );
127
128 if ( $old_id !== $this->module_id ) {
129 Red_Module::flush_by_module( $old_id );
130 Red_Module::flush_by_module( $this->module_id );
131 }
132
133 return true;
134 }
135
136 public function delete() {
137 global $wpdb;
138
139 // Delete all items in this group
140 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_items WHERE group_id=%d", $this->id ) );
141
142 Red_Module::flush( $this->id );
143
144 // Delete the group
145 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_groups WHERE id=%d", $this->id ) );
146
147 if ( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_groups" ) === 0 ) {
148 $wpdb->insert( $wpdb->prefix . 'redirection_groups', array( 'name' => __( 'Redirections' ), 'module_id' => 1, 'position' => 0 ) );
149 }
150 }
151
152 public function get_total_redirects() {
153 global $wpdb;
154
155 return intval( $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_items WHERE group_id=%d", $this->id ) ), 10 );
156 }
157
158 public function enable() {
159 global $wpdb;
160
161 $wpdb->update( $wpdb->prefix . 'redirection_groups', array( 'status' => 'enabled' ), array( 'id' => $this->id ) );
162 $wpdb->update( $wpdb->prefix . 'redirection_items', array( 'status' => 'enabled' ), array( 'group_id' => $this->id ) );
163
164 Red_Module::flush( $this->id );
165 }
166
167 public function disable() {
168 global $wpdb;
169
170 $wpdb->update( $wpdb->prefix . 'redirection_groups', array( 'status' => 'disabled' ), array( 'id' => $this->id ) );
171 $wpdb->update( $wpdb->prefix . 'redirection_items', array( 'status' => 'disabled' ), array( 'group_id' => $this->id ) );
172
173 Red_Module::flush( $this->id );
174 }
175
176 public function get_module_id() {
177 return $this->module_id;
178 }
179
180 public static function get_filtered( array $params ) {
181 global $wpdb;
182
183 $orderby = 'id';
184 $direction = 'DESC';
185 $limit = RED_DEFAULT_PER_PAGE;
186 $offset = 0;
187 $where = '';
188
189 if ( isset( $params['orderby'] ) && in_array( $params['orderby'], array( 'name' ), true ) ) {
190 $orderby = $params['orderby'];
191 }
192
193 if ( isset( $params['direction'] ) && in_array( $params['direction'], array( 'asc', 'desc' ), true ) ) {
194 $direction = strtoupper( $params['direction'] );
195 }
196
197 if ( isset( $params['filter'] ) && strlen( $params['filter'] ) > 0 ) {
198 if ( isset( $params['filterBy'] ) && $params['filterBy'] === 'module' ) {
199 $where = $wpdb->prepare( 'WHERE module_id=%d', intval( $params['filter'], 10 ) );
200 } else {
201 $where = $wpdb->prepare( 'WHERE name LIKE %s', '%' . $wpdb->esc_like( trim( $params['filter'] ) ) . '%' );
202 }
203 }
204
205 if ( isset( $params['per_page'] ) ) {
206 $limit = intval( $params['per_page'], 10 );
207 $limit = min( RED_MAX_PER_PAGE, $limit );
208 $limit = max( 5, $limit );
209 }
210
211 if ( isset( $params['page'] ) ) {
212 $offset = intval( $params['page'], 10 );
213 $offset = max( 0, $offset );
214 $offset *= $limit;
215 }
216
217 $rows = $wpdb->get_results(
218 "SELECT * FROM {$wpdb->prefix}redirection_groups $where " . $wpdb->prepare( "ORDER BY $orderby $direction LIMIT %d,%d", $offset, $limit )
219 );
220 $total_items = intval( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_groups " . $where ) );
221 $items = array();
222
223 $options = red_get_options();
224
225 foreach ( $rows as $row ) {
226 $group = new Red_Group( $row );
227 $group_json = $group->to_json();
228
229 if ( $group->get_id() === $options['last_group_id'] ) {
230 $group_json['default'] = true;
231 }
232
233 $items[] = $group_json;
234 }
235
236 return array(
237 'items' => $items,
238 'total' => intval( $total_items, 10 ),
239 );
240 }
241
242 public function to_json() {
243 $module = Red_Module::get( $this->get_module_id() );
244
245 return array(
246 'id' => $this->get_id(),
247 'name' => $this->get_name(),
248 'redirects' => $this->get_total_redirects(),
249 'module_id' => $this->get_module_id(),
250 'moduleName' => $module ? $module->get_name() : '',
251 'enabled' => $this->is_enabled(),
252 );
253 }
254 }
255