Skip to content

Instantly share code, notes, and snippets.

@enlacee
Last active October 22, 2021 02:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enlacee/393ad1e11f0885d5a11764806554578c to your computer and use it in GitHub Desktop.
Save enlacee/393ad1e11f0885d5a11764806554578c to your computer and use it in GitHub Desktop.
Save login of all users in WP (you must be create a table named wp_metrix)
<?php
/*
* track logIn (save data in table metric)
*/
add_action( 'wp_login', function($login, $user) {
if ( class_exists('\MinderOptions\Core\Chatbot') == true ) {
$keycode = 'sign_in';
$stringValue = time(); // save time in UNIX format
$userId = $user->ID;
$rs = \MinderOptions\Core\Chatbot::saveCodeMetricByREST($keycode, $stringValue, $userId);
if ( $rs === false ) {
error_log( "Logs sign_in USEID={$user->ID}: Logins where not registered" );
}
}
}, 10, 2);
// ##########
// static function using APIREST
// ##########
/*
* save data in table codeMetricx by API-REST
*
* @return
*/
public static function saveCodeMetricByREST( $keycode, $stringValue, $userId ) {
$result = false;
$json_data = array(
'code' => $keycode,
'value' => $stringValue,
'user_id' => $userId,
);
$URLpure = "/api/v1/code-metric";
$request = new \WP_REST_Request( 'POST', $URLpure );
$request->set_header( 'content-type', 'application/json' );
$request->set_body( json_encode($json_data) );
$response = rest_do_request( $request );
$data = $response->get_data();
if ( isset($data['success']) == true && $data['success'] == true ) {
$result = true; // data structure is CORRECT! (therefore the data was registered!)
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment