PluginProbe
WANotifier for Forms and Actions / 2.2.0
WANotifier for Forms and Actions v2.2.0
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.2.0, at includes/classes/class-notifier-cf7.php

128 lines 3.6 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 $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