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

122 lines 3.3 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 $ContactForm = WPCF7_ContactForm::get_instance( $form_id );
66 $form_fields = $ContactForm->scan_form_tags();
67
68 $merge_tags = array();
69
70 $excluded_field_types = array('submit');
71 foreach($form_fields as $field){
72 if(in_array($field->type, $excluded_field_types)){
73 continue;
74 }
75 $return_type = 'text';
76 $field_name = $field->name;
77 $field_type = $field->type;
78 $merge_tags['Contact Form 7'][] = array(
79 'id' => 'cf7_' . $form_id . '_' . $field_name,
80 'label' => $field_name,
81 'preview_value' => '',
82 'return_type' => $return_type,
83 'value' => function ( $sanitized_data ) use ( $field_name ) {
84 $value = isset($sanitized_data[$field_name]) ? $sanitized_data[$field_name] : '';
85 if(is_array($value)){
86 $value = implode(', ', $value);
87 }
88 return html_entity_decode(sanitize_text_field($value));
89 }
90 );
91 }
92 return $merge_tags;
93 }
94
95 /*
96 * Get recipient fields
97 */
98 public static function get_recipient_fields($form_id){
99 $ContactForm = WPCF7_ContactForm::get_instance( $form_id );
100 $form_fields = $ContactForm->scan_form_tags();
101
102 $recipient_fields = array();
103 foreach($form_fields as $field){
104 if('tel' != $field->type){
105 continue;
106 }
107
108 $field_name = $field->name;
109 $recipient_fields['Contact Form 7'][] = array(
110 'id' => 'cf7_' . $form_id . '_' . $field_name,
111 'label' => $field_name,
112 'value' => function ( $sanitized_data ) use ( $field_name ) {
113 $value = isset($sanitized_data[$field_name]) ? $sanitized_data[$field_name] : '';
114 return html_entity_decode(sanitize_text_field($value));
115 }
116 );
117 }
118 return $recipient_fields;
119 }
120
121 }
122