| 1 |
<?php |
| 2 |
/** |
| 3 |
* Manage DB connections using a provided DB driver. |
| 4 |
* |
| 5 |
* @package WP_Stream |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Stream; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class - DB |
| 12 |
*/ |
| 13 |
class DB { |
| 14 |
/** |
| 15 |
* Holds the driver instance |
| 16 |
* |
| 17 |
* @var DB_Driver |
| 18 |
*/ |
| 19 |
public $driver; |
| 20 |
|
| 21 |
/** |
| 22 |
* Number of records in last request |
| 23 |
* |
| 24 |
* @var int |
| 25 |
*/ |
| 26 |
protected $found_records_count = 0; |
| 27 |
|
| 28 |
/** |
| 29 |
* Class constructor. |
| 30 |
* |
| 31 |
* @param DB_Driver $driver Driver we want to use. |
| 32 |
*/ |
| 33 |
public function __construct( $driver ) { |
| 34 |
$this->driver = $driver; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Insert a record |
| 39 |
* |
| 40 |
* @param array $record New record. |
| 41 |
* |
| 42 |
* @return int |
| 43 |
*/ |
| 44 |
public function insert( $record ) { |
| 45 |
if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { |
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Filter allows modification of record information |
| 51 |
* |
| 52 |
* @param array $record |
| 53 |
* |
| 54 |
* @return array |
| 55 |
*/ |
| 56 |
$record = apply_filters( 'wp_stream_record_array', $record ); |
| 57 |
|
| 58 |
$data = $this->sanitize_record( $record ); |
| 59 |
if ( empty( $data ) ) { |
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
$record_id = $this->driver->insert_record( $data ); |
| 64 |
|
| 65 |
if ( ! $record_id ) { |
| 66 |
/** |
| 67 |
* Fires on a record insertion error |
| 68 |
* |
| 69 |
* @param array $record |
| 70 |
* @param mixed $result |
| 71 |
*/ |
| 72 |
do_action( 'wp_stream_record_insert_error', $record, false ); |
| 73 |
|
| 74 |
return false; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Fires after a record has been inserted |
| 79 |
* |
| 80 |
* @param int $record_id |
| 81 |
* @param array $record |
| 82 |
*/ |
| 83 |
do_action( 'wp_stream_record_inserted', $record_id, $record ); |
| 84 |
|
| 85 |
return absint( $record_id ); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Ensure the record matches our schema. |
| 90 |
* |
| 91 |
* @param array $record Record to store. |
| 92 |
* |
| 93 |
* @return array |
| 94 |
*/ |
| 95 |
protected function sanitize_record( $record ) { |
| 96 |
if ( ! is_array( $record ) ) { |
| 97 |
return array(); |
| 98 |
} |
| 99 |
|
| 100 |
$record_defaults = array( |
| 101 |
'object_id' => null, |
| 102 |
'site_id' => null, |
| 103 |
'blog_id' => null, |
| 104 |
'user_id' => null, |
| 105 |
'user_role' => null, |
| 106 |
'created' => null, |
| 107 |
'summary' => null, |
| 108 |
'ip' => null, |
| 109 |
'connector' => null, |
| 110 |
'context' => null, |
| 111 |
'action' => null, |
| 112 |
'meta' => array(), |
| 113 |
); |
| 114 |
|
| 115 |
// Records can have only these fields. |
| 116 |
$record = array_intersect_key( $record, $record_defaults ); |
| 117 |
|
| 118 |
// Sanitize all record values. |
| 119 |
return array_map( |
| 120 |
function ( $value ) { |
| 121 |
if ( ! is_array( $value ) ) { |
| 122 |
return wp_strip_all_tags( $value ); |
| 123 |
} |
| 124 |
|
| 125 |
return $value; |
| 126 |
}, |
| 127 |
$record |
| 128 |
); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Returns array of existing values for requested column. |
| 133 |
* Used to fill search filters with only used items, instead of all items. |
| 134 |
* |
| 135 |
* GROUP BY allows query to find just the first occurrence of each value in the column, |
| 136 |
* increasing the efficiency of the query. |
| 137 |
* |
| 138 |
* @see assemble_records |
| 139 |
* @since 1.0.4 |
| 140 |
* |
| 141 |
* @param string $column Table column to pull data from. |
| 142 |
* |
| 143 |
* @return array |
| 144 |
*/ |
| 145 |
public function existing_records( $column ) { |
| 146 |
// Sanitize column. |
| 147 |
$allowed_columns = array( 'ID', 'site_id', 'blog_id', 'object_id', 'user_id', 'user_role', 'created', 'summary', 'connector', 'context', 'action', 'ip' ); |
| 148 |
if ( ! in_array( $column, $allowed_columns, true ) ) { |
| 149 |
return array(); |
| 150 |
} |
| 151 |
|
| 152 |
$rows = $this->driver->get_column_values( $column ); |
| 153 |
|
| 154 |
if ( is_array( $rows ) && ! empty( $rows ) ) { |
| 155 |
$output_array = array(); |
| 156 |
|
| 157 |
foreach ( $rows as $row ) { |
| 158 |
foreach ( $row as $cell => $value ) { |
| 159 |
$output_array[ $value ] = $value; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
return (array) $output_array; |
| 164 |
} |
| 165 |
|
| 166 |
$column = sprintf( 'stream_%s', $column ); |
| 167 |
|
| 168 |
$term_labels = wp_stream_get_instance()->connectors->term_labels; |
| 169 |
return isset( $term_labels[ $column ] ) ? $term_labels[ $column ] : array(); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Get stream records |
| 174 |
* |
| 175 |
* @param array $args Arguments to filter result by. |
| 176 |
* |
| 177 |
* @return array Stream Records |
| 178 |
*/ |
| 179 |
public function get_records( $args ) { |
| 180 |
$defaults = array( |
| 181 |
// Search param. |
| 182 |
'search' => null, |
| 183 |
'search_field' => 'summary', |
| 184 |
'record_after' => null, // Deprecated, use date_after instead |
| 185 |
// Date-based filters. |
| 186 |
'date' => null, // Ex: 2015-07-01. |
| 187 |
'date_from' => null, // Ex: 2015-07-01. |
| 188 |
'date_to' => null, // Ex: 2015-07-01. |
| 189 |
'date_after' => null, // Ex: 2015-07-01T15:19:21+00:00. |
| 190 |
'date_before' => null, // Ex: 2015-07-01T15:19:21+00:00. |
| 191 |
// Record ID filters. |
| 192 |
'record' => null, |
| 193 |
'record__in' => array(), |
| 194 |
'record__not_in' => array(), |
| 195 |
// Pagination params. |
| 196 |
'records_per_page' => get_option( 'posts_per_page', 20 ), |
| 197 |
'paged' => 1, |
| 198 |
// Order. |
| 199 |
'order' => 'desc', |
| 200 |
'orderby' => 'date', |
| 201 |
// Fields selection. |
| 202 |
'fields' => array(), |
| 203 |
); |
| 204 |
|
| 205 |
// Additional property fields. |
| 206 |
$properties = array( |
| 207 |
'user_id' => null, |
| 208 |
'user_role' => null, |
| 209 |
'ip' => null, |
| 210 |
'object_id' => null, |
| 211 |
'site_id' => null, |
| 212 |
'blog_id' => null, |
| 213 |
'connector' => null, |
| 214 |
'context' => null, |
| 215 |
'action' => null, |
| 216 |
); |
| 217 |
|
| 218 |
/** |
| 219 |
* Filter allows additional query properties to be added |
| 220 |
* |
| 221 |
* @return array Array of query properties |
| 222 |
*/ |
| 223 |
$properties = apply_filters( 'wp_stream_query_properties', $properties ); |
| 224 |
|
| 225 |
// Add property fields to defaults, including their __in/__not_in variations. |
| 226 |
foreach ( $properties as $property => $default ) { |
| 227 |
if ( ! isset( $defaults[ $property ] ) ) { |
| 228 |
$defaults[ $property ] = $default; |
| 229 |
} |
| 230 |
|
| 231 |
$defaults[ "{$property}__in" ] = array(); |
| 232 |
$defaults[ "{$property}__not_in" ] = array(); |
| 233 |
} |
| 234 |
|
| 235 |
$args = wp_parse_args( $args, $defaults ); |
| 236 |
|
| 237 |
/** |
| 238 |
* Filter allows additional arguments to query $args |
| 239 |
* |
| 240 |
* @return array Array of query arguments |
| 241 |
*/ |
| 242 |
$args = apply_filters( 'wp_stream_query_args', $args ); |
| 243 |
|
| 244 |
$result = (array) $this->driver->get_records( $args ); |
| 245 |
$this->found_records_count = isset( $result['count'] ) ? $result['count'] : 0; |
| 246 |
|
| 247 |
return empty( $result['items'] ) ? array() : $result['items']; |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Helper function, backwards compatibility |
| 252 |
* |
| 253 |
* @param array $args Argument to filter result by. |
| 254 |
* |
| 255 |
* @return array Stream Records |
| 256 |
*/ |
| 257 |
public function query( $args ) { |
| 258 |
return $this->get_records( $args ); |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Return the number of records found in last request |
| 263 |
* |
| 264 |
* @return int |
| 265 |
*/ |
| 266 |
public function get_found_records_count() { |
| 267 |
return $this->found_records_count; |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* Public getter to return table names |
| 272 |
* |
| 273 |
* @return array |
| 274 |
*/ |
| 275 |
public function get_table_names() { |
| 276 |
return $this->driver->get_table_names(); |
| 277 |
} |
| 278 |
} |
| 279 |
|