Last active
November 5, 2023 13:19
-
-
Save mihdan/ae0b518e248dc6b45c48d60a76790f55 to your computer and use it in GitHub Desktop.
Отправляет все формы Contacy Form 7 с сайта в приватный канал в Telegram
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* CF7 to Telegram | |
*/ | |
namespace Mihdan\Kadence_Child; | |
/** | |
* Class ContactFormToTelegram | |
* | |
* @package Mihdan\Kadence_Child | |
*/ | |
class ContactFormToTelegram { | |
private $args; | |
public function __construct() { | |
$this->rgs = [ | |
'bot_token' => TELEGRAM_BOT_TOKEN, | |
'receivers' => [ | |
TELEGRAM_RECEIVERS, | |
], | |
]; | |
} | |
public function setup_hooks() { | |
add_filter( 'wpcf7_mail_components', [ $this, 'to_tlg_action' ], 10, 3 ); | |
} | |
public function to_tlg_send_message( $message, $receiver ) { | |
$params['text'] = wp_strip_all_tags( $message ); | |
$params['chat_id'] = (int) $receiver; | |
$api_url = sprintf( | |
'https://api.telegram.org/bot%s/sendMessage?%s', | |
$this->args['bot_token'], | |
http_build_query( $params ) | |
); | |
$response = wp_remote_get( $api_url ); | |
if ( is_wp_error( $response ) ) { | |
error_log( 'Error in cf7-to-tlg: ' . $response ); | |
} else { | |
$response_body = json_decode( wp_remote_retrieve_body( $response ), true ); | |
if ( ! $response_body['ok'] ) { | |
error_log( 'Error in cf7-to-tlg: ' . json_encode( $response_body ) ); | |
} | |
} | |
} | |
public function to_tlg_action( $components, $form, $instance ) { | |
foreach ( $this->args['receivers'] as $receiver ) { | |
$this->to_tlg_send_message( $components['body'], $receiver ); | |
} | |
return $components; | |
} | |
} | |
( new ContactFormToTelegram() )->setup_hooks(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment