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