/**
  * parse location into latitude and longitude
  * @param $location: address
  */
 public static function getLocationFromAddress($location)
 {
     $url = sprintf(self::geocode, urlencode($location));
     HW_HOANGWEB::load_class('HW_CURL');
     $resp = HW_CURL::curl_get($url);
     //other way
     #$resp = @file_get_contents($url);
     $json = json_decode($resp);
     if (isset($json->results[0])) {
         return $json->results[0]->geometry->location;
     }
 }
 /**
  * ajax handle
  */
 public function _hw_catch_gdrive_formfields()
 {
     if (!wp_verify_nonce($_REQUEST['nonce'], "hw_catch_gdrive_formfields_nonce")) {
         exit("No naughty business please");
     }
     $gform_id = urldecode($_REQUEST['gform_id']);
     //google form ID
     $fields = array();
     //get form content
     $url_action = hw_wpcf7_valid_gform_url($gform_id);
     $html = HW_CURL::curl_get($url_action);
     //parse form fields
     preg_match_all('#<(input|textarea|select).+?\\/?>#', $html, $result);
     if (isset($result[0]) && is_array($result[0])) {
         foreach ($result[0] as $f) {
             preg_match_all('#(name|aria-label)=\\"([^"]*)\\"#', $f, $atts);
             if (isset($atts[2]) && is_array($atts[2]) && count($atts[2])) {
                 $fname = strtolower($atts[2][0]);
                 //field name should be lowercase
                 if (strrpos($fname, 'entry.', -strlen($fname)) === FALSE) {
                     continue;
                 }
                 $fields[$fname] = isset($atts[2][1]) ? trim($atts[2][1]) : '';
                 //get fields name & label
                 //detect sendEmail field
                 if (trim($fields[$fname]) == 'sendEmail') {
                     $avaiable_fsendEmail = true;
                 }
             }
         }
     }
     $out = '<div class="result hwcf-parse-gform-result">';
     if (count($fields)) {
         $out .= '<span>Chú ý: tạo thêm thuộc tính "gfield" cho các trường form của bạn bằng cách tên các trường tương ứng dưới đây:
                 </span>';
         $ex_scfield = '';
         $has_special_field = false;
         //check exists form has special field
         $out .= '<table>';
         //display fields
         foreach ($fields as $name => $label) {
             //check for special field
             if (isset(self::$special_gfields[$label])) {
                 $has_special_field = true;
                 //marked exists one or more special fields
                 continue;
                 //ignore special fields
             }
             if (!$ex_scfield) {
                 $ex_scfield = '<tr><td colspan="2"><br/><span>VD: <strong>[text* your-name gfield:' . $name . ']</strong></span>';
                 $out .= $ex_scfield . '<hr/></td></tr>';
             }
             $out .= '<tr>';
             $out .= '<td valign="bottom"><div class="g-field"><span>' . $label . '</span><br/><input type="text" readonly value="gfield:' . $name . '"/></div></td>';
             $out .= '<td valign="bottom"><div class="g-field"><input type="text" value="placeholder \'' . $label . '\'" readonly/></div></td>';
             $out .= '</tr>';
         }
         //display special fields
         if ($has_special_field) {
             $special_fields_shortcode_tag = '[hw_wpcf7_special_fields hwspgf ';
             $reverse_fields = array_flip($fields);
             $out .= '<tr><td colspan="2" class="special-fields-label">Các trường đặc biệt sử dụng bởi Google Script</td></tr>';
             foreach (self::$special_gfields as $flabel => $desc) {
                 if (isset($reverse_fields[$flabel])) {
                     $special_fields_shortcode_tag .= $flabel . ':' . $reverse_fields[$flabel] . ' ';
                     $out .= '<tr class="gform-special-field">';
                     $out .= '<td valign="bottom" colspan=""><div class="g-field"><span>' . $flabel . '</span><br/><input type="text" readonly value="gfield:' . $reverse_fields[$flabel] . '"/></div></td>';
                     $out .= '<td valign="top"><div class="g-field">' . $desc . '</div></td>';
                     $out .= '</tr>';
                 }
             }
             $special_fields_shortcode_tag = trim($special_fields_shortcode_tag);
             //truncate space around this variable value
             $special_fields_shortcode_tag .= ']';
         }
         $out .= '</table>';
         if (!isset($avaiable_fsendEmail)) {
             $out .= '<span style="color:red">Cảnh báo: không tìm thấy nhãn trường (chứa nội dung gửi email) "sendEmail" trong google form. Để có thể gửi mail từ google spreadsheet vui lòng thêm trường với nhãn "sendEmail" trong google form có liên kết vào spreadsheet đó.</span>';
         } else {
             $out .= '<p>Chúc mừng: đã tìm thấy trường lưu nội dung gửi email trong google form của bạn. Nội dung này tự động lấy từ Email template ở box "Email" ngay bên dưới.</p>';
             $out .= '<p>Copy trường shortcode này vào nội dung trường form fields ở trên, đặt phía dưới cùng của form hoặc bất kỳ vị trí nào đều hoạt động.<br/><input type="text" style="width:100%" value="' . $special_fields_shortcode_tag . '" readonly/></p>';
         }
     } else {
         $out .= 'Không tìm thấy dữ liệu. Hãy kiểm tra lại google form ID?';
     }
     $out .= '</div>';
     if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
         //$result = json_encode($result);
         echo $out;
     } else {
         header("Location: " . $_SERVER["HTTP_REFERER"]);
     }
     die;
 }
 /**
  * contact form submision
  * @param object $WPCF7_ContactForm: current contact form object
  */
 public function _hw_wpcf7_do_something($WPCF7_ContactForm)
 {
     /* Use WPCF7_Submission object's get_posted_data() method to get it. */
     $properties = $WPCF7_ContactForm->get_properties();
     //get google form ID of this form
     $gformID = $properties['hw_gformID'];
     // get the contact form object
     $wpcf7 = WPCF7_ContactForm::get_current();
     if (isset($properties['enable_email_by_gapp']) && $properties['enable_email_by_gapp']) {
         //  do not send the email
         $wpcf7->skip_mail = true;
         //turn off default send mail by wpcf7, use google drive instead
     }
     $atts = $WPCF7_ContactForm->form_scan_shortcode();
     $fields_title = array();
     //fields title
     $data = array();
     //fields value
     //submission data
     $submission = WPCF7_Submission::get_instance();
     if ($submission) {
         //get storage service
         $storage_hook = $WPCF7_ContactForm->prop('hwcf_data_hook');
         //get posted form data
         $posted_data = $submission->get_posted_data();
         //parse email template into user data
         $mail_temp = $WPCF7_ContactForm->prop('mail');
         $result = wpcf7_mail_replace_tags($mail_temp);
         $admin_email = !empty($mail_temp['recipient']) ? $mail_temp['recipient'] : get_option('admin_email');
         //set special field value
         $special_fields_value['sendEmail'] = $result['body'];
         $special_fields_value['admin_email'] = $admin_email;
         #admin email
         $special_fields_value['website'] = hw_wpcf7_current_page_url();
         #site url
         foreach ($atts as $field) {
             //loop each field
             $tag = new WPCF7_Shortcode($field);
             $name = $tag->name;
             //get field name
             if ($tag->has_option('gfield') && $tag->type != 'hw_wpcf7_special_fields') {
                 if ($tag->get_option('gfield', '', true)) {
                     $name = $tag->get_option('gfield', '', true);
                     //modify field value
                     $data[$name] = apply_filters('hwwpcf7_field_value', $posted_data[$tag->name], array('name' => $name, 'tag' => $tag, 'data' => &$data, 'wpcf7' => $wpcf7));
                     /*if(isset($_POST['product_id']) && $tag->name=='order_detail'){
                           $sp=get_post($_POST['product_id']);
                           $data[$name] = '[ID='.$sp->ID.']'.PHP_EOL.$sp->post_title.PHP_EOL.get_permalink($sp->ID);
                       }*/
                     //else $data[$name] = $posted_data[$tag->name];
                 }
             }
             /**
              * get field title
              */
             if ($tag->has_option('placeholder') && $tag->type != 'hw_wpcf7_special_fields') {
                 $fields_title[$name] = (string) reset($tag->values);
                 #$tag->get_option('placeholder','',true);
             }
             /**
              * special tag to get special fields
              */
             if ($tag->type == 'hw_wpcf7_special_fields') {
                 foreach (HW_WPCF7::$special_gfields as $fname => $desc) {
                     if ($tag->has_option($fname) && isset($special_fields_value[$fname])) {
                         $data[$tag->get_option($fname, '', true)] = $special_fields_value[$fname];
                         //add special field value to data
                     }
                 }
             }
         }
         //storage
         if ($storage_hook == 'google_form') {
             //get google form id
             $gform_id = $WPCF7_ContactForm->prop('hw_gformID');
             //from google spreadsheet as responses that link to google form. Create event onSubmitForm. you can send mail using google script.
             hw_wpcf7_post_gform($gform_id, $data);
         } elseif ($storage_hook == 'url') {
             $hook_url = $WPCF7_ContactForm->prop('hook_url');
             //web hook url
             $data['labels'] = serialize($fields_title);
             //nest labels for all fields in one data together
             HW_CURL::curl_post($hook_url, $data);
         }
         /*hw_mail(array(
            'subject'=>'Khách hàng liên hệ từ '.home_url(),
            'body'=>$body
           ));*/
     }
 }
Пример #4
0
/**
 * post record into google form response
 * @param string $gform_id: google form ID
 * @param array $posted_data: data present record in google spreadsheet that store for google form
 */
function hw_wpcf7_post_gform($gform_id, $posted_data = array())
{
    //save form data
    $url_action = hw_wpcf7_valid_gform_response_url($gform_id);
    return HW_CURL::curl_post($url_action, $posted_data);
}