Hello @peperene5150
Can you share some details on when and where you use the wp_insert_user() function? Usually this happens, if the taxonomies are not initiated yet.
Hey there.
This could potentially be the case, we are using this fonction for sso purpose when the sso provider return an user that don’t exist yet in our database
Do you know any way to bypass this behavor OR ensure that the taxonomies are loaded before calling wp_insert_user() ?
Hello @creanoguillaume
The taxonomies are created during the init-phase. I suggest to run your custom code during a later phase or to set a priority which makes it run after the initialization of Asgaros Forum (which uses the priority 10).
Hi thx for your anwser.
(I am working with guillaume)
One thing that I do not understand is that it perfectly works before php8 but when we updrade php version to 8.03 the process you describe in your plugin breaks, liked described in the intro.
regards
N
Can you guys share the snippet of your custom code which you mentioned before (which inserts the new user)? I think there is probably an issue related to this which can be fixed somehow.
Ofc, there is the snippet. what this do is :
1. check if we have a $_POST[“SAMLResponse”] from the sso,
2. Parse that $_POST as $response_idp (array),
3. check if the $response_idp have an av2_interne_id.
4. check if we don’t already have an user with this interne_id (we add it to the users meta when we create the user ) .
5. if we don’t have one, we check if we don’t have an user with the $response_idp[“av2_mail“]
6.then we create the user if all match.
function.php :
$template_directory = get_template_directory();
include "{$template_directory}/functions/ext-sso.php";
ext-sso.php : (part)
if (!empty($_POST["SAMLResponse"])) { // Si retour SSO (après la phase de connection de l'utilisateur)
$dom = new DOMDocument();
$dom->loadXML(base64_decode($_POST["SAMLResponse"]));
$doc = $dom->documentElement;
$xpath = new DOMXpath($dom);
$xpath->registerNamespace('samlp', 'urn:oasis:names:tc:SAML:2.0:protocol');
$xpath->registerNamespace('saml', 'urn:oasis:names:tc:SAML:2.0:assertion');
$response_idp = [];
// Récupération des données d'un retour SAML
foreach ($xpath->query('/samlp:Response/saml:Assertion/saml:AttributeStatement/saml:Attribute', $doc) as $attr) {
foreach ($xpath->query('saml:AttributeValue', $attr) as $value) {
$response_idp[$attr->getAttribute('Name')] = $value->textContent;
}
}
foreach ($xpath->query('/samlp:Response/saml:Assertion/saml:Subject/saml:NameID', $doc) as $value) {
$nameID = $value->textContent;
}
$response_idp["av2_interne_id"] = !empty($response_idp["av2_interne_id"]) ? trim($response_idp["av2_interne_id"]) : "";
global $wpdb;
$user_id = $nameID = null;
$new_user_role = "subscriber"; // Rôle attribuer aux users
// Le SSO peut ne pas renvoyer de données et provoquer une connexion avec un compte qui n'est pas le sien.
if (!empty($response_idp["av2_interne_id"])) {
// On cherche à savoir s'il n'existe pas un utilisateur avec le même sso_interne_id (il est sensé être unique => un UNIQUE user)
$users = get_users([
'meta_key' => 'sso_interne_id',
'meta_value' => $response_idp["av2_interne_id"],
]);
// L'utilisateur n'existe pas
if (empty($users)) {
//! un compte wp peut deja exister mais ne pas être relier au sso (cas rare de création de compte a la main ...)
$user_candidate = get_user_by('email', $response_idp["av2_mail"]);
//! si pas d'utilisateur detecter avec le mail, on cherche avec le login : (l'utilisateur a changer de mail par n moyen avant de bind avec le sso).
$user_candidate = empty($user_candidate) ? get_user_by('login', $response_idp["av2_mail"]) : $user_candidate;
// Création de compte classique pour un nouvel utilisateur
if (!$user_candidate) {
$user_id = wp_insert_user([
"user_login" => $response_idp["av2_mail"],
"first_name" => $response_idp["av2_prenom"],
"last_name" => $response_idp["av2_nom"],
"user_pass" => wp_generate_password(/* length */20, /* special_chars */ true, /* extra_special_chars */ true),
"user_email" => $response_idp["av2_mail"],
"display_name" => "{$response_idp["av2_prenom"]} {$response_idp["av2_nom"]}",
"nickname" => "{$response_idp["av2_prenom"]} {$response_idp["av2_nom"]}",
"role" => $new_user_role
]);
Hi @asgaros,
can you comeback to us on this thread please?
regards
Hello @peperene5150
I dont see any code related to Asgaros Forum there. But you could try to put it around a wrapper-function so that it gets executed during the init-phase earliest. For example:
add_action('init', 'my_initialize_function');
function my_initialize_function() {
// Your Code
}
Hi, in parallele of this, we tried to configure our site with php7.4 and everything works well but when we activate php8 then we got a php error that indicates an Asgaros error :
PHP Fatal error: Uncaught TypeError: array_filter(): Argument #1 ($array) must be of type array, WP_Error given in /var/www/vhosts/mysite.com/httpdocs/wp-content/plugins/asgaros-forum/includes/forum-usergroups.php:258 Stack trace: #0 /var/www/vhosts/mysite.com/httpdocs/wp-content/plugins/asgaros-forum/includes/forum-usergroups.php(258): array_filter() #1 /var/www/vhosts/mysite.com/httpdocs/wp-content/plugins/asgaros-forum/includes/forum-usergroups.php(794): AsgarosForumUserGroups::getUserGroups() #2 /var/www/vhosts/mysite.com/httpdocs/wp-includes/class-wp-hook.php(312): AsgarosForumUserGroups->add_new_user_to_usergroups() #3 /var/www/vhosts/mysite.com/httpdocs/wp-includes/class-wp-hook.php(334): WP_Hook->apply_filters() #4 /var/www/vhosts/mysite.com/httpdocs/wp-includes/plugin.php(517): WP_Hook->do_action() #5 /var/www/vhosts/mysite.com/httpdocs/wp-includes/user.php(2497): do_action() #8 /var/www/vhosts/mysite.com/httpdocs/wp-settings.php(600): include('...') #9 /var/www/vhosts/mysite.com/wp-config.php(112): require_once('...') #10 /var/www/vhosts/mysite.com/httpdocs/wp-load.php(55): require_once('...') #11 /var/www/vhosts/mysite.com/httpdocs/wp-login.php(12): require('...') #12 {main} thrown in /var/www/vhosts/mysite.com/httpdocs/wp-content/plugins/asgaros-forum/includes/forum-usergroups.php on line 258
Have you tried the code-change I described above? The issue is caused by creating an user while not all post-types/taxonomies are initialized yet. So the user-creation should not happen before the init-stage.
Not sure why it only happens with PHP8, but I guess PHP8 is more strict now compared to 7.