[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-admin/ -> install-helper.php (source)

   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
   6   * require_once.
   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   *
  13   *     check_column( 'wp_links', 'link_description', 'mediumtext' );
  14   *     if ( check_column( $wpdb->comments, 'comment_author', 'tinytext' ) ) {
  15   *         echo "ok\n";
  16   *     }
  17   *
  18   *     $error_count = 0;
  19   *     $tablename = $wpdb->links;
  20   *     // Check the column.
  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 );
  24   *     }
  25   *
  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   *     }
  32   *
  33   * @package WordPress
  34   * @subpackage Plugin
  35   */
  36  
  37  /** Load WordPress Bootstrap */
  38  require_once dirname( __DIR__ ) . '/wp-load.php';
  39  
  40  if ( ! function_exists( 'maybe_create_table' ) ) :
  41      /**
  42       * Creates a table in the database if it doesn't already exist.
  43       *
  44       * @since 1.0.0
  45       *
  46       * @global wpdb $wpdb WordPress database abstraction object.
  47       *
  48       * @param string $table_name Database table name.
  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.
  51       */
  52  	function maybe_create_table( $table_name, $create_ddl ) {
  53          global $wpdb;
  54  
  55          foreach ( $wpdb->get_col( 'SHOW TABLES', 0 ) as $table ) {
  56              if ( $table === $table_name ) {
  57                  return true;
  58              }
  59          }
  60  
  61          // Didn't find it, so try to create it.
  62          $wpdb->query( $create_ddl );
  63  
  64          // We cannot directly tell that whether this succeeded!
  65          foreach ( $wpdb->get_col( 'SHOW TABLES', 0 ) as $table ) {
  66              if ( $table === $table_name ) {
  67                  return true;
  68              }
  69          }
  70  
  71          return false;
  72      }
  73  endif;
  74  
  75  if ( ! function_exists( 'maybe_add_column' ) ) :
  76      /**
  77       * Adds column to database table, if it doesn't already exist.
  78       *
  79       * @since 1.0.0
  80       *
  81       * @global wpdb $wpdb WordPress database abstraction object.
  82       *
  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.
  87       */
  88  	function maybe_add_column( $table_name, $column_name, $create_ddl ) {
  89          global $wpdb;
  90  
  91          foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) {
  92              if ( $column === $column_name ) {
  93                  return true;
  94              }
  95          }
  96  
  97          // Didn't find it, so try to create it.
  98          $wpdb->query( $create_ddl );
  99  
 100          // We cannot directly tell that whether this succeeded!
 101          foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) {
 102              if ( $column === $column_name ) {
 103                  return true;
 104              }
 105          }
 106  
 107          return false;
 108      }
 109  endif;
 110  
 111  /**
 112   * Drops column from database table, if it exists.
 113   *
 114   * @since 1.0.0
 115   *
 116   * @global wpdb $wpdb WordPress database abstraction object.
 117   *
 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.
 122   */
 123  function maybe_drop_column( $table_name, $column_name, $drop_ddl ) {
 124      global $wpdb;
 125  
 126      foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) {
 127          if ( $column === $column_name ) {
 128  
 129              // Found it, so try to drop it.
 130              $wpdb->query( $drop_ddl );
 131  
 132              // We cannot directly tell that whether this succeeded!
 133              foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) {
 134                  if ( $column === $column_name ) {
 135                      return false;
 136                  }
 137              }
 138          }
 139      }
 140  
 141      // Else didn't find it.
 142      return true;
 143  }
 144  
 145  /**
 146   * Checks that database table column matches the criteria.
 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   *
 163   * @global wpdb $wpdb WordPress database abstraction object.
 164   *
 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.
 172   * @return bool True, if matches. False, if not matching.
 173   */
 174  function check_column( $table_name, $col_name, $col_type, $is_null = null, $key = null, $default_value = null, $extra = null ) {
 175      global $wpdb;
 176  
 177      $diffs   = 0;
 178      $results = $wpdb->get_results( "DESC $table_name" );
 179  
 180      foreach ( $results as $row ) {
 181  
 182          if ( $row->Field === $col_name ) {
 183  
 184              // Got our column, check the params.
 185              if ( ( null !== $col_type ) && ( $row->Type !== $col_type ) ) {
 186                  ++$diffs;
 187              }
 188              if ( ( null !== $is_null ) && ( $row->Null !== $is_null ) ) {
 189                  ++$diffs;
 190              }
 191              if ( ( null !== $key ) && ( $row->Key !== $key ) ) {
 192                  ++$diffs;
 193              }
 194              if ( ( null !== $default_value ) && ( $row->Default !== $default_value ) ) {
 195                  ++$diffs;
 196              }
 197              if ( ( null !== $extra ) && ( $row->Extra !== $extra ) ) {
 198                  ++$diffs;
 199              }
 200  
 201              if ( $diffs > 0 ) {
 202                  return false;
 203              }
 204  
 205              return true;
 206          } // End if found our column.
 207      }
 208  
 209      return false;
 210  }


Generated: Sat Dec 21 01:00:02 2024 Cross-referenced by PHPXref 0.7.1