mp-wp_genesis 1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * Plugins may load this file to gain access to special helper functions for
mp-wp_genesis 4 * plugin installation. This file is not included by WordPress and it is
mp-wp_genesis 5 * recommended, to prevent fatal errors, that this file is included using
mp-wp_genesis 6 * require_once().
mp-wp_genesis 7 *
mp-wp_genesis 8 * These functions are not optimized for speed, but they should only be used
mp-wp_genesis 9 * once in a while, so speed shouldn't be a concern. If it is and you are
mp-wp_genesis 10 * needing to use these functions a lot, you might experience time outs. If you
mp-wp_genesis 11 * do, then it is advised to just write the SQL code yourself.
mp-wp_genesis 12 *
mp-wp_genesis 13 * You can turn debugging on, by setting $debug to 1 after you include this
mp-wp_genesis 14 * file.
mp-wp_genesis 15 *
mp-wp_genesis 16 * <code>
mp-wp_genesis 17 * check_column('wp_links', 'link_description', 'mediumtext');
mp-wp_genesis 18 * if (check_column($wpdb->comments, 'comment_author', 'tinytext'))
mp-wp_genesis 19 * echo "ok\n";
mp-wp_genesis 20 *
mp-wp_genesis 21 * $error_count = 0;
mp-wp_genesis 22 * $tablename = $wpdb->links;
mp-wp_genesis 23 * // check the column
mp-wp_genesis 24 * if (!check_column($wpdb->links, 'link_description', 'varchar(255)')) {
mp-wp_genesis 25 * $ddl = "ALTER TABLE $wpdb->links MODIFY COLUMN link_description varchar(255) NOT NULL DEFAULT '' ";
mp-wp_genesis 26 * $q = $wpdb->query($ddl);
mp-wp_genesis 27 * }
mp-wp_genesis 28 *
mp-wp_genesis 29 * if (check_column($wpdb->links, 'link_description', 'varchar(255)')) {
mp-wp_genesis 30 * $res .= $tablename . ' - ok <br />';
mp-wp_genesis 31 * } else {
mp-wp_genesis 32 * $res .= 'There was a problem with ' . $tablename . '<br />';
mp-wp_genesis 33 * ++$error_count;
mp-wp_genesis 34 * }
mp-wp_genesis 35 * </code>
mp-wp_genesis 36 *
mp-wp_genesis 37 * @package WordPress
mp-wp_genesis 38 * @subpackage Plugin
mp-wp_genesis 39 */
mp-wp_genesis 40
mp-wp_genesis 41 /**
mp-wp_genesis 42 * @global bool $wp_only_load_config
mp-wp_genesis 43 * @name $wp_only_load_config
mp-wp_genesis 44 * @var bool
mp-wp_genesis 45 * @since unknown
mp-wp_genesis 46 */
mp-wp_genesis 47 $wp_only_load_config = true;
mp-wp_genesis 48
mp-wp_genesis 49 /** Load WordPress Bootstrap */
mp-wp_genesis 50 require_once(dirname(dirname(__FILE__)).'/wp-load.php');
mp-wp_genesis 51
mp-wp_genesis 52 /**
mp-wp_genesis 53 * Turn debugging on or off.
mp-wp_genesis 54 * @global bool|int $debug
mp-wp_genesis 55 * @name $debug
mp-wp_genesis 56 * @var bool|int
mp-wp_genesis 57 * @since unknown
mp-wp_genesis 58 */
mp-wp_genesis 59 $debug = 0;
mp-wp_genesis 60
mp-wp_genesis 61 if ( ! function_exists('maybe_create_table') ) :
mp-wp_genesis 62 /**
mp-wp_genesis 63 * Create database table, if it doesn't already exist.
mp-wp_genesis 64 *
mp-wp_genesis 65 * @since unknown
mp-wp_genesis 66 * @package WordPress
mp-wp_genesis 67 * @subpackage Plugin
mp-wp_genesis 68 * @uses $wpdb
mp-wp_genesis 69 *
mp-wp_genesis 70 * @param string $table_name Database table name.
mp-wp_genesis 71 * @param string $create_ddl Create database table SQL.
mp-wp_genesis 72 * @return bool False on error, true if already exists or success.
mp-wp_genesis 73 */
mp-wp_genesis 74 function maybe_create_table($table_name, $create_ddl) {
mp-wp_genesis 75 global $wpdb;
mp-wp_genesis 76 foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
mp-wp_genesis 77 if ($table == $table_name) {
mp-wp_genesis 78 return true;
mp-wp_genesis 79 }
mp-wp_genesis 80 }
mp-wp_genesis 81 //didn't find it try to create it.
mp-wp_genesis 82 $wpdb->query($create_ddl);
mp-wp_genesis 83 // we cannot directly tell that whether this succeeded!
mp-wp_genesis 84 foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
mp-wp_genesis 85 if ($table == $table_name) {
mp-wp_genesis 86 return true;
mp-wp_genesis 87 }
mp-wp_genesis 88 }
mp-wp_genesis 89 return false;
mp-wp_genesis 90 }
mp-wp_genesis 91 endif;
mp-wp_genesis 92
mp-wp_genesis 93 if ( ! function_exists('maybe_add_column') ) :
mp-wp_genesis 94 /**
mp-wp_genesis 95 * Add column to database table, if column doesn't already exist in table.
mp-wp_genesis 96 *
mp-wp_genesis 97 * @since unknown
mp-wp_genesis 98 * @package WordPress
mp-wp_genesis 99 * @subpackage Plugin
mp-wp_genesis 100 * @uses $wpdb
mp-wp_genesis 101 * @uses $debug
mp-wp_genesis 102 *
mp-wp_genesis 103 * @param string $table_name Database table name
mp-wp_genesis 104 * @param string $column_name Table column name
mp-wp_genesis 105 * @param string $create_ddl SQL to add column to table.
mp-wp_genesis 106 * @return bool False on failure. True, if already exists or was successful.
mp-wp_genesis 107 */
mp-wp_genesis 108 function maybe_add_column($table_name, $column_name, $create_ddl) {
mp-wp_genesis 109 global $wpdb, $debug;
mp-wp_genesis 110 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
mp-wp_genesis 111 if ($debug) echo("checking $column == $column_name<br />");
mp-wp_genesis 112
mp-wp_genesis 113 if ($column == $column_name) {
mp-wp_genesis 114 return true;
mp-wp_genesis 115 }
mp-wp_genesis 116 }
mp-wp_genesis 117 //didn't find it try to create it.
mp-wp_genesis 118 $wpdb->query($create_ddl);
mp-wp_genesis 119 // we cannot directly tell that whether this succeeded!
mp-wp_genesis 120 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
mp-wp_genesis 121 if ($column == $column_name) {
mp-wp_genesis 122 return true;
mp-wp_genesis 123 }
mp-wp_genesis 124 }
mp-wp_genesis 125 return false;
mp-wp_genesis 126 }
mp-wp_genesis 127 endif;
mp-wp_genesis 128
mp-wp_genesis 129 /**
mp-wp_genesis 130 * Drop column from database table, if it exists.
mp-wp_genesis 131 *
mp-wp_genesis 132 * @since unknown
mp-wp_genesis 133 * @package WordPress
mp-wp_genesis 134 * @subpackage Plugin
mp-wp_genesis 135 * @uses $wpdb
mp-wp_genesis 136 *
mp-wp_genesis 137 * @param string $table_name Table name
mp-wp_genesis 138 * @param string $column_name Column name
mp-wp_genesis 139 * @param string $drop_ddl SQL statement to drop column.
mp-wp_genesis 140 * @return bool False on failure, true on success or doesn't exist.
mp-wp_genesis 141 */
mp-wp_genesis 142 function maybe_drop_column($table_name, $column_name, $drop_ddl) {
mp-wp_genesis 143 global $wpdb;
mp-wp_genesis 144 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
mp-wp_genesis 145 if ($column == $column_name) {
mp-wp_genesis 146 //found it try to drop it.
mp-wp_genesis 147 $wpdb->query($drop_ddl);
mp-wp_genesis 148 // we cannot directly tell that whether this succeeded!
mp-wp_genesis 149 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
mp-wp_genesis 150 if ($column == $column_name) {
mp-wp_genesis 151 return false;
mp-wp_genesis 152 }
mp-wp_genesis 153 }
mp-wp_genesis 154 }
mp-wp_genesis 155 }
mp-wp_genesis 156 // else didn't find it
mp-wp_genesis 157 return true;
mp-wp_genesis 158 }
mp-wp_genesis 159
mp-wp_genesis 160 /**
mp-wp_genesis 161 * Check column matches criteria.
mp-wp_genesis 162 *
mp-wp_genesis 163 * Uses the SQL DESC for retrieving the table info for the column. It will help
mp-wp_genesis 164 * understand the parameters, if you do more research on what column information
mp-wp_genesis 165 * is returned by the SQL statement. Pass in null to skip checking that
mp-wp_genesis 166 * criteria.
mp-wp_genesis 167 *
mp-wp_genesis 168 * Column names returned from DESC table are case sensitive and are listed:
mp-wp_genesis 169 * Field
mp-wp_genesis 170 * Type
mp-wp_genesis 171 * Null
mp-wp_genesis 172 * Key
mp-wp_genesis 173 * Default
mp-wp_genesis 174 * Extra
mp-wp_genesis 175 *
mp-wp_genesis 176 * @since unknown
mp-wp_genesis 177 * @package WordPress
mp-wp_genesis 178 * @subpackage Plugin
mp-wp_genesis 179 *
mp-wp_genesis 180 * @param string $table_name Table name
mp-wp_genesis 181 * @param string $col_name Column name
mp-wp_genesis 182 * @param string $col_type Column type
mp-wp_genesis 183 * @param bool $is_null Optional. Check is null.
mp-wp_genesis 184 * @param mixed $key Optional. Key info.
mp-wp_genesis 185 * @param mixed $default Optional. Default value.
mp-wp_genesis 186 * @param mixed $extra Optional. Extra value.
mp-wp_genesis 187 * @return bool True, if matches. False, if not matching.
mp-wp_genesis 188 */
mp-wp_genesis 189 function check_column($table_name, $col_name, $col_type, $is_null = null, $key = null, $default = null, $extra = null) {
mp-wp_genesis 190 global $wpdb, $debug;
mp-wp_genesis 191 $diffs = 0;
mp-wp_genesis 192 $results = $wpdb->get_results("DESC $table_name");
mp-wp_genesis 193
mp-wp_genesis 194 foreach ($results as $row ) {
mp-wp_genesis 195 if ($debug > 1) print_r($row);
mp-wp_genesis 196
mp-wp_genesis 197 if ($row->Field == $col_name) {
mp-wp_genesis 198 // got our column, check the params
mp-wp_genesis 199 if ($debug) echo ("checking $row->Type against $col_type\n");
mp-wp_genesis 200 if (($col_type != null) && ($row->Type != $col_type)) {
mp-wp_genesis 201 ++$diffs;
mp-wp_genesis 202 }
mp-wp_genesis 203 if (($is_null != null) && ($row->Null != $is_null)) {
mp-wp_genesis 204 ++$diffs;
mp-wp_genesis 205 }
mp-wp_genesis 206 if (($key != null) && ($row->Key != $key)) {
mp-wp_genesis 207 ++$diffs;
mp-wp_genesis 208 }
mp-wp_genesis 209 if (($default != null) && ($row->Default != $default)) {
mp-wp_genesis 210 ++$diffs;
mp-wp_genesis 211 }
mp-wp_genesis 212 if (($extra != null) && ($row->Extra != $extra)) {
mp-wp_genesis 213 ++$diffs;
mp-wp_genesis 214 }
mp-wp_genesis 215 if ($diffs > 0) {
mp-wp_genesis 216 if ($debug) echo ("diffs = $diffs returning false\n");
mp-wp_genesis 217 return false;
mp-wp_genesis 218 }
mp-wp_genesis 219 return true;
mp-wp_genesis 220 } // end if found our column
mp-wp_genesis 221 }
mp-wp_genesis 222 return false;
mp-wp_genesis 223 }
mp-wp_genesis 224
mp-wp_genesis 225 ?>