author | ymh <ymh.work@gmail.com> |
Thu, 29 Sep 2022 08:06:27 +0200 | |
changeset 20 | 7b1b88e27a20 |
parent 19 | 3d72ae0968f4 |
child 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* Plugins may load this file to gain access to special helper functions for |
|
4 |
* plugin installation. This file is not included by WordPress and it is |
|
5 |
* recommended, to prevent fatal errors, that this file is included using |
|
16 | 6 |
* require_once. |
0 | 7 |
* |
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 |
|
10 |
* needing to use these functions a lot, you might experience time outs. If you |
|
11 |
* do, then it is advised to just write the SQL code yourself. |
|
12 |
* |
|
5 | 13 |
* check_column( 'wp_links', 'link_description', 'mediumtext' ); |
14 |
* if ( check_column( $wpdb->comments, 'comment_author', 'tinytext' ) ) { |
|
15 |
* echo "ok\n"; |
|
16 |
* } |
|
0 | 17 |
* |
5 | 18 |
* $error_count = 0; |
19 |
* $tablename = $wpdb->links; |
|
20 |
* // Check the column. |
|
16 | 21 |
* if ( ! check_column( $wpdb->links, 'link_description', 'varchar( 255 )' ) ) { |
5 | 22 |
* $ddl = "ALTER TABLE $wpdb->links MODIFY COLUMN link_description varchar(255) NOT NULL DEFAULT '' "; |
23 |
* $q = $wpdb->query( $ddl ); |
|
24 |
* } |
|
0 | 25 |
* |
5 | 26 |
* if ( check_column( $wpdb->links, 'link_description', 'varchar( 255 )' ) ) { |
27 |
* $res .= $tablename . ' - ok <br />'; |
|
28 |
* } else { |
|
29 |
* $res .= 'There was a problem with ' . $tablename . '<br />'; |
|
30 |
* ++$error_count; |
|
31 |
* } |
|
0 | 32 |
* |
33 |
* @package WordPress |
|
34 |
* @subpackage Plugin |
|
35 |
*/ |
|
36 |
||
37 |
/** Load WordPress Bootstrap */ |
|
16 | 38 |
require_once dirname( __DIR__ ) . '/wp-load.php'; |
0 | 39 |
|
9 | 40 |
if ( ! function_exists( 'maybe_create_table' ) ) : |
41 |
/** |
|
16 | 42 |
* Creates a table in the database if it doesn't already exist. |
9 | 43 |
* |
44 |
* @since 1.0.0 |
|
45 |
* |
|
46 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
47 |
* |
|
48 |
* @param string $table_name Database table name. |
|
16 | 49 |
* @param string $create_ddl SQL statement to create table. |
50 |
* @return bool True on success or if the table already exists. False on failure. |
|
9 | 51 |
*/ |
52 |
function maybe_create_table( $table_name, $create_ddl ) { |
|
53 |
global $wpdb; |
|
16 | 54 |
|
9 | 55 |
foreach ( $wpdb->get_col( 'SHOW TABLES', 0 ) as $table ) { |
16 | 56 |
if ( $table === $table_name ) { |
9 | 57 |
return true; |
58 |
} |
|
0 | 59 |
} |
16 | 60 |
|
9 | 61 |
// Didn't find it, so try to create it. |
62 |
$wpdb->query( $create_ddl ); |
|
5 | 63 |
|
9 | 64 |
// We cannot directly tell that whether this succeeded! |
65 |
foreach ( $wpdb->get_col( 'SHOW TABLES', 0 ) as $table ) { |
|
16 | 66 |
if ( $table === $table_name ) { |
9 | 67 |
return true; |
68 |
} |
|
0 | 69 |
} |
16 | 70 |
|
9 | 71 |
return false; |
0 | 72 |
} |
73 |
endif; |
|
74 |
||
9 | 75 |
if ( ! function_exists( 'maybe_add_column' ) ) : |
76 |
/** |
|
16 | 77 |
* Adds column to database table, if it doesn't already exist. |
9 | 78 |
* |
79 |
* @since 1.0.0 |
|
80 |
* |
|
81 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
82 |
* |
|
16 | 83 |
* @param string $table_name Database table name. |
84 |
* @param string $column_name Table column name. |
|
85 |
* @param string $create_ddl SQL statement to add column. |
|
86 |
* @return bool True on success or if the column already exists. False on failure. |
|
9 | 87 |
*/ |
88 |
function maybe_add_column( $table_name, $column_name, $create_ddl ) { |
|
89 |
global $wpdb; |
|
16 | 90 |
|
9 | 91 |
foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) { |
16 | 92 |
if ( $column === $column_name ) { |
9 | 93 |
return true; |
94 |
} |
|
0 | 95 |
} |
5 | 96 |
|
9 | 97 |
// Didn't find it, so try to create it. |
98 |
$wpdb->query( $create_ddl ); |
|
5 | 99 |
|
9 | 100 |
// We cannot directly tell that whether this succeeded! |
101 |
foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) { |
|
16 | 102 |
if ( $column === $column_name ) { |
9 | 103 |
return true; |
104 |
} |
|
0 | 105 |
} |
16 | 106 |
|
9 | 107 |
return false; |
0 | 108 |
} |
109 |
endif; |
|
110 |
||
111 |
/** |
|
16 | 112 |
* Drops column from database table, if it exists. |
0 | 113 |
* |
114 |
* @since 1.0.0 |
|
5 | 115 |
* |
116 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
0 | 117 |
* |
16 | 118 |
* @param string $table_name Database table name. |
119 |
* @param string $column_name Table column name. |
|
120 |
* @param string $drop_ddl SQL statement to drop column. |
|
121 |
* @return bool True on success or if the column doesn't exist. False on failure. |
|
0 | 122 |
*/ |
9 | 123 |
function maybe_drop_column( $table_name, $column_name, $drop_ddl ) { |
0 | 124 |
global $wpdb; |
16 | 125 |
|
9 | 126 |
foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) { |
16 | 127 |
if ( $column === $column_name ) { |
5 | 128 |
|
129 |
// Found it, so try to drop it. |
|
9 | 130 |
$wpdb->query( $drop_ddl ); |
5 | 131 |
|
132 |
// We cannot directly tell that whether this succeeded! |
|
9 | 133 |
foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) { |
16 | 134 |
if ( $column === $column_name ) { |
0 | 135 |
return false; |
136 |
} |
|
137 |
} |
|
138 |
} |
|
139 |
} |
|
16 | 140 |
|
5 | 141 |
// Else didn't find it. |
0 | 142 |
return true; |
143 |
} |
|
144 |
||
145 |
/** |
|
16 | 146 |
* Checks that database table column matches the criteria. |
0 | 147 |
* |
148 |
* 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 |
|
150 |
* is returned by the SQL statement. Pass in null to skip checking that |
|
151 |
* criteria. |
|
152 |
* |
|
153 |
* Column names returned from DESC table are case sensitive and are listed: |
|
154 |
* Field |
|
155 |
* Type |
|
156 |
* Null |
|
157 |
* Key |
|
158 |
* Default |
|
159 |
* Extra |
|
160 |
* |
|
161 |
* @since 1.0.0 |
|
162 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
163 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
164 |
* |
19 | 165 |
* @param string $table_name Database table name. |
166 |
* @param string $col_name Table column name. |
|
167 |
* @param string $col_type Table column type. |
|
168 |
* @param bool $is_null Optional. Check is null. |
|
169 |
* @param mixed $key Optional. Key info. |
|
170 |
* @param mixed $default_value Optional. Default value. |
|
171 |
* @param mixed $extra Optional. Extra value. |
|
0 | 172 |
* @return bool True, if matches. False, if not matching. |
173 |
*/ |
|
19 | 174 |
function check_column( $table_name, $col_name, $col_type, $is_null = null, $key = null, $default_value = null, $extra = null ) { |
0 | 175 |
global $wpdb; |
16 | 176 |
|
9 | 177 |
$diffs = 0; |
178 |
$results = $wpdb->get_results( "DESC $table_name" ); |
|
0 | 179 |
|
9 | 180 |
foreach ( $results as $row ) { |
0 | 181 |
|
16 | 182 |
if ( $row->Field === $col_name ) { |
5 | 183 |
|
184 |
// Got our column, check the params. |
|
16 | 185 |
if ( ( null !== $col_type ) && ( $row->Type !== $col_type ) ) { |
0 | 186 |
++$diffs; |
187 |
} |
|
16 | 188 |
if ( ( null !== $is_null ) && ( $row->Null !== $is_null ) ) { |
0 | 189 |
++$diffs; |
190 |
} |
|
16 | 191 |
if ( ( null !== $key ) && ( $row->Key !== $key ) ) { |
0 | 192 |
++$diffs; |
193 |
} |
|
19 | 194 |
if ( ( null !== $default_value ) && ( $row->Default !== $default_value ) ) { |
0 | 195 |
++$diffs; |
196 |
} |
|
16 | 197 |
if ( ( null !== $extra ) && ( $row->Extra !== $extra ) ) { |
0 | 198 |
++$diffs; |
199 |
} |
|
16 | 200 |
|
9 | 201 |
if ( $diffs > 0 ) { |
0 | 202 |
return false; |
203 |
} |
|
16 | 204 |
|
0 | 205 |
return true; |
16 | 206 |
} // End if found our column. |
0 | 207 |
} |
16 | 208 |
|
0 | 209 |
return false; |
210 |
} |