PluginProbe
Redirection / 2.3.14
Redirection v2.3.14
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.3.14, at models/database.php

249 lines 8.5 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_modules`(
77 `id` int(11) unsigned NOT NULL auto_increment,
78 `type` varchar(20) NOT NULL default '',
79 `name` varchar(50) NOT NULL default '',
80 `options` mediumtext,
81 PRIMARY KEY ( `id`),
82 KEY `name` (`name`),
83 KEY `type` (`type`)
84 ) $charset_collate",
85
86 "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}redirection_404` (
87 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
88 `created` datetime NOT NULL,
89 `url` varchar(255) NOT NULL DEFAULT '',
90 `agent` varchar(255) DEFAULT NULL,
91 `referrer` varchar(255) DEFAULT NULL,
92 `ip` int(10) unsigned NOT NULL,
93 PRIMARY KEY (`id`),
94 KEY `created` (`created`),
95 KEY `url` (`url`),
96 KEY `ip` (`ip`),
97 KEY `referrer` (`referrer`)
98 ) $charset_collate;"
99 );
100
101 foreach ( $create AS $sql ) {
102 if ( $wpdb->query( $sql ) === false )
103 return false;
104 }
105
106 // Modules
107 if ( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_modules" ) == 0 ) {
108 $wpdb->insert( $wpdb->prefix.'redirection_modules', array( 'type' => 'wp', 'name' => __( 'WordPress', 'redirection' ), 'options' => '' ) );
109 $wpdb->insert( $wpdb->prefix.'redirection_modules', array( 'type' => 'apache', 'name' => __( 'Apache', 'redirection' ), 'options' => '' ) );
110 }
111
112 // Groups
113 if ( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_groups" ) == 0 ) {
114 $wpdb->insert( $wpdb->prefix.'redirection_groups', array( 'name' => __( 'Redirections' ), 'module_id' => 1, 'position' => 0 ) );
115 $wpdb->insert( $wpdb->prefix.'redirection_groups', array( 'name' => __( 'Modified Posts' ), 'module_id' => 1, 'position' => 1 ) );
116
117 $options = get_option( 'redirection_options' );
118 $options['monitor_post'] = 2;
119 $options['monitor_category'] = 2;
120
121 update_option( 'redirection_options', $options );
122 }
123 }
124
125 public function upgrade( $current, $target ) {
126 global $wpdb;
127
128 $wpdb->show_errors();
129
130 // No previous version? Install the DB tables
131 if ( $current === false )
132 $success = $this->install();
133 else {
134 // Try and upgrade from a previous version
135 if ( $current == '2.0' )
136 $this->upgrade_from_20();
137 elseif ( $current == '2.0.1' )
138 $this->upgrade_from_21();
139 elseif ( $current == '2.0.2' )
140 $this->upgrade_from_22();
141
142 if ( version_compare( $current, '2.1.16' ) == -1 )
143 $this->upgrade_to_216();
144
145 if ( version_compare( $current, '2.2' ) == -1 )
146 $this->upgrade_to_220();
147
148 if ( version_compare( $current, '2.3.1' ) == -1 )
149 $this->upgrade_to_231();
150
151 $success = true;
152 }
153
154 // Set our current version
155 update_option( 'redirection_version', $target );
156
157 $wpdb->hide_errors();
158 return $success;
159 }
160
161 private function upgrade_to_231() {
162 global $wpdb;
163
164 $charset_collate = $this->get_charset();
165
166 $wpdb->query( "DELETE FROM {$wpdb->prefix}redirection_modules WHERE type='404'" );
167 $wpdb->query( "UPDATE {$wpdb->prefix}redirection_groups SET module_id=1 WHERE module_id=3" );
168
169 $wpdb->query( "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}redirection_404` (
170 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
171 `created` datetime NOT NULL,
172 `url` varchar(255) NOT NULL DEFAULT '',
173 `agent` varchar(255) DEFAULT NULL,
174 `referrer` varchar(255) DEFAULT NULL,
175 `ip` int(10) unsigned NOT NULL,
176 PRIMARY KEY (`id`),
177 KEY `created` (`created`),
178 KEY `url` (`url`),
179 KEY `ip` (`ip`,`id`),
180 KEY `referrer` (`referrer`)
181 ) $charset_collate;" );
182 }
183
184 private function upgrade_from_20() {
185 global $wpdb;
186
187 $this->upgrade_from_21();
188 $this->upgrade_from_22();
189 }
190
191 private function upgrade_from_21() {
192 global $wpdb;
193
194 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_items` ADD `title` varchar(50) NULL" );
195
196 $this->upgrade_from_22();
197 }
198
199 private function upgrade_from_22() {
200 global $wpdb;
201
202 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_items` CHANGE `title` `title` varchar(50) NULL" );
203 }
204
205 private function upgrade_to_216() {
206 global $wpdb;
207
208 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_groups` ADD INDEX(module_id)" );
209 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_groups` ADD INDEX(status)" );
210 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_items` ADD INDEX(url(200))" );
211 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_items` ADD INDEX(status)" );
212 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_items` ADD INDEX(regex)" );
213 }
214
215 private function upgrade_to_220() {
216 global $wpdb;
217
218 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_items` ADD INDEX `group_idpos` (`group_id`,`position`)" );
219 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_items` ADD INDEX `group` (`group_id`)" );
220
221 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_logs` ADD INDEX `group` (`group_id`)" );
222 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_logs` ADD INDEX `redirection_id` (`redirection_id`)" );
223 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_logs` ADD INDEX `created` (`created`)" );
224 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_logs` ADD INDEX `ip` (`ip`)" );
225 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_logs` ADD INDEX `group_id` (`group_id`)" );
226 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_logs` ADD INDEX `module_id` (`module_id`)" );
227
228 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_modules` ADD INDEX `name` (`name`)" );
229 $wpdb->query( "ALTER TABLE `{$wpdb->prefix}redirection_modules` ADD INDEX `type` (`type`)" );
230 }
231
232 public function remove( $plugin ) {
233 global $wpdb;
234
235 $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}redirection;" );
236 $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}redirection_items;" );
237 $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}redirection_logs;" );
238 $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}redirection_groups;" );
239 $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}redirection_modules;" );
240 $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}redirection_404;" );
241
242 delete_option( 'redirection_lookup' );
243 delete_option( 'redirection_post' );
244 delete_option( 'redirection_root' );
245 delete_option( 'redirection_index' );
246 delete_option( 'redirection_version' );
247 }
248 }
249