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

116 lines 3.6 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 /**
5 * @return array<string, string>
6 */
7 public function get_stages() {
8 return [
9 'add_match_url_400' => 'Add a matched URL column',
10 'add_match_url_index' => 'Add match URL index',
11 'add_redirect_data_400' => 'Add column to store new flags',
12 'convert_existing_urls_400' => 'Convert existing URLs to new format',
13 ];
14 }
15
16 /**
17 * @param \wpdb $wpdb
18 * @param string $column
19 * @return bool
20 */
21 private function has_column( $wpdb, $column ) {
22 $wpdb->hide_errors();
23 $wpdb->suppress_errors( true );
24 $existing = $wpdb->get_row( "SHOW CREATE TABLE `{$wpdb->prefix}redirection_items`", ARRAY_N );
25 $wpdb->show_errors();
26 $wpdb->suppress_errors( false );
27
28 if ( isset( $existing[1] ) && strpos( strtolower( $existing[1] ), strtolower( $column ) ) !== false ) {
29 return true;
30 }
31
32 return false;
33 }
34
35 /**
36 * @param \wpdb $wpdb
37 * @return bool
38 */
39 private function has_match_index( $wpdb ) {
40 $wpdb->hide_errors();
41 $wpdb->suppress_errors( true );
42 $existing = $wpdb->get_row( "SHOW CREATE TABLE `{$wpdb->prefix}redirection_items`", ARRAY_N );
43 $wpdb->show_errors();
44 $wpdb->suppress_errors( false );
45
46 if ( isset( $existing[1] ) && strpos( strtolower( $existing[1] ), 'key `match_url' ) !== false ) {
47 return true;
48 }
49
50 return false;
51 }
52
53 /**
54 * @param \wpdb $wpdb
55 * @return bool
56 */
57 protected function add_match_url_400( $wpdb ) {
58 if ( ! $this->has_column( $wpdb, '`match_url` varchar(2000)' ) ) {
59 return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_items` ADD `match_url` VARCHAR(2000) NULL DEFAULT NULL AFTER `url`" );
60 }
61
62 return true;
63 }
64
65 /**
66 * @param \wpdb $wpdb
67 * @return bool|void
68 */
69 protected function add_match_url_index( $wpdb ) {
70 if ( ! $this->has_match_index( $wpdb ) ) {
71 return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_items` ADD INDEX `match_url` (`match_url`(191))" );
72 }
73 }
74
75 /**
76 * @param \wpdb $wpdb
77 * @return bool
78 */
79 protected function add_redirect_data_400( $wpdb ) {
80 if ( ! $this->has_column( $wpdb, '`match_data` TEXT' ) ) {
81 return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_items` ADD `match_data` TEXT NULL DEFAULT NULL AFTER `match_url`" );
82 }
83
84 return true;
85 }
86
87 /**
88 * @param \wpdb $wpdb
89 * @return bool
90 */
91 protected function convert_existing_urls_400( $wpdb ) {
92 // All regex get match_url=regex
93 $this->do_query( $wpdb, "UPDATE `{$wpdb->prefix}redirection_items` SET match_url='regex' WHERE regex=1" );
94
95 // Remove query part from all URLs and lowercase
96 $this->do_query( $wpdb, "UPDATE `{$wpdb->prefix}redirection_items` SET match_url=LOWER(url) WHERE regex=0" );
97
98 // Set exact match if query param present
99
100 $table_name = $wpdb->prefix . 'redirection_items';
101 // phpstan requires literal-string but table prefix must be dynamic for WordPress multisite
102 /** @phpstan-ignore argument.type */
103 $prepared = $wpdb->prepare( "UPDATE `{$table_name}` SET match_data=%s WHERE regex=0 AND match_url LIKE %s", '{"source":{"flag_query":"exactorder"}}', '%?%' ); // phpcs:ignore
104 if ( $prepared !== null ) {
105 $this->do_query( $wpdb, $prepared );
106 }
107
108 // Trim the last / from a URL
109 $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) = '/'" );
110 $this->do_query( $wpdb, "UPDATE `{$wpdb->prefix}redirection_items` SET match_url=REPLACE(match_url, '/?', '?') WHERE regex=0" );
111
112 // Any URL that is now empty becomes /
113 return $this->do_query( $wpdb, "UPDATE `{$wpdb->prefix}redirection_items` SET match_url='/' WHERE match_url=''" );
114 }
115 }
116