activity-log/classes/class-aal-api.php

194 lines
4.3 KiB
PHP
Raw Normal View History

2013-04-02 21:23:02 +03:00
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
2013-04-05 11:05:54 +03:00
2013-04-05 18:38:25 +03:00
class AAL_API {
2013-04-02 21:23:02 +03:00
public function __construct() {
2023-07-30 10:41:18 +03:00
add_action( 'admin_init', [ $this, 'maybe_add_schedule_delete_old_items' ] );
add_action( 'aal/maintenance/clear_old_items', [ $this, 'delete_old_items' ] );
}
2023-07-30 10:41:18 +03:00
public function maybe_add_schedule_delete_old_items() {
if ( ! wp_next_scheduled( 'aal/maintenance/clear_old_items' ) ) {
wp_schedule_event( time(), 'daily', 'aal/maintenance/clear_old_items' );
}
}
public function delete_old_items() {
2013-04-03 10:20:43 +03:00
global $wpdb;
$logs_lifespan = absint( AAL_Main::instance()->settings->get_option( 'logs_lifespan' ) );
if ( empty( $logs_lifespan ) ) {
2013-05-18 22:53:03 +03:00
return;
}
2014-06-22 13:57:43 +03:00
$wpdb->query(
$wpdb->prepare(
'DELETE FROM `' . $wpdb->activity_log . '`
WHERE `hist_time` < %d',
2014-06-22 13:57:43 +03:00
strtotime( '-' . $logs_lifespan . ' days', current_time( 'timestamp' ) )
)
);
2013-04-03 10:20:43 +03:00
}
2014-02-27 21:35:16 +00:00
2014-06-22 14:11:31 +03:00
/**
* Get real address
*
2014-06-22 14:11:31 +03:00
* @since 2.1.4
* @return string real address IP
*/
protected function _get_ip_address() {
$header_key = AAL_Main::instance()->settings->get_option( 'log_visitor_ip_source' );
if ( empty( $header_key ) ) {
$header_key = 'REMOTE_ADDR';
}
if ( 'no-collect-ip' === $header_key ) {
return '';
}
$visitor_ip_address = '';
if ( ! empty( $_SERVER[ $header_key ] ) ) {
$visitor_ip_address = $_SERVER[ $header_key ];
2014-06-22 14:11:31 +03:00
}
$remote_address = apply_filters( 'aal_get_ip_address', $visitor_ip_address );
if ( ! empty( $remote_address ) && filter_var( $remote_address, FILTER_VALIDATE_IP ) ) {
return $remote_address;
}
2014-06-22 14:11:31 +03:00
return '127.0.0.1';
}
2014-02-27 21:35:16 +00:00
/**
* @since 2.0.0
* @return void
*/
2014-02-27 21:28:45 +00:00
public function erase_all_items() {
2014-02-04 23:09:33 +00:00
global $wpdb;
$wpdb->query( 'TRUNCATE `' . $wpdb->activity_log . '`' );
2014-02-04 23:09:33 +00:00
}
2013-04-03 10:20:43 +03:00
2014-02-27 21:35:16 +00:00
/**
* @since 1.0.0
*
2014-02-27 21:35:16 +00:00
* @param array $args
* @return void
*/
2014-02-27 21:28:45 +00:00
public function insert( $args ) {
2013-04-02 21:23:02 +03:00
global $wpdb;
2014-08-19 11:11:31 +03:00
$args = wp_parse_args(
$args,
array(
'action' => '',
'object_type' => '',
'object_subtype' => '',
'object_name' => '',
'object_id' => '',
'hist_ip' => $this->_get_ip_address(),
'hist_time' => current_time( 'timestamp' ),
)
);
$args = $this->setup_userdata( $args );
$should_skip_insert = apply_filters( 'aal_skip_insert_log', false, $args );
if ( $should_skip_insert ) {
return;
}
2013-06-03 00:17:22 +03:00
// Make sure for non duplicate.
2014-06-22 13:57:43 +03:00
$check_duplicate = $wpdb->get_row(
$wpdb->prepare(
'SELECT `histid` FROM `' . $wpdb->activity_log . '`
WHERE `user_caps` = %s
AND `action` = %s
AND `object_type` = %s
AND `object_subtype` = %s
AND `object_name` = %s
AND `user_id` = %s
AND `hist_ip` = %s
AND `hist_time` = %s
2014-06-22 13:57:43 +03:00
;',
$args['user_caps'],
$args['action'],
$args['object_type'],
$args['object_subtype'],
$args['object_name'],
$args['user_id'],
$args['hist_ip'],
$args['hist_time']
)
);
if ( $check_duplicate ) {
2013-06-03 00:17:22 +03:00
return;
}
2013-04-02 21:23:02 +03:00
2014-06-22 13:57:43 +03:00
$wpdb->insert(
$wpdb->activity_log,
2013-04-02 21:23:02 +03:00
array(
'action' => $args['action'],
'object_type' => $args['object_type'],
'object_subtype' => $args['object_subtype'],
'object_name' => $args['object_name'],
'object_id' => $args['object_id'],
'user_id' => $args['user_id'],
2013-04-03 19:44:33 +03:00
'user_caps' => $args['user_caps'],
2013-04-12 11:23:33 +03:00
'hist_ip' => $args['hist_ip'],
2013-04-03 19:44:33 +03:00
'hist_time' => $args['hist_time'],
2013-04-02 21:23:02 +03:00
),
2014-06-22 13:57:43 +03:00
array( '%s', '%s', '%s', '%s', '%d', '%d', '%s', '%s', '%d' )
2013-04-02 21:23:02 +03:00
);
2013-04-05 18:38:25 +03:00
do_action( 'aal_insert_log', $args );
2013-04-02 21:23:02 +03:00
}
private function setup_userdata( $args ) {
$user = false;
if ( function_exists( 'get_user_by' ) ) {
$user = get_user_by( 'id', get_current_user_id() );
}
if ( $user ) {
$args['user_caps'] = strtolower( key( $user->caps ) );
if ( empty( $args['user_id'] ) ) {
$args['user_id'] = $user->ID;
}
} else {
$args['user_caps'] = 'guest';
if ( empty( $args['user_id'] ) ) {
$args['user_id'] = 0;
}
}
// TODO: Find better way to Multisite compatibility.
// Fallback for multisite with bbPress
if ( empty( $args['user_caps'] ) || 'bbp_participant' === $args['user_caps'] ) {
$args['user_caps'] = 'administrator';
}
return $args;
}
2013-04-02 21:23:02 +03:00
}
2014-02-27 21:35:16 +00:00
/**
* @since 1.0.0
*
2014-02-27 21:35:16 +00:00
* @see AAL_API::insert
*
* @param array $args
* @return void
*/
2013-04-05 18:38:25 +03:00
function aal_insert_log( $args = array() ) {
2014-02-27 21:28:45 +00:00
AAL_Main::instance()->api->insert( $args );
2013-04-02 21:23:02 +03:00
}