-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce advanced Exclude tab to replace Connectors tab #251 #278
Changes from 10 commits
75f8d91
93f67d3
f0055d5
be7107e
6ce5757
0b32a07
53967d3
f675f64
dda9bd5
dad30d9
73fca85
94f5fbf
5d6bd93
0a487d7
bcc7116
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,9 @@ public static function register() { | |
if ( ! self::is_logging_enabled_for_user() ) { | ||
return; | ||
} | ||
if ( ! self::is_logging_enabled_for_ip() ) { | ||
return; | ||
} | ||
|
||
foreach ( $class::$actions as $action ) { | ||
add_action( $action, array( $class, 'callback' ), null, 5 ); | ||
|
@@ -97,8 +100,44 @@ public static function is_logging_enabled_for_user( $user = null ) { | |
} else { | ||
// If a user is part of a role that we don't want to log, we disable it | ||
$user_roles = array_values( $user->roles ); | ||
$roles_logged = WP_Stream_Settings::$options['general_log_activity_for']; | ||
$bool = ! ( count( array_intersect( $user_roles, $roles_logged ) ) === 0 ); | ||
$roles_logged = WP_Stream_Settings::$options['exclude_authors_and_roles']; | ||
$bool = ( count( array_intersect( $user_roles, $roles_logged ) ) === 0 ); | ||
//Check user id in exclude array | ||
if ( $bool ){ | ||
$bool = ! ( in_array( $user->ID , $roles_logged ) ); | ||
} | ||
} | ||
|
||
/** | ||
* Filter sets boolean result value for this method | ||
* | ||
* @param bool | ||
* @param obj $user Current user object | ||
* @param string Current class name | ||
* @return bool | ||
*/ | ||
return apply_filters( 'wp_stream_record_log', $bool, $user, get_called_class() ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @faishal nice! even including hook docs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @faishal Unless we have this filter somewhere else with the same name, and receiving same input, we should change the filter name to reflect what it really does. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @faishal Also wanted to leave a /five for this! |
||
} | ||
|
||
/** | ||
* Check if we need to record action for IP | ||
* | ||
* @param null $ip | ||
* | ||
* @return mixed|void | ||
*/ | ||
public static function is_logging_enabled_for_ip( $ip = null ) { | ||
if ( is_null( $ip ) ){ | ||
$ip = filter_input( INPUT_SERVER, 'REMOTE_ADDR', FILTER_VALIDATE_IP ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @faishal We have recently switched to |
||
} else { | ||
$ip = filter_var( $ip, FILTER_VALIDATE_IP ); | ||
} | ||
|
||
// If ip is not valid the we will log the action | ||
if ( $ip === false ) { | ||
$bool = true; | ||
} else { | ||
$bool = self::is_logging_enabled( 'ip_addresses', $ip ); | ||
} | ||
|
||
/** | ||
|
@@ -109,9 +148,43 @@ public static function is_logging_enabled_for_user( $user = null ) { | |
* @param string Current class name | ||
* @return bool | ||
*/ | ||
|
||
$user = wp_get_current_user(); | ||
return apply_filters( 'wp_stream_record_log', $bool, $user, get_called_class() ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @faishal I'm not sure of the purpose of this filter here, since it handles users not IPs while inside the check for exclusion of IPs. |
||
} | ||
|
||
/** | ||
* @param $action string action slug to check whether logging is enable or not for that action | ||
* | ||
* @return bool | ||
*/ | ||
public static function is_logging_enabled_for_action( $action ) { | ||
return self::is_logging_enabled( 'actions', $action ); | ||
} | ||
|
||
/** | ||
* @param $context string context slug to check whether logging is enable or not for that context | ||
* | ||
* @return bool | ||
*/ | ||
public static function is_logging_enabled_for_context( $context ) { | ||
return self::is_logging_enabled( 'contexts', $context ); | ||
} | ||
|
||
/** | ||
* This function is use to check whether logging is enabled | ||
* | ||
* @param $column string name of the setting key (actions|ip_addresses|contexts|connectors) | ||
* @param $value string to check in excluded array | ||
* @return array | ||
*/ | ||
public static function is_logging_enabled( $column, $value ){ | ||
|
||
$excluded_values = WP_Stream_Settings::get_excluded_by_key( $column ); | ||
$bool = ( ! in_array( $value, $excluded_values ) ) ; | ||
|
||
return $bool; | ||
} | ||
/** | ||
* Log handler | ||
* | ||
|
@@ -125,6 +198,17 @@ public static function is_logging_enabled_for_user( $user = null ) { | |
* @return void | ||
*/ | ||
public static function log( $message, $args, $object_id, $contexts, $user_id = null ) { | ||
//Prevent inserting Excluded Context & Actions | ||
foreach ( $contexts as $context => $action ){ | ||
if ( ! self::is_logging_enabled_for_context( $context ) ){ | ||
unset( $contexts[$context] ); | ||
} else if ( ! self::is_logging_enabled_for_action( $action ) ){ | ||
unset( $contexts[$context] ); | ||
} | ||
} | ||
if ( count( $contexts ) == 0 ){ | ||
return ; | ||
} | ||
$class = get_called_class(); | ||
|
||
return WP_Stream_Log::get_instance()->log( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding checks in the base class of all connector will cause it to be executed by all connectors while they try to register themselves, which is something we probably don't need. We probably need to move those checks ( including
is_logging_enabled_for_user
by @jonathanbardo ) toincludes/connectors.php
so it is done once on plugin load.@fjarrett @jonathanbardo What do you think ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I think it would be more efficient.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@faishal If you agree as well on this, please carry on with moving any redundant checks ( IP / User exclusion ) to the
WP_Stream_Connectors
class.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shadyvb I am also agree with that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shadyvb should i move functions
is_logging_enabled_for_user
&is_logging_enabled_for_ip
intoWP_Stream_Settings
because it is a part ofsettings
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer putting them in the same class that'll use them, just to make it easier for people dealing with the same functionality later. Unless other classes would be using them, which is a far aim in this scenario.