yaxu
(Alex McLean)
2020 年 9 月 16 日午後 5:57
1
皆様、こんにちは。
特定のメールアドレスに対して、ユーザーを1つまたは2つのグループに追加し、アカウントを持っていない場合はフォーラムへの招待を送信する API 機能を探していました。その機能は見つからなかったのですが、これは「一括アップロード」招待機能と全く同じものであることに気づきました。これは API にはドキュメント化されていませんが、ブラウザが行っている処理です。
そこで、以下の PHP コードを使用して CSV ファイルを 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
)
);
この方法は数ヶ月間問題なく機能していましたが、最近では以下のエラーが Discourse ログに表示されるようになり、動作しなくなりました:Can't verify CSRF token authenticity.
これはバグでしょうか?API キーを通じて認証されているはずなのに、なぜこのようなエラーが出るのでしょうか?それとも、ドキュメント化されていない API の部分を使用しようとしていることがそもそも無理筋なのでしょうか?
yaxu
(Alex McLean)
2020 年 9 月 23 日午後 9:27
2
もしよろしければ、これは修正される可能性のあるバグかどうか確認していただけませんか?いずれにせよ、代替案を探し始めます。
これについて何かアイデアはありますか、@techAPJ さん?
yaxu
(Alex McLean)
2020 年 9 月 24 日午前 8:23
4
こんにちは、ご確認ありがとうございます。ドキュメント化された API を使って、何かを無理やり組み合わせてみました(「無理やり」に重点を置きますが、もしかしたら他の誰かの役に立つかもしれません):
function tidal_process_order( $order_id ){
$order = new WC_Order($order_id);
$order_email = $order->get_billing_email();
$group_names_arr = [];
// SKU をグループ名として取得
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);
// メールアドレスで Discourse ユーザーが既に存在するか確認
$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) {
// ユーザーが存在する場合、グループに追加
$user_id = $matches[0]->id;
foreach($group_names_arr as $group) {
// グループ名からグループ ID を取得
$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));
// ユーザーをグループに追加
$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 {
// ユーザーが存在しない場合、グループ名付きで招待
$body = json_encode(array('email' => $order_email, 'group_names' => $group_names, "custom_message" => "Welcome!"));
$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' );