| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) or die( 'Cheatin\' uh?' ); |
| 3 |
|
| 4 |
/** |
| 5 |
* DB base class |
| 6 |
* |
| 7 |
* @source https://gist.github.com/pippinsplugins/e220a7f0f0f2fbe64608 |
| 8 |
* @since 1.5 |
| 9 |
*/ |
| 10 |
abstract class Imagify_Abstract_DB { |
| 11 |
|
| 12 |
/** |
| 13 |
* The name of our database table |
| 14 |
* |
| 15 |
* @access public |
| 16 |
* @since 1.5 |
| 17 |
*/ |
| 18 |
public $table_name; |
| 19 |
|
| 20 |
/** |
| 21 |
* The version of our database table |
| 22 |
* |
| 23 |
* @access public |
| 24 |
* @since 1.5 |
| 25 |
*/ |
| 26 |
public $version; |
| 27 |
|
| 28 |
/** |
| 29 |
* The name of the primary column |
| 30 |
* |
| 31 |
* @access public |
| 32 |
* @since 1.5 |
| 33 |
*/ |
| 34 |
public $primary_key; |
| 35 |
|
| 36 |
/** |
| 37 |
* Get things started |
| 38 |
* |
| 39 |
* @access public |
| 40 |
* @since 1.5 |
| 41 |
*/ |
| 42 |
public function __construct() {} |
| 43 |
|
| 44 |
/** |
| 45 |
* Whitelist of columns |
| 46 |
* |
| 47 |
* @access public |
| 48 |
* @since 1.5 |
| 49 |
* @return array |
| 50 |
*/ |
| 51 |
public function get_columns() { |
| 52 |
return array(); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Default column values |
| 57 |
* |
| 58 |
* @access public |
| 59 |
* @since 1.5 |
| 60 |
* @return array |
| 61 |
*/ |
| 62 |
public function get_column_defaults() { |
| 63 |
return array(); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Retrieve a row by the primary key |
| 68 |
* |
| 69 |
* @access public |
| 70 |
* @since 1.5 |
| 71 |
* @return object |
| 72 |
*/ |
| 73 |
public function get( $row_id ) { |
| 74 |
global $wpdb; |
| 75 |
return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->table_name WHERE $this->primary_key = %s LIMIT 1;", $row_id ), ARRAY_A ); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Retrieve a row by a specific column / value |
| 80 |
* |
| 81 |
* @access public |
| 82 |
* @since 1.5 |
| 83 |
* @return object |
| 84 |
*/ |
| 85 |
public function get_by( $column, $row_id ) { |
| 86 |
global $wpdb; |
| 87 |
$column = esc_sql( $column ); |
| 88 |
return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->table_name WHERE $column = %s LIMIT 1;", $row_id ), ARRAY_A ); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Retrieve a specific column's value by the primary key |
| 93 |
* |
| 94 |
* @access public |
| 95 |
* @since 1.5 |
| 96 |
* @return string |
| 97 |
*/ |
| 98 |
public function get_column( $column, $row_id ) { |
| 99 |
global $wpdb; |
| 100 |
$column = esc_sql( $column ); |
| 101 |
return $wpdb->get_var( $wpdb->prepare( "SELECT $column FROM $this->table_name WHERE $this->primary_key = %s LIMIT 1;", $row_id ) ); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Retrieve a specific column's value by the the specified column / value |
| 106 |
* |
| 107 |
* @access public |
| 108 |
* @since 1.5 |
| 109 |
* @return string |
| 110 |
*/ |
| 111 |
public function get_column_by( $column, $column_where, $column_value ) { |
| 112 |
global $wpdb; |
| 113 |
$column_where = esc_sql( $column_where ); |
| 114 |
$column = esc_sql( $column ); |
| 115 |
return $wpdb->get_var( $wpdb->prepare( "SELECT $column FROM $this->table_name WHERE $column_where = %s LIMIT 1;", $column_value ) ); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Insert a new row |
| 120 |
* |
| 121 |
* @access public |
| 122 |
* @since 1.5 |
| 123 |
* @return int |
| 124 |
*/ |
| 125 |
public function insert( $data ) { |
| 126 |
global $wpdb; |
| 127 |
|
| 128 |
// Set default values |
| 129 |
$data = wp_parse_args( $data, $this->get_column_defaults() ); |
| 130 |
|
| 131 |
// Initialise column format array |
| 132 |
$column_formats = $this->get_columns(); |
| 133 |
|
| 134 |
// Force fields to lower case |
| 135 |
$data = array_change_key_case( $data ); |
| 136 |
|
| 137 |
// White list columns |
| 138 |
$data = array_intersect_key( $data, $column_formats ); |
| 139 |
|
| 140 |
// Reorder $column_formats to match the order of columns given in $data |
| 141 |
$data_keys = array_keys( $data ); |
| 142 |
$column_formats = array_merge( array_flip( $data_keys ), $column_formats ); |
| 143 |
|
| 144 |
$wpdb->insert( $this->table_name, $data, $column_formats ); |
| 145 |
|
| 146 |
return $wpdb->insert_id; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Update a row |
| 151 |
* |
| 152 |
* @access public |
| 153 |
* @since 1.5 |
| 154 |
* @return bool |
| 155 |
*/ |
| 156 |
public function update( $row_id, $data = array(), $where = '' ) { |
| 157 |
global $wpdb; |
| 158 |
|
| 159 |
// Row ID must be positive integer |
| 160 |
$row_id = absint( $row_id ); |
| 161 |
|
| 162 |
if( empty( $row_id ) ) { |
| 163 |
return false; |
| 164 |
} |
| 165 |
|
| 166 |
if( empty( $where ) ) { |
| 167 |
$where = $this->primary_key; |
| 168 |
} |
| 169 |
|
| 170 |
// Initialise column format array |
| 171 |
$column_formats = $this->get_columns(); |
| 172 |
|
| 173 |
// Force fields to lower case |
| 174 |
$data = array_change_key_case( $data ); |
| 175 |
|
| 176 |
// White list columns |
| 177 |
$data = array_intersect_key( $data, $column_formats ); |
| 178 |
|
| 179 |
// Reorder $column_formats to match the order of columns given in $data |
| 180 |
$data_keys = array_keys( $data ); |
| 181 |
$column_formats = array_merge( array_flip( $data_keys ), $column_formats ); |
| 182 |
|
| 183 |
if ( false === (bool) $this->get_column( $this->primary_key, $row_id ) ) { |
| 184 |
$this->insert( $data ); |
| 185 |
return true; |
| 186 |
} |
| 187 |
|
| 188 |
if ( false === $wpdb->update( $this->table_name, $data, array( $where => $row_id ), $column_formats ) ) { |
| 189 |
return false; |
| 190 |
} |
| 191 |
|
| 192 |
return true; |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Delete a row identified by the primary key |
| 197 |
* |
| 198 |
* @access public |
| 199 |
* @since 1.5 |
| 200 |
* @return bool |
| 201 |
*/ |
| 202 |
public function delete( $row_id = 0 ) { |
| 203 |
global $wpdb; |
| 204 |
|
| 205 |
// Row ID must be positive integer |
| 206 |
$row_id = absint( $row_id ); |
| 207 |
|
| 208 |
if( empty( $row_id ) ) { |
| 209 |
return false; |
| 210 |
} |
| 211 |
|
| 212 |
if ( false === $wpdb->query( $wpdb->prepare( "DELETE FROM $this->table_name WHERE $this->primary_key = %d", $row_id ) ) ) { |
| 213 |
return false; |
| 214 |
} |
| 215 |
|
| 216 |
return true; |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Check if the given table exists |
| 221 |
* |
| 222 |
* @since 1.5 |
| 223 |
* @param string $table The table name |
| 224 |
* @return bool If the table name exists |
| 225 |
*/ |
| 226 |
public function table_exists( $table ) { |
| 227 |
global $wpdb; |
| 228 |
$table = sanitize_text_field( $table ); |
| 229 |
|
| 230 |
return $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE '%s'", $table ) ) === $table; |
| 231 |
} |
| 232 |
|
| 233 |
} |