PluginProbe
Redirection / 2.7.1
Redirection v2.7.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 2.7.1, at models/group.php

247 lines 6.8 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 $tracking;
7 private $module_id;
8 private $status;
9 private $position;
10
11 public function __construct( $values = '' ) {
12 if ( is_object( $values ) ) {
13 foreach ( $values as $key => $value ) {
14 $this->$key = $value;
15 }
16
17 $this->id = intval( $this->id, 10 );
18 $this->module_id = intval( $this->module_id, 10 );
19 }
20 }
21
22 public function get_name() {
23 return $this->name;
24 }
25
26 public function get_id() {
27 return $this->id;
28 }
29
30 public function is_enabled() {
31 return $this->status === 'enabled' ? true : false;
32 }
33
34 static function get( $id ) {
35 global $wpdb;
36
37 $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 ) );
38 if ( $row )
39 return new Red_Group( $row );
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( stripslashes( $name ) );
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( stripslashes( $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 public function get_total_redirects() {
152 global $wpdb;
153
154 return intval( $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_items WHERE group_id=%d", $this->id ) ), 10 );
155 }
156
157 public function enable() {
158 global $wpdb;
159
160 $wpdb->update( $wpdb->prefix.'redirection_groups', array( 'status' => 'enabled' ), array( 'id' => $this->id ) );
161 $wpdb->update( $wpdb->prefix.'redirection_items', array( 'status' => 'enabled' ), array( 'group_id' => $this->id ) );
162
163 Red_Module::flush( $this->id );
164 }
165
166 public function disable() {
167 global $wpdb;
168
169 $wpdb->update( $wpdb->prefix.'redirection_groups', array( 'status' => 'disabled' ), array( 'id' => $this->id ) );
170 $wpdb->update( $wpdb->prefix.'redirection_items', array( 'status' => 'disabled' ), array( 'group_id' => $this->id ) );
171
172 Red_Module::flush( $this->id );
173 }
174
175 public function get_module_id() {
176 return $this->module_id;
177 }
178
179 public static function get_filtered( array $params ) {
180 global $wpdb;
181
182 $orderby = 'id';
183 $direction = 'DESC';
184 $limit = RED_DEFAULT_PER_PAGE;
185 $offset = 0;
186 $where = '';
187
188 if ( isset( $params['orderBy'] ) && in_array( $params['orderBy'], array( 'name' ), true ) ) {
189 $orderby = $params['orderBy'];
190 }
191
192 if ( isset( $params['direction'] ) && in_array( $params['direction'], array( 'asc', 'desc' ), true ) ) {
193 $direction = strtoupper( $params['direction'] );
194 }
195
196 if ( isset( $params['filter'] ) && strlen( $params['filter'] ) > 0 ) {
197 if ( isset( $params['filterBy'] ) && $params['filterBy'] === 'module' ) {
198 $where = $wpdb->prepare( "WHERE module_id=%d", intval( $params['filter'], 10 ) );
199 } else {
200 $where = $wpdb->prepare( 'WHERE name LIKE %s', '%'.$wpdb->esc_like( trim( $params['filter'] ) ).'%' );
201 }
202 }
203
204 if ( isset( $params['perPage'] ) ) {
205 $limit = intval( $params['perPage'], 10 );
206 $limit = min( 100, $limit );
207 $limit = max( 5, $limit );
208 }
209
210 if ( isset( $params['page'] ) ) {
211 $offset = intval( $params['page'], 10 );
212 $offset = max( 0, $offset );
213 $offset *= $limit;
214 }
215
216 $table = $wpdb->prefix.'redirection_groups';
217 $sql = trim( "SELECT * FROM {$table} $where " ).$wpdb->prepare( " ORDER BY $orderby $direction LIMIT %d,%d", $offset, $limit );
218
219 $rows = $wpdb->get_results( $sql );
220 $total_items = intval( $wpdb->get_var( "SELECT COUNT(*) FROM {$table} ".$where ) );
221 $items = array();
222
223 foreach ( $rows as $row ) {
224 $group = new Red_Group( $row );
225 $items[] = $group->to_json();
226 }
227
228 return array(
229 'items' => $items,
230 'total' => intval( $total_items, 10 ),
231 );
232 }
233
234 public function to_json() {
235 $module = Red_Module::get( $this->get_module_id() );
236
237 return array(
238 'id' => $this->get_id(),
239 'name' => $this->get_name(),
240 'redirects' => $this->get_total_redirects(),
241 'module_id' => $this->get_module_id(),
242 'moduleName' => $module ? $module->get_name() : '',
243 'enabled' => $this->is_enabled(),
244 );
245 }
246 }
247