PluginProbe
Redirection / 4.2.2
Redirection v4.2.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 / database / schema / 400.php

400.php in Redirection 4.2.2, at database/schema/400.php

69 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Red_Database_400 extends Red_Database_Upgrader {
4 public function get_stages() {
5 return [
6 'add_match_url_400' => 'Add a matched URL column',
7 'add_match_url_index' => 'Add match URL index',
8 'add_redirect_data_400' => 'Add column to store new flags',
9 'convert_existing_urls_400' => 'Convert existing URLs to new format',
10 ];
11 }
12
13 private function has_column( $wpdb, $column ) {
14 $existing = $wpdb->get_row( "SHOW CREATE TABLE `{$wpdb->prefix}redirection_items`", ARRAY_N );
15
16 if ( isset( $existing[1] ) && strpos( strtolower( $existing[1] ), strtolower( $column ) ) !== false ) {
17 return true;
18 }
19
20 return false;
21 }
22
23 private function has_match_index( $wpdb ) {
24 $existing = $wpdb->get_row( "SHOW CREATE TABLE `{$wpdb->prefix}redirection_items`", ARRAY_N );
25
26 if ( isset( $existing[1] ) && strpos( strtolower( $existing[1] ), 'key `match_url' ) !== false ) {
27 return true;
28 }
29
30 return false;
31 }
32
33 protected function add_match_url_400( $wpdb ) {
34 if ( ! $this->has_column( $wpdb, '`match_url` varchar(2000)' ) ) {
35 return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_items` ADD `match_url` VARCHAR(2000) NULL DEFAULT NULL AFTER `url`" );
36 }
37
38 return true;
39 }
40
41 protected function add_match_url_index( $wpdb ) {
42 if ( ! $this->has_match_index( $wpdb ) ) {
43 return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_items` ADD INDEX `match_url` (`match_url`(191))" );
44 }
45 }
46
47 protected function add_redirect_data_400( $wpdb ) {
48 if ( ! $this->has_column( $wpdb, '`match_data` TEXT' ) ) {
49 return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_items` ADD `match_data` TEXT NULL DEFAULT NULL AFTER `match_url`" );
50 }
51
52 return true;
53 }
54
55 protected function convert_existing_urls_400( $wpdb ) {
56 // All regex get match_url=regex
57 $this->do_query( $wpdb, "UPDATE `{$wpdb->prefix}redirection_items` SET match_url='regex' WHERE regex=1" );
58
59 // Remove query part from all URLs and lowercase
60 $this->do_query( $wpdb, "UPDATE `{$wpdb->prefix}redirection_items` SET match_url=SUBSTRING_INDEX(LOWER(url), '?', 1) WHERE regex=0" );
61
62 // Trim the last / from a URL
63 $this->do_query( $wpdb, "UPDATE `{$wpdb->prefix}redirection_items` SET match_url=LEFT(match_url,LENGTH(match_url)-1) WHERE regex=0 AND match_url != '/' AND RIGHT(match_url, 1) = '/'" );
64
65 // Any URL that is now empty becomes /
66 return $this->do_query( $wpdb, "UPDATE `{$wpdb->prefix}redirection_items` SET match_url='/' WHERE match_url=''" );
67 }
68 }
69