Automatizzare inviti in blocco

Ciao a tutti,
Stavo cercando una funzionalità API che, per un dato indirizzo email, aggiungesse un utente a uno o due gruppi, invitandolo al forum se non avesse già un account. Non sono riuscito a trovare tale funzionalità, ma ho notato che è esattamente ciò che fa la funzione di invito ‘caricamento in massa’. Questa non è documentata nell’API, ma è ciò che fa il browser.

Quindi ho fatto lo stesso, utilizzando il seguente codice PHP per inviare un file CSV tramite POST:

        $body = "--ossifrage\r
Content-Disposition: form-data; name=\"type\"\r
\r
csv\r
--ossifrage\r
Content-Disposition: form-data; name=\"files[]\"; filename=\"test.csv\"\r
Content-Type: text/csv\r
\r
$order_email,$group_names\r
\r
\r
--ossifrage--\r
";
$r=wp_remote_post( 'https://club.tidalcycles.org/invites/upload_csv.json', array(
                    'method' => 'POST',
                    'headers' => array('Content-Type' => 'multipart/form-data; boundary=ossifrage', 
                                       'Api-key' => '(redacted)',
                                       'Api-Username' => 'yaxu'),
                    'body' => $body
                    )
                 ); 

Questo ha funzionato perfettamente per alcuni mesi, ma recentemente ha smesso di funzionare, generando il seguente errore nei log di Discourse: Can't verify CSRF token authenticity.

Si tratta di un bug, dato che dovrei essere autenticato tramite la chiave API? O sto cercando di utilizzare parti non documentate dell’API, il che è un percorso senza speranza?

Mi chiedevo solo se qualcuno potesse confermare se si tratta di un bug che verrà probabilmente corretto o meno? Comunque, inizierò a cercare una soluzione alternativa.

Hai qualche idea su questo, @techAPJ?

Ciao, grazie per l’attenzione. Sono riuscito a mettere insieme qualcosa usando l’API documentata (con enfasi su “messo insieme”, ma forse può essere utile a qualcun altro):

function tidal_process_order( $order_id ){
        $order = new WC_Order($order_id);
        $order_email = $order->get_billing_email();
        $group_names_arr = [];
        // ottieni gli SKU come nomi di gruppo
        foreach ( $order->get_items() as $item ) {
          if ( $item['product_id'] > 0 ) {
            $_product = $item->get_product();
            $sku = $_product->get_sku();
            array_push($group_names_arr, $sku);
          }
        }

        $group_names = join(",",$group_names_arr);

        // controlla se l'utente di Discourse esiste già con l'email
        $r=wp_remote_get( "https://club.tidalcycles.org/admin/users/list/all.json?email=$order_email", array(
                          'headers' => array('Api-key' => 'redacted',
                                             'Api-Username' => 'redacted')
                                            )
                        );
        $matches = json_decode($r['body']);
        if (count($matches) > 0) {
          // l'utente esiste, aggiungilo ai gruppi
          $user_id = $matches[0]->id;
          foreach($group_names_arr as $group) {
            // ottieni l'ID del gruppo dal nome del gruppo
            $r=wp_remote_get( "https://club.tidalcycles.org/groups/$group.json", array(
                              'headers' => array('Api-key' => 'redacted',
                                                 'Api-Username' => 'redacted')
                              )
                            );
            $group = json_decode($r['body']);
            $body = json_encode(array('group_id' => $group->group->id));
            // aggiungi l'utente al gruppo
            $r=wp_remote_post( "https://club.tidalcycles.org/admin/users/$user_id/groups", array(
                               'headers' => array('Api-key' => 'redacted',
                                                  'Api-Username' => 'redacted',
                                                  'Content-Type' => 'application/json', 
                                                 ),
                               'body' => $body
                            ));
          }
        }
        else {
            // l'utente non esiste, invitalo con i nomi dei gruppi
            $body = json_encode(array('email' => $order_email, 'group_names' => $group_names, "custom_message" => "Benvenuto!"));
            $r=wp_remote_post( "https://club.tidalcycles.org/invites", array(
                               'headers' => array('Api-key' => 'redacted',
                                                  'Api-Username' => 'redacted',
                                                  'Content-Type' => 'application/json', 
                                                 ),
                               'body' => $body
                            ));
        };
}

add_action( 'woocommerce_order_status_processing', 'tidal_process_order' );