| 1 |
<?php |
| 2 |
namespace WP_Stream; |
| 3 |
|
| 4 |
class DB_Driver_WPDB implements DB_Driver { |
| 5 |
/** |
| 6 |
* Holds Query class |
| 7 |
* |
| 8 |
* @var Query |
| 9 |
*/ |
| 10 |
protected $query; |
| 11 |
|
| 12 |
/** |
| 13 |
* Hold records table name |
| 14 |
* |
| 15 |
* @var string |
| 16 |
*/ |
| 17 |
public $table; |
| 18 |
|
| 19 |
/** |
| 20 |
* Hold meta table name |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
public $table_meta; |
| 25 |
|
| 26 |
/** |
| 27 |
* Class constructor. |
| 28 |
*/ |
| 29 |
public function __construct() { |
| 30 |
$this->query = new Query( $this ); |
| 31 |
|
| 32 |
global $wpdb; |
| 33 |
$prefix = apply_filters( 'wp_stream_db_tables_prefix', $wpdb->base_prefix ); |
| 34 |
|
| 35 |
$this->table = $prefix . 'stream'; |
| 36 |
$this->table_meta = $prefix . 'stream_meta'; |
| 37 |
|
| 38 |
$wpdb->stream = $this->table; |
| 39 |
$wpdb->streammeta = $this->table_meta; |
| 40 |
|
| 41 |
// Hack for get_metadata |
| 42 |
$wpdb->recordmeta = $this->table_meta; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Insert a record. |
| 47 |
* |
| 48 |
* @param array $data Data to insert. |
| 49 |
* |
| 50 |
* @return int |
| 51 |
*/ |
| 52 |
public function insert_record( $data ) { |
| 53 |
global $wpdb; |
| 54 |
|
| 55 |
if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { |
| 56 |
return false; |
| 57 |
} |
| 58 |
|
| 59 |
$meta = $data['meta']; |
| 60 |
unset( $data['meta'] ); |
| 61 |
|
| 62 |
$result = $wpdb->insert( $this->table, $data ); |
| 63 |
if ( ! $result ) { |
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
$record_id = $wpdb->insert_id; |
| 68 |
|
| 69 |
// Insert record meta |
| 70 |
foreach ( (array) $meta as $key => $vals ) { |
| 71 |
foreach ( (array) $vals as $val ) { |
| 72 |
$this->insert_meta( $record_id, $key, $val ); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
return $record_id; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Insert record meta |
| 81 |
* |
| 82 |
* @param int $record_id |
| 83 |
* @param string $key |
| 84 |
* @param string $val |
| 85 |
* |
| 86 |
* @return array |
| 87 |
*/ |
| 88 |
public function insert_meta( $record_id, $key, $val ) { |
| 89 |
global $wpdb; |
| 90 |
|
| 91 |
$result = $wpdb->insert( |
| 92 |
$this->table_meta, |
| 93 |
array( |
| 94 |
'record_id' => $record_id, |
| 95 |
'meta_key' => $key, |
| 96 |
'meta_value' => $val, |
| 97 |
) |
| 98 |
); |
| 99 |
|
| 100 |
return $result; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Retrieve records |
| 105 |
* |
| 106 |
* @param array $args |
| 107 |
* |
| 108 |
* @return array |
| 109 |
*/ |
| 110 |
public function get_records( $args ) { |
| 111 |
return $this->query->query( $args ); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Returns array of existing values for requested column. |
| 116 |
* Used to fill search filters with only used items, instead of all items. |
| 117 |
* |
| 118 |
* GROUP BY allows query to find just the first occurrence of each value in the column, |
| 119 |
* increasing the efficiency of the query. |
| 120 |
* |
| 121 |
* @param string $column |
| 122 |
* |
| 123 |
* @return array |
| 124 |
*/ |
| 125 |
public function get_column_values( $column ) { |
| 126 |
global $wpdb; |
| 127 |
return (array) $wpdb->get_results( |
| 128 |
"SELECT DISTINCT $column FROM $wpdb->stream", // @codingStandardsIgnoreLine can't prepare column name |
| 129 |
'ARRAY_A' |
| 130 |
); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Public getter to return table names |
| 135 |
* |
| 136 |
* @return array |
| 137 |
*/ |
| 138 |
public function get_table_names() { |
| 139 |
return array( |
| 140 |
$this->table, |
| 141 |
$this->table_meta, |
| 142 |
); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Init storage. |
| 147 |
* |
| 148 |
* @param \WP_Stream\Plugin $plugin Instance of the plugin. |
| 149 |
* @return \WP_Stream\Install |
| 150 |
*/ |
| 151 |
public function setup_storage( $plugin ) { |
| 152 |
return new Install( $plugin ); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Purge storage. |
| 157 |
* |
| 158 |
* @param \WP_Stream\Plugin $plugin Instance of the plugin. |
| 159 |
* @return \WP_Stream\Uninstall |
| 160 |
*/ |
| 161 |
public function purge_storage( $plugin ) { |
| 162 |
$uninstall = new Uninstall( $plugin ); |
| 163 |
add_action( 'wp_ajax_wp_stream_uninstall', array( $uninstall, 'uninstall' ) ); |
| 164 |
|
| 165 |
return $uninstall; |
| 166 |
} |
| 167 |
|
| 168 |
} |
| 169 |
|