Last active
July 22, 2021 06:57
-
-
Save mennwebs/5b1bd63e884ab003b7653ce3be5430b2 to your computer and use it in GitHub Desktop.
WP Shortcode for sending to Line Notify [s_line]
This file contains hidden or 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 | |
| add_action( 'forminator_form_after_handle_submit', 'ws_get_message', 10, 2 ); | |
| add_action( 'forminator_form_after_save_entry', 'ws_get_message', 10, 2 ); | |
| // เก็บข้อความจากฟอร์มแล้วส่งไลน์ | |
| function ws_get_message($form_id, $response) { | |
| // ===แก้ตรงนี้===== | |
| $fields = ['name-1', 'phone-1','textarea-1']; | |
| $labels = ['ชื่อ', 'เบอร์โทร', 'ข้อความ']; | |
| // ============== | |
| if ( !$response['success'] ) return; | |
| // เริ่มเก็บข้อมูล | |
| $vars = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING); | |
| $entries = Forminator_API::get_entries( $form_id ); | |
| $count = 0; | |
| foreach( $entries as $entry ): | |
| $count++; | |
| $meta = (array) $entry->meta_data; | |
| // เช็คว่าข้อมูลที่ Post ตรงกับรายการสั่งซื้อใน Entry นี้ | |
| if ( $vars['phone-1'] == $meta['phone-1']['value']) { | |
| $massage = "#" . $entry->entry_id; | |
| foreach( $meta as $key => $value ) { | |
| if( in_array( $key, $fields ) ) { | |
| $label = $labels[array_search( $key , $fields)]; | |
| if (isset( $key )) { | |
| $massage .= "\n" . $label .": " . $value['value']; | |
| } | |
| } | |
| } | |
| break; | |
| } | |
| // ค้นหาแค่ 5 รายการล่าสุดเท่านั้น เพื่อไม่ให้ระบบโหลดเกินไป | |
| if( $count > 5 ) break; | |
| endforeach; | |
| // ส่งไลน์ | |
| ws_send_line( $massage ); | |
| } | |
| // เอาไว้ส่งไลน์ | |
| function ws_send_line( $massage ){ | |
| // ===แก้ตรงนี้===== | |
| $token = 'xxxxxxxxxxxx'; | |
| // ============== | |
| $api_url = 'https://notify-api.line.me/api/notify'; | |
| $massage_push = array('message' => $massage); | |
| $curl = curl_init(); | |
| curl_setopt( $curl, CURLOPT_URL, $api_url ); | |
| curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, 'POST' ); | |
| curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE); | |
| curl_setopt( $curl, CURLOPT_POSTFIELDS, $massage_push ); | |
| curl_setopt( $curl, CURLOPT_HTTPHEADER, array( | |
| 'Content-Type: multipart/form-data', | |
| 'Authorization: Bearer ' . $token | |
| ) ); | |
| $result_callback = curl_exec( $curl ); | |
| if( curl_errno( $curl ) ){ | |
| wp_die( __( curl_error( $curl ) ), __( 'Error' ), array( 'response' => 403 ) ); | |
| } | |
| curl_close( $curl ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment