create_database_table(); } } public function admintosh_log_activity($user_id, $action_type, $object_type, $object_id, $message) { global $wpdb; $table_name = $wpdb->prefix . 'ats_activity_logs'; $ip_address = \Admintosh\Inc\Helper::get_user_ip_address(); $user_agent = \Admintosh\Inc\Helper::get_user_device_info();; $wpdb->insert( $table_name, [ 'user_id' => $user_id, 'action_type' => sanitize_text_field( $action_type ), 'object_type' => sanitize_text_field( $object_type ), 'object_id' => sanitize_text_field( $object_id ), 'message' => sanitize_text_field( $message ), 'ip_address' => sanitize_text_field( $ip_address ), 'user_agent' => sanitize_text_field( $user_agent['user_agent'] ?? '' ), 'timestamp' => sanitize_text_field( current_time( 'timestamp' ) ) ], ['%d', '%s', '%s', '%d', '%s', '%s', '%s', '%s'] ); } public function log_post_trash( $post_id, $previous_status ) { if( !$post_id ) { return; } $user_id = get_current_user_id(); $title = get_the_title($post_id); $post_type = get_post_type($post_id); $action = 'trash'; $message = "title: $title (ID: $post_id) move to $action."; $this->admintosh_log_activity($user_id, $action, $post_type, $post_id, $message); } public function log_post_changes($post_id, $post, $update) { if (wp_is_post_revision($post_id)) return; $user_id = get_current_user_id(); $action = $update ? 'updated' : 'created'; $post_type = get_post_type($post_id); $message = "title: {$post->post_title} (ID: $post_id) was $action."; $this->admintosh_log_activity($user_id, $action, $post_type, $post_id, $message); } public function log_post_deletion($post_id) { $user_id = get_current_user_id(); $post_type = get_post_type($post_id); $title = get_the_title($post_id); $message = "title: $title (ID: $post_id) was permanently deleted."; $this->admintosh_log_activity($user_id, 'deleted', $post_type, $post_id, $message); } public function log_user_role_change($user_id, $new_roles, $old_roles) { if( !empty( $old_roles ) ) { $message = "User (ID: $user_id) role changed from " . implode(', ', $old_roles) . " to " . $new_roles . "."; $this->admintosh_log_activity(get_current_user_id(), 'role_change', 'user', $user_id, $message); } } public function log_plugin_status($plugin, $network_wide) { $user_id = get_current_user_id(); $plugin_data = get_plugin_data(WP_PLUGIN_DIR . '/' . $plugin); $plugin_name = $plugin_data['Name']; $message = "Plugin '$plugin_name' was activated."; $this->admintosh_log_activity($user_id, 'activated', 'plugin', 0, $message); } public function log_plugin_deactivation($plugin, $network_wide) { $user_id = get_current_user_id(); $plugin_data = get_plugin_data(WP_PLUGIN_DIR . '/' . $plugin); $plugin_name = $plugin_data['Name']; $message = "Plugin '$plugin_name' was deactivated."; $this->admintosh_log_activity($user_id, 'deactivated', 'plugin', 0, $message); } public function log_theme_change( $new_name, $new_theme, $old_theme ) { $user_id = get_current_user_id(); $message = "Theme changed '{$old_theme->get('Name')}' to $new_name."; $this->admintosh_log_activity($user_id, 'theme_change', 'theme', 0, $message); } public function add_activity_log_page() { add_submenu_page( 'admintosh-setting-admin', esc_html__( 'Activity Logs', 'admintosh' ), esc_html__( 'Activity Logs', 'admintosh' ), 'manage_options', 'admintosh-activity-logs', [$this, 'display_activity_logs'] ); } public function display_activity_logs() { global $wpdb; $table_name = $wpdb->prefix . 'ats_activity_logs'; $logs = $wpdb->get_results("SELECT * FROM $table_name ORDER BY timestamp DESC LIMIT 50"); // $dateTimeFormat = get_option( 'date_format' ) . ' ' . get_option( 'time_format' ); ?>

user_id); $display_name = !empty( $user->display_name ) ? $user->display_name : ''; ?>
user_id})" ); ?> action_type ); ?> object_type} (ID: {$log->object_id})" ); ?> message ); ?> ip_address ); ?> timestamp ) ); ?>
prefix . 'ats_activity_logs'; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE IF NOT EXISTS $table_name ( id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, user_id BIGINT UNSIGNED NOT NULL, action_type VARCHAR(255) NOT NULL, object_type VARCHAR(255) NOT NULL, object_id BIGINT UNSIGNED NOT NULL, message TEXT NOT NULL, ip_address VARCHAR(45) NOT NULL, user_agent TEXT NOT NULL, timestamp TEXT NOT NULL ) $charset_collate;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); update_option( 'admintosh_activity_logs_db_table', 'yes' ); } }