| @@ -1,12 +1,20 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * Handles top-level record keeping functionality. | |
| 4 | + * | |
| 5 | + * @package WP_Stream | |
| 6 | + */ | |
| 2 | 7 | |
| 3 | 8 | namespace WP_Stream; |
| 4 | 9 | |
| 10 | +/** | |
| 11 | + * Class - Log | |
| 12 | + */ | |
| 5 | 13 | class Log { |
| 6 | 14 | |
| 7 | 15 | /** |
| 8 | - * Hold Plugin class | |
| 16 | + * Holds Instance of plugin object | |
| 9 | 17 | * |
| 10 | 18 | * @var Plugin |
| 11 | 19 | */ |
| 12 | 20 | public $plugin; |
| @@ -28,9 +36,9 @@ | ||
| 28 | 36 | |
| 29 | 37 | /** |
| 30 | 38 | * Class constructor. |
| 31 | 39 | * |
| 32 | - * @param Plugin $plugin The main Plugin class. | |
| 40 | + * @param Plugin $plugin Instance of plugin object. | |
| 33 | 41 | */ |
| 34 | 42 | public function __construct( $plugin ) { |
| 35 | 43 | $this->plugin = $plugin; |
| 36 | 44 | |
| @@ -111,11 +119,8 @@ | ||
| 111 | 119 | |
| 112 | 120 | // Add user meta to Stream meta. |
| 113 | 121 | $stream_meta['user_meta'] = $user_meta; |
| 114 | 122 | |
| 115 | - // Get the current time in milliseconds. | |
| 116 | - $iso_8601_extended_date = wp_stream_get_iso_8601_extended_date(); | |
| 117 | - | |
| 118 | 123 | if ( ! empty( $user->roles ) ) { |
| 119 | 124 | $roles = array_values( $user->roles ); |
| 120 | 125 | $role = $roles[0]; |
| 121 | 126 | } elseif ( is_multisite() && is_super_admin() && $wp_roles->is_role( 'administrator' ) ) { |
| @@ -129,9 +134,9 @@ | ||
| 129 | 134 | 'site_id' => (int) is_multisite() ? get_current_site()->id : 1, |
| 130 | 135 | 'blog_id' => (int) apply_filters( 'wp_stream_blog_id_logged', get_current_blog_id() ), |
| 131 | 136 | 'user_id' => (int) $user_id, |
| 132 | 137 | 'user_role' => (string) $role, |
| 133 | - 'created' => (string) $iso_8601_extended_date, | |
| 138 | + 'created' => (string) current_time( 'mysql', true ), | |
| 134 | 139 | 'summary' => (string) vsprintf( $message, $args ), |
| 135 | 140 | 'connector' => (string) $connector, |
| 136 | 141 | 'context' => (string) $context, |
| 137 | 142 | 'action' => (string) $action, |
| @@ -145,9 +150,9 @@ | ||
| 145 | 150 | |
| 146 | 151 | $result = $this->plugin->db->insert( $recordarr ); |
| 147 | 152 | |
| 148 | 153 | // This is helpful in development environments: |
| 149 | - // error_log( $this->debug_backtrace( $recordarr ) ); | |
| 154 | + // error_log( $this->debug_backtrace( $recordarr ) );. | |
| 150 | 155 | |
| 151 | 156 | return $result; |
| 152 | 157 | } |
| 153 | 158 | |
| @@ -162,8 +167,10 @@ | ||
| 162 | 167 | * |
| 163 | 168 | * @return bool |
| 164 | 169 | */ |
| 165 | 170 | public function is_record_excluded( $connector, $context, $action, $user = null, $ip = null ) { |
| 171 | + $exclude_record = false; | |
| 172 | + | |
| 166 | 173 | if ( is_null( $user ) ) { |
| 167 | 174 | $user = wp_get_current_user(); |
| 168 | 175 | } |
| 169 | 176 | |
| @@ -189,67 +196,28 @@ | ||
| 189 | 196 | ); |
| 190 | 197 | |
| 191 | 198 | $exclude_settings = isset( $this->plugin->settings->options['exclude_rules'] ) ? $this->plugin->settings->options['exclude_rules'] : array(); |
| 192 | 199 | |
| 193 | - if ( is_multisite() && is_plugin_active_for_network( $this->plugin->locations['plugin'] ) && ! is_network_admin() ) { | |
| 194 | - $multisite_options = (array) get_site_option( 'wp_stream_network', array() ); | |
| 195 | - $multisite_exclude_settings = isset( $multisite_options['exclude_rules'] ) ? $multisite_options['exclude_rules'] : array(); | |
| 196 | - | |
| 197 | - if ( ! empty( $multisite_exclude_settings ) ) { | |
| 198 | - foreach ( $multisite_exclude_settings['exclude_row'] as $key => $rule ) { | |
| 199 | - $exclude_settings['exclude_row'][] = $multisite_exclude_settings['exclude_row'][ $key ]; | |
| 200 | - $exclude_settings['author_or_role'][] = $multisite_exclude_settings['author_or_role'][ $key ]; | |
| 201 | - $exclude_settings['connector'][] = $multisite_exclude_settings['connector'][ $key ]; | |
| 202 | - $exclude_settings['context'][] = $multisite_exclude_settings['context'][ $key ]; | |
| 203 | - $exclude_settings['action'][] = $multisite_exclude_settings['action'][ $key ]; | |
| 204 | - $exclude_settings['ip_address'][] = $multisite_exclude_settings['ip_address'][ $key ]; | |
| 205 | - } | |
| 206 | - } | |
| 200 | + if ( is_multisite() && $this->plugin->is_network_activated() && ! is_network_admin() ) { | |
| 201 | + $multisite_options = (array) get_site_option( 'wp_stream_network', array() ); | |
| 202 | + $exclude_settings = isset( $multisite_options['exclude_rules'] ) ? $multisite_options['exclude_rules'] : array(); | |
| 207 | 203 | } |
| 208 | 204 | |
| 209 | - $exclude_record = false; | |
| 205 | + foreach ( $this->exclude_rules_by_rows( $exclude_settings ) as $exclude_rule ) { | |
| 206 | + $exclude = array( | |
| 207 | + 'connector' => ! empty( $exclude_rule['connector'] ) ? $exclude_rule['connector'] : null, | |
| 208 | + 'context' => ! empty( $exclude_rule['context'] ) ? $exclude_rule['context'] : null, | |
| 209 | + 'action' => ! empty( $exclude_rule['action'] ) ? $exclude_rule['action'] : null, | |
| 210 | + 'ip_address' => ! empty( $exclude_rule['ip_address'] ) ? $exclude_rule['ip_address'] : null, | |
| 211 | + 'author' => is_numeric( $exclude_rule['author_or_role'] ) ? absint( $exclude_rule['author_or_role'] ) : null, | |
| 212 | + 'role' => ( ! empty( $exclude_rule['author_or_role'] ) && ! is_numeric( $exclude_rule['author_or_role'] ) ) ? $exclude_rule['author_or_role'] : null, | |
| 213 | + ); | |
| 210 | 214 | |
| 211 | - if ( isset( $exclude_settings['exclude_row'] ) && ! empty( $exclude_settings['exclude_row'] ) ) { | |
| 212 | - foreach ( $exclude_settings['exclude_row'] as $key => $value ) { | |
| 213 | - // Prepare values. | |
| 214 | - $author_or_role = isset( $exclude_settings['author_or_role'][ $key ] ) ? $exclude_settings['author_or_role'][ $key ] : ''; | |
| 215 | - $connector = isset( $exclude_settings['connector'][ $key ] ) ? $exclude_settings['connector'][ $key ] : ''; | |
| 216 | - $context = isset( $exclude_settings['context'][ $key ] ) ? $exclude_settings['context'][ $key ] : ''; | |
| 217 | - $action = isset( $exclude_settings['action'][ $key ] ) ? $exclude_settings['action'][ $key ] : ''; | |
| 218 | - $ip_address = isset( $exclude_settings['ip_address'][ $key ] ) ? $exclude_settings['ip_address'][ $key ] : ''; | |
| 215 | + $exclude_rules = array_filter( $exclude, 'strlen' ); | |
| 219 | 216 | |
| 220 | - $exclude = array( | |
| 221 | - 'connector' => ! empty( $connector ) ? $connector : null, | |
| 222 | - 'context' => ! empty( $context ) ? $context : null, | |
| 223 | - 'action' => ! empty( $action ) ? $action : null, | |
| 224 | - 'ip_address' => ! empty( $ip_address ) ? $ip_address : null, | |
| 225 | - 'author' => is_numeric( $author_or_role ) ? absint( $author_or_role ) : null, | |
| 226 | - 'role' => ( ! empty( $author_or_role ) && ! is_numeric( $author_or_role ) ) ? $author_or_role : null, | |
| 227 | - ); | |
| 228 | - | |
| 229 | - $exclude_rules = array_filter( $exclude, 'strlen' ); | |
| 230 | - | |
| 231 | - if ( ! empty( $exclude_rules ) ) { | |
| 232 | - $matches_exclusion_rule = true; | |
| 233 | - | |
| 234 | - foreach ( $exclude_rules as $exclude_key => $exclude_value ) { | |
| 235 | - if ( 'ip_address' === $exclude_key ) { | |
| 236 | - $ip_addresses = explode( ',', $exclude_value ); | |
| 237 | - if ( ! in_array( $record['ip_address'], $ip_addresses, true ) ) { | |
| 238 | - $matches_exclusion_rule = false; | |
| 239 | - break; | |
| 240 | - } | |
| 241 | - } elseif ( $record[ $exclude_key ] !== $exclude_value ) { | |
| 242 | - $matches_exclusion_rule = false; | |
| 243 | - break; | |
| 244 | - } | |
| 245 | - } | |
| 246 | - | |
| 247 | - if ( $matches_exclusion_rule ) { | |
| 248 | - $exclude_record = true; | |
| 249 | - break; | |
| 250 | - } | |
| 251 | - } | |
| 217 | + if ( $this->record_matches_rules( $record, $exclude_rules ) ) { | |
| 218 | + $exclude_record = true; | |
| 219 | + break; | |
| 252 | 220 | } |
| 253 | 221 | } |
| 254 | 222 | |
| 255 | 223 | /** |
| @@ -265,8 +233,77 @@ | ||
| 265 | 233 | return apply_filters( 'wp_stream_is_record_excluded', $exclude_record, $record ); |
| 266 | 234 | } |
| 267 | 235 | |
| 268 | 236 | /** |
| 237 | + * Check if a record to stored matches certain rules. | |
| 238 | + * | |
| 239 | + * @param array $record List of record parameters. | |
| 240 | + * @param array $exclude_rules List of record exclude rules. | |
| 241 | + * | |
| 242 | + * @return boolean | |
| 243 | + */ | |
| 244 | + public function record_matches_rules( $record, $exclude_rules ) { | |
| 245 | + $matches_needed = count( $exclude_rules ); | |
| 246 | + $matches_found = 0; | |
| 247 | + foreach ( $exclude_rules as $exclude_key => $exclude_value ) { | |
| 248 | + if ( ! isset( $record[ $exclude_key ] ) || is_null( $exclude_value ) ) { | |
| 249 | + continue; | |
| 250 | + } | |
| 251 | + | |
| 252 | + if ( 'ip_address' === $exclude_key ) { | |
| 253 | + $ip_addresses = explode( ',', $exclude_value ); | |
| 254 | + | |
| 255 | + if ( in_array( $record['ip_address'], $ip_addresses, true ) ) { | |
| 256 | + $matches_found++; | |
| 257 | + } | |
| 258 | + } elseif ( $record[ $exclude_key ] === $exclude_value ) { | |
| 259 | + $matches_found++; | |
| 260 | + } | |
| 261 | + } | |
| 262 | + | |
| 263 | + return $matches_found === $matches_needed; | |
| 264 | + } | |
| 265 | + | |
| 266 | + /** | |
| 267 | + * Get all exclude rules by row because we store them by rule instead. | |
| 268 | + * | |
| 269 | + * @param array $rules List of rules indexed by rule ID. | |
| 270 | + * | |
| 271 | + * @return array | |
| 272 | + */ | |
| 273 | + public function exclude_rules_by_rows( $rules ) { | |
| 274 | + $excludes = array(); | |
| 275 | + | |
| 276 | + // TODO: Move these to where the settings are generated to ensure they're in sync. | |
| 277 | + $rule_keys = array( | |
| 278 | + 'exclude_row', | |
| 279 | + 'author_or_role', | |
| 280 | + 'connector', | |
| 281 | + 'context', | |
| 282 | + 'action', | |
| 283 | + 'ip_address', | |
| 284 | + ); | |
| 285 | + | |
| 286 | + if ( empty( $rules['exclude_row'] ) ) { | |
| 287 | + return array(); | |
| 288 | + } | |
| 289 | + | |
| 290 | + foreach ( array_keys( $rules['exclude_row'] ) as $row_id ) { | |
| 291 | + $excludes[ $row_id ] = array(); | |
| 292 | + | |
| 293 | + foreach ( $rule_keys as $rule_key ) { | |
| 294 | + if ( isset( $rules[ $rule_key ][ $row_id ] ) ) { | |
| 295 | + $excludes[ $row_id ][ $rule_key ] = $rules[ $rule_key ][ $row_id ]; | |
| 296 | + } else { | |
| 297 | + $excludes[ $row_id ][ $rule_key ] = null; | |
| 298 | + } | |
| 299 | + } | |
| 300 | + } | |
| 301 | + | |
| 302 | + return $excludes; | |
| 303 | + } | |
| 304 | + | |
| 305 | + /** | |
| 269 | 306 | * Helper function to send a full backtrace of calls to the PHP error log for debugging |
| 270 | 307 | * |
| 271 | 308 | * @param array $recordarr Record argument array. |
| 272 | 309 | * |
| @@ -290,9 +327,10 @@ | ||
| 290 | 327 | unset( $stream_meta['user_meta'] ); |
| 291 | 328 | |
| 292 | 329 | if ( $stream_meta ) { |
| 293 | 330 | array_walk( |
| 294 | - $stream_meta, function ( &$value, $key ) { | |
| 331 | + $stream_meta, | |
| 332 | + function ( &$value, $key ) { | |
| 295 | 333 | $value = sprintf( '%s: %s', $key, ( '' === $value ) ? 'null' : $value ); |
| 296 | 334 | } |
| 297 | 335 | ); |
| 298 | 336 | $stream_meta = implode( ', ', $stream_meta ); |
| @@ -302,9 +340,10 @@ | ||
| 302 | 340 | $user_meta = isset( $recordarr['meta']['user_meta'] ) ? $recordarr['meta']['user_meta'] : null; |
| 303 | 341 | |
| 304 | 342 | if ( $user_meta ) { |
| 305 | 343 | array_walk( |
| 306 | - $user_meta, function ( &$value, $key ) { | |
| 344 | + $user_meta, | |
| 345 | + function ( &$value, $key ) { | |
| 307 | 346 | $value = sprintf( '%s: %s', $key, ( '' === $value ) ? 'null' : $value ); |
| 308 | 347 | } |
| 309 | 348 | ); |
| 310 | 349 | |