@@ -163,6 +163,8 @@ function ( $var ) {
163163 * @return bool
164164 */
165165 public function is_record_excluded ( $ connector , $ context , $ action , $ user = null , $ ip = null ) {
166+ $ exclude_record = false ;
167+
166168 if ( is_null ( $ user ) ) {
167169 $ user = wp_get_current_user ();
168170 }
@@ -191,64 +193,25 @@ public function is_record_excluded( $connector, $context, $action, $user = null,
191193 $ exclude_settings = isset ( $ this ->plugin ->settings ->options ['exclude_rules ' ] ) ? $ this ->plugin ->settings ->options ['exclude_rules ' ] : array ();
192194
193195 if ( is_multisite () && $ this ->plugin ->is_network_activated () && ! 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- }
196+ $ multisite_options = (array ) get_site_option ( 'wp_stream_network ' , array () );
197+ $ exclude_settings = isset ( $ multisite_options ['exclude_rules ' ] ) ? $ multisite_options ['exclude_rules ' ] : array ();
207198 }
208199
209- $ exclude_record = false ;
200+ foreach ( $ this ->exclude_rules_by_rows ( $ exclude_settings ) as $ exclude_rule ) {
201+ $ exclude = array (
202+ 'connector ' => ! empty ( $ exclude_rule ['connector ' ] ) ? $ exclude_rule ['connector ' ] : null ,
203+ 'context ' => ! empty ( $ exclude_rule ['context ' ] ) ? $ exclude_rule ['context ' ] : null ,
204+ 'action ' => ! empty ( $ exclude_rule ['action ' ] ) ? $ exclude_rule ['action ' ] : null ,
205+ 'ip_address ' => ! empty ( $ exclude_rule ['ip_address ' ] ) ? $ exclude_rule ['ip_address ' ] : null ,
206+ 'author ' => is_numeric ( $ exclude_rule ['author_or_role ' ] ) ? absint ( $ exclude_rule ['author_or_role ' ] ) : null ,
207+ 'role ' => ( ! empty ( $ exclude_rule ['author_or_role ' ] ) && ! is_numeric ( $ exclude_rule ['author_or_role ' ] ) ) ? $ exclude_rule ['author_or_role ' ] : null ,
208+ );
210209
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 ] : '' ;
219-
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- }
210+ $ exclude_rules = array_filter ( $ exclude , 'strlen ' );
211+
212+ if ( $ this ->record_matches_rules ( $ record , $ exclude_rules ) ) {
213+ $ exclude_record = true ;
214+ break ;
252215 }
253216 }
254217
@@ -265,6 +228,73 @@ public function is_record_excluded( $connector, $context, $action, $user = null,
265228 return apply_filters ( 'wp_stream_is_record_excluded ' , $ exclude_record , $ record );
266229 }
267230
231+ /**
232+ * Check if a record to stored matches certain rules.
233+ *
234+ * @param array $record List of record parameters.
235+ * @param array $exclude_rules List of record exclude rules.
236+ *
237+ * @return boolean
238+ */
239+ public function record_matches_rules ( $ record , $ exclude_rules ) {
240+ foreach ( $ exclude_rules as $ exclude_key => $ exclude_value ) {
241+ if ( ! isset ( $ record [ $ exclude_key ] ) ) {
242+ continue ;
243+ }
244+
245+ if ( 'ip_address ' === $ exclude_key ) {
246+ $ ip_addresses = explode ( ', ' , $ exclude_value );
247+
248+ if ( in_array ( $ record ['ip_address ' ], $ ip_addresses , true ) ) {
249+ return true ;
250+ }
251+ } elseif ( $ record [ $ exclude_key ] === $ exclude_value ) {
252+ return true ;
253+ }
254+ }
255+
256+ return false ;
257+ }
258+
259+ /**
260+ * Get all exclude rules by row because we store them by rule instead.
261+ *
262+ * @param array $rules List of rules indexed by rule ID.
263+ *
264+ * @return array
265+ */
266+ public function exclude_rules_by_rows ( $ rules ) {
267+ $ excludes = array ();
268+
269+ // TODO: Move these to where the settings are generated to ensure they're in sync.
270+ $ rule_keys = array (
271+ 'exclude_row ' ,
272+ 'author_or_role ' ,
273+ 'connector ' ,
274+ 'context ' ,
275+ 'action ' ,
276+ 'ip_address ' ,
277+ );
278+
279+ if ( empty ( $ rules ['exclude_row ' ] ) ) {
280+ return array ();
281+ }
282+
283+ foreach ( array_keys ( $ rules ['exclude_row ' ] ) as $ row_id ) {
284+ $ excludes [ $ row_id ] = array ();
285+
286+ foreach ( $ rule_keys as $ rule_key ) {
287+ if ( isset ( $ rules [ $ rule_key ][ $ row_id ] ) ) {
288+ $ excludes [ $ row_id ][ $ rule_key ] = $ rules [ $ rule_key ][ $ row_id ];
289+ } else {
290+ $ excludes [ $ row_id ][ $ rule_key ] = null ;
291+ }
292+ }
293+ }
294+
295+ return $ excludes ;
296+ }
297+
268298 /**
269299 * Helper function to send a full backtrace of calls to the PHP error log for debugging
270300 *
0 commit comments