Last active
October 22, 2021 02:27
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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