| 1 |
<?php |
| 2 |
/** |
| 3 |
* Contact Form 7 notifications |
| 4 |
* |
| 5 |
* @package Wa_Notifier |
| 6 |
*/ |
| 7 |
class Notifier_ContactForm7 { |
| 8 |
|
| 9 |
/** |
| 10 |
* Init. |
| 11 |
*/ |
| 12 |
public static function init() { |
| 13 |
add_filter( 'notifier_notification_triggers', array( __CLASS__, 'add_triggers'), 10 ); |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Add notification triggers |
| 18 |
*/ |
| 19 |
public static function add_triggers($existing_triggers) { |
| 20 |
$triggers = array(); |
| 21 |
$form_ids = get_posts(array ( |
| 22 |
'post_type' => 'wpcf7_contact_form', |
| 23 |
'post_status' => 'publish', |
| 24 |
'numberposts' => -1, |
| 25 |
'fields' => 'ids' |
| 26 |
)); |
| 27 |
foreach($form_ids as $form_id){ |
| 28 |
$trigger_id = 'cf7_' . $form_id; |
| 29 |
$title = get_the_title( $form_id ); |
| 30 |
$triggers[] = array( |
| 31 |
'id' => $trigger_id, |
| 32 |
'label' => 'Form "' . $title . '" is submitted', |
| 33 |
'description' => 'Trigger notification when <b>'.$title.'</b> form is submitted.', |
| 34 |
'merge_tags' => self::get_merge_tags($form_id), |
| 35 |
'recipient_fields' => self::get_recipient_fields($form_id), |
| 36 |
'action' => array( |
| 37 |
'hook' => 'wpcf7_before_send_mail', |
| 38 |
'callback' => function ( $form ) use ( $trigger_id ) { |
| 39 |
$trigger_form_id = str_replace('cf7_', '', $trigger_id); |
| 40 |
if($form->id() != $trigger_form_id){ |
| 41 |
return; |
| 42 |
} |
| 43 |
$sanitized_data = array(); |
| 44 |
foreach($_POST as $key => $data){ |
| 45 |
if(is_array($data)){ |
| 46 |
$sanitized_data[$key] = notifier_sanitize_array($data); |
| 47 |
} |
| 48 |
else{ |
| 49 |
$sanitized_data[$key] = sanitize_text_field($data); |
| 50 |
} |
| 51 |
} |
| 52 |
Notifier_Notification_Triggers::send_trigger_request( $trigger_id, $sanitized_data ); |
| 53 |
} |
| 54 |
) |
| 55 |
); |
| 56 |
} |
| 57 |
$existing_triggers['Contact Form 7'] = $triggers; |
| 58 |
return $existing_triggers; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Get merge tags for the current form |
| 63 |
*/ |
| 64 |
public static function get_merge_tags( $form_id ) { |
| 65 |
$fieldsArray = get_post_meta($form_id); |
| 66 |
$meta = $fieldsArray['_form'][0]; |
| 67 |
$TagsManager = WPCF7_FormTagsManager::get_instance(); |
| 68 |
$form_fields = $TagsManager->filter( $meta, array() ); |
| 69 |
|
| 70 |
$merge_tags = Notifier_Notification_Merge_Tags::get_merge_tags(); |
| 71 |
|
| 72 |
$excluded_field_types = array('submit'); |
| 73 |
foreach($form_fields as $field){ |
| 74 |
if(in_array($field->type, $excluded_field_types)){ |
| 75 |
continue; |
| 76 |
} |
| 77 |
$return_type = 'text'; |
| 78 |
$field_name = $field->name; |
| 79 |
$field_type = $field->type; |
| 80 |
$merge_tags['Contact Form 7'][] = array( |
| 81 |
'id' => 'cf7_' . $form_id . '_' . $field_name, |
| 82 |
'label' => $field_name, |
| 83 |
'preview_value' => '', |
| 84 |
'return_type' => $return_type, |
| 85 |
'value' => function ( $sanitized_data ) use ( $field_name ) { |
| 86 |
$value = isset($sanitized_data[$field_name]) ? $sanitized_data[$field_name] : ''; |
| 87 |
if(is_array($value)){ |
| 88 |
$value = implode(', ', $value); |
| 89 |
} |
| 90 |
return html_entity_decode(sanitize_text_field($value)); |
| 91 |
} |
| 92 |
); |
| 93 |
} |
| 94 |
|
| 95 |
return $merge_tags; |
| 96 |
} |
| 97 |
|
| 98 |
/* |
| 99 |
* Get recipient fields |
| 100 |
*/ |
| 101 |
public static function get_recipient_fields($form_id){ |
| 102 |
$fieldsArray = get_post_meta( $form_id ); |
| 103 |
$meta = $fieldsArray['_form'][0]; |
| 104 |
$TagsManager = WPCF7_FormTagsManager::get_instance(); |
| 105 |
$form_fields = $TagsManager->filter( $meta, array() ); |
| 106 |
|
| 107 |
$recipient_fields = array(); |
| 108 |
foreach($form_fields as $field){ |
| 109 |
|
| 110 |
if('tel' != $field->basetype){ |
| 111 |
continue; |
| 112 |
} |
| 113 |
|
| 114 |
$field_name = $field->name; |
| 115 |
$recipient_fields['Contact Form 7'][] = array( |
| 116 |
'id' => 'cf7_' . $form_id . '_' . $field_name, |
| 117 |
'label' => $field_name, |
| 118 |
'value' => function ( $sanitized_data ) use ( $field_name ) { |
| 119 |
$value = isset($sanitized_data[$field_name]) ? $sanitized_data[$field_name] : ''; |
| 120 |
return html_entity_decode(sanitize_text_field($value)); |
| 121 |
} |
| 122 |
); |
| 123 |
} |
| 124 |
return $recipient_fields; |
| 125 |
} |
| 126 |
|
| 127 |
} |
| 128 |
|