| 1 |
<?php |
| 2 |
/** |
| 3 |
* Charitable DB base class. |
| 4 |
* |
| 5 |
* Credit: This is based on Easy Digital Downloads' EDD_DB class. Major props to Pippin Williamson. |
| 6 |
* https://github.com/easydigitaldownloads/Easy-Digital-Downloads/blob/master/includes/class-edd-db.php |
| 7 |
* |
| 8 |
* @package Charitable/Classes/Charitable_DB |
| 9 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 10 |
* @version 1.0.0 |
| 11 |
*/ |
| 12 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 13 |
|
| 14 |
if ( ! class_exists( 'Charitable_DB' ) ) : |
| 15 |
|
| 16 |
/** |
| 17 |
* Charitable_DB |
| 18 |
* |
| 19 |
* @abstract |
| 20 |
* @since 1.0.0 |
| 21 |
*/ |
| 22 |
abstract class Charitable_DB { |
| 23 |
|
| 24 |
/** |
| 25 |
* The name of our database table |
| 26 |
* |
| 27 |
* @access public |
| 28 |
* @since 1.0.0 |
| 29 |
*/ |
| 30 |
public $table_name; |
| 31 |
|
| 32 |
/** |
| 33 |
* The version of our database table |
| 34 |
* |
| 35 |
* @access public |
| 36 |
* @since 1.0.0 |
| 37 |
*/ |
| 38 |
public $version; |
| 39 |
|
| 40 |
/** |
| 41 |
* The name of the primary column |
| 42 |
* |
| 43 |
* @access public |
| 44 |
* @since 1.0.0 |
| 45 |
*/ |
| 46 |
public $primary_key; |
| 47 |
|
| 48 |
/** |
| 49 |
* Get things started |
| 50 |
* |
| 51 |
* @access public |
| 52 |
* @since 1.0.0 |
| 53 |
*/ |
| 54 |
public function __construct() {} |
| 55 |
|
| 56 |
/** |
| 57 |
* Whitelist of columns |
| 58 |
* |
| 59 |
* @return array |
| 60 |
* @abstract |
| 61 |
* @access public |
| 62 |
* @since 1.0.0 |
| 63 |
*/ |
| 64 |
abstract public function get_columns(); |
| 65 |
|
| 66 |
/** |
| 67 |
* Default column values |
| 68 |
* |
| 69 |
* @return array |
| 70 |
* @abstract |
| 71 |
* @access public |
| 72 |
* @since 1.0.0 |
| 73 |
*/ |
| 74 |
abstract public function get_column_defaults(); |
| 75 |
|
| 76 |
/** |
| 77 |
* Return the format for the given column. |
| 78 |
* |
| 79 |
* @param string $column |
| 80 |
* @return %s, %d or %f |
| 81 |
* @access public |
| 82 |
* @since 1.0.0 |
| 83 |
*/ |
| 84 |
public function get_column_format($column) { |
| 85 |
$columns = $this->get_columns(); |
| 86 |
$format = isset( $columns[$column] ) ? $columns[$column] : false; |
| 87 |
|
| 88 |
// If the column isn't found, throw an exception. |
| 89 |
if ( false === $format ) { |
| 90 |
throw new Exception( sprintf( 'Invalid column passed %s', $column ) ); |
| 91 |
} |
| 92 |
|
| 93 |
// If the column format isn't valid, throw an exception. |
| 94 |
if ( ! in_array( $format, array( '%s', '%d', '%f' ) ) ) { |
| 95 |
throw new Exception( sprintf( 'Invalid column format for column %s. Format returned %s', $column, $format ) ); |
| 96 |
} |
| 97 |
|
| 98 |
return $format; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Retrieve a row by the primary key |
| 103 |
* |
| 104 |
* @access public |
| 105 |
* @since 1.0.0 |
| 106 |
* @return object |
| 107 |
*/ |
| 108 |
public function get( $row_id ) { |
| 109 |
global $wpdb; |
| 110 |
return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->table_name WHERE $this->primary_key = %d LIMIT 1;", $row_id ) ); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Retrieve a row by a specific column / value |
| 115 |
* |
| 116 |
* @access public |
| 117 |
* @since 1.0.0 |
| 118 |
* @return object |
| 119 |
*/ |
| 120 |
public function get_by( $column, $row_id ) { |
| 121 |
global $wpdb; |
| 122 |
return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->table_name WHERE $column = {$this->get_column_format($column)} LIMIT 1;", $row_id ) ); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Retrieve a specific column's value by the primary key |
| 127 |
* |
| 128 |
* @access public |
| 129 |
* @since 1.0.0 |
| 130 |
* @return string |
| 131 |
*/ |
| 132 |
public function get_column( $column, $row_id ) { |
| 133 |
global $wpdb; |
| 134 |
return $wpdb->get_var( $wpdb->prepare( "SELECT $column FROM $this->table_name WHERE $this->primary_key = %d LIMIT 1;", $row_id ) ); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Retrieve a specific column's value by the the specified column / value |
| 139 |
* |
| 140 |
* @access public |
| 141 |
* @since 1.0.0 |
| 142 |
* @return string |
| 143 |
*/ |
| 144 |
public function get_column_by( $column, $column_where, $column_value ) { |
| 145 |
global $wpdb; |
| 146 |
return $wpdb->get_var( $wpdb->prepare( "SELECT $column FROM $this->table_name WHERE $column_where = {$this->get_column_format($column_where)} LIMIT 1;", $column_value ) ); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Count all rows. |
| 151 |
* |
| 152 |
* @return int |
| 153 |
* @access public |
| 154 |
* @since 1.0.0 |
| 155 |
*/ |
| 156 |
public function count_all() { |
| 157 |
global $wpdb; |
| 158 |
return $wpdb->get_var( "SELECT COUNT( * ) FROM $this->table_name;" ); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Count all rows that certain criteria. |
| 163 |
* |
| 164 |
* @return int |
| 165 |
* @access public |
| 166 |
* @since 1.0.0 |
| 167 |
*/ |
| 168 |
public function count_by( $column, $column_value ) { |
| 169 |
global $wpdb; |
| 170 |
return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( * ) FROM $this->table_name WHERE $column = {$this->get_column_format($column)};", $column_value ) ); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Insert a new row |
| 175 |
* |
| 176 |
* @access public |
| 177 |
* @since 1.0.0 |
| 178 |
* @return int |
| 179 |
*/ |
| 180 |
public function insert( $data, $type = '' ) { |
| 181 |
global $wpdb; |
| 182 |
|
| 183 |
/* Set default values */ |
| 184 |
$data = wp_parse_args( $data, $this->get_column_defaults() ); |
| 185 |
|
| 186 |
do_action( 'charitable_pre_insert_' . $type, $data ); |
| 187 |
|
| 188 |
/* Initialise column format array */ |
| 189 |
$column_formats = $this->get_columns(); |
| 190 |
|
| 191 |
/* Force fields to lower case */ |
| 192 |
$data = array_change_key_case( $data ); |
| 193 |
|
| 194 |
/* White list columns */ |
| 195 |
$data = array_intersect_key( $data, $column_formats ); |
| 196 |
|
| 197 |
/* Reorder $column_formats to match the order of columns given in $data */ |
| 198 |
$data_keys = array_keys( $data ); |
| 199 |
$column_formats = array_merge( array_flip( $data_keys ), $column_formats ); |
| 200 |
|
| 201 |
$inserted = $wpdb->insert( $this->table_name, $data, $column_formats ); |
| 202 |
|
| 203 |
/* If the insert failed, return 0 */ |
| 204 |
if ( false === $inserted ) { |
| 205 |
return 0; |
| 206 |
} |
| 207 |
|
| 208 |
do_action( 'charitable_post_insert_' . $type, $wpdb->insert_id, $data ); |
| 209 |
|
| 210 |
return $wpdb->insert_id; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Update a row |
| 215 |
* |
| 216 |
* @global WPDB $wpdb |
| 217 |
* @param int $row_id |
| 218 |
* @param array $data |
| 219 |
* @param string $where Column used in where argument. |
| 220 |
* @access public |
| 221 |
* @since 1.0.0 |
| 222 |
* @return bool |
| 223 |
*/ |
| 224 |
public function update( $row_id, $data = array(), $where = '' ) { |
| 225 |
global $wpdb; |
| 226 |
|
| 227 |
// Row ID must be positive integer |
| 228 |
$row_id = absint( $row_id ); |
| 229 |
|
| 230 |
if( empty( $row_id ) ) { |
| 231 |
return false; |
| 232 |
} |
| 233 |
|
| 234 |
if( empty( $where ) ) { |
| 235 |
$where = $this->primary_key; |
| 236 |
} |
| 237 |
|
| 238 |
// Initialise column format array |
| 239 |
$column_formats = $this->get_columns(); |
| 240 |
|
| 241 |
// Force fields to lower case |
| 242 |
$data = array_change_key_case( $data ); |
| 243 |
|
| 244 |
// White list columns |
| 245 |
$data = array_intersect_key( $data, $column_formats ); |
| 246 |
|
| 247 |
// Reorder $column_formats to match the order of columns given in $data |
| 248 |
$data_keys = array_keys( $data ); |
| 249 |
$column_formats = array_merge( array_flip( $data_keys ), $column_formats ); |
| 250 |
|
| 251 |
if ( false === $wpdb->update( $this->table_name, $data, array( $where => $row_id ), $column_formats ) ) { |
| 252 |
return false; |
| 253 |
} |
| 254 |
|
| 255 |
return true; |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Delete a row identified by the primary key |
| 260 |
* |
| 261 |
* @param int $row_id |
| 262 |
* @access public |
| 263 |
* @since 1.0.0 |
| 264 |
* @return bool |
| 265 |
*/ |
| 266 |
public function delete( $row_id = 0 ) { |
| 267 |
// Row ID must be positive integer |
| 268 |
$row_id = absint( $row_id ); |
| 269 |
|
| 270 |
if( empty( $row_id ) ) { |
| 271 |
return false; |
| 272 |
} |
| 273 |
|
| 274 |
return $this->delete_by( $this->primary_key, $row_id ); |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Delete a row identified by a specific column. |
| 279 |
* |
| 280 |
* @global WPDB $wpdb |
| 281 |
* @param string $column |
| 282 |
* @param mixed $row_id |
| 283 |
* @access public |
| 284 |
* @since 1.2.0 |
| 285 |
* @return bool |
| 286 |
*/ |
| 287 |
public function delete_by( $column, $row_id = 0 ) { |
| 288 |
global $wpdb; |
| 289 |
|
| 290 |
$result = $wpdb->query( $wpdb->prepare( "DELETE FROM $this->table_name WHERE $column = {$this->get_column_format($column)}", $row_id ) ); |
| 291 |
|
| 292 |
return false !== $result; |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Create the table. |
| 297 |
* |
| 298 |
* @global $wpdb |
| 299 |
* @param string $sql |
| 300 |
* @access protected |
| 301 |
* @since 1.0.0 |
| 302 |
*/ |
| 303 |
protected function _create_table( $sql ) { |
| 304 |
global $wpdb; |
| 305 |
|
| 306 |
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 307 |
|
| 308 |
dbDelta( $sql ); |
| 309 |
|
| 310 |
update_option( $this->table_name . '_db_version', $this->version ); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
endif; |