← All changes
|
includes/Core/Integration/Telegram/TelegramHandler.php
+44
-18
2.0
→
3.3.1
View file →
| @@ -6,21 +6,28 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace BitCode\BitForm\Core\Integration\Telegram; |
| 9 | 9 | |
| 10 | +if (!defined('ABSPATH')) { | |
| 11 | + exit; | |
| 12 | +} | |
| 13 | + | |
| 10 | 14 | use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 11 | 15 | use BitCode\BitForm\Core\Util\HttpHelper; |
| 16 | +use BitCode\BitForm\GlobalHelper; | |
| 12 | 17 | use WP_Error; |
| 13 | 18 | |
| 14 | 19 | /** |
| 15 | 20 | * Provide functionality for Telegram integration |
| 16 | 21 | */ |
| 17 | -class TelegramHandler { | |
| 22 | +class TelegramHandler | |
| 23 | +{ | |
| 18 | 24 | private $_formID; |
| 19 | 25 | private $_integrationID; |
| 20 | - public const API_ENDPOINT = 'https://api.telegram.org/bot'; | |
| 26 | + public static $api_endpoint = 'https://api.telegram.org/bot'; | |
| 21 | 27 | |
| 22 | - public function __construct($integrationID, $fromID) { | |
| 28 | + public function __construct($integrationID, $fromID) | |
| 29 | + { | |
| 23 | 30 | $this->_formID = $fromID; |
| 24 | 31 | $this->_integrationID = $integrationID; |
| 25 | 32 | } |
| 26 | 33 | |
| @@ -28,9 +35,10 @@ | ||
| 28 | 35 | * Helps to register ajax function's with wp |
| 29 | 36 | * |
| 30 | 37 | * @return null |
| 31 | 38 | */ |
| 32 | - public static function registerAjax() { | |
| 39 | + public static function registerAjax() | |
| 40 | + { | |
| 33 | 41 | add_action('wp_ajax_bitforms_telegram_authorize', [__CLASS__, 'telegramAuthorize']); |
| 34 | 42 | add_action('wp_ajax_bitforms_refresh_get_updates', [__CLASS__, 'refreshGetUpdates']); |
| 35 | 43 | } |
| 36 | 44 | |
| @@ -38,13 +46,20 @@ | ||
| 38 | 46 | * Process ajax request for generate_token |
| 39 | 47 | * |
| 40 | 48 | * @return JSON zoho crm api response and status |
| 41 | 49 | */ |
| 42 | - public static function telegramAuthorize() { | |
| 50 | + public static function telegramAuthorize() | |
| 51 | + { | |
| 43 | 52 | $authorizationHeader = null; |
| 44 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 45 | - $inputJSON = file_get_contents('php://input'); | |
| 46 | - $requestsParams = json_decode($inputJSON); | |
| 53 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { | |
| 54 | + GlobalHelper::requirePostMethod(); | |
| 55 | + | |
| 56 | + try { | |
| 57 | + $requestsParams = GlobalHelper::formatRequestData(); | |
| 58 | + } catch (\InvalidArgumentException $e) { | |
| 59 | + wp_send_json_error($e->getMessage(), 400); | |
| 60 | + } | |
| 61 | + | |
| 47 | 62 | if ( |
| 48 | 63 | empty($requestsParams->bot_api_key) |
| 49 | 64 | ) { |
| 50 | 65 | wp_send_json_error( |
| @@ -55,9 +70,9 @@ | ||
| 55 | 70 | 400 |
| 56 | 71 | ); |
| 57 | 72 | } |
| 58 | 73 | |
| 59 | - $apiEndpoint = self::API_ENDPOINT . $requestsParams->bot_api_key . '/getMe'; | |
| 74 | + $apiEndpoint = self::$api_endpoint . $requestsParams->bot_api_key . '/getMe'; | |
| 60 | 75 | $authorizationHeader['Accept'] = 'application/x-www-form-urlencoded'; |
| 61 | 76 | $apiResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader); |
| 62 | 77 | |
| 63 | 78 | if (is_wp_error($apiResponse) || !$apiResponse->ok) { |
| @@ -65,9 +80,9 @@ | ||
| 65 | 80 | empty($apiResponse->error_code) ? 'Unknown' : $apiResponse, |
| 66 | 81 | 400 |
| 67 | 82 | ); |
| 68 | 83 | } |
| 69 | - $apiEndpoint = self::API_ENDPOINT . $requestsParams->bot_api_key . '/getUpdates'; | |
| 84 | + $apiEndpoint = self::$api_endpoint . $requestsParams->bot_api_key . '/getUpdates'; | |
| 70 | 85 | $authorizationHeader['Accept'] = 'application/x-www-form-urlencoded'; |
| 71 | 86 | $apiResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader); |
| 72 | 87 | |
| 73 | 88 | if (is_wp_error($apiResponse) || !$apiResponse->ok) { |
| @@ -93,14 +108,20 @@ | ||
| 93 | 108 | * Process ajax request for refresh telegram get Updates |
| 94 | 109 | * |
| 95 | 110 | * @return JSON telegram get Updates data |
| 96 | 111 | */ |
| 97 | - public static function refreshGetUpdates() { | |
| 112 | + public static function refreshGetUpdates() | |
| 113 | + { | |
| 98 | 114 | $authorizationHeader = null; |
| 99 | 115 | $response = null; |
| 100 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 101 | - $inputJSON = file_get_contents('php://input'); | |
| 102 | - $requestsParams = json_decode($inputJSON); | |
| 116 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { | |
| 117 | + GlobalHelper::requirePostMethod(); | |
| 118 | + | |
| 119 | + try { | |
| 120 | + $requestsParams = GlobalHelper::formatRequestData(); | |
| 121 | + } catch (\InvalidArgumentException $e) { | |
| 122 | + wp_send_json_error($e->getMessage(), 400); | |
| 123 | + } | |
| 103 | 124 | if ( |
| 104 | 125 | empty($requestsParams->bot_api_key) |
| 105 | 126 | ) { |
| 106 | 127 | wp_send_json_error( |
| @@ -110,9 +131,9 @@ | ||
| 110 | 131 | ), |
| 111 | 132 | 400 |
| 112 | 133 | ); |
| 113 | 134 | } |
| 114 | - $apiEndpoint = self::API_ENDPOINT . $requestsParams->bot_api_key . '/getUpdates'; | |
| 135 | + $apiEndpoint = self::$api_endpoint . $requestsParams->bot_api_key . '/getUpdates'; | |
| 115 | 136 | $authorizationHeader['Accept'] = 'application/json'; |
| 116 | 137 | $telegramResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader); |
| 117 | 138 | |
| 118 | 139 | $allList = []; |
| @@ -124,8 +145,13 @@ | ||
| 124 | 145 | $allList[$list->my_chat_member->chat->title] = (object) [ |
| 125 | 146 | 'id' => $list->my_chat_member->chat->id, |
| 126 | 147 | 'name' => $list->my_chat_member->chat->title, |
| 127 | 148 | ]; |
| 149 | + } elseif (isset($list->channel_post->chat->title, $list->channel_post->chat->id)) { | |
| 150 | + $allList[$list->channel_post->chat->title] = (object) [ | |
| 151 | + 'id' => $list->channel_post->chat->id, | |
| 152 | + 'name' => $list->channel_post->chat->title, | |
| 153 | + ]; | |
| 128 | 154 | } |
| 129 | 155 | } |
| 130 | 156 | uksort($allList, 'strnatcasecmp'); |
| 131 | 157 | |
| @@ -147,11 +173,11 @@ | ||
| 147 | 173 | ); |
| 148 | 174 | } |
| 149 | 175 | } |
| 150 | 176 | |
| 151 | - public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) { | |
| 177 | + public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) | |
| 178 | + { | |
| 152 | 179 | $integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details; |
| 153 | - // var_dump($integrationDetails); die; | |
| 154 | 180 | |
| 155 | 181 | $bot_api_key = $integrationDetails->bot_api_key; |
| 156 | 182 | $parse_mode = $integrationDetails->parse_mode; |
| 157 | 183 | $chat_id = $integrationDetails->chat_id; |
| @@ -164,9 +190,9 @@ | ||
| 164 | 190 | || empty($body) |
| 165 | 191 | ) { |
| 166 | 192 | return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for Telegram api', 'bit-form')); |
| 167 | 193 | } |
| 168 | - $recordApiHelper = new RecordApiHelper(self::API_ENDPOINT . $bot_api_key, $this->_integrationID, $logID, $entryID); | |
| 194 | + $recordApiHelper = new RecordApiHelper(self::$api_endpoint . $bot_api_key, $this->_integrationID, $logID, $entryID); | |
| 169 | 195 | $telegramApiResponse = $recordApiHelper->executeRecordApi( |
| 170 | 196 | $integrationDetails, |
| 171 | 197 | $fieldValues, |
| 172 | 198 | $this->_formID, |