| @@ -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,24 @@ | ||
| 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 ); | |
| 58 | + array_walk( | |
| 59 | + $record, | |
| 60 | + function( &$value, &$key ) { | |
| 61 | + if ( ! is_array( $value ) ) { | |
| 62 | + $value = wp_strip_all_tags( $value ); | |
| 63 | + } | |
| 52 | 64 | } |
| 53 | - }); | |
| 65 | + ); | |
| 54 | 66 | |
| 55 | 67 | if ( empty( $record ) ) { |
| 56 | 68 | return false; |
| 57 | 69 | } |
| 58 | 70 | |
| 59 | - $fields = array( 'object_id', 'site_id', 'blog_id', 'user_id', 'user_role', 'created', 'summary', 'ip', 'connector', 'context', 'action' ); | |
| 71 | + $fields = array( 'object_id', 'site_id', 'blog_id', 'user_id', 'user_role', 'created', 'summary', 'ip', 'connector', 'context', 'action', 'meta' ); | |
| 60 | 72 | $data = array_intersect_key( $record, array_flip( $fields ) ); |
| 61 | 73 | |
| 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 | 74 | $record_id = $this->driver->insert_record( $data ); |
| 76 | 75 | |
| 77 | 76 | if ( ! $record_id ) { |
| 78 | 77 | /** |
| @@ -106,14 +105,14 @@ | ||
| 106 | 105 | * |
| 107 | 106 | * @see assemble_records |
| 108 | 107 | * @since 1.0.4 |
| 109 | 108 | * |
| 110 | - * @param string $column | |
| 109 | + * @param string $column Table column to pull data from. | |
| 111 | 110 | * |
| 112 | 111 | * @return array |
| 113 | 112 | */ |
| 114 | 113 | public function existing_records( $column ) { |
| 115 | - // Sanitize column | |
| 114 | + // Sanitize column. | |
| 116 | 115 | $allowed_columns = array( 'ID', 'site_id', 'blog_id', 'object_id', 'user_id', 'user_role', 'created', 'summary', 'connector', 'context', 'action', 'ip' ); |
| 117 | 116 | if ( ! in_array( $column, $allowed_columns, true ) ) { |
| 118 | 117 | return array(); |
| 119 | 118 | } |
| @@ -140,39 +139,39 @@ | ||
| 140 | 139 | |
| 141 | 140 | /** |
| 142 | 141 | * Get stream records |
| 143 | 142 | * |
| 144 | - * @param array Query args | |
| 143 | + * @param array $args Arguments to filter result by. | |
| 145 | 144 | * |
| 146 | 145 | * @return array Stream Records |
| 147 | 146 | */ |
| 148 | 147 | public function get_records( $args ) { |
| 149 | 148 | $defaults = array( |
| 150 | - // Search param | |
| 149 | + // Search param. | |
| 151 | 150 | 'search' => null, |
| 152 | 151 | 'search_field' => 'summary', |
| 153 | 152 | '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 | |
| 153 | + // Date-based filters. | |
| 154 | + 'date' => null, // Ex: 2015-07-01. | |
| 155 | + 'date_from' => null, // Ex: 2015-07-01. | |
| 156 | + 'date_to' => null, // Ex: 2015-07-01. | |
| 157 | + 'date_after' => null, // Ex: 2015-07-01T15:19:21+00:00. | |
| 158 | + 'date_before' => null, // Ex: 2015-07-01T15:19:21+00:00. | |
| 159 | + // Record ID filters. | |
| 161 | 160 | 'record' => null, |
| 162 | 161 | 'record__in' => array(), |
| 163 | 162 | 'record__not_in' => array(), |
| 164 | - // Pagination params | |
| 163 | + // Pagination params. | |
| 165 | 164 | 'records_per_page' => get_option( 'posts_per_page', 20 ), |
| 166 | 165 | 'paged' => 1, |
| 167 | - // Order | |
| 166 | + // Order. | |
| 168 | 167 | 'order' => 'desc', |
| 169 | 168 | 'orderby' => 'date', |
| 170 | - // Fields selection | |
| 169 | + // Fields selection. | |
| 171 | 170 | 'fields' => array(), |
| 172 | 171 | ); |
| 173 | 172 | |
| 174 | - // Additional property fields | |
| 173 | + // Additional property fields. | |
| 175 | 174 | $properties = array( |
| 176 | 175 | 'user_id' => null, |
| 177 | 176 | 'user_role' => null, |
| 178 | 177 | 'ip' => null, |
| @@ -190,9 +189,9 @@ | ||
| 190 | 189 | * @return array Array of query properties |
| 191 | 190 | */ |
| 192 | 191 | $properties = apply_filters( 'wp_stream_query_properties', $properties ); |
| 193 | 192 | |
| 194 | - // Add property fields to defaults, including their __in/__not_in variations | |
| 193 | + // Add property fields to defaults, including their __in/__not_in variations. | |
| 195 | 194 | foreach ( $properties as $property => $default ) { |
| 196 | 195 | if ( ! isset( $defaults[ $property ] ) ) { |
| 197 | 196 | $defaults[ $property ] = $default; |
| 198 | 197 | } |
| @@ -209,9 +208,9 @@ | ||
| 209 | 208 | * @return array Array of query arguments |
| 210 | 209 | */ |
| 211 | 210 | $args = apply_filters( 'wp_stream_query_args', $args ); |
| 212 | 211 | |
| 213 | - $result = (array) $this->driver->get_records( $args ); | |
| 212 | + $result = (array) $this->driver->get_records( $args ); | |
| 214 | 213 | $this->found_records_count = isset( $result['count'] ) ? $result['count'] : 0; |
| 215 | 214 | |
| 216 | 215 | return empty( $result['items'] ) ? array() : $result['items']; |
| 217 | 216 | } |
| @@ -218,9 +217,9 @@ | ||
| 218 | 217 | |
| 219 | 218 | /** |
| 220 | 219 | * Helper function, backwards compatibility |
| 221 | 220 | * |
| 222 | - * @param array $args Query args | |
| 221 | + * @param array $args Argument to filter result by. | |
| 223 | 222 | * |
| 224 | 223 | * @return array Stream Records |
| 225 | 224 | */ |
| 226 | 225 | public function query( $args ) { |
| @@ -229,9 +228,9 @@ | ||
| 229 | 228 | |
| 230 | 229 | /** |
| 231 | 230 | * Return the number of records found in last request |
| 232 | 231 | * |
| 233 | - * return int | |
| 232 | + * @return int | |
| 234 | 233 | */ |
| 235 | 234 | public function get_found_records_count() { |
| 236 | 235 | return $this->found_records_count; |
| 237 | 236 | } |