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