install-helper.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. /** Load WordPress Bootstrap */
  37. require_once dirname( __DIR__ ) . '/wp-load.php';
  38. if ( ! function_exists( 'maybe_create_table' ) ) :
  39. /**
  40. * Creates a table in the database if it doesn't already exist.
  41. *
  42. * @since 1.0.0
  43. *
  44. * @global wpdb $wpdb WordPress database abstraction object.
  45. *
  46. * @param string $table_name Database table name.
  47. * @param string $create_ddl SQL statement to create table.
  48. * @return bool True on success or if the table already exists. False on failure.
  49. */
  50. function maybe_create_table( $table_name, $create_ddl ) {
  51. global $wpdb;
  52. foreach ( $wpdb->get_col( 'SHOW TABLES', 0 ) as $table ) {
  53. if ( $table === $table_name ) {
  54. return true;
  55. }
  56. }
  57. // Didn't find it, so try to create it.
  58. $wpdb->query( $create_ddl );
  59. // We cannot directly tell that whether this succeeded!
  60. foreach ( $wpdb->get_col( 'SHOW TABLES', 0 ) as $table ) {
  61. if ( $table === $table_name ) {
  62. return true;
  63. }
  64. }
  65. return false;
  66. }
  67. endif;
  68. if ( ! function_exists( 'maybe_add_column' ) ) :
  69. /**
  70. * Adds column to database table, if it doesn't already exist.
  71. *
  72. * @since 1.0.0
  73. *
  74. * @global wpdb $wpdb WordPress database abstraction object.
  75. *
  76. * @param string $table_name Database table name.
  77. * @param string $column_name Table column name.
  78. * @param string $create_ddl SQL statement to add column.
  79. * @return bool True on success or if the column already exists. False on failure.
  80. */
  81. function maybe_add_column( $table_name, $column_name, $create_ddl ) {
  82. global $wpdb;
  83. foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) {
  84. if ( $column === $column_name ) {
  85. return true;
  86. }
  87. }
  88. // Didn't find it, so try to create it.
  89. $wpdb->query( $create_ddl );
  90. // We cannot directly tell that whether this succeeded!
  91. foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) {
  92. if ( $column === $column_name ) {
  93. return true;
  94. }
  95. }
  96. return false;
  97. }
  98. endif;
  99. /**
  100. * Drops column from database table, if it exists.
  101. *
  102. * @since 1.0.0
  103. *
  104. * @global wpdb $wpdb WordPress database abstraction object.
  105. *
  106. * @param string $table_name Database table name.
  107. * @param string $column_name Table column name.
  108. * @param string $drop_ddl SQL statement to drop column.
  109. * @return bool True on success or if the column doesn't exist. False on failure.
  110. */
  111. function maybe_drop_column( $table_name, $column_name, $drop_ddl ) {
  112. global $wpdb;
  113. foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) {
  114. if ( $column === $column_name ) {
  115. // Found it, so try to drop it.
  116. $wpdb->query( $drop_ddl );
  117. // We cannot directly tell that whether this succeeded!
  118. foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) {
  119. if ( $column === $column_name ) {
  120. return false;
  121. }
  122. }
  123. }
  124. }
  125. // Else didn't find it.
  126. return true;
  127. }
  128. /**
  129. * Checks that database table column matches the criteria.
  130. *
  131. * Uses the SQL DESC for retrieving the table info for the column. It will help
  132. * understand the parameters, if you do more research on what column information
  133. * is returned by the SQL statement. Pass in null to skip checking that
  134. * criteria.
  135. *
  136. * Column names returned from DESC table are case sensitive and are listed:
  137. * Field
  138. * Type
  139. * Null
  140. * Key
  141. * Default
  142. * Extra
  143. *
  144. * @since 1.0.0
  145. *
  146. * @global wpdb $wpdb WordPress database abstraction object.
  147. *
  148. * @param string $table_name Database table name.
  149. * @param string $col_name Table column name.
  150. * @param string $col_type Table column type.
  151. * @param bool $is_null Optional. Check is null.
  152. * @param mixed $key Optional. Key info.
  153. * @param mixed $default_value Optional. Default value.
  154. * @param mixed $extra Optional. Extra value.
  155. * @return bool True, if matches. False, if not matching.
  156. */
  157. function check_column( $table_name, $col_name, $col_type, $is_null = null, $key = null, $default_value = null, $extra = null ) {
  158. global $wpdb;
  159. $diffs = 0;
  160. $results = $wpdb->get_results( "DESC $table_name" );
  161. foreach ( $results as $row ) {
  162. if ( $row->Field === $col_name ) {
  163. // Got our column, check the params.
  164. if ( ( null !== $col_type ) && ( $row->Type !== $col_type ) ) {
  165. ++$diffs;
  166. }
  167. if ( ( null !== $is_null ) && ( $row->Null !== $is_null ) ) {
  168. ++$diffs;
  169. }
  170. if ( ( null !== $key ) && ( $row->Key !== $key ) ) {
  171. ++$diffs;
  172. }
  173. if ( ( null !== $default_value ) && ( $row->Default !== $default_value ) ) {
  174. ++$diffs;
  175. }
  176. if ( ( null !== $extra ) && ( $row->Extra !== $extra ) ) {
  177. ++$diffs;
  178. }
  179. if ( $diffs > 0 ) {
  180. return false;
  181. }
  182. return true;
  183. } // End if found our column.
  184. }
  185. return false;
  186. }