PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wordpress/wp-admin/install-helper.php

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