28 const PUBLISH =
'publish';
29 const DRAFT =
'draft';
30 const TRASH =
'trash';
39 public $__version =
'1.0.1';
49 public static function statuses()
52 self::ALL => __(
'All', WPDK_TEXTDOMAIN ),
53 self::DRAFT => __(
'Draft', WPDK_TEXTDOMAIN ),
54 self::PUBLISH => __(
'Publish', WPDK_TEXTDOMAIN ),
55 self::TRASH => __(
'Trash', WPDK_TEXTDOMAIN ),
71 public static function sanitizeStatus( $status, $statuses = null )
73 $status = esc_attr( $status );
74 $statuses = is_null( $statuses ) ? self::statuses() : $statuses;
75 $allowed = array_keys( $statuses );
76 if ( !in_array( $status, $allowed ) ) {
138 public function __construct( $table_name, $sql_file =
'' )
143 $this->tableName = sprintf(
'%s%s', $wpdb->prefix, $table_name );
146 $this->sqlFilename = $sql_file;
149 $this->primaryKey = $this->primaryKey();
161 public function primaryKey()
169 FROM `information_schema`.`COLUMNS`
170 WHERE (`TABLE_SCHEMA` =
'{$db}')
171 AND (`TABLE_NAME` = '{$this->tableName}
')
172 AND (`COLUMN_KEY` = 'PRI
');
174 return $wpdb->get_var( $sql );
187 public function count( $distinct = '', $status = '' )
192 if ( !empty( $status ) && is_array( $status ) ) {
193 if ( is_numeric( $status[key( $status )] ) ) {
194 $where = sprintf( 'WHERE %s = %s
', key( $status ), $status[key( $status )] );
197 $where = sprintf( "WHERE %s = '%s
'", key( $status ), $status[key( $status )] );
201 if ( empty( $distinct ) ) {
203 SELECT COUNT(*) AS count
204 FROM `{$this->tableName}`
207 return absint( $wpdb->get_var( $sql ) );
211 SELECT DISTINCT(`{$distinct}`),
213 FROM `{$this->tableName}`
215 GROUP BY `{$distinct}`
218 $results = $wpdb->get_results( $sql, ARRAY_A );
220 foreach ( $results as $res ) {
221 $result[$res[$distinct]] = $res['count
'];
238 public function delete( $pks )
242 if ( !is_array( $pks ) ) {
243 $pks = array( $pks );
246 $ids = implode( ',
', $pks );
249 DELETE FROM `{$this->tableName}`
250 WHERE `$this->primaryKey` IN({$ids})
252 $result = $wpdb->query( $sql );
268 public function groupBy( $column, $order_by = true, $order = 'ASC
' )
272 $sql_order = $order_by ? sprintf( 'ORDER BY `%s` %s
', $column, $order ) : '';
276 FROM `{$this->tableName}`
280 $results = $wpdb->get_results( $sql, ARRAY_A );
282 foreach ( $results as $res ) {
283 if ( !empty( $res[$column] ) ) {
284 $result[] = $res[$column];
301 public function map( $source_row, $destination_object )
303 if ( is_object( $source_row ) ) {
304 foreach ( $source_row as $field => $value ) {
305 $destination_object->$field = $value;
306 if ( method_exists( $destination_object, 'column_
' . $field ) ) {
307 call_user_func( array(
313 return $destination_object;
338 public function select( $id = false, $order_by = '', $order = 'ASC
', $where = '' )
343 if ( !empty( $id ) ) {
344 if ( is_array( $id ) ) {
345 $id = implode( ',
', $id );
347 $sql_where = sprintf( ' AND %s IN(%s)
', $this->primaryKey, $id );
350 if ( !empty( $where ) ) {
351 $sql_where = sprintf( '%s AND %s
', $sql_where, $where );
354 $order_by = empty( $order_by ) ? $this->primaryKey : $order_by;
357 SELECT * FROM `{$this->tableName}`
359 ORDER BY {$order_by} {$order}
362 $rows = $wpdb->get_results( $sql, OBJECT_K );
378 public function _select( $query = null, $order_by = '', $order = 'ASC
' )
382 /* Sanitize order by. */
383 $order_by = empty( $order_by ) ? $this->primaryKey : $order_by;
385 /* Build where condiction from an obejct (single record). */
386 $where = $this->where( $query );
389 SELECT * FROM {$this->tableName}
394 ORDER BY {$order_by} {$order}
397 $results = $wpdb->get_results( $sql, OBJECT_K );
410 public function update()
414 // Hide database warning and error
415 $wpdb->hide_errors();
416 $wpdb->suppress_errors();
421 if ( !empty( $this->sqlFilename ) && !empty( $this->tableName ) ) {
422 if ( !function_exists( 'dbDelta
' ) ) {
423 require_once( ABSPATH . 'wp-admin/includes/upgrade.php
' );
425 $content = file_get_contents( $this->sqlFilename );
426 if ( empty( $content ) ) {
431 // Replace table name
432 $sql = str_replace( '%s
', $this->tableName, $content );
435 $pattern = '@(([\
'"]).*?[^\\\]\2)|((?:\#|--).*?$|/\*(?:[^/*]|/(?!\*)|\*(?!/)|(?R))*\*\/)\s*|(?<=;)\s+@ms';
456 $sql_sanitize = trim( preg_replace( $pattern,
'$1', $sql ) );
457 preg_match_all( $pattern, $sql, $comments );
463 @dbDelta( $sql_sanitize );
467 $EZSQL_ERROR = array();
483 public function where( $query, $prefix =
'' )
487 if ( !is_null( $query ) ) {
490 if ( !empty( $prefix ) ) {
491 $prefix = rtrim( $prefix,
'.' ) .
'.';
494 $desc = $query->desc();
504 foreach ( $query as $property => $value ) {
505 if ( isset( $desc[$property] ) && !is_null( $value ) ) {
508 $type = $desc[$property]->Type;
509 $pos = strpos( $type,
'(' );
510 $type = (
false === $pos ) ? $type : substr( $type, 0, $pos );
514 if ( in_array( $type, $numeric ) ) {
515 $stack[] = sprintf(
'AND %s%s = %s', $prefix, $property, $value );
518 $stack[] = sprintf(
'AND %s%s = \'%s\'', $prefix, $property, $value );
523 if ( !empty( $stack ) ) {
524 $result = implode(
' ', $stack );
534 public function deleteWherePrimaryKey( $id )
536 _deprecated_function( __METHOD__,
'1.0.0',
'delete()' );
537 $this->
delete( $id );
543 public function selectWhereID( $id, $object, $output = OBJECT )
545 _deprecated_function( __METHOD__,
'1.0.0',
'select()' );
546 $this->select( $id );
572 class WPDKDBTableRow {
580 const NULL_VALUE =
'!NULL!';
601 public function __construct( __WPDKDBTable $dbtable, $pk = null )
603 $this->table = $dbtable;
605 if ( !is_null( $pk ) ) {
606 if ( is_numeric( $pk ) ) {
607 $this->initByID( $pk );
609 elseif ( is_array( $pk ) ) {
612 elseif ( is_object( $pk ) ) {
627 private function initByID( $pk )
633 $sql = sprintf(
'SELECT * FROM %s WHERE %s = %s', $this->table->tableName, $this->table->primaryKey, $pk );
636 $sql = $this->get_sql( $pk, $sql );
639 $sql = apply_filters(
'wpdk_db_table_' . $this->table->tableName .
'_sql', $sql );
641 $row = $wpdb->get_row( $sql );
643 if ( !is_null( $row ) ) {
644 foreach ( $row as $property => $value ) {
645 $this->$property = $value;
646 if ( method_exists( $this,
'column_' . $property ) ) {
647 call_user_func( array( $this,
'column_' . $property ), $value );
665 public function get_sql( $pk, $sql )
682 public static function getInstance( __WPDKDBTable $dbtable, $pk = null )
684 $instance =
new WPDKDBTableRow( $dbtable, $pk );
695 public function desc()
698 $sql = sprintf(
'DESC %s', $this->table->tableName );
700 $result = $wpdb->get_results( $sql, OBJECT_K );
710 public function defaults()