PluginProbe
WANotifier for Forms and Actions / trunk
WANotifier for Forms and Actions vtrunk
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 / integrations / class-notifier-cf7.php

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

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