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