PluginProbe
Redirection / 5.3.10
Redirection v5.3.10
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 5.3.10, at database/schema/400.php

73 lines 2.8 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=LOWER(url) WHERE regex=0" );
61
62 // Set exact match if query param present
63 $this->do_query( $wpdb, $wpdb->prepare( "UPDATE `{$wpdb->prefix}redirection_items` SET match_data=%s WHERE regex=0 AND match_url LIKE '%?%'", '{"source":{"flag_query":"exactorder"}}' ) );
64
65 // Trim the last / from a URL
66 $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) = '/'" );
67 $this->do_query( $wpdb, "UPDATE `{$wpdb->prefix}redirection_items` SET match_url=REPLACE(match_url, '/?', '?') WHERE regex=0" );
68
69 // Any URL that is now empty becomes /
70 return $this->do_query( $wpdb, "UPDATE `{$wpdb->prefix}redirection_items` SET match_url='/' WHERE match_url=''" );
71 }
72 }
73