| @@ -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,34 +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' ); | |
| 60 | - $data = array_intersect_key( $record, array_flip( $fields ) ); | |
| 61 | - | |
| 62 | - $meta = array(); | |
| 63 | - foreach ( (array) $record['meta'] as $key => $vals ) { | |
| 64 | - // If associative array, serialize it, otherwise loop on its members | |
| 65 | - $vals = ( is_array( $vals ) && 0 !== key( $vals ) ) ? array( $vals ) : $vals; | |
| 66 | - | |
| 67 | - foreach ( (array) $vals as $num => $val ) { | |
| 68 | - $vals[ $num ] = maybe_serialize( $val ); | |
| 69 | - } | |
| 70 | - $meta[ $key ] = $vals; | |
| 71 | - } | |
| 72 | - | |
| 73 | - $data['meta'] = $meta; | |
| 74 | - | |
| 75 | 63 | $record_id = $this->driver->insert_record( $data ); |
| 76 | 64 | |
| 77 | 65 | if ( ! $record_id ) { |
| 78 | 66 | /** |
| @@ -97,8 +85,51 @@ | ||
| 97 | 85 | return absint( $record_id ); |
| 98 | 86 | } |
| 99 | 87 | |
| 100 | 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 | + /** | |
| 101 | 132 | * Returns array of existing values for requested column. |
| 102 | 133 | * Used to fill search filters with only used items, instead of all items. |
| 103 | 134 | * |
| 104 | 135 | * GROUP BY allows query to find just the first occurrence of each value in the column, |
| @@ -106,14 +137,14 @@ | ||
| 106 | 137 | * |
| 107 | 138 | * @see assemble_records |
| 108 | 139 | * @since 1.0.4 |
| 109 | 140 | * |
| 110 | - * @param string $column | |
| 141 | + * @param string $column Table column to pull data from. | |
| 111 | 142 | * |
| 112 | 143 | * @return array |
| 113 | 144 | */ |
| 114 | 145 | public function existing_records( $column ) { |
| 115 | - // Sanitize column | |
| 146 | + // Sanitize column. | |
| 116 | 147 | $allowed_columns = array( 'ID', 'site_id', 'blog_id', 'object_id', 'user_id', 'user_role', 'created', 'summary', 'connector', 'context', 'action', 'ip' ); |
| 117 | 148 | if ( ! in_array( $column, $allowed_columns, true ) ) { |
| 118 | 149 | return array(); |
| 119 | 150 | } |
| @@ -140,39 +171,39 @@ | ||
| 140 | 171 | |
| 141 | 172 | /** |
| 142 | 173 | * Get stream records |
| 143 | 174 | * |
| 144 | - * @param array Query args | |
| 175 | + * @param array $args Arguments to filter result by. | |
| 145 | 176 | * |
| 146 | 177 | * @return array Stream Records |
| 147 | 178 | */ |
| 148 | 179 | public function get_records( $args ) { |
| 149 | 180 | $defaults = array( |
| 150 | - // Search param | |
| 181 | + // Search param. | |
| 151 | 182 | 'search' => null, |
| 152 | 183 | 'search_field' => 'summary', |
| 153 | 184 | 'record_after' => null, // Deprecated, use date_after instead |
| 154 | - // Date-based filters | |
| 155 | - 'date' => null, // Ex: 2015-07-01 | |
| 156 | - 'date_from' => null, // Ex: 2015-07-01 | |
| 157 | - 'date_to' => null, // Ex: 2015-07-01 | |
| 158 | - 'date_after' => null, // Ex: 2015-07-01T15:19:21+00:00 | |
| 159 | - 'date_before' => null, // Ex: 2015-07-01T15:19:21+00:00 | |
| 160 | - // 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. | |
| 161 | 192 | 'record' => null, |
| 162 | 193 | 'record__in' => array(), |
| 163 | 194 | 'record__not_in' => array(), |
| 164 | - // Pagination params | |
| 195 | + // Pagination params. | |
| 165 | 196 | 'records_per_page' => get_option( 'posts_per_page', 20 ), |
| 166 | 197 | 'paged' => 1, |
| 167 | - // Order | |
| 198 | + // Order. | |
| 168 | 199 | 'order' => 'desc', |
| 169 | 200 | 'orderby' => 'date', |
| 170 | - // Fields selection | |
| 201 | + // Fields selection. | |
| 171 | 202 | 'fields' => array(), |
| 172 | 203 | ); |
| 173 | 204 | |
| 174 | - // Additional property fields | |
| 205 | + // Additional property fields. | |
| 175 | 206 | $properties = array( |
| 176 | 207 | 'user_id' => null, |
| 177 | 208 | 'user_role' => null, |
| 178 | 209 | 'ip' => null, |
| @@ -190,9 +221,9 @@ | ||
| 190 | 221 | * @return array Array of query properties |
| 191 | 222 | */ |
| 192 | 223 | $properties = apply_filters( 'wp_stream_query_properties', $properties ); |
| 193 | 224 | |
| 194 | - // Add property fields to defaults, including their __in/__not_in variations | |
| 225 | + // Add property fields to defaults, including their __in/__not_in variations. | |
| 195 | 226 | foreach ( $properties as $property => $default ) { |
| 196 | 227 | if ( ! isset( $defaults[ $property ] ) ) { |
| 197 | 228 | $defaults[ $property ] = $default; |
| 198 | 229 | } |
| @@ -209,9 +240,9 @@ | ||
| 209 | 240 | * @return array Array of query arguments |
| 210 | 241 | */ |
| 211 | 242 | $args = apply_filters( 'wp_stream_query_args', $args ); |
| 212 | 243 | |
| 213 | - $result = (array) $this->driver->get_records( $args ); | |
| 244 | + $result = (array) $this->driver->get_records( $args ); | |
| 214 | 245 | $this->found_records_count = isset( $result['count'] ) ? $result['count'] : 0; |
| 215 | 246 | |
| 216 | 247 | return empty( $result['items'] ) ? array() : $result['items']; |
| 217 | 248 | } |
| @@ -218,9 +249,9 @@ | ||
| 218 | 249 | |
| 219 | 250 | /** |
| 220 | 251 | * Helper function, backwards compatibility |
| 221 | 252 | * |
| 222 | - * @param array $args Query args | |
| 253 | + * @param array $args Argument to filter result by. | |
| 223 | 254 | * |
| 224 | 255 | * @return array Stream Records |
| 225 | 256 | */ |
| 226 | 257 | public function query( $args ) { |
| @@ -229,9 +260,9 @@ | ||
| 229 | 260 | |
| 230 | 261 | /** |
| 231 | 262 | * Return the number of records found in last request |
| 232 | 263 | * |
| 233 | - * return int | |
| 264 | + * @return int | |
| 234 | 265 | */ |
| 235 | 266 | public function get_found_records_count() { |
| 236 | 267 | return $this->found_records_count; |
| 237 | 268 | } |