| 1 |
<?php |
| 2 |
|
| 3 |
// Do not allow the file to be called directly. |
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* This class is used to log any events that happen on the WordPress site. |
| 10 |
*/ |
| 11 |
class P_Event_Log extends P_Core { |
| 12 |
|
| 13 |
/** |
| 14 |
* Add the actions required for the activity logger. |
| 15 |
* |
| 16 |
* @param Patchstack $core |
| 17 |
* @return void |
| 18 |
*/ |
| 19 |
public function __construct( $core ) { |
| 20 |
parent::__construct( $core ); |
| 21 |
if ( ! $this->get_option( 'patchstack_activity_log_is_enabled', true ) ) { |
| 22 |
return; |
| 23 |
} |
| 24 |
|
| 25 |
// The activity logger feature can only be used on an activated license. |
| 26 |
if ( ! $this->license_is_active() || $this->get_option( 'patchstack_license_free', 0 ) == 1 ) { |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
// Include all the different event loggers. |
| 31 |
foreach ( [ 'posts', 'plugins', 'core', 'options', 'users', 'comments', 'attachment' ] as $event ) { |
| 32 |
require_once dirname( __FILE__ ) . '/events/' . $event . '.php'; |
| 33 |
$class = 'P_Event_' . ucfirst( $event ); |
| 34 |
new $class(); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* If any of our event listeners get triggered, it will insert data using this method. |
| 40 |
* |
| 41 |
* @param array $args The arguments to log. |
| 42 |
* @return void |
| 43 |
*/ |
| 44 |
public function insert( $args ) { |
| 45 |
// Get the author name, if the user is logged in. |
| 46 |
$user = get_user_by( 'id', get_current_user_id() ); |
| 47 |
$author = ! $user ? 'Unauthenticated user' : $user->data->user_login; |
| 48 |
|
| 49 |
// If it's a scheduled task running, we override the name. |
| 50 |
if ( defined( 'DOING_CRON' ) && DOING_CRON ) { |
| 51 |
$author = 'WPCron'; |
| 52 |
} |
| 53 |
|
| 54 |
// Exception for when the action is 'logged in'. |
| 55 |
if ( $args['action'] == 'logged in' ) { |
| 56 |
$author = $args['object_name']; |
| 57 |
} |
| 58 |
|
| 59 |
// Log the action. |
| 60 |
if ( ! is_null( $this->get_ip() ) ) { |
| 61 |
|
| 62 |
// Skip unauthenticated user on post object. |
| 63 |
if ( $author == 'Unauthenticated user' && $args['object'] == 'post' ) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
// Insert into the logs. |
| 68 |
global $wpdb; |
| 69 |
$wpdb->insert( |
| 70 |
$wpdb->prefix . 'patchstack_event_log', |
| 71 |
[ |
| 72 |
'author' => $author, |
| 73 |
'ip' => $this->get_ip(), |
| 74 |
'object' => isset( $args['object'] ) ? $args['object'] : '', |
| 75 |
'object_id' => isset( $args['object_id'] ) ? (int) $args['object_id'] : '', |
| 76 |
'action' => isset( $args['action'] ) ? $args['action'] : '', |
| 77 |
'object_name' => isset( $args['object_name'] ) ? $args['object_name'] : '', |
| 78 |
'date' => current_time( 'mysql' ), |
| 79 |
'flag' => '', |
| 80 |
], |
| 81 |
[ '%s', '%s', '%s', '%d', '%s', '%s', '%s' ] |
| 82 |
); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|