PluginProbe
Redirection / 2.4.2
Redirection v2.4.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 / database.php

database.php in Redirection 2.4.2, at models/database.php

242 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class RE_Database {
4 private function get_charset() {
5 global $wpdb;
6
7 $charset_collate = '';
8 if ( ! empty( $wpdb->charset ) )
9 $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
10
11 if ( ! empty( $wpdb->collate ) )
12 $charset_collate .= " COLLATE $wpdb->collate";
13
14 return $charset_collate;
15 }
16
17 public function install() {
18 global $wpdb;
19
20 $charset_collate = $this->get_charset();
21
22 $create = array(
23 "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}redirection_items`(
24 `id` int(11) unsigned NOT NULL auto_increment,
25 `url` mediumtext NOT NULL,
26 `regex` int(11) unsigned NOT NULL default '0',
27 `position` int(11) unsigned NOT NULL default '0',
28 `last_count` int(10) unsigned NOT NULL default '0',
29 `last_access` datetime NOT NULL,
30 `group_id` int(11) NOT NULL default '0',
31 `status` enum('enabled','disabled' ) NOT NULL default 'enabled',
32 `action_type` varchar(20) NOT NULL,
33 `action_code` int(11) unsigned NOT NULL,
34 `action_data` mediumtext,
35 `match_type` varchar(20) NOT NULL,
36 `title` varchar(50) NULL,
37 PRIMARY KEY ( `id`),
38 KEY `url` (`url`(200)),
39 KEY `status` (`status`),
40 KEY `regex` (`regex`),
41 KEY `group_idpos` (`group_id`,`position`),
42 KEY `group` (`group_id`)
43 ) $charset_collate",
44
45 "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}redirection_groups`(
46 `id` int(11) NOT NULL auto_increment,
47 `name` varchar(50) NOT NULL,
48 `tracking` int(11) NOT NULL default '1',
49 `module_id` int(11) unsigned NOT NULL default '0',
50 `status` enum('enabled','disabled' ) NOT NULL default 'enabled',
51 `position` int(11) unsigned NOT NULL default '0',
52 PRIMARY KEY ( `id`),
53 KEY `module_id` (`module_id`),
54 KEY `status` (`status`)
55 ) $charset_collate",
56
57 "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}redirection_logs`(
58 `id` int(11) unsigned NOT NULL auto_increment,
59 `created` datetime NOT NULL,
60 `url` mediumtext NOT NULL,
61 `sent_to` mediumtext,
62 `agent` mediumtext NOT NULL,
63 `referrer` mediumtext,
64 `redirection_id` int(11) unsigned default NULL,
65 `ip` varchar(17) NOT NULL default '',
66 `module_id` int(11) unsigned NOT NULL,
67 `group_id` int(11) unsigned default NULL,
68 PRIMARY KEY ( `id`),
69 KEY `created` (`created`),
70 KEY `redirection_id` (`redirection_id`),
71 KEY `ip` (`ip`),
72 KEY `group_id` (`group_id`),
73 KEY `module_id` (`module_id`)
74 ) $charset_collate",
75
76 "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}redirection_404` (
77 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
78 `created` datetime NOT NULL,
79 `url` varchar(255) NOT NULL DEFAULT '',
80 `agent` varchar(255) DEFAULT NULL,
81 `referrer` varchar(255) DEFAULT NULL,
82 `ip` int(10) unsigned NOT NULL,
83 PRIMARY KEY (`id`),
84 KEY `created` (`created`),
85 KEY `url` (`url`),
86 KEY `ip` (`ip`),
87 KEY `referrer` (`referrer`)
88 ) $charset_collate;"
89 );
90
91 foreach ( $create AS $sql ) {
92 if ( $wpdb->query( $sql ) === false )
93 return false;
94 }
95
96 // Groups
97 if ( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_groups" ) == 0 ) {
98 $wpdb->insert( $wpdb->prefix.'redirection_groups', array( 'name' => __( 'Redirections' ), 'module_id' => 1, 'position' => 0 ) );
99 $wpdb->insert( $wpdb->prefix.'redirection_groups', array( 'name' => __( 'Modified Posts' ), 'module_id' => 1, 'position' => 1 ) );
100
101 $options = get_option( 'redirection_options' );
102 $options['monitor_post'] = 2;
103 $options['monitor_category'] = 2;
104
105 update_option( 'redirection_options', $options );
106 }
107 }
108
109 public function upgrade( $current, $target ) {
110 global $wpdb;
111
112 $wpdb->show_errors();
113
114 // No previous version? Install the DB tables
115 if ( $current === false )
116 $success = $this->install();
117 else {
118 // Try and upgrade from a previous version
119 if ( $current == '2.0' )
120 $this->upgrade_from_20();
121 elseif ( $current == '2.0.1' )
122 $this->upgrade_from_21();
123 elseif ( $current == '2.0.2' )
124 $this->upgrade_from_22();
125
126 if ( version_compare( $current, '2.1.16' ) == -1 )
127 $this->upgrade_to_216();
128
129 if ( version_compare( $current, '2.2' ) == -1 )
130 $this->upgrade_to_220();
131
132 if ( version_compare( $current, '2.3.1' ) == -1 )
133 $this->upgrade_to_231();
134
135 if ( version_compare( $current, '2.3.2' ) == -1 )
136 $this->upgrade_to_232();
137
138 $success = true;
139 }
140
141 // Set our current version
142 update_option( 'redirection_version', $target );
143
144 $wpdb->hide_errors();
145 return $success;
146 }
147
148 private function upgrade_to_231() {
149 global $wpdb;
150
151 $charset_collate = $this->get_charset();
152
153 $wpdb->query( "DELETE FROM {$wpdb->prefix}redirection_modules WHERE type='404'" );
154 $wpdb->query( "UPDATE {$wpdb->prefix}redirection_groups SET module_id=1 WHERE module_id=3" );
155
156 $wpdb->query( "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}redirection_404` (
157 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
158 `created` datetime NOT NULL,
159 `url` varchar(255) NOT NULL DEFAULT '',
160 `agent` varchar(255) DEFAULT NULL,
161 `referrer` varchar(255) DEFAULT NULL,
162 `ip` int(10) unsigned NOT NULL,
163 PRIMARY KEY (`id`),
164 KEY `created` (`created`),
165 KEY `url` (`url`),
166 KEY `ip` (`ip`,`id`),
167 KEY `referrer` (`referrer`)
168 ) $charset_collate;" );
169 }
170
171 private function upgrade_to_232() {
172 global $wpdb;
173
174 $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}redirection_modules;" );
175 }
176
177 private function upgrade_from_20() {
178 global $wpdb;
179
180 $this->upgrade_from_21();
181 $this->upgrade_from_22();
182 }
183
184 private function upgrade_from_21() {
185 global $wpdb;
186
187 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_items` ADD `title` varchar(50) NULL" );
188
189 $this->upgrade_from_22();
190 }
191
192 private function upgrade_from_22() {
193 global $wpdb;
194
195 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_items` CHANGE `title` `title` varchar(50) NULL" );
196 }
197
198 private function upgrade_to_216() {
199 global $wpdb;
200
201 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_groups` ADD INDEX(module_id)" );
202 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_groups` ADD INDEX(status)" );
203 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_items` ADD INDEX(url(200))" );
204 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_items` ADD INDEX(status)" );
205 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_items` ADD INDEX(regex)" );
206 }
207
208 private function upgrade_to_220() {
209 global $wpdb;
210
211 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_items` ADD INDEX `group_idpos` (`group_id`,`position`)" );
212 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_items` ADD INDEX `group` (`group_id`)" );
213
214 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_logs` ADD INDEX `group` (`group_id`)" );
215 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_logs` ADD INDEX `redirection_id` (`redirection_id`)" );
216 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_logs` ADD INDEX `created` (`created`)" );
217 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_logs` ADD INDEX `ip` (`ip`)" );
218 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_logs` ADD INDEX `group_id` (`group_id`)" );
219 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_logs` ADD INDEX `module_id` (`module_id`)" );
220
221 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_modules` ADD INDEX `name` (`name`)" );
222 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_modules` ADD INDEX `type` (`type`)" );
223 }
224
225 public function remove( $plugin ) {
226 global $wpdb;
227
228 $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}redirection;" );
229 $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}redirection_items;" );
230 $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}redirection_logs;" );
231 $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}redirection_groups;" );
232 $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}redirection_modules;" );
233 $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}redirection_404;" );
234
235 delete_option( 'redirection_lookup' );
236 delete_option( 'redirection_post' );
237 delete_option( 'redirection_root' );
238 delete_option( 'redirection_index' );
239 delete_option( 'redirection_version' );
240 }
241 }
242