| @@ -1,10 +1,19 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * Manage DB connections using a provided DB driver. | |
| 4 | + * | |
| 5 | + * @package WP_Stream | |
| 6 | + */ | |
| 7 | + | |
| 2 | 8 | namespace WP_Stream; |
| 3 | 9 | |
| 10 | +/** | |
| 11 | + * Class - DB | |
| 12 | + */ | |
| 4 | 13 | class DB { |
| 5 | 14 | /** |
| 6 | - * Hold the Driver class | |
| 15 | + * Holds the driver instance | |
| 7 | 16 | * |
| 8 | 17 | * @var DB_Driver |
| 9 | 18 | */ |
| 10 | 19 | public $driver; |
| @@ -27,9 +36,9 @@ | ||
| 27 | 36 | |
| 28 | 37 | /** |
| 29 | 38 | * Insert a record |
| 30 | 39 | * |
| 31 | - * @param array $record | |
| 40 | + * @param array $record New record. | |
| 32 | 41 | * |
| 33 | 42 | * @return int |
| 34 | 43 | */ |
| 35 | 44 | public function insert( $record ) { |
| @@ -45,21 +54,13 @@ | ||
| 45 | 54 | * @return array |
| 46 | 55 | */ |
| 47 | 56 | $record = apply_filters( 'wp_stream_record_array', $record ); |
| 48 | 57 | |
| 49 | - array_walk( $record, function( &$value, &$key ) { | |
| 50 | - if ( ! is_array( $value ) ) { | |
| 51 | - $value = strip_tags( $value ); | |
| 52 | - } | |
| 53 | - }); | |
| 54 | - | |
| 55 | - if ( empty( $record ) ) { | |
| 58 | + $data = $this->sanitize_record( $record ); | |
| 59 | + if ( empty( $data ) ) { | |
| 56 | 60 | return false; |
| 57 | 61 | } |
| 58 | 62 | |
| 59 | - $fields = array( 'object_id', 'site_id', 'blog_id', 'user_id', 'user_role', 'created', 'summary', 'ip', 'connector', 'context', 'action', 'meta' ); | |
| 60 | - $data = array_intersect_key( $record, array_flip( $fields ) ); | |
| 61 | - | |
| 62 | 63 | $record_id = $this->driver->insert_record( $data ); |
| 63 | 64 | |
| 64 | 65 | if ( ! $record_id ) { |
| 65 | 66 | /** |
| @@ -84,8 +85,51 @@ | ||
| 84 | 85 | return absint( $record_id ); |
| 85 | 86 | } |
| 86 | 87 | |
| 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 | + /** | |
| 88 | 132 | * Returns array of existing values for requested column. |
| 89 | 133 | * Used to fill search filters with only used items, instead of all items. |
| 90 | 134 | * |
| 91 | 135 | * GROUP BY allows query to find just the first occurrence of each value in the column, |
| @@ -93,14 +137,14 @@ | ||
| 93 | 137 | * |
| 94 | 138 | * @see assemble_records |
| 95 | 139 | * @since 1.0.4 |
| 96 | 140 | * |
| 97 | - * @param string $column | |
| 141 | + * @param string $column Table column to pull data from. | |
| 98 | 142 | * |
| 99 | 143 | * @return array |
| 100 | 144 | */ |
| 101 | 145 | public function existing_records( $column ) { |
| 102 | - // Sanitize column | |
| 146 | + // Sanitize column. | |
| 103 | 147 | $allowed_columns = array( 'ID', 'site_id', 'blog_id', 'object_id', 'user_id', 'user_role', 'created', 'summary', 'connector', 'context', 'action', 'ip' ); |
| 104 | 148 | if ( ! in_array( $column, $allowed_columns, true ) ) { |
| 105 | 149 | return array(); |
| 106 | 150 | } |
| @@ -127,39 +171,39 @@ | ||
| 127 | 171 | |
| 128 | 172 | /** |
| 129 | 173 | * Get stream records |
| 130 | 174 | * |
| 131 | - * @param array Query args | |
| 175 | + * @param array $args Arguments to filter result by. | |
| 132 | 176 | * |
| 133 | 177 | * @return array Stream Records |
| 134 | 178 | */ |
| 135 | 179 | public function get_records( $args ) { |
| 136 | 180 | $defaults = array( |
| 137 | - // Search param | |
| 181 | + // Search param. | |
| 138 | 182 | 'search' => null, |
| 139 | 183 | 'search_field' => 'summary', |
| 140 | 184 | 'record_after' => null, // Deprecated, use date_after instead |
| 141 | - // Date-based filters | |
| 142 | - 'date' => null, // Ex: 2015-07-01 | |
| 143 | - 'date_from' => null, // Ex: 2015-07-01 | |
| 144 | - 'date_to' => null, // Ex: 2015-07-01 | |
| 145 | - 'date_after' => null, // Ex: 2015-07-01T15:19:21+00:00 | |
| 146 | - 'date_before' => null, // Ex: 2015-07-01T15:19:21+00:00 | |
| 147 | - // Record ID filters | |
| 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. | |
| 148 | 192 | 'record' => null, |
| 149 | 193 | 'record__in' => array(), |
| 150 | 194 | 'record__not_in' => array(), |
| 151 | - // Pagination params | |
| 195 | + // Pagination params. | |
| 152 | 196 | 'records_per_page' => get_option( 'posts_per_page', 20 ), |
| 153 | 197 | 'paged' => 1, |
| 154 | - // Order | |
| 198 | + // Order. | |
| 155 | 199 | 'order' => 'desc', |
| 156 | 200 | 'orderby' => 'date', |
| 157 | - // Fields selection | |
| 201 | + // Fields selection. | |
| 158 | 202 | 'fields' => array(), |
| 159 | 203 | ); |
| 160 | 204 | |
| 161 | - // Additional property fields | |
| 205 | + // Additional property fields. | |
| 162 | 206 | $properties = array( |
| 163 | 207 | 'user_id' => null, |
| 164 | 208 | 'user_role' => null, |
| 165 | 209 | 'ip' => null, |
| @@ -177,9 +221,9 @@ | ||
| 177 | 221 | * @return array Array of query properties |
| 178 | 222 | */ |
| 179 | 223 | $properties = apply_filters( 'wp_stream_query_properties', $properties ); |
| 180 | 224 | |
| 181 | - // Add property fields to defaults, including their __in/__not_in variations | |
| 225 | + // Add property fields to defaults, including their __in/__not_in variations. | |
| 182 | 226 | foreach ( $properties as $property => $default ) { |
| 183 | 227 | if ( ! isset( $defaults[ $property ] ) ) { |
| 184 | 228 | $defaults[ $property ] = $default; |
| 185 | 229 | } |
| @@ -196,9 +240,9 @@ | ||
| 196 | 240 | * @return array Array of query arguments |
| 197 | 241 | */ |
| 198 | 242 | $args = apply_filters( 'wp_stream_query_args', $args ); |
| 199 | 243 | |
| 200 | - $result = (array) $this->driver->get_records( $args ); | |
| 244 | + $result = (array) $this->driver->get_records( $args ); | |
| 201 | 245 | $this->found_records_count = isset( $result['count'] ) ? $result['count'] : 0; |
| 202 | 246 | |
| 203 | 247 | return empty( $result['items'] ) ? array() : $result['items']; |
| 204 | 248 | } |
| @@ -205,9 +249,9 @@ | ||
| 205 | 249 | |
| 206 | 250 | /** |
| 207 | 251 | * Helper function, backwards compatibility |
| 208 | 252 | * |
| 209 | - * @param array $args Query args | |
| 253 | + * @param array $args Argument to filter result by. | |
| 210 | 254 | * |
| 211 | 255 | * @return array Stream Records |
| 212 | 256 | */ |
| 213 | 257 | public function query( $args ) { |
| @@ -216,9 +260,9 @@ | ||
| 216 | 260 | |
| 217 | 261 | /** |
| 218 | 262 | * Return the number of records found in last request |
| 219 | 263 | * |
| 220 | - * return int | |
| 264 | + * @return int | |
| 221 | 265 | */ |
| 222 | 266 | public function get_found_records_count() { |
| 223 | 267 | return $this->found_records_count; |
| 224 | 268 | } |