Add Webhooks to new listing / requests post

Hello !

I’ve created a discord webhook system to be notified when an listing or request is posted (currently ‘updated listing’).

I’m having a problem with the request hook, because when I update / create a request, two webhooks are sent.

Do you know why?

And I’m also having a problem with retrieving images, how do I extract the images from the ads one by one ?

add_action(
   'hivepress/v1/models/listing/update',
   'send_discord_webhook',
   1000,
   2
);

add_action(
   'hivepress/v1/models/request/update',
   'send_discord_webhook',
   1000,
   2
);

function send_discord_webhook( $listing_id, $listing ) {

   // Vérifiez si l'annonce est bien publiée après la mise à jour.
   if ( 'publish' !== $listing->get_status() ) {
       return;
   }

   $user = $listing->get_user();
   $listing_title = $listing->get_title();
   $listing_desc = $listing->get_description();
   $listing_url = get_permalink($listing->get_id());
   $listing_price = $listing->get_price();

   // Déterminer le type d'événement
   $hook = current_filter(); // Obtenir le nom du hook actuel

   if ( $hook == 'hivepress/v1/models/listing/update' ) {
       $author_name = "Une nouvelle proposition de service est disponible !";
       $title_prefix = "🔗 Voir l'Annonce";
   } elseif ( $hook == 'hivepress/v1/models/request/update' ) {
       $author_name = "Une nouvelle demande de service est disponible !";
       $title_prefix = "🔗 Voir la recherche";
   } else {
       return; // Si le hook ne correspond pas, ne rien faire
   }

   // Préparer l'embed Discord
   $embed = [
       "author" => [
           "name" => $author_name,
           "icon_url" => "https://cdn.discordapp.com/emojis/1053771836678226011.webp?size=96&quality=lossless",
       ],
       "title" => $listing_title,
       "type" => "rich",
       "url" => $listing_url,
       "color" => hexdec("24242c"),
       "fields" => [
           [
               "name" => $title_prefix,
               "value" => $listing_desc . "\n [Plus de détail]($listing_url)\n",
               "inline" => false
           ],
           [
               "name" => "💰 Rémunération",
               "value" => "A partir de ".$listing_price ." €\n",
               "inline" => false
           ],
           [
               "name" => "Rappel : Pour votre sécurité 🔐 , veillez à garder les discussions et paiements au sein de notre site.",
               "value" => "",
               "inline" => false
           ]
       ],
      
   ];

   // Préparer les données complètes à envoyer via le webhook
   $webhook_data = [
       "embeds" => [$embed],
   ];

   // Encoder en JSON
   $webhook_json = json_encode($webhook_data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

   // Envoyer la requête POST au webhook
   $webhook_url = 'https://canary.discord.com/api/webhooks/1204200348667871342/Uk6wMCD8PAp2YGPiYg74Dp-Cp-Yg6V7Yl-FHG1v0p-IkY3opmSmKYPY34khxbZlKj4kL'; // Remplacez par votre URL de webhook Discord
   $args = [
       'headers' => [ 'Content-Type' => 'application/json' ],
       'body'    => $webhook_json,
       'data_format' => 'body',
   ];

   $response = wp_remote_post($webhook_url, $args);

   // Debugging: Vérifier la réponse de la requête
   if (is_wp_error($response)) {
       error_log('Erreur lors de l\'envoi du webhook Discord : ' . $response->get_error_message());
   } else {
       error_log('Webhook Discord envoyé avec succès.');
       error_log('Réponse : ' . print_r($response, true));
   }
}

Thank a lot for your work !

Hi,

It’s hard to say because other extensions may also run the update hook (e.g. if they update one of the fields when the listing is updated). Please try using remove_action inside the callback function to ensure that it runs once per listing/request.

If there’s a listing/request object, you can get image URLs this way:

$urls=$listing->get_images__url('thumbnail');

You can also pass other image sizes or nothing to get the original image file.

Hope this helps

Hello,

Thanks :slight_smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.