1 <?php |
1 <?php |
2 /** |
2 /** |
3 * Plugins may load this file to gain access to special helper functions for |
3 * Plugins may load this file to gain access to special helper functions |
4 * plugin installation. This file is not included by WordPress and it is |
4 * for plugin installation. This file is not included by WordPress and it is |
5 * recommended, to prevent fatal errors, that this file is included using |
5 * recommended, to prevent fatal errors, that this file is included using |
6 * require_once. |
6 * require_once. |
7 * |
7 * |
8 * These functions are not optimized for speed, but they should only be used |
8 * These functions are not optimized for speed, but they should only be used |
9 * once in a while, so speed shouldn't be a concern. If it is and you are |
9 * once in a while, so speed shouldn't be a concern. If it is and you are |
10 * needing to use these functions a lot, you might experience time outs. If you |
10 * needing to use these functions a lot, you might experience timeouts. |
11 * do, then it is advised to just write the SQL code yourself. |
11 * If you do, then it is advised to just write the SQL code yourself. |
12 * |
12 * |
13 * check_column( 'wp_links', 'link_description', 'mediumtext' ); |
13 * check_column( 'wp_links', 'link_description', 'mediumtext' ); |
|
14 * |
14 * if ( check_column( $wpdb->comments, 'comment_author', 'tinytext' ) ) { |
15 * if ( check_column( $wpdb->comments, 'comment_author', 'tinytext' ) ) { |
15 * echo "ok\n"; |
16 * echo "ok\n"; |
16 * } |
17 * } |
17 * |
18 * |
18 * $error_count = 0; |
|
19 * $tablename = $wpdb->links; |
|
20 * // Check the column. |
19 * // Check the column. |
21 * if ( ! check_column( $wpdb->links, 'link_description', 'varchar( 255 )' ) ) { |
20 * if ( ! check_column( $wpdb->links, 'link_description', 'varchar( 255 )' ) ) { |
22 * $ddl = "ALTER TABLE $wpdb->links MODIFY COLUMN link_description varchar(255) NOT NULL DEFAULT '' "; |
21 * $ddl = "ALTER TABLE $wpdb->links MODIFY COLUMN link_description varchar(255) NOT NULL DEFAULT '' "; |
23 * $q = $wpdb->query( $ddl ); |
22 * $q = $wpdb->query( $ddl ); |
24 * } |
23 * } |
|
24 * |
|
25 * $error_count = 0; |
|
26 * $tablename = $wpdb->links; |
25 * |
27 * |
26 * if ( check_column( $wpdb->links, 'link_description', 'varchar( 255 )' ) ) { |
28 * if ( check_column( $wpdb->links, 'link_description', 'varchar( 255 )' ) ) { |
27 * $res .= $tablename . ' - ok <br />'; |
29 * $res .= $tablename . ' - ok <br />'; |
28 * } else { |
30 * } else { |
29 * $res .= 'There was a problem with ' . $tablename . '<br />'; |
31 * $res .= 'There was a problem with ' . $tablename . '<br />'; |
57 return true; |
59 return true; |
58 } |
60 } |
59 } |
61 } |
60 |
62 |
61 // Didn't find it, so try to create it. |
63 // Didn't find it, so try to create it. |
|
64 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- No applicable variables for this query. |
62 $wpdb->query( $create_ddl ); |
65 $wpdb->query( $create_ddl ); |
63 |
66 |
64 // We cannot directly tell that whether this succeeded! |
67 // We cannot directly tell whether this succeeded! |
65 foreach ( $wpdb->get_col( 'SHOW TABLES', 0 ) as $table ) { |
68 foreach ( $wpdb->get_col( 'SHOW TABLES', 0 ) as $table ) { |
66 if ( $table === $table_name ) { |
69 if ( $table === $table_name ) { |
67 return true; |
70 return true; |
68 } |
71 } |
69 } |
72 } |
86 * @return bool True on success or if the column already exists. False on failure. |
89 * @return bool True on success or if the column already exists. False on failure. |
87 */ |
90 */ |
88 function maybe_add_column( $table_name, $column_name, $create_ddl ) { |
91 function maybe_add_column( $table_name, $column_name, $create_ddl ) { |
89 global $wpdb; |
92 global $wpdb; |
90 |
93 |
|
94 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Cannot be prepared. Fetches columns for table names. |
91 foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) { |
95 foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) { |
92 if ( $column === $column_name ) { |
96 if ( $column === $column_name ) { |
93 return true; |
97 return true; |
94 } |
98 } |
95 } |
99 } |
96 |
100 |
97 // Didn't find it, so try to create it. |
101 // Didn't find it, so try to create it. |
|
102 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- No applicable variables for this query. |
98 $wpdb->query( $create_ddl ); |
103 $wpdb->query( $create_ddl ); |
99 |
104 |
100 // We cannot directly tell that whether this succeeded! |
105 // We cannot directly tell whether this succeeded! |
|
106 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Cannot be prepared. Fetches columns for table names. |
101 foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) { |
107 foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) { |
102 if ( $column === $column_name ) { |
108 if ( $column === $column_name ) { |
103 return true; |
109 return true; |
104 } |
110 } |
105 } |
111 } |
121 * @return bool True on success or if the column doesn't exist. False on failure. |
127 * @return bool True on success or if the column doesn't exist. False on failure. |
122 */ |
128 */ |
123 function maybe_drop_column( $table_name, $column_name, $drop_ddl ) { |
129 function maybe_drop_column( $table_name, $column_name, $drop_ddl ) { |
124 global $wpdb; |
130 global $wpdb; |
125 |
131 |
|
132 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Cannot be prepared. Fetches columns for table names. |
126 foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) { |
133 foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) { |
127 if ( $column === $column_name ) { |
134 if ( $column === $column_name ) { |
128 |
135 |
129 // Found it, so try to drop it. |
136 // Found it, so try to drop it. |
|
137 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- No applicable variables for this query. |
130 $wpdb->query( $drop_ddl ); |
138 $wpdb->query( $drop_ddl ); |
131 |
139 |
132 // We cannot directly tell that whether this succeeded! |
140 // We cannot directly tell whether this succeeded! |
|
141 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Cannot be prepared. Fetches columns for table names. |
133 foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) { |
142 foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) { |
134 if ( $column === $column_name ) { |
143 if ( $column === $column_name ) { |
135 return false; |
144 return false; |
136 } |
145 } |
137 } |
146 } |
145 /** |
154 /** |
146 * Checks that database table column matches the criteria. |
155 * Checks that database table column matches the criteria. |
147 * |
156 * |
148 * Uses the SQL DESC for retrieving the table info for the column. It will help |
157 * Uses the SQL DESC for retrieving the table info for the column. It will help |
149 * understand the parameters, if you do more research on what column information |
158 * understand the parameters, if you do more research on what column information |
150 * is returned by the SQL statement. Pass in null to skip checking that |
159 * is returned by the SQL statement. Pass in null to skip checking that criteria. |
151 * criteria. |
160 * |
152 * |
161 * Column names returned from DESC table are case sensitive and are as listed: |
153 * Column names returned from DESC table are case sensitive and are listed: |
162 * |
154 * Field |
163 * - Field |
155 * Type |
164 * - Type |
156 * Null |
165 * - Null |
157 * Key |
166 * - Key |
158 * Default |
167 * - Default |
159 * Extra |
168 * - Extra |
160 * |
169 * |
161 * @since 1.0.0 |
170 * @since 1.0.0 |
162 * |
171 * |
163 * @global wpdb $wpdb WordPress database abstraction object. |
172 * @global wpdb $wpdb WordPress database abstraction object. |
164 * |
173 * |
172 * @return bool True, if matches. False, if not matching. |
181 * @return bool True, if matches. False, if not matching. |
173 */ |
182 */ |
174 function check_column( $table_name, $col_name, $col_type, $is_null = null, $key = null, $default_value = null, $extra = null ) { |
183 function check_column( $table_name, $col_name, $col_type, $is_null = null, $key = null, $default_value = null, $extra = null ) { |
175 global $wpdb; |
184 global $wpdb; |
176 |
185 |
177 $diffs = 0; |
186 $diffs = 0; |
|
187 |
|
188 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Cannot be prepared. Fetches columns for table names. |
178 $results = $wpdb->get_results( "DESC $table_name" ); |
189 $results = $wpdb->get_results( "DESC $table_name" ); |
179 |
190 |
180 foreach ( $results as $row ) { |
191 foreach ( $results as $row ) { |
181 |
192 |
182 if ( $row->Field === $col_name ) { |
193 if ( $row->Field === $col_name ) { |