<?php

/*
 * cass_shortcode_member_register - Action fo register a new member
 *
 * This part is used to register a new member
 */

add_action( 'admin_post_member_register', 'prefix_admin_member_register_callback' );
add_action( 'admin_post_nopriv_member_register', 'prefix_admin_member_register_callback' );

function prefix_admin_member_register_callback() {
  $response = array();
  $response['success'] = true;
  $response['http_status'] = 200;
  $response['message'] = 'Inscription réussie';

  //Verify if the data are set
  if(isset($_POST['inputLast']) && isset($_POST['inputFirst']) && isset($_POST['inputMail']) && isset($_POST['inputPassword']) && isset($_POST['inputMemberNo']) && isset($_POST['cassLevel']) && isset($_POST['cassGroup']) && isset($_POST['inputTelephone']) && isset($_POST['inputAddress']) && isset($_POST['inputNPA']) && isset($_POST['inputLocality']) &&
  $_POST['inputLast']!="" && $_POST['inputFirst']!="" && $_POST['inputMail']!="" && $_POST['inputPassword']!="" && $_POST['inputMemberNo']!="" && $_POST['cassLevel']!="" && $_POST['cassGroup']!="" && $_POST['inputTelephone']!="" && $_POST['inputAddress']!="" && $_POST['inputNPA']!="" && $_POST['inputLocality']!="") {
    //Sanitize the data
    $user = array(
      'inputLast' => SantizeGlobal($_POST['inputLast']),
      'inputFirst' => SantizeGlobal($_POST['inputFirst']),
      'inputMail' => SantizeGlobal($_POST['inputMail']),
      'inputPassword' => SantizeGlobal($_POST['inputPassword']),
      'inputMemberNo' => SantizeGlobal($_POST['inputMemberNo']),
      'cassLevel' => intval($_POST['cassLevel']),
      'cassGroup' => intval($_POST['cassGroup']),
      'inputTelephone' => SantizeGlobal($_POST['inputTelephone']),
      'inputAddress' => SantizeGlobal($_POST['inputAddress']),
      'inputNPA' => SantizeGlobal($_POST['inputNPA']),
      'inputLocality' => SantizeGlobal($_POST['inputLocality'])
    );

  
    // Verifiy reCAPTCHA
    $recaptcha_response = $_POST['g-recaptcha-response'];
    // old $recaptcha_secret = '6LdHGBApAAAAACFW4530C1yhqA-ZZnb5uHkuNiJw';
    $recaptcha_secret = '6LdZ14gqAAAAAD-KCRbFOJPdsq6-2j8mCXKT_sup';
    $recaptcha_verify_url = "https://www.google.com/recaptcha/api/siteverify?secret={$recaptcha_secret}&response={$recaptcha_response}";
    $recaptcha_response_data = json_decode(file_get_contents($recaptcha_verify_url));

    if (!boolval($recaptcha_response_data->success)) {  
      $response['success'] = false;
      $response['message'] = 'Le reCAPTCHA n\'a pas été validé';
    } else {
      if(!is_email($user['inputMail'])) {
        $response['success'] = false;
        $response['message'] = 'L\'adresse e-mail n\'est pas valide';
      } else if (strlen($user['inputPassword']) < 6) {
        $response['success'] = false;
        $response['message'] = 'Le mot de passe doit contenir au moins 6 caractères';
      } else if (strlen($user['inputMemberNo']) == 0) {
        $response['success'] = false;
        $response['message'] = 'Le numéro de membre doit contenir au moins 6 caractères';
      } else if (strlen($user['inputTelephone']) == 0 ) {
        $response['success'] = false;
        $response['message'] = 'Le numéro de téléphone doit contenir au moins 10 caractères';
      } else if (strlen($user['inputNPA']) == 0) {
        $response['success'] = false;
        $response['message'] = 'Le NPA doit contenir au moins 4 caractères';
      } else if (strlen($user['inputLocality']) == 0) {
        $response['success'] = false;
        $response['message'] = 'La localité doit contenir au moins 4 caractères';
      } 


    //Verify if the user already exists
    $user_id = $user_id = email_exists($_POST['inputMail']);
    if($user_id){
      $response['success'] = false;
      $response['message'] = 'Un utilisateur avec cette adresse e-mail existe déjà';
    } else {
      //Create the user with the data entered by the user in the array $user
      if($response['success'] == true) {
        $user_id = wp_insert_user( array(
          'user_login' => $user['inputMail'],
          'user_pass' => $user['inputPassword'],
          'user_email' => $user['inputMail'],
          'first_name' => $user['inputFirst'],
          'last_name' => $user['inputLast'],
          'user_nicename' => $user['inputLast']." ".$user['inputLast'],
        ));

        $user2 = new WP_User($user_id);
        $user2->add_role('CASS_MEMBER');
        $user2->add_role('subscriber');
  
        //Update the user meta
        update_user_meta($user_id,"cassMemberNo",$user["inputMemberNo"]);
        update_user_meta($user_id,"cassLevel",$user["cassLevel"]);
        update_user_meta($user_id,"cassGroup",$user["cassGroup"]);
        update_user_meta($user_id,"cassTelephone",$user["inputTelephone"]);
        update_user_meta($user_id,"cassAddress",$user["inputAddress"]);
        update_user_meta($user_id,"cassNPA",$user["inputNPA"]);
        update_user_meta($user_id,"cassLocality",$user["inputLocality"]); 
      }  
    }
  }
  } else {
    $response['success'] = false;
    $response['message'] = 'Les données ne sont pas valides';
  }

  header('Content-Type: application/json');
  echo json_encode($response);
}


