PluginProbe
Redirection / 2.3.5
Redirection v2.3.5
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 / module.php

module.php in Redirection 2.3.5, at models/module.php

268 lines 6.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_Module {
4 var $id;
5
6 function Red_Module( $values = '' ) {
7 if ( is_object( $values ) ) {
8 foreach ( $values AS $key => $value ) {
9 $this->$key = $value;
10 }
11
12 if ( $this->options )
13 $this->load( unserialize( $this->options ) );
14 }
15 }
16
17 function module_flush( $items ) {
18 }
19
20 function module_flush_delete() {
21 }
22
23 static function flush( $id ) {
24 $module = self::get( $id );
25 if ( $module && $module->is_valid() )
26 $module->module_flush( Red_Item::get_all_for_module( $id ) );
27 }
28
29 static function flush_delete( $id ) {
30 $module = self::get( $id );
31 if ( $module )
32 $module->module_flush_delete();
33 }
34
35 function update( $data ) {
36 global $wpdb;
37
38 $this->name = $data['name'];
39 $options = $this->save( $data );
40 $wpdb->update( $wpdb->prefix.'redirection_modules', array( 'name' => trim( $data['name'] ), 'options' => empty( $options ) ? '' : serialize( $options ) ), array( 'id' => intval( $this->id ) ) );
41
42 self::clear_cache( $this->id );
43 }
44
45 function delete() {
46 global $wpdb;
47
48 $groups = Red_Group::get_for_module( $this->id );
49 if ( count( $groups ) > 0 ) {
50 foreach ( $groups AS $group ) {
51 $group->delete( $group->id );
52 }
53 }
54
55 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_modules WHERE id=%d", $this->id ) );
56
57 RE_Log::delete_for_module( $this->id );
58 self::clear_cache( $this->id );
59 self::flush_delete( $this->id );
60 }
61
62 static function clear_cache( $module ) {
63 delete_option( 'redirection_module_cache' );
64 self::flush( $module );
65 }
66
67 function create( $data ) {
68 global $wpdb;
69
70 if ( strlen( $data['name'] ) > 0 ) {
71 $db = array(
72 'name' => trim( $data['name'] ),
73 'type' => $data['type'],
74 );
75
76 if ( isset( $data['options'] ) )
77 $db['options'] = serialize( $data['options'] );
78
79 $wpdb->insert( $wpdb->prefix.'redirection_modules', $db );
80
81 self::flush( $wpdb->insert_id );
82 return $wpdb->insert_id;
83 }
84
85 return false;
86 }
87
88 static function get( $id ) {
89 global $wpdb;
90
91 $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_modules WHERE id=%d", $id ) );
92 if ( $row )
93 return self::new_item( $row );
94 return false;
95 }
96
97 function get_by_type( $type ) {
98 global $wpdb;
99
100 $cache = get_option( 'redirection_module_cache' );
101 if ( $cache && isset( $cache[$type] ) )
102 return $cache[$type];
103
104 $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_modules WHERE type=%s ORDER BY id", $type ) );
105 $items = array();
106 if ( count( $rows ) > 0 ) {
107 foreach ( $rows AS $row ) {
108 $items[] = self::new_item( $row );
109 }
110 }
111
112 $cache[$type] = $items;
113 update_option( 'redirection_module_cache', $cache );
114 return $items;
115 }
116
117 /**
118 * Get all modules
119 */
120 static function get_all() {
121 global $wpdb;
122
123 $rows = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}redirection_modules WHERE id > 0 ORDER BY id" );
124 $items = array();
125 if ( count( $rows ) > 0 ) {
126 foreach ( $rows AS $row ) {
127 $items[] = self::new_item( $row );
128 }
129 }
130
131 return array_filter( $items );
132 }
133
134 /**
135 * Get first module
136 */
137 static function get_first_id() {
138 global $wpdb;
139 return $wpdb->get_var( "SELECT id FROM {$wpdb->prefix}redirection_modules ORDER BY id LIMIT 0,1" );
140 }
141
142 /**
143 * Get all modules
144 */
145
146 static function get_for_select() {
147 $data = array();
148 $items = self::get_all();
149
150 foreach ( $items AS $item ) {
151 $data[$item->id] = $item->name;
152 }
153
154 return $data;
155 }
156
157 /**
158 * Get all module types
159 */
160 static function get_types() {
161 return array (
162 'apache' => __( 'Apache', 'redirection' ),
163 'wp' => __( 'WordPress', 'redirection' ),
164 );
165 }
166
167 static function new_item( $data ) {
168 $map = array (
169 'apache' => array( 'Apache_Module', 'apache.php' ),
170 'wp' => array( 'WordPress_Module', 'wordpress.php' ),
171 );
172
173 if ( isset( $map[$data->type] ) ) {
174 $obj = $map[$data->type][0];
175 $file = $map[$data->type][1];
176
177 if ( !class_exists( $obj ) )
178 include dirname( __FILE__ )."/../modules/$file";
179
180 return new $obj( $data );
181 }
182
183 return false;
184 }
185
186 function canonical() {
187 $can = array( 'none' => '&mdash;', 'nowww' => __( 'Strip WWW', 'redirection' ), 'www' => __( 'Force WWW', 'redirection' ) );
188 return $can[$this->canonical];
189 }
190
191 function index() {
192 $can = array( 'ignore' => '&mdash;', 'remove' => __( 'Strip index.php', 'redirection' ) );
193 return $can[$this->index];
194 }
195
196 function options() {
197 }
198
199 function type() {
200 $types = $this->get_types();
201 return $types[$this->type];
202 }
203
204 function checked( $item, $field = '' ) {
205 if ( $field && is_array( $item ) ) {
206 if ( isset( $item[$field] ) && $item[$field] )
207 echo ' checked="checked"';
208 }
209 elseif ( !empty( $item ) )
210 echo ' checked="checked"';
211 }
212
213 function select( $items, $default = '' ) {
214 if ( count( $items ) > 0 ) {
215 foreach ( $items AS $key => $value ) {
216 echo '<option value="'.$key.'"'.($key == $default ? ' selected="selected"' : '' ).'>'.$value.'</option>';
217 }
218 }
219 }
220
221 function groups() {
222 global $wpdb;
223
224 return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(id ) FROM {$wpdb->prefix}redirection_groups WHERE module_id=%d", $this->id ) );
225 }
226
227 function redirects() {
228 global $wpdb;
229
230 $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT({$wpdb->prefix}redirection_items.id) FROM {$wpdb->prefix}redirection_groups INNER JOIN {$wpdb->prefix}redirection_items ON {$wpdb->prefix}redirection_items.group_id={$wpdb->prefix}redirection_groups.id WHERE module_id=%d GROUP BY {$wpdb->prefix}redirection_items.group_id", $this->id ) );
231 if ( $count > 0 )
232 return $count;
233 return 0;
234 }
235
236 function hits() {
237 global $wpdb;
238
239 $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_logs WHERE module_id=%d", $this->id ) );
240 if ( $count > 0 )
241 return $count;
242 return 0;
243 }
244
245 function reset() {
246 self::clear_cache( $this->id );
247
248 $groups = Red_Group::get_for_module( $this->id );
249 if ( count( $groups ) > 0 ) {
250 foreach ( $groups AS $group ) {
251 $group->reset();
252 }
253 }
254
255 RE_Log::delete_for_module( $this->id );
256 }
257
258 function is_valid() {
259 return true;
260 }
261
262 function load( $data ) {
263 }
264
265 function config() {
266 }
267 }
268