PluginProbe
Redirection / 2.2.7
Redirection v2.2.7
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.2.7, at models/module.php

269 lines 6.2 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 function Red_Module( $values = '' ) {
5 if ( is_object( $values ) ) {
6 foreach ( $values AS $key => $value ) {
7 $this->$key = $value;
8 }
9
10 if ( $this->options )
11 $this->load( unserialize( $this->options ) );
12 }
13 }
14
15 function module_flush( $items ) {
16 }
17
18 function module_flush_delete() {
19 }
20
21 function flush( $id ) {
22 $module = Red_Module::get( $id );
23 if ( $module && $module->is_valid() )
24 $module->module_flush( Red_Item::get_all_for_module( $id ) );
25 }
26
27 function flush_delete( $id ) {
28 $module = Red_Module::get( $id );
29 if ( $module )
30 $module->module_flush_delete();
31 }
32
33 function update( $data ) {
34 global $wpdb;
35
36 $this->name = $data['name'];
37 $options = $this->save( $data );
38 $wpdb->update( $wpdb->prefix.'redirection_modules', array( 'name' => trim( $data['name'] ), 'options' => empty( $options ) ? '' : serialize( $options ) ), array( 'id' => intval( $this->id ) ) );
39
40 Red_Module::clear_cache( $this->id );
41 }
42
43 function delete() {
44 global $wpdb;
45
46 $groups = Red_Group::get_for_module( $this->id );
47 if ( count( $groups ) > 0 ) {
48 foreach ( $groups AS $group ) {
49 $group->delete( $group->id );
50 }
51 }
52
53 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_modules WHERE id=%d", $this->id ) );
54
55 RE_Log::delete_for_module( $this->id );
56 Red_Module::clear_cache( $this->id );
57 Red_Module::flush_delete( $this->id );
58 }
59
60 function clear_cache( $module ) {
61 delete_option( 'redirection_module_cache' );
62 Red_Module::flush( $module );
63 }
64
65 function create( $data ) {
66 global $wpdb;
67
68 if ( strlen( $data['name'] ) > 0 ) {
69 $db = array(
70 'name' => trim( $data['name'] ),
71 'type' => $data['type'],
72 );
73
74 if ( isset( $data['options'] ) )
75 $db['options'] = serialize( $data['options'] );
76
77 $wpdb->insert( $wpdb->prefix.'redirection_modules', $db );
78
79 Red_Module::flush( $wpdb->insert_id );
80 return $wpdb->insert_id;
81 }
82
83 return false;
84 }
85
86 function get( $id ) {
87 global $wpdb;
88
89 $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_modules WHERE id=%d", $id ) );
90 if ( $row )
91 return Red_Module::new_item( $row );
92 return false;
93 }
94
95 function get_by_type( $type ) {
96 global $wpdb;
97
98 $cache = get_option( 'redirection_module_cache' );
99 if ( $cache && isset( $cache[$type] ) )
100 return $cache[$type];
101
102 $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_modules WHERE type=%s ORDER BY id", $type ) );
103 $items = array();
104 if ( count( $rows ) > 0 ) {
105 foreach ( $rows AS $row ) {
106 $items[] = Red_Module::new_item( $row );
107 }
108 }
109
110 $cache[$type] = $items;
111 update_option( 'redirection_module_cache', $cache );
112 return $items;
113 }
114
115 /**
116 * Get all modules
117 */
118 function get_all() {
119 global $wpdb;
120
121 $rows = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}redirection_modules WHERE id > 0 ORDER BY id" );
122 $items = array();
123 if ( count( $rows ) > 0 ) {
124 foreach ( $rows AS $row ) {
125 $items[] = Red_Module::new_item( $row );
126 }
127 }
128
129 return $items;
130 }
131
132 /**
133 * Get first module
134 */
135 function get_first_id() {
136 global $wpdb;
137 return $wpdb->get_var( "SELECT id FROM {$wpdb->prefix}redirection_modules ORDER BY id LIMIT 0,1" );
138 }
139
140 /**
141 * Get all modules
142 */
143
144 function get_for_select() {
145 $data = array();
146 $items = Red_Module::get_all();
147
148 foreach ( $items AS $item ) {
149 $data[$item->id] = $item->name;
150 }
151
152 return $data;
153 }
154
155 /**
156 * Get all module types
157 */
158 function get_types() {
159 return array (
160 'apache' => __( 'Apache', 'redirection' ),
161 'wp' => __( 'WordPress', 'redirection' ),
162 '404' => __( '404 Errors', 'redirection' ),
163 );
164 }
165
166 function new_item( $data ) {
167 $map = array (
168 'apache' => array( 'Apache_Module', 'apache.php' ),
169 'wp' => array( 'WordPress_Module', 'wordpress.php' ),
170 '404' => array( 'Error404_Module', '404.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(id ) 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 Red_Module::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
269