PluginProbe
WANotifier for Forms and Actions / 3.1.0
WANotifier for Forms and Actions v3.1.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 / integrations / class-notifier-wpforms.php

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

125 lines 3.2 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 * WPForms notifications
8 *
9 * @package Wa_Notifier
10 */
11 class Notifier_WPForms {
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' => 'wpforms',
27 'post_status' => 'publish',
28 'numberposts' => -1,
29 'fields' => 'ids'
30 ));
31
32 foreach($form_ids as $form_id){
33 $trigger_id = 'wpf_' . $form_id;
34 $title = get_the_title( $form_id );
35 $triggers[] = array(
36 'id' => $trigger_id,
37 'label' => 'Form "' . $title . '" is submitted',
38 'description' => 'Trigger notification when <b>'.$title.'</b> form is submitted.',
39 'merge_tags' => self::get_merge_tags($form_id),
40 'recipient_fields' => self::get_recipient_fields($form_id),
41 'action' => array(
42 'hook' => 'wpforms_process_complete_'.$form_id,
43 'callback' => function ( $fields, $entry ) use ( $trigger_id ) {
44 Notifier_Notification_Triggers::send_trigger_request( $trigger_id, $fields );
45 },
46 'args_num' => 2,
47 )
48 );
49 }
50 $existing_triggers['WPForms'] = $triggers;
51 return $existing_triggers;
52 }
53
54 /**
55 * Get merge tags for the current form
56 */
57 public static function get_merge_tags( $form_id ) {
58
59 $merge_tags = Notifier_Notification_Merge_Tags::get_merge_tags();
60 $wpforms = wpforms()->get( 'form' )->get( $form_id, [ 'content_only' => true ] );
61
62 $excluded_field_types = array('html', 'captcha_recaptcha', 'pagebreak', 'divider');
63 $form_fields = $wpforms['fields'];
64
65 if(is_array($form_fields)){
66 foreach($form_fields as $field){
67
68 if(in_array($field['type'], $excluded_field_types)){
69 continue;
70 }
71
72 $field_name = $field['label'];
73 $field_id = $field['id'];
74 $return_type = 'text';
75
76 $merge_tags['WPForms'][] = array(
77 'id' => 'wpf_' . $form_id . '_' . $field_id,
78 'label' => $field_name,
79 'preview_value' => '',
80 'return_type' => $return_type,
81 'value' => function ( $fields ) use ( $field_id ) {
82 $value = isset($fields[$field_id][ 'value' ]) ? $fields[$field_id][ 'value' ] : '';
83 if(is_array($value)){
84 $value = implode(', ', $value);
85 }
86 return html_entity_decode(sanitize_text_field($value));
87 }
88 );
89 }
90 }
91 return $merge_tags;
92 }
93
94 /*
95 * Get recipient fields
96 */
97 public static function get_recipient_fields($form_id){
98 $wpforms = wpforms()->get( 'form' )->get( $form_id, [ 'content_only' => true ] );
99 $form_fields = $wpforms['fields'];
100
101 $recipient_fields = array();
102
103 if(is_array($form_fields)){
104 foreach($form_fields as $field){
105 if('phone' != $field['type']){
106 continue;
107 }
108
109 $field_name = $field['label'];
110 $field_id = $field['id'];
111
112 $recipient_fields['WPForms'][] = array(
113 'id' => 'wpf_' . $form_id . '_' . $field_id,
114 'label' => $field_name,
115 'value' => function ( $fields ) use ( $field_id ) {
116 $value = isset($fields[$field_id][ 'value' ]) ? $fields[$field_id][ 'value' ] : '';
117 return html_entity_decode(sanitize_text_field($value));
118 }
119 );
120 }
121 }
122 return $recipient_fields;
123 }
124 }
125