Custom Logger Code
-
I tried to come up with code for a Custom Logger for logged-in users page visits tracking, based on the example in the documentation.
Any suggestions are welcome. Below is my code:
if (!class_exists('\Simple_History\Loggers\Logger')) { return; } add_action( 'simple_history/add_custom_logger', function ($simple_history) { $simple_history->register_logger('User_Page_Visit_Logger'); } ); class User_Page_Visit_Logger extends \Simple_History\Loggers\Logger { public $slug = 'User_Page_Visit_Logger'; public function getInfo() { return array( 'name' => __('User Page Visit Logger', 'simple-history'), 'description' => __('Logs page visits by logged-in users', 'simple-history'), 'capability' => 'read', 'messages' => array( 'page_visit' => __('User {user_id} ({user_name}, {user_email}) visited "{post_title}" ({post_permalink})', 'simple-history'), ), 'labels' => array( 'search' => array( 'label' => __('User Page Visits', 'simple-history'), 'options' => array( __('Page Visits', 'simple-history') => array( 'page_visit', ), ), ), ), ); } public function loaded() { if (is_user_logged_in()) { add_action('wp', array($this, 'log_page_visit')); } } public function log_page_visit() { $current_user = wp_get_current_user(); $post = get_queried_object(); if ($current_user && $post) { $context = array( '_initiator' => LogInitiators::WEB_USER, 'user_id' => $current_user->ID, 'user_name' => $current_user->display_name, 'user_email' => $current_user->user_email, 'post_title' => get_the_title($post), 'post_permalink' => get_permalink($post), ); $this->infoMessage('page_visit', $context); } } }
Viewing 8 replies - 1 through 8 (of 8 total)
Viewing 8 replies - 1 through 8 (of 8 total)
- The topic ‘Custom Logger Code’ is closed to new replies.