-
-
Save Roshanb54/92313073e297f4a35af11405afc542fc to your computer and use it in GitHub Desktop.
A PHP class to handle CRUD functionality in WordPress default tables.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Abstract class which has helper functions to get data from the database | |
*/ | |
abstract class Base_Custom_Data | |
{ | |
/** | |
* The current table name | |
* | |
* @var boolean | |
*/ | |
private $tableName = false; | |
/** | |
* Constructor for the database class to inject the table name | |
* | |
* @param String $tableName - The current table name | |
*/ | |
public function __construct($tableName) | |
{ | |
$this->tableName = $tableName; | |
} | |
/** | |
* Insert data into the current data | |
* | |
* @param array $data - Data to enter into the database table | |
* | |
* @return InsertQuery Object | |
*/ | |
public function insert(array $data) | |
{ | |
global $wpdb; | |
if(empty($data)) | |
{ | |
return false; | |
} | |
$wpdb->insert($this->tableName, $data); | |
return $wpdb->insert_id; | |
} | |
/** | |
* Get all from the selected table | |
* | |
* @param String $orderBy - Order by column name | |
* | |
* @return Table result | |
*/ | |
public function get_all( $orderBy = NULL ) | |
{ | |
global $wpdb; | |
$sql = 'SELECT * FROM `'.$this->tableName.'`'; | |
if(!empty($orderBy)) | |
{ | |
$sql .= ' ORDER BY ' . $orderBy; | |
} | |
$all = $wpdb->get_results($sql); | |
return $all; | |
} | |
/** | |
* Get a value by a condition | |
* | |
* @param Array $conditionValue - A key value pair of the conditions you want to search on | |
* @param String $condition - A string value for the condition of the query default to equals | |
* | |
* @return Table result | |
*/ | |
public function get_by(array $conditionValue, $condition = '=') | |
{ | |
global $wpdb; | |
$sql = 'SELECT * FROM `'.$this->tableName.'` WHERE '; | |
foreach ($conditionValue as $field => $value) { | |
switch(strtolower($condition)) | |
{ | |
case 'in': | |
if(!is_array($value)) | |
{ | |
throw new Exception("Values for IN query must be an array.", 1); | |
} | |
$sql .= $wpdb->prepare('`%s` IN (%s)', $field, implode(',', $value)); | |
break; | |
default: | |
$sql .= $wpdb->prepare('`'.$field.'` '.$condition.' %s', $value); | |
break; | |
} | |
} | |
$result = $wpdb->get_results($sql); | |
return $result; | |
} | |
/** | |
* Update a table record in the database | |
* | |
* @param array $data - Array of data to be updated | |
* @param array $conditionValue - Key value pair for the where clause of the query | |
* | |
* @return Updated object | |
*/ | |
public function update(array $data, array $conditionValue) | |
{ | |
global $wpdb; | |
if(empty($data)) | |
{ | |
return false; | |
} | |
$updated = $wpdb->update( $this->tableName, $data, $conditionValue); | |
return $updated; | |
} | |
/** | |
* Delete row on the database table | |
* | |
* @param array $conditionValue - Key value pair for the where clause of the query | |
* | |
* @return Int - Num rows deleted | |
*/ | |
public function delete(array $conditionValue) | |
{ | |
global $wpdb; | |
$deleted = $wpdb->delete( $this->tableName, $conditionValue ); | |
return $deleted; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment