| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 4 |
|
| 5 |
/** |
| 6 |
* FlipperCode_Database class file. |
| 7 |
* |
| 8 |
* @author Flipper Code <hello@flippercode.com> |
| 9 |
* @package Core |
| 10 |
*/ |
| 11 |
|
| 12 |
if ( ! class_exists( 'FlipperCode_Database' ) ) { |
| 13 |
|
| 14 |
/** |
| 15 |
* Class : FlipperCode_Database |
| 16 |
* |
| 17 |
* @author Flipper Code <hello@flippercode.com> |
| 18 |
* @package Core |
| 19 |
*/ |
| 20 |
class FlipperCode_Database { |
| 21 |
/** |
| 22 |
* Connection reference. |
| 23 |
* |
| 24 |
* @var [type] |
| 25 |
*/ |
| 26 |
public $connection; |
| 27 |
/** |
| 28 |
* Intialize connection string. |
| 29 |
*/ |
| 30 |
function __construct() { |
| 31 |
|
| 32 |
global $wpdb; |
| 33 |
$this->connection = $wpdb; |
| 34 |
} |
| 35 |
/** |
| 36 |
* Connect to database. |
| 37 |
* |
| 38 |
* @return string Connection reference. |
| 39 |
*/ |
| 40 |
public static function connect() { |
| 41 |
|
| 42 |
global $wpdb; |
| 43 |
return $wpdb; |
| 44 |
} |
| 45 |
/** |
| 46 |
* Read query over connection. |
| 47 |
* |
| 48 |
* @param string $query SQL Query. |
| 49 |
* @param string $connection Connection String. |
| 50 |
* @return object Records cursor. |
| 51 |
*/ |
| 52 |
public static function reader( $query, $connection ) { |
| 53 |
|
| 54 |
$cursor = $connection->get_results( $query ); |
| 55 |
return $cursor; |
| 56 |
} |
| 57 |
/** |
| 58 |
* Execute delete query. |
| 59 |
* |
| 60 |
* @param string $query SQL Query. |
| 61 |
* @param string $connection Connection String. |
| 62 |
* @return boolean True or False. |
| 63 |
*/ |
| 64 |
public static function non_query( $query, $connection ) { |
| 65 |
|
| 66 |
$result = $connection->query( $query ); |
| 67 |
|
| 68 |
if ( 0 == $result or 'FALSE' == $result ) { |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
return $result; |
| 73 |
} |
| 74 |
/** |
| 75 |
* Insert or Update records |
| 76 |
* |
| 77 |
* @param string $table Table Name. |
| 78 |
* @param array $data Data array. |
| 79 |
* @param string $where Condition. |
| 80 |
* @return int Insert ID or Update Status. |
| 81 |
*/ |
| 82 |
public static function insert_or_update( $table, $data, $where = '' ) { |
| 83 |
|
| 84 |
global $wpdb; |
| 85 |
$wpdb->show_errors(); |
| 86 |
if ( ! is_array( $where ) ) { |
| 87 |
$wpdb->insert( $table, $data ); |
| 88 |
$result = $wpdb->insert_id; |
| 89 |
$result = apply_filters('wpgmp_fetch_location_id',$result,$data,$wpdb,"",$table); |
| 90 |
if( $result > 0 ) { |
| 91 |
return $result; |
| 92 |
} else { |
| 93 |
return false; |
| 94 |
} |
| 95 |
} else { |
| 96 |
$result = $wpdb->update( $table, $data, $where ); |
| 97 |
$result = apply_filters('wpgmp_fetch_location_id',$result,$data,$wpdb,$where,$table); |
| 98 |
return $result; |
| 99 |
} |
| 100 |
|
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|