PluginProbe
WANotifier for Forms and Actions / 2.1.2
WANotifier for Forms and Actions v2.1.2
3.1.0 3.0.4 2.7.10 2.7.11 2.7.12 2.7.13 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 3.0.0 3.0.1 3.0.2 3.0.3 trunk 0.1.0 0.1.1 1.0.0 1.0.1 1.0.2 1.0.3 All 66 releases
notifier / includes / classes / class-notifier-cf7.php

class-notifier-cf7.php in WANotifier for Forms and Actions 2.1.2, at includes/classes/class-notifier-cf7.php

130 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $tags = $TagsManager->scan( $meta );
69 $form_fields = $TagsManager->filter( $meta, $data );
70
71 $merge_tags = Notifier_Notification_Merge_Tags::get_merge_tags();
72
73 $excluded_field_types = array('submit');
74 foreach($form_fields as $field){
75 if(in_array($field->type, $excluded_field_types)){
76 continue;
77 }
78 $return_type = 'text';
79 $field_name = $field->name;
80 $field_type = $field->type;
81 $merge_tags['Contact Form 7'][] = array(
82 'id' => 'cf7_' . $form_id . '_' . $field_name,
83 'label' => $field_name,
84 'preview_value' => '',
85 'return_type' => $return_type,
86 'value' => function ( $sanitized_data ) use ( $field_name ) {
87 $value = isset($sanitized_data[$field_name]) ? $sanitized_data[$field_name] : '';
88 if(is_array($value)){
89 $value = implode(', ', $value);
90 }
91 return html_entity_decode(sanitize_text_field($value));
92 }
93 );
94 }
95
96 return $merge_tags;
97 }
98
99 /*
100 * Get recipient fields
101 */
102 public static function get_recipient_fields($form_id){
103 $fieldsArray = get_post_meta( $form_id );
104 $meta = $fieldsArray['_form'][0];
105 $TagsManager = WPCF7_FormTagsManager::get_instance();
106 $tags = $TagsManager->scan( $meta );
107 $form_fields = $TagsManager->filter( $meta, $data );
108
109 $recipient_fields = array();
110 foreach($form_fields as $field){
111
112 if('tel' != $field->basetype){
113 continue;
114 }
115
116 $field_name = $field->name;
117 $recipient_fields['Contact Form 7'][] = array(
118 'id' => 'cf7_' . $form_id . '_' . $field_name,
119 'label' => $field_name,
120 'value' => function ( $sanitized_data ) use ( $field_name ) {
121 $value = isset($sanitized_data[$field_name]) ? $sanitized_data[$field_name] : '';
122 return html_entity_decode(sanitize_text_field($value));
123 }
124 );
125 }
126 return $recipient_fields;
127 }
128
129 }
130