WordPress(およびその他のプラットフォーム)でDiscourse招待メールを自動送信する無料の方法

Automate sending Discourse invite emails with Zapier の議論に続きます:

目標達成への道中で、このトピックを見つけました。素晴らしいですが、Zapier は結局無料ではないので :frowning:、以下の 2 つを作成しました。誰かの役に立てば幸いです。これらをテーマの functions.php に追加してください。

もし、これに似た便利なコードがあれば教えてください :smiley:!Discourse API や、コーディングだけで WordPress と無料で接続する方法についてもっと学びたいのです ^^!

コメント投稿後(保留状態)に Discourse 招待を送信する

function invite_forum_from_comment($comment_ID) {
 $comment = get_comment( $comment_ID );
 $comment_email = get_comment_author_email($comment);
 $maybe_notify = ( '0' == $comment->comment_approved );
 if ( $maybe_notify ) {
  // ユーザーを招待
  wp_remote_post( 'https://forum.yourdomain.com/invites', array(
      'method' => 'POST',
      'headers' => array('Content-Type' => 'multipart/form-data', 
                         'Api-key' => 'yourapikey',
                         'Api-Username' => 'yourusername'),
                          'body' => array('email' => $comment_email,
  'custom_message' => 'コメントありがとうございます。承認待ちの間、ぜひ当フォーラムに参加しませんか?!')
            )
      );  
     };
    }
    add_action('comment_post', 'invite_forum_from_comment');

WooCommerce の注文完了後にユーザーを Discourse に招待する

  function invite_forum_from_order( $order_id ) {
          // 注文データの取得
          $order = new WC_Order($order_id);
          // 注文からユーザーのメールを取得
          $order_email = $order->billing_email;   
          // ユーザーを招待
          wp_remote_post( 'https://forum.yourdomain.com/invites', array(
              'method' => 'POST',
              'headers' => array('Content-Type' => 'multipart/form-data', 
                                 'Api-key' => 'yourapikey',
                                 'Api-Username' => 'yourusername'),
              'body' => array('email' => $order_email,
  'custom_message' => 'ご注文ありがとうございます。時間をお無駄にせず、ぜひ当フォーラムに参加してください!')
                    )
              );  
          
        }
 // 新しく作成した関数を「ありがとう」ページに追加
  add_action( 'woocommerce_thankyou', 'invite_forum_from_order', 10, 1 );

更新:このコードは、スパマーを招待しないように、承認されたコメントからのみ招待を行います。

function invite_forum_from_approved_comment($comment_ID) {
 $comment = get_comment( $comment_ID );
 $comment_email = get_comment_author_email($comment);
 $maybe_notify = ( '1' == $comment->comment_approved );
 if ( $maybe_notify ) {
  // ユーザーを招待
  wp_remote_post( 'https://forum.fususu.com/invites', array(
      'method' => 'POST',
      'headers' => array('Content-Type' => 'multipart/form-data', 
                         'Api-key' => 'xxxxxxxxxxxxxxxxxxxx',
                         'Api-Username' => 'xxxx'),
      'body' => array('email' => $comment_email, 
                     'custom_message' => 'ありがとうございます。あなたのコメントは承認され、当フォーラムに参加する招待権を獲得しました。')
        )
  );  
 };
}
add_action('comment_unapproved_to_approved', 'invite_forum_from_approved_comment');

Mautic システムの連絡先をフォーラムに招待する途中、Mautic にはすでに Discourse へ POST を送信できる非常に優れたウェブフック機能があることに気づきました :D、^^! キャンペーン内でアクションを作成するだけで完了です!コードは不要です :smiley:



Zapier には申し訳ありませんが、彼らは優れています。しかし、制限のない無料のソリューションの方が好きです :blush: