So I want to get the saved database option and then update it with the API return and then save the option in the database again, but I'm having some issues.
So I have the following method:
public static function refresh_access_token(string $user_id, string $access_token): bool
{
if (!$authenticated_users = get_option('instagram_authenticated_users')) {
return false;
}
// The code for the cURL request is here (It's not needed for this question).
$array = json_decode($curl_exec);
foreach ($authenticated_users as $user) {
if ($user['user_id'] === (int) $user_id) {
$user['access_token'] = $array->access_token;
$user['access_token_expiration'] = time() + $array->expires_in;
$user['last_updated'] = (string) time();
}
}
return update_option('instagram_authenticated_users', $user);
}
The cURL response $array
gives me the following:
$array = {stdClass} [3]
access_token = "IGQVJVV1YzUEdMb1lDajNZAcmRxakR3V0dkOXUyWHdtSGM0ZAHFYempkX0VHYVlKYTVYMkNxYVVpNVFuclZAlWVRsbmktNjN2cG1ISEJ4T2VJWUd0M2JBMGcyUlFXOWFlTjdEVDhKaEJB"
token_type = "bearer"
expires_in = {int} 5099901
Now within $authenticated_users
, I have an array of arrays which outputs:
$authenticated_users = {array} [2]
[0] = {array} [5]
username = "semi_test1"
user_id = {int} 17841449642220098
access_token = "IGQVJWMDJnblBnUGMtMTVFa1NpUGdValNBVUZAyZAWM2OTdSSkRFdmNUbnVOQXJqeFhwbDVmT0c3aXJfamFYdnZANSlpXc3Mwc05PS0tMSzNsbXhES0tkTzNoOEY3RFRIb3dsblBiTXN3"
access_token_expiration = {int} 1651281005
last_updated = {int} 1646181108
[1] = {array} [5]
username = "semi_test2"
user_id = {int} 17841400835712753
access_token = "IGQVJVN3VOaUJrU2NzdGxWVTlwaXFLT2h1bnpFU3FKaEpOUGNPeWh2SkpjdHpnRXkyaGJ3NDZArXzJvRWFHdVRqZAEFfN0RodjV4cHQ2YTliSmhyVThUSjlCc1paLV9Fd2dqbzI1b25B"
access_token_expiration = {int} 1651281136
last_updated = {int} 1646181136
Now I'm looking at my $user_id
param and using the foreach to compare them to the user_id
inside an array, and if it matches, update the values inside the array and then update the options, but also retain the other array that hasn't been changed.
I've tried everything and it doesn't seem like it's working and the update_option('instagram_authenticated_users')
doesn't update the values and it retains the same data.
So I want get_option('instagram_authenticated_userss')
when called after the update to be the following with the new data (The second index should have been updated):
$authenticated_users = {array} [2]
[0] = {array} [5]
username = "semi_test1"
user_id = {int} 17841449642220098
access_token = "IGQVJWMDJnblBnUGMtMTVFa1NpUGdValNBVUZAyZAWM2OTdSSkRFdmNUbnVOQXJqeFhwbDVmT0c3aXJfamFYdnZANSlpXc3Mwc05PS0tMSzNsbXhES0tkTzNoOEY3RFRIb3dsblBiTXN3"
access_token_expiration = {int} 1651281005
last_updated = {int} 1646181108
[1] = {array} [5]
username = "semi_test2"
user_id = {int} 17841400835712753
access_token = "IGQVJYOEdqQ0hpbmZAHWlFsdDdMNHdUN1hmenhlV2ZAYOTBtMTJiaFhtSjhyUW9DVm9UREtLZAlFQVHhuVE1XUUFBNUF5SHoxdWRJNXd5dUF6ZAkNKeEtNYmVzRzNTWXdGSmhldG9ILTdn" (NEW VALUE)
access_token_expiration = {int} 1651282134 (NEW VALUE)
last_updated = {int} 1646181136 (NEW VALUE)
Can someone spot that I might be doing wrong?