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
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.
14
* check_column('wp_links', 'link_description', 'mediumtext');
15
* if (check_column($wpdb->comments, 'comment_author', 'tinytext'))
19
* $tablename = $wpdb->links;
21
* if (!check_column($wpdb->links, 'link_description', 'varchar(255)')) {
22
* $ddl = "ALTER TABLE $wpdb->links MODIFY COLUMN link_description varchar(255) NOT NULL DEFAULT '' ";
23
* $q = $wpdb->query($ddl);
26
* if (check_column($wpdb->links, 'link_description', 'varchar(255)')) {
27
* $res .= $tablename . ' - ok <br />';
29
* $res .= 'There was a problem with ' . $tablename . '<br />';
38
/** Load WordPress Bootstrap */
39
require_once(dirname(dirname(__FILE__)).'/wp-load.php');
41
if ( ! function_exists('maybe_create_table') ) :
43
* Create database table, if it doesn't already exist.
49
* @param string $table_name Database table name.
50
* @param string $create_ddl Create database table SQL.
51
* @return bool False on error, true if already exists or success.
53
function maybe_create_table($table_name, $create_ddl) {
55
foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
56
if ($table == $table_name) {
60
// Didn't find it, so try to create it.
61
$wpdb->query($create_ddl);
63
// We cannot directly tell that whether this succeeded!
64
foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
65
if ($table == $table_name) {
73
if ( ! function_exists('maybe_add_column') ) :
75
* Add column to database table, if column doesn't already exist in table.
81
* @param string $table_name Database table name
82
* @param string $column_name Table column name
83
* @param string $create_ddl SQL to add column to table.
84
* @return bool False on failure. True, if already exists or was successful.
86
function maybe_add_column($table_name, $column_name, $create_ddl) {
88
foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
90
if ($column == $column_name) {
95
// Didn't find it, so try to create it.
96
$wpdb->query($create_ddl);
98
// We cannot directly tell that whether this succeeded!
99
foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
100
if ($column == $column_name) {
109
* Drop column from database table, if it exists.
115
* @param string $table_name Table name
116
* @param string $column_name Column name
117
* @param string $drop_ddl SQL statement to drop column.
118
* @return bool False on failure, true on success or doesn't exist.
120
function maybe_drop_column($table_name, $column_name, $drop_ddl) {
122
foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
123
if ($column == $column_name) {
125
// Found it, so try to drop it.
126
$wpdb->query($drop_ddl);
128
// We cannot directly tell that whether this succeeded!
129
foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
130
if ($column == $column_name) {
136
// Else didn't find it.
141
* Check column matches criteria.
143
* Uses the SQL DESC for retrieving the table info for the column. It will help
144
* understand the parameters, if you do more research on what column information
145
* is returned by the SQL statement. Pass in null to skip checking that
148
* Column names returned from DESC table are case sensitive and are listed:
158
* @param string $table_name Table name
159
* @param string $col_name Column name
160
* @param string $col_type Column type
161
* @param bool $is_null Optional. Check is null.
162
* @param mixed $key Optional. Key info.
163
* @param mixed $default Optional. Default value.
164
* @param mixed $extra Optional. Extra value.
165
* @return bool True, if matches. False, if not matching.
167
function check_column($table_name, $col_name, $col_type, $is_null = null, $key = null, $default = null, $extra = null) {
170
$results = $wpdb->get_results("DESC $table_name");
172
foreach ($results as $row ) {
174
if ($row->Field == $col_name) {
176
// Got our column, check the params.
177
if (($col_type != null) && ($row->Type != $col_type)) {
180
if (($is_null != null) && ($row->Null != $is_null)) {
183
if (($key != null) && ($row->Key != $key)) {
186
if (($default != null) && ($row->Default != $default)) {
189
if (($extra != null) && ($row->Extra != $extra)) {
196
} // end if found our column