File: /home/storetra12/www/hubstaff-operations/create-members.php
<?php
require_once('../wp-load.php');
require_once('global-vars.php');
function write_to_member_import_log($message) {
// Get WordPress root directory
$log_file = ABSPATH . 'create_member_logs.txt';
// Format message with timestamp
$log_message = "[" . date("Y-m-d H:i:s") . "] " . $message . PHP_EOL;
// Write to log file (append mode)
file_put_contents($log_file, $log_message, FILE_APPEND | LOCK_EX);
}
function create_members_hubstaff_table_if_not_exists() {
global $wpdb;
$table_name = $wpdb->prefix . 'hubstaff_members';
// Check if the table already exists
if ( $wpdb->get_var( "SHOW TABLES LIKE '{$table_name}'" ) != $table_name ) {
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
user_id BIGINT(20) NOT NULL,
name VARCHAR(50) DEFAULT '',
last_client_activity DATETIME DEFAULT NULL,
status VARCHAR(50) DEFAULT '',
api_for_user ENUM('y','n') NOT NULL DEFAULT 'n',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}
}
// // Uncomment the following line to run it once.
create_members_hubstaff_table_if_not_exists();
$organization_id = HUBSTAFF_ORG_ID;
$currentMonth = date('m');
$currentYear = date('Y');
global $wpdb;
$table_name = $wpdb->prefix . 'hubstaff_members';
$members_array = $wpdb->get_results("SELECT * FROM $table_name");
if (isset($_GET['resetapi'])) {
$reset_status = $_GET['resetapi'];
if($reset_status == 'yes')
{
$result = $wpdb->query($wpdb->prepare("UPDATE $table_name SET api_for_user='n' "));
if($result === false) {
echo "❌ Database error: " . $wpdb->last_error;
}elseif($result === 0) {
echo "ℹ️ No rows were affected. It may have already been 'n'.";
} else {
echo "✅ Successfully updated $result row(s).";
}
}
}else{
$base_url = "https://api.hubstaff.com/v2/organizations/$organization_id/members?include_removed=false&include_profile=false&include_projects=false&include_limits=false&page_limit=500";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $base_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
'Authorization: Bearer '.HUBSTAFF_ACCESS_TOKEN
),
));
$response = curl_exec($curl);
$members = json_decode($response, true);
curl_close($curl);
if( !isset($member['code']) ){
$members = $members['members'];
/* ONLY GET USERS ARE ACTIVELY WORKED ON */
$filtered_members = array_filter($members, function($member) use ($currentMonth, $currentYear) {
if (!empty($member['last_client_activity'])) {
$activityDate = date_create($member['last_client_activity']);
return $activityDate &&
date_format($activityDate, 'm') === $currentMonth &&
date_format($activityDate, 'Y') === $currentYear;
}
return false;
});
$finalized_users = array_values($filtered_members);
if (is_array($finalized_users)) {
// GET THE USER INFORMATION
foreach($finalized_users as $key => $user_array){
$userid = $user_array['user_id'];
$last_client_acstivity = $user_array['last_client_activity'];
$name = '';
$user_idsaved = $wpdb->get_results("SELECT id FROM $table_name where user_id = $userid");
if( empty($user_idsaved))
{
$userinfo = get_user_from_Hubstaff($userid);
if( isset($userinfo['code']) ){
wait_some_time_before_call();
$userinfo = get_user_from_Hubstaff($userid);
}
$members_array = $userinfo['user'];
$name = $members_array['name'];
$insert_member_status = $wpdb->insert(
$table_name,
[
'user_id' => $userid,
'name' => $name,
'last_client_activity' => $last_client_acstivity,
'status' => 'active',
'api_for_user'=> 'n',
],
[ '%d', '%s', '%s', '%s', '%s' ] // Format types
);
// Check if insert was successful
if (false === $insert_member_status) {
echo 'Insert failed: ' . $wpdb->last_error;
} else {
write_to_member_import_log(" Insert success Member: $name ");
echo 'Insert success Member:'.$name;
}
}else{
$userinfo = get_user_from_Hubstaff($userid);
if( isset($userinfo['code']) ){
wait_some_time_before_call();
$userinfo = get_user_from_Hubstaff($userid);
}
$members_array = $userinfo['user'];
$name = $members_array['name'];
$result = $wpdb->update(
$table_name,
[
'api_for_user' => 'n',
'last_client_activity' => $last_client_acstivity,
'name' => $name,
],
[ 'user_id' => $userid ], // WHERE clause`
[ '%s' ], // Value formats
[ '%s' ], // Value formats
[ '%s' ], // Value formats
[ '%d' ] // WHERE clause format
);
write_to_member_import_log(" Member Updated : $name User ID: $userid ");
}
} //if loop for checking array is not empty
} //foreach end
} /* if rate limit not empty */else{
wait_some_time_before_call();
}/* ELSE IF RATE LIMIT OVER */
} /* else loop if not reset data then change api status */
function wait_some_time_before_call(){
write_to_member_import_log(" ℹ️ WAITING FOR 300 SECOND TO PASS FOR NEXT API CALL ");
sleep(305);
}
function get_user_from_Hubstaff($userid){
sleep(10);
$userinfo = '';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.hubstaff.com/v2/users/'.$userid,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
'Authorization: Bearer '.HUBSTAFF_ACCESS_TOKEN
),
));
$user_information = curl_exec($curl);
curl_close($curl);
$userinfo = json_decode($user_information, true);
return $userinfo;
}